OSDN Git Service

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