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