OSDN Git Service

cfc4a1593b61d7f8c9289e69f261a044d44941b2
[pf3gnuchains/gcc-fork.git] / gcc / config / m32c / m32c.c
1 /* Target Code for R8C/M16C/M32C
2    Copyright (C) 2005, 2006, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4    Contributed by Red Hat.
5
6    This file is part of GCC.
7
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 3, or (at your
11    option) any later version.
12
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.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING3.  If not see
20    <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-flags.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "toplev.h"
38 #include "obstack.h"
39 #include "tree.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "except.h"
43 #include "function.h"
44 #include "ggc.h"
45 #include "target.h"
46 #include "target-def.h"
47 #include "tm_p.h"
48 #include "langhooks.h"
49 #include "gimple.h"
50 #include "df.h"
51
52 /* Prototypes */
53
54 /* Used by m32c_pushm_popm.  */
55 typedef enum
56 {
57   PP_pushm,
58   PP_popm,
59   PP_justcount
60 } Push_Pop_Type;
61
62 static bool m32c_function_needs_enter (void);
63 static tree interrupt_handler (tree *, tree, tree, int, bool *);
64 static tree function_vector_handler (tree *, tree, tree, int, bool *);
65 static int interrupt_p (tree node);
66 static int bank_switch_p (tree node);
67 static int fast_interrupt_p (tree node);
68 static int interrupt_p (tree node);
69 static bool m32c_asm_integer (rtx, unsigned int, int);
70 static int m32c_comp_type_attributes (const_tree, const_tree);
71 static bool m32c_fixed_condition_code_regs (unsigned int *, unsigned int *);
72 static struct machine_function *m32c_init_machine_status (void);
73 static void m32c_insert_attributes (tree, tree *);
74 static bool m32c_legitimate_address_p (enum machine_mode, rtx, bool);
75 static bool m32c_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
76                                     const_tree, bool);
77 static bool m32c_promote_prototypes (const_tree);
78 static int m32c_pushm_popm (Push_Pop_Type);
79 static bool m32c_strict_argument_naming (CUMULATIVE_ARGS *);
80 static rtx m32c_struct_value_rtx (tree, int);
81 static rtx m32c_subreg (enum machine_mode, rtx, enum machine_mode, int);
82 static int need_to_save (int);
83 static rtx m32c_function_value (const_tree, const_tree, bool);
84 static rtx m32c_libcall_value (enum machine_mode, const_rtx);
85
86 int current_function_special_page_vector (rtx);
87
88 #define SYMBOL_FLAG_FUNCVEC_FUNCTION    (SYMBOL_FLAG_MACH_DEP << 0)
89
90 #define streq(a,b) (strcmp ((a), (b)) == 0)
91
92 /* Internal support routines */
93
94 /* Debugging statements are tagged with DEBUG0 only so that they can
95    be easily enabled individually, by replacing the '0' with '1' as
96    needed.  */
97 #define DEBUG0 0
98 #define DEBUG1 1
99
100 #if DEBUG0
101 /* This is needed by some of the commented-out debug statements
102    below.  */
103 static char const *class_names[LIM_REG_CLASSES] = REG_CLASS_NAMES;
104 #endif
105 static int class_contents[LIM_REG_CLASSES][1] = REG_CLASS_CONTENTS;
106
107 /* These are all to support encode_pattern().  */
108 static char pattern[30], *patternp;
109 static GTY(()) rtx patternr[30];
110 #define RTX_IS(x) (streq (pattern, x))
111
112 /* Some macros to simplify the logic throughout this file.  */
113 #define IS_MEM_REGNO(regno) ((regno) >= MEM0_REGNO && (regno) <= MEM7_REGNO)
114 #define IS_MEM_REG(rtx) (GET_CODE (rtx) == REG && IS_MEM_REGNO (REGNO (rtx)))
115
116 #define IS_CR_REGNO(regno) ((regno) >= SB_REGNO && (regno) <= PC_REGNO)
117 #define IS_CR_REG(rtx) (GET_CODE (rtx) == REG && IS_CR_REGNO (REGNO (rtx)))
118
119 /* We do most RTX matching by converting the RTX into a string, and
120    using string compares.  This vastly simplifies the logic in many of
121    the functions in this file.
122
123    On exit, pattern[] has the encoded string (use RTX_IS("...") to
124    compare it) and patternr[] has pointers to the nodes in the RTX
125    corresponding to each character in the encoded string.  The latter
126    is mostly used by print_operand().
127
128    Unrecognized patterns have '?' in them; this shows up when the
129    assembler complains about syntax errors.
130 */
131
132 static void
133 encode_pattern_1 (rtx x)
134 {
135   int i;
136
137   if (patternp == pattern + sizeof (pattern) - 2)
138     {
139       patternp[-1] = '?';
140       return;
141     }
142
143   patternr[patternp - pattern] = x;
144
145   switch (GET_CODE (x))
146     {
147     case REG:
148       *patternp++ = 'r';
149       break;
150     case SUBREG:
151       if (GET_MODE_SIZE (GET_MODE (x)) !=
152           GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
153         *patternp++ = 'S';
154       encode_pattern_1 (XEXP (x, 0));
155       break;
156     case MEM:
157       *patternp++ = 'm';
158     case CONST:
159       encode_pattern_1 (XEXP (x, 0));
160       break;
161     case PLUS:
162       *patternp++ = '+';
163       encode_pattern_1 (XEXP (x, 0));
164       encode_pattern_1 (XEXP (x, 1));
165       break;
166     case PRE_DEC:
167       *patternp++ = '>';
168       encode_pattern_1 (XEXP (x, 0));
169       break;
170     case POST_INC:
171       *patternp++ = '<';
172       encode_pattern_1 (XEXP (x, 0));
173       break;
174     case LO_SUM:
175       *patternp++ = 'L';
176       encode_pattern_1 (XEXP (x, 0));
177       encode_pattern_1 (XEXP (x, 1));
178       break;
179     case HIGH:
180       *patternp++ = 'H';
181       encode_pattern_1 (XEXP (x, 0));
182       break;
183     case SYMBOL_REF:
184       *patternp++ = 's';
185       break;
186     case LABEL_REF:
187       *patternp++ = 'l';
188       break;
189     case CODE_LABEL:
190       *patternp++ = 'c';
191       break;
192     case CONST_INT:
193     case CONST_DOUBLE:
194       *patternp++ = 'i';
195       break;
196     case UNSPEC:
197       *patternp++ = 'u';
198       *patternp++ = '0' + XCINT (x, 1, UNSPEC);
199       for (i = 0; i < XVECLEN (x, 0); i++)
200         encode_pattern_1 (XVECEXP (x, 0, i));
201       break;
202     case USE:
203       *patternp++ = 'U';
204       break;
205     case PARALLEL:
206       *patternp++ = '|';
207       for (i = 0; i < XVECLEN (x, 0); i++)
208         encode_pattern_1 (XVECEXP (x, 0, i));
209       break;
210     case EXPR_LIST:
211       *patternp++ = 'E';
212       encode_pattern_1 (XEXP (x, 0));
213       if (XEXP (x, 1))
214         encode_pattern_1 (XEXP (x, 1));
215       break;
216     default:
217       *patternp++ = '?';
218 #if DEBUG0
219       fprintf (stderr, "can't encode pattern %s\n",
220                GET_RTX_NAME (GET_CODE (x)));
221       debug_rtx (x);
222       gcc_unreachable ();
223 #endif
224       break;
225     }
226 }
227
228 static void
229 encode_pattern (rtx x)
230 {
231   patternp = pattern;
232   encode_pattern_1 (x);
233   *patternp = 0;
234 }
235
236 /* Since register names indicate the mode they're used in, we need a
237    way to determine which name to refer to the register with.  Called
238    by print_operand().  */
239
240 static const char *
241 reg_name_with_mode (int regno, enum machine_mode mode)
242 {
243   int mlen = GET_MODE_SIZE (mode);
244   if (regno == R0_REGNO && mlen == 1)
245     return "r0l";
246   if (regno == R0_REGNO && (mlen == 3 || mlen == 4))
247     return "r2r0";
248   if (regno == R0_REGNO && mlen == 6)
249     return "r2r1r0";
250   if (regno == R0_REGNO && mlen == 8)
251     return "r3r1r2r0";
252   if (regno == R1_REGNO && mlen == 1)
253     return "r1l";
254   if (regno == R1_REGNO && (mlen == 3 || mlen == 4))
255     return "r3r1";
256   if (regno == A0_REGNO && TARGET_A16 && (mlen == 3 || mlen == 4))
257     return "a1a0";
258   return reg_names[regno];
259 }
260
261 /* How many bytes a register uses on stack when it's pushed.  We need
262    to know this because the push opcode needs to explicitly indicate
263    the size of the register, even though the name of the register
264    already tells it that.  Used by m32c_output_reg_{push,pop}, which
265    is only used through calls to ASM_OUTPUT_REG_{PUSH,POP}.  */
266
267 static int
268 reg_push_size (int regno)
269 {
270   switch (regno)
271     {
272     case R0_REGNO:
273     case R1_REGNO:
274       return 2;
275     case R2_REGNO:
276     case R3_REGNO:
277     case FLG_REGNO:
278       return 2;
279     case A0_REGNO:
280     case A1_REGNO:
281     case SB_REGNO:
282     case FB_REGNO:
283     case SP_REGNO:
284       if (TARGET_A16)
285         return 2;
286       else
287         return 3;
288     default:
289       gcc_unreachable ();
290     }
291 }
292
293 static int *class_sizes = 0;
294
295 /* Given two register classes, find the largest intersection between
296    them.  If there is no intersection, return RETURNED_IF_EMPTY
297    instead.  */
298 static int
299 reduce_class (int original_class, int limiting_class, int returned_if_empty)
300 {
301   int cc = class_contents[original_class][0];
302   int i, best = NO_REGS;
303   int best_size = 0;
304
305   if (original_class == limiting_class)
306     return original_class;
307
308   if (!class_sizes)
309     {
310       int r;
311       class_sizes = (int *) xmalloc (LIM_REG_CLASSES * sizeof (int));
312       for (i = 0; i < LIM_REG_CLASSES; i++)
313         {
314           class_sizes[i] = 0;
315           for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
316             if (class_contents[i][0] & (1 << r))
317               class_sizes[i]++;
318         }
319     }
320
321   cc &= class_contents[limiting_class][0];
322   for (i = 0; i < LIM_REG_CLASSES; i++)
323     {
324       int ic = class_contents[i][0];
325
326       if ((~cc & ic) == 0)
327         if (best_size < class_sizes[i])
328           {
329             best = i;
330             best_size = class_sizes[i];
331           }
332
333     }
334   if (best == NO_REGS)
335     return returned_if_empty;
336   return best;
337 }
338
339 /* Returns TRUE If there are any registers that exist in both register
340    classes.  */
341 static int
342 classes_intersect (int class1, int class2)
343 {
344   return class_contents[class1][0] & class_contents[class2][0];
345 }
346
347 /* Used by m32c_register_move_cost to determine if a move is
348    impossibly expensive.  */
349 static int
350 class_can_hold_mode (int rclass, enum machine_mode mode)
351 {
352   /* Cache the results:  0=untested  1=no  2=yes */
353   static char results[LIM_REG_CLASSES][MAX_MACHINE_MODE];
354   if (results[rclass][mode] == 0)
355     {
356       int r, n, i;
357       results[rclass][mode] = 1;
358       for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
359         if (class_contents[rclass][0] & (1 << r)
360             && HARD_REGNO_MODE_OK (r, mode))
361           {
362             int ok = 1;
363             n = HARD_REGNO_NREGS (r, mode);
364             for (i = 1; i < n; i++)
365               if (!(class_contents[rclass][0] & (1 << (r + i))))
366                 ok = 0;
367             if (ok)
368               {
369                 results[rclass][mode] = 2;
370                 break;
371               }
372           }
373     }
374 #if DEBUG0
375   fprintf (stderr, "class %s can hold %s? %s\n",
376            class_names[rclass], mode_name[mode],
377            (results[rclass][mode] == 2) ? "yes" : "no");
378 #endif
379   return results[rclass][mode] == 2;
380 }
381
382 /* Run-time Target Specification.  */
383
384 /* Memregs are memory locations that gcc treats like general
385    registers, as there are a limited number of true registers and the
386    m32c families can use memory in most places that registers can be
387    used.
388
389    However, since memory accesses are more expensive than registers,
390    we allow the user to limit the number of memregs available, in
391    order to try to persuade gcc to try harder to use real registers.
392
393    Memregs are provided by m32c-lib1.S.
394 */
395
396 int target_memregs = 16;
397 static bool target_memregs_set = FALSE;
398 int ok_to_change_target_memregs = TRUE;
399
400 #undef  TARGET_HANDLE_OPTION
401 #define TARGET_HANDLE_OPTION m32c_handle_option
402 static bool
403 m32c_handle_option (size_t code,
404                     const char *arg ATTRIBUTE_UNUSED,
405                     int value ATTRIBUTE_UNUSED)
406 {
407   if (code == OPT_memregs_)
408     {
409       target_memregs_set = TRUE;
410       target_memregs = atoi (arg);
411     }
412   return TRUE;
413 }
414
415 /* Implements OVERRIDE_OPTIONS.  We limit memregs to 0..16, and
416    provide a default.  */
417 void
418 m32c_override_options (void)
419 {
420   if (target_memregs_set)
421     {
422       if (target_memregs < 0 || target_memregs > 16)
423         error ("invalid target memregs value '%d'", target_memregs);
424     }
425   else
426     target_memregs = 16;
427
428   if (TARGET_A24)
429     flag_ivopts = 0;
430 }
431
432 /* Defining data structures for per-function information */
433
434 /* The usual; we set up our machine_function data.  */
435 static struct machine_function *
436 m32c_init_machine_status (void)
437 {
438   struct machine_function *machine;
439   machine =
440     (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
441
442   return machine;
443 }
444
445 /* Implements INIT_EXPANDERS.  We just set up to call the above
446    function.  */
447 void
448 m32c_init_expanders (void)
449 {
450   init_machine_status = m32c_init_machine_status;
451 }
452
453 /* Storage Layout */
454
455 /* Register Basics */
456
457 /* Basic Characteristics of Registers */
458
459 /* Whether a mode fits in a register is complex enough to warrant a
460    table.  */
461 static struct
462 {
463   char qi_regs;
464   char hi_regs;
465   char pi_regs;
466   char si_regs;
467   char di_regs;
468 } nregs_table[FIRST_PSEUDO_REGISTER] =
469 {
470   { 1, 1, 2, 2, 4 },            /* r0 */
471   { 0, 1, 0, 0, 0 },            /* r2 */
472   { 1, 1, 2, 2, 0 },            /* r1 */
473   { 0, 1, 0, 0, 0 },            /* r3 */
474   { 0, 1, 1, 0, 0 },            /* a0 */
475   { 0, 1, 1, 0, 0 },            /* a1 */
476   { 0, 1, 1, 0, 0 },            /* sb */
477   { 0, 1, 1, 0, 0 },            /* fb */
478   { 0, 1, 1, 0, 0 },            /* sp */
479   { 1, 1, 1, 0, 0 },            /* pc */
480   { 0, 0, 0, 0, 0 },            /* fl */
481   { 1, 1, 1, 0, 0 },            /* ap */
482   { 1, 1, 2, 2, 4 },            /* mem0 */
483   { 1, 1, 2, 2, 4 },            /* mem1 */
484   { 1, 1, 2, 2, 4 },            /* mem2 */
485   { 1, 1, 2, 2, 4 },            /* mem3 */
486   { 1, 1, 2, 2, 4 },            /* mem4 */
487   { 1, 1, 2, 2, 0 },            /* mem5 */
488   { 1, 1, 2, 2, 0 },            /* mem6 */
489   { 1, 1, 0, 0, 0 },            /* mem7 */
490 };
491
492 /* Implements CONDITIONAL_REGISTER_USAGE.  We adjust the number of
493    available memregs, and select which registers need to be preserved
494    across calls based on the chip family.  */
495
496 void
497 m32c_conditional_register_usage (void)
498 {
499   int i;
500
501   if (0 <= target_memregs && target_memregs <= 16)
502     {
503       /* The command line option is bytes, but our "registers" are
504          16-bit words.  */
505       for (i = (target_memregs+1)/2; i < 8; i++)
506         {
507           fixed_regs[MEM0_REGNO + i] = 1;
508           CLEAR_HARD_REG_BIT (reg_class_contents[MEM_REGS], MEM0_REGNO + i);
509         }
510     }
511
512   /* M32CM and M32C preserve more registers across function calls.  */
513   if (TARGET_A24)
514     {
515       call_used_regs[R1_REGNO] = 0;
516       call_used_regs[R2_REGNO] = 0;
517       call_used_regs[R3_REGNO] = 0;
518       call_used_regs[A0_REGNO] = 0;
519       call_used_regs[A1_REGNO] = 0;
520     }
521 }
522
523 /* How Values Fit in Registers */
524
525 /* Implements HARD_REGNO_NREGS.  This is complicated by the fact that
526    different registers are different sizes from each other, *and* may
527    be different sizes in different chip families.  */
528 static int
529 m32c_hard_regno_nregs_1 (int regno, enum machine_mode mode)
530 {
531   if (regno == FLG_REGNO && mode == CCmode)
532     return 1;
533   if (regno >= FIRST_PSEUDO_REGISTER)
534     return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
535
536   if (regno >= MEM0_REGNO && regno <= MEM7_REGNO)
537     return (GET_MODE_SIZE (mode) + 1) / 2;
538
539   if (GET_MODE_SIZE (mode) <= 1)
540     return nregs_table[regno].qi_regs;
541   if (GET_MODE_SIZE (mode) <= 2)
542     return nregs_table[regno].hi_regs;
543   if (regno == A0_REGNO && mode == PSImode && TARGET_A16)
544     return 2;
545   if ((GET_MODE_SIZE (mode) <= 3 || mode == PSImode) && TARGET_A24)
546     return nregs_table[regno].pi_regs;
547   if (GET_MODE_SIZE (mode) <= 4)
548     return nregs_table[regno].si_regs;
549   if (GET_MODE_SIZE (mode) <= 8)
550     return nregs_table[regno].di_regs;
551   return 0;
552 }
553
554 int
555 m32c_hard_regno_nregs (int regno, enum machine_mode mode)
556 {
557   int rv = m32c_hard_regno_nregs_1 (regno, mode);
558   return rv ? rv : 1;
559 }
560
561 /* Implements HARD_REGNO_MODE_OK.  The above function does the work
562    already; just test its return value.  */
563 int
564 m32c_hard_regno_ok (int regno, enum machine_mode mode)
565 {
566   return m32c_hard_regno_nregs_1 (regno, mode) != 0;
567 }
568
569 /* Implements MODES_TIEABLE_P.  In general, modes aren't tieable since
570    registers are all different sizes.  However, since most modes are
571    bigger than our registers anyway, it's easier to implement this
572    function that way, leaving QImode as the only unique case.  */
573 int
574 m32c_modes_tieable_p (enum machine_mode m1, enum machine_mode m2)
575 {
576   if (GET_MODE_SIZE (m1) == GET_MODE_SIZE (m2))
577     return 1;
578
579 #if 0
580   if (m1 == QImode || m2 == QImode)
581     return 0;
582 #endif
583
584   return 1;
585 }
586
587 /* Register Classes */
588
589 /* Implements REGNO_REG_CLASS.  */
590 enum machine_mode
591 m32c_regno_reg_class (int regno)
592 {
593   switch (regno)
594     {
595     case R0_REGNO:
596       return R0_REGS;
597     case R1_REGNO:
598       return R1_REGS;
599     case R2_REGNO:
600       return R2_REGS;
601     case R3_REGNO:
602       return R3_REGS;
603     case A0_REGNO:
604     case A1_REGNO:
605       return A_REGS;
606     case SB_REGNO:
607       return SB_REGS;
608     case FB_REGNO:
609       return FB_REGS;
610     case SP_REGNO:
611       return SP_REGS;
612     case FLG_REGNO:
613       return FLG_REGS;
614     default:
615       if (IS_MEM_REGNO (regno))
616         return MEM_REGS;
617       return ALL_REGS;
618     }
619 }
620
621 /* Implements REG_CLASS_FROM_CONSTRAINT.  Note that some constraints only match
622    for certain chip families.  */
623 int
624 m32c_reg_class_from_constraint (char c ATTRIBUTE_UNUSED, const char *s)
625 {
626   if (memcmp (s, "Rsp", 3) == 0)
627     return SP_REGS;
628   if (memcmp (s, "Rfb", 3) == 0)
629     return FB_REGS;
630   if (memcmp (s, "Rsb", 3) == 0)
631     return SB_REGS;
632   if (memcmp (s, "Rcr", 3) == 0)
633     return TARGET_A16 ? CR_REGS : NO_REGS;
634   if (memcmp (s, "Rcl", 3) == 0)
635     return TARGET_A24 ? CR_REGS : NO_REGS;
636   if (memcmp (s, "R0w", 3) == 0)
637     return R0_REGS;
638   if (memcmp (s, "R1w", 3) == 0)
639     return R1_REGS;
640   if (memcmp (s, "R2w", 3) == 0)
641     return R2_REGS;
642   if (memcmp (s, "R3w", 3) == 0)
643     return R3_REGS;
644   if (memcmp (s, "R02", 3) == 0)
645     return R02_REGS;
646   if (memcmp (s, "R13", 3) == 0)
647     return R13_REGS;
648   if (memcmp (s, "R03", 3) == 0)
649     return R03_REGS;
650   if (memcmp (s, "Rdi", 3) == 0)
651     return DI_REGS;
652   if (memcmp (s, "Rhl", 3) == 0)
653     return HL_REGS;
654   if (memcmp (s, "R23", 3) == 0)
655     return R23_REGS;
656   if (memcmp (s, "Ra0", 3) == 0)
657     return A0_REGS;
658   if (memcmp (s, "Ra1", 3) == 0)
659     return A1_REGS;
660   if (memcmp (s, "Raa", 3) == 0)
661     return A_REGS;
662   if (memcmp (s, "Raw", 3) == 0)
663     return TARGET_A16 ? A_REGS : NO_REGS;
664   if (memcmp (s, "Ral", 3) == 0)
665     return TARGET_A24 ? A_REGS : NO_REGS;
666   if (memcmp (s, "Rqi", 3) == 0)
667     return QI_REGS;
668   if (memcmp (s, "Rad", 3) == 0)
669     return AD_REGS;
670   if (memcmp (s, "Rsi", 3) == 0)
671     return SI_REGS;
672   if (memcmp (s, "Rhi", 3) == 0)
673     return HI_REGS;
674   if (memcmp (s, "Rhc", 3) == 0)
675     return HC_REGS;
676   if (memcmp (s, "Rra", 3) == 0)
677     return RA_REGS;
678   if (memcmp (s, "Rfl", 3) == 0)
679     return FLG_REGS;
680   if (memcmp (s, "Rmm", 3) == 0)
681     {
682       if (fixed_regs[MEM0_REGNO])
683         return NO_REGS;
684       return MEM_REGS;
685     }
686
687   /* PSImode registers - i.e. whatever can hold a pointer.  */
688   if (memcmp (s, "Rpi", 3) == 0)
689     {
690       if (TARGET_A16)
691         return HI_REGS;
692       else
693         return RA_REGS; /* r2r0 and r3r1 can hold pointers.  */
694     }
695
696   /* We handle this one as an EXTRA_CONSTRAINT.  */
697   if (memcmp (s, "Rpa", 3) == 0)
698     return NO_REGS;
699
700   if (*s == 'R')
701     {
702       fprintf(stderr, "unrecognized R constraint: %.3s\n", s);
703       gcc_unreachable();
704     }
705
706   return NO_REGS;
707 }
708
709 /* Implements REGNO_OK_FOR_BASE_P.  */
710 int
711 m32c_regno_ok_for_base_p (int regno)
712 {
713   if (regno == A0_REGNO
714       || regno == A1_REGNO || regno >= FIRST_PSEUDO_REGISTER)
715     return 1;
716   return 0;
717 }
718
719 #define DEBUG_RELOAD 0
720
721 /* Implements PREFERRED_RELOAD_CLASS.  In general, prefer general
722    registers of the appropriate size.  */
723 int
724 m32c_preferred_reload_class (rtx x, int rclass)
725 {
726   int newclass = rclass;
727
728 #if DEBUG_RELOAD
729   fprintf (stderr, "\npreferred_reload_class for %s is ",
730            class_names[rclass]);
731 #endif
732   if (rclass == NO_REGS)
733     rclass = GET_MODE (x) == QImode ? HL_REGS : R03_REGS;
734
735   if (classes_intersect (rclass, CR_REGS))
736     {
737       switch (GET_MODE (x))
738         {
739         case QImode:
740           newclass = HL_REGS;
741           break;
742         default:
743           /*      newclass = HI_REGS; */
744           break;
745         }
746     }
747
748   else if (newclass == QI_REGS && GET_MODE_SIZE (GET_MODE (x)) > 2)
749     newclass = SI_REGS;
750   else if (GET_MODE_SIZE (GET_MODE (x)) > 4
751            && ~class_contents[rclass][0] & 0x000f)
752     newclass = DI_REGS;
753
754   rclass = reduce_class (rclass, newclass, rclass);
755
756   if (GET_MODE (x) == QImode)
757     rclass = reduce_class (rclass, HL_REGS, rclass);
758
759 #if DEBUG_RELOAD
760   fprintf (stderr, "%s\n", class_names[rclass]);
761   debug_rtx (x);
762
763   if (GET_CODE (x) == MEM
764       && GET_CODE (XEXP (x, 0)) == PLUS
765       && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
766     fprintf (stderr, "Glorm!\n");
767 #endif
768   return rclass;
769 }
770
771 /* Implements PREFERRED_OUTPUT_RELOAD_CLASS.  */
772 int
773 m32c_preferred_output_reload_class (rtx x, int rclass)
774 {
775   return m32c_preferred_reload_class (x, rclass);
776 }
777
778 /* Implements LIMIT_RELOAD_CLASS.  We basically want to avoid using
779    address registers for reloads since they're needed for address
780    reloads.  */
781 int
782 m32c_limit_reload_class (enum machine_mode mode, int rclass)
783 {
784 #if DEBUG_RELOAD
785   fprintf (stderr, "limit_reload_class for %s: %s ->",
786            mode_name[mode], class_names[rclass]);
787 #endif
788
789   if (mode == QImode)
790     rclass = reduce_class (rclass, HL_REGS, rclass);
791   else if (mode == HImode)
792     rclass = reduce_class (rclass, HI_REGS, rclass);
793   else if (mode == SImode)
794     rclass = reduce_class (rclass, SI_REGS, rclass);
795
796   if (rclass != A_REGS)
797     rclass = reduce_class (rclass, DI_REGS, rclass);
798
799 #if DEBUG_RELOAD
800   fprintf (stderr, " %s\n", class_names[rclass]);
801 #endif
802   return rclass;
803 }
804
805 /* Implements SECONDARY_RELOAD_CLASS.  QImode have to be reloaded in
806    r0 or r1, as those are the only real QImode registers.  CR regs get
807    reloaded through appropriately sized general or address
808    registers.  */
809 int
810 m32c_secondary_reload_class (int rclass, enum machine_mode mode, rtx x)
811 {
812   int cc = class_contents[rclass][0];
813 #if DEBUG0
814   fprintf (stderr, "\nsecondary reload class %s %s\n",
815            class_names[rclass], mode_name[mode]);
816   debug_rtx (x);
817 #endif
818   if (mode == QImode
819       && GET_CODE (x) == MEM && (cc & ~class_contents[R23_REGS][0]) == 0)
820     return QI_REGS;
821   if (classes_intersect (rclass, CR_REGS)
822       && GET_CODE (x) == REG
823       && REGNO (x) >= SB_REGNO && REGNO (x) <= SP_REGNO)
824     return TARGET_A16 ? HI_REGS : A_REGS;
825   return NO_REGS;
826 }
827
828 /* Implements CLASS_LIKELY_SPILLED_P.  A_REGS is needed for address
829    reloads.  */
830 int
831 m32c_class_likely_spilled_p (int regclass)
832 {
833   if (regclass == A_REGS)
834     return 1;
835   return reg_class_size[regclass] == 1;
836 }
837
838 /* Implements CLASS_MAX_NREGS.  We calculate this according to its
839    documented meaning, to avoid potential inconsistencies with actual
840    class definitions.  */
841 int
842 m32c_class_max_nregs (int regclass, enum machine_mode mode)
843 {
844   int rn, max = 0;
845
846   for (rn = 0; rn < FIRST_PSEUDO_REGISTER; rn++)
847     if (class_contents[regclass][0] & (1 << rn))
848       {
849         int n = m32c_hard_regno_nregs (rn, mode);
850         if (max < n)
851           max = n;
852       }
853   return max;
854 }
855
856 /* Implements CANNOT_CHANGE_MODE_CLASS.  Only r0 and r1 can change to
857    QI (r0l, r1l) because the chip doesn't support QI ops on other
858    registers (well, it does on a0/a1 but if we let gcc do that, reload
859    suffers).  Otherwise, we allow changes to larger modes.  */
860 int
861 m32c_cannot_change_mode_class (enum machine_mode from,
862                                enum machine_mode to, int rclass)
863 {
864   int rn;
865 #if DEBUG0
866   fprintf (stderr, "cannot change from %s to %s in %s\n",
867            mode_name[from], mode_name[to], class_names[rclass]);
868 #endif
869
870   /* If the larger mode isn't allowed in any of these registers, we
871      can't allow the change.  */
872   for (rn = 0; rn < FIRST_PSEUDO_REGISTER; rn++)
873     if (class_contents[rclass][0] & (1 << rn))
874       if (! m32c_hard_regno_ok (rn, to))
875         return 1;
876
877   if (to == QImode)
878     return (class_contents[rclass][0] & 0x1ffa);
879
880   if (class_contents[rclass][0] & 0x0005        /* r0, r1 */
881       && GET_MODE_SIZE (from) > 1)
882     return 0;
883   if (GET_MODE_SIZE (from) > 2) /* all other regs */
884     return 0;
885
886   return 1;
887 }
888
889 /* Helpers for the rest of the file.  */
890 /* TRUE if the rtx is a REG rtx for the given register.  */
891 #define IS_REG(rtx,regno) (GET_CODE (rtx) == REG \
892                            && REGNO (rtx) == regno)
893 /* TRUE if the rtx is a pseudo - specifically, one we can use as a
894    base register in address calculations (hence the "strict"
895    argument).  */
896 #define IS_PSEUDO(rtx,strict) (!strict && GET_CODE (rtx) == REG \
897                                && (REGNO (rtx) == AP_REGNO \
898                                    || REGNO (rtx) >= FIRST_PSEUDO_REGISTER))
899
900 /* Implements CONST_OK_FOR_CONSTRAINT_P.  Currently, all constant
901    constraints start with 'I', with the next two characters indicating
902    the type and size of the range allowed.  */
903 int
904 m32c_const_ok_for_constraint_p (HOST_WIDE_INT value,
905                                 char c ATTRIBUTE_UNUSED, const char *str)
906 {
907   /* s=signed u=unsigned n=nonzero m=minus l=log2able,
908      [sun] bits [SUN] bytes, p=pointer size
909      I[-0-9][0-9] matches that number */
910   if (memcmp (str, "Is3", 3) == 0)
911     {
912       return (-8 <= value && value <= 7);
913     }
914   if (memcmp (str, "IS1", 3) == 0)
915     {
916       return (-128 <= value && value <= 127);
917     }
918   if (memcmp (str, "IS2", 3) == 0)
919     {
920       return (-32768 <= value && value <= 32767);
921     }
922   if (memcmp (str, "IU2", 3) == 0)
923     {
924       return (0 <= value && value <= 65535);
925     }
926   if (memcmp (str, "IU3", 3) == 0)
927     {
928       return (0 <= value && value <= 0x00ffffff);
929     }
930   if (memcmp (str, "In4", 3) == 0)
931     {
932       return (-8 <= value && value && value <= 8);
933     }
934   if (memcmp (str, "In5", 3) == 0)
935     {
936       return (-16 <= value && value && value <= 16);
937     }
938   if (memcmp (str, "In6", 3) == 0)
939     {
940       return (-32 <= value && value && value <= 32);
941     }
942   if (memcmp (str, "IM2", 3) == 0)
943     {
944       return (-65536 <= value && value && value <= -1);
945     }
946   if (memcmp (str, "Ilb", 3) == 0)
947     {
948       int b = exact_log2 (value);
949       return (b >= 0 && b <= 7);
950     }
951   if (memcmp (str, "Imb", 3) == 0)
952     {
953       int b = exact_log2 ((value ^ 0xff) & 0xff);
954       return (b >= 0 && b <= 7);
955     }
956   if (memcmp (str, "ImB", 3) == 0)
957     {
958       int b = exact_log2 ((value ^ 0xffff) & 0xffff);
959       return (b >= 0 && b <= 7);
960     }
961   if (memcmp (str, "Ilw", 3) == 0)
962     {
963       int b = exact_log2 (value);
964       return (b >= 0 && b <= 15);
965     }
966   if (memcmp (str, "Imw", 3) == 0)
967     {
968       int b = exact_log2 ((value ^ 0xffff) & 0xffff);
969       return (b >= 0 && b <= 15);
970     }
971   if (memcmp (str, "I00", 3) == 0)
972     {
973       return (value == 0);
974     }
975   return 0;
976 }
977
978 /* Implements EXTRA_CONSTRAINT_STR (see next function too).  'S' is
979    for memory constraints, plus "Rpa" for PARALLEL rtx's we use for
980    call return values.  */
981 int
982 m32c_extra_constraint_p2 (rtx value, char c ATTRIBUTE_UNUSED, const char *str)
983 {
984   encode_pattern (value);
985   if (memcmp (str, "Sd", 2) == 0)
986     {
987       /* This is the common "src/dest" address */
988       rtx r;
989       if (GET_CODE (value) == MEM && CONSTANT_P (XEXP (value, 0)))
990         return 1;
991       if (RTX_IS ("ms") || RTX_IS ("m+si"))
992         return 1;
993       if (RTX_IS ("m++rii"))
994         {
995           if (REGNO (patternr[3]) == FB_REGNO
996               && INTVAL (patternr[4]) == 0)
997             return 1;
998         }
999       if (RTX_IS ("mr"))
1000         r = patternr[1];
1001       else if (RTX_IS ("m+ri") || RTX_IS ("m+rs") || RTX_IS ("m+r+si"))
1002         r = patternr[2];
1003       else
1004         return 0;
1005       if (REGNO (r) == SP_REGNO)
1006         return 0;
1007       return m32c_legitimate_address_p (GET_MODE (value), XEXP (value, 0), 1);
1008     }
1009   else if (memcmp (str, "Sa", 2) == 0)
1010     {
1011       rtx r;
1012       if (RTX_IS ("mr"))
1013         r = patternr[1];
1014       else if (RTX_IS ("m+ri"))
1015         r = patternr[2];
1016       else
1017         return 0;
1018       return (IS_REG (r, A0_REGNO) || IS_REG (r, A1_REGNO));
1019     }
1020   else if (memcmp (str, "Si", 2) == 0)
1021     {
1022       return (RTX_IS ("mi") || RTX_IS ("ms") || RTX_IS ("m+si"));
1023     }
1024   else if (memcmp (str, "Ss", 2) == 0)
1025     {
1026       return ((RTX_IS ("mr")
1027                && (IS_REG (patternr[1], SP_REGNO)))
1028               || (RTX_IS ("m+ri") && (IS_REG (patternr[2], SP_REGNO))));
1029     }
1030   else if (memcmp (str, "Sf", 2) == 0)
1031     {
1032       return ((RTX_IS ("mr")
1033                && (IS_REG (patternr[1], FB_REGNO)))
1034               || (RTX_IS ("m+ri") && (IS_REG (patternr[2], FB_REGNO))));
1035     }
1036   else if (memcmp (str, "Sb", 2) == 0)
1037     {
1038       return ((RTX_IS ("mr")
1039                && (IS_REG (patternr[1], SB_REGNO)))
1040               || (RTX_IS ("m+ri") && (IS_REG (patternr[2], SB_REGNO))));
1041     }
1042   else if (memcmp (str, "Sp", 2) == 0)
1043     {
1044       /* Absolute addresses 0..0x1fff used for bit addressing (I/O ports) */
1045       return (RTX_IS ("mi")
1046               && !(INTVAL (patternr[1]) & ~0x1fff));
1047     }
1048   else if (memcmp (str, "S1", 2) == 0)
1049     {
1050       return r1h_operand (value, QImode);
1051     }
1052
1053   gcc_assert (str[0] != 'S');
1054
1055   if (memcmp (str, "Rpa", 2) == 0)
1056     return GET_CODE (value) == PARALLEL;
1057
1058   return 0;
1059 }
1060
1061 /* This is for when we're debugging the above.  */
1062 int
1063 m32c_extra_constraint_p (rtx value, char c, const char *str)
1064 {
1065   int rv = m32c_extra_constraint_p2 (value, c, str);
1066 #if DEBUG0
1067   fprintf (stderr, "\nconstraint %.*s: %d\n", CONSTRAINT_LEN (c, str), str,
1068            rv);
1069   debug_rtx (value);
1070 #endif
1071   return rv;
1072 }
1073
1074 /* Implements EXTRA_MEMORY_CONSTRAINT.  Currently, we only use strings
1075    starting with 'S'.  */
1076 int
1077 m32c_extra_memory_constraint (char c, const char *str ATTRIBUTE_UNUSED)
1078 {
1079   return c == 'S';
1080 }
1081
1082 /* Implements EXTRA_ADDRESS_CONSTRAINT.  We reserve 'A' strings for these,
1083    but don't currently define any.  */
1084 int
1085 m32c_extra_address_constraint (char c, const char *str ATTRIBUTE_UNUSED)
1086 {
1087   return c == 'A';
1088 }
1089
1090 /* STACK AND CALLING */
1091
1092 /* Frame Layout */
1093
1094 /* Implements RETURN_ADDR_RTX.  Note that R8C and M16C push 24 bits
1095    (yes, THREE bytes) onto the stack for the return address, but we
1096    don't support pointers bigger than 16 bits on those chips.  This
1097    will likely wreak havoc with exception unwinding.  FIXME.  */
1098 rtx
1099 m32c_return_addr_rtx (int count)
1100 {
1101   enum machine_mode mode;
1102   int offset;
1103   rtx ra_mem;
1104
1105   if (count)
1106     return NULL_RTX;
1107   /* we want 2[$fb] */
1108
1109   if (TARGET_A24)
1110     {
1111       /* It's four bytes */
1112       mode = PSImode;
1113       offset = 4;
1114     }
1115   else
1116     {
1117       /* FIXME: it's really 3 bytes */
1118       mode = HImode;
1119       offset = 2;
1120     }
1121
1122   ra_mem =
1123     gen_rtx_MEM (mode, plus_constant (gen_rtx_REG (Pmode, FP_REGNO), offset));
1124   return copy_to_mode_reg (mode, ra_mem);
1125 }
1126
1127 /* Implements INCOMING_RETURN_ADDR_RTX.  See comment above.  */
1128 rtx
1129 m32c_incoming_return_addr_rtx (void)
1130 {
1131   /* we want [sp] */
1132   return gen_rtx_MEM (PSImode, gen_rtx_REG (PSImode, SP_REGNO));
1133 }
1134
1135 /* Exception Handling Support */
1136
1137 /* Implements EH_RETURN_DATA_REGNO.  Choose registers able to hold
1138    pointers.  */
1139 int
1140 m32c_eh_return_data_regno (int n)
1141 {
1142   switch (n)
1143     {
1144     case 0:
1145       return A0_REGNO;
1146     case 1:
1147       if (TARGET_A16)
1148         return R3_REGNO;
1149       else
1150         return R1_REGNO;
1151     default:
1152       return INVALID_REGNUM;
1153     }
1154 }
1155
1156 /* Implements EH_RETURN_STACKADJ_RTX.  Saved and used later in
1157    m32c_emit_eh_epilogue.  */
1158 rtx
1159 m32c_eh_return_stackadj_rtx (void)
1160 {
1161   if (!cfun->machine->eh_stack_adjust)
1162     {
1163       rtx sa;
1164
1165       sa = gen_rtx_REG (Pmode, R0_REGNO);
1166       cfun->machine->eh_stack_adjust = sa;
1167     }
1168   return cfun->machine->eh_stack_adjust;
1169 }
1170
1171 /* Registers That Address the Stack Frame */
1172
1173 /* Implements DWARF_FRAME_REGNUM and DBX_REGISTER_NUMBER.  Note that
1174    the original spec called for dwarf numbers to vary with register
1175    width as well, for example, r0l, r0, and r2r0 would each have
1176    different dwarf numbers.  GCC doesn't support this, and we don't do
1177    it, and gdb seems to like it this way anyway.  */
1178 unsigned int
1179 m32c_dwarf_frame_regnum (int n)
1180 {
1181   switch (n)
1182     {
1183     case R0_REGNO:
1184       return 5;
1185     case R1_REGNO:
1186       return 6;
1187     case R2_REGNO:
1188       return 7;
1189     case R3_REGNO:
1190       return 8;
1191     case A0_REGNO:
1192       return 9;
1193     case A1_REGNO:
1194       return 10;
1195     case FB_REGNO:
1196       return 11;
1197     case SB_REGNO:
1198       return 19;
1199
1200     case SP_REGNO:
1201       return 12;
1202     case PC_REGNO:
1203       return 13;
1204     default:
1205       return DWARF_FRAME_REGISTERS + 1;
1206     }
1207 }
1208
1209 /* The frame looks like this:
1210
1211    ap -> +------------------------------
1212          | Return address (3 or 4 bytes)
1213          | Saved FB (2 or 4 bytes)
1214    fb -> +------------------------------
1215          | local vars
1216          | register saves fb
1217          |        through r0 as needed
1218    sp -> +------------------------------
1219 */
1220
1221 /* We use this to wrap all emitted insns in the prologue.  */
1222 static rtx
1223 F (rtx x)
1224 {
1225   RTX_FRAME_RELATED_P (x) = 1;
1226   return x;
1227 }
1228
1229 /* This maps register numbers to the PUSHM/POPM bitfield, and tells us
1230    how much the stack pointer moves for each, for each cpu family.  */
1231 static struct
1232 {
1233   int reg1;
1234   int bit;
1235   int a16_bytes;
1236   int a24_bytes;
1237 } pushm_info[] =
1238 {
1239   /* These are in reverse push (nearest-to-sp) order.  */
1240   { R0_REGNO, 0x80, 2, 2 },
1241   { R1_REGNO, 0x40, 2, 2 },
1242   { R2_REGNO, 0x20, 2, 2 },
1243   { R3_REGNO, 0x10, 2, 2 },
1244   { A0_REGNO, 0x08, 2, 4 },
1245   { A1_REGNO, 0x04, 2, 4 },
1246   { SB_REGNO, 0x02, 2, 4 },
1247   { FB_REGNO, 0x01, 2, 4 }
1248 };
1249
1250 #define PUSHM_N (sizeof(pushm_info)/sizeof(pushm_info[0]))
1251
1252 /* Returns TRUE if we need to save/restore the given register.  We
1253    save everything for exception handlers, so that any register can be
1254    unwound.  For interrupt handlers, we save everything if the handler
1255    calls something else (because we don't know what *that* function
1256    might do), but try to be a bit smarter if the handler is a leaf
1257    function.  We always save $a0, though, because we use that in the
1258    epilogue to copy $fb to $sp.  */
1259 static int
1260 need_to_save (int regno)
1261 {
1262   if (fixed_regs[regno])
1263     return 0;
1264   if (crtl->calls_eh_return)
1265     return 1;
1266   if (regno == FP_REGNO)
1267     return 0;
1268   if (cfun->machine->is_interrupt
1269       && (!cfun->machine->is_leaf
1270           || (regno == A0_REGNO
1271               && m32c_function_needs_enter ())
1272           ))
1273     return 1;
1274   if (df_regs_ever_live_p (regno)
1275       && (!call_used_regs[regno] || cfun->machine->is_interrupt))
1276     return 1;
1277   return 0;
1278 }
1279
1280 /* This function contains all the intelligence about saving and
1281    restoring registers.  It always figures out the register save set.
1282    When called with PP_justcount, it merely returns the size of the
1283    save set (for eliminating the frame pointer, for example).  When
1284    called with PP_pushm or PP_popm, it emits the appropriate
1285    instructions for saving (pushm) or restoring (popm) the
1286    registers.  */
1287 static int
1288 m32c_pushm_popm (Push_Pop_Type ppt)
1289 {
1290   int reg_mask = 0;
1291   int byte_count = 0, bytes;
1292   int i;
1293   rtx dwarf_set[PUSHM_N];
1294   int n_dwarfs = 0;
1295   int nosave_mask = 0;
1296
1297   if (crtl->return_rtx
1298       && GET_CODE (crtl->return_rtx) == PARALLEL
1299       && !(crtl->calls_eh_return || cfun->machine->is_interrupt))
1300     {
1301       rtx exp = XVECEXP (crtl->return_rtx, 0, 0);
1302       rtx rv = XEXP (exp, 0);
1303       int rv_bytes = GET_MODE_SIZE (GET_MODE (rv));
1304
1305       if (rv_bytes > 2)
1306         nosave_mask |= 0x20;    /* PSI, SI */
1307       else
1308         nosave_mask |= 0xf0;    /* DF */
1309       if (rv_bytes > 4)
1310         nosave_mask |= 0x50;    /* DI */
1311     }
1312
1313   for (i = 0; i < (int) PUSHM_N; i++)
1314     {
1315       /* Skip if neither register needs saving.  */
1316       if (!need_to_save (pushm_info[i].reg1))
1317         continue;
1318
1319       if (pushm_info[i].bit & nosave_mask)
1320         continue;
1321
1322       reg_mask |= pushm_info[i].bit;
1323       bytes = TARGET_A16 ? pushm_info[i].a16_bytes : pushm_info[i].a24_bytes;
1324
1325       if (ppt == PP_pushm)
1326         {
1327           enum machine_mode mode = (bytes == 2) ? HImode : SImode;
1328           rtx addr;
1329
1330           /* Always use stack_pointer_rtx instead of calling
1331              rtx_gen_REG ourselves.  Code elsewhere in GCC assumes
1332              that there is a single rtx representing the stack pointer,
1333              namely stack_pointer_rtx, and uses == to recognize it.  */
1334           addr = stack_pointer_rtx;
1335
1336           if (byte_count != 0)
1337             addr = gen_rtx_PLUS (GET_MODE (addr), addr, GEN_INT (byte_count));
1338
1339           dwarf_set[n_dwarfs++] =
1340             gen_rtx_SET (VOIDmode,
1341                          gen_rtx_MEM (mode, addr),
1342                          gen_rtx_REG (mode, pushm_info[i].reg1));
1343           F (dwarf_set[n_dwarfs - 1]);
1344
1345         }
1346       byte_count += bytes;
1347     }
1348
1349   if (cfun->machine->is_interrupt)
1350     {
1351       cfun->machine->intr_pushm = reg_mask & 0xfe;
1352       reg_mask = 0;
1353       byte_count = 0;
1354     }
1355
1356   if (cfun->machine->is_interrupt)
1357     for (i = MEM0_REGNO; i <= MEM7_REGNO; i++)
1358       if (need_to_save (i))
1359         {
1360           byte_count += 2;
1361           cfun->machine->intr_pushmem[i - MEM0_REGNO] = 1;
1362         }
1363
1364   if (ppt == PP_pushm && byte_count)
1365     {
1366       rtx note = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (n_dwarfs + 1));
1367       rtx pushm;
1368
1369       if (reg_mask)
1370         {
1371           XVECEXP (note, 0, 0)
1372             = gen_rtx_SET (VOIDmode,
1373                            stack_pointer_rtx,
1374                            gen_rtx_PLUS (GET_MODE (stack_pointer_rtx),
1375                                          stack_pointer_rtx,
1376                                          GEN_INT (-byte_count)));
1377           F (XVECEXP (note, 0, 0));
1378
1379           for (i = 0; i < n_dwarfs; i++)
1380             XVECEXP (note, 0, i + 1) = dwarf_set[i];
1381
1382           pushm = F (emit_insn (gen_pushm (GEN_INT (reg_mask))));
1383
1384           REG_NOTES (pushm) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, note,
1385                                                  REG_NOTES (pushm));
1386         }
1387
1388       if (cfun->machine->is_interrupt)
1389         for (i = MEM0_REGNO; i <= MEM7_REGNO; i++)
1390           if (cfun->machine->intr_pushmem[i - MEM0_REGNO])
1391             {
1392               if (TARGET_A16)
1393                 pushm = emit_insn (gen_pushhi_16 (gen_rtx_REG (HImode, i)));
1394               else
1395                 pushm = emit_insn (gen_pushhi_24 (gen_rtx_REG (HImode, i)));
1396               F (pushm);
1397             }
1398     }
1399   if (ppt == PP_popm && byte_count)
1400     {
1401       if (cfun->machine->is_interrupt)
1402         for (i = MEM7_REGNO; i >= MEM0_REGNO; i--)
1403           if (cfun->machine->intr_pushmem[i - MEM0_REGNO])
1404             {
1405               if (TARGET_A16)
1406                 emit_insn (gen_pophi_16 (gen_rtx_REG (HImode, i)));
1407               else
1408                 emit_insn (gen_pophi_24 (gen_rtx_REG (HImode, i)));
1409             }
1410       if (reg_mask)
1411         emit_insn (gen_popm (GEN_INT (reg_mask)));
1412     }
1413
1414   return byte_count;
1415 }
1416
1417 /* Implements INITIAL_ELIMINATION_OFFSET.  See the comment above that
1418    diagrams our call frame.  */
1419 int
1420 m32c_initial_elimination_offset (int from, int to)
1421 {
1422   int ofs = 0;
1423
1424   if (from == AP_REGNO)
1425     {
1426       if (TARGET_A16)
1427         ofs += 5;
1428       else
1429         ofs += 8;
1430     }
1431
1432   if (to == SP_REGNO)
1433     {
1434       ofs += m32c_pushm_popm (PP_justcount);
1435       ofs += get_frame_size ();
1436     }
1437
1438   /* Account for push rounding.  */
1439   if (TARGET_A24)
1440     ofs = (ofs + 1) & ~1;
1441 #if DEBUG0
1442   fprintf (stderr, "initial_elimination_offset from=%d to=%d, ofs=%d\n", from,
1443            to, ofs);
1444 #endif
1445   return ofs;
1446 }
1447
1448 /* Passing Function Arguments on the Stack */
1449
1450 /* Implements PUSH_ROUNDING.  The R8C and M16C have byte stacks, the
1451    M32C has word stacks.  */
1452 int
1453 m32c_push_rounding (int n)
1454 {
1455   if (TARGET_R8C || TARGET_M16C)
1456     return n;
1457   return (n + 1) & ~1;
1458 }
1459
1460 /* Passing Arguments in Registers */
1461
1462 /* Implements FUNCTION_ARG.  Arguments are passed partly in registers,
1463    partly on stack.  If our function returns a struct, a pointer to a
1464    buffer for it is at the top of the stack (last thing pushed).  The
1465    first few real arguments may be in registers as follows:
1466
1467    R8C/M16C:    arg1 in r1 if it's QI or HI (else it's pushed on stack)
1468                 arg2 in r2 if it's HI (else pushed on stack)
1469                 rest on stack
1470    M32C:        arg1 in r0 if it's QI or HI (else it's pushed on stack)
1471                 rest on stack
1472
1473    Structs are not passed in registers, even if they fit.  Only
1474    integer and pointer types are passed in registers.
1475
1476    Note that when arg1 doesn't fit in r1, arg2 may still be passed in
1477    r2 if it fits.  */
1478 rtx
1479 m32c_function_arg (CUMULATIVE_ARGS * ca,
1480                    enum machine_mode mode, tree type, int named)
1481 {
1482   /* Can return a reg, parallel, or 0 for stack */
1483   rtx rv = NULL_RTX;
1484 #if DEBUG0
1485   fprintf (stderr, "func_arg %d (%s, %d)\n",
1486            ca->parm_num, mode_name[mode], named);
1487   debug_tree (type);
1488 #endif
1489
1490   if (mode == VOIDmode)
1491     return GEN_INT (0);
1492
1493   if (ca->force_mem || !named)
1494     {
1495 #if DEBUG0
1496       fprintf (stderr, "func arg: force %d named %d, mem\n", ca->force_mem,
1497                named);
1498 #endif
1499       return NULL_RTX;
1500     }
1501
1502   if (type && INTEGRAL_TYPE_P (type) && POINTER_TYPE_P (type))
1503     return NULL_RTX;
1504
1505   if (type && AGGREGATE_TYPE_P (type))
1506     return NULL_RTX;
1507
1508   switch (ca->parm_num)
1509     {
1510     case 1:
1511       if (GET_MODE_SIZE (mode) == 1 || GET_MODE_SIZE (mode) == 2)
1512         rv = gen_rtx_REG (mode, TARGET_A16 ? R1_REGNO : R0_REGNO);
1513       break;
1514
1515     case 2:
1516       if (TARGET_A16 && GET_MODE_SIZE (mode) == 2)
1517         rv = gen_rtx_REG (mode, R2_REGNO);
1518       break;
1519     }
1520
1521 #if DEBUG0
1522   debug_rtx (rv);
1523 #endif
1524   return rv;
1525 }
1526
1527 #undef TARGET_PASS_BY_REFERENCE
1528 #define TARGET_PASS_BY_REFERENCE m32c_pass_by_reference
1529 static bool
1530 m32c_pass_by_reference (CUMULATIVE_ARGS * ca ATTRIBUTE_UNUSED,
1531                         enum machine_mode mode ATTRIBUTE_UNUSED,
1532                         const_tree type ATTRIBUTE_UNUSED,
1533                         bool named ATTRIBUTE_UNUSED)
1534 {
1535   return 0;
1536 }
1537
1538 /* Implements INIT_CUMULATIVE_ARGS.  */
1539 void
1540 m32c_init_cumulative_args (CUMULATIVE_ARGS * ca,
1541                            tree fntype,
1542                            rtx libname ATTRIBUTE_UNUSED,
1543                            tree fndecl,
1544                            int n_named_args ATTRIBUTE_UNUSED)
1545 {
1546   if (fntype && aggregate_value_p (TREE_TYPE (fntype), fndecl))
1547     ca->force_mem = 1;
1548   else
1549     ca->force_mem = 0;
1550   ca->parm_num = 1;
1551 }
1552
1553 /* Implements FUNCTION_ARG_ADVANCE.  force_mem is set for functions
1554    returning structures, so we always reset that.  Otherwise, we only
1555    need to know the sequence number of the argument to know what to do
1556    with it.  */
1557 void
1558 m32c_function_arg_advance (CUMULATIVE_ARGS * ca,
1559                            enum machine_mode mode ATTRIBUTE_UNUSED,
1560                            tree type ATTRIBUTE_UNUSED,
1561                            int named ATTRIBUTE_UNUSED)
1562 {
1563   if (ca->force_mem)
1564     ca->force_mem = 0;
1565   else
1566     ca->parm_num++;
1567 }
1568
1569 /* Implements FUNCTION_ARG_REGNO_P.  */
1570 int
1571 m32c_function_arg_regno_p (int r)
1572 {
1573   if (TARGET_A24)
1574     return (r == R0_REGNO);
1575   return (r == R1_REGNO || r == R2_REGNO);
1576 }
1577
1578 /* HImode and PSImode are the two "native" modes as far as GCC is
1579    concerned, but the chips also support a 32-bit mode which is used
1580    for some opcodes in R8C/M16C and for reset vectors and such.  */
1581 #undef TARGET_VALID_POINTER_MODE
1582 #define TARGET_VALID_POINTER_MODE m32c_valid_pointer_mode
1583 static bool
1584 m32c_valid_pointer_mode (enum machine_mode mode)
1585 {
1586   if (mode == HImode
1587       || mode == PSImode
1588       || mode == SImode
1589       )
1590     return 1;
1591   return 0;
1592 }
1593
1594 /* How Scalar Function Values Are Returned */
1595
1596 /* Implements TARGET_LIBCALL_VALUE.  Most values are returned in $r0, or some
1597    combination of registers starting there (r2r0 for longs, r3r1r2r0
1598    for long long, r3r2r1r0 for doubles), except that that ABI
1599    currently doesn't work because it ends up using all available
1600    general registers and gcc often can't compile it.  So, instead, we
1601    return anything bigger than 16 bits in "mem0" (effectively, a
1602    memory location).  */
1603
1604 #undef TARGET_LIBCALL_VALUE
1605 #define TARGET_LIBCALL_VALUE m32c_libcall_value
1606
1607 static rtx
1608 m32c_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
1609 {
1610   /* return reg or parallel */
1611 #if 0
1612   /* FIXME: GCC has difficulty returning large values in registers,
1613      because that ties up most of the general registers and gives the
1614      register allocator little to work with.  Until we can resolve
1615      this, large values are returned in memory.  */
1616   if (mode == DFmode)
1617     {
1618       rtx rv;
1619
1620       rv = gen_rtx_PARALLEL (mode, rtvec_alloc (4));
1621       XVECEXP (rv, 0, 0) = gen_rtx_EXPR_LIST (VOIDmode,
1622                                               gen_rtx_REG (HImode,
1623                                                            R0_REGNO),
1624                                               GEN_INT (0));
1625       XVECEXP (rv, 0, 1) = gen_rtx_EXPR_LIST (VOIDmode,
1626                                               gen_rtx_REG (HImode,
1627                                                            R1_REGNO),
1628                                               GEN_INT (2));
1629       XVECEXP (rv, 0, 2) = gen_rtx_EXPR_LIST (VOIDmode,
1630                                               gen_rtx_REG (HImode,
1631                                                            R2_REGNO),
1632                                               GEN_INT (4));
1633       XVECEXP (rv, 0, 3) = gen_rtx_EXPR_LIST (VOIDmode,
1634                                               gen_rtx_REG (HImode,
1635                                                            R3_REGNO),
1636                                               GEN_INT (6));
1637       return rv;
1638     }
1639
1640   if (TARGET_A24 && GET_MODE_SIZE (mode) > 2)
1641     {
1642       rtx rv;
1643
1644       rv = gen_rtx_PARALLEL (mode, rtvec_alloc (1));
1645       XVECEXP (rv, 0, 0) = gen_rtx_EXPR_LIST (VOIDmode,
1646                                               gen_rtx_REG (mode,
1647                                                            R0_REGNO),
1648                                               GEN_INT (0));
1649       return rv;
1650     }
1651 #endif
1652
1653   if (GET_MODE_SIZE (mode) > 2)
1654     return gen_rtx_REG (mode, MEM0_REGNO);
1655   return gen_rtx_REG (mode, R0_REGNO);
1656 }
1657
1658 /* Implements TARGET_FUNCTION_VALUE.  Functions and libcalls have the same
1659    conventions.  */
1660
1661 #undef TARGET_FUNCTION_VALUE
1662 #define TARGET_FUNCTION_VALUE m32c_function_value
1663
1664 static rtx
1665 m32c_function_value (const_tree valtype,
1666                      const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1667                      bool outgoing ATTRIBUTE_UNUSED)
1668 {
1669   /* return reg or parallel */
1670   const enum machine_mode mode = TYPE_MODE (valtype);
1671   return m32c_libcall_value (mode, NULL_RTX);
1672 }
1673
1674 /* Implements FUNCTION_VALUE_REGNO_P.  */
1675
1676 bool
1677 m32c_function_value_regno_p (const unsigned int regno)
1678 {
1679   return (regno == R0_REGNO || regno == MEM0_REGNO);
1680 }
1681
1682 /* How Large Values Are Returned */
1683
1684 /* We return structures by pushing the address on the stack, even if
1685    we use registers for the first few "real" arguments.  */
1686 #undef TARGET_STRUCT_VALUE_RTX
1687 #define TARGET_STRUCT_VALUE_RTX m32c_struct_value_rtx
1688 static rtx
1689 m32c_struct_value_rtx (tree fndecl ATTRIBUTE_UNUSED,
1690                        int incoming ATTRIBUTE_UNUSED)
1691 {
1692   return 0;
1693 }
1694
1695 /* Function Entry and Exit */
1696
1697 /* Implements EPILOGUE_USES.  Interrupts restore all registers.  */
1698 int
1699 m32c_epilogue_uses (int regno ATTRIBUTE_UNUSED)
1700 {
1701   if (cfun->machine->is_interrupt)
1702     return 1;
1703   return 0;
1704 }
1705
1706 /* Implementing the Varargs Macros */
1707
1708 #undef TARGET_STRICT_ARGUMENT_NAMING
1709 #define TARGET_STRICT_ARGUMENT_NAMING m32c_strict_argument_naming
1710 static bool
1711 m32c_strict_argument_naming (CUMULATIVE_ARGS * ca ATTRIBUTE_UNUSED)
1712 {
1713   return 1;
1714 }
1715
1716 /* Trampolines for Nested Functions */
1717
1718 /*
1719    m16c:
1720    1 0000 75C43412              mov.w   #0x1234,a0
1721    2 0004 FC000000              jmp.a   label
1722
1723    m32c:
1724    1 0000 BC563412              mov.l:s #0x123456,a0
1725    2 0004 CC000000              jmp.a   label
1726 */
1727
1728 /* Implements TRAMPOLINE_SIZE.  */
1729 int
1730 m32c_trampoline_size (void)
1731 {
1732   /* Allocate extra space so we can avoid the messy shifts when we
1733      initialize the trampoline; we just write past the end of the
1734      opcode.  */
1735   return TARGET_A16 ? 8 : 10;
1736 }
1737
1738 /* Implements TRAMPOLINE_ALIGNMENT.  */
1739 int
1740 m32c_trampoline_alignment (void)
1741 {
1742   return 2;
1743 }
1744
1745 /* Implements TARGET_TRAMPOLINE_INIT.  */
1746
1747 #undef TARGET_TRAMPOLINE_INIT
1748 #define TARGET_TRAMPOLINE_INIT m32c_trampoline_init
1749 static void
1750 m32c_trampoline_init (rtx m_tramp, tree fndecl, rtx chainval)
1751 {
1752   rtx function = XEXP (DECL_RTL (fndecl), 0);
1753
1754 #define A0(m,i) adjust_address (m_tramp, m, i)
1755   if (TARGET_A16)
1756     {
1757       /* Note: we subtract a "word" because the moves want signed
1758          constants, not unsigned constants.  */
1759       emit_move_insn (A0 (HImode, 0), GEN_INT (0xc475 - 0x10000));
1760       emit_move_insn (A0 (HImode, 2), chainval);
1761       emit_move_insn (A0 (QImode, 4), GEN_INT (0xfc - 0x100));
1762       /* We use 16-bit addresses here, but store the zero to turn it
1763          into a 24-bit offset.  */
1764       emit_move_insn (A0 (HImode, 5), function);
1765       emit_move_insn (A0 (QImode, 7), GEN_INT (0x00));
1766     }
1767   else
1768     {
1769       /* Note that the PSI moves actually write 4 bytes.  Make sure we
1770          write stuff out in the right order, and leave room for the
1771          extra byte at the end.  */
1772       emit_move_insn (A0 (QImode, 0), GEN_INT (0xbc - 0x100));
1773       emit_move_insn (A0 (PSImode, 1), chainval);
1774       emit_move_insn (A0 (QImode, 4), GEN_INT (0xcc - 0x100));
1775       emit_move_insn (A0 (PSImode, 5), function);
1776     }
1777 #undef A0
1778 }
1779
1780 /* Implicit Calls to Library Routines */
1781
1782 #undef TARGET_INIT_LIBFUNCS
1783 #define TARGET_INIT_LIBFUNCS m32c_init_libfuncs
1784 static void
1785 m32c_init_libfuncs (void)
1786 {
1787   /* We do this because the M32C has an HImode operand, but the
1788      M16C has an 8-bit operand.  Since gcc looks at the match data
1789      and not the expanded rtl, we have to reset the optab so that
1790      the right modes are found. */
1791   if (TARGET_A24)
1792     {
1793       optab_handler (cstore_optab, QImode)->insn_code = CODE_FOR_cstoreqi4_24;
1794       optab_handler (cstore_optab, HImode)->insn_code = CODE_FOR_cstorehi4_24;
1795       optab_handler (cstore_optab, PSImode)->insn_code = CODE_FOR_cstorepsi4_24;
1796     }
1797 }
1798
1799 /* Addressing Modes */
1800
1801 /* The r8c/m32c family supports a wide range of non-orthogonal
1802    addressing modes, including the ability to double-indirect on *some*
1803    of them.  Not all insns support all modes, either, but we rely on
1804    predicates and constraints to deal with that.  */
1805 #undef TARGET_LEGITIMATE_ADDRESS_P
1806 #define TARGET_LEGITIMATE_ADDRESS_P m32c_legitimate_address_p
1807 bool
1808 m32c_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1809 {
1810   int mode_adjust;
1811   if (CONSTANT_P (x))
1812     return 1;
1813
1814   /* Wide references to memory will be split after reload, so we must
1815      ensure that all parts of such splits remain legitimate
1816      addresses.  */
1817   mode_adjust = GET_MODE_SIZE (mode) - 1;
1818
1819   /* allowing PLUS yields mem:HI(plus:SI(mem:SI(plus:SI in m32c_split_move */
1820   if (GET_CODE (x) == PRE_DEC
1821       || GET_CODE (x) == POST_INC || GET_CODE (x) == PRE_MODIFY)
1822     {
1823       return (GET_CODE (XEXP (x, 0)) == REG
1824               && REGNO (XEXP (x, 0)) == SP_REGNO);
1825     }
1826
1827 #if 0
1828   /* This is the double indirection detection, but it currently
1829      doesn't work as cleanly as this code implies, so until we've had
1830      a chance to debug it, leave it disabled.  */
1831   if (TARGET_A24 && GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) != PLUS)
1832     {
1833 #if DEBUG_DOUBLE
1834       fprintf (stderr, "double indirect\n");
1835 #endif
1836       x = XEXP (x, 0);
1837     }
1838 #endif
1839
1840   encode_pattern (x);
1841   if (RTX_IS ("r"))
1842     {
1843       /* Most indexable registers can be used without displacements,
1844          although some of them will be emitted with an explicit zero
1845          to please the assembler.  */
1846       switch (REGNO (patternr[0]))
1847         {
1848         case A0_REGNO:
1849         case A1_REGNO:
1850         case SB_REGNO:
1851         case FB_REGNO:
1852         case SP_REGNO:
1853           return 1;
1854
1855         default:
1856           if (IS_PSEUDO (patternr[0], strict))
1857             return 1;
1858           return 0;
1859         }
1860     }
1861   if (RTX_IS ("+ri"))
1862     {
1863       /* This is more interesting, because different base registers
1864          allow for different displacements - both range and signedness
1865          - and it differs from chip series to chip series too.  */
1866       int rn = REGNO (patternr[1]);
1867       HOST_WIDE_INT offs = INTVAL (patternr[2]);
1868       switch (rn)
1869         {
1870         case A0_REGNO:
1871         case A1_REGNO:
1872         case SB_REGNO:
1873           /* The syntax only allows positive offsets, but when the
1874              offsets span the entire memory range, we can simulate
1875              negative offsets by wrapping.  */
1876           if (TARGET_A16)
1877             return (offs >= -65536 && offs <= 65535 - mode_adjust);
1878           if (rn == SB_REGNO)
1879             return (offs >= 0 && offs <= 65535 - mode_adjust);
1880           /* A0 or A1 */
1881           return (offs >= -16777216 && offs <= 16777215);
1882
1883         case FB_REGNO:
1884           if (TARGET_A16)
1885             return (offs >= -128 && offs <= 127 - mode_adjust);
1886           return (offs >= -65536 && offs <= 65535 - mode_adjust);
1887
1888         case SP_REGNO:
1889           return (offs >= -128 && offs <= 127 - mode_adjust);
1890
1891         default:
1892           if (IS_PSEUDO (patternr[1], strict))
1893             return 1;
1894           return 0;
1895         }
1896     }
1897   if (RTX_IS ("+rs") || RTX_IS ("+r+si"))
1898     {
1899       rtx reg = patternr[1];
1900
1901       /* We don't know where the symbol is, so only allow base
1902          registers which support displacements spanning the whole
1903          address range.  */
1904       switch (REGNO (reg))
1905         {
1906         case A0_REGNO:
1907         case A1_REGNO:
1908           /* $sb needs a secondary reload, but since it's involved in
1909              memory address reloads too, we don't deal with it very
1910              well.  */
1911           /*    case SB_REGNO: */
1912           return 1;
1913         default:
1914           if (IS_PSEUDO (reg, strict))
1915             return 1;
1916           return 0;
1917         }
1918     }
1919   return 0;
1920 }
1921
1922 /* Implements REG_OK_FOR_BASE_P.  */
1923 int
1924 m32c_reg_ok_for_base_p (rtx x, int strict)
1925 {
1926   if (GET_CODE (x) != REG)
1927     return 0;
1928   switch (REGNO (x))
1929     {
1930     case A0_REGNO:
1931     case A1_REGNO:
1932     case SB_REGNO:
1933     case FB_REGNO:
1934     case SP_REGNO:
1935       return 1;
1936     default:
1937       if (IS_PSEUDO (x, strict))
1938         return 1;
1939       return 0;
1940     }
1941 }
1942
1943 /* We have three choices for choosing fb->aN offsets.  If we choose -128,
1944    we need one MOVA -128[fb],aN opcode and 16-bit aN displacements,
1945    like this:
1946        EB 4B FF    mova    -128[$fb],$a0
1947        D8 0C FF FF mov.w:Q #0,-1[$a0]
1948
1949    Alternately, we subtract the frame size, and hopefully use 8-bit aN
1950    displacements:
1951        7B F4       stc $fb,$a0
1952        77 54 00 01 sub #256,$a0
1953        D8 08 01    mov.w:Q #0,1[$a0]
1954
1955    If we don't offset (i.e. offset by zero), we end up with:
1956        7B F4       stc $fb,$a0
1957        D8 0C 00 FF mov.w:Q #0,-256[$a0]
1958
1959    We have to subtract *something* so that we have a PLUS rtx to mark
1960    that we've done this reload.  The -128 offset will never result in
1961    an 8-bit aN offset, and the payoff for the second case is five
1962    loads *if* those loads are within 256 bytes of the other end of the
1963    frame, so the third case seems best.  Note that we subtract the
1964    zero, but detect that in the addhi3 pattern.  */
1965
1966 #define BIG_FB_ADJ 0
1967
1968 /* Implements LEGITIMIZE_ADDRESS.  The only address we really have to
1969    worry about is frame base offsets, as $fb has a limited
1970    displacement range.  We deal with this by attempting to reload $fb
1971    itself into an address register; that seems to result in the best
1972    code.  */
1973 #undef TARGET_LEGITIMIZE_ADDRESS
1974 #define TARGET_LEGITIMIZE_ADDRESS m32c_legitimize_address
1975 static rtx
1976 m32c_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1977                          enum machine_mode mode)
1978 {
1979 #if DEBUG0
1980   fprintf (stderr, "m32c_legitimize_address for mode %s\n", mode_name[mode]);
1981   debug_rtx (x);
1982   fprintf (stderr, "\n");
1983 #endif
1984
1985   if (GET_CODE (x) == PLUS
1986       && GET_CODE (XEXP (x, 0)) == REG
1987       && REGNO (XEXP (x, 0)) == FB_REGNO
1988       && GET_CODE (XEXP (x, 1)) == CONST_INT
1989       && (INTVAL (XEXP (x, 1)) < -128
1990           || INTVAL (XEXP (x, 1)) > (128 - GET_MODE_SIZE (mode))))
1991     {
1992       /* reload FB to A_REGS */
1993       rtx temp = gen_reg_rtx (Pmode);
1994       x = copy_rtx (x);
1995       emit_insn (gen_rtx_SET (VOIDmode, temp, XEXP (x, 0)));
1996       XEXP (x, 0) = temp;
1997     }
1998
1999   return x;
2000 }
2001
2002 /* Implements LEGITIMIZE_RELOAD_ADDRESS.  See comment above.  */
2003 int
2004 m32c_legitimize_reload_address (rtx * x,
2005                                 enum machine_mode mode,
2006                                 int opnum,
2007                                 int type, int ind_levels ATTRIBUTE_UNUSED)
2008 {
2009 #if DEBUG0
2010   fprintf (stderr, "\nm32c_legitimize_reload_address for mode %s\n",
2011            mode_name[mode]);
2012   debug_rtx (*x);
2013 #endif
2014
2015   /* At one point, this function tried to get $fb copied to an address
2016      register, which in theory would maximize sharing, but gcc was
2017      *also* still trying to reload the whole address, and we'd run out
2018      of address registers.  So we let gcc do the naive (but safe)
2019      reload instead, when the above function doesn't handle it for
2020      us.
2021
2022      The code below is a second attempt at the above.  */
2023
2024   if (GET_CODE (*x) == PLUS
2025       && GET_CODE (XEXP (*x, 0)) == REG
2026       && REGNO (XEXP (*x, 0)) == FB_REGNO
2027       && GET_CODE (XEXP (*x, 1)) == CONST_INT
2028       && (INTVAL (XEXP (*x, 1)) < -128
2029           || INTVAL (XEXP (*x, 1)) > (128 - GET_MODE_SIZE (mode))))
2030     {
2031       rtx sum;
2032       int offset = INTVAL (XEXP (*x, 1));
2033       int adjustment = -BIG_FB_ADJ;
2034
2035       sum = gen_rtx_PLUS (Pmode, XEXP (*x, 0),
2036                           GEN_INT (adjustment));
2037       *x = gen_rtx_PLUS (Pmode, sum, GEN_INT (offset - adjustment));
2038       if (type == RELOAD_OTHER)
2039         type = RELOAD_FOR_OTHER_ADDRESS;
2040       push_reload (sum, NULL_RTX, &XEXP (*x, 0), NULL,
2041                    A_REGS, Pmode, VOIDmode, 0, 0, opnum,
2042                    type);
2043       return 1;
2044     }
2045
2046   if (GET_CODE (*x) == PLUS
2047       && GET_CODE (XEXP (*x, 0)) == PLUS
2048       && GET_CODE (XEXP (XEXP (*x, 0), 0)) == REG
2049       && REGNO (XEXP (XEXP (*x, 0), 0)) == FB_REGNO
2050       && GET_CODE (XEXP (XEXP (*x, 0), 1)) == CONST_INT
2051       && GET_CODE (XEXP (*x, 1)) == CONST_INT
2052       )
2053     {
2054       if (type == RELOAD_OTHER)
2055         type = RELOAD_FOR_OTHER_ADDRESS;
2056       push_reload (XEXP (*x, 0), NULL_RTX, &XEXP (*x, 0), NULL,
2057                    A_REGS, Pmode, VOIDmode, 0, 0, opnum,
2058                    type);
2059       return 1;
2060     }
2061
2062   return 0;
2063 }
2064
2065 /* Implements LEGITIMATE_CONSTANT_P.  We split large constants anyway,
2066    so we can allow anything.  */
2067 int
2068 m32c_legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
2069 {
2070   return 1;
2071 }
2072
2073
2074 /* Condition Code Status */
2075
2076 #undef TARGET_FIXED_CONDITION_CODE_REGS
2077 #define TARGET_FIXED_CONDITION_CODE_REGS m32c_fixed_condition_code_regs
2078 static bool
2079 m32c_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
2080 {
2081   *p1 = FLG_REGNO;
2082   *p2 = INVALID_REGNUM;
2083   return true;
2084 }
2085
2086 /* Describing Relative Costs of Operations */
2087
2088 /* Implements REGISTER_MOVE_COST.  We make impossible moves
2089    prohibitively expensive, like trying to put QIs in r2/r3 (there are
2090    no opcodes to do that).  We also discourage use of mem* registers
2091    since they're really memory.  */
2092 int
2093 m32c_register_move_cost (enum machine_mode mode, int from, int to)
2094 {
2095   int cost = COSTS_N_INSNS (3);
2096   int cc = class_contents[from][0] | class_contents[to][0];
2097   /* FIXME: pick real values, but not 2 for now.  */
2098   if (mode == QImode && (cc & class_contents[R23_REGS][0]))
2099     {
2100       if (!(cc & ~class_contents[R23_REGS][0]))
2101         cost = COSTS_N_INSNS (1000);
2102       else
2103         cost = COSTS_N_INSNS (80);
2104     }
2105
2106   if (!class_can_hold_mode (from, mode) || !class_can_hold_mode (to, mode))
2107     cost = COSTS_N_INSNS (1000);
2108
2109   if (classes_intersect (from, CR_REGS))
2110     cost += COSTS_N_INSNS (5);
2111
2112   if (classes_intersect (to, CR_REGS))
2113     cost += COSTS_N_INSNS (5);
2114
2115   if (from == MEM_REGS || to == MEM_REGS)
2116     cost += COSTS_N_INSNS (50);
2117   else if (classes_intersect (from, MEM_REGS)
2118            || classes_intersect (to, MEM_REGS))
2119     cost += COSTS_N_INSNS (10);
2120
2121 #if DEBUG0
2122   fprintf (stderr, "register_move_cost %s from %s to %s = %d\n",
2123            mode_name[mode], class_names[from], class_names[to], cost);
2124 #endif
2125   return cost;
2126 }
2127
2128 /*  Implements MEMORY_MOVE_COST.  */
2129 int
2130 m32c_memory_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
2131                        int reg_class ATTRIBUTE_UNUSED,
2132                        int in ATTRIBUTE_UNUSED)
2133 {
2134   /* FIXME: pick real values.  */
2135   return COSTS_N_INSNS (10);
2136 }
2137
2138 /* Here we try to describe when we use multiple opcodes for one RTX so
2139    that gcc knows when to use them.  */
2140 #undef TARGET_RTX_COSTS
2141 #define TARGET_RTX_COSTS m32c_rtx_costs
2142 static bool
2143 m32c_rtx_costs (rtx x, int code, int outer_code, int *total,
2144                 bool speed ATTRIBUTE_UNUSED)
2145 {
2146   switch (code)
2147     {
2148     case REG:
2149       if (REGNO (x) >= MEM0_REGNO && REGNO (x) <= MEM7_REGNO)
2150         *total += COSTS_N_INSNS (500);
2151       else
2152         *total += COSTS_N_INSNS (1);
2153       return true;
2154
2155     case ASHIFT:
2156     case LSHIFTRT:
2157     case ASHIFTRT:
2158       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2159         {
2160           /* mov.b r1l, r1h */
2161           *total +=  COSTS_N_INSNS (1);
2162           return true;
2163         }
2164       if (INTVAL (XEXP (x, 1)) > 8
2165           || INTVAL (XEXP (x, 1)) < -8)
2166         {
2167           /* mov.b #N, r1l */
2168           /* mov.b r1l, r1h */
2169           *total +=  COSTS_N_INSNS (2);
2170           return true;
2171         }
2172       return true;
2173
2174     case LE:
2175     case LEU:
2176     case LT:
2177     case LTU:
2178     case GT:
2179     case GTU:
2180     case GE:
2181     case GEU:
2182     case NE:
2183     case EQ:
2184       if (outer_code == SET)
2185         {
2186           *total += COSTS_N_INSNS (2);
2187           return true;
2188         }
2189       break;
2190
2191     case ZERO_EXTRACT:
2192       {
2193         rtx dest = XEXP (x, 0);
2194         rtx addr = XEXP (dest, 0);
2195         switch (GET_CODE (addr))
2196           {
2197           case CONST_INT:
2198             *total += COSTS_N_INSNS (1);
2199             break;
2200           case SYMBOL_REF:
2201             *total += COSTS_N_INSNS (3);
2202             break;
2203           default:
2204             *total += COSTS_N_INSNS (2);
2205             break;
2206           }
2207         return true;
2208       }
2209       break;
2210
2211     default:
2212       /* Reasonable default.  */
2213       if (TARGET_A16 && GET_MODE(x) == SImode)
2214         *total += COSTS_N_INSNS (2);
2215       break;
2216     }
2217   return false;
2218 }
2219
2220 #undef TARGET_ADDRESS_COST
2221 #define TARGET_ADDRESS_COST m32c_address_cost
2222 static int
2223 m32c_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
2224 {
2225   int i;
2226   /*  fprintf(stderr, "\naddress_cost\n");
2227       debug_rtx(addr);*/
2228   switch (GET_CODE (addr))
2229     {
2230     case CONST_INT:
2231       i = INTVAL (addr);
2232       if (i == 0)
2233         return COSTS_N_INSNS(1);
2234       if (0 < i && i <= 255)
2235         return COSTS_N_INSNS(2);
2236       if (0 < i && i <= 65535)
2237         return COSTS_N_INSNS(3);
2238       return COSTS_N_INSNS(4);
2239     case SYMBOL_REF:
2240       return COSTS_N_INSNS(4);
2241     case REG:
2242       return COSTS_N_INSNS(1);
2243     case PLUS:
2244       if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
2245         {
2246           i = INTVAL (XEXP (addr, 1));
2247           if (i == 0)
2248             return COSTS_N_INSNS(1);
2249           if (0 < i && i <= 255)
2250             return COSTS_N_INSNS(2);
2251           if (0 < i && i <= 65535)
2252             return COSTS_N_INSNS(3);
2253         }
2254       return COSTS_N_INSNS(4);
2255     default:
2256       return 0;
2257     }
2258 }
2259
2260 /* Defining the Output Assembler Language */
2261
2262 /* The Overall Framework of an Assembler File */
2263
2264 #undef TARGET_HAVE_NAMED_SECTIONS
2265 #define TARGET_HAVE_NAMED_SECTIONS true
2266
2267 /* Output of Data */
2268
2269 /* We may have 24 bit sizes, which is the native address size.
2270    Currently unused, but provided for completeness.  */
2271 #undef TARGET_ASM_INTEGER
2272 #define TARGET_ASM_INTEGER m32c_asm_integer
2273 static bool
2274 m32c_asm_integer (rtx x, unsigned int size, int aligned_p)
2275 {
2276   switch (size)
2277     {
2278     case 3:
2279       fprintf (asm_out_file, "\t.3byte\t");
2280       output_addr_const (asm_out_file, x);
2281       fputc ('\n', asm_out_file);
2282       return true;
2283     case 4:
2284       if (GET_CODE (x) == SYMBOL_REF)
2285         {
2286           fprintf (asm_out_file, "\t.long\t");
2287           output_addr_const (asm_out_file, x);
2288           fputc ('\n', asm_out_file);
2289           return true;
2290         }
2291       break;
2292     }
2293   return default_assemble_integer (x, size, aligned_p);
2294 }
2295
2296 /* Output of Assembler Instructions */
2297
2298 /* We use a lookup table because the addressing modes are non-orthogonal.  */
2299
2300 static struct
2301 {
2302   char code;
2303   char const *pattern;
2304   char const *format;
2305 }
2306 const conversions[] = {
2307   { 0, "r", "0" },
2308
2309   { 0, "mr", "z[1]" },
2310   { 0, "m+ri", "3[2]" },
2311   { 0, "m+rs", "3[2]" },
2312   { 0, "m+r+si", "4+5[2]" },
2313   { 0, "ms", "1" },
2314   { 0, "mi", "1" },
2315   { 0, "m+si", "2+3" },
2316
2317   { 0, "mmr", "[z[2]]" },
2318   { 0, "mm+ri", "[4[3]]" },
2319   { 0, "mm+rs", "[4[3]]" },
2320   { 0, "mm+r+si", "[5+6[3]]" },
2321   { 0, "mms", "[[2]]" },
2322   { 0, "mmi", "[[2]]" },
2323   { 0, "mm+si", "[4[3]]" },
2324
2325   { 0, "i", "#0" },
2326   { 0, "s", "#0" },
2327   { 0, "+si", "#1+2" },
2328   { 0, "l", "#0" },
2329
2330   { 'l', "l", "0" },
2331   { 'd', "i", "0" },
2332   { 'd', "s", "0" },
2333   { 'd', "+si", "1+2" },
2334   { 'D', "i", "0" },
2335   { 'D', "s", "0" },
2336   { 'D', "+si", "1+2" },
2337   { 'x', "i", "#0" },
2338   { 'X', "i", "#0" },
2339   { 'm', "i", "#0" },
2340   { 'b', "i", "#0" },
2341   { 'B', "i", "0" },
2342   { 'p', "i", "0" },
2343
2344   { 0, 0, 0 }
2345 };
2346
2347 /* This is in order according to the bitfield that pushm/popm use.  */
2348 static char const *pushm_regs[] = {
2349   "fb", "sb", "a1", "a0", "r3", "r2", "r1", "r0"
2350 };
2351
2352 /* Implements PRINT_OPERAND.  */
2353 void
2354 m32c_print_operand (FILE * file, rtx x, int code)
2355 {
2356   int i, j, b;
2357   const char *comma;
2358   HOST_WIDE_INT ival;
2359   int unsigned_const = 0;
2360   int force_sign;
2361
2362   /* Multiplies; constants are converted to sign-extended format but
2363    we need unsigned, so 'u' and 'U' tell us what size unsigned we
2364    need.  */
2365   if (code == 'u')
2366     {
2367       unsigned_const = 2;
2368       code = 0;
2369     }
2370   if (code == 'U')
2371     {
2372       unsigned_const = 1;
2373       code = 0;
2374     }
2375   /* This one is only for debugging; you can put it in a pattern to
2376      force this error.  */
2377   if (code == '!')
2378     {
2379       fprintf (stderr, "dj: unreviewed pattern:");
2380       if (current_output_insn)
2381         debug_rtx (current_output_insn);
2382       gcc_unreachable ();
2383     }
2384   /* PSImode operations are either .w or .l depending on the target.  */
2385   if (code == '&')
2386     {
2387       if (TARGET_A16)
2388         fprintf (file, "w");
2389       else
2390         fprintf (file, "l");
2391       return;
2392     }
2393   /* Inverted conditionals.  */
2394   if (code == 'C')
2395     {
2396       switch (GET_CODE (x))
2397         {
2398         case LE:
2399           fputs ("gt", file);
2400           break;
2401         case LEU:
2402           fputs ("gtu", file);
2403           break;
2404         case LT:
2405           fputs ("ge", file);
2406           break;
2407         case LTU:
2408           fputs ("geu", file);
2409           break;
2410         case GT:
2411           fputs ("le", file);
2412           break;
2413         case GTU:
2414           fputs ("leu", file);
2415           break;
2416         case GE:
2417           fputs ("lt", file);
2418           break;
2419         case GEU:
2420           fputs ("ltu", file);
2421           break;
2422         case NE:
2423           fputs ("eq", file);
2424           break;
2425         case EQ:
2426           fputs ("ne", file);
2427           break;
2428         default:
2429           gcc_unreachable ();
2430         }
2431       return;
2432     }
2433   /* Regular conditionals.  */
2434   if (code == 'c')
2435     {
2436       switch (GET_CODE (x))
2437         {
2438         case LE:
2439           fputs ("le", file);
2440           break;
2441         case LEU:
2442           fputs ("leu", file);
2443           break;
2444         case LT:
2445           fputs ("lt", file);
2446           break;
2447         case LTU:
2448           fputs ("ltu", file);
2449           break;
2450         case GT:
2451           fputs ("gt", file);
2452           break;
2453         case GTU:
2454           fputs ("gtu", file);
2455           break;
2456         case GE:
2457           fputs ("ge", file);
2458           break;
2459         case GEU:
2460           fputs ("geu", file);
2461           break;
2462         case NE:
2463           fputs ("ne", file);
2464           break;
2465         case EQ:
2466           fputs ("eq", file);
2467           break;
2468         default:
2469           gcc_unreachable ();
2470         }
2471       return;
2472     }
2473   /* Used in negsi2 to do HImode ops on the two parts of an SImode
2474      operand.  */
2475   if (code == 'h' && GET_MODE (x) == SImode)
2476     {
2477       x = m32c_subreg (HImode, x, SImode, 0);
2478       code = 0;
2479     }
2480   if (code == 'H' && GET_MODE (x) == SImode)
2481     {
2482       x = m32c_subreg (HImode, x, SImode, 2);
2483       code = 0;
2484     }
2485   if (code == 'h' && GET_MODE (x) == HImode)
2486     {
2487       x = m32c_subreg (QImode, x, HImode, 0);
2488       code = 0;
2489     }
2490   if (code == 'H' && GET_MODE (x) == HImode)
2491     {
2492       /* We can't actually represent this as an rtx.  Do it here.  */
2493       if (GET_CODE (x) == REG)
2494         {
2495           switch (REGNO (x))
2496             {
2497             case R0_REGNO:
2498               fputs ("r0h", file);
2499               return;
2500             case R1_REGNO:
2501               fputs ("r1h", file);
2502               return;
2503             default:
2504               gcc_unreachable();
2505             }
2506         }
2507       /* This should be a MEM.  */
2508       x = m32c_subreg (QImode, x, HImode, 1);
2509       code = 0;
2510     }
2511   /* This is for BMcond, which always wants word register names.  */
2512   if (code == 'h' && GET_MODE (x) == QImode)
2513     {
2514       if (GET_CODE (x) == REG)
2515         x = gen_rtx_REG (HImode, REGNO (x));
2516       code = 0;
2517     }
2518   /* 'x' and 'X' need to be ignored for non-immediates.  */
2519   if ((code == 'x' || code == 'X') && GET_CODE (x) != CONST_INT)
2520     code = 0;
2521
2522   encode_pattern (x);
2523   force_sign = 0;
2524   for (i = 0; conversions[i].pattern; i++)
2525     if (conversions[i].code == code
2526         && streq (conversions[i].pattern, pattern))
2527       {
2528         for (j = 0; conversions[i].format[j]; j++)
2529           /* backslash quotes the next character in the output pattern.  */
2530           if (conversions[i].format[j] == '\\')
2531             {
2532               fputc (conversions[i].format[j + 1], file);
2533               j++;
2534             }
2535           /* Digits in the output pattern indicate that the
2536              corresponding RTX is to be output at that point.  */
2537           else if (ISDIGIT (conversions[i].format[j]))
2538             {
2539               rtx r = patternr[conversions[i].format[j] - '0'];
2540               switch (GET_CODE (r))
2541                 {
2542                 case REG:
2543                   fprintf (file, "%s",
2544                            reg_name_with_mode (REGNO (r), GET_MODE (r)));
2545                   break;
2546                 case CONST_INT:
2547                   switch (code)
2548                     {
2549                     case 'b':
2550                     case 'B':
2551                       {
2552                         int v = INTVAL (r);
2553                         int i = (int) exact_log2 (v);
2554                         if (i == -1)
2555                           i = (int) exact_log2 ((v ^ 0xffff) & 0xffff);
2556                         if (i == -1)
2557                           i = (int) exact_log2 ((v ^ 0xff) & 0xff);
2558                         /* Bit position.  */
2559                         fprintf (file, "%d", i);
2560                       }
2561                       break;
2562                     case 'x':
2563                       /* Unsigned byte.  */
2564                       fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2565                                INTVAL (r) & 0xff);
2566                       break;
2567                     case 'X':
2568                       /* Unsigned word.  */
2569                       fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2570                                INTVAL (r) & 0xffff);
2571                       break;
2572                     case 'p':
2573                       /* pushm and popm encode a register set into a single byte.  */
2574                       comma = "";
2575                       for (b = 7; b >= 0; b--)
2576                         if (INTVAL (r) & (1 << b))
2577                           {
2578                             fprintf (file, "%s%s", comma, pushm_regs[b]);
2579                             comma = ",";
2580                           }
2581                       break;
2582                     case 'm':
2583                       /* "Minus".  Output -X  */
2584                       ival = (-INTVAL (r) & 0xffff);
2585                       if (ival & 0x8000)
2586                         ival = ival - 0x10000;
2587                       fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
2588                       break;
2589                     default:
2590                       ival = INTVAL (r);
2591                       if (conversions[i].format[j + 1] == '[' && ival < 0)
2592                         {
2593                           /* We can simulate negative displacements by
2594                              taking advantage of address space
2595                              wrapping when the offset can span the
2596                              entire address range.  */
2597                           rtx base =
2598                             patternr[conversions[i].format[j + 2] - '0'];
2599                           if (GET_CODE (base) == REG)
2600                             switch (REGNO (base))
2601                               {
2602                               case A0_REGNO:
2603                               case A1_REGNO:
2604                                 if (TARGET_A24)
2605                                   ival = 0x1000000 + ival;
2606                                 else
2607                                   ival = 0x10000 + ival;
2608                                 break;
2609                               case SB_REGNO:
2610                                 if (TARGET_A16)
2611                                   ival = 0x10000 + ival;
2612                                 break;
2613                               }
2614                         }
2615                       else if (code == 'd' && ival < 0 && j == 0)
2616                         /* The "mova" opcode is used to do addition by
2617                            computing displacements, but again, we need
2618                            displacements to be unsigned *if* they're
2619                            the only component of the displacement
2620                            (i.e. no "symbol-4" type displacement).  */
2621                         ival = (TARGET_A24 ? 0x1000000 : 0x10000) + ival;
2622
2623                       if (conversions[i].format[j] == '0')
2624                         {
2625                           /* More conversions to unsigned.  */
2626                           if (unsigned_const == 2)
2627                             ival &= 0xffff;
2628                           if (unsigned_const == 1)
2629                             ival &= 0xff;
2630                         }
2631                       if (streq (conversions[i].pattern, "mi")
2632                           || streq (conversions[i].pattern, "mmi"))
2633                         {
2634                           /* Integers used as addresses are unsigned.  */
2635                           ival &= (TARGET_A24 ? 0xffffff : 0xffff);
2636                         }
2637                       if (force_sign && ival >= 0)
2638                         fputc ('+', file);
2639                       fprintf (file, HOST_WIDE_INT_PRINT_DEC, ival);
2640                       break;
2641                     }
2642                   break;
2643                 case CONST_DOUBLE:
2644                   /* We don't have const_double constants.  If it
2645                      happens, make it obvious.  */
2646                   fprintf (file, "[const_double 0x%lx]",
2647                            (unsigned long) CONST_DOUBLE_HIGH (r));
2648                   break;
2649                 case SYMBOL_REF:
2650                   assemble_name (file, XSTR (r, 0));
2651                   break;
2652                 case LABEL_REF:
2653                   output_asm_label (r);
2654                   break;
2655                 default:
2656                   fprintf (stderr, "don't know how to print this operand:");
2657                   debug_rtx (r);
2658                   gcc_unreachable ();
2659                 }
2660             }
2661           else
2662             {
2663               if (conversions[i].format[j] == 'z')
2664                 {
2665                   /* Some addressing modes *must* have a displacement,
2666                      so insert a zero here if needed.  */
2667                   int k;
2668                   for (k = j + 1; conversions[i].format[k]; k++)
2669                     if (ISDIGIT (conversions[i].format[k]))
2670                       {
2671                         rtx reg = patternr[conversions[i].format[k] - '0'];
2672                         if (GET_CODE (reg) == REG
2673                             && (REGNO (reg) == SB_REGNO
2674                                 || REGNO (reg) == FB_REGNO
2675                                 || REGNO (reg) == SP_REGNO))
2676                           fputc ('0', file);
2677                       }
2678                   continue;
2679                 }
2680               /* Signed displacements off symbols need to have signs
2681                  blended cleanly.  */
2682               if (conversions[i].format[j] == '+'
2683                   && (!code || code == 'D' || code == 'd')
2684                   && ISDIGIT (conversions[i].format[j + 1])
2685                   && (GET_CODE (patternr[conversions[i].format[j + 1] - '0'])
2686                       == CONST_INT))
2687                 {
2688                   force_sign = 1;
2689                   continue;
2690                 }
2691               fputc (conversions[i].format[j], file);
2692             }
2693         break;
2694       }
2695   if (!conversions[i].pattern)
2696     {
2697       fprintf (stderr, "unconvertible operand %c `%s'", code ? code : '-',
2698                pattern);
2699       debug_rtx (x);
2700       fprintf (file, "[%c.%s]", code ? code : '-', pattern);
2701     }
2702
2703   return;
2704 }
2705
2706 /* Implements PRINT_OPERAND_PUNCT_VALID_P.  See m32c_print_operand
2707    above for descriptions of what these do.  */
2708 int
2709 m32c_print_operand_punct_valid_p (int c)
2710 {
2711   if (c == '&' || c == '!')
2712     return 1;
2713   return 0;
2714 }
2715
2716 /* Implements PRINT_OPERAND_ADDRESS.  Nothing unusual here.  */
2717 void
2718 m32c_print_operand_address (FILE * stream, rtx address)
2719 {
2720   if (GET_CODE (address) == MEM)
2721     address = XEXP (address, 0);
2722   else
2723     /* cf: gcc.dg/asm-4.c.  */
2724     gcc_assert (GET_CODE (address) == REG);
2725
2726   m32c_print_operand (stream, address, 0);
2727 }
2728
2729 /* Implements ASM_OUTPUT_REG_PUSH.  Control registers are pushed
2730    differently than general registers.  */
2731 void
2732 m32c_output_reg_push (FILE * s, int regno)
2733 {
2734   if (regno == FLG_REGNO)
2735     fprintf (s, "\tpushc\tflg\n");
2736   else
2737     fprintf (s, "\tpush.%c\t%s\n",
2738              " bwll"[reg_push_size (regno)], reg_names[regno]);
2739 }
2740
2741 /* Likewise for ASM_OUTPUT_REG_POP.  */
2742 void
2743 m32c_output_reg_pop (FILE * s, int regno)
2744 {
2745   if (regno == FLG_REGNO)
2746     fprintf (s, "\tpopc\tflg\n");
2747   else
2748     fprintf (s, "\tpop.%c\t%s\n",
2749              " bwll"[reg_push_size (regno)], reg_names[regno]);
2750 }
2751
2752 /* Defining target-specific uses of `__attribute__' */
2753
2754 /* Used to simplify the logic below.  Find the attributes wherever
2755    they may be.  */
2756 #define M32C_ATTRIBUTES(decl) \
2757   (TYPE_P (decl)) ? TYPE_ATTRIBUTES (decl) \
2758                 : DECL_ATTRIBUTES (decl) \
2759                   ? (DECL_ATTRIBUTES (decl)) \
2760                   : TYPE_ATTRIBUTES (TREE_TYPE (decl))
2761
2762 /* Returns TRUE if the given tree has the "interrupt" attribute.  */
2763 static int
2764 interrupt_p (tree node ATTRIBUTE_UNUSED)
2765 {
2766   tree list = M32C_ATTRIBUTES (node);
2767   while (list)
2768     {
2769       if (is_attribute_p ("interrupt", TREE_PURPOSE (list)))
2770         return 1;
2771       list = TREE_CHAIN (list);
2772     }
2773   return fast_interrupt_p (node);
2774 }
2775
2776 /* Returns TRUE if the given tree has the "bank_switch" attribute.  */
2777 static int
2778 bank_switch_p (tree node ATTRIBUTE_UNUSED)
2779 {
2780   tree list = M32C_ATTRIBUTES (node);
2781   while (list)
2782     {
2783       if (is_attribute_p ("bank_switch", TREE_PURPOSE (list)))
2784         return 1;
2785       list = TREE_CHAIN (list);
2786     }
2787   return 0;
2788 }
2789
2790 /* Returns TRUE if the given tree has the "fast_interrupt" attribute.  */
2791 static int
2792 fast_interrupt_p (tree node ATTRIBUTE_UNUSED)
2793 {
2794   tree list = M32C_ATTRIBUTES (node);
2795   while (list)
2796     {
2797       if (is_attribute_p ("fast_interrupt", TREE_PURPOSE (list)))
2798         return 1;
2799       list = TREE_CHAIN (list);
2800     }
2801   return 0;
2802 }
2803
2804 static tree
2805 interrupt_handler (tree * node ATTRIBUTE_UNUSED,
2806                    tree name ATTRIBUTE_UNUSED,
2807                    tree args ATTRIBUTE_UNUSED,
2808                    int flags ATTRIBUTE_UNUSED,
2809                    bool * no_add_attrs ATTRIBUTE_UNUSED)
2810 {
2811   return NULL_TREE;
2812 }
2813
2814 /* Returns TRUE if given tree has the "function_vector" attribute. */
2815 int
2816 m32c_special_page_vector_p (tree func)
2817 {
2818   tree list;
2819
2820   if (TREE_CODE (func) != FUNCTION_DECL)
2821     return 0;
2822
2823   list = M32C_ATTRIBUTES (func);
2824   while (list)
2825     {
2826       if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
2827         return 1;
2828       list = TREE_CHAIN (list);
2829     }
2830   return 0;
2831 }
2832
2833 static tree
2834 function_vector_handler (tree * node ATTRIBUTE_UNUSED,
2835                          tree name ATTRIBUTE_UNUSED,
2836                          tree args ATTRIBUTE_UNUSED,
2837                          int flags ATTRIBUTE_UNUSED,
2838                          bool * no_add_attrs ATTRIBUTE_UNUSED)
2839 {
2840   if (TARGET_R8C)
2841     {
2842       /* The attribute is not supported for R8C target.  */
2843       warning (OPT_Wattributes,
2844                 "%qE attribute is not supported for R8C target",
2845                 name);
2846       *no_add_attrs = true;
2847     }
2848   else if (TREE_CODE (*node) != FUNCTION_DECL)
2849     {
2850       /* The attribute must be applied to functions only.  */
2851       warning (OPT_Wattributes,
2852                 "%qE attribute applies only to functions",
2853                 name);
2854       *no_add_attrs = true;
2855     }
2856   else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
2857     {
2858       /* The argument must be a constant integer.  */
2859       warning (OPT_Wattributes,
2860                 "%qE attribute argument not an integer constant",
2861                 name);
2862       *no_add_attrs = true;
2863     }
2864   else if (TREE_INT_CST_LOW (TREE_VALUE (args)) < 18
2865            || TREE_INT_CST_LOW (TREE_VALUE (args)) > 255)
2866     {
2867       /* The argument value must be between 18 to 255.  */
2868       warning (OPT_Wattributes,
2869                 "%qE attribute argument should be between 18 to 255",
2870                 name);
2871       *no_add_attrs = true;
2872     }
2873   return NULL_TREE;
2874 }
2875
2876 /* If the function is assigned the attribute 'function_vector', it
2877    returns the function vector number, otherwise returns zero.  */
2878 int
2879 current_function_special_page_vector (rtx x)
2880 {
2881   int num;
2882
2883   if ((GET_CODE(x) == SYMBOL_REF)
2884       && (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
2885     {
2886       tree list;
2887       tree t = SYMBOL_REF_DECL (x);
2888
2889       if (TREE_CODE (t) != FUNCTION_DECL)
2890         return 0;
2891
2892       list = M32C_ATTRIBUTES (t);
2893       while (list)
2894         {
2895           if (is_attribute_p ("function_vector", TREE_PURPOSE (list)))
2896             {
2897               num = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (list)));
2898               return num;
2899             }
2900
2901           list = TREE_CHAIN (list);
2902         }
2903
2904       return 0;
2905     }
2906   else
2907     return 0;
2908 }
2909
2910 #undef TARGET_ATTRIBUTE_TABLE
2911 #define TARGET_ATTRIBUTE_TABLE m32c_attribute_table
2912 static const struct attribute_spec m32c_attribute_table[] = {
2913   {"interrupt", 0, 0, false, false, false, interrupt_handler},
2914   {"bank_switch", 0, 0, false, false, false, interrupt_handler},
2915   {"fast_interrupt", 0, 0, false, false, false, interrupt_handler},
2916   {"function_vector", 1, 1, true,  false, false, function_vector_handler},
2917   {0, 0, 0, 0, 0, 0, 0}
2918 };
2919
2920 #undef TARGET_COMP_TYPE_ATTRIBUTES
2921 #define TARGET_COMP_TYPE_ATTRIBUTES m32c_comp_type_attributes
2922 static int
2923 m32c_comp_type_attributes (const_tree type1 ATTRIBUTE_UNUSED,
2924                            const_tree type2 ATTRIBUTE_UNUSED)
2925 {
2926   /* 0=incompatible 1=compatible 2=warning */
2927   return 1;
2928 }
2929
2930 #undef TARGET_INSERT_ATTRIBUTES
2931 #define TARGET_INSERT_ATTRIBUTES m32c_insert_attributes
2932 static void
2933 m32c_insert_attributes (tree node ATTRIBUTE_UNUSED,
2934                         tree * attr_ptr ATTRIBUTE_UNUSED)
2935 {
2936   /* Nothing to do here.  */
2937 }
2938
2939 /* Predicates */
2940
2941 /* This is a list of legal subregs of hard regs.  */
2942 static const struct {
2943   unsigned char outer_mode_size;
2944   unsigned char inner_mode_size;
2945   unsigned char byte_mask;
2946   unsigned char legal_when;
2947   unsigned int regno;
2948 } legal_subregs[] = {
2949   {1, 2, 0x03, 1, R0_REGNO}, /* r0h r0l */
2950   {1, 2, 0x03, 1, R1_REGNO}, /* r1h r1l */
2951   {1, 2, 0x01, 1, A0_REGNO},
2952   {1, 2, 0x01, 1, A1_REGNO},
2953
2954   {1, 4, 0x01, 1, A0_REGNO},
2955   {1, 4, 0x01, 1, A1_REGNO},
2956
2957   {2, 4, 0x05, 1, R0_REGNO}, /* r2 r0 */
2958   {2, 4, 0x05, 1, R1_REGNO}, /* r3 r1 */
2959   {2, 4, 0x05, 16, A0_REGNO}, /* a1 a0 */
2960   {2, 4, 0x01, 24, A0_REGNO}, /* a1 a0 */
2961   {2, 4, 0x01, 24, A1_REGNO}, /* a1 a0 */
2962
2963   {4, 8, 0x55, 1, R0_REGNO}, /* r3 r1 r2 r0 */
2964 };
2965
2966 /* Returns TRUE if OP is a subreg of a hard reg which we don't
2967    support.  */
2968 bool
2969 m32c_illegal_subreg_p (rtx op)
2970 {
2971   int offset;
2972   unsigned int i;
2973   int src_mode, dest_mode;
2974
2975   if (GET_CODE (op) != SUBREG)
2976     return false;
2977
2978   dest_mode = GET_MODE (op);
2979   offset = SUBREG_BYTE (op);
2980   op = SUBREG_REG (op);
2981   src_mode = GET_MODE (op);
2982
2983   if (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (src_mode))
2984     return false;
2985   if (GET_CODE (op) != REG)
2986     return false;
2987   if (REGNO (op) >= MEM0_REGNO)
2988     return false;
2989
2990   offset = (1 << offset);
2991
2992   for (i = 0; i < ARRAY_SIZE (legal_subregs); i ++)
2993     if (legal_subregs[i].outer_mode_size == GET_MODE_SIZE (dest_mode)
2994         && legal_subregs[i].regno == REGNO (op)
2995         && legal_subregs[i].inner_mode_size == GET_MODE_SIZE (src_mode)
2996         && legal_subregs[i].byte_mask & offset)
2997       {
2998         switch (legal_subregs[i].legal_when)
2999           {
3000           case 1:
3001             return false;
3002           case 16:
3003             if (TARGET_A16)
3004               return false;
3005             break;
3006           case 24:
3007             if (TARGET_A24)
3008               return false;
3009             break;
3010           }
3011       }
3012   return true;
3013 }
3014
3015 /* Returns TRUE if we support a move between the first two operands.
3016    At the moment, we just want to discourage mem to mem moves until
3017    after reload, because reload has a hard time with our limited
3018    number of address registers, and we can get into a situation where
3019    we need three of them when we only have two.  */
3020 bool
3021 m32c_mov_ok (rtx * operands, enum machine_mode mode ATTRIBUTE_UNUSED)
3022 {
3023   rtx op0 = operands[0];
3024   rtx op1 = operands[1];
3025
3026   if (TARGET_A24)
3027     return true;
3028
3029 #define DEBUG_MOV_OK 0
3030 #if DEBUG_MOV_OK
3031   fprintf (stderr, "m32c_mov_ok %s\n", mode_name[mode]);
3032   debug_rtx (op0);
3033   debug_rtx (op1);
3034 #endif
3035
3036   if (GET_CODE (op0) == SUBREG)
3037     op0 = XEXP (op0, 0);
3038   if (GET_CODE (op1) == SUBREG)
3039     op1 = XEXP (op1, 0);
3040
3041   if (GET_CODE (op0) == MEM
3042       && GET_CODE (op1) == MEM
3043       && ! reload_completed)
3044     {
3045 #if DEBUG_MOV_OK
3046       fprintf (stderr, " - no, mem to mem\n");
3047 #endif
3048       return false;
3049     }
3050
3051 #if DEBUG_MOV_OK
3052   fprintf (stderr, " - ok\n");
3053 #endif
3054   return true;
3055 }
3056
3057 /* Returns TRUE if two consecutive HImode mov instructions, generated
3058    for moving an immediate double data to a double data type variable
3059    location, can be combined into single SImode mov instruction.  */
3060 bool
3061 m32c_immd_dbl_mov (rtx * operands, 
3062                    enum machine_mode mode ATTRIBUTE_UNUSED)
3063 {
3064   int flag = 0, okflag = 0, offset1 = 0, offset2 = 0, offsetsign = 0;
3065   const char *str1;
3066   const char *str2;
3067
3068   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
3069       && MEM_SCALAR_P (operands[0])
3070       && !MEM_IN_STRUCT_P (operands[0])
3071       && GET_CODE (XEXP (operands[2], 0)) == CONST
3072       && GET_CODE (XEXP (XEXP (operands[2], 0), 0)) == PLUS
3073       && GET_CODE (XEXP (XEXP (XEXP (operands[2], 0), 0), 0)) == SYMBOL_REF
3074       && GET_CODE (XEXP (XEXP (XEXP (operands[2], 0), 0), 1)) == CONST_INT
3075       && MEM_SCALAR_P (operands[2])
3076       && !MEM_IN_STRUCT_P (operands[2]))
3077     flag = 1; 
3078
3079   else if (GET_CODE (XEXP (operands[0], 0)) == CONST
3080            && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == PLUS
3081            && GET_CODE (XEXP (XEXP (XEXP (operands[0], 0), 0), 0)) == SYMBOL_REF
3082            && MEM_SCALAR_P (operands[0])
3083            && !MEM_IN_STRUCT_P (operands[0])
3084            && !(INTVAL (XEXP (XEXP (XEXP (operands[0], 0), 0), 1)) %4)
3085            && GET_CODE (XEXP (operands[2], 0)) == CONST
3086            && GET_CODE (XEXP (XEXP (operands[2], 0), 0)) == PLUS
3087            && GET_CODE (XEXP (XEXP (XEXP (operands[2], 0), 0), 0)) == SYMBOL_REF
3088            && MEM_SCALAR_P (operands[2])
3089            && !MEM_IN_STRUCT_P (operands[2]))
3090     flag = 2; 
3091
3092   else if (GET_CODE (XEXP (operands[0], 0)) == PLUS
3093            &&  GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
3094            &&  REGNO (XEXP (XEXP (operands[0], 0), 0)) == FB_REGNO 
3095            &&  GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT
3096            &&  MEM_SCALAR_P (operands[0])
3097            &&  !MEM_IN_STRUCT_P (operands[0])
3098            &&  !(INTVAL (XEXP (XEXP (operands[0], 0), 1)) %4)
3099            &&  REGNO (XEXP (XEXP (operands[2], 0), 0)) == FB_REGNO 
3100            &&  GET_CODE (XEXP (XEXP (operands[2], 0), 1)) == CONST_INT
3101            &&  MEM_SCALAR_P (operands[2])
3102            &&  !MEM_IN_STRUCT_P (operands[2]))
3103     flag = 3; 
3104
3105   else
3106     return false;
3107
3108   switch (flag)
3109     {
3110     case 1:
3111       str1 = XSTR (XEXP (operands[0], 0), 0);
3112       str2 = XSTR (XEXP (XEXP (XEXP (operands[2], 0), 0), 0), 0);
3113       if (strcmp (str1, str2) == 0)
3114         okflag = 1; 
3115       else
3116         okflag = 0; 
3117       break;
3118     case 2:
3119       str1 = XSTR (XEXP (XEXP (XEXP (operands[0], 0), 0), 0), 0);
3120       str2 = XSTR (XEXP (XEXP (XEXP (operands[2], 0), 0), 0), 0);
3121       if (strcmp(str1,str2) == 0)
3122         okflag = 1; 
3123       else
3124         okflag = 0; 
3125       break; 
3126     case 3:
3127       offset1 = INTVAL (XEXP (XEXP (operands[0], 0), 1));
3128       offset2 = INTVAL (XEXP (XEXP (operands[2], 0), 1));
3129       offsetsign = offset1 >> ((sizeof (offset1) * 8) -1);
3130       if (((offset2-offset1) == 2) && offsetsign != 0)
3131         okflag = 1;
3132       else 
3133         okflag = 0; 
3134       break; 
3135     default:
3136       okflag = 0; 
3137     } 
3138       
3139   if (okflag == 1)
3140     {
3141       HOST_WIDE_INT val;
3142       operands[4] = gen_rtx_MEM (SImode, XEXP (operands[0], 0));
3143
3144       val = (INTVAL (operands[3]) << 16) + (INTVAL (operands[1]) & 0xFFFF);
3145       operands[5] = gen_rtx_CONST_INT (VOIDmode, val);
3146      
3147       return true;
3148     }
3149
3150   return false;
3151 }  
3152
3153 /* Expanders */
3154
3155 /* Subregs are non-orthogonal for us, because our registers are all
3156    different sizes.  */
3157 static rtx
3158 m32c_subreg (enum machine_mode outer,
3159              rtx x, enum machine_mode inner, int byte)
3160 {
3161   int r, nr = -1;
3162
3163   /* Converting MEMs to different types that are the same size, we
3164      just rewrite them.  */
3165   if (GET_CODE (x) == SUBREG
3166       && SUBREG_BYTE (x) == 0
3167       && GET_CODE (SUBREG_REG (x)) == MEM
3168       && (GET_MODE_SIZE (GET_MODE (x))
3169           == GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3170     {
3171       rtx oldx = x;
3172       x = gen_rtx_MEM (GET_MODE (x), XEXP (SUBREG_REG (x), 0));
3173       MEM_COPY_ATTRIBUTES (x, SUBREG_REG (oldx));
3174     }
3175
3176   /* Push/pop get done as smaller push/pops.  */
3177   if (GET_CODE (x) == MEM
3178       && (GET_CODE (XEXP (x, 0)) == PRE_DEC
3179           || GET_CODE (XEXP (x, 0)) == POST_INC))
3180     return gen_rtx_MEM (outer, XEXP (x, 0));
3181   if (GET_CODE (x) == SUBREG
3182       && GET_CODE (XEXP (x, 0)) == MEM
3183       && (GET_CODE (XEXP (XEXP (x, 0), 0)) == PRE_DEC
3184           || GET_CODE (XEXP (XEXP (x, 0), 0)) == POST_INC))
3185     return gen_rtx_MEM (outer, XEXP (XEXP (x, 0), 0));
3186
3187   if (GET_CODE (x) != REG)
3188     return simplify_gen_subreg (outer, x, inner, byte);
3189
3190   r = REGNO (x);
3191   if (r >= FIRST_PSEUDO_REGISTER || r == AP_REGNO)
3192     return simplify_gen_subreg (outer, x, inner, byte);
3193
3194   if (IS_MEM_REGNO (r))
3195     return simplify_gen_subreg (outer, x, inner, byte);
3196
3197   /* This is where the complexities of our register layout are
3198      described.  */
3199   if (byte == 0)
3200     nr = r;
3201   else if (outer == HImode)
3202     {
3203       if (r == R0_REGNO && byte == 2)
3204         nr = R2_REGNO;
3205       else if (r == R0_REGNO && byte == 4)
3206         nr = R1_REGNO;
3207       else if (r == R0_REGNO && byte == 6)
3208         nr = R3_REGNO;
3209       else if (r == R1_REGNO && byte == 2)
3210         nr = R3_REGNO;
3211       else if (r == A0_REGNO && byte == 2)
3212         nr = A1_REGNO;
3213     }
3214   else if (outer == SImode)
3215     {
3216       if (r == R0_REGNO && byte == 0)
3217         nr = R0_REGNO;
3218       else if (r == R0_REGNO && byte == 4)
3219         nr = R1_REGNO;
3220     }
3221   if (nr == -1)
3222     {
3223       fprintf (stderr, "m32c_subreg %s %s %d\n",
3224                mode_name[outer], mode_name[inner], byte);
3225       debug_rtx (x);
3226       gcc_unreachable ();
3227     }
3228   return gen_rtx_REG (outer, nr);
3229 }
3230
3231 /* Used to emit move instructions.  We split some moves,
3232    and avoid mem-mem moves.  */
3233 int
3234 m32c_prepare_move (rtx * operands, enum machine_mode mode)
3235 {
3236   if (TARGET_A16 && mode == PSImode)
3237     return m32c_split_move (operands, mode, 1);
3238   if ((GET_CODE (operands[0]) == MEM)
3239       && (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY))
3240     {
3241       rtx pmv = XEXP (operands[0], 0);
3242       rtx dest_reg = XEXP (pmv, 0);
3243       rtx dest_mod = XEXP (pmv, 1);
3244
3245       emit_insn (gen_rtx_SET (Pmode, dest_reg, dest_mod));
3246       operands[0] = gen_rtx_MEM (mode, dest_reg);
3247     }
3248   if (can_create_pseudo_p () && MEM_P (operands[0]) && MEM_P (operands[1]))
3249     operands[1] = copy_to_mode_reg (mode, operands[1]);
3250   return 0;
3251 }
3252
3253 #define DEBUG_SPLIT 0
3254
3255 /* Returns TRUE if the given PSImode move should be split.  We split
3256    for all r8c/m16c moves, since it doesn't support them, and for
3257    POP.L as we can only *push* SImode.  */
3258 int
3259 m32c_split_psi_p (rtx * operands)
3260 {
3261 #if DEBUG_SPLIT
3262   fprintf (stderr, "\nm32c_split_psi_p\n");
3263   debug_rtx (operands[0]);
3264   debug_rtx (operands[1]);
3265 #endif
3266   if (TARGET_A16)
3267     {
3268 #if DEBUG_SPLIT
3269       fprintf (stderr, "yes, A16\n");
3270 #endif
3271       return 1;
3272     }
3273   if (GET_CODE (operands[1]) == MEM
3274       && GET_CODE (XEXP (operands[1], 0)) == POST_INC)
3275     {
3276 #if DEBUG_SPLIT
3277       fprintf (stderr, "yes, pop.l\n");
3278 #endif
3279       return 1;
3280     }
3281 #if DEBUG_SPLIT
3282   fprintf (stderr, "no, default\n");
3283 #endif
3284   return 0;
3285 }
3286
3287 /* Split the given move.  SPLIT_ALL is 0 if splitting is optional
3288    (define_expand), 1 if it is not optional (define_insn_and_split),
3289    and 3 for define_split (alternate api). */
3290 int
3291 m32c_split_move (rtx * operands, enum machine_mode mode, int split_all)
3292 {
3293   rtx s[4], d[4];
3294   int parts, si, di, rev = 0;
3295   int rv = 0, opi = 2;
3296   enum machine_mode submode = HImode;
3297   rtx *ops, local_ops[10];
3298
3299   /* define_split modifies the existing operands, but the other two
3300      emit new insns.  OPS is where we store the operand pairs, which
3301      we emit later.  */
3302   if (split_all == 3)
3303     ops = operands;
3304   else
3305     ops = local_ops;
3306
3307   /* Else HImode.  */
3308   if (mode == DImode)
3309     submode = SImode;
3310
3311   /* Before splitting mem-mem moves, force one operand into a
3312      register.  */
3313   if (can_create_pseudo_p () && MEM_P (operands[0]) && MEM_P (operands[1]))
3314     {
3315 #if DEBUG0
3316       fprintf (stderr, "force_reg...\n");
3317       debug_rtx (operands[1]);
3318 #endif
3319       operands[1] = force_reg (mode, operands[1]);
3320 #if DEBUG0
3321       debug_rtx (operands[1]);
3322 #endif
3323     }
3324
3325   parts = 2;
3326
3327 #if DEBUG_SPLIT
3328   fprintf (stderr, "\nsplit_move %d all=%d\n", !can_create_pseudo_p (),
3329            split_all);
3330   debug_rtx (operands[0]);
3331   debug_rtx (operands[1]);
3332 #endif
3333
3334   /* Note that split_all is not used to select the api after this
3335      point, so it's safe to set it to 3 even with define_insn.  */
3336   /* None of the chips can move SI operands to sp-relative addresses,
3337      so we always split those.  */
3338   if (m32c_extra_constraint_p (operands[0], 'S', "Ss"))
3339     split_all = 3;
3340
3341   /* We don't need to split these.  */
3342   if (TARGET_A24
3343       && split_all != 3
3344       && (mode == SImode || mode == PSImode)
3345       && !(GET_CODE (operands[1]) == MEM
3346            && GET_CODE (XEXP (operands[1], 0)) == POST_INC))
3347     return 0;
3348
3349   /* First, enumerate the subregs we'll be dealing with.  */
3350   for (si = 0; si < parts; si++)
3351     {
3352       d[si] =
3353         m32c_subreg (submode, operands[0], mode,
3354                      si * GET_MODE_SIZE (submode));
3355       s[si] =
3356         m32c_subreg (submode, operands[1], mode,
3357                      si * GET_MODE_SIZE (submode));
3358     }
3359
3360   /* Split pushes by emitting a sequence of smaller pushes.  */
3361   if (GET_CODE (d[0]) == MEM && GET_CODE (XEXP (d[0], 0)) == PRE_DEC)
3362     {
3363       for (si = parts - 1; si >= 0; si--)
3364         {
3365           ops[opi++] = gen_rtx_MEM (submode,
3366                                     gen_rtx_PRE_DEC (Pmode,
3367                                                      gen_rtx_REG (Pmode,
3368                                                                   SP_REGNO)));
3369           ops[opi++] = s[si];
3370         }
3371
3372       rv = 1;
3373     }
3374   /* Likewise for pops.  */
3375   else if (GET_CODE (s[0]) == MEM && GET_CODE (XEXP (s[0], 0)) == POST_INC)
3376     {
3377       for (di = 0; di < parts; di++)
3378         {
3379           ops[opi++] = d[di];
3380           ops[opi++] = gen_rtx_MEM (submode,
3381                                     gen_rtx_POST_INC (Pmode,
3382                                                       gen_rtx_REG (Pmode,
3383                                                                    SP_REGNO)));
3384         }
3385       rv = 1;
3386     }
3387   else if (split_all)
3388     {
3389       /* if d[di] == s[si] for any di < si, we'll early clobber. */
3390       for (di = 0; di < parts - 1; di++)
3391         for (si = di + 1; si < parts; si++)
3392           if (reg_mentioned_p (d[di], s[si]))
3393             rev = 1;
3394
3395       if (rev)
3396         for (si = 0; si < parts; si++)
3397           {
3398             ops[opi++] = d[si];
3399             ops[opi++] = s[si];
3400           }
3401       else
3402         for (si = parts - 1; si >= 0; si--)
3403           {
3404             ops[opi++] = d[si];
3405             ops[opi++] = s[si];
3406           }
3407       rv = 1;
3408     }
3409   /* Now emit any moves we may have accumulated.  */
3410   if (rv && split_all != 3)
3411     {
3412       int i;
3413       for (i = 2; i < opi; i += 2)
3414         emit_move_insn (ops[i], ops[i + 1]);
3415     }
3416   return rv;
3417 }
3418
3419 /* The m32c has a number of opcodes that act like memcpy, strcmp, and
3420    the like.  For the R8C they expect one of the addresses to be in
3421    R1L:An so we need to arrange for that.  Otherwise, it's just a
3422    matter of picking out the operands we want and emitting the right
3423    pattern for them.  All these expanders, which correspond to
3424    patterns in blkmov.md, must return nonzero if they expand the insn,
3425    or zero if they should FAIL.  */
3426
3427 /* This is a memset() opcode.  All operands are implied, so we need to
3428    arrange for them to be in the right registers.  The opcode wants
3429    addresses, not [mem] syntax.  $0 is the destination (MEM:BLK), $1
3430    the count (HI), and $2 the value (QI).  */
3431 int
3432 m32c_expand_setmemhi(rtx *operands)
3433 {
3434   rtx desta, count, val;
3435   rtx desto, counto;
3436
3437   desta = XEXP (operands[0], 0);
3438   count = operands[1];
3439   val = operands[2];
3440
3441   desto = gen_reg_rtx (Pmode);
3442   counto = gen_reg_rtx (HImode);
3443
3444   if (GET_CODE (desta) != REG
3445       || REGNO (desta) < FIRST_PSEUDO_REGISTER)
3446     desta = copy_to_mode_reg (Pmode, desta);
3447
3448   /* This looks like an arbitrary restriction, but this is by far the
3449      most common case.  For counts 8..14 this actually results in
3450      smaller code with no speed penalty because the half-sized
3451      constant can be loaded with a shorter opcode.  */
3452   if (GET_CODE (count) == CONST_INT
3453       && GET_CODE (val) == CONST_INT
3454       && ! (INTVAL (count) & 1)
3455       && (INTVAL (count) > 1)
3456       && (INTVAL (val) <= 7 && INTVAL (val) >= -8))
3457     {
3458       unsigned v = INTVAL (val) & 0xff;
3459       v = v | (v << 8);
3460       count = copy_to_mode_reg (HImode, GEN_INT (INTVAL (count) / 2));
3461       val = copy_to_mode_reg (HImode, GEN_INT (v));
3462       if (TARGET_A16)
3463         emit_insn (gen_setmemhi_whi_op (desto, counto, val, desta, count));
3464       else
3465         emit_insn (gen_setmemhi_wpsi_op (desto, counto, val, desta, count));
3466       return 1;
3467     }
3468
3469   /* This is the generalized memset() case.  */
3470   if (GET_CODE (val) != REG
3471       || REGNO (val) < FIRST_PSEUDO_REGISTER)
3472     val = copy_to_mode_reg (QImode, val);
3473
3474   if (GET_CODE (count) != REG
3475       || REGNO (count) < FIRST_PSEUDO_REGISTER)
3476     count = copy_to_mode_reg (HImode, count);
3477
3478   if (TARGET_A16)
3479     emit_insn (gen_setmemhi_bhi_op (desto, counto, val, desta, count));
3480   else
3481     emit_insn (gen_setmemhi_bpsi_op (desto, counto, val, desta, count));
3482
3483   return 1;
3484 }
3485
3486 /* This is a memcpy() opcode.  All operands are implied, so we need to
3487    arrange for them to be in the right registers.  The opcode wants
3488    addresses, not [mem] syntax.  $0 is the destination (MEM:BLK), $1
3489    is the source (MEM:BLK), and $2 the count (HI).  */
3490 int
3491 m32c_expand_movmemhi(rtx *operands)
3492 {
3493   rtx desta, srca, count;
3494   rtx desto, srco, counto;
3495
3496   desta = XEXP (operands[0], 0);
3497   srca = XEXP (operands[1], 0);
3498   count = operands[2];
3499
3500   desto = gen_reg_rtx (Pmode);
3501   srco = gen_reg_rtx (Pmode);
3502   counto = gen_reg_rtx (HImode);
3503
3504   if (GET_CODE (desta) != REG
3505       || REGNO (desta) < FIRST_PSEUDO_REGISTER)
3506     desta = copy_to_mode_reg (Pmode, desta);
3507
3508   if (GET_CODE (srca) != REG
3509       || REGNO (srca) < FIRST_PSEUDO_REGISTER)
3510     srca = copy_to_mode_reg (Pmode, srca);
3511
3512   /* Similar to setmem, but we don't need to check the value.  */
3513   if (GET_CODE (count) == CONST_INT
3514       && ! (INTVAL (count) & 1)
3515       && (INTVAL (count) > 1))
3516     {
3517       count = copy_to_mode_reg (HImode, GEN_INT (INTVAL (count) / 2));
3518       if (TARGET_A16)
3519         emit_insn (gen_movmemhi_whi_op (desto, srco, counto, desta, srca, count));
3520       else
3521         emit_insn (gen_movmemhi_wpsi_op (desto, srco, counto, desta, srca, count));
3522       return 1;
3523     }
3524
3525   /* This is the generalized memset() case.  */
3526   if (GET_CODE (count) != REG
3527       || REGNO (count) < FIRST_PSEUDO_REGISTER)
3528     count = copy_to_mode_reg (HImode, count);
3529
3530   if (TARGET_A16)
3531     emit_insn (gen_movmemhi_bhi_op (desto, srco, counto, desta, srca, count));
3532   else
3533     emit_insn (gen_movmemhi_bpsi_op (desto, srco, counto, desta, srca, count));
3534
3535   return 1;
3536 }
3537
3538 /* This is a stpcpy() opcode.  $0 is the destination (MEM:BLK) after
3539    the copy, which should point to the NUL at the end of the string,
3540    $1 is the destination (MEM:BLK), and $2 is the source (MEM:BLK).
3541    Since our opcode leaves the destination pointing *after* the NUL,
3542    we must emit an adjustment.  */
3543 int
3544 m32c_expand_movstr(rtx *operands)
3545 {
3546   rtx desta, srca;
3547   rtx desto, srco;
3548
3549   desta = XEXP (operands[1], 0);
3550   srca = XEXP (operands[2], 0);
3551
3552   desto = gen_reg_rtx (Pmode);
3553   srco = gen_reg_rtx (Pmode);
3554
3555   if (GET_CODE (desta) != REG
3556       || REGNO (desta) < FIRST_PSEUDO_REGISTER)
3557     desta = copy_to_mode_reg (Pmode, desta);
3558
3559   if (GET_CODE (srca) != REG
3560       || REGNO (srca) < FIRST_PSEUDO_REGISTER)
3561     srca = copy_to_mode_reg (Pmode, srca);
3562
3563   emit_insn (gen_movstr_op (desto, srco, desta, srca));
3564   /* desto ends up being a1, which allows this type of add through MOVA.  */
3565   emit_insn (gen_addpsi3 (operands[0], desto, GEN_INT (-1)));
3566
3567   return 1;
3568 }
3569
3570 /* This is a strcmp() opcode.  $0 is the destination (HI) which holds
3571    <=>0 depending on the comparison, $1 is one string (MEM:BLK), and
3572    $2 is the other (MEM:BLK).  We must do the comparison, and then
3573    convert the flags to a signed integer result.  */
3574 int
3575 m32c_expand_cmpstr(rtx *operands)
3576 {
3577   rtx src1a, src2a;
3578
3579   src1a = XEXP (operands[1], 0);
3580   src2a = XEXP (operands[2], 0);
3581
3582   if (GET_CODE (src1a) != REG
3583       || REGNO (src1a) < FIRST_PSEUDO_REGISTER)
3584     src1a = copy_to_mode_reg (Pmode, src1a);
3585
3586   if (GET_CODE (src2a) != REG
3587       || REGNO (src2a) < FIRST_PSEUDO_REGISTER)
3588     src2a = copy_to_mode_reg (Pmode, src2a);
3589
3590   emit_insn (gen_cmpstrhi_op (src1a, src2a, src1a, src2a));
3591   emit_insn (gen_cond_to_int (operands[0]));
3592
3593   return 1;
3594 }
3595
3596
3597 typedef rtx (*shift_gen_func)(rtx, rtx, rtx);
3598
3599 static shift_gen_func
3600 shift_gen_func_for (int mode, int code)
3601 {
3602 #define GFF(m,c,f) if (mode == m && code == c) return f
3603   GFF(QImode,  ASHIFT,   gen_ashlqi3_i);
3604   GFF(QImode,  ASHIFTRT, gen_ashrqi3_i);
3605   GFF(QImode,  LSHIFTRT, gen_lshrqi3_i);
3606   GFF(HImode,  ASHIFT,   gen_ashlhi3_i);
3607   GFF(HImode,  ASHIFTRT, gen_ashrhi3_i);
3608   GFF(HImode,  LSHIFTRT, gen_lshrhi3_i);
3609   GFF(PSImode, ASHIFT,   gen_ashlpsi3_i);
3610   GFF(PSImode, ASHIFTRT, gen_ashrpsi3_i);
3611   GFF(PSImode, LSHIFTRT, gen_lshrpsi3_i);
3612   GFF(SImode,  ASHIFT,   TARGET_A16 ? gen_ashlsi3_16 : gen_ashlsi3_24);
3613   GFF(SImode,  ASHIFTRT, TARGET_A16 ? gen_ashrsi3_16 : gen_ashrsi3_24);
3614   GFF(SImode,  LSHIFTRT, TARGET_A16 ? gen_lshrsi3_16 : gen_lshrsi3_24);
3615 #undef GFF
3616   gcc_unreachable ();
3617 }
3618
3619 /* The m32c only has one shift, but it takes a signed count.  GCC
3620    doesn't want this, so we fake it by negating any shift count when
3621    we're pretending to shift the other way.  Also, the shift count is
3622    limited to -8..8.  It's slightly better to use two shifts for 9..15
3623    than to load the count into r1h, so we do that too.  */
3624 int
3625 m32c_prepare_shift (rtx * operands, int scale, int shift_code)
3626 {
3627   enum machine_mode mode = GET_MODE (operands[0]);
3628   shift_gen_func func = shift_gen_func_for (mode, shift_code);
3629   rtx temp;
3630
3631   if (GET_CODE (operands[2]) == CONST_INT)
3632     {
3633       int maxc = TARGET_A24 && (mode == PSImode || mode == SImode) ? 32 : 8;
3634       int count = INTVAL (operands[2]) * scale;
3635
3636       while (count > maxc)
3637         {
3638           temp = gen_reg_rtx (mode);
3639           emit_insn (func (temp, operands[1], GEN_INT (maxc)));
3640           operands[1] = temp;
3641           count -= maxc;
3642         }
3643       while (count < -maxc)
3644         {
3645           temp = gen_reg_rtx (mode);
3646           emit_insn (func (temp, operands[1], GEN_INT (-maxc)));
3647           operands[1] = temp;
3648           count += maxc;
3649         }
3650       emit_insn (func (operands[0], operands[1], GEN_INT (count)));
3651       return 1;
3652     }
3653
3654   temp = gen_reg_rtx (QImode);
3655   if (scale < 0)
3656     /* The pattern has a NEG that corresponds to this. */
3657     emit_move_insn (temp, gen_rtx_NEG (QImode, operands[2]));
3658   else if (TARGET_A16 && mode == SImode)
3659     /* We do this because the code below may modify this, we don't
3660        want to modify the origin of this value.  */
3661     emit_move_insn (temp, operands[2]);
3662   else
3663     /* We'll only use it for the shift, no point emitting a move.  */
3664     temp = operands[2];
3665
3666   if (TARGET_A16 && GET_MODE_SIZE (mode) == 4)
3667     {
3668       /* The m16c has a limit of -16..16 for SI shifts, even when the
3669          shift count is in a register.  Since there are so many targets
3670          of these shifts, it's better to expand the RTL here than to
3671          call a helper function.
3672
3673          The resulting code looks something like this:
3674
3675                 cmp.b   r1h,-16
3676                 jge.b   1f
3677                 shl.l   -16,dest
3678                 add.b   r1h,16
3679         1f:     cmp.b   r1h,16
3680                 jle.b   1f
3681                 shl.l   16,dest
3682                 sub.b   r1h,16
3683         1f:     shl.l   r1h,dest
3684
3685          We take advantage of the fact that "negative" shifts are
3686          undefined to skip one of the comparisons.  */
3687
3688       rtx count;
3689       rtx label, lref, insn, tempvar;
3690
3691       emit_move_insn (operands[0], operands[1]);
3692
3693       count = temp;
3694       label = gen_label_rtx ();
3695       lref = gen_rtx_LABEL_REF (VOIDmode, label);
3696       LABEL_NUSES (label) ++;
3697
3698       tempvar = gen_reg_rtx (mode);
3699
3700       if (shift_code == ASHIFT)
3701         {
3702           /* This is a left shift.  We only need check positive counts.  */
3703           emit_jump_insn (gen_cbranchqi4 (gen_rtx_LE (VOIDmode, 0, 0),
3704                                           count, GEN_INT (16), label));
3705           emit_insn (func (tempvar, operands[0], GEN_INT (8)));
3706           emit_insn (func (operands[0], tempvar, GEN_INT (8)));
3707           insn = emit_insn (gen_addqi3 (count, count, GEN_INT (-16)));
3708           emit_label_after (label, insn);
3709         }
3710       else
3711         {
3712           /* This is a right shift.  We only need check negative counts.  */
3713           emit_jump_insn (gen_cbranchqi4 (gen_rtx_GE (VOIDmode, 0, 0),
3714                                           count, GEN_INT (-16), label));
3715           emit_insn (func (tempvar, operands[0], GEN_INT (-8)));
3716           emit_insn (func (operands[0], tempvar, GEN_INT (-8)));
3717           insn = emit_insn (gen_addqi3 (count, count, GEN_INT (16)));
3718           emit_label_after (label, insn);
3719         }
3720       operands[1] = operands[0];
3721       emit_insn (func (operands[0], operands[0], count));
3722       return 1;
3723     }
3724
3725   operands[2] = temp;
3726   return 0;
3727 }
3728
3729 /* The m32c has a limited range of operations that work on PSImode
3730    values; we have to expand to SI, do the math, and truncate back to
3731    PSI.  Yes, this is expensive, but hopefully gcc will learn to avoid
3732    those cases.  */
3733 void
3734 m32c_expand_neg_mulpsi3 (rtx * operands)
3735 {
3736   /* operands: a = b * i */
3737   rtx temp1; /* b as SI */
3738   rtx scale /* i as SI */;
3739   rtx temp2; /* a*b as SI */
3740
3741   temp1 = gen_reg_rtx (SImode);
3742   temp2 = gen_reg_rtx (SImode);
3743   if (GET_CODE (operands[2]) != CONST_INT)
3744     {
3745       scale = gen_reg_rtx (SImode);
3746       emit_insn (gen_zero_extendpsisi2 (scale, operands[2]));
3747     }
3748   else
3749     scale = copy_to_mode_reg (SImode, operands[2]);
3750
3751   emit_insn (gen_zero_extendpsisi2 (temp1, operands[1]));
3752   temp2 = expand_simple_binop (SImode, MULT, temp1, scale, temp2, 1, OPTAB_LIB);
3753   emit_insn (gen_truncsipsi2 (operands[0], temp2));
3754 }
3755
3756 /* Pattern Output Functions */
3757
3758 int
3759 m32c_expand_movcc (rtx *operands)
3760 {
3761   rtx rel = operands[1];
3762   rtx cmp;
3763
3764   if (GET_CODE (rel) != EQ && GET_CODE (rel) != NE)
3765     return 1;
3766   if (GET_CODE (operands[2]) != CONST_INT
3767       || GET_CODE (operands[3]) != CONST_INT)
3768     return 1;
3769   if (GET_CODE (rel) == NE)
3770     {
3771       rtx tmp = operands[2];
3772       operands[2] = operands[3];
3773       operands[3] = tmp;
3774       rel = gen_rtx_EQ (GET_MODE (rel), XEXP (rel, 0), XEXP (rel, 1));
3775     }
3776
3777   emit_move_insn (operands[0],
3778                   gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
3779                                         rel,
3780                                         operands[2],
3781                                         operands[3]));
3782   return 0;
3783 }
3784
3785 /* Used for the "insv" pattern.  Return nonzero to fail, else done.  */
3786 int
3787 m32c_expand_insv (rtx *operands)
3788 {
3789   rtx op0, src0, p;
3790   int mask;
3791
3792   if (INTVAL (operands[1]) != 1)
3793     return 1;
3794
3795   /* Our insv opcode (bset, bclr) can only insert a one-bit constant.  */
3796   if (GET_CODE (operands[3]) != CONST_INT)
3797     return 1;
3798   if (INTVAL (operands[3]) != 0
3799       && INTVAL (operands[3]) != 1
3800       && INTVAL (operands[3]) != -1)
3801     return 1;
3802
3803   mask = 1 << INTVAL (operands[2]);
3804
3805   op0 = operands[0];
3806   if (GET_CODE (op0) == SUBREG
3807       && SUBREG_BYTE (op0) == 0)
3808     {
3809       rtx sub = SUBREG_REG (op0);
3810       if (GET_MODE (sub) == HImode || GET_MODE (sub) == QImode)
3811         op0 = sub;
3812     }
3813
3814   if (!can_create_pseudo_p ()
3815       || (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0)))
3816     src0 = op0;
3817   else
3818     {
3819       src0 = gen_reg_rtx (GET_MODE (op0));
3820       emit_move_insn (src0, op0);
3821     }
3822
3823   if (GET_MODE (op0) == HImode
3824       && INTVAL (operands[2]) >= 8
3825       && GET_MODE (op0) == MEM)
3826     {
3827       /* We are little endian.  */
3828       rtx new_mem = gen_rtx_MEM (QImode, plus_constant (XEXP (op0, 0), 1));
3829       MEM_COPY_ATTRIBUTES (new_mem, op0);
3830       mask >>= 8;
3831     }
3832
3833   /* First, we generate a mask with the correct polarity.  If we are
3834      storing a zero, we want an AND mask, so invert it.  */
3835   if (INTVAL (operands[3]) == 0)
3836     {
3837       /* Storing a zero, use an AND mask */
3838       if (GET_MODE (op0) == HImode)
3839         mask ^= 0xffff;
3840       else
3841         mask ^= 0xff;
3842     }
3843   /* Now we need to properly sign-extend the mask in case we need to
3844      fall back to an AND or OR opcode.  */
3845   if (GET_MODE (op0) == HImode)
3846     {
3847       if (mask & 0x8000)
3848         mask -= 0x10000;
3849     }
3850   else
3851     {
3852       if (mask & 0x80)
3853         mask -= 0x100;
3854     }
3855
3856   switch (  (INTVAL (operands[3]) ? 4 : 0)
3857           + ((GET_MODE (op0) == HImode) ? 2 : 0)
3858           + (TARGET_A24 ? 1 : 0))
3859     {
3860     case 0: p = gen_andqi3_16 (op0, src0, GEN_INT (mask)); break;
3861     case 1: p = gen_andqi3_24 (op0, src0, GEN_INT (mask)); break;
3862     case 2: p = gen_andhi3_16 (op0, src0, GEN_INT (mask)); break;
3863     case 3: p = gen_andhi3_24 (op0, src0, GEN_INT (mask)); break;
3864     case 4: p = gen_iorqi3_16 (op0, src0, GEN_INT (mask)); break;
3865     case 5: p = gen_iorqi3_24 (op0, src0, GEN_INT (mask)); break;
3866     case 6: p = gen_iorhi3_16 (op0, src0, GEN_INT (mask)); break;
3867     case 7: p = gen_iorhi3_24 (op0, src0, GEN_INT (mask)); break;
3868     default: p = NULL_RTX; break; /* Not reached, but silences a warning.  */
3869     }
3870
3871   emit_insn (p);
3872   return 0;
3873 }
3874
3875 const char *
3876 m32c_scc_pattern(rtx *operands, RTX_CODE code)
3877 {
3878   static char buf[30];
3879   if (GET_CODE (operands[0]) == REG
3880       && REGNO (operands[0]) == R0_REGNO)
3881     {
3882       if (code == EQ)
3883         return "stzx\t#1,#0,r0l";
3884       if (code == NE)
3885         return "stzx\t#0,#1,r0l";
3886     }
3887   sprintf(buf, "bm%s\t0,%%h0\n\tand.b\t#1,%%0", GET_RTX_NAME (code));
3888   return buf;
3889 }
3890
3891 /* Encode symbol attributes of a SYMBOL_REF into its
3892    SYMBOL_REF_FLAGS. */
3893 static void
3894 m32c_encode_section_info (tree decl, rtx rtl, int first)
3895 {
3896   int extra_flags = 0;
3897
3898   default_encode_section_info (decl, rtl, first);
3899   if (TREE_CODE (decl) == FUNCTION_DECL
3900       && m32c_special_page_vector_p (decl))
3901
3902     extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
3903
3904   if (extra_flags)
3905     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
3906 }
3907
3908 /* Returns TRUE if the current function is a leaf, and thus we can
3909    determine which registers an interrupt function really needs to
3910    save.  The logic below is mostly about finding the insn sequence
3911    that's the function, versus any sequence that might be open for the
3912    current insn.  */
3913 static int
3914 m32c_leaf_function_p (void)
3915 {
3916   rtx saved_first, saved_last;
3917   struct sequence_stack *seq;
3918   int rv;
3919
3920   saved_first = crtl->emit.x_first_insn;
3921   saved_last = crtl->emit.x_last_insn;
3922   for (seq = crtl->emit.sequence_stack; seq && seq->next; seq = seq->next)
3923     ;
3924   if (seq)
3925     {
3926       crtl->emit.x_first_insn = seq->first;
3927       crtl->emit.x_last_insn = seq->last;
3928     }
3929
3930   rv = leaf_function_p ();
3931
3932   crtl->emit.x_first_insn = saved_first;
3933   crtl->emit.x_last_insn = saved_last;
3934   return rv;
3935 }
3936
3937 /* Returns TRUE if the current function needs to use the ENTER/EXIT
3938    opcodes.  If the function doesn't need the frame base or stack
3939    pointer, it can use the simpler RTS opcode.  */
3940 static bool
3941 m32c_function_needs_enter (void)
3942 {
3943   rtx insn;
3944   struct sequence_stack *seq;
3945   rtx sp = gen_rtx_REG (Pmode, SP_REGNO);
3946   rtx fb = gen_rtx_REG (Pmode, FB_REGNO);
3947
3948   insn = get_insns ();
3949   for (seq = crtl->emit.sequence_stack;
3950        seq;
3951        insn = seq->first, seq = seq->next);
3952
3953   while (insn)
3954     {
3955       if (reg_mentioned_p (sp, insn))
3956         return true;
3957       if (reg_mentioned_p (fb, insn))
3958         return true;
3959       insn = NEXT_INSN (insn);
3960     }
3961   return false;
3962 }
3963
3964 /* Mark all the subexpressions of the PARALLEL rtx PAR as
3965    frame-related.  Return PAR.
3966
3967    dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
3968    PARALLEL rtx other than the first if they do not have the
3969    FRAME_RELATED flag set on them.  So this function is handy for
3970    marking up 'enter' instructions.  */
3971 static rtx
3972 m32c_all_frame_related (rtx par)
3973 {
3974   int len = XVECLEN (par, 0);
3975   int i;
3976
3977   for (i = 0; i < len; i++)
3978     F (XVECEXP (par, 0, i));
3979
3980   return par;
3981 }
3982
3983 /* Emits the prologue.  See the frame layout comment earlier in this
3984    file.  We can reserve up to 256 bytes with the ENTER opcode, beyond
3985    that we manually update sp.  */
3986 void
3987 m32c_emit_prologue (void)
3988 {
3989   int frame_size, extra_frame_size = 0, reg_save_size;
3990   int complex_prologue = 0;
3991
3992   cfun->machine->is_leaf = m32c_leaf_function_p ();
3993   if (interrupt_p (cfun->decl))
3994     {
3995       cfun->machine->is_interrupt = 1;
3996       complex_prologue = 1;
3997     }
3998   else if (bank_switch_p (cfun->decl))
3999     warning (OPT_Wattributes,
4000              "%<bank_switch%> has no effect on non-interrupt functions");
4001
4002   reg_save_size = m32c_pushm_popm (PP_justcount);
4003
4004   if (interrupt_p (cfun->decl))
4005     {
4006       if (bank_switch_p (cfun->decl))
4007         emit_insn (gen_fset_b ());
4008       else if (cfun->machine->intr_pushm)
4009         emit_insn (gen_pushm (GEN_INT (cfun->machine->intr_pushm)));
4010     }
4011
4012   frame_size =
4013     m32c_initial_elimination_offset (FB_REGNO, SP_REGNO) - reg_save_size;
4014   if (frame_size == 0
4015       && !m32c_function_needs_enter ())
4016     cfun->machine->use_rts = 1;
4017
4018   if (frame_size > 254)
4019     {
4020       extra_frame_size = frame_size - 254;
4021       frame_size = 254;
4022     }
4023   if (cfun->machine->use_rts == 0)
4024     F (emit_insn (m32c_all_frame_related
4025                   (TARGET_A16
4026                    ? gen_prologue_enter_16 (GEN_INT (frame_size + 2))
4027                    : gen_prologue_enter_24 (GEN_INT (frame_size + 4)))));
4028
4029   if (extra_frame_size)
4030     {
4031       complex_prologue = 1;
4032       if (TARGET_A16)
4033         F (emit_insn (gen_addhi3 (gen_rtx_REG (HImode, SP_REGNO),
4034                                   gen_rtx_REG (HImode, SP_REGNO),
4035                                   GEN_INT (-extra_frame_size))));
4036       else
4037         F (emit_insn (gen_addpsi3 (gen_rtx_REG (PSImode, SP_REGNO),
4038                                    gen_rtx_REG (PSImode, SP_REGNO),
4039                                    GEN_INT (-extra_frame_size))));
4040     }
4041
4042   complex_prologue += m32c_pushm_popm (PP_pushm);
4043
4044   /* This just emits a comment into the .s file for debugging.  */
4045   if (complex_prologue)
4046     emit_insn (gen_prologue_end ());
4047 }
4048
4049 /* Likewise, for the epilogue.  The only exception is that, for
4050    interrupts, we must manually unwind the frame as the REIT opcode
4051    doesn't do that.  */
4052 void
4053 m32c_emit_epilogue (void)
4054 {
4055   /* This just emits a comment into the .s file for debugging.  */
4056   if (m32c_pushm_popm (PP_justcount) > 0 || cfun->machine->is_interrupt)
4057     emit_insn (gen_epilogue_start ());
4058
4059   m32c_pushm_popm (PP_popm);
4060
4061   if (cfun->machine->is_interrupt)
4062     {
4063       enum machine_mode spmode = TARGET_A16 ? HImode : PSImode;
4064
4065       /* REIT clears B flag and restores $fp for us, but we still
4066          have to fix up the stack.  USE_RTS just means we didn't
4067          emit ENTER.  */
4068       if (!cfun->machine->use_rts)
4069         {
4070           emit_move_insn (gen_rtx_REG (spmode, A0_REGNO),
4071                           gen_rtx_REG (spmode, FP_REGNO));
4072           emit_move_insn (gen_rtx_REG (spmode, SP_REGNO),
4073                           gen_rtx_REG (spmode, A0_REGNO));
4074           /* We can't just add this to the POPM because it would be in
4075              the wrong order, and wouldn't fix the stack if we're bank
4076              switching.  */
4077           if (TARGET_A16)
4078             emit_insn (gen_pophi_16 (gen_rtx_REG (HImode, FP_REGNO)));
4079           else
4080             emit_insn (gen_poppsi (gen_rtx_REG (PSImode, FP_REGNO)));
4081         }
4082       if (!bank_switch_p (cfun->decl) && cfun->machine->intr_pushm)
4083         emit_insn (gen_popm (GEN_INT (cfun->machine->intr_pushm)));
4084
4085       /* The FREIT (Fast REturn from InTerrupt) instruction should be
4086          generated only for M32C/M32CM targets (generate the REIT
4087          instruction otherwise).  */
4088       if (fast_interrupt_p (cfun->decl))
4089         {
4090           /* Check if fast_attribute is set for M32C or M32CM.  */
4091           if (TARGET_A24)
4092             {
4093               emit_jump_insn (gen_epilogue_freit ());
4094             }
4095           /* If fast_interrupt attribute is set for an R8C or M16C
4096              target ignore this attribute and generated REIT
4097              instruction.  */
4098           else
4099             {
4100               warning (OPT_Wattributes,
4101                        "%<fast_interrupt%> attribute directive ignored");
4102               emit_jump_insn (gen_epilogue_reit_16 ());
4103             }
4104         }
4105       else if (TARGET_A16)
4106         emit_jump_insn (gen_epilogue_reit_16 ());
4107       else
4108         emit_jump_insn (gen_epilogue_reit_24 ());
4109     }
4110   else if (cfun->machine->use_rts)
4111     emit_jump_insn (gen_epilogue_rts ());
4112   else if (TARGET_A16)
4113     emit_jump_insn (gen_epilogue_exitd_16 ());
4114   else
4115     emit_jump_insn (gen_epilogue_exitd_24 ());
4116   emit_barrier ();
4117 }
4118
4119 void
4120 m32c_emit_eh_epilogue (rtx ret_addr)
4121 {
4122   /* R0[R2] has the stack adjustment.  R1[R3] has the address to
4123      return to.  We have to fudge the stack, pop everything, pop SP
4124      (fudged), and return (fudged).  This is actually easier to do in
4125      assembler, so punt to libgcc.  */
4126   emit_jump_insn (gen_eh_epilogue (ret_addr, cfun->machine->eh_stack_adjust));
4127   /*  emit_clobber (gen_rtx_REG (HImode, R0L_REGNO)); */
4128   emit_barrier ();
4129 }
4130
4131 /* Indicate which flags must be properly set for a given conditional.  */
4132 static int
4133 flags_needed_for_conditional (rtx cond)
4134 {
4135   switch (GET_CODE (cond))
4136     {
4137     case LE:
4138     case GT:
4139       return FLAGS_OSZ;
4140     case LEU:
4141     case GTU:
4142       return FLAGS_ZC;
4143     case LT:
4144     case GE:
4145       return FLAGS_OS;
4146     case LTU:
4147     case GEU:
4148       return FLAGS_C;
4149     case EQ:
4150     case NE:
4151       return FLAGS_Z;
4152     default:
4153       return FLAGS_N;
4154     }
4155 }
4156
4157 #define DEBUG_CMP 0
4158
4159 /* Returns true if a compare insn is redundant because it would only
4160    set flags that are already set correctly.  */
4161 static bool
4162 m32c_compare_redundant (rtx cmp, rtx *operands)
4163 {
4164   int flags_needed;
4165   int pflags;
4166   rtx prev, pp, next;
4167   rtx op0, op1, op2;
4168 #if DEBUG_CMP
4169   int prev_icode, i;
4170 #endif
4171
4172   op0 = operands[0];
4173   op1 = operands[1];
4174   op2 = operands[2];
4175
4176 #if DEBUG_CMP
4177   fprintf(stderr, "\n\033[32mm32c_compare_redundant\033[0m\n");
4178   debug_rtx(cmp);
4179   for (i=0; i<2; i++)
4180     {
4181       fprintf(stderr, "operands[%d] = ", i);
4182       debug_rtx(operands[i]);
4183     }
4184 #endif
4185
4186   next = next_nonnote_insn (cmp);
4187   if (!next || !INSN_P (next))
4188     {
4189 #if DEBUG_CMP
4190       fprintf(stderr, "compare not followed by insn\n");
4191       debug_rtx(next);
4192 #endif
4193       return false;
4194     }
4195   if (GET_CODE (PATTERN (next)) == SET
4196       && GET_CODE (XEXP ( PATTERN (next), 1)) == IF_THEN_ELSE)
4197     {
4198       next = XEXP (XEXP (PATTERN (next), 1), 0);
4199     }
4200   else if (GET_CODE (PATTERN (next)) == SET)
4201     {
4202       /* If this is a conditional, flags_needed will be something
4203          other than FLAGS_N, which we test below.  */
4204       next = XEXP (PATTERN (next), 1);
4205     }
4206   else
4207     {
4208 #if DEBUG_CMP
4209       fprintf(stderr, "compare not followed by conditional\n");
4210       debug_rtx(next);
4211 #endif
4212       return false;
4213     }
4214 #if DEBUG_CMP
4215   fprintf(stderr, "conditional is: ");
4216   debug_rtx(next);
4217 #endif
4218
4219   flags_needed = flags_needed_for_conditional (next);
4220   if (flags_needed == FLAGS_N)
4221     {
4222 #if DEBUG_CMP
4223       fprintf(stderr, "compare not followed by conditional\n");
4224       debug_rtx(next);
4225 #endif
4226       return false;
4227     }
4228
4229   /* Compare doesn't set overflow and carry the same way that
4230      arithmetic instructions do, so we can't replace those.  */
4231   if (flags_needed & FLAGS_OC)
4232     return false;
4233
4234   prev = cmp;
4235   do {
4236     prev = prev_nonnote_insn (prev);
4237     if (!prev)
4238       {
4239 #if DEBUG_CMP
4240         fprintf(stderr, "No previous insn.\n");
4241 #endif
4242         return false;
4243       }
4244     if (!INSN_P (prev))
4245       {
4246 #if DEBUG_CMP
4247         fprintf(stderr, "Previous insn is a non-insn.\n");
4248 #endif
4249         return false;
4250       }
4251     pp = PATTERN (prev);
4252     if (GET_CODE (pp) != SET)
4253       {
4254 #if DEBUG_CMP
4255         fprintf(stderr, "Previous insn is not a SET.\n");
4256 #endif
4257         return false;
4258       }
4259     pflags = get_attr_flags (prev);
4260
4261     /* Looking up attributes of previous insns corrupted the recog
4262        tables.  */
4263     INSN_UID (cmp) = -1;
4264     recog (PATTERN (cmp), cmp, 0);
4265
4266     if (pflags == FLAGS_N
4267         && reg_mentioned_p (op0, pp))
4268       {
4269 #if DEBUG_CMP
4270         fprintf(stderr, "intermediate non-flags insn uses op:\n");
4271         debug_rtx(prev);
4272 #endif
4273         return false;
4274       }
4275
4276     /* Check for comparisons against memory - between volatiles and
4277        aliases, we just can't risk this one.  */
4278     if (GET_CODE (operands[0]) == MEM
4279         || GET_CODE (operands[0]) == MEM)
4280       {
4281 #if DEBUG_CMP
4282         fprintf(stderr, "comparisons with memory:\n");
4283         debug_rtx(prev);
4284 #endif
4285         return false;
4286       }
4287
4288     /* Check for PREV changing a register that's used to compute a
4289        value in CMP, even if it doesn't otherwise change flags.  */
4290     if (GET_CODE (operands[0]) == REG
4291         && rtx_referenced_p (SET_DEST (PATTERN (prev)), operands[0]))
4292       {
4293 #if DEBUG_CMP
4294         fprintf(stderr, "sub-value affected, op0:\n");
4295         debug_rtx(prev);
4296 #endif
4297         return false;
4298       }
4299     if (GET_CODE (operands[1]) == REG
4300         && rtx_referenced_p (SET_DEST (PATTERN (prev)), operands[1]))
4301       {
4302 #if DEBUG_CMP
4303         fprintf(stderr, "sub-value affected, op1:\n");
4304         debug_rtx(prev);
4305 #endif
4306         return false;
4307       }
4308
4309   } while (pflags == FLAGS_N);
4310 #if DEBUG_CMP
4311   fprintf(stderr, "previous flag-setting insn:\n");
4312   debug_rtx(prev);
4313   debug_rtx(pp);
4314 #endif
4315
4316   if (GET_CODE (pp) == SET
4317       && GET_CODE (XEXP (pp, 0)) == REG
4318       && REGNO (XEXP (pp, 0)) == FLG_REGNO
4319       && GET_CODE (XEXP (pp, 1)) == COMPARE)
4320     {
4321       /* Adjacent cbranches must have the same operands to be
4322          redundant.  */
4323       rtx pop0 = XEXP (XEXP (pp, 1), 0);
4324       rtx pop1 = XEXP (XEXP (pp, 1), 1);
4325 #if DEBUG_CMP
4326       fprintf(stderr, "adjacent cbranches\n");
4327       debug_rtx(pop0);
4328       debug_rtx(pop1);
4329 #endif
4330       if (rtx_equal_p (op0, pop0)
4331           && rtx_equal_p (op1, pop1))
4332         return true;
4333 #if DEBUG_CMP
4334       fprintf(stderr, "prev cmp not same\n");
4335 #endif
4336       return false;
4337     }
4338
4339   /* Else the previous insn must be a SET, with either the source or
4340      dest equal to operands[0], and operands[1] must be zero.  */
4341
4342   if (!rtx_equal_p (op1, const0_rtx))
4343     {
4344 #if DEBUG_CMP
4345       fprintf(stderr, "operands[1] not const0_rtx\n");
4346 #endif
4347       return false;
4348     }
4349   if (GET_CODE (pp) != SET)
4350     {
4351 #if DEBUG_CMP
4352       fprintf (stderr, "pp not set\n");
4353 #endif
4354       return false;
4355     }
4356   if (!rtx_equal_p (op0, SET_SRC (pp))
4357       && !rtx_equal_p (op0, SET_DEST (pp)))
4358     {
4359 #if DEBUG_CMP
4360       fprintf(stderr, "operands[0] not found in set\n");
4361 #endif
4362       return false;
4363     }
4364
4365 #if DEBUG_CMP
4366   fprintf(stderr, "cmp flags %x prev flags %x\n", flags_needed, pflags);
4367 #endif
4368   if ((pflags & flags_needed) == flags_needed)
4369     return true;
4370
4371   return false;
4372 }
4373
4374 /* Return the pattern for a compare.  This will be commented out if
4375    the compare is redundant, else a normal pattern is returned.  Thus,
4376    the assembler output says where the compare would have been.  */
4377 char *
4378 m32c_output_compare (rtx insn, rtx *operands)
4379 {
4380   static char templ[] = ";cmp.b\t%1,%0";
4381   /*                             ^ 5  */
4382
4383   templ[5] = " bwll"[GET_MODE_SIZE(GET_MODE(operands[0]))];
4384   if (m32c_compare_redundant (insn, operands))
4385     {
4386 #if DEBUG_CMP
4387       fprintf(stderr, "cbranch: cmp not needed\n");
4388 #endif
4389       return templ;
4390     }
4391
4392 #if DEBUG_CMP
4393   fprintf(stderr, "cbranch: cmp needed: `%s'\n", templ + 1);
4394 #endif
4395   return templ + 1;
4396 }
4397
4398 #undef TARGET_ENCODE_SECTION_INFO
4399 #define TARGET_ENCODE_SECTION_INFO m32c_encode_section_info
4400
4401 /* If the frame pointer isn't used, we detect it manually.  But the
4402    stack pointer doesn't have as flexible addressing as the frame
4403    pointer, so we always assume we have it.  */
4404
4405 #undef TARGET_FRAME_POINTER_REQUIRED
4406 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
4407
4408 /* The Global `targetm' Variable. */
4409
4410 struct gcc_target targetm = TARGET_INITIALIZER;
4411
4412 #include "gt-m32c.h"