OSDN Git Service

config/m32r.c (m32r_legitimize_pic_address): Changed to support GOTOFF
[pf3gnuchains/gcc-fork.git] / gcc / config / m32r / m32r.c
1 /* Subroutines used for code generation on the Renesas M32R cpu.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 2, or (at your
10    option) any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "expr.h"
37 #include "function.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "ggc.h"
41 #include "integrate.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "target-def.h"
45
46 /* Save the operands last given to a compare for use when we
47    generate a scc or bcc insn.  */
48 rtx m32r_compare_op0, m32r_compare_op1;
49
50 /* Array of valid operand punctuation characters.  */
51 char m32r_punct_chars[256];
52
53 /* Selected code model.  */
54 const char * m32r_model_string = M32R_MODEL_DEFAULT;
55 enum m32r_model m32r_model;
56
57 /* Selected SDA support.  */
58 const char * m32r_sdata_string = M32R_SDATA_DEFAULT;
59 enum m32r_sdata m32r_sdata;
60
61 /* Machine-specific symbol_ref flags.  */
62 #define SYMBOL_FLAG_MODEL_SHIFT         SYMBOL_FLAG_MACH_DEP_SHIFT
63 #define SYMBOL_REF_MODEL(X) \
64   ((enum m32r_model) ((SYMBOL_REF_FLAGS (X) >> SYMBOL_FLAG_MODEL_SHIFT) & 3))
65
66 /* For string literals, etc.  */
67 #define LIT_NAME_P(NAME) ((NAME)[0] == '*' && (NAME)[1] == '.')
68
69 /* Cache-flush support. Cache-flush is used at trampoline.
70    Default cache-flush is "trap 12".
71     default cache-flush function is "_flush_cache"  (CACHE_FLUSH_FUNC)
72     default cache-flush trap-interrupt number is "12". (CACHE_FLUSH_TRAP)
73    You can change how to generate code of cache-flush with following options.
74    -flush-func=FLUSH-FUNC-NAME
75    -no-flush-func
76    -fluch-trap=TRAP-NUMBER
77    -no-flush-trap.  */
78 const char *m32r_cache_flush_func = CACHE_FLUSH_FUNC;
79 const char *m32r_cache_flush_trap_string = CACHE_FLUSH_TRAP;
80 int m32r_cache_flush_trap = 12;
81
82 /* Forward declaration.  */
83 static void  init_reg_tables (void);
84 static void  block_move_call (rtx, rtx, rtx);
85 static int   m32r_is_insn (rtx);
86 const struct attribute_spec m32r_attribute_table[];
87 static tree  m32r_handle_model_attribute (tree *, tree, tree, int, bool *);
88 static void  m32r_output_function_prologue (FILE *, HOST_WIDE_INT);
89 static void  m32r_output_function_epilogue (FILE *, HOST_WIDE_INT);
90
91 static void  m32r_file_start (void);
92
93 static int    m32r_adjust_priority (rtx, int);
94 static int    m32r_issue_rate (void);
95
96 static void m32r_encode_section_info (tree, rtx, int);
97 static bool m32r_in_small_data_p (tree);
98 static bool m32r_return_in_memory (tree, tree);
99 static void m32r_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
100                                          tree, int *, int);
101 static void init_idents (void);
102 static bool m32r_rtx_costs (rtx, int, int, int *);
103 static bool m32r_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
104                                     tree, bool);
105 \f
106 /* Initialize the GCC target structure.  */
107 #undef  TARGET_ATTRIBUTE_TABLE
108 #define TARGET_ATTRIBUTE_TABLE m32r_attribute_table
109
110 #undef  TARGET_ASM_ALIGNED_HI_OP
111 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
112 #undef  TARGET_ASM_ALIGNED_SI_OP
113 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
114
115 #undef  TARGET_ASM_FUNCTION_PROLOGUE
116 #define TARGET_ASM_FUNCTION_PROLOGUE m32r_output_function_prologue
117 #undef  TARGET_ASM_FUNCTION_EPILOGUE
118 #define TARGET_ASM_FUNCTION_EPILOGUE m32r_output_function_epilogue
119
120 #undef  TARGET_ASM_FILE_START
121 #define TARGET_ASM_FILE_START m32r_file_start
122
123 #undef  TARGET_SCHED_ADJUST_PRIORITY
124 #define TARGET_SCHED_ADJUST_PRIORITY m32r_adjust_priority
125 #undef  TARGET_SCHED_ISSUE_RATE
126 #define TARGET_SCHED_ISSUE_RATE m32r_issue_rate
127
128 #undef  TARGET_ENCODE_SECTION_INFO
129 #define TARGET_ENCODE_SECTION_INFO m32r_encode_section_info
130 #undef  TARGET_IN_SMALL_DATA_P
131 #define TARGET_IN_SMALL_DATA_P m32r_in_small_data_p
132
133 #undef  TARGET_RTX_COSTS
134 #define TARGET_RTX_COSTS m32r_rtx_costs
135 #undef  TARGET_ADDRESS_COST
136 #define TARGET_ADDRESS_COST hook_int_rtx_0
137
138 #undef  TARGET_PROMOTE_PROTOTYPES
139 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
140 #undef  TARGET_RETURN_IN_MEMORY
141 #define TARGET_RETURN_IN_MEMORY m32r_return_in_memory
142 #undef  TARGET_SETUP_INCOMING_VARARGS
143 #define TARGET_SETUP_INCOMING_VARARGS m32r_setup_incoming_varargs
144 #undef  TARGET_MUST_PASS_IN_STACK
145 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
146 #undef  TARGET_PASS_BY_REFERENCE
147 #define TARGET_PASS_BY_REFERENCE m32r_pass_by_reference
148
149 struct gcc_target targetm = TARGET_INITIALIZER;
150 \f
151 /* Called by OVERRIDE_OPTIONS to initialize various things.  */
152
153 void
154 m32r_init (void)
155 {
156   init_reg_tables ();
157
158   /* Initialize array for PRINT_OPERAND_PUNCT_VALID_P.  */
159   memset (m32r_punct_chars, 0, sizeof (m32r_punct_chars));
160   m32r_punct_chars['#'] = 1;
161   m32r_punct_chars['@'] = 1; /* ??? no longer used */
162
163   /* Provide default value if not specified.  */
164   if (!g_switch_set)
165     g_switch_value = SDATA_DEFAULT_SIZE;
166
167   if (strcmp (m32r_model_string, "small") == 0)
168     m32r_model = M32R_MODEL_SMALL;
169   else if (strcmp (m32r_model_string, "medium") == 0)
170     m32r_model = M32R_MODEL_MEDIUM;
171   else if (strcmp (m32r_model_string, "large") == 0)
172     m32r_model = M32R_MODEL_LARGE;
173   else
174     error ("bad value (%s) for -mmodel switch", m32r_model_string);
175
176   if (strcmp (m32r_sdata_string, "none") == 0)
177     m32r_sdata = M32R_SDATA_NONE;
178   else if (strcmp (m32r_sdata_string, "sdata") == 0)
179     m32r_sdata = M32R_SDATA_SDATA;
180   else if (strcmp (m32r_sdata_string, "use") == 0)
181     m32r_sdata = M32R_SDATA_USE;
182   else
183     error ("bad value (%s) for -msdata switch", m32r_sdata_string);
184
185   if (m32r_cache_flush_trap_string)
186     {
187       /* Change trap-number (12) for cache-flush to the others (0 - 15).  */
188       m32r_cache_flush_trap = atoi (m32r_cache_flush_trap_string);
189       if (m32r_cache_flush_trap < 0 || m32r_cache_flush_trap > 15)
190         error ("bad value (%s) for -flush-trap=n (0=<n<=15)",
191                m32r_cache_flush_trap_string);
192     }
193 }
194
195 /* Vectors to keep interesting information about registers where it can easily
196    be got.  We use to use the actual mode value as the bit number, but there
197    is (or may be) more than 32 modes now.  Instead we use two tables: one
198    indexed by hard register number, and one indexed by mode.  */
199
200 /* The purpose of m32r_mode_class is to shrink the range of modes so that
201    they all fit (as bit numbers) in a 32 bit word (again).  Each real mode is
202    mapped into one m32r_mode_class mode.  */
203
204 enum m32r_mode_class
205 {
206   C_MODE,
207   S_MODE, D_MODE, T_MODE, O_MODE,
208   SF_MODE, DF_MODE, TF_MODE, OF_MODE, A_MODE
209 };
210
211 /* Modes for condition codes.  */
212 #define C_MODES (1 << (int) C_MODE)
213
214 /* Modes for single-word and smaller quantities.  */
215 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
216
217 /* Modes for double-word and smaller quantities.  */
218 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
219
220 /* Modes for quad-word and smaller quantities.  */
221 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
222
223 /* Modes for accumulators.  */
224 #define A_MODES (1 << (int) A_MODE)
225
226 /* Value is 1 if register/mode pair is acceptable on arc.  */
227
228 const unsigned int m32r_hard_regno_mode_ok[FIRST_PSEUDO_REGISTER] =
229 {
230   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
231   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, S_MODES, S_MODES, S_MODES,
232   S_MODES, C_MODES, A_MODES, A_MODES
233 };
234
235 unsigned int m32r_mode_class [NUM_MACHINE_MODES];
236
237 enum reg_class m32r_regno_reg_class[FIRST_PSEUDO_REGISTER];
238
239 static void
240 init_reg_tables (void)
241 {
242   int i;
243
244   for (i = 0; i < NUM_MACHINE_MODES; i++)
245     {
246       switch (GET_MODE_CLASS (i))
247         {
248         case MODE_INT:
249         case MODE_PARTIAL_INT:
250         case MODE_COMPLEX_INT:
251           if (GET_MODE_SIZE (i) <= 4)
252             m32r_mode_class[i] = 1 << (int) S_MODE;
253           else if (GET_MODE_SIZE (i) == 8)
254             m32r_mode_class[i] = 1 << (int) D_MODE;
255           else if (GET_MODE_SIZE (i) == 16)
256             m32r_mode_class[i] = 1 << (int) T_MODE;
257           else if (GET_MODE_SIZE (i) == 32)
258             m32r_mode_class[i] = 1 << (int) O_MODE;
259           else 
260             m32r_mode_class[i] = 0;
261           break;
262         case MODE_FLOAT:
263         case MODE_COMPLEX_FLOAT:
264           if (GET_MODE_SIZE (i) <= 4)
265             m32r_mode_class[i] = 1 << (int) SF_MODE;
266           else if (GET_MODE_SIZE (i) == 8)
267             m32r_mode_class[i] = 1 << (int) DF_MODE;
268           else if (GET_MODE_SIZE (i) == 16)
269             m32r_mode_class[i] = 1 << (int) TF_MODE;
270           else if (GET_MODE_SIZE (i) == 32)
271             m32r_mode_class[i] = 1 << (int) OF_MODE;
272           else 
273             m32r_mode_class[i] = 0;
274           break;
275         case MODE_CC:
276           m32r_mode_class[i] = 1 << (int) C_MODE;
277           break;
278         default:
279           m32r_mode_class[i] = 0;
280           break;
281         }
282     }
283
284   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
285     {
286       if (GPR_P (i))
287         m32r_regno_reg_class[i] = GENERAL_REGS;
288       else if (i == ARG_POINTER_REGNUM)
289         m32r_regno_reg_class[i] = GENERAL_REGS;
290       else
291         m32r_regno_reg_class[i] = NO_REGS;
292     }
293 }
294 \f
295 /* M32R specific attribute support.
296
297    interrupt - for interrupt functions
298
299    model - select code model used to access object
300
301         small: addresses use 24 bits, use bl to make calls
302         medium: addresses use 32 bits, use bl to make calls
303         large: addresses use 32 bits, use seth/add3/jl to make calls
304
305         Grep for MODEL in m32r.h for more info.  */
306
307 static tree small_ident1;
308 static tree small_ident2;
309 static tree medium_ident1;
310 static tree medium_ident2;
311 static tree large_ident1;
312 static tree large_ident2;
313
314 static void
315 init_idents (void)
316 {
317   if (small_ident1 == 0)
318     {
319       small_ident1 = get_identifier ("small");
320       small_ident2 = get_identifier ("__small__");
321       medium_ident1 = get_identifier ("medium");
322       medium_ident2 = get_identifier ("__medium__");
323       large_ident1 = get_identifier ("large");
324       large_ident2 = get_identifier ("__large__");
325     }
326 }
327
328 const struct attribute_spec m32r_attribute_table[] =
329 {
330   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
331   { "interrupt", 0, 0, true,  false, false, NULL },
332   { "model",     1, 1, true,  false, false, m32r_handle_model_attribute },
333   { NULL,        0, 0, false, false, false, NULL }
334 };
335
336
337 /* Handle an "model" attribute; arguments as in
338    struct attribute_spec.handler.  */
339 static tree
340 m32r_handle_model_attribute (tree *node ATTRIBUTE_UNUSED, tree name,
341                              tree args, int flags ATTRIBUTE_UNUSED,
342                              bool *no_add_attrs)
343 {
344   tree arg;
345
346   init_idents ();
347   arg = TREE_VALUE (args);
348
349   if (arg != small_ident1
350       && arg != small_ident2
351       && arg != medium_ident1
352       && arg != medium_ident2
353       && arg != large_ident1
354       && arg != large_ident2)
355     {
356       warning ("invalid argument of %qs attribute",
357                IDENTIFIER_POINTER (name));
358       *no_add_attrs = true;
359     }
360
361   return NULL_TREE;
362 }
363 \f
364 /* Encode section information of DECL, which is either a VAR_DECL,
365    FUNCTION_DECL, STRING_CST, CONSTRUCTOR, or ???.
366
367    For the M32R we want to record:
368
369    - whether the object lives in .sdata/.sbss.
370    - what code model should be used to access the object
371 */
372
373 static void
374 m32r_encode_section_info (tree decl, rtx rtl, int first)
375 {
376   int extra_flags = 0;
377   tree model_attr;
378   enum m32r_model model;
379
380   default_encode_section_info (decl, rtl, first);
381
382   if (!DECL_P (decl))
383     return;
384
385   model_attr = lookup_attribute ("model", DECL_ATTRIBUTES (decl));
386   if (model_attr)
387     {
388       tree id;
389
390       init_idents ();
391
392       id = TREE_VALUE (TREE_VALUE (model_attr));
393
394       if (id == small_ident1 || id == small_ident2)
395         model = M32R_MODEL_SMALL;
396       else if (id == medium_ident1 || id == medium_ident2)
397         model = M32R_MODEL_MEDIUM;
398       else if (id == large_ident1 || id == large_ident2)
399         model = M32R_MODEL_LARGE;
400       else
401         abort (); /* shouldn't happen */
402     }
403   else
404     {
405       if (TARGET_MODEL_SMALL)
406         model = M32R_MODEL_SMALL;
407       else if (TARGET_MODEL_MEDIUM)
408         model = M32R_MODEL_MEDIUM;
409       else if (TARGET_MODEL_LARGE)
410         model = M32R_MODEL_LARGE;
411       else
412         abort (); /* shouldn't happen */
413     }
414   extra_flags |= model << SYMBOL_FLAG_MODEL_SHIFT;
415
416   if (extra_flags)
417     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
418 }
419
420 /* Only mark the object as being small data area addressable if
421    it hasn't been explicitly marked with a code model.
422
423    The user can explicitly put an object in the small data area with the
424    section attribute.  If the object is in sdata/sbss and marked with a
425    code model do both [put the object in .sdata and mark it as being
426    addressed with a specific code model - don't mark it as being addressed
427    with an SDA reloc though].  This is ok and might be useful at times.  If
428    the object doesn't fit the linker will give an error.  */
429
430 static bool
431 m32r_in_small_data_p (tree decl)
432 {
433   tree section;
434
435   if (TREE_CODE (decl) != VAR_DECL)
436     return false;
437
438   if (lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
439     return false;
440
441   section = DECL_SECTION_NAME (decl);
442   if (section)
443     {
444       char *name = (char *) TREE_STRING_POINTER (section);
445       if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
446         return true;
447     }
448   else
449     {
450       if (! TREE_READONLY (decl) && ! TARGET_SDATA_NONE)
451         {
452           int size = int_size_in_bytes (TREE_TYPE (decl));
453
454           if (size > 0 && (unsigned HOST_WIDE_INT) size <= g_switch_value)
455             return true;
456         }
457     }
458
459   return false;
460 }
461
462 /* Do anything needed before RTL is emitted for each function.  */
463
464 void
465 m32r_init_expanders (void)
466 {
467   /* ??? At one point there was code here.  The function is left in
468      to make it easy to experiment.  */
469 }
470 \f
471 /* Acceptable arguments to the call insn.  */
472
473 int
474 call_address_operand (rtx op, enum machine_mode mode)
475 {
476   return symbolic_operand (op, mode);
477
478 /* Constants and values in registers are not OK, because
479    the m32r BL instruction can only support PC relative branching.  */ 
480 }
481
482 int
483 call_operand (rtx op, enum machine_mode mode)
484 {
485   if (GET_CODE (op) != MEM)
486     return 0;
487   op = XEXP (op, 0);
488   return call_address_operand (op, mode);
489 }
490
491 /* Returns 1 if OP is a symbol reference.  */
492
493 int
494 symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
495 {
496   switch (GET_CODE (op))
497     {
498     case SYMBOL_REF:
499     case LABEL_REF:
500     case CONST :
501       return 1;
502
503     default:
504       return 0;
505     }
506 }
507
508 /* Return 1 if OP is a reference to an object in .sdata/.sbss.  */
509
510 int
511 small_data_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
512 {
513   if (! TARGET_SDATA_USE)
514     return 0;
515
516   if (GET_CODE (op) == SYMBOL_REF)
517     return SYMBOL_REF_SMALL_P (op);
518
519   if (GET_CODE (op) == CONST
520       && GET_CODE (XEXP (op, 0)) == PLUS
521       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
522       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
523       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
524     return SYMBOL_REF_SMALL_P (XEXP (XEXP (op, 0), 0));
525
526   return 0;
527 }
528
529 /* Return 1 if OP is a symbol that can use 24 bit addressing.  */
530
531 int
532 addr24_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
533 {
534   rtx sym;
535
536   if (flag_pic)
537     return 0;
538
539   if (GET_CODE (op) == LABEL_REF)
540     return TARGET_ADDR24;
541
542   if (GET_CODE (op) == SYMBOL_REF)
543     sym = op;
544   else if (GET_CODE (op) == CONST
545            && GET_CODE (XEXP (op, 0)) == PLUS
546            && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
547            && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
548            && UINT24_P (INTVAL (XEXP (XEXP (op, 0), 1))))
549     sym = XEXP (XEXP (op, 0), 0);
550   else
551     return 0;
552
553   if (SYMBOL_REF_MODEL (sym) == M32R_MODEL_SMALL)
554     return 1;
555
556   if (TARGET_ADDR24
557       && (CONSTANT_POOL_ADDRESS_P (sym)
558           || LIT_NAME_P (XSTR (sym, 0))))
559     return 1;
560
561   return 0;
562 }
563
564 /* Return 1 if OP is a symbol that needs 32 bit addressing.  */
565
566 int
567 addr32_operand (rtx op, enum machine_mode mode)
568 {
569   rtx sym;
570
571   if (GET_CODE (op) == LABEL_REF)
572     return TARGET_ADDR32;
573
574   if (GET_CODE (op) == SYMBOL_REF)
575     sym = op;
576   else if (GET_CODE (op) == CONST
577            && GET_CODE (XEXP (op, 0)) == PLUS
578            && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
579            && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
580            && ! flag_pic)
581     sym = XEXP (XEXP (op, 0), 0);
582   else
583     return 0;
584
585   return (! addr24_operand (sym, mode)
586           && ! small_data_operand (sym, mode));
587 }
588
589 /* Return 1 if OP is a function that can be called with the `bl' insn.  */
590
591 int
592 call26_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
593 {
594   if (flag_pic)
595     return 1;
596
597   if (GET_CODE (op) == SYMBOL_REF)
598     return SYMBOL_REF_MODEL (op) != M32R_MODEL_LARGE;
599
600   return TARGET_CALL26;
601 }
602
603 /* Returns 1 if OP is an acceptable operand for seth/add3.  */
604
605 int
606 seth_add3_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
607 {
608   if (flag_pic)
609     return 0;
610
611   if (GET_CODE (op) == SYMBOL_REF
612       || GET_CODE (op) == LABEL_REF)
613     return 1;
614
615   if (GET_CODE (op) == CONST
616       && GET_CODE (XEXP (op, 0)) == PLUS
617       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
618       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
619       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
620     return 1;
621
622   return 0;
623 }
624
625 /* Return true if OP is a signed 8 bit immediate value.  */
626
627 int
628 int8_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
629 {
630   if (GET_CODE (op) != CONST_INT)
631     return 0;
632   return INT8_P (INTVAL (op));
633 }
634
635 /* Return true if OP is a signed 16 bit immediate value
636    useful in comparisons.  */
637
638 int
639 cmp_int16_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
640 {
641   if (GET_CODE (op) != CONST_INT)
642     return 0;
643   return CMP_INT16_P (INTVAL (op));
644 }
645
646 /* Return true if OP is an unsigned 16 bit immediate value.  */
647
648 int
649 uint16_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
650 {
651   if (GET_CODE (op) != CONST_INT)
652     return 0;
653   return UINT16_P (INTVAL (op));
654 }
655
656 /* Return true if OP is a register or signed 16 bit value.  */
657
658 int
659 reg_or_int16_operand (rtx op, enum machine_mode mode)
660 {
661   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
662     return register_operand (op, mode);
663   if (GET_CODE (op) != CONST_INT)
664     return 0;
665   return INT16_P (INTVAL (op));
666 }
667
668 /* Return true if OP is a register or an unsigned 16 bit value.  */
669
670 int
671 reg_or_uint16_operand (rtx op, enum machine_mode mode)
672 {
673   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
674     return register_operand (op, mode);
675   if (GET_CODE (op) != CONST_INT)
676     return 0;
677   return UINT16_P (INTVAL (op));
678 }
679
680 /* Return true if OP is a register or an integer value that can be
681    used is SEQ/SNE.  We can use either XOR of the value or ADD of
682    the negative of the value for the constant.  Don't allow 0,
683    because that is special cased.  */
684
685 int
686 reg_or_eq_int16_operand (rtx op, enum machine_mode mode)
687 {
688   HOST_WIDE_INT value;
689
690   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
691     return register_operand (op, mode);
692
693   if (GET_CODE (op) != CONST_INT)
694     return 0;
695
696   value = INTVAL (op);
697   return (value != 0) && (UINT16_P (value) || CMP_INT16_P (-value));
698 }
699
700 /* Return true if OP is a register or signed 16 bit value for compares.  */
701
702 int
703 reg_or_cmp_int16_operand (rtx op, enum machine_mode mode)
704 {
705   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
706     return register_operand (op, mode);
707   if (GET_CODE (op) != CONST_INT)
708     return 0;
709   return CMP_INT16_P (INTVAL (op));
710 }
711
712 /* Return true if OP is a register or the constant 0.  */
713
714 int
715 reg_or_zero_operand (rtx op, enum machine_mode mode)
716 {
717   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
718     return register_operand (op, mode);
719
720   if (GET_CODE (op) != CONST_INT)
721     return 0;
722
723   return INTVAL (op) == 0;
724 }
725
726 /* Return true if OP is a const_int requiring two instructions to load.  */
727
728 int
729 two_insn_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
730 {
731   if (GET_CODE (op) != CONST_INT)
732     return 0;
733   if (INT16_P (INTVAL (op))
734       || UINT24_P (INTVAL (op))
735       || UPPER16_P (INTVAL (op)))
736     return 0;
737   return 1;
738 }
739
740 /* Return true if OP is an acceptable argument for a single word
741    move source.  */
742
743 int
744 move_src_operand (rtx op, enum machine_mode mode)
745 {
746   switch (GET_CODE (op))
747     {
748     case LABEL_REF :
749     case SYMBOL_REF :
750     case CONST :
751       return addr24_operand (op, mode);
752     case CONST_INT :
753       /* ??? We allow more cse opportunities if we only allow constants
754          loadable with one insn, and split the rest into two.  The instances
755          where this would help should be rare and the current way is
756          simpler.  */
757       if (HOST_BITS_PER_WIDE_INT > 32)
758         {
759           HOST_WIDE_INT rest = INTVAL (op) >> 31;
760           return (rest == 0 || rest == -1);
761         }
762       else
763         return 1;
764     case CONST_DOUBLE :
765       if (mode == SFmode)
766         return 1;
767       else if (mode == SImode)
768         {
769           /* Large unsigned constants are represented as const_double's.  */
770           unsigned HOST_WIDE_INT low, high;
771
772           low = CONST_DOUBLE_LOW (op);
773           high = CONST_DOUBLE_HIGH (op);
774           return high == 0 && low <= (unsigned) 0xffffffff;
775         }
776       else
777         return 0;
778     case REG :
779       return register_operand (op, mode);
780     case SUBREG :
781       /* (subreg (mem ...) ...) can occur here if the inner part was once a
782          pseudo-reg and is now a stack slot.  */
783       if (GET_CODE (SUBREG_REG (op)) == MEM)
784         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
785       else
786         return register_operand (op, mode);
787     case MEM :
788       if (GET_CODE (XEXP (op, 0)) == PRE_INC
789           || GET_CODE (XEXP (op, 0)) == PRE_DEC)
790         return 0;               /* loads can't do pre-{inc,dec} */
791       return address_operand (XEXP (op, 0), mode);
792     default :
793       return 0;
794     }
795 }
796
797 /* Return true if OP is an acceptable argument for a double word
798    move source.  */
799
800 int
801 move_double_src_operand (rtx op, enum machine_mode mode)
802 {
803   switch (GET_CODE (op))
804     {
805     case CONST_INT :
806     case CONST_DOUBLE :
807       return 1;
808     case REG :
809       return register_operand (op, mode);
810     case SUBREG :
811       /* (subreg (mem ...) ...) can occur here if the inner part was once a
812          pseudo-reg and is now a stack slot.  */
813       if (GET_CODE (SUBREG_REG (op)) == MEM)
814         return move_double_src_operand (SUBREG_REG (op), mode);
815       else
816         return register_operand (op, mode);
817     case MEM :
818       /* Disallow auto inc/dec for now.  */
819       if (GET_CODE (XEXP (op, 0)) == PRE_DEC
820           || GET_CODE (XEXP (op, 0)) == PRE_INC)
821         return 0;
822       return address_operand (XEXP (op, 0), mode);
823     default :
824       return 0;
825     }
826 }
827
828 /* Return true if OP is an acceptable argument for a move destination.  */
829
830 int
831 move_dest_operand (rtx op, enum machine_mode mode)
832 {
833   switch (GET_CODE (op))
834     {
835     case REG :
836       return register_operand (op, mode);
837     case SUBREG :
838       /* (subreg (mem ...) ...) can occur here if the inner part was once a
839          pseudo-reg and is now a stack slot.  */
840       if (GET_CODE (SUBREG_REG (op)) == MEM)
841         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
842       else
843         return register_operand (op, mode);
844     case MEM :
845       if (GET_CODE (XEXP (op, 0)) == POST_INC)
846         return 0;               /* stores can't do post inc */
847       return address_operand (XEXP (op, 0), mode);
848     default :
849       return 0;
850     }
851 }
852
853 /* Return 1 if OP is a DImode const we want to handle inline.
854    This must match the code in the movdi pattern.
855    It is used by the 'G' CONST_DOUBLE_OK_FOR_LETTER.  */
856
857 int
858 easy_di_const (rtx op)
859 {
860   rtx high_rtx, low_rtx;
861   HOST_WIDE_INT high, low;
862
863   split_double (op, &high_rtx, &low_rtx);
864   high = INTVAL (high_rtx);
865   low = INTVAL (low_rtx);
866   /* Pick constants loadable with 2 16 bit `ldi' insns.  */
867   if (high >= -128 && high <= 127
868       && low >= -128 && low <= 127)
869     return 1;
870   return 0;
871 }
872
873 /* Return 1 if OP is a DFmode const we want to handle inline.
874    This must match the code in the movdf pattern.
875    It is used by the 'H' CONST_DOUBLE_OK_FOR_LETTER.  */
876
877 int
878 easy_df_const (rtx op)
879 {
880   REAL_VALUE_TYPE r;
881   long l[2];
882
883   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
884   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
885   if (l[0] == 0 && l[1] == 0)
886     return 1;
887   if ((l[0] & 0xffff) == 0 && l[1] == 0)
888     return 1;
889   return 0;
890 }
891
892 /* Return 1 if OP is an EQ or NE comparison operator.  */
893
894 int
895 eqne_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
896 {
897   enum rtx_code code = GET_CODE (op);
898
899   return (code == EQ || code == NE);
900 }
901
902 /* Return 1 if OP is a signed comparison operator.  */
903
904 int
905 signed_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
906 {
907   enum rtx_code code = GET_CODE (op);
908
909   return (COMPARISON_P (op)
910           && (code == EQ || code == NE
911               || code == LT || code == LE || code == GT || code == GE));
912 }
913
914 /* Return 1 if OP is (mem (reg ...)).
915    This is used in insn length calcs.  */
916
917 int
918 memreg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
919 {
920   return GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == REG;
921 }
922
923 /* Return true if OP is an acceptable input argument for a zero/sign extend
924    operation.  */
925
926 int
927 extend_operand (rtx op, enum machine_mode mode)
928 {
929   rtx addr;
930
931   switch (GET_CODE (op))
932     {
933     case REG :
934     case SUBREG :
935       return register_operand (op, mode);
936
937     case MEM :
938       addr = XEXP (op, 0);
939       if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
940         return 0;               /* loads can't do pre inc/pre dec */
941
942       return address_operand (addr, mode);
943
944     default :
945       return 0;
946     }
947 }
948
949 /* Return nonzero if the operand is an insn that is a small insn.
950    Allow const_int 0 as well, which is a placeholder for NOP slots.  */
951
952 int
953 small_insn_p (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
954 {
955   if (GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
956     return 1;
957
958   if (! INSN_P (op))
959     return 0;
960
961   return get_attr_length (op) == 2;
962 }
963
964 /* Return nonzero if the operand is an insn that is a large insn.  */
965
966 int
967 large_insn_p (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
968 {
969   if (! INSN_P (op))
970     return 0;
971
972   return get_attr_length (op) != 2;
973 }
974
975 /* Return nonzero if TYPE must be passed by indirect reference.  */
976
977 static bool
978 m32r_pass_by_reference (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
979                         enum machine_mode mode, tree type,
980                         bool named ATTRIBUTE_UNUSED)
981 {
982   int size;
983
984   if (type)
985     size = int_size_in_bytes (type);
986   else
987     size = GET_MODE_SIZE (mode);
988
989   return (size < 0 || size > 8);
990 }
991 \f
992 /* Comparisons.  */
993
994 /* X and Y are two things to compare using CODE.  Emit the compare insn and
995    return the rtx for compare [arg0 of the if_then_else].
996    If need_compare is true then the comparison insn must be generated, rather
997    than being subsumed into the following branch instruction.  */
998
999 rtx
1000 gen_compare (enum rtx_code code, rtx x, rtx y, int need_compare)
1001 {
1002   enum rtx_code compare_code;
1003   enum rtx_code branch_code;
1004   rtx cc_reg = gen_rtx_REG (CCmode, CARRY_REGNUM);
1005   int must_swap = 0;
1006
1007   switch (code)
1008     {
1009     case EQ:  compare_code = EQ;  branch_code = NE; break;
1010     case NE:  compare_code = EQ;  branch_code = EQ; break;
1011     case LT:  compare_code = LT;  branch_code = NE; break;
1012     case LE:  compare_code = LT;  branch_code = EQ; must_swap = 1; break;
1013     case GT:  compare_code = LT;  branch_code = NE; must_swap = 1; break;
1014     case GE:  compare_code = LT;  branch_code = EQ; break;
1015     case LTU: compare_code = LTU; branch_code = NE; break;
1016     case LEU: compare_code = LTU; branch_code = EQ; must_swap = 1; break;
1017     case GTU: compare_code = LTU; branch_code = NE; must_swap = 1; break;
1018     case GEU: compare_code = LTU; branch_code = EQ; break;
1019
1020     default:
1021       abort ();
1022     }
1023
1024   if (need_compare)
1025     {
1026       switch (compare_code)
1027         {
1028         case EQ:
1029           if (GET_CODE (y) == CONST_INT
1030               && CMP_INT16_P (INTVAL (y))               /* Reg equal to small const.  */
1031               && y != const0_rtx)
1032             {
1033               rtx tmp = gen_reg_rtx (SImode);           
1034               
1035               emit_insn (gen_addsi3 (tmp, x, GEN_INT (-INTVAL (y))));
1036               x = tmp;
1037               y = const0_rtx;
1038             }
1039           else if (CONSTANT_P (y))                      /* Reg equal to const.  */
1040             {
1041               rtx tmp = force_reg (GET_MODE (x), y);
1042               y = tmp;
1043             }
1044
1045           if (register_operand (y, SImode)              /* Reg equal to reg.  */
1046               || y == const0_rtx)                       /* Reg equal to zero.  */
1047             {
1048               emit_insn (gen_cmp_eqsi_insn (x, y));
1049                 
1050               return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
1051             }
1052           break;
1053       
1054         case LT:
1055           if (register_operand (y, SImode)
1056               || (GET_CODE (y) == CONST_INT && CMP_INT16_P (INTVAL (y))))
1057             {
1058               rtx tmp = gen_reg_rtx (SImode);         /* Reg compared to reg.  */
1059               
1060               switch (code)
1061                 {
1062                 case LT:
1063                   emit_insn (gen_cmp_ltsi_insn (x, y));
1064                   code = EQ;
1065                   break;
1066                 case LE:
1067                   if (y == const0_rtx)
1068                     tmp = const1_rtx;
1069                   else
1070                     emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
1071                   emit_insn (gen_cmp_ltsi_insn (x, tmp));
1072                   code = EQ;
1073                   break;
1074                 case GT:
1075                   if (GET_CODE (y) == CONST_INT)
1076                     tmp = gen_rtx_PLUS (SImode, y, const1_rtx);
1077                   else
1078                     emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
1079                   emit_insn (gen_cmp_ltsi_insn (x, tmp));
1080                   code = NE;
1081                   break;
1082                 case GE:
1083                   emit_insn (gen_cmp_ltsi_insn (x, y));
1084                   code = NE;
1085                   break;
1086                 default:
1087                   abort ();
1088                 }
1089               
1090               return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
1091             }
1092           break;
1093           
1094         case LTU:
1095           if (register_operand (y, SImode)
1096               || (GET_CODE (y) == CONST_INT && CMP_INT16_P (INTVAL (y))))
1097             {
1098               rtx tmp = gen_reg_rtx (SImode);         /* Reg (unsigned) compared to reg.  */
1099               
1100               switch (code)
1101                 {
1102                 case LTU:
1103                   emit_insn (gen_cmp_ltusi_insn (x, y));
1104                   code = EQ;
1105                   break;
1106                 case LEU:
1107                   if (y == const0_rtx)
1108                     tmp = const1_rtx;
1109                   else
1110                     emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
1111                   emit_insn (gen_cmp_ltusi_insn (x, tmp));
1112                   code = EQ;
1113                   break;
1114                 case GTU:
1115                   if (GET_CODE (y) == CONST_INT)
1116                     tmp = gen_rtx_PLUS (SImode, y, const1_rtx);
1117                   else
1118                     emit_insn (gen_addsi3 (tmp, y, constm1_rtx));
1119                   emit_insn (gen_cmp_ltusi_insn (x, tmp));
1120                   code = NE;
1121                   break;
1122                 case GEU:
1123                   emit_insn (gen_cmp_ltusi_insn (x, y));
1124                   code = NE;
1125                   break;
1126                 default:
1127                   abort();
1128                 }
1129               
1130               return gen_rtx_fmt_ee (code, CCmode, cc_reg, const0_rtx);
1131             }
1132           break;
1133
1134         default:
1135           abort();
1136         }
1137     }
1138   else
1139     {
1140       /* Reg/reg equal comparison.  */
1141       if (compare_code == EQ
1142           && register_operand (y, SImode))
1143         return gen_rtx_fmt_ee (code, CCmode, x, y);
1144       
1145       /* Reg/zero signed comparison.  */
1146       if ((compare_code == EQ || compare_code == LT)
1147           && y == const0_rtx)
1148         return gen_rtx_fmt_ee (code, CCmode, x, y);
1149       
1150       /* Reg/smallconst equal comparison.  */
1151       if (compare_code == EQ
1152           && GET_CODE (y) == CONST_INT
1153           && CMP_INT16_P (INTVAL (y)))
1154         {
1155           rtx tmp = gen_reg_rtx (SImode);
1156
1157           emit_insn (gen_addsi3 (tmp, x, GEN_INT (-INTVAL (y))));
1158           return gen_rtx_fmt_ee (code, CCmode, tmp, const0_rtx);
1159         }
1160       
1161       /* Reg/const equal comparison.  */
1162       if (compare_code == EQ
1163           && CONSTANT_P (y))
1164         {
1165           rtx tmp = force_reg (GET_MODE (x), y);
1166
1167           return gen_rtx_fmt_ee (code, CCmode, x, tmp);
1168         }
1169     }
1170
1171   if (CONSTANT_P (y))
1172     {
1173       if (must_swap)
1174         y = force_reg (GET_MODE (x), y);
1175       else
1176         {
1177           int ok_const = reg_or_int16_operand (y, GET_MODE (y));
1178
1179           if (! ok_const)
1180             y = force_reg (GET_MODE (x), y);
1181         }
1182     }
1183
1184   switch (compare_code)
1185     {
1186     case EQ :
1187       emit_insn (gen_cmp_eqsi_insn (must_swap ? y : x, must_swap ? x : y));
1188       break;
1189     case LT :
1190       emit_insn (gen_cmp_ltsi_insn (must_swap ? y : x, must_swap ? x : y));
1191       break;
1192     case LTU :
1193       emit_insn (gen_cmp_ltusi_insn (must_swap ? y : x, must_swap ? x : y));
1194       break;
1195
1196     default:
1197       abort ();
1198     }
1199
1200   return gen_rtx_fmt_ee (branch_code, VOIDmode, cc_reg, CONST0_RTX (CCmode));
1201 }
1202 \f
1203 /* Split a 2 word move (DI or DF) into component parts.  */
1204
1205 rtx
1206 gen_split_move_double (rtx operands[])
1207 {
1208   enum machine_mode mode = GET_MODE (operands[0]);
1209   rtx dest = operands[0];
1210   rtx src  = operands[1];
1211   rtx val;
1212
1213   /* We might have (SUBREG (MEM)) here, so just get rid of the
1214      subregs to make this code simpler.  It is safe to call
1215      alter_subreg any time after reload.  */
1216   if (GET_CODE (dest) == SUBREG)
1217     alter_subreg (&dest);
1218   if (GET_CODE (src) == SUBREG)
1219     alter_subreg (&src);
1220
1221   start_sequence ();
1222   if (GET_CODE (dest) == REG)
1223     {
1224       int dregno = REGNO (dest);
1225
1226       /* Reg = reg.  */
1227       if (GET_CODE (src) == REG)
1228         {
1229           int sregno = REGNO (src);
1230
1231           int reverse = (dregno == sregno + 1);
1232
1233           /* We normally copy the low-numbered register first.  However, if
1234              the first register operand 0 is the same as the second register of
1235              operand 1, we must copy in the opposite order.  */
1236           emit_insn (gen_rtx_SET (VOIDmode,
1237                                   operand_subword (dest, reverse, TRUE, mode),
1238                                   operand_subword (src,  reverse, TRUE, mode)));
1239
1240           emit_insn (gen_rtx_SET (VOIDmode,
1241                                   operand_subword (dest, !reverse, TRUE, mode),
1242                                   operand_subword (src,  !reverse, TRUE, mode)));
1243         }
1244
1245       /* Reg = constant.  */
1246       else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
1247         {
1248           rtx words[2];
1249           split_double (src, &words[0], &words[1]);
1250           emit_insn (gen_rtx_SET (VOIDmode,
1251                                   operand_subword (dest, 0, TRUE, mode),
1252                                   words[0]));
1253
1254           emit_insn (gen_rtx_SET (VOIDmode,
1255                                   operand_subword (dest, 1, TRUE, mode),
1256                                   words[1]));
1257         }
1258
1259       /* Reg = mem.  */
1260       else if (GET_CODE (src) == MEM)
1261         {
1262           /* If the high-address word is used in the address, we must load it
1263              last.  Otherwise, load it first.  */
1264           int reverse
1265             = (refers_to_regno_p (dregno, dregno + 1, XEXP (src, 0), 0) != 0);
1266
1267           /* We used to optimize loads from single registers as
1268
1269                 ld r1,r3+; ld r2,r3
1270
1271              if r3 were not used subsequently.  However, the REG_NOTES aren't
1272              propagated correctly by the reload phase, and it can cause bad
1273              code to be generated.  We could still try:
1274
1275                 ld r1,r3+; ld r2,r3; addi r3,-4
1276
1277              which saves 2 bytes and doesn't force longword alignment.  */
1278           emit_insn (gen_rtx_SET (VOIDmode,
1279                                   operand_subword (dest, reverse, TRUE, mode),
1280                                   adjust_address (src, SImode,
1281                                                   reverse * UNITS_PER_WORD)));
1282
1283           emit_insn (gen_rtx_SET (VOIDmode,
1284                                   operand_subword (dest, !reverse, TRUE, mode),
1285                                   adjust_address (src, SImode,
1286                                                   !reverse * UNITS_PER_WORD)));
1287         }
1288       else
1289         abort ();
1290     }
1291
1292   /* Mem = reg.  */
1293   /* We used to optimize loads from single registers as
1294
1295         st r1,r3; st r2,+r3
1296
1297      if r3 were not used subsequently.  However, the REG_NOTES aren't
1298      propagated correctly by the reload phase, and it can cause bad
1299      code to be generated.  We could still try:
1300
1301         st r1,r3; st r2,+r3; addi r3,-4
1302
1303      which saves 2 bytes and doesn't force longword alignment.  */
1304   else if (GET_CODE (dest) == MEM && GET_CODE (src) == REG)
1305     {
1306       emit_insn (gen_rtx_SET (VOIDmode,
1307                               adjust_address (dest, SImode, 0),
1308                               operand_subword (src, 0, TRUE, mode)));
1309
1310       emit_insn (gen_rtx_SET (VOIDmode,
1311                               adjust_address (dest, SImode, UNITS_PER_WORD),
1312                               operand_subword (src, 1, TRUE, mode)));
1313     }
1314
1315   else
1316     abort ();
1317
1318   val = get_insns ();
1319   end_sequence ();
1320   return val;
1321 }
1322
1323 \f
1324 /* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.  */
1325
1326 int
1327 function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1328                             tree type, int named ATTRIBUTE_UNUSED)
1329 {
1330   int ret;
1331   unsigned int size =
1332     (((mode == BLKmode && type)
1333       ? (unsigned int) int_size_in_bytes (type)
1334       : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1)
1335     / UNITS_PER_WORD;
1336
1337   if (*cum >= M32R_MAX_PARM_REGS)
1338     ret = 0;
1339   else if (*cum + size > M32R_MAX_PARM_REGS)
1340     ret = (*cum + size) - M32R_MAX_PARM_REGS;
1341   else
1342     ret = 0;
1343
1344   return ret;
1345 }
1346
1347 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
1348
1349 static bool
1350 m32r_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
1351 {
1352   return m32r_pass_by_reference (NULL, TYPE_MODE (type), type, false);
1353 }
1354
1355 /* Do any needed setup for a variadic function.  For the M32R, we must
1356    create a register parameter block, and then copy any anonymous arguments
1357    in registers to memory.
1358
1359    CUM has not been updated for the last named argument which has type TYPE
1360    and mode MODE, and we rely on this fact.  */
1361
1362 static void
1363 m32r_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1364                              tree type, int *pretend_size, int no_rtl)
1365 {
1366   int first_anon_arg;
1367
1368   if (no_rtl)
1369     return;
1370
1371   /* All BLKmode values are passed by reference.  */
1372   if (mode == BLKmode)
1373     abort ();
1374
1375   first_anon_arg = (ROUND_ADVANCE_CUM (*cum, mode, type)
1376                     + ROUND_ADVANCE_ARG (mode, type));
1377
1378   if (first_anon_arg < M32R_MAX_PARM_REGS)
1379     {
1380       /* Note that first_reg_offset < M32R_MAX_PARM_REGS.  */
1381       int first_reg_offset = first_anon_arg;
1382       /* Size in words to "pretend" allocate.  */
1383       int size = M32R_MAX_PARM_REGS - first_reg_offset;
1384       rtx regblock;
1385
1386       regblock = gen_rtx_MEM (BLKmode,
1387                               plus_constant (arg_pointer_rtx,
1388                                              FIRST_PARM_OFFSET (0)));
1389       set_mem_alias_set (regblock, get_varargs_alias_set ());
1390       move_block_from_reg (first_reg_offset, regblock, size);
1391
1392       *pretend_size = (size * UNITS_PER_WORD);
1393     }
1394 }
1395
1396 \f
1397 /* Return true if INSN is real instruction bearing insn.  */
1398
1399 static int
1400 m32r_is_insn (rtx insn)
1401 {
1402   return (INSN_P (insn)
1403           && GET_CODE (PATTERN (insn)) != USE
1404           && GET_CODE (PATTERN (insn)) != CLOBBER
1405           && GET_CODE (PATTERN (insn)) != ADDR_VEC);
1406 }
1407
1408 /* Increase the priority of long instructions so that the
1409    short instructions are scheduled ahead of the long ones.  */
1410
1411 static int
1412 m32r_adjust_priority (rtx insn, int priority)
1413 {
1414   if (m32r_is_insn (insn)
1415       && get_attr_insn_size (insn) != INSN_SIZE_SHORT)
1416     priority <<= 3;
1417
1418   return priority;
1419 }
1420
1421 \f
1422 /* Indicate how many instructions can be issued at the same time.
1423    This is sort of a lie.  The m32r can issue only 1 long insn at
1424    once, but it can issue 2 short insns.  The default therefore is
1425    set at 2, but this can be overridden by the command line option
1426    -missue-rate=1.  */
1427
1428 static int
1429 m32r_issue_rate (void)
1430 {
1431   return ((TARGET_LOW_ISSUE_RATE) ? 1 : 2);
1432 }
1433 \f
1434 /* Cost functions.  */
1435
1436 static bool
1437 m32r_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED, int *total)
1438 {
1439   switch (code)
1440     {
1441       /* Small integers are as cheap as registers.  4 byte values can be
1442          fetched as immediate constants - let's give that the cost of an
1443          extra insn.  */
1444     case CONST_INT:
1445       if (INT16_P (INTVAL (x)))
1446         {
1447           *total = 0;
1448           return true;
1449         }
1450       /* FALLTHRU */
1451
1452     case CONST:
1453     case LABEL_REF:
1454     case SYMBOL_REF:
1455       *total = COSTS_N_INSNS (1);
1456       return true;
1457
1458     case CONST_DOUBLE:
1459       {
1460         rtx high, low;
1461
1462         split_double (x, &high, &low);
1463         *total = COSTS_N_INSNS (!INT16_P (INTVAL (high))
1464                                 + !INT16_P (INTVAL (low)));
1465         return true;
1466       }
1467
1468     case MULT:
1469       *total = COSTS_N_INSNS (3);
1470       return true;
1471
1472     case DIV:
1473     case UDIV:
1474     case MOD:
1475     case UMOD:
1476       *total = COSTS_N_INSNS (10);
1477       return true;
1478
1479     default:
1480       return false;
1481     }
1482 }
1483 \f
1484 /* Type of function DECL.
1485
1486    The result is cached.  To reset the cache at the end of a function,
1487    call with DECL = NULL_TREE.  */
1488
1489 enum m32r_function_type
1490 m32r_compute_function_type (tree decl)
1491 {
1492   /* Cached value.  */
1493   static enum m32r_function_type fn_type = M32R_FUNCTION_UNKNOWN;
1494   /* Last function we were called for.  */
1495   static tree last_fn = NULL_TREE;
1496
1497   /* Resetting the cached value?  */
1498   if (decl == NULL_TREE)
1499     {
1500       fn_type = M32R_FUNCTION_UNKNOWN;
1501       last_fn = NULL_TREE;
1502       return fn_type;
1503     }
1504
1505   if (decl == last_fn && fn_type != M32R_FUNCTION_UNKNOWN)
1506     return fn_type;
1507
1508   /* Compute function type.  */
1509   fn_type = (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE
1510              ? M32R_FUNCTION_INTERRUPT
1511              : M32R_FUNCTION_NORMAL);
1512
1513   last_fn = decl;
1514   return fn_type;
1515 }
1516 \f/* Function prologue/epilogue handlers.  */
1517
1518 /* M32R stack frames look like:
1519
1520              Before call                       After call
1521         +-----------------------+       +-----------------------+
1522         |                       |       |                       |
1523    high |  local variables,     |       |  local variables,     |
1524    mem  |  reg save area, etc.  |       |  reg save area, etc.  |
1525         |                       |       |                       |
1526         +-----------------------+       +-----------------------+
1527         |                       |       |                       |
1528         |  arguments on stack.  |       |  arguments on stack.  |
1529         |                       |       |                       |
1530   SP+0->+-----------------------+       +-----------------------+
1531                                         |  reg parm save area,  |
1532                                         |  only created for     |    
1533                                         |  variable argument    |    
1534                                         |  functions            |    
1535                                         +-----------------------+
1536                                         |   previous frame ptr  |
1537                                         +-----------------------+    
1538                                         |                       |    
1539                                         |  register save area   |    
1540                                         |                       |    
1541                                         +-----------------------+
1542                                         |    return address     |    
1543                                         +-----------------------+    
1544                                         |                       |    
1545                                         |  local variables      |    
1546                                         |                       |    
1547                                         +-----------------------+    
1548                                         |                       |    
1549                                         |  alloca allocations   |    
1550                                         |                       |    
1551                                         +-----------------------+    
1552                                         |                       |    
1553    low                                  |  arguments on stack   |    
1554    memory                               |                       |    
1555                                   SP+0->+-----------------------+    
1556
1557 Notes:
1558 1) The "reg parm save area" does not exist for non variable argument fns.
1559 2) The "reg parm save area" can be eliminated completely if we saved regs
1560    containing anonymous args separately but that complicates things too
1561    much (so it's not done).
1562 3) The return address is saved after the register save area so as to have as
1563    many insns as possible between the restoration of `lr' and the `jmp lr'.  */
1564
1565 /* Structure to be filled in by m32r_compute_frame_size with register
1566    save masks, and offsets for the current function.  */
1567 struct m32r_frame_info
1568 {
1569   unsigned int total_size;      /* # bytes that the entire frame takes up.  */
1570   unsigned int extra_size;      /* # bytes of extra stuff.  */
1571   unsigned int pretend_size;    /* # bytes we push and pretend caller did.  */
1572   unsigned int args_size;       /* # bytes that outgoing arguments take up.  */
1573   unsigned int reg_size;        /* # bytes needed to store regs.  */
1574   unsigned int var_size;        /* # bytes that variables take up.  */
1575   unsigned int gmask;           /* Mask of saved gp registers.  */
1576   unsigned int save_fp;         /* Nonzero if fp must be saved.  */
1577   unsigned int save_lr;         /* Nonzero if lr (return addr) must be saved.  */
1578   int          initialized;     /* Nonzero if frame size already calculated.  */
1579 };
1580
1581 /* Current frame information calculated by m32r_compute_frame_size.  */
1582 static struct m32r_frame_info current_frame_info;
1583
1584 /* Zero structure to initialize current_frame_info.  */
1585 static struct m32r_frame_info zero_frame_info;
1586
1587 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
1588 #define RETURN_ADDR_MASK   (1 << (RETURN_ADDR_REGNUM))
1589
1590 /* Tell prologue and epilogue if register REGNO should be saved / restored.
1591    The return address and frame pointer are treated separately.
1592    Don't consider them here.  */
1593 #define MUST_SAVE_REGISTER(regno, interrupt_p) \
1594   ((regno) != RETURN_ADDR_REGNUM && (regno) != FRAME_POINTER_REGNUM \
1595   && (regs_ever_live[regno] && (!call_really_used_regs[regno] || interrupt_p)))
1596
1597 #define MUST_SAVE_FRAME_POINTER (regs_ever_live[FRAME_POINTER_REGNUM])
1598 #define MUST_SAVE_RETURN_ADDR   (regs_ever_live[RETURN_ADDR_REGNUM] || current_function_profile)
1599
1600 #define SHORT_INSN_SIZE 2       /* Size of small instructions.  */
1601 #define LONG_INSN_SIZE 4        /* Size of long instructions.  */
1602
1603 /* Return the bytes needed to compute the frame pointer from the current
1604    stack pointer.
1605
1606    SIZE is the size needed for local variables.  */
1607
1608 unsigned int
1609 m32r_compute_frame_size (int size)      /* # of var. bytes allocated.  */
1610 {
1611   int regno;
1612   unsigned int total_size, var_size, args_size, pretend_size, extra_size;
1613   unsigned int reg_size, frame_size;
1614   unsigned int gmask;
1615   enum m32r_function_type fn_type;
1616   int interrupt_p;
1617   int pic_reg_used = flag_pic && (current_function_uses_pic_offset_table);
1618
1619   var_size      = M32R_STACK_ALIGN (size);
1620   args_size     = M32R_STACK_ALIGN (current_function_outgoing_args_size);
1621   pretend_size  = current_function_pretend_args_size;
1622   extra_size    = FIRST_PARM_OFFSET (0);
1623   total_size    = extra_size + pretend_size + args_size + var_size;
1624   reg_size      = 0;
1625   gmask         = 0;
1626
1627   /* See if this is an interrupt handler.  Call used registers must be saved
1628      for them too.  */
1629   fn_type = m32r_compute_function_type (current_function_decl);
1630   interrupt_p = M32R_INTERRUPT_P (fn_type);
1631
1632   /* Calculate space needed for registers.  */
1633   for (regno = 0; regno < M32R_MAX_INT_REGS; regno++)
1634     {
1635       if (MUST_SAVE_REGISTER (regno, interrupt_p)
1636           || (regno == PIC_OFFSET_TABLE_REGNUM && pic_reg_used))
1637         {
1638           reg_size += UNITS_PER_WORD;
1639           gmask |= 1 << regno;
1640         }
1641     }
1642
1643   current_frame_info.save_fp = MUST_SAVE_FRAME_POINTER;
1644   current_frame_info.save_lr = MUST_SAVE_RETURN_ADDR || pic_reg_used;
1645
1646   reg_size += ((current_frame_info.save_fp + current_frame_info.save_lr)
1647                * UNITS_PER_WORD);
1648   total_size += reg_size;
1649
1650   /* ??? Not sure this is necessary, and I don't think the epilogue
1651      handler will do the right thing if this changes total_size.  */
1652   total_size = M32R_STACK_ALIGN (total_size);
1653
1654   frame_size = total_size - (pretend_size + reg_size);
1655
1656   /* Save computed information.  */
1657   current_frame_info.total_size   = total_size;
1658   current_frame_info.extra_size   = extra_size;
1659   current_frame_info.pretend_size = pretend_size;
1660   current_frame_info.var_size     = var_size;
1661   current_frame_info.args_size    = args_size;
1662   current_frame_info.reg_size     = reg_size;
1663   current_frame_info.gmask        = gmask;
1664   current_frame_info.initialized  = reload_completed;
1665
1666   /* Ok, we're done.  */
1667   return total_size;
1668 }
1669 \f
1670 /* The table we use to reference PIC data.  */
1671 static rtx global_offset_table;
1672                                                                                 
1673 static void
1674 m32r_reload_lr (rtx sp, int size)
1675 {
1676   rtx lr = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
1677
1678   if (size == 0)
1679     emit_insn (gen_movsi (lr, gen_rtx_MEM (Pmode, sp)));
1680   else if (size <= 32768)
1681     emit_insn (gen_movsi (lr, gen_rtx_MEM (Pmode,
1682                                            gen_rtx_PLUS (Pmode, sp,
1683                                                          GEN_INT (size)))));
1684   else
1685     {   
1686       rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1687
1688       emit_insn (gen_movsi (tmp, GEN_INT (size)));
1689       emit_insn (gen_addsi3 (tmp, tmp, sp));
1690       emit_insn (gen_movsi (lr, gen_rtx_MEM (Pmode, tmp)));
1691     }
1692
1693   emit_insn (gen_rtx_USE (VOIDmode, lr));
1694 }
1695
1696 void
1697 m32r_load_pic_register (void)
1698 {
1699   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1700   emit_insn (gen_get_pc (pic_offset_table_rtx, global_offset_table,
1701                          GEN_INT (TARGET_MODEL_SMALL)));
1702                                                                                 
1703   /* Need to emit this whether or not we obey regdecls,
1704      since setjmp/longjmp can cause life info to screw up.  */
1705   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
1706 }
1707
1708 /* Expand the m32r prologue as a series of insns.  */
1709
1710 void
1711 m32r_expand_prologue (void)
1712 {
1713   int regno;
1714   int frame_size;
1715   unsigned int gmask;
1716   int pic_reg_used = flag_pic && (current_function_uses_pic_offset_table);
1717
1718   if (! current_frame_info.initialized)
1719     m32r_compute_frame_size (get_frame_size ());
1720
1721   gmask = current_frame_info.gmask;
1722
1723   /* These cases shouldn't happen.  Catch them now.  */
1724   if (current_frame_info.total_size == 0 && gmask)
1725     abort ();
1726
1727   /* Allocate space for register arguments if this is a variadic function.  */
1728   if (current_frame_info.pretend_size != 0)
1729     {
1730       /* Use a HOST_WIDE_INT temporary, since negating an unsigned int gives
1731          the wrong result on a 64-bit host.  */
1732       HOST_WIDE_INT pretend_size = current_frame_info.pretend_size;
1733       emit_insn (gen_addsi3 (stack_pointer_rtx,
1734                              stack_pointer_rtx,
1735                              GEN_INT (-pretend_size)));
1736     }
1737
1738   /* Save any registers we need to and set up fp.  */
1739   if (current_frame_info.save_fp)
1740     emit_insn (gen_movsi_push (stack_pointer_rtx, frame_pointer_rtx));
1741
1742   gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1743
1744   /* Save any needed call-saved regs (and call-used if this is an
1745      interrupt handler).  */
1746   for (regno = 0; regno <= M32R_MAX_INT_REGS; ++regno)
1747     {
1748       if ((gmask & (1 << regno)) != 0)
1749         emit_insn (gen_movsi_push (stack_pointer_rtx,
1750                                    gen_rtx_REG (Pmode, regno)));
1751     }
1752
1753   if (current_frame_info.save_lr)
1754     emit_insn (gen_movsi_push (stack_pointer_rtx,
1755                                gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
1756
1757   /* Allocate the stack frame.  */
1758   frame_size = (current_frame_info.total_size
1759                 - (current_frame_info.pretend_size
1760                    + current_frame_info.reg_size));
1761
1762   if (frame_size == 0)
1763     ; /* Nothing to do.  */
1764   else if (frame_size <= 32768)
1765     emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1766                            GEN_INT (-frame_size)));
1767   else
1768     {
1769       rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
1770
1771       emit_insn (gen_movsi (tmp, GEN_INT (frame_size)));
1772       emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
1773     }
1774
1775   if (frame_pointer_needed)
1776     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
1777
1778   if (current_function_profile)
1779     /* Push lr for mcount (form_pc, x).  */
1780     emit_insn (gen_movsi_push (stack_pointer_rtx,
1781                                gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
1782                                                                                 
1783   if (pic_reg_used)
1784     {
1785       m32r_load_pic_register ();
1786       m32r_reload_lr (stack_pointer_rtx,
1787                       (current_function_profile ? 0 : frame_size));
1788     }
1789
1790   if (current_function_profile && !pic_reg_used)
1791     emit_insn (gen_blockage ());
1792 }
1793
1794 \f
1795 /* Set up the stack and frame pointer (if desired) for the function.
1796    Note, if this is changed, you need to mirror the changes in
1797    m32r_compute_frame_size which calculates the prolog size.  */
1798
1799 static void
1800 m32r_output_function_prologue (FILE * file, HOST_WIDE_INT size)
1801 {
1802   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
1803
1804   /* If this is an interrupt handler, mark it as such.  */
1805   if (M32R_INTERRUPT_P (fn_type))
1806     fprintf (file, "\t%s interrupt handler\n", ASM_COMMENT_START);
1807
1808   if (! current_frame_info.initialized)
1809     m32r_compute_frame_size (size);
1810
1811   /* This is only for the human reader.  */
1812   fprintf (file,
1813            "\t%s PROLOGUE, vars= %d, regs= %d, args= %d, extra= %d\n",
1814            ASM_COMMENT_START,
1815            current_frame_info.var_size,
1816            current_frame_info.reg_size / 4,
1817            current_frame_info.args_size,
1818            current_frame_info.extra_size);
1819 }
1820 \f
1821 /* Do any necessary cleanup after a function to restore stack, frame,
1822    and regs.  */
1823
1824 static void
1825 m32r_output_function_epilogue (FILE * file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
1826 {
1827   int regno;
1828   int noepilogue = FALSE;
1829   int total_size;
1830   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
1831
1832   /* This is only for the human reader.  */
1833   fprintf (file, "\t%s EPILOGUE\n", ASM_COMMENT_START);
1834
1835   if (!current_frame_info.initialized)
1836     abort ();
1837   total_size = current_frame_info.total_size;
1838
1839   if (total_size == 0)
1840     {
1841       rtx insn = get_last_insn ();
1842
1843       /* If the last insn was a BARRIER, we don't have to write any code
1844          because a jump (aka return) was put there.  */
1845       if (GET_CODE (insn) == NOTE)
1846         insn = prev_nonnote_insn (insn);
1847       if (insn && GET_CODE (insn) == BARRIER)
1848         noepilogue = TRUE;
1849     }
1850
1851   if (!noepilogue)
1852     {
1853       unsigned int var_size = current_frame_info.var_size;
1854       unsigned int args_size = current_frame_info.args_size;
1855       unsigned int gmask = current_frame_info.gmask;
1856       int can_trust_sp_p = !current_function_calls_alloca;
1857       const char * sp_str = reg_names[STACK_POINTER_REGNUM];
1858       const char * fp_str = reg_names[FRAME_POINTER_REGNUM];
1859
1860       /* The first thing to do is point the sp at the bottom of the register
1861          save area.  */
1862       if (can_trust_sp_p)
1863         {
1864           unsigned int reg_offset = var_size + args_size;
1865           if (reg_offset == 0)
1866             ; /* Nothing to do.  */
1867           else if (reg_offset < 128)
1868             fprintf (file, "\taddi %s,%s%d\n",
1869                      sp_str, IMMEDIATE_PREFIX, reg_offset);
1870           else if (reg_offset < 32768)
1871             fprintf (file, "\tadd3 %s,%s,%s%d\n",
1872                      sp_str, sp_str, IMMEDIATE_PREFIX, reg_offset);
1873           else
1874             fprintf (file, "\tld24 %s,%s%d\n\tadd %s,%s\n",
1875                      reg_names[PROLOGUE_TMP_REGNUM],
1876                      IMMEDIATE_PREFIX, reg_offset,
1877                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
1878         }
1879       else if (frame_pointer_needed)
1880         {
1881           unsigned int reg_offset = var_size + args_size;
1882
1883           if (reg_offset == 0)
1884             fprintf (file, "\tmv %s,%s\n", sp_str, fp_str);
1885           else if (reg_offset < 32768)
1886             fprintf (file, "\tadd3 %s,%s,%s%d\n",
1887                      sp_str, fp_str, IMMEDIATE_PREFIX, reg_offset);
1888           else
1889             fprintf (file, "\tld24 %s,%s%d\n\tadd %s,%s\n",
1890                      reg_names[PROLOGUE_TMP_REGNUM],
1891                      IMMEDIATE_PREFIX, reg_offset,
1892                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
1893         }
1894       else
1895         abort ();
1896
1897       if (current_frame_info.save_lr)
1898         fprintf (file, "\tpop %s\n", reg_names[RETURN_ADDR_REGNUM]);
1899
1900       /* Restore any saved registers, in reverse order of course.  */
1901       gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1902       for (regno = M32R_MAX_INT_REGS - 1; regno >= 0; --regno)
1903         {
1904           if ((gmask & (1L << regno)) != 0)
1905             fprintf (file, "\tpop %s\n", reg_names[regno]);
1906         }
1907
1908       if (current_frame_info.save_fp)
1909         fprintf (file, "\tpop %s\n", fp_str);
1910
1911       /* Remove varargs area if present.  */
1912       if (current_frame_info.pretend_size != 0)
1913         fprintf (file, "\taddi %s,%s%d\n",
1914                  sp_str, IMMEDIATE_PREFIX, current_frame_info.pretend_size);
1915         
1916       /* Emit the return instruction.  */
1917       if (M32R_INTERRUPT_P (fn_type))
1918         fprintf (file, "\trte\n");
1919       else
1920         fprintf (file, "\tjmp %s\n", reg_names[RETURN_ADDR_REGNUM]);
1921     }
1922
1923   /* Reset state info for each function.  */
1924   current_frame_info = zero_frame_info;
1925   m32r_compute_function_type (NULL_TREE);
1926 }
1927 \f
1928 /* Return nonzero if this function is known to have a null or 1 instruction
1929    epilogue.  */
1930
1931 int
1932 direct_return (void)
1933 {
1934   if (!reload_completed)
1935     return FALSE;
1936
1937   if (! current_frame_info.initialized)
1938     m32r_compute_frame_size (get_frame_size ());
1939
1940    return current_frame_info.total_size == 0;
1941 }
1942
1943 \f
1944 /* PIC.  */
1945
1946 int
1947 m32r_legitimate_pic_operand_p (rtx x)
1948 {
1949   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1950     return 0;
1951                                                                                 
1952   if (GET_CODE (x) == CONST
1953       && GET_CODE (XEXP (x, 0)) == PLUS
1954       && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1955           || GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)
1956       && (GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
1957     return 0;
1958                                                                                 
1959   return 1;
1960 }
1961
1962 rtx
1963 m32r_legitimize_pic_address (rtx orig, rtx reg)
1964 {
1965 #ifdef DEBUG_PIC
1966   printf("m32r_legitimize_pic_address()\n");
1967 #endif
1968
1969   if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF)
1970     {
1971       rtx pic_ref, address;
1972       rtx insn;
1973       int subregs = 0;
1974
1975       if (reg == 0)
1976         {
1977           if (reload_in_progress || reload_completed)
1978             abort ();
1979           else
1980             reg = gen_reg_rtx (Pmode);
1981
1982           subregs = 1;
1983         }
1984
1985       if (subregs)
1986         address = gen_reg_rtx (Pmode);
1987       else
1988         address = reg;
1989
1990       current_function_uses_pic_offset_table = 1;
1991
1992       if (GET_CODE (orig) == LABEL_REF
1993           || (GET_CODE (orig) == SYMBOL_REF && SYMBOL_REF_LOCAL_P (orig)))
1994         {
1995           emit_insn (gen_gotoff_load_addr (reg, orig));
1996           emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx));
1997           return reg;
1998         }
1999
2000       emit_insn (gen_pic_load_addr (address, orig));
2001
2002       emit_insn (gen_addsi3 (address, address, pic_offset_table_rtx));
2003       pic_ref = gen_const_mem (Pmode, address);
2004       insn = emit_move_insn (reg, pic_ref);
2005 #if 0
2006       /* Put a REG_EQUAL note on this insn, so that it can be optimized
2007          by loop.  */
2008       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
2009                   REG_NOTES (insn));
2010 #endif
2011       return reg;
2012     }
2013   else if (GET_CODE (orig) == CONST)
2014     {
2015       rtx base, offset;
2016
2017       if (GET_CODE (XEXP (orig, 0)) == PLUS
2018           && XEXP (XEXP (orig, 0), 1) == pic_offset_table_rtx)
2019         return orig;
2020
2021       if (reg == 0)
2022         {
2023           if (reload_in_progress || reload_completed)
2024             abort ();
2025           else
2026             reg = gen_reg_rtx (Pmode);
2027         }
2028
2029       if (GET_CODE (XEXP (orig, 0)) == PLUS)
2030         {
2031           base = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 0), reg);
2032           if (base == reg)
2033             offset = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 1), NULL_RTX);
2034           else
2035             offset = m32r_legitimize_pic_address (XEXP (XEXP (orig, 0), 1), reg);
2036         }
2037       else
2038         return orig;
2039
2040       if (GET_CODE (offset) == CONST_INT)
2041         {
2042           if (INT16_P (INTVAL (offset)))
2043             return plus_constant (base, INTVAL (offset));
2044           else if (! reload_in_progress && ! reload_completed)
2045             offset = force_reg (Pmode, offset);
2046           else
2047             /* If we reach here, then something is seriously wrong.  */
2048             abort ();
2049         }
2050
2051       return gen_rtx_PLUS (Pmode, base, offset);
2052     }
2053
2054   return orig;
2055 }
2056
2057 /* Emit special PIC prologues and epilogues.  */
2058
2059 void
2060 m32r_finalize_pic (void)
2061 {
2062   current_function_uses_pic_offset_table |= current_function_profile;
2063 }
2064 \f
2065 /* Nested function support.  */
2066
2067 /* Emit RTL insns to initialize the variable parts of a trampoline.
2068    FNADDR is an RTX for the address of the function's pure code.
2069    CXT is an RTX for the static chain value for the function.  */
2070
2071 void
2072 m32r_initialize_trampoline (rtx tramp ATTRIBUTE_UNUSED,
2073                             rtx fnaddr ATTRIBUTE_UNUSED,
2074                             rtx cxt ATTRIBUTE_UNUSED)
2075 {
2076 }
2077 \f
2078 static void
2079 m32r_file_start (void)
2080 {
2081   default_file_start ();
2082
2083   if (flag_verbose_asm)
2084     fprintf (asm_out_file,
2085              "%s M32R/D special options: -G " HOST_WIDE_INT_PRINT_UNSIGNED "\n",
2086              ASM_COMMENT_START, g_switch_value);
2087
2088   if (TARGET_LITTLE_ENDIAN)
2089     fprintf (asm_out_file, "\t.little\n");
2090 }
2091 \f
2092 /* Print operand X (an rtx) in assembler syntax to file FILE.
2093    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
2094    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
2095
2096 void
2097 m32r_print_operand (FILE * file, rtx x, int code)
2098 {
2099   rtx addr;
2100
2101   switch (code)
2102     {
2103       /* The 's' and 'p' codes are used by output_block_move() to
2104          indicate post-increment 's'tores and 'p're-increment loads.  */
2105     case 's':
2106       if (GET_CODE (x) == REG)
2107         fprintf (file, "@+%s", reg_names [REGNO (x)]);
2108       else
2109         output_operand_lossage ("invalid operand to %%s code");
2110       return;
2111       
2112     case 'p':
2113       if (GET_CODE (x) == REG)
2114         fprintf (file, "@%s+", reg_names [REGNO (x)]);
2115       else
2116         output_operand_lossage ("invalid operand to %%p code");
2117       return;
2118
2119     case 'R' :
2120       /* Write second word of DImode or DFmode reference,
2121          register or memory.  */
2122       if (GET_CODE (x) == REG)
2123         fputs (reg_names[REGNO (x)+1], file);
2124       else if (GET_CODE (x) == MEM)
2125         {
2126           fprintf (file, "@(");
2127           /* Handle possible auto-increment.  Since it is pre-increment and
2128              we have already done it, we can just use an offset of four.  */
2129           /* ??? This is taken from rs6000.c I think.  I don't think it is
2130              currently necessary, but keep it around.  */
2131           if (GET_CODE (XEXP (x, 0)) == PRE_INC
2132               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2133             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 4));
2134           else
2135             output_address (plus_constant (XEXP (x, 0), 4));
2136           fputc (')', file);
2137         }
2138       else
2139         output_operand_lossage ("invalid operand to %%R code");
2140       return;
2141
2142     case 'H' : /* High word.  */
2143     case 'L' : /* Low word.  */
2144       if (GET_CODE (x) == REG)
2145         {
2146           /* L = least significant word, H = most significant word.  */
2147           if ((WORDS_BIG_ENDIAN != 0) ^ (code == 'L'))
2148             fputs (reg_names[REGNO (x)], file);
2149           else
2150             fputs (reg_names[REGNO (x)+1], file);
2151         }
2152       else if (GET_CODE (x) == CONST_INT
2153                || GET_CODE (x) == CONST_DOUBLE)
2154         {
2155           rtx first, second;
2156
2157           split_double (x, &first, &second);
2158           fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2159                    code == 'L' ? INTVAL (first) : INTVAL (second));
2160         }
2161       else
2162         output_operand_lossage ("invalid operand to %%H/%%L code");
2163       return;
2164
2165     case 'A' :
2166       {
2167         char str[30];
2168
2169         if (GET_CODE (x) != CONST_DOUBLE
2170             || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
2171           fatal_insn ("bad insn for 'A'", x);
2172
2173         real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (x), sizeof (str), 0, 1);
2174         fprintf (file, "%s", str);
2175         return;
2176       }
2177
2178     case 'B' : /* Bottom half.  */
2179     case 'T' : /* Top half.  */
2180       /* Output the argument to a `seth' insn (sets the Top half-word).
2181          For constants output arguments to a seth/or3 pair to set Top and
2182          Bottom halves.  For symbols output arguments to a seth/add3 pair to
2183          set Top and Bottom halves.  The difference exists because for
2184          constants seth/or3 is more readable but for symbols we need to use
2185          the same scheme as `ld' and `st' insns (16 bit addend is signed).  */
2186       switch (GET_CODE (x))
2187         {
2188         case CONST_INT :
2189         case CONST_DOUBLE :
2190           {
2191             rtx first, second;
2192
2193             split_double (x, &first, &second);
2194             x = WORDS_BIG_ENDIAN ? second : first;
2195             fprintf (file, HOST_WIDE_INT_PRINT_HEX,
2196                      (code == 'B'
2197                       ? INTVAL (x) & 0xffff
2198                       : (INTVAL (x) >> 16) & 0xffff));
2199           }
2200           return;
2201         case CONST :
2202         case SYMBOL_REF :
2203           if (code == 'B'
2204               && small_data_operand (x, VOIDmode))
2205             {
2206               fputs ("sda(", file);
2207               output_addr_const (file, x);
2208               fputc (')', file);
2209               return;
2210             }
2211           /* fall through */
2212         case LABEL_REF :
2213           fputs (code == 'T' ? "shigh(" : "low(", file);
2214           output_addr_const (file, x);
2215           fputc (')', file);
2216           return;
2217         default :
2218           output_operand_lossage ("invalid operand to %%T/%%B code");
2219           return;
2220         }
2221       break;
2222
2223     case 'U' :
2224       /* ??? wip */
2225       /* Output a load/store with update indicator if appropriate.  */
2226       if (GET_CODE (x) == MEM)
2227         {
2228           if (GET_CODE (XEXP (x, 0)) == PRE_INC
2229               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
2230             fputs (".a", file);
2231         }
2232       else
2233         output_operand_lossage ("invalid operand to %%U code");
2234       return;
2235
2236     case 'N' :
2237       /* Print a constant value negated.  */
2238       if (GET_CODE (x) == CONST_INT)
2239         output_addr_const (file, GEN_INT (- INTVAL (x)));
2240       else
2241         output_operand_lossage ("invalid operand to %%N code");
2242       return;
2243
2244     case 'X' :
2245       /* Print a const_int in hex.  Used in comments.  */
2246       if (GET_CODE (x) == CONST_INT)
2247         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
2248       return;
2249
2250     case '#' :
2251       fputs (IMMEDIATE_PREFIX, file);
2252       return;
2253
2254     case 0 :
2255       /* Do nothing special.  */
2256       break;
2257
2258     default :
2259       /* Unknown flag.  */
2260       output_operand_lossage ("invalid operand output code");
2261     }
2262
2263   switch (GET_CODE (x))
2264     {
2265     case REG :
2266       fputs (reg_names[REGNO (x)], file);
2267       break;
2268
2269     case MEM :
2270       addr = XEXP (x, 0);
2271       if (GET_CODE (addr) == PRE_INC)
2272         {
2273           if (GET_CODE (XEXP (addr, 0)) != REG)
2274             fatal_insn ("pre-increment address is not a register", x);
2275
2276           fprintf (file, "@+%s", reg_names[REGNO (XEXP (addr, 0))]);
2277         }
2278       else if (GET_CODE (addr) == PRE_DEC)
2279         {
2280           if (GET_CODE (XEXP (addr, 0)) != REG)
2281             fatal_insn ("pre-decrement address is not a register", x);
2282
2283           fprintf (file, "@-%s", reg_names[REGNO (XEXP (addr, 0))]);
2284         }
2285       else if (GET_CODE (addr) == POST_INC)
2286         {
2287           if (GET_CODE (XEXP (addr, 0)) != REG)
2288             fatal_insn ("post-increment address is not a register", x);
2289
2290           fprintf (file, "@%s+", reg_names[REGNO (XEXP (addr, 0))]);
2291         }
2292       else
2293         {
2294           fputs ("@(", file);
2295           output_address (XEXP (x, 0));
2296           fputc (')', file);
2297         }
2298       break;
2299
2300     case CONST_DOUBLE :
2301       /* We handle SFmode constants here as output_addr_const doesn't.  */
2302       if (GET_MODE (x) == SFmode)
2303         {
2304           REAL_VALUE_TYPE d;
2305           long l;
2306
2307           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
2308           REAL_VALUE_TO_TARGET_SINGLE (d, l);
2309           fprintf (file, "0x%08lx", l);
2310           break;
2311         }
2312
2313       /* Fall through.  Let output_addr_const deal with it.  */
2314
2315     default :
2316       output_addr_const (file, x);
2317       break;
2318     }
2319 }
2320
2321 /* Print a memory address as an operand to reference that memory location.  */
2322
2323 void
2324 m32r_print_operand_address (FILE * file, rtx addr)
2325 {
2326   rtx base;
2327   rtx index = 0;
2328   int offset = 0;
2329
2330   switch (GET_CODE (addr))
2331     {
2332     case REG :
2333       fputs (reg_names[REGNO (addr)], file);
2334       break;
2335
2336     case PLUS :
2337       if (GET_CODE (XEXP (addr, 0)) == CONST_INT)
2338         offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
2339       else if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
2340         offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
2341       else
2342         base = XEXP (addr, 0), index = XEXP (addr, 1);
2343       if (GET_CODE (base) == REG)
2344         {
2345           /* Print the offset first (if present) to conform to the manual.  */
2346           if (index == 0)
2347             {
2348               if (offset != 0)
2349                 fprintf (file, "%d,", offset);
2350               fputs (reg_names[REGNO (base)], file);
2351             }
2352           /* The chip doesn't support this, but left in for generality.  */
2353           else if (GET_CODE (index) == REG)
2354             fprintf (file, "%s,%s",
2355                      reg_names[REGNO (base)], reg_names[REGNO (index)]);
2356           /* Not sure this can happen, but leave in for now.  */
2357           else if (GET_CODE (index) == SYMBOL_REF)
2358             {
2359               output_addr_const (file, index);
2360               fputc (',', file);
2361               fputs (reg_names[REGNO (base)], file);
2362             }
2363           else
2364             fatal_insn ("bad address", addr);
2365         }
2366       else if (GET_CODE (base) == LO_SUM)
2367         {
2368           if (index != 0
2369               || GET_CODE (XEXP (base, 0)) != REG)
2370             abort ();
2371           if (small_data_operand (XEXP (base, 1), VOIDmode))
2372             fputs ("sda(", file);
2373           else
2374             fputs ("low(", file);
2375           output_addr_const (file, plus_constant (XEXP (base, 1), offset));
2376           fputs ("),", file);
2377           fputs (reg_names[REGNO (XEXP (base, 0))], file);
2378         }
2379       else
2380         fatal_insn ("bad address", addr);
2381       break;
2382
2383     case LO_SUM :
2384       if (GET_CODE (XEXP (addr, 0)) != REG)
2385         fatal_insn ("lo_sum not of register", addr);
2386       if (small_data_operand (XEXP (addr, 1), VOIDmode))
2387         fputs ("sda(", file);
2388       else
2389         fputs ("low(", file);
2390       output_addr_const (file, XEXP (addr, 1));
2391       fputs ("),", file);
2392       fputs (reg_names[REGNO (XEXP (addr, 0))], file);
2393       break;
2394
2395     case PRE_INC :      /* Assume SImode.  */
2396       fprintf (file, "+%s", reg_names[REGNO (XEXP (addr, 0))]);
2397       break;
2398
2399     case PRE_DEC :      /* Assume SImode.  */
2400       fprintf (file, "-%s", reg_names[REGNO (XEXP (addr, 0))]);
2401       break;
2402
2403     case POST_INC :     /* Assume SImode.  */
2404       fprintf (file, "%s+", reg_names[REGNO (XEXP (addr, 0))]);
2405       break;
2406
2407     default :
2408       output_addr_const (file, addr);
2409       break;
2410     }
2411 }
2412
2413 /* Return true if the operands are the constants 0 and 1.  */
2414
2415 int
2416 zero_and_one (rtx operand1, rtx operand2)
2417 {
2418   return
2419        GET_CODE (operand1) == CONST_INT
2420     && GET_CODE (operand2) == CONST_INT
2421     && (  ((INTVAL (operand1) == 0) && (INTVAL (operand2) == 1))
2422         ||((INTVAL (operand1) == 1) && (INTVAL (operand2) == 0)));
2423 }
2424
2425 /* Return nonzero if the operand is suitable for use in a conditional move sequence.  */
2426
2427 int
2428 conditional_move_operand (rtx operand, enum machine_mode mode)
2429 {
2430   /* Only defined for simple integers so far...  */
2431   if (mode != SImode && mode != HImode && mode != QImode)
2432     return FALSE;
2433
2434   /* At the moment we can handle moving registers and loading constants.  */
2435   /* To be added: Addition/subtraction/bitops/multiplication of registers.  */
2436
2437   switch (GET_CODE (operand))
2438     {
2439     case REG:
2440       return 1;
2441
2442     case CONST_INT:
2443       return INT8_P (INTVAL (operand));
2444
2445     default:
2446 #if 0
2447       fprintf (stderr, "Test for cond move op of type: %s\n",
2448                GET_RTX_NAME (GET_CODE (operand)));
2449 #endif
2450       return 0;
2451     }
2452 }
2453
2454 /* Return true if the code is a test of the carry bit.  */
2455
2456 int
2457 carry_compare_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2458 {
2459   rtx x;
2460
2461   if (GET_MODE (op) != CCmode && GET_MODE (op) != VOIDmode)
2462     return FALSE;
2463
2464   if (GET_CODE (op) != NE && GET_CODE (op) != EQ)
2465     return FALSE;
2466
2467   x = XEXP (op, 0);
2468   if (GET_CODE (x) != REG || REGNO (x) != CARRY_REGNUM)
2469     return FALSE;
2470
2471   x = XEXP (op, 1);
2472   if (GET_CODE (x) != CONST_INT || INTVAL (x) != 0)
2473     return FALSE;
2474
2475   return TRUE;
2476 }
2477
2478 /* Generate the correct assembler code to handle the conditional loading of a
2479    value into a register.  It is known that the operands satisfy the
2480    conditional_move_operand() function above.  The destination is operand[0].
2481    The condition is operand [1].  The 'true' value is operand [2] and the
2482    'false' value is operand [3].  */
2483
2484 char *
2485 emit_cond_move (rtx * operands, rtx insn ATTRIBUTE_UNUSED)
2486 {
2487   static char buffer [100];
2488   const char * dest = reg_names [REGNO (operands [0])];
2489   
2490   buffer [0] = 0;
2491   
2492   /* Destination must be a register.  */
2493   if (GET_CODE (operands [0]) != REG)
2494     abort();
2495   if (! conditional_move_operand (operands [2], SImode))
2496     abort();
2497   if (! conditional_move_operand (operands [3], SImode))
2498     abort();
2499       
2500   /* Check to see if the test is reversed.  */
2501   if (GET_CODE (operands [1]) == NE)
2502     {
2503       rtx tmp = operands [2];
2504       operands [2] = operands [3];
2505       operands [3] = tmp;
2506     }
2507
2508   sprintf (buffer, "mvfc %s, cbr", dest);
2509
2510   /* If the true value was '0' then we need to invert the results of the move.  */
2511   if (INTVAL (operands [2]) == 0)
2512     sprintf (buffer + strlen (buffer), "\n\txor3 %s, %s, #1",
2513              dest, dest);
2514
2515   return buffer;
2516 }
2517
2518 /* Returns true if the registers contained in the two
2519    rtl expressions are different.  */
2520
2521 int
2522 m32r_not_same_reg (rtx a, rtx b)
2523 {
2524   int reg_a = -1;
2525   int reg_b = -2;
2526   
2527   while (GET_CODE (a) == SUBREG)
2528     a = SUBREG_REG (a);
2529   
2530   if (GET_CODE (a) == REG)
2531     reg_a = REGNO (a);
2532   
2533   while (GET_CODE (b) == SUBREG)
2534     b = SUBREG_REG (b);
2535   
2536   if (GET_CODE (b) == REG)
2537     reg_b = REGNO (b);
2538   
2539   return reg_a != reg_b;
2540 }
2541
2542 \f
2543 rtx
2544 m32r_function_symbol (const char *name)
2545 {
2546   int extra_flags = 0;
2547   enum m32r_model model;
2548   rtx sym = gen_rtx_SYMBOL_REF (Pmode, name);
2549
2550   if (TARGET_MODEL_SMALL)
2551     model = M32R_MODEL_SMALL;
2552   else if (TARGET_MODEL_MEDIUM)
2553     model = M32R_MODEL_MEDIUM;
2554   else if (TARGET_MODEL_LARGE)
2555     model = M32R_MODEL_LARGE;
2556   else
2557     abort (); /* Shouldn't happen.  */
2558   extra_flags |= model << SYMBOL_FLAG_MODEL_SHIFT;
2559                                                                                 
2560   if (extra_flags)
2561     SYMBOL_REF_FLAGS (sym) |= extra_flags;
2562
2563   return sym;
2564 }
2565
2566 /* Use a library function to move some bytes.  */
2567
2568 static void
2569 block_move_call (rtx dest_reg, rtx src_reg, rtx bytes_rtx)
2570 {
2571   /* We want to pass the size as Pmode, which will normally be SImode
2572      but will be DImode if we are using 64 bit longs and pointers.  */
2573   if (GET_MODE (bytes_rtx) != VOIDmode
2574       && GET_MODE (bytes_rtx) != Pmode)
2575     bytes_rtx = convert_to_mode (Pmode, bytes_rtx, 1);
2576
2577   emit_library_call (m32r_function_symbol ("memcpy"), 0,
2578                      VOIDmode, 3, dest_reg, Pmode, src_reg, Pmode,
2579                      convert_to_mode (TYPE_MODE (sizetype), bytes_rtx,
2580                                       TYPE_UNSIGNED (sizetype)),
2581                      TYPE_MODE (sizetype));
2582 }
2583
2584 /* The maximum number of bytes to copy using pairs of load/store instructions.
2585    If a block is larger than this then a loop will be generated to copy
2586    MAX_MOVE_BYTES chunks at a time.  The value of 32 is a semi-arbitrary choice.
2587    A customer uses Dhrystome as their benchmark, and Dhrystone has a 31 byte
2588    string copy in it.  */
2589 #define MAX_MOVE_BYTES 32
2590
2591 /* Expand string/block move operations.
2592
2593    operands[0] is the pointer to the destination.
2594    operands[1] is the pointer to the source.
2595    operands[2] is the number of bytes to move.
2596    operands[3] is the alignment.  */
2597
2598 void
2599 m32r_expand_block_move (rtx operands[])
2600 {
2601   rtx           orig_dst  = operands[0];
2602   rtx           orig_src  = operands[1];
2603   rtx           bytes_rtx = operands[2];
2604   rtx           align_rtx = operands[3];
2605   int           constp    = GET_CODE (bytes_rtx) == CONST_INT;
2606   HOST_WIDE_INT bytes     = constp ? INTVAL (bytes_rtx) : 0;
2607   int           align     = INTVAL (align_rtx);
2608   int           leftover;
2609   rtx           src_reg;
2610   rtx           dst_reg;
2611
2612   if (constp && bytes <= 0)
2613     return;
2614
2615   /* Move the address into scratch registers.  */
2616   dst_reg = copy_addr_to_reg (XEXP (orig_dst, 0));
2617   src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
2618
2619   if (align > UNITS_PER_WORD)
2620     align = UNITS_PER_WORD;
2621
2622   /* If we prefer size over speed, always use a function call.
2623      If we do not know the size, use a function call.
2624      If the blocks are not word aligned, use a function call.  */
2625   if (optimize_size || ! constp || align != UNITS_PER_WORD)
2626     {
2627       block_move_call (dst_reg, src_reg, bytes_rtx);
2628       return;
2629     }
2630
2631   leftover = bytes % MAX_MOVE_BYTES;
2632   bytes   -= leftover;
2633   
2634   /* If necessary, generate a loop to handle the bulk of the copy.  */
2635   if (bytes)
2636     {
2637       rtx label = NULL_RTX;
2638       rtx final_src = NULL_RTX;
2639       rtx at_a_time = GEN_INT (MAX_MOVE_BYTES);
2640       rtx rounded_total = GEN_INT (bytes);
2641       rtx new_dst_reg = gen_reg_rtx (SImode);
2642       rtx new_src_reg = gen_reg_rtx (SImode);
2643
2644       /* If we are going to have to perform this loop more than
2645          once, then generate a label and compute the address the
2646          source register will contain upon completion of the final
2647          iteration.  */
2648       if (bytes > MAX_MOVE_BYTES)
2649         {
2650           final_src = gen_reg_rtx (Pmode);
2651
2652           if (INT16_P(bytes))
2653             emit_insn (gen_addsi3 (final_src, src_reg, rounded_total));
2654           else
2655             {
2656               emit_insn (gen_movsi (final_src, rounded_total));
2657               emit_insn (gen_addsi3 (final_src, final_src, src_reg));
2658             }
2659
2660           label = gen_label_rtx ();
2661           emit_label (label);
2662         }
2663
2664       /* It is known that output_block_move() will update src_reg to point
2665          to the word after the end of the source block, and dst_reg to point
2666          to the last word of the destination block, provided that the block
2667          is MAX_MOVE_BYTES long.  */
2668       emit_insn (gen_movmemsi_internal (dst_reg, src_reg, at_a_time,
2669                                         new_dst_reg, new_src_reg));
2670       emit_move_insn (dst_reg, new_dst_reg);
2671       emit_move_insn (src_reg, new_src_reg);
2672       emit_insn (gen_addsi3 (dst_reg, dst_reg, GEN_INT (4)));
2673       
2674       if (bytes > MAX_MOVE_BYTES)
2675         {
2676           emit_insn (gen_cmpsi (src_reg, final_src));
2677           emit_jump_insn (gen_bne (label));
2678         }
2679     }
2680
2681   if (leftover)
2682     emit_insn (gen_movmemsi_internal (dst_reg, src_reg, GEN_INT (leftover),
2683                                       gen_reg_rtx (SImode),
2684                                       gen_reg_rtx (SImode)));
2685 }
2686
2687 \f
2688 /* Emit load/stores for a small constant word aligned block_move. 
2689
2690    operands[0] is the memory address of the destination.
2691    operands[1] is the memory address of the source.
2692    operands[2] is the number of bytes to move.
2693    operands[3] is a temp register.
2694    operands[4] is a temp register.  */
2695
2696 void
2697 m32r_output_block_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
2698 {
2699   HOST_WIDE_INT bytes = INTVAL (operands[2]);
2700   int           first_time;
2701   int           got_extra = 0;
2702   
2703   if (bytes < 1 || bytes > MAX_MOVE_BYTES)
2704     abort ();
2705   
2706   /* We do not have a post-increment store available, so the first set of
2707      stores are done without any increment, then the remaining ones can use
2708      the pre-increment addressing mode.
2709      
2710      Note: expand_block_move() also relies upon this behavior when building
2711      loops to copy large blocks.  */
2712   first_time = 1;
2713   
2714   while (bytes > 0)
2715     {
2716       if (bytes >= 8)
2717         {
2718           if (first_time)
2719             {
2720               output_asm_insn ("ld\t%5, %p1", operands);
2721               output_asm_insn ("ld\t%6, %p1", operands);
2722               output_asm_insn ("st\t%5, @%0", operands);
2723               output_asm_insn ("st\t%6, %s0", operands);
2724             }
2725           else
2726             {
2727               output_asm_insn ("ld\t%5, %p1", operands);
2728               output_asm_insn ("ld\t%6, %p1", operands);
2729               output_asm_insn ("st\t%5, %s0", operands);
2730               output_asm_insn ("st\t%6, %s0", operands);
2731             }
2732
2733           bytes -= 8;
2734         }
2735       else if (bytes >= 4)
2736         {
2737           if (bytes > 4)
2738             got_extra = 1;
2739           
2740           output_asm_insn ("ld\t%5, %p1", operands);
2741           
2742           if (got_extra)
2743             output_asm_insn ("ld\t%6, %p1", operands);
2744                 
2745           if (first_time)
2746             output_asm_insn ("st\t%5, @%0", operands);
2747           else
2748             output_asm_insn ("st\t%5, %s0", operands);
2749
2750           bytes -= 4;
2751         }
2752       else 
2753         {
2754           /* Get the entire next word, even though we do not want all of it.
2755              The saves us from doing several smaller loads, and we assume that
2756              we cannot cause a page fault when at least part of the word is in
2757              valid memory [since we don't get called if things aren't properly
2758              aligned].  */
2759           int dst_offset = first_time ? 0 : 4;
2760           /* The amount of increment we have to make to the
2761              destination pointer.  */
2762           int dst_inc_amount = dst_offset + bytes - 4;
2763           /* The same for the source pointer.  */
2764           int src_inc_amount = bytes;
2765           int last_shift;
2766           rtx my_operands[3];
2767
2768           /* If got_extra is true then we have already loaded
2769              the next word as part of loading and storing the previous word.  */
2770           if (! got_extra)
2771             output_asm_insn ("ld\t%6, @%1", operands);
2772
2773           if (bytes >= 2)
2774             {
2775               bytes -= 2;
2776
2777               output_asm_insn ("sra3\t%5, %6, #16", operands);
2778               my_operands[0] = operands[5];
2779               my_operands[1] = GEN_INT (dst_offset);
2780               my_operands[2] = operands[0];
2781               output_asm_insn ("sth\t%0, @(%1,%2)", my_operands);
2782               
2783               /* If there is a byte left to store then increment the
2784                  destination address and shift the contents of the source
2785                  register down by 8 bits.  We could not do the address
2786                  increment in the store half word instruction, because it does
2787                  not have an auto increment mode.  */
2788               if (bytes > 0)  /* assert (bytes == 1) */
2789                 {
2790                   dst_offset += 2;
2791                   last_shift = 8;
2792                 }
2793             }
2794           else
2795             last_shift = 24;
2796
2797           if (bytes > 0)
2798             {
2799               my_operands[0] = operands[6];
2800               my_operands[1] = GEN_INT (last_shift);
2801               output_asm_insn ("srai\t%0, #%1", my_operands);
2802               my_operands[0] = operands[6];
2803               my_operands[1] = GEN_INT (dst_offset);
2804               my_operands[2] = operands[0];
2805               output_asm_insn ("stb\t%0, @(%1,%2)", my_operands);
2806             }
2807
2808           /* Update the destination pointer if needed.  We have to do
2809              this so that the patterns matches what we output in this
2810              function.  */
2811           if (dst_inc_amount
2812               && !find_reg_note (insn, REG_UNUSED, operands[0]))
2813             {
2814               my_operands[0] = operands[0];
2815               my_operands[1] = GEN_INT (dst_inc_amount);
2816               output_asm_insn ("addi\t%0, #%1", my_operands);
2817             }
2818           
2819           /* Update the source pointer if needed.  We have to do this
2820              so that the patterns matches what we output in this
2821              function.  */
2822           if (src_inc_amount
2823               && !find_reg_note (insn, REG_UNUSED, operands[1]))
2824             {
2825               my_operands[0] = operands[1];
2826               my_operands[1] = GEN_INT (src_inc_amount);
2827               output_asm_insn ("addi\t%0, #%1", my_operands);
2828             }
2829           
2830           bytes = 0;
2831         }
2832
2833       first_time = 0;
2834     }
2835 }
2836
2837 /* Return true if op is an integer constant, less than or equal to
2838    MAX_MOVE_BYTES.  */
2839
2840 int
2841 m32r_block_immediate_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2842 {
2843   if (GET_CODE (op) != CONST_INT
2844       || INTVAL (op) > MAX_MOVE_BYTES
2845       || INTVAL (op) <= 0)
2846     return 0;
2847
2848   return 1;
2849 }
2850
2851 /* Return true if using NEW_REG in place of OLD_REG is ok.  */
2852
2853 int
2854 m32r_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
2855                            unsigned int new_reg)
2856 {
2857   /* Interrupt routines can't clobber any register that isn't already used.  */
2858   if (lookup_attribute ("interrupt", DECL_ATTRIBUTES (current_function_decl))
2859       && !regs_ever_live[new_reg])
2860     return 0;
2861
2862   /* We currently emit epilogues as text, not rtl, so the liveness
2863      of the return address register isn't visible.  */
2864   if (current_function_is_leaf && new_reg == RETURN_ADDR_REGNUM)
2865     return 0;
2866
2867   return 1;
2868 }
2869
2870 rtx
2871 m32r_return_addr (int count)
2872 {
2873   if (count != 0)
2874     return const0_rtx;
2875   
2876   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
2877 }