target.h (struct gcc_target): Add frame_pointer_required field.
[platform/upstream/gcc.git] / gcc / config / fr30 / fr30.c
1 /* FR30 specific functions.
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4    Contributed by Cygnus Solutions.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING3.  If not see
20    <http://www.gnu.org/licenses/>.  */
21
22 /*{{{  Includes */ 
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "insn-attr.h"
35 #include "flags.h"
36 #include "recog.h"
37 #include "tree.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "obstack.h"
41 #include "except.h"
42 #include "function.h"
43 #include "toplev.h"
44 #include "tm_p.h"
45 #include "target.h"
46 #include "target-def.h"
47
48 /*}}}*/
49 /*{{{  Function Prologues & Epilogues */ 
50
51 /* The FR30 stack looks like this:
52
53              Before call                       After call
54    FP ->|                       |       |                       |
55         +-----------------------+       +-----------------------+       high 
56         |                       |       |                       |       memory
57         |  local variables,     |       |  local variables,     |
58         |  reg save area, etc.  |       |  reg save area, etc.  |
59         |                       |       |                       |
60         +-----------------------+       +-----------------------+
61         |                       |       |                       |
62         | args to the func that |       |  args to this func.   |
63         | is being called that  |       |                       |
64    SP ->| do not fit in regs    |       |                       |
65         +-----------------------+       +-----------------------+
66                                         |  args that used to be |  \
67                                         | in regs; only created |   |  pretend_size 
68                                    AP-> |   for vararg funcs    |  /  
69                                         +-----------------------+    
70                                         |                       |  \  
71                                         |  register save area   |   |
72                                         |                       |   |
73                                         +-----------------------+   |  reg_size
74                                         |    return address     |   | 
75                                         +-----------------------+   |
76                                    FP ->|   previous frame ptr  |  /
77                                         +-----------------------+    
78                                         |                       |  \   
79                                         |  local variables      |   |  var_size 
80                                         |                       |  /  
81                                         +-----------------------+    
82                                         |                       |  \       
83      low                                |  room for args to     |   |
84      memory                             |  other funcs called   |   |  args_size     
85                                         |  from this one        |   |
86                                    SP ->|                       |  /  
87                                         +-----------------------+    
88    
89    Note, AP is a fake hard register.  It will be eliminated in favor of
90    SP or FP as appropriate.
91
92    Note, Some or all of the stack sections above may be omitted if they 
93    are not needed.  */
94
95 /* Structure to be filled in by fr30_compute_frame_size() with register
96    save masks, and offsets for the current function.  */
97 struct fr30_frame_info
98 {
99   unsigned int total_size;      /* # Bytes that the entire frame takes up.  */
100   unsigned int pretend_size;    /* # Bytes we push and pretend caller did.  */
101   unsigned int args_size;       /* # Bytes that outgoing arguments take up.  */
102   unsigned int reg_size;        /* # Bytes needed to store regs.  */
103   unsigned int var_size;        /* # Bytes that variables take up.  */
104   unsigned int frame_size;      /* # Bytes in current frame.  */
105   unsigned int gmask;           /* Mask of saved registers.  */
106   unsigned int save_fp;         /* Nonzero if frame pointer must be saved.  */
107   unsigned int save_rp;         /* Nonzero if return pointer must be saved.  */
108   int          initialised;     /* Nonzero if frame size already calculated.  */
109 };
110
111 /* Current frame information calculated by fr30_compute_frame_size().  */
112 static struct fr30_frame_info   current_frame_info;
113
114 /* Zero structure to initialize current_frame_info.  */
115 static struct fr30_frame_info   zero_frame_info;
116
117 static void fr30_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
118                                          tree, int *, int);
119 static bool fr30_must_pass_in_stack (enum machine_mode, const_tree);
120 static int fr30_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
121                                    tree, bool);
122 static bool fr30_frame_pointer_required (void);
123
124 #define FRAME_POINTER_MASK      (1 << (FRAME_POINTER_REGNUM))
125 #define RETURN_POINTER_MASK     (1 << (RETURN_POINTER_REGNUM))
126
127 /* Tell prologue and epilogue if register REGNO should be saved / restored.
128    The return address and frame pointer are treated separately.
129    Don't consider them here.  */
130 #define MUST_SAVE_REGISTER(regno)      \
131   (   (regno) != RETURN_POINTER_REGNUM \
132    && (regno) != FRAME_POINTER_REGNUM  \
133    && df_regs_ever_live_p (regno)      \
134    && ! call_used_regs [regno]         )
135
136 #define MUST_SAVE_FRAME_POINTER  (df_regs_ever_live_p (FRAME_POINTER_REGNUM)  || frame_pointer_needed)
137 #define MUST_SAVE_RETURN_POINTER (df_regs_ever_live_p (RETURN_POINTER_REGNUM) || crtl->profile)
138
139 #if UNITS_PER_WORD == 4
140 #define WORD_ALIGN(SIZE) (((SIZE) + 3) & ~3)
141 #endif
142 \f
143 /* Initialize the GCC target structure.  */
144 #undef  TARGET_ASM_ALIGNED_HI_OP
145 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
146 #undef  TARGET_ASM_ALIGNED_SI_OP
147 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
148
149 #undef  TARGET_PROMOTE_PROTOTYPES
150 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
151 #undef  TARGET_PASS_BY_REFERENCE
152 #define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
153 #undef  TARGET_ARG_PARTIAL_BYTES
154 #define TARGET_ARG_PARTIAL_BYTES fr30_arg_partial_bytes
155
156 #undef  TARGET_SETUP_INCOMING_VARARGS
157 #define TARGET_SETUP_INCOMING_VARARGS fr30_setup_incoming_varargs
158 #undef  TARGET_MUST_PASS_IN_STACK
159 #define TARGET_MUST_PASS_IN_STACK fr30_must_pass_in_stack
160
161 #undef TARGET_FRAME_POINTER_REQUIRED
162 #define TARGET_FRAME_POINTER_REQUIRED fr30_frame_pointer_required
163
164 struct gcc_target targetm = TARGET_INITIALIZER;
165 \f
166 /* Returns the number of bytes offset between FROM_REG and TO_REG
167    for the current function.  As a side effect it fills in the 
168    current_frame_info structure, if the data is available.  */
169 unsigned int
170 fr30_compute_frame_size (int from_reg, int to_reg)
171 {
172   int           regno;
173   unsigned int  return_value;
174   unsigned int  var_size;
175   unsigned int  args_size;
176   unsigned int  pretend_size;
177   unsigned int  reg_size;
178   unsigned int  gmask;
179
180   var_size      = WORD_ALIGN (get_frame_size ());
181   args_size     = WORD_ALIGN (crtl->outgoing_args_size);
182   pretend_size  = crtl->args.pretend_args_size;
183
184   reg_size      = 0;
185   gmask         = 0;
186
187   /* Calculate space needed for registers.  */
188   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno ++)
189     {
190       if (MUST_SAVE_REGISTER (regno))
191         {
192           reg_size += UNITS_PER_WORD;
193           gmask |= 1 << regno;
194         }
195     }
196
197   current_frame_info.save_fp = MUST_SAVE_FRAME_POINTER;
198   current_frame_info.save_rp = MUST_SAVE_RETURN_POINTER;
199
200   reg_size += (current_frame_info.save_fp + current_frame_info.save_rp)
201                * UNITS_PER_WORD;
202
203   /* Save computed information.  */
204   current_frame_info.pretend_size = pretend_size;
205   current_frame_info.var_size     = var_size;
206   current_frame_info.args_size    = args_size;
207   current_frame_info.reg_size     = reg_size;
208   current_frame_info.frame_size   = args_size + var_size;
209   current_frame_info.total_size   = args_size + var_size + reg_size + pretend_size;
210   current_frame_info.gmask        = gmask;
211   current_frame_info.initialised  = reload_completed;
212
213   /* Calculate the required distance.  */
214   return_value = 0;
215   
216   if (to_reg == STACK_POINTER_REGNUM)
217     return_value += args_size + var_size;
218   
219   if (from_reg == ARG_POINTER_REGNUM)
220     return_value += reg_size;
221
222   return return_value;
223 }
224
225 /* Called after register allocation to add any instructions needed for the
226    prologue.  Using a prologue insn is favored compared to putting all of the
227    instructions in output_function_prologue(), since it allows the scheduler
228    to intermix instructions with the saves of the caller saved registers.  In
229    some cases, it might be necessary to emit a barrier instruction as the last
230    insn to prevent such scheduling.  */
231
232 void
233 fr30_expand_prologue (void)
234 {
235   int regno;
236   rtx insn;
237
238   if (! current_frame_info.initialised)
239     fr30_compute_frame_size (0, 0);
240
241   /* This cases shouldn't happen.  Catch it now.  */
242   gcc_assert (current_frame_info.total_size || !current_frame_info.gmask);
243
244   /* Allocate space for register arguments if this is a variadic function.  */
245   if (current_frame_info.pretend_size)
246     {
247       int regs_to_save = current_frame_info.pretend_size / UNITS_PER_WORD;
248       
249       /* Push argument registers into the pretend arg area.  */
250       for (regno = FIRST_ARG_REGNUM + FR30_NUM_ARG_REGS; regno --, regs_to_save --;)
251         {
252           insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
253           RTX_FRAME_RELATED_P (insn) = 1;
254         }
255     }
256
257   if (current_frame_info.gmask)
258     {
259       /* Save any needed call-saved regs.  */
260       for (regno = STACK_POINTER_REGNUM; regno--;)
261         {
262           if ((current_frame_info.gmask & (1 << regno)) != 0)
263             {
264               insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
265               RTX_FRAME_RELATED_P (insn) = 1;
266             }
267         }
268     }
269
270   /* Save return address if necessary.  */
271   if (current_frame_info.save_rp)
272     {
273       insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, 
274                                                      RETURN_POINTER_REGNUM)));
275       RTX_FRAME_RELATED_P (insn) = 1;
276     }
277
278   /* Save old frame pointer and create new one, if necessary.  */
279   if (current_frame_info.save_fp)
280     {
281       if (current_frame_info.frame_size < ((1 << 10) - UNITS_PER_WORD))
282         {
283           int enter_size = current_frame_info.frame_size + UNITS_PER_WORD;
284           rtx pattern;
285           
286           insn = emit_insn (gen_enter_func (GEN_INT (enter_size)));
287           RTX_FRAME_RELATED_P (insn) = 1;
288           
289           pattern = PATTERN (insn);
290           
291           /* Also mark all 3 subexpressions as RTX_FRAME_RELATED_P. */
292           if (GET_CODE (pattern) == PARALLEL)
293             {
294               int x;
295               for (x = XVECLEN (pattern, 0); x--;)
296                 {
297                   rtx part = XVECEXP (pattern, 0, x);
298                   
299                   /* One of the insns in the ENTER pattern updates the
300                      frame pointer.  If we do not actually need the frame
301                      pointer in this function then this is a side effect
302                      rather than a desired effect, so we do not mark that
303                      insn as being related to the frame set up.  Doing this
304                      allows us to compile the crash66.C test file in the
305                      G++ testsuite.  */
306                   if (! frame_pointer_needed
307                       && GET_CODE (part) == SET
308                       && SET_DEST (part) == hard_frame_pointer_rtx)
309                     RTX_FRAME_RELATED_P (part) = 0;
310                   else
311                     RTX_FRAME_RELATED_P (part) = 1;
312                 }
313             }
314         }
315       else
316         {
317           insn = emit_insn (gen_movsi_push (frame_pointer_rtx));
318           RTX_FRAME_RELATED_P (insn) = 1;
319
320           if (frame_pointer_needed)
321             {
322               insn = emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
323               RTX_FRAME_RELATED_P (insn) = 1;
324             }
325         }
326     }
327
328   /* Allocate the stack frame.  */
329   if (current_frame_info.frame_size == 0)
330     ; /* Nothing to do.  */
331   else if (current_frame_info.save_fp
332            && current_frame_info.frame_size < ((1 << 10) - UNITS_PER_WORD))
333     ; /* Nothing to do.  */
334   else if (current_frame_info.frame_size <= 512)
335     {
336       insn = emit_insn (gen_add_to_stack
337                          (GEN_INT (- (signed) current_frame_info.frame_size)));
338       RTX_FRAME_RELATED_P (insn) = 1;
339     }
340   else
341     {
342       rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
343       insn = emit_insn (gen_movsi (tmp, GEN_INT (current_frame_info.frame_size)));
344       RTX_FRAME_RELATED_P (insn) = 1;
345       insn = emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
346       RTX_FRAME_RELATED_P (insn) = 1;
347     }
348
349   if (crtl->profile)
350     emit_insn (gen_blockage ());
351 }
352
353 /* Called after register allocation to add any instructions needed for the
354    epilogue.  Using an epilogue insn is favored compared to putting all of the
355    instructions in output_function_epilogue(), since it allows the scheduler
356    to intermix instructions with the restores of the caller saved registers.
357    In some cases, it might be necessary to emit a barrier instruction as the
358    first insn to prevent such scheduling.  */
359 void
360 fr30_expand_epilogue (void)
361 {
362   int regno;
363
364   /* Perform the inversion operations of the prologue.  */
365   gcc_assert (current_frame_info.initialised);
366   
367   /* Pop local variables and arguments off the stack.
368      If frame_pointer_needed is TRUE then the frame pointer register
369      has actually been used as a frame pointer, and we can recover
370      the stack pointer from it, otherwise we must unwind the stack
371      manually.  */
372   if (current_frame_info.frame_size > 0)
373     {
374       if (current_frame_info.save_fp && frame_pointer_needed)
375         {
376           emit_insn (gen_leave_func ());
377           current_frame_info.save_fp = 0;
378         }
379       else if (current_frame_info.frame_size <= 508)
380         emit_insn (gen_add_to_stack
381                    (GEN_INT (current_frame_info.frame_size)));
382       else
383         {
384           rtx tmp = gen_rtx_REG (Pmode, PROLOGUE_TMP_REGNUM);
385           emit_insn (gen_movsi (tmp, GEN_INT (current_frame_info.frame_size)));
386           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
387         }
388     }
389   
390   if (current_frame_info.save_fp)
391     emit_insn (gen_movsi_pop (frame_pointer_rtx));
392   
393   /* Pop all the registers that were pushed.  */
394   if (current_frame_info.save_rp)
395     emit_insn (gen_movsi_pop (gen_rtx_REG (Pmode, RETURN_POINTER_REGNUM)));
396     
397   for (regno = 0; regno < STACK_POINTER_REGNUM; regno ++)
398     if (current_frame_info.gmask & (1 << regno))
399       emit_insn (gen_movsi_pop (gen_rtx_REG (Pmode, regno)));
400   
401   if (current_frame_info.pretend_size)
402     emit_insn (gen_add_to_stack (GEN_INT (current_frame_info.pretend_size)));
403
404   /* Reset state info for each function.  */
405   current_frame_info = zero_frame_info;
406
407   emit_jump_insn (gen_return_from_func ());
408 }
409
410 /* Do any needed setup for a variadic function.  We must create a register
411    parameter block, and then copy any anonymous arguments, plus the last
412    named argument, from registers into memory.  * copying actually done in
413    fr30_expand_prologue().
414
415    ARG_REGS_USED_SO_FAR has *not* been updated for the last named argument
416    which has type TYPE and mode MODE, and we rely on this fact.  */
417 void
418 fr30_setup_incoming_varargs (CUMULATIVE_ARGS *arg_regs_used_so_far,
419                              enum machine_mode mode,
420                              tree type ATTRIBUTE_UNUSED,
421                              int *pretend_size,
422                              int second_time ATTRIBUTE_UNUSED)
423 {
424   int size;
425
426   /* All BLKmode values are passed by reference.  */
427   gcc_assert (mode != BLKmode);
428
429   /* ??? This run-time test as well as the code inside the if
430      statement is probably unnecessary.  */
431   if (targetm.calls.strict_argument_naming (arg_regs_used_so_far))
432     /* If TARGET_STRICT_ARGUMENT_NAMING returns true, then the last named
433        arg must not be treated as an anonymous arg.  */
434     arg_regs_used_so_far += fr30_num_arg_regs (mode, type);
435
436   size = FR30_NUM_ARG_REGS - (* arg_regs_used_so_far);
437
438   if (size <= 0)
439     return;
440
441   * pretend_size = (size * UNITS_PER_WORD);
442 }
443
444 /*}}}*/
445 /*{{{  Printing operands */ 
446
447 /* Print a memory address as an operand to reference that memory location.  */
448
449 void
450 fr30_print_operand_address (FILE *stream, rtx address)
451 {
452   switch (GET_CODE (address))
453     {
454     case SYMBOL_REF:
455       output_addr_const (stream, address);
456       break;
457       
458     default:
459       fprintf (stderr, "code = %x\n", GET_CODE (address));
460       debug_rtx (address);
461       output_operand_lossage ("fr30_print_operand_address: unhandled address");
462       break;
463     }
464 }
465
466 /* Print an operand.  */
467
468 void
469 fr30_print_operand (FILE *file, rtx x, int code)
470 {
471   rtx x0;
472   
473   switch (code)
474     {
475     case '#':
476       /* Output a :D if this instruction is delayed.  */
477       if (dbr_sequence_length () != 0)
478         fputs (":D", file);
479       return;
480       
481     case 'p':
482       /* Compute the register name of the second register in a hi/lo
483          register pair.  */
484       if (GET_CODE (x) != REG)
485         output_operand_lossage ("fr30_print_operand: unrecognized %%p code");
486       else
487         fprintf (file, "r%d", REGNO (x) + 1);
488       return;
489       
490     case 'b':
491       /* Convert GCC's comparison operators into FR30 comparison codes.  */
492       switch (GET_CODE (x))
493         {
494         case EQ:  fprintf (file, "eq"); break;
495         case NE:  fprintf (file, "ne"); break;
496         case LT:  fprintf (file, "lt"); break;
497         case LE:  fprintf (file, "le"); break;
498         case GT:  fprintf (file, "gt"); break;
499         case GE:  fprintf (file, "ge"); break;
500         case LTU: fprintf (file, "c"); break;
501         case LEU: fprintf (file, "ls"); break;
502         case GTU: fprintf (file, "hi"); break;
503         case GEU: fprintf (file, "nc");  break;
504         default:
505           output_operand_lossage ("fr30_print_operand: unrecognized %%b code");
506           break;
507         }
508       return;
509       
510     case 'B':
511       /* Convert GCC's comparison operators into the complimentary FR30
512          comparison codes.  */
513       switch (GET_CODE (x))
514         {
515         case EQ:  fprintf (file, "ne"); break;
516         case NE:  fprintf (file, "eq"); break;
517         case LT:  fprintf (file, "ge"); break;
518         case LE:  fprintf (file, "gt"); break;
519         case GT:  fprintf (file, "le"); break;
520         case GE:  fprintf (file, "lt"); break;
521         case LTU: fprintf (file, "nc"); break;
522         case LEU: fprintf (file, "hi"); break;
523         case GTU: fprintf (file, "ls"); break;
524         case GEU: fprintf (file, "c"); break;
525         default:
526           output_operand_lossage ("fr30_print_operand: unrecognized %%B code");
527           break;
528         }
529       return;
530
531     case 'A':
532       /* Print a signed byte value as an unsigned value.  */
533       if (GET_CODE (x) != CONST_INT)
534         output_operand_lossage ("fr30_print_operand: invalid operand to %%A code");
535       else
536         {
537           HOST_WIDE_INT val;
538           
539           val = INTVAL (x);
540
541           val &= 0xff;
542
543           fprintf (file, HOST_WIDE_INT_PRINT_DEC, val);
544         }
545       return;
546       
547     case 'x':
548       if (GET_CODE (x) != CONST_INT
549           || INTVAL (x) < 16
550           || INTVAL (x) > 32)
551         output_operand_lossage ("fr30_print_operand: invalid %%x code");
552       else
553         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) - 16);
554       return;
555
556     case 'F':
557       if (GET_CODE (x) != CONST_DOUBLE)
558         output_operand_lossage ("fr30_print_operand: invalid %%F code");
559       else
560         {
561           char str[30];
562
563           real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (x),
564                            sizeof (str), 0, 1);
565           fputs (str, file);
566         }
567       return;
568       
569     case 0:
570       /* Handled below.  */
571       break;
572       
573     default:
574       fprintf (stderr, "unknown code = %x\n", code);
575       output_operand_lossage ("fr30_print_operand: unknown code");
576       return;
577     }
578
579   switch (GET_CODE (x))
580     {
581     case REG:
582       fputs (reg_names [REGNO (x)], file);
583       break;
584
585     case MEM:
586       x0 = XEXP (x,0);
587       
588       switch (GET_CODE (x0))
589         {
590         case REG:
591           gcc_assert ((unsigned) REGNO (x0) < ARRAY_SIZE (reg_names));
592           fprintf (file, "@%s", reg_names [REGNO (x0)]);
593           break;
594
595         case PLUS:
596           if (GET_CODE (XEXP (x0, 0)) != REG
597               || REGNO (XEXP (x0, 0)) < FRAME_POINTER_REGNUM
598               || REGNO (XEXP (x0, 0)) > STACK_POINTER_REGNUM
599               || GET_CODE (XEXP (x0, 1)) != CONST_INT)
600             {
601               fprintf (stderr, "bad INDEXed address:");
602               debug_rtx (x);
603               output_operand_lossage ("fr30_print_operand: unhandled MEM");
604             }
605           else if (REGNO (XEXP (x0, 0)) == FRAME_POINTER_REGNUM)
606             {
607               HOST_WIDE_INT val = INTVAL (XEXP (x0, 1));
608               if (val < -(1 << 9) || val > ((1 << 9) - 4))
609                 {
610                   fprintf (stderr, "frame INDEX out of range:");
611                   debug_rtx (x);
612                   output_operand_lossage ("fr30_print_operand: unhandled MEM");
613                 }
614               fprintf (file, "@(r14, #" HOST_WIDE_INT_PRINT_DEC ")", val);
615             }
616           else
617             {
618               HOST_WIDE_INT val = INTVAL (XEXP (x0, 1));
619               if (val < 0 || val > ((1 << 6) - 4))
620                 {
621                   fprintf (stderr, "stack INDEX out of range:");
622                   debug_rtx (x);
623                   output_operand_lossage ("fr30_print_operand: unhandled MEM");
624                 }
625               fprintf (file, "@(r15, #" HOST_WIDE_INT_PRINT_DEC ")", val);
626             }
627           break;
628           
629         case SYMBOL_REF:
630           output_address (x0);
631           break;
632           
633         default:
634           fprintf (stderr, "bad MEM code = %x\n", GET_CODE (x0));
635           debug_rtx (x);
636           output_operand_lossage ("fr30_print_operand: unhandled MEM");
637           break;
638         }
639       break;
640       
641     case CONST_DOUBLE :
642       /* We handle SFmode constants here as output_addr_const doesn't.  */
643       if (GET_MODE (x) == SFmode)
644         {
645           REAL_VALUE_TYPE d;
646           long l;
647
648           REAL_VALUE_FROM_CONST_DOUBLE (d, x);
649           REAL_VALUE_TO_TARGET_SINGLE (d, l);
650           fprintf (file, "0x%08lx", l);
651           break;
652         }
653
654       /* Fall through.  Let output_addr_const deal with it.  */
655     default:
656       output_addr_const (file, x);
657       break;
658     }
659
660   return;
661 }
662
663 /*}}}*/
664 /*{{{  Function arguments */ 
665
666 /* Return true if we should pass an argument on the stack rather than
667    in registers.  */
668
669 static bool
670 fr30_must_pass_in_stack (enum machine_mode mode, const_tree type)
671 {
672   if (mode == BLKmode)
673     return true;
674   if (type == NULL)
675     return false;
676   return AGGREGATE_TYPE_P (type);
677 }
678
679 /* Compute the number of word sized registers needed to hold a
680    function argument of mode INT_MODE and tree type TYPE.  */
681 int
682 fr30_num_arg_regs (enum machine_mode mode, tree type)
683 {
684   int size;
685
686   if (targetm.calls.must_pass_in_stack (mode, type))
687     return 0;
688
689   if (type && mode == BLKmode)
690     size = int_size_in_bytes (type);
691   else
692     size = GET_MODE_SIZE (mode);
693
694   return (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
695 }
696
697 /* Returns the number of bytes in which *part* of a parameter of machine
698    mode MODE and tree type TYPE (which may be NULL if the type is not known).
699    If the argument fits entirely in the argument registers, or entirely on
700    the stack, then 0 is returned.
701    CUM is the number of argument registers already used by earlier
702    parameters to the function.  */
703
704 static int
705 fr30_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
706                         tree type, bool named)
707 {
708   /* Unnamed arguments, i.e. those that are prototyped as ...
709      are always passed on the stack.
710      Also check here to see if all the argument registers are full.  */
711   if (named == 0 || *cum >= FR30_NUM_ARG_REGS)
712     return 0;
713
714   /* Work out how many argument registers would be needed if this
715      parameter were to be passed entirely in registers.  If there
716      are sufficient argument registers available (or if no registers
717      are needed because the parameter must be passed on the stack)
718      then return zero, as this parameter does not require partial
719      register, partial stack stack space.  */
720   if (*cum + fr30_num_arg_regs (mode, type) <= FR30_NUM_ARG_REGS)
721     return 0;
722   
723   return (FR30_NUM_ARG_REGS - *cum) * UNITS_PER_WORD;
724 }
725
726 /*}}}*/
727 /*{{{  Operand predicates */ 
728
729 #ifndef Mmode
730 #define Mmode enum machine_mode
731 #endif
732
733 /* Returns true iff all the registers in the operands array
734    are in descending or ascending order.  */
735 int
736 fr30_check_multiple_regs (rtx *operands, int num_operands, int descending)
737 {
738   if (descending)
739     {
740       unsigned int prev_regno = 0;
741       
742       while (num_operands --)
743         {
744           if (GET_CODE (operands [num_operands]) != REG)
745             return 0;
746           
747           if (REGNO (operands [num_operands]) < prev_regno)
748             return 0;
749           
750           prev_regno = REGNO (operands [num_operands]);
751         }
752     }
753   else
754     {
755       unsigned int prev_regno = CONDITION_CODE_REGNUM;
756       
757       while (num_operands --)
758         {
759           if (GET_CODE (operands [num_operands]) != REG)
760             return 0;
761           
762           if (REGNO (operands [num_operands]) > prev_regno)
763             return 0;
764           
765           prev_regno = REGNO (operands [num_operands]);
766         }
767     }
768
769   return 1;
770 }
771
772 int
773 fr30_const_double_is_zero (rtx operand)
774 {
775   REAL_VALUE_TYPE d;
776
777   if (operand == NULL || GET_CODE (operand) != CONST_DOUBLE)
778     return 0;
779
780   REAL_VALUE_FROM_CONST_DOUBLE (d, operand);
781
782   return REAL_VALUES_EQUAL (d, dconst0);
783 }
784
785 /*}}}*/
786 /*{{{  Instruction Output Routines  */
787
788 /* Output a double word move.
789    It must be REG<-REG, REG<-MEM, MEM<-REG or REG<-CONST.
790    On the FR30 we are constrained by the fact that it does not
791    support offsetable addresses, and so we have to load the
792    address of the secnd word into the second destination register
793    before we can use it.  */
794
795 rtx
796 fr30_move_double (rtx * operands)
797 {
798   rtx src  = operands[1];
799   rtx dest = operands[0];
800   enum rtx_code src_code = GET_CODE (src);
801   enum rtx_code dest_code = GET_CODE (dest);
802   enum machine_mode mode = GET_MODE (dest);
803   rtx val;
804
805   start_sequence ();
806
807   if (dest_code == REG)
808     {
809       if (src_code == REG)
810         {
811           int reverse = (REGNO (dest) == REGNO (src) + 1);
812           
813           /* We normally copy the low-numbered register first.  However, if
814              the first register of operand 0 is the same as the second register
815              of operand 1, we must copy in the opposite order.  */
816           emit_insn (gen_rtx_SET (VOIDmode,
817                                   operand_subword (dest, reverse, TRUE, mode),
818                                   operand_subword (src,  reverse, TRUE, mode)));
819           
820           emit_insn (gen_rtx_SET (VOIDmode,
821                               operand_subword (dest, !reverse, TRUE, mode),
822                               operand_subword (src,  !reverse, TRUE, mode)));
823         }
824       else if (src_code == MEM)
825         {
826           rtx addr = XEXP (src, 0);
827           int dregno = REGNO (dest);
828           rtx dest0 = operand_subword (dest, 0, TRUE, mode);;
829           rtx dest1 = operand_subword (dest, 1, TRUE, mode);;
830           rtx new_mem;
831           
832           gcc_assert (GET_CODE (addr) == REG);
833           
834           /* Copy the address before clobbering it.  See PR 34174.  */
835           emit_insn (gen_rtx_SET (SImode, dest1, addr));
836           emit_insn (gen_rtx_SET (VOIDmode, dest0,
837                                   adjust_address (src, SImode, 0)));
838           emit_insn (gen_rtx_SET (SImode, dest1,
839                                   plus_constant (dest1, UNITS_PER_WORD)));
840
841           new_mem = gen_rtx_MEM (SImode, dest1);
842           MEM_COPY_ATTRIBUTES (new_mem, src);
843               
844           emit_insn (gen_rtx_SET (VOIDmode, dest1, new_mem));
845         }
846       else if (src_code == CONST_INT || src_code == CONST_DOUBLE)
847         {
848           rtx words[2];
849           split_double (src, &words[0], &words[1]);
850           emit_insn (gen_rtx_SET (VOIDmode,
851                                   operand_subword (dest, 0, TRUE, mode),
852                                   words[0]));
853       
854           emit_insn (gen_rtx_SET (VOIDmode,
855                                   operand_subword (dest, 1, TRUE, mode),
856                                   words[1]));
857         }
858     }
859   else if (src_code == REG && dest_code == MEM)
860     {
861       rtx addr = XEXP (dest, 0);
862       rtx src0;
863       rtx src1;
864
865       gcc_assert (GET_CODE (addr) == REG);
866
867       src0 = operand_subword (src, 0, TRUE, mode);
868       src1 = operand_subword (src, 1, TRUE, mode);
869
870       emit_move_insn (adjust_address (dest, SImode, 0), src0);
871
872       if (REGNO (addr) == STACK_POINTER_REGNUM
873           || REGNO (addr) == FRAME_POINTER_REGNUM)
874         emit_insn (gen_rtx_SET (VOIDmode,
875                                 adjust_address (dest, SImode, UNITS_PER_WORD),
876                                 src1));
877       else
878         {
879           rtx new_mem;
880           rtx scratch_reg_r0 = gen_rtx_REG (SImode, 0);
881
882           /* We need a scratch register to hold the value of 'address + 4'.
883              We use r0 for this purpose. It is used for example for long
884              jumps and is already marked to not be used by normal register
885              allocation.  */
886           emit_insn (gen_movsi_internal (scratch_reg_r0, addr));
887           emit_insn (gen_addsi_small_int (scratch_reg_r0, scratch_reg_r0,
888                                           GEN_INT (UNITS_PER_WORD)));
889           new_mem = gen_rtx_MEM (SImode, scratch_reg_r0);
890           MEM_COPY_ATTRIBUTES (new_mem, dest);
891           emit_move_insn (new_mem, src1);
892           emit_insn (gen_blockage ());
893         }
894     }
895   else
896     /* This should have been prevented by the constraints on movdi_insn.  */
897     gcc_unreachable ();
898
899   val = get_insns ();
900   end_sequence ();
901
902   return val;
903 }
904
905 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
906
907 bool
908 fr30_frame_pointer_required (void)
909 {
910   return (flag_omit_frame_pointer == 0 || crtl->args.pretend_args_size > 0);
911 }
912
913 /*}}}*/
914 /* Local Variables: */
915 /* folded-file: t   */
916 /* End:             */