OSDN Git Service

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