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