OSDN Git Service

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