OSDN Git Service

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