OSDN Git Service

06e9fc9ae24dbb6c3fa80594c68e81177d4b82e3
[pf3gnuchains/gcc-fork.git] / gcc / config / m32r / m32r.c
1 /* Subroutines used for code generation on the M32R/D cpu.
2    Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include "config.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "hard-reg-set.h"
27 #include "real.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "expr.h"
35 #include "recog.h"
36
37 /* Save the operands last given to a compare for use when we
38    generate a scc or bcc insn.  */
39 rtx m32r_compare_op0, m32r_compare_op1;
40
41 /* Array of valid operand punctuation characters.  */
42 char m32r_punct_chars[256];
43
44 static void init_reg_tables ();
45
46 /* Selected code model.  */
47 char *m32r_model_string = M32R_MODEL_DEFAULT;
48 enum m32r_model m32r_model;
49
50 /* Selected SDA support.  */
51 char *m32r_sdata_string = M32R_SDATA_DEFAULT;
52 enum m32r_sdata m32r_sdata;
53
54 /* Called by OVERRIDE_OPTIONS to initialize various things.  */
55
56 void
57 m32r_init ()
58 {
59   init_reg_tables ();
60
61   /* Initialize array for PRINT_OPERAND_PUNCT_VALID_P.  */
62   memset (m32r_punct_chars, 0, sizeof (m32r_punct_chars));
63   m32r_punct_chars['#'] = 1;
64   m32r_punct_chars['@'] = 1; /* ??? no longer used */
65
66   /* Provide default value if not specified.  */
67   if (!g_switch_set)
68     g_switch_value = SDATA_DEFAULT_SIZE;
69
70   if (strcmp (m32r_model_string, "small") == 0)
71     m32r_model = M32R_MODEL_SMALL;
72   else if (strcmp (m32r_model_string, "medium") == 0)
73     m32r_model = M32R_MODEL_MEDIUM;
74   else if (strcmp (m32r_model_string, "large") == 0)
75     m32r_model = M32R_MODEL_LARGE;
76   else
77     error ("bad value (%s) for -mmodel switch", m32r_model_string);
78
79   if (strcmp (m32r_sdata_string, "none") == 0)
80     m32r_sdata = M32R_SDATA_NONE;
81   else if (strcmp (m32r_sdata_string, "sdata") == 0)
82     m32r_sdata = M32R_SDATA_SDATA;
83   else if (strcmp (m32r_sdata_string, "use") == 0)
84     m32r_sdata = M32R_SDATA_USE;
85   else
86     error ("bad value (%s) for -msdata switch", m32r_sdata_string);
87 }
88
89 /* Vectors to keep interesting information about registers where it can easily
90    be got.  We use to use the actual mode value as the bit number, but there
91    is (or may be) more than 32 modes now.  Instead we use two tables: one
92    indexed by hard register number, and one indexed by mode.  */
93
94 /* The purpose of m32r_mode_class is to shrink the range of modes so that
95    they all fit (as bit numbers) in a 32 bit word (again).  Each real mode is
96    mapped into one m32r_mode_class mode.  */
97
98 enum m32r_mode_class {
99   C_MODE,
100   S_MODE, D_MODE, T_MODE, O_MODE,
101   SF_MODE, DF_MODE, TF_MODE, OF_MODE
102 };
103
104 /* Modes for condition codes.  */
105 #define C_MODES (1 << (int) C_MODE)
106
107 /* Modes for single-word and smaller quantities.  */
108 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
109
110 /* Modes for double-word and smaller quantities.  */
111 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
112
113 /* Modes for quad-word and smaller quantities.  */
114 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
115
116 /* Value is 1 if register/mode pair is acceptable on arc.  */
117
118 unsigned int m32r_hard_regno_mode_ok[FIRST_PSEUDO_REGISTER] = {
119   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
120   T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, S_MODES, S_MODES, S_MODES,
121   S_MODES, C_MODES
122 };
123
124 unsigned int m32r_mode_class [NUM_MACHINE_MODES];
125
126 enum reg_class m32r_regno_reg_class[FIRST_PSEUDO_REGISTER];
127
128 static void
129 init_reg_tables ()
130 {
131   int i;
132
133   for (i = 0; i < NUM_MACHINE_MODES; i++)
134     {
135       switch (GET_MODE_CLASS (i))
136         {
137         case MODE_INT:
138         case MODE_PARTIAL_INT:
139         case MODE_COMPLEX_INT:
140           if (GET_MODE_SIZE (i) <= 4)
141             m32r_mode_class[i] = 1 << (int) S_MODE;
142           else if (GET_MODE_SIZE (i) == 8)
143             m32r_mode_class[i] = 1 << (int) D_MODE;
144           else if (GET_MODE_SIZE (i) == 16)
145             m32r_mode_class[i] = 1 << (int) T_MODE;
146           else if (GET_MODE_SIZE (i) == 32)
147             m32r_mode_class[i] = 1 << (int) O_MODE;
148           else 
149             m32r_mode_class[i] = 0;
150           break;
151         case MODE_FLOAT:
152         case MODE_COMPLEX_FLOAT:
153           if (GET_MODE_SIZE (i) <= 4)
154             m32r_mode_class[i] = 1 << (int) SF_MODE;
155           else if (GET_MODE_SIZE (i) == 8)
156             m32r_mode_class[i] = 1 << (int) DF_MODE;
157           else if (GET_MODE_SIZE (i) == 16)
158             m32r_mode_class[i] = 1 << (int) TF_MODE;
159           else if (GET_MODE_SIZE (i) == 32)
160             m32r_mode_class[i] = 1 << (int) OF_MODE;
161           else 
162             m32r_mode_class[i] = 0;
163           break;
164         case MODE_CC:
165         default:
166           /* mode_class hasn't been initialized yet for EXTRA_CC_MODES, so
167              we must explicitly check for them here.  */
168           if (i == (int) CCmode)
169             m32r_mode_class[i] = 1 << (int) C_MODE;
170           else
171             m32r_mode_class[i] = 0;
172           break;
173         }
174     }
175
176   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
177     {
178       if (GPR_P (i))
179         m32r_regno_reg_class[i] = GENERAL_REGS;
180       else if (i == ARG_POINTER_REGNUM)
181         m32r_regno_reg_class[i] = GENERAL_REGS;
182       else
183         m32r_regno_reg_class[i] = NO_REGS;
184     }
185 }
186 \f
187 /* M32R specific attribute support.
188
189    interrupt - for interrupt functions
190
191    model - select code model used to access object
192
193         small: addresses use 24 bits, use bl to make calls
194         medium: addresses use 32 bits, use bl to make calls
195         large: addresses use 32 bits, use seth/add3/jl to make calls
196
197         Grep for MODEL in m32r.h for more info.
198 */
199
200 /* Return nonzero if IDENTIFIER is a valid decl attribute.  */
201
202 int
203 m32r_valid_machine_decl_attribute (type, attributes, identifier, args)
204      tree type;
205      tree attributes;
206      tree identifier;
207      tree args;
208 {
209   static tree interrupt_ident, model_ident;
210   static tree small_ident, medium_ident, large_ident;
211
212   if (interrupt_ident == 0)
213     {
214       interrupt_ident = get_identifier ("__interrupt__");
215       model_ident = get_identifier ("__model__");
216       small_ident = get_identifier ("__small__");
217       medium_ident = get_identifier ("__medium__");
218       large_ident = get_identifier ("__large__");
219     }
220
221   if (identifier == interrupt_ident
222       && list_length (args) == 0)
223     return 1;
224
225   if (identifier == model_ident
226       && list_length (args) == 1
227       && (TREE_VALUE (args) == small_ident
228           || TREE_VALUE (args) == medium_ident
229           || TREE_VALUE (args) == large_ident))
230     return 1;
231
232   return 0;
233 }
234
235 /* Return zero if TYPE1 and TYPE are incompatible, one if they are compatible,
236    and two if they are nearly compatible (which causes a warning to be
237    generated).  */
238
239 int
240 m32r_comp_type_attributes (type1, type2)
241      tree type1, type2;
242 {
243   return 1;
244 }
245
246 /* Set the default attributes for TYPE.  */
247
248 void
249 m32r_set_default_type_attributes (type)
250      tree type;
251 {
252 }
253 \f
254 /* A C statement or statements to switch to the appropriate
255    section for output of DECL.  DECL is either a `VAR_DECL' node
256    or a constant of some sort.  RELOC indicates whether forming
257    the initial value of DECL requires link-time relocations.  */
258
259 void
260 m32r_select_section (decl, reloc)
261      tree decl;
262      int reloc;
263 {
264   if (TREE_CODE (decl) == STRING_CST)
265     {
266       if (! flag_writable_strings)
267         const_section ();
268       else
269         data_section ();
270     }
271   else if (TREE_CODE (decl) == VAR_DECL)
272     {
273       if (SDATA_NAME_P (XSTR (XEXP (DECL_RTL (decl), 0), 0)))
274         sdata_section ();
275       else if ((flag_pic && reloc)
276                || !TREE_READONLY (decl)
277                || TREE_SIDE_EFFECTS (decl)
278                || !DECL_INITIAL (decl)
279                || (DECL_INITIAL (decl) != error_mark_node
280                    && !TREE_CONSTANT (DECL_INITIAL (decl))))
281         data_section ();
282       else
283         const_section ();
284     }
285   else
286     const_section ();
287 }
288
289 /* Encode section information of DECL, which is either a VAR_DECL,
290    FUNCTION_DECL, STRING_CST, CONSTRUCTOR, or ???.
291
292    For the M32R we want to record:
293
294    - whether the object lives in .sdata/.sbss.
295      objects living in .sdata/.sbss are prefixed with SDATA_FLAG_CHAR
296
297    - what code model should be used to access the object
298      small: recorded with no flag - for space efficiency since they'll
299             be the most common
300      medium: prefixed with MEDIUM_FLAG_CHAR
301      large: prefixed with LARGE_FLAG_CHAR
302 */
303
304 void
305 m32r_encode_section_info (decl)
306      tree decl;
307 {
308   char prefix = 0;
309   tree model = 0;
310
311   switch (TREE_CODE (decl))
312     {
313     case VAR_DECL :
314     case FUNCTION_DECL :
315       model = lookup_attribute ("model", DECL_MACHINE_ATTRIBUTES (decl));
316       break;
317     case STRING_CST :
318     case CONSTRUCTOR :
319       /* ??? document all others that can appear here */
320     default :
321       return;
322     }
323
324   /* Only mark the object as being small data area addressable if
325      it hasn't been explicitly marked with a code model.
326
327      The user can explicitly put an object in the small data area with the
328      section attribute.  If the object is in sdata/sbss and marked with a
329      code model do both [put the object in .sdata and mark it as being
330      addressed with a specific code model - don't mark it as being addressed
331      with an SDA reloc though].  This is ok and might be useful at times.  If
332      the object doesn't fit the linker will give an error.  */
333
334   if (! model)
335     {
336       if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd'
337           && DECL_SECTION_NAME (decl) != NULL_TREE)
338         {
339           char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
340           if (! strcmp (name, ".sdata") || ! strcmp (name, ".sbss"))
341             {
342 #if 0 /* ??? There's no reason to disallow this, is there?  */
343               if (TREE_READONLY (decl))
344                 error_with_decl (decl, "const objects cannot go in .sdata/.sbss");
345 #endif
346               prefix = SDATA_FLAG_CHAR;
347             }
348         }
349       else
350         {
351           if (TREE_CODE (decl) == VAR_DECL
352               && ! TREE_READONLY (decl)
353               && ! TARGET_SDATA_NONE)
354             {
355               int size = int_size_in_bytes (TREE_TYPE (decl));
356
357               if (size > 0 && size <= g_switch_value)
358                 prefix = SDATA_FLAG_CHAR;
359             }
360         }
361     }
362
363   /* If data area not decided yet, check for a code model.  */
364   if (prefix == 0)
365     {
366       if (model)
367         {
368           if (TREE_VALUE (TREE_VALUE (model)) == get_identifier ("__small__"))
369             ; /* don't mark the symbol specially */
370           else if (TREE_VALUE (TREE_VALUE (model)) == get_identifier ("__medium__"))
371             prefix = MEDIUM_FLAG_CHAR;
372           else if (TREE_VALUE (TREE_VALUE (model)) == get_identifier ("__large__"))
373             prefix = LARGE_FLAG_CHAR;
374           else
375             abort (); /* shouldn't happen */
376         }
377       else
378         {
379           if (TARGET_MODEL_SMALL)
380             ; /* don't mark the symbol specially */
381           else if (TARGET_MODEL_MEDIUM)
382             prefix = MEDIUM_FLAG_CHAR;
383           else if (TARGET_MODEL_LARGE)
384             prefix = LARGE_FLAG_CHAR;
385           else
386             abort (); /* shouldn't happen */
387         }
388     }
389
390   if (prefix != 0)
391     {
392       rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
393                  ? TREE_CST_RTL (decl) : DECL_RTL (decl));
394       char *str = XSTR (XEXP (rtl, 0), 0);
395       int len = strlen (str);
396       char *newstr = savealloc (len + 2);
397       strcpy (newstr + 1, str);
398       *newstr = prefix;
399       XSTR (XEXP (rtl, 0), 0) = newstr;
400     }
401 }
402
403 /* Do anything needed before RTL is emitted for each function.  */
404
405 void
406 m32r_init_expanders ()
407 {
408   /* ??? At one point there was code here.  The function is left in
409      to make it easy to experiment.  */
410 }
411 \f
412 /* Acceptable arguments to the call insn.  */
413
414 int
415 call_address_operand (op, mode)
416      rtx op;
417      enum machine_mode mode;
418 {
419   return (symbolic_operand (op, mode)
420           || (GET_CODE (op) == CONST_INT && LEGITIMATE_CONSTANT_P (op))
421           || (GET_CODE (op) == REG));
422 }
423
424 int
425 call_operand (op, mode)
426      rtx op;
427      enum machine_mode mode;
428 {
429   if (GET_CODE (op) != MEM)
430     return 0;
431   op = XEXP (op, 0);
432   return call_address_operand (op, mode);
433 }
434
435 /* Returns 1 if OP is a symbol reference.  */
436
437 int
438 symbolic_operand (op, mode)
439      rtx op;
440      enum machine_mode mode;
441 {
442   switch (GET_CODE (op))
443     {
444     case SYMBOL_REF:
445     case LABEL_REF:
446     case CONST :
447       return 1;
448     default:
449       return 0;
450     }
451 }
452
453 /* Return truth value of statement that OP is a symbolic memory
454    operand of mode MODE.  */
455
456 int
457 symbolic_memory_operand (op, mode)
458      rtx op;
459      enum machine_mode mode;
460 {
461   if (GET_CODE (op) == SUBREG)
462     op = SUBREG_REG (op);
463   if (GET_CODE (op) != MEM)
464     return 0;
465   op = XEXP (op, 0);
466   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
467           || GET_CODE (op) == LABEL_REF);
468 }
469
470 /* Return 1 if OP is a reference to an object in .sdata/.sbss.  */
471
472 int
473 small_data_operand (op, mode)
474      rtx op;
475      enum machine_mode mode;
476 {
477   if (! TARGET_SDATA_USE)
478     return 0;
479
480   if (GET_CODE (op) == SYMBOL_REF)
481     return SDATA_NAME_P (XSTR (op, 0));
482
483   if (GET_CODE (op) == CONST
484       && GET_CODE (XEXP (op, 0)) == PLUS
485       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
486       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
487       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
488     return SDATA_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0));
489
490   return 0;
491 }
492
493 /* Return 1 if OP is a symbol that can use 24 bit addressing.  */
494
495 int
496 addr24_operand (op, mode)
497      rtx op;
498      enum machine_mode mode;
499 {
500   if (GET_CODE (op) == LABEL_REF)
501     return TARGET_ADDR24;
502
503   if (GET_CODE (op) == SYMBOL_REF)
504     return (SMALL_NAME_P (XSTR (op, 0))
505             || (TARGET_ADDR24
506                 && CONSTANT_POOL_ADDRESS_P (op)));
507
508   if (GET_CODE (op) == CONST
509       && GET_CODE (XEXP (op, 0)) == PLUS
510       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
511       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
512       && UINT24_P (INTVAL (XEXP (XEXP (op, 0), 1))))
513     {
514       rtx sym = XEXP (XEXP (op, 0), 0);
515       return (SMALL_NAME_P (XSTR (sym, 0))
516               || (TARGET_ADDR24
517                   && CONSTANT_POOL_ADDRESS_P (op)));
518     }
519
520   return 0;
521 }
522
523 /* Return 1 if OP is a symbol that needs 32 bit addressing.  */
524
525 int
526 addr32_operand (op, mode)
527      rtx op;
528      enum machine_mode mode;
529 {
530   if (GET_CODE (op) == LABEL_REF)
531     return TARGET_ADDR32;
532
533   if (GET_CODE (op) == SYMBOL_REF)
534     return (! addr24_operand (op)
535             && ! small_data_operand (op));
536
537   if (GET_CODE (op) == CONST
538       && GET_CODE (XEXP (op, 0)) == PLUS
539       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
540       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT)
541     {
542       return (! addr24_operand (op)
543               && ! small_data_operand (op));
544     }
545
546   return 0;
547 }
548
549 /* Return 1 if OP is a function that can be called with the `bl' insn.  */
550
551 int
552 call26_operand (op, mode)
553      rtx op;
554      enum machine_mode mode;
555 {
556   if (GET_CODE (op) == SYMBOL_REF)
557     return ! LARGE_NAME_P (XSTR (op, 0));
558
559   return TARGET_CALL26;
560 }
561
562 /* Return 1 if OP is a function that must be called with 32 bit addressing.  */
563
564 int
565 call32_operand (op, mode)
566      rtx op;
567      enum machine_mode mode;
568 {
569   return ! call26_operand (op, mode);
570 }
571
572 /* Returns 1 if OP is an acceptable operand for seth/add3.  */
573
574 int
575 seth_add3_operand (op, mode)
576      rtx op;
577      enum machine_mode mode;
578 {
579   if (GET_CODE (op) == SYMBOL_REF
580       || GET_CODE (op) == LABEL_REF)
581     return 1;
582
583   if (GET_CODE (op) == CONST
584       && GET_CODE (XEXP (op, 0)) == PLUS
585       && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
586       && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
587       && INT16_P (INTVAL (XEXP (XEXP (op, 0), 1))))
588     return 1;
589
590   return 0;
591 }
592
593 /* Return true if OP is a signed 8 bit immediate value.  */
594
595 int
596 int8_operand (op, mode)
597      rtx op;
598      enum machine_mode mode;
599 {
600   if (GET_CODE (op) != CONST_INT)
601     return 0;
602   return INT8_P (INTVAL (op));
603 }
604
605 /* Return true if OP is a signed 16 bit immediate value.  */
606
607 int
608 int16_operand (op, mode)
609      rtx op;
610      enum machine_mode mode;
611 {
612   if (GET_CODE (op) != CONST_INT)
613     return 0;
614   return INT16_P (INTVAL (op));
615 }
616
617 /* Return true if OP is a signed 16 bit immediate value
618    useful in comparisons.  */
619
620 int
621 cmp_int16_operand (op, mode)
622      rtx op;
623      enum machine_mode mode;
624 {
625   if (GET_CODE (op) != CONST_INT)
626     return 0;
627   return CMP_INT16_P (INTVAL (op));
628 }
629
630 /* Return true if OP is an unsigned 16 bit immediate value.  */
631
632 int
633 uint16_operand (op, mode)
634      rtx op;
635      enum machine_mode mode;
636 {
637   if (GET_CODE (op) != CONST_INT)
638     return 0;
639   return UINT16_P (INTVAL (op));
640 }
641
642 /* Return true if OP is an unsigned 24 bit immediate value.  */
643
644 int
645 uint24_operand (op, mode)
646      rtx op;
647      enum machine_mode mode;
648 {
649   if (GET_CODE (op) != CONST_INT)
650     return 0;
651   return UINT24_P (INTVAL (op));
652 }
653
654 /* Return true if OP is a register or signed 8 bit value.  */
655
656 int
657 reg_or_int8_operand (op, mode)
658      rtx op;
659      enum machine_mode mode;
660 {
661   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
662     return register_operand (op, mode);
663   if (GET_CODE (op) != CONST_INT)
664     return 0;
665   return INT8_P (INTVAL (op));
666 }
667
668 /* Return true if OP is a register or signed 8 bit value.  */
669
670 int
671 reg_or_int16_operand (op, mode)
672      rtx op;
673      enum machine_mode mode;
674 {
675   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
676     return register_operand (op, mode);
677   if (GET_CODE (op) != CONST_INT)
678     return 0;
679   return INT16_P (INTVAL (op));
680 }
681
682 /* Return true if OP is a register or signed 8 bit value.  */
683
684 int
685 reg_or_uint16_operand (op, mode)
686      rtx op;
687      enum machine_mode mode;
688 {
689   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
690     return register_operand (op, mode);
691   if (GET_CODE (op) != CONST_INT)
692     return 0;
693   return UINT16_P (INTVAL (op));
694 }
695
696 /* Return true if OP is a register or signed 16 bit value for compares.  */
697
698 int
699 reg_or_cmp_int16_operand (op, mode)
700      rtx op;
701      enum machine_mode mode;
702 {
703   if (GET_CODE (op) == REG || GET_CODE (op) == SUBREG)
704     return register_operand (op, mode);
705   if (GET_CODE (op) != CONST_INT)
706     return 0;
707   return CMP_INT16_P (INTVAL (op));
708 }
709
710 /* Return true if OP is a const_int requiring two instructions to load.  */
711
712 int
713 two_insn_const_operand (op, mode)
714      rtx op;
715      enum machine_mode mode;
716 {
717   if (GET_CODE (op) != CONST_INT)
718     return 0;
719   if (INT16_P (INTVAL (op))
720       || UINT24_P (INTVAL (op))
721       || UPPER16_P (INTVAL (op)))
722     return 0;
723   return 1;
724 }
725
726 /* Return true if OP is an acceptable argument for a single word
727    move source.  */
728
729 int
730 move_src_operand (op, mode)
731      rtx op;
732      enum machine_mode mode;
733 {
734   switch (GET_CODE (op))
735     {
736     case SYMBOL_REF :
737     case CONST :
738       return addr24_operand (op, mode);
739     case CONST_INT :
740       /* ??? We allow more cse opportunities if we only allow constants
741          loadable with one insn, and split the rest into two.  The instances
742          where this would help should be rare and the current way is
743          simpler.  */
744       return INT32_P (INTVAL (op));
745     case LABEL_REF :
746       return TARGET_ADDR24;
747     case CONST_DOUBLE :
748       if (mode == SFmode)
749         return 1;
750       else if (mode == SImode)
751         {
752           /* Large unsigned constants are represented as const_double's.  */
753           unsigned HOST_WIDE_INT low, high;
754
755           low = CONST_DOUBLE_LOW (op);
756           high = CONST_DOUBLE_HIGH (op);
757           return high == 0 && low <= 0xffffffff;
758         }
759       else
760         return 0;
761     case REG :
762       return register_operand (op, mode);
763     case SUBREG :
764       /* (subreg (mem ...) ...) can occur here if the inner part was once a
765          pseudo-reg and is now a stack slot.  */
766       if (GET_CODE (SUBREG_REG (op)) == MEM)
767         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
768       else
769         return register_operand (op, mode);
770     case MEM :
771       return address_operand (XEXP (op, 0), mode);
772     default :
773       return 0;
774     }
775 }
776
777 /* Return true if OP is an acceptable argument for a double word
778    move source.  */
779
780 int
781 move_double_src_operand (op, mode)
782      rtx op;
783      enum machine_mode mode;
784 {
785   switch (GET_CODE (op))
786     {
787     case CONST_INT :
788     case CONST_DOUBLE :
789       if (mode == DFmode)
790         return easy_df_const (op);
791       else
792         return easy_di_const (op);
793     case REG :
794       return register_operand (op, mode);
795     case SUBREG :
796       /* (subreg (mem ...) ...) can occur here if the inner part was once a
797          pseudo-reg and is now a stack slot.  */
798       if (GET_CODE (SUBREG_REG (op)) == MEM)
799         return move_double_src_operand (SUBREG_REG (op), mode);
800       else
801         return register_operand (op, mode);
802     case MEM :
803       /* Disallow auto inc/dec for now.  */
804       if (GET_CODE (XEXP (op, 0)) == PRE_DEC
805           || GET_CODE (XEXP (op, 0)) == PRE_INC)
806         return 0;
807       return address_operand (XEXP (op, 0), mode);
808     default :
809       return 0;
810     }
811 }
812
813 /* Return true if OP is an acceptable argument for a move destination.  */
814
815 int
816 move_dest_operand (op, mode)
817      rtx op;
818      enum machine_mode mode;
819 {
820   switch (GET_CODE (op))
821     {
822     case REG :
823       return register_operand (op, mode);
824     case SUBREG :
825       /* (subreg (mem ...) ...) can occur here if the inner part was once a
826          pseudo-reg and is now a stack slot.  */
827       if (GET_CODE (SUBREG_REG (op)) == MEM)
828         return address_operand (XEXP (SUBREG_REG (op), 0), mode);
829       else
830         return register_operand (op, mode);
831     case MEM :
832       return address_operand (XEXP (op, 0), mode);
833     default :
834       return 0;
835     }
836 }
837
838 /* Return 1 if OP is a DImode const we want to handle inline.
839    This must match the code in the movdi pattern.
840    It is used by the 'G' CONST_DOUBLE_OK_FOR_LETTER.  */
841
842 int
843 easy_di_const (op)
844      rtx op;
845 {
846   rtx high_rtx, low_rtx;
847   HOST_WIDE_INT high, low;
848
849   split_double (op, &high_rtx, &low_rtx);
850   high = INTVAL (high_rtx);
851   low = INTVAL (low_rtx);
852   /* Pick constants loadable with 2 16 bit `ldi' insns.  */
853   if (high >= -128 && high <= 127
854       && low >= -128 && low <= 127)
855     return 1;
856   return 0;
857 }
858
859 /* Return 1 if OP is a DFmode const we want to handle inline.
860    This must match the code in the movdf pattern.
861    It is used by the 'H' CONST_DOUBLE_OK_FOR_LETTER.  */
862
863 int
864 easy_df_const (op)
865      rtx op;
866 {
867   REAL_VALUE_TYPE r;
868   long l[2];
869
870   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
871   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
872   if (l[0] == 0 && l[1] == 0)
873     return 1;
874   if ((l[0] & 0xffff) == 0 && l[1] == 0)
875     return 1;
876   return 0;
877 }
878
879 /* Return 1 if OP is an EQ or NE comparison operator.  */
880
881 int
882 eqne_comparison_operator (op, mode)
883     rtx op;
884     enum machine_mode mode;
885 {
886   enum rtx_code code = GET_CODE (op);
887
888   if (GET_RTX_CLASS (code) != '<')
889     return 0;
890   return (code == EQ || code == NE);
891 }
892
893 /* Return 1 if OP is a signed comparison operator.  */
894
895 int
896 signed_comparison_operator (op, mode)
897     rtx op;
898     enum machine_mode mode;
899 {
900   enum rtx_code code = GET_CODE (op);
901
902   if (GET_RTX_CLASS (code) != '<')
903     return 0;
904   return (code == EQ || code == NE
905           || code == LT || code == LE || code == GT || code == GE);
906 }
907
908 /* Return 1 if OP is (mem (reg ...)).
909    This is used in insn length calcs.  */
910
911 int
912 memreg_operand (op, mode)
913      rtx op;
914      enum machine_mode mode;
915 {
916   return GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == REG;
917 }
918 \f
919 /* Comparisons.  */
920
921 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
922    return the mode to be used for the comparison.  */
923
924 enum machine_mode
925 m32r_select_cc_mode (op, x, y)
926      enum rtx_code op;
927      rtx x, y;
928 {
929   return CCmode;
930 }
931
932 /* X and Y are two things to compare using CODE.  Emit the compare insn and
933    return the rtx for compare [arg0 of the if_then_else].  */
934
935 rtx
936 gen_compare (code, x, y)
937      enum rtx_code code;
938      rtx x, y;
939 {
940   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
941   enum rtx_code compare_code, branch_code;
942   rtx cc_reg = gen_rtx (REG, mode, CARRY_REGNUM);
943   int swap_p = 0;
944
945   switch (code)
946     {
947     case EQ: compare_code = EQ; branch_code = NE; break;
948     case NE: compare_code = EQ; branch_code = EQ; break;
949     case LT: compare_code = LT; branch_code = NE; break;
950     case LE: compare_code = LT; branch_code = EQ; swap_p = 1; break;
951     case GT: compare_code = LT; branch_code = NE; swap_p = 1; break;
952     case GE: compare_code = LT; branch_code = EQ; break;
953     case LTU: compare_code = LTU; branch_code = NE; break;
954     case LEU: compare_code = LTU; branch_code = EQ; swap_p = 1; break;
955     case GTU: compare_code = LTU; branch_code = NE; swap_p = 1; break;
956     case GEU: compare_code = LTU; branch_code = EQ; break;
957     }
958
959   if (! TARGET_OLD_COMPARE)
960     {
961       /* reg/reg equal comparison */
962       if (compare_code == EQ
963           && register_operand (y, SImode))
964         return gen_rtx (code, mode, x, y);
965       /* reg/zero signed comparison */
966       if ((compare_code == EQ || compare_code == LT)
967           && y == const0_rtx)
968         return gen_rtx (code, mode, x, y);
969       /* reg/smallconst equal comparison */
970       if (compare_code == EQ
971           && GET_CODE (y) == CONST_INT
972           && CMP_INT16_P (INTVAL (y)))
973         {
974           rtx tmp = gen_reg_rtx (SImode);
975           emit_insn (gen_cmp_ne_small_const_insn (tmp, x, y));
976           return gen_rtx (code, mode, tmp, const0_rtx);
977         }
978       /* reg/const equal comparison */
979       if (compare_code == EQ
980           && CONSTANT_P (y))
981         {
982           rtx tmp = force_reg (GET_MODE (x), y);
983           return gen_rtx (code, mode, x, tmp);
984         }
985     }
986
987   if (swap_p && CONSTANT_P (y))
988     y = force_reg (GET_MODE (x), y);
989   else if (CONSTANT_P (y))
990     {
991       int ok_const_p =
992         (code == LTU || code == LEU || code == GTU || code == GEU)
993           ? uint16_operand (y, GET_MODE (y))
994           : reg_or_cmp_int16_operand (y, GET_MODE (y));
995       if (! ok_const_p)
996         y = force_reg (GET_MODE (x), y);
997     }
998
999   switch (compare_code)
1000     {
1001     case EQ :
1002       emit_insn (gen_cmp_eqsi_insn (swap_p ? y : x, swap_p ? x : y));
1003       break;
1004     case LT :
1005       emit_insn (gen_cmp_ltsi_insn (swap_p ? y : x, swap_p ? x : y));
1006       break;
1007     case LTU :
1008       emit_insn (gen_cmp_ltusi_insn (swap_p ? y : x, swap_p ? x : y));
1009       break;
1010     }
1011
1012   return gen_rtx (branch_code, VOIDmode, cc_reg, CONST0_RTX (mode));
1013 }
1014 \f
1015 /* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.  */
1016
1017 int
1018 function_arg_partial_nregs (cum, mode, type, named)
1019      CUMULATIVE_ARGS *cum;
1020      enum machine_mode mode;
1021      tree type;
1022      int named;
1023 {
1024   int ret;
1025   int size = (((mode == BLKmode && type)
1026                ? int_size_in_bytes (type)
1027                : GET_MODE_SIZE (mode)) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1028
1029   if (*cum >= M32R_MAX_PARM_REGS)
1030     ret = 0;
1031   else if (*cum + size > M32R_MAX_PARM_REGS)
1032     ret = (*cum + size) - M32R_MAX_PARM_REGS;
1033   else
1034     ret = 0;
1035
1036   return ret;
1037 }
1038
1039 /* Do any needed setup for a variadic function.  For the M32R, we must
1040    create a register parameter block, and then copy any anonymous arguments
1041    in registers to memory.
1042
1043    CUM has not been updated for the last named argument which has type TYPE
1044    and mode MODE, and we rely on this fact.  */
1045
1046 void
1047 m32r_setup_incoming_varargs (cum, mode, type, pretend_size, no_rtl)
1048      CUMULATIVE_ARGS *cum;
1049      enum machine_mode mode;
1050      tree type;
1051      int *pretend_size;
1052      int no_rtl;
1053 {
1054   int first_anon_arg;
1055
1056   if (no_rtl)
1057     return;
1058
1059   /* All BLKmode values are passed by reference.  */
1060   if (mode == BLKmode)
1061     abort ();
1062
1063   /* We must treat `__builtin_va_alist' as an anonymous arg.  */
1064   if (current_function_varargs)
1065     first_anon_arg = *cum;
1066   else
1067     first_anon_arg = (ROUND_ADVANCE_CUM (*cum, mode, type)
1068                       + ROUND_ADVANCE_ARG (mode, type));
1069
1070   if (first_anon_arg < M32R_MAX_PARM_REGS)
1071     {
1072       /* Note that first_reg_offset < M32R_MAX_PARM_REGS.  */
1073       int first_reg_offset = first_anon_arg;
1074       /* Size in words to "pretend" allocate.  */
1075       int size = M32R_MAX_PARM_REGS - first_reg_offset;
1076       rtx regblock;
1077
1078       regblock = gen_rtx (MEM, BLKmode,
1079                           plus_constant (arg_pointer_rtx,
1080                                          FIRST_PARM_OFFSET (0)));
1081       move_block_from_reg (first_reg_offset, regblock,
1082                            size, size * UNITS_PER_WORD);
1083
1084       *pretend_size = (size * UNITS_PER_WORD);
1085     }
1086 }
1087 \f
1088 /* Cost functions.  */
1089
1090 /* Provide the costs of an addressing mode that contains ADDR.
1091    If ADDR is not a valid address, its cost is irrelevant.
1092
1093    This function is trivial at the moment.  This code doesn't live
1094    in m32r.h so it's easy to experiment.  */
1095
1096 int
1097 m32r_address_cost (addr)
1098      rtx addr;
1099 {
1100   return 1;
1101 }
1102 \f
1103 /* Type of function DECL.
1104
1105    The result is cached.  To reset the cache at the end of a function,
1106    call with DECL = NULL_TREE.  */
1107
1108 enum m32r_function_type
1109 m32r_compute_function_type (decl)
1110      tree decl;
1111 {
1112   /* Cached value.  */
1113   static enum m32r_function_type fn_type = M32R_FUNCTION_UNKNOWN;
1114   /* Last function we were called for.  */
1115   static tree last_fn = NULL_TREE;
1116
1117   /* Resetting the cached value?  */
1118   if (decl == NULL_TREE)
1119     {
1120       fn_type = M32R_FUNCTION_UNKNOWN;
1121       last_fn = NULL_TREE;
1122       return fn_type;
1123     }
1124
1125   if (decl == last_fn && fn_type != M32R_FUNCTION_UNKNOWN)
1126     return fn_type;
1127
1128   /* Compute function type.  */
1129   fn_type = (lookup_attribute ("interrupt", DECL_MACHINE_ATTRIBUTES (current_function_decl)) != NULL_TREE
1130              ? M32R_FUNCTION_INTERRUPT
1131              : M32R_FUNCTION_NORMAL);
1132
1133   last_fn = decl;
1134   return fn_type;
1135 }
1136 \f/* Function prologue/epilogue handlers.  */
1137
1138 /* M32R stack frames look like:
1139
1140              Before call                       After call
1141         +-----------------------+       +-----------------------+
1142         |                       |       |                       |
1143    high |  local variables,     |       |  local variables,     |
1144    mem  |  reg save area, etc.  |       |  reg save area, etc.  |
1145         |                       |       |                       |
1146         +-----------------------+       +-----------------------+
1147         |                       |       |                       |
1148         |  arguments on stack.  |       |  arguments on stack.  |
1149         |                       |       |                       |
1150   SP+0->+-----------------------+       +-----------------------+
1151                                         |  reg parm save area,  |
1152                                         |  only created for     |    
1153                                         |  variable argument    |    
1154                                         |  functions            |    
1155                                         +-----------------------+
1156                                         |   previous frame ptr  |
1157                                         +-----------------------+    
1158                                         |                       |    
1159                                         |  register save area   |    
1160                                         |                       |    
1161                                         +-----------------------+
1162                                         |    return address     |    
1163                                         +-----------------------+    
1164                                         |                       |    
1165                                         |  local variables      |    
1166                                         |                       |    
1167                                         +-----------------------+    
1168                                         |                       |    
1169                                         |  alloca allocations   |    
1170                                         |                       |    
1171                                         +-----------------------+    
1172                                         |                       |    
1173    low                                  |  arguments on stack   |    
1174    memory                               |                       |    
1175                                   SP+0->+-----------------------+    
1176
1177 Notes:
1178 1) The "reg parm save area" does not exist for non variable argument fns.
1179 2) The "reg parm save area" can be eliminated completely if we saved regs
1180    containing anonymous args separately but that complicates things too
1181    much (so it's not done).
1182 3) The return address is saved after the register save area so as to have as
1183    many insns as possible between the restoration of `lr' and the `jmp lr'.
1184 */
1185
1186 /* Structure to be filled in by m32r_compute_frame_size with register
1187    save masks, and offsets for the current function.  */
1188 struct m32r_frame_info
1189 {
1190   unsigned int total_size;      /* # bytes that the entire frame takes up */
1191   unsigned int extra_size;      /* # bytes of extra stuff */
1192   unsigned int pretend_size;    /* # bytes we push and pretend caller did */
1193   unsigned int args_size;       /* # bytes that outgoing arguments take up */
1194   unsigned int reg_size;        /* # bytes needed to store regs */
1195   unsigned int var_size;        /* # bytes that variables take up */
1196   unsigned int gmask;           /* mask of saved gp registers */
1197   unsigned int save_fp;         /* nonzero if fp must be saved */
1198   unsigned int save_lr;         /* nonzero if lr (return addr) must be saved */
1199   int          initialized;     /* nonzero if frame size already calculated */
1200 };
1201
1202 /* Current frame information calculated by m32r_compute_frame_size.  */
1203 static struct m32r_frame_info current_frame_info;
1204
1205 /* Zero structure to initialize current_frame_info.  */
1206 static struct m32r_frame_info zero_frame_info;
1207
1208 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
1209 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
1210
1211 /* Tell prologue and epilogue if register REGNO should be saved / restored.
1212    The return address and frame pointer are treated separately.
1213    Don't consider them here.  */
1214 #define MUST_SAVE_REGISTER(regno, interrupt_p) \
1215 ((regno) != RETURN_ADDR_REGNUM && (regno) != FRAME_POINTER_REGNUM \
1216  && (regs_ever_live[regno] && (!call_used_regs[regno] || interrupt_p)))
1217
1218 #define MUST_SAVE_FRAME_POINTER (regs_ever_live[FRAME_POINTER_REGNUM])
1219 #define MUST_SAVE_RETURN_ADDR (regs_ever_live[RETURN_ADDR_REGNUM])
1220
1221 /* Return the bytes needed to compute the frame pointer from the current
1222    stack pointer.
1223
1224    SIZE is the size needed for local variables.  */
1225
1226 unsigned int
1227 m32r_compute_frame_size (size)
1228      int size;                  /* # of var. bytes allocated.  */
1229 {
1230   int regno;
1231   unsigned int total_size, var_size, args_size, pretend_size, extra_size;
1232   unsigned int reg_size;
1233   unsigned int gmask;
1234   enum m32r_function_type fn_type;
1235   int interrupt_p;
1236
1237   var_size      = M32R_STACK_ALIGN (size);
1238   args_size     = M32R_STACK_ALIGN (current_function_outgoing_args_size);
1239   pretend_size  = current_function_pretend_args_size;
1240   extra_size    = FIRST_PARM_OFFSET (0);
1241   total_size    = extra_size + pretend_size + args_size + var_size;
1242   reg_size      = 0;
1243   gmask         = 0;
1244
1245   /* See if this is an interrupt handler.  Call used registers must be saved
1246      for them too.  */
1247   fn_type = m32r_compute_function_type (current_function_decl);
1248   interrupt_p = M32R_INTERRUPT_P (fn_type);
1249
1250   /* Calculate space needed for registers.  */
1251
1252   for (regno = 0; regno < M32R_MAX_INT_REGS; regno++)
1253     {
1254       if (MUST_SAVE_REGISTER (regno, interrupt_p))
1255         {
1256           reg_size += UNITS_PER_WORD;
1257           gmask |= 1 << regno;
1258         }
1259     }
1260
1261   current_frame_info.save_fp = MUST_SAVE_FRAME_POINTER;
1262   current_frame_info.save_lr = MUST_SAVE_RETURN_ADDR;
1263
1264   reg_size += ((current_frame_info.save_fp + current_frame_info.save_lr)
1265                * UNITS_PER_WORD);
1266   total_size += reg_size;
1267
1268   /* ??? Not sure this is necessary, and I don't think the epilogue
1269      handler will do the right thing if this changes total_size.  */
1270   total_size = M32R_STACK_ALIGN (total_size);
1271
1272   /* Save computed information.  */
1273   current_frame_info.total_size   = total_size;
1274   current_frame_info.extra_size   = extra_size;
1275   current_frame_info.pretend_size = pretend_size;
1276   current_frame_info.var_size     = var_size;
1277   current_frame_info.args_size    = args_size;
1278   current_frame_info.reg_size     = reg_size;
1279   current_frame_info.gmask        = gmask;
1280   current_frame_info.initialized  = reload_completed;
1281
1282   /* Ok, we're done.  */
1283   return total_size;
1284 }
1285 \f
1286 /* Set up the stack and frame pointer (if desired) for the function.  */
1287
1288 void
1289 m32r_output_function_prologue (file, size)
1290      FILE *file;
1291      int size;
1292 {
1293   int regno;
1294   int total_size, frame_size;
1295   char *sp_str = reg_names[STACK_POINTER_REGNUM];
1296   char *fp_str = reg_names[FRAME_POINTER_REGNUM];
1297   unsigned int gmask = current_frame_info.gmask;
1298   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
1299
1300   /* If this is an interrupt handler, mark it as such.  */
1301   if (M32R_INTERRUPT_P (fn_type))
1302     {
1303       fprintf (file, "\t%s interrupt handler\n",
1304                ASM_COMMENT_START);
1305     }
1306
1307   /* This is only for the human reader.  */
1308   fprintf (file, "\t%s BEGIN PROLOGUE %s vars= %d, regs= %d, args= %d, extra= %d\n",
1309            ASM_COMMENT_START, ASM_COMMENT_START,
1310            current_frame_info.var_size,
1311            current_frame_info.reg_size / 4,
1312            current_frame_info.args_size,
1313            current_frame_info.extra_size);
1314
1315   total_size = (! current_frame_info.initialized
1316                 ? m32r_compute_frame_size (size)
1317                 : current_frame_info.total_size);
1318
1319   /* These cases shouldn't happen.  Catch them now.  */
1320   if (total_size == 0 && gmask)
1321     abort ();
1322
1323 #if 1
1324   /* Allocate space for register arguments if this is a variadic function.  */
1325   if (current_frame_info.pretend_size != 0)
1326     fprintf (file, "\taddi %s,%d\n",
1327              sp_str, -current_frame_info.pretend_size);
1328 #else
1329   /* If there are unnamed args in registers, save them.  */
1330   if (current_function_stdarg || current_function_varargs)
1331     {
1332       int i;
1333       fprintf (file, "\taddi %s,%d\n",
1334                sp_str, - M32R_MAX_PARM_REGS * UNITS_PER_WORD);
1335       for (i = 0; i < M32R_MAX_PARM_REGS; ++i)
1336         fprintf (file, "\tst %s,@(sp,%d)\n",
1337                  reg_names[i], i * UNITS_PER_WORD);
1338     }
1339 #endif
1340
1341   /* Save any registers we need to and set up fp.  */
1342
1343   if (current_frame_info.save_fp)
1344     fprintf (file, "\tpush %s\n", fp_str);
1345
1346   gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1347
1348   /* Save any needed call-saved regs (and call-used if this is an
1349      interrupt handler).  */
1350   for (regno = 0; regno <= M32R_MAX_INT_REGS; ++regno)
1351     {
1352       if ((gmask & (1 << regno)) != 0)
1353         fprintf (file, "\tpush %s\n", reg_names[regno]);
1354     }
1355
1356   if (current_frame_info.save_lr)
1357     fprintf (file, "\tpush %s\n", reg_names[RETURN_ADDR_REGNUM]);
1358
1359   /* Allocate the stack frame.  */
1360   frame_size = total_size - (current_frame_info.pretend_size
1361                              + current_frame_info.reg_size);
1362   if (frame_size == 0)
1363     ; /* nothing to do */
1364   else if (frame_size <= 128)
1365     fprintf (file, "\taddi %s,%d\n",
1366              sp_str, -frame_size);
1367   else if (frame_size <= 32768)
1368     fprintf (file, "\tadd3 %s,%s,%d\n",
1369              sp_str, sp_str, -frame_size);
1370   else
1371     fprintf (file, "\tld24 %s,%d\n\tsub %s,%s\n",
1372              reg_names[PROLOGUE_TMP_REGNUM], frame_size,
1373              sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
1374
1375   if (frame_pointer_needed)
1376     fprintf (file, "\tmv %s,%s\n", fp_str, sp_str);
1377
1378   fprintf (file, "\t%s END PROLOGUE\n", ASM_COMMENT_START);
1379 }
1380 \f
1381 /* Do any necessary cleanup after a function to restore stack, frame,
1382    and regs. */
1383
1384 void
1385 m32r_output_function_epilogue (file, size)
1386      FILE *file;
1387      int size;
1388 {
1389   int regno;
1390   int noepilogue = FALSE;
1391   int total_size;
1392   enum m32r_function_type fn_type = m32r_compute_function_type (current_function_decl);
1393
1394   /* This is only for the human reader.  */
1395   fprintf (file, "\t%s EPILOGUE\n", ASM_COMMENT_START);
1396
1397   if (!current_frame_info.initialized)
1398     abort ();
1399   total_size = current_frame_info.total_size;
1400
1401   if (total_size == 0)
1402     {
1403       rtx insn = get_last_insn ();
1404
1405       /* If the last insn was a BARRIER, we don't have to write any code
1406          because a jump (aka return) was put there.  */
1407       if (GET_CODE (insn) == NOTE)
1408         insn = prev_nonnote_insn (insn);
1409       if (insn && GET_CODE (insn) == BARRIER)
1410         noepilogue = TRUE;
1411     }
1412
1413   if (!noepilogue)
1414     {
1415       unsigned int pretend_size = current_frame_info.pretend_size;
1416       unsigned int frame_size = total_size - pretend_size;
1417       unsigned int var_size = current_frame_info.var_size;
1418       unsigned int args_size = current_frame_info.args_size;
1419       unsigned int gmask = current_frame_info.gmask;
1420       int can_trust_sp_p = !current_function_calls_alloca;
1421       char *sp_str = reg_names[STACK_POINTER_REGNUM];
1422       char *fp_str = reg_names[FRAME_POINTER_REGNUM];
1423
1424       /* The first thing to do is point the sp at the bottom of the register
1425          save area.  */
1426       if (can_trust_sp_p)
1427         {
1428           unsigned int reg_offset = var_size + args_size;
1429           if (reg_offset == 0)
1430             ; /* nothing to do */
1431           else if (reg_offset < 32768)
1432             fprintf (file, "\tadd3 %s,%s,%d\n",
1433                      sp_str, sp_str, reg_offset);
1434           else
1435             fprintf (file, "\tld24 %s,%d\n\tadd %s,%s\n",
1436                      reg_names[PROLOGUE_TMP_REGNUM], reg_offset,
1437                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
1438         }
1439       else if (frame_pointer_needed)
1440         {
1441           unsigned int reg_offset = var_size + args_size;
1442           if (reg_offset == 0)
1443             fprintf (file, "\tmv %s,%s\n", sp_str, fp_str);
1444           else if (reg_offset < 32768)
1445             fprintf (file, "\tadd3 %s,%s,%d\n",
1446                      sp_str, fp_str, reg_offset);
1447           else
1448             fprintf (file, "\tld24 %s,%d\n\tadd %s,%s\n",
1449                      reg_names[PROLOGUE_TMP_REGNUM], reg_offset,
1450                      sp_str, reg_names[PROLOGUE_TMP_REGNUM]);
1451         }
1452       else
1453         abort ();
1454
1455       if (current_frame_info.save_lr)
1456         fprintf (file, "\tpop %s\n", reg_names[RETURN_ADDR_REGNUM]);
1457
1458       /* Restore any saved registers, in reverse order of course.  */
1459       gmask &= ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK);
1460       for (regno = M32R_MAX_INT_REGS - 1; regno >= 0; --regno)
1461         {
1462           if ((gmask & (1L << regno)) != 0)
1463             fprintf (file, "\tpop %s\n", reg_names[regno]);
1464         }
1465
1466       if (current_frame_info.save_fp)
1467         fprintf (file, "\tpop %s\n", fp_str);
1468
1469       /* Remove varargs area if present.  */
1470       if (current_frame_info.pretend_size != 0)
1471         fprintf (file, "\taddi %s,%d\n",
1472                  sp_str, current_frame_info.pretend_size);
1473         
1474       /* Emit the return instruction.  */
1475       if (M32R_INTERRUPT_P (fn_type))
1476         fprintf (file, "\trte\n");
1477       else
1478         fprintf (file, "\tjmp %s\n", reg_names[RETURN_ADDR_REGNUM]);
1479     }
1480
1481 #if 0 /* no longer needed */
1482   /* Ensure the function cleanly ends on a 32 bit boundary.  */
1483   fprintf (file, "\t.fillinsn\n");
1484 #endif
1485
1486   /* Reset state info for each function.  */
1487   current_frame_info = zero_frame_info;
1488   m32r_compute_function_type (NULL_TREE);
1489 }
1490 \f
1491 /* PIC */
1492
1493 /* Emit special PIC prologues and epilogues.  */
1494
1495 void
1496 m32r_finalize_pic ()
1497 {
1498   /* nothing to do */
1499 }
1500 \f
1501 /* Nested function support.  */
1502
1503 /* Emit RTL insns to initialize the variable parts of a trampoline.
1504    FNADDR is an RTX for the address of the function's pure code.
1505    CXT is an RTX for the static chain value for the function.  */
1506
1507 void
1508 m32r_initialize_trampoline (tramp, fnaddr, cxt)
1509      rtx tramp, fnaddr, cxt;
1510 {
1511 }
1512 \f
1513 /* Set the cpu type and print out other fancy things,
1514    at the top of the file.  */
1515
1516 void
1517 m32r_asm_file_start (file)
1518      FILE *file;
1519 {
1520   if (flag_verbose_asm)
1521     fprintf (file, "%s M32R/D special options: -G %d\n",
1522              ASM_COMMENT_START, g_switch_value);
1523 }
1524 \f
1525 /* Print operand X (an rtx) in assembler syntax to file FILE.
1526    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
1527    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
1528
1529 void
1530 m32r_print_operand (file, x, code)
1531      FILE *file;
1532      rtx x;
1533      int code;
1534 {
1535   switch (code)
1536     {
1537     case 'R' :
1538       /* Write second word of DImode or DFmode reference,
1539          register or memory.  */
1540       if (GET_CODE (x) == REG)
1541         fputs (reg_names[REGNO (x)+1], file);
1542       else if (GET_CODE (x) == MEM)
1543         {
1544           fprintf (file, "@(");
1545           /* Handle possible auto-increment.  Since it is pre-increment and
1546              we have already done it, we can just use an offset of four.  */
1547           /* ??? This is taken from rs6000.c I think.  I don't think it is
1548              currently necessary, but keep it around.  */
1549           if (GET_CODE (XEXP (x, 0)) == PRE_INC
1550               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
1551             output_address (plus_constant (XEXP (XEXP (x, 0), 0), 4));
1552           else
1553             output_address (plus_constant (XEXP (x, 0), 4));
1554           fputc (')', file);
1555         }
1556       else
1557         output_operand_lossage ("invalid operand to %R code");
1558       return;
1559
1560     case 'H' : /* High word */
1561     case 'L' : /* Low word */
1562       if (GET_CODE (x) == REG)
1563         {
1564           /* L = least significant word, H = most significant word */
1565           if ((WORDS_BIG_ENDIAN != 0) ^ (code == 'L'))
1566             fputs (reg_names[REGNO (x)], file);
1567           else
1568             fputs (reg_names[REGNO (x)+1], file);
1569         }
1570       else if (GET_CODE (x) == CONST_INT
1571                || GET_CODE (x) == CONST_DOUBLE)
1572         {
1573           rtx first, second;
1574
1575           split_double (x, &first, &second);
1576           fprintf (file, "0x%08lx",
1577                    code == 'L' ? INTVAL (first) : INTVAL (second));
1578         }
1579       else
1580         output_operand_lossage ("invalid operand to %H/%L code");
1581       return;
1582
1583     case 'A' :
1584       {
1585         REAL_VALUE_TYPE d;
1586         char str[30];
1587
1588         if (GET_CODE (x) != CONST_DOUBLE
1589             || GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT)
1590           abort ();
1591         REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1592         REAL_VALUE_TO_DECIMAL (d, "%.20e", str);
1593         fprintf (file, "%s", str);
1594         return;
1595       }
1596
1597     case 'B' : /* Bottom half */
1598     case 'T' : /* Top half */
1599       /* Output the argument to a `seth' insn (sets the Top half-word).
1600          For constants output arguments to a seth/or3 pair to set Top and
1601          Bottom halves.  For symbols output arguments to a seth/add3 pair to
1602          set Top and Bottom halves.  The difference exists because for
1603          constants seth/or3 is more readable but for symbols we need to use
1604          the same scheme as `ld' and `st' insns (16 bit addend is signed).  */
1605       switch (GET_CODE (x))
1606         {
1607         case CONST_INT :
1608         case CONST_DOUBLE :
1609           {
1610             rtx first, second;
1611
1612             split_double (x, &first, &second);
1613             x = WORDS_BIG_ENDIAN ? second : first;
1614             fprintf (file,
1615 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1616                      "0x%x",
1617 #else
1618                      "0x%lx",
1619 #endif
1620                      (code == 'B'
1621                       ? INTVAL (x) & 0xffff
1622                       : (INTVAL (x) >> 16) & 0xffff));
1623           }
1624           return;
1625         case CONST :
1626         case SYMBOL_REF :
1627           if (code == 'B'
1628               && small_data_operand (x, VOIDmode))
1629             {
1630               fputs ("sda(", file);
1631               output_addr_const (file, x);
1632               fputc (')', file);
1633               return;
1634             }
1635           /* fall through */
1636         case LABEL_REF :
1637           fputs (code == 'T' ? "shigh(" : "low(", file);
1638           output_addr_const (file, x);
1639           fputc (')', file);
1640           return;
1641         default :
1642           output_operand_lossage ("invalid operand to %T/%B code");
1643           return;
1644         }
1645       break;
1646
1647     case 'U' :
1648       /* FIXME: wip */
1649       /* Output a load/store with update indicator if appropriate.  */
1650       if (GET_CODE (x) == MEM)
1651         {
1652           if (GET_CODE (XEXP (x, 0)) == PRE_INC
1653               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
1654             fputs (".a", file);
1655         }
1656       else
1657         output_operand_lossage ("invalid operand to %U code");
1658       return;
1659
1660     case 'N' :
1661       /* Print a constant value negated.  */
1662       if (GET_CODE (x) == CONST_INT)
1663         output_addr_const (file, GEN_INT (- INTVAL (x)));
1664       else
1665         output_operand_lossage ("invalid operand to %N code");
1666       return;
1667
1668     case 'X' :
1669       /* Print a const_int in hex.  Used in comments.  */
1670       if (GET_CODE (x) == CONST_INT)
1671         fprintf (file,
1672 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
1673                  "0x%x",
1674 #else
1675                  "0x%lx",
1676 #endif
1677                  INTVAL (x));
1678       return;
1679
1680     case '#' :
1681       fputs (IMMEDIATE_PREFIX, file);
1682       return;
1683
1684 #if 0 /* ??? no longer used */
1685     case '@' :
1686       fputs (reg_names[SDA_REGNUM], file);
1687       return;
1688 #endif
1689
1690     case 0 :
1691       /* Do nothing special.  */
1692       break;
1693
1694     default :
1695       /* Unknown flag.  */
1696       output_operand_lossage ("invalid operand output code");
1697     }
1698
1699   switch (GET_CODE (x))
1700     {
1701     case REG :
1702       fputs (reg_names[REGNO (x)], file);
1703       break;
1704
1705     case MEM :
1706       fprintf (file, "@(");
1707       if (GET_CODE (XEXP (x, 0)) == PRE_INC)
1708         output_address (plus_constant (XEXP (XEXP (x, 0), 0),
1709                                        GET_MODE_SIZE (GET_MODE (x))));
1710       else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
1711         output_address (plus_constant (XEXP (XEXP (x, 0), 0),
1712                                        - GET_MODE_SIZE (GET_MODE (x))));
1713       else
1714         output_address (XEXP (x, 0));
1715       fputc (')', file);
1716       break;
1717
1718     case CONST_DOUBLE :
1719       /* We handle SFmode constants here as output_addr_const doesn't.  */
1720       if (GET_MODE (x) == SFmode)
1721         {
1722           REAL_VALUE_TYPE d;
1723           long l;
1724
1725           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
1726           REAL_VALUE_TO_TARGET_SINGLE (d, l);
1727           fprintf (file, "0x%08lx", l);
1728           break;
1729         }
1730
1731       /* Fall through.  Let output_addr_const deal with it.  */
1732
1733     default :
1734       output_addr_const (file, x);
1735       break;
1736     }
1737 }
1738
1739 /* Print a memory address as an operand to reference that memory location.  */
1740
1741 void
1742 m32r_print_operand_address (file, addr)
1743      FILE *file;
1744      rtx addr;
1745 {
1746   register rtx base, index = 0;
1747   int offset = 0;
1748
1749   switch (GET_CODE (addr))
1750     {
1751     case REG :
1752       fputs (reg_names[REGNO (addr)], file);
1753       break;
1754
1755     case PLUS :
1756       if (GET_CODE (XEXP (addr, 0)) == CONST_INT)
1757         offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
1758       else if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
1759         offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
1760       else
1761         base = XEXP (addr, 0), index = XEXP (addr, 1);
1762       if (GET_CODE (base) == REG)
1763         {
1764           /* Print the offset first (if present) to conform to the manual.  */
1765           if (index == 0)
1766             {
1767               if (offset != 0)
1768                 fprintf (file, "%d,", offset);
1769               fputs (reg_names[REGNO (base)], file);
1770             }
1771           /* The chip doesn't support this, but left in for generality.  */
1772           else if (GET_CODE (index) == REG)
1773             fprintf (file, "%s,%s",
1774                      reg_names[REGNO (base)], reg_names[REGNO (index)]);
1775           /* Not sure this can happen, but leave in for now.  */
1776           else if (GET_CODE (index) == SYMBOL_REF)
1777             {
1778               output_addr_const (file, index);
1779               fputc (',', file);
1780               fputs (reg_names[REGNO (base)], file);
1781             }
1782           else
1783             abort ();
1784         }
1785       else if (GET_CODE (base) == LO_SUM)
1786         {
1787           if (index != 0
1788               || GET_CODE (XEXP (base, 0)) != REG)
1789             abort ();
1790           if (small_data_operand (XEXP (base, 1), VOIDmode))
1791             fputs ("sda(", file);
1792           else
1793             fputs ("low(", file);
1794           output_addr_const (file, plus_constant (XEXP (base, 1), offset));
1795           fputs ("),", file);
1796           fputs (reg_names[REGNO (XEXP (base, 0))], file);
1797         }
1798       else
1799         abort ();
1800       break;
1801
1802     case LO_SUM :
1803       if (GET_CODE (XEXP (addr, 0)) != REG)
1804         abort ();
1805       if (small_data_operand (XEXP (addr, 1), VOIDmode))
1806         fputs ("sda(", file);
1807       else
1808         fputs ("low(", file);
1809       output_addr_const (file, XEXP (addr, 1));
1810       fputs ("),", file);
1811       fputs (reg_names[REGNO (XEXP (addr, 0))], file);
1812       break;
1813
1814     case PRE_INC :
1815     case PRE_DEC :
1816       /* We shouldn't get here as we've lost the mode of the memory object
1817          (which says how much to inc/dec by).  */
1818       abort ();
1819       break;
1820
1821     default :
1822       output_addr_const (file, addr);
1823       break;
1824     }
1825 }