OSDN Git Service

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