Daily bump.
[official-gcc.git] / gcc / config / moxie / moxie.c
blobb646a432bb2fa4173053c07d40160c4b8eb17e42
1 /* Target Code for moxie
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 Contributed by Anthony Green.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "reload.h"
36 #include "diagnostic-core.h"
37 #include "obstack.h"
38 #include "tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "calls.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "except.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "tm_p.h"
50 #include "langhooks.h"
51 #include "df.h"
53 #define LOSE_AND_RETURN(msgid, x) \
54 do \
55 { \
56 moxie_operand_lossage (msgid, x); \
57 return; \
58 } while (0)
60 /* Worker function for TARGET_RETURN_IN_MEMORY. */
62 static bool
63 moxie_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
65 const HOST_WIDE_INT size = int_size_in_bytes (type);
66 return (size == -1 || size > 2 * UNITS_PER_WORD);
69 /* Define how to find the value returned by a function.
70 VALTYPE is the data type of the value (as a tree).
71 If the precise function being called is known, FUNC is its
72 FUNCTION_DECL; otherwise, FUNC is 0.
74 We always return values in register $r0 for moxie. */
76 static rtx
77 moxie_function_value (const_tree valtype,
78 const_tree fntype_or_decl ATTRIBUTE_UNUSED,
79 bool outgoing ATTRIBUTE_UNUSED)
81 return gen_rtx_REG (TYPE_MODE (valtype), MOXIE_R0);
84 /* Define how to find the value returned by a library function.
86 We always return values in register $r0 for moxie. */
88 static rtx
89 moxie_libcall_value (enum machine_mode mode,
90 const_rtx fun ATTRIBUTE_UNUSED)
92 return gen_rtx_REG (mode, MOXIE_R0);
95 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
97 We always return values in register $r0 for moxie. */
99 static bool
100 moxie_function_value_regno_p (const unsigned int regno)
102 return (regno == MOXIE_R0);
105 /* Emit an error message when we're in an asm, and a fatal error for
106 "normal" insns. Formatted output isn't easily implemented, since we
107 use output_operand_lossage to output the actual message and handle the
108 categorization of the error. */
110 static void
111 moxie_operand_lossage (const char *msgid, rtx op)
113 debug_rtx (op);
114 output_operand_lossage ("%s", msgid);
117 /* The PRINT_OPERAND_ADDRESS worker. */
119 void
120 moxie_print_operand_address (FILE *file, rtx x)
122 switch (GET_CODE (x))
124 case REG:
125 fprintf (file, "(%s)", reg_names[REGNO (x)]);
126 break;
128 case PLUS:
129 switch (GET_CODE (XEXP (x, 1)))
131 case CONST_INT:
132 fprintf (file, "%ld(%s)",
133 INTVAL(XEXP (x, 1)), reg_names[REGNO (XEXP (x, 0))]);
134 break;
135 case SYMBOL_REF:
136 output_addr_const (file, XEXP (x, 1));
137 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
138 break;
139 case CONST:
141 rtx plus = XEXP (XEXP (x, 1), 0);
142 if (GET_CODE (XEXP (plus, 0)) == SYMBOL_REF
143 && CONST_INT_P (XEXP (plus, 1)))
145 output_addr_const(file, XEXP (plus, 0));
146 fprintf (file,"+%ld(%s)", INTVAL (XEXP (plus, 1)),
147 reg_names[REGNO (XEXP (x, 0))]);
149 else
150 abort();
152 break;
153 default:
154 abort();
156 break;
158 default:
159 output_addr_const (file, x);
160 break;
164 /* The PRINT_OPERAND worker. */
166 void
167 moxie_print_operand (FILE *file, rtx x, int code)
169 rtx operand = x;
171 /* New code entries should just be added to the switch below. If
172 handling is finished, just return. If handling was just a
173 modification of the operand, the modified operand should be put in
174 "operand", and then do a break to let default handling
175 (zero-modifier) output the operand. */
177 switch (code)
179 case 0:
180 /* No code, print as usual. */
181 break;
183 default:
184 LOSE_AND_RETURN ("invalid operand modifier letter", x);
187 /* Print an operand as without a modifier letter. */
188 switch (GET_CODE (operand))
190 case REG:
191 if (REGNO (operand) > MOXIE_R13)
192 internal_error ("internal error: bad register: %d", REGNO (operand));
193 fprintf (file, "%s", reg_names[REGNO (operand)]);
194 return;
196 case MEM:
197 output_address (XEXP (operand, 0));
198 return;
200 default:
201 /* No need to handle all strange variants, let output_addr_const
202 do it for us. */
203 if (CONSTANT_P (operand))
205 output_addr_const (file, operand);
206 return;
209 LOSE_AND_RETURN ("unexpected operand", x);
213 /* Per-function machine data. */
214 struct GTY(()) machine_function
216 /* Number of bytes saved on the stack for callee saved registers. */
217 int callee_saved_reg_size;
219 /* Number of bytes saved on the stack for local variables. */
220 int local_vars_size;
222 /* The sum of 2 sizes: locals vars and padding byte for saving the
223 * registers. Used in expand_prologue () and expand_epilogue(). */
224 int size_for_adjusting_sp;
227 /* Zero initialization is OK for all current fields. */
229 static struct machine_function *
230 moxie_init_machine_status (void)
232 return ggc_alloc_cleared_machine_function ();
236 /* The TARGET_OPTION_OVERRIDE worker.
237 All this curently does is set init_machine_status. */
238 static void
239 moxie_option_override (void)
241 /* Set the per-function-data initializer. */
242 init_machine_status = moxie_init_machine_status;
245 /* Compute the size of the local area and the size to be adjusted by the
246 * prologue and epilogue. */
248 static void
249 moxie_compute_frame (void)
251 /* For aligning the local variables. */
252 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
253 int padding_locals;
254 int regno;
256 /* Padding needed for each element of the frame. */
257 cfun->machine->local_vars_size = get_frame_size ();
259 /* Align to the stack alignment. */
260 padding_locals = cfun->machine->local_vars_size % stack_alignment;
261 if (padding_locals)
262 padding_locals = stack_alignment - padding_locals;
264 cfun->machine->local_vars_size += padding_locals;
266 cfun->machine->callee_saved_reg_size = 0;
268 /* Save callee-saved registers. */
269 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
270 if (df_regs_ever_live_p (regno) && (! call_used_regs[regno]))
271 cfun->machine->callee_saved_reg_size += 4;
273 cfun->machine->size_for_adjusting_sp =
274 crtl->args.pretend_args_size
275 + cfun->machine->local_vars_size
276 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
279 void
280 moxie_expand_prologue (void)
282 int regno;
283 rtx insn;
285 moxie_compute_frame ();
287 if (flag_stack_usage_info)
288 current_function_static_stack_size = cfun->machine->size_for_adjusting_sp;
290 /* Save callee-saved registers. */
291 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
293 if (!fixed_regs[regno] && df_regs_ever_live_p (regno) && !call_used_regs[regno])
295 insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
296 RTX_FRAME_RELATED_P (insn) = 1;
300 if (cfun->machine->size_for_adjusting_sp > 0)
302 int i = cfun->machine->size_for_adjusting_sp;
303 while ((i >= 255) && (i <= 510))
305 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
306 stack_pointer_rtx,
307 GEN_INT (255)));
308 RTX_FRAME_RELATED_P (insn) = 1;
309 i -= 255;
311 if (i <= 255)
313 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
314 stack_pointer_rtx,
315 GEN_INT (i)));
316 RTX_FRAME_RELATED_P (insn) = 1;
318 else
320 rtx reg = gen_rtx_REG (SImode, MOXIE_R12);
321 insn = emit_move_insn (reg, GEN_INT (i));
322 RTX_FRAME_RELATED_P (insn) = 1;
323 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
324 stack_pointer_rtx,
325 reg));
326 RTX_FRAME_RELATED_P (insn) = 1;
331 void
332 moxie_expand_epilogue (void)
334 int regno;
335 rtx reg;
337 if (cfun->machine->callee_saved_reg_size != 0)
339 reg = gen_rtx_REG (Pmode, MOXIE_R12);
340 if (cfun->machine->callee_saved_reg_size <= 255)
342 emit_move_insn (reg, hard_frame_pointer_rtx);
343 emit_insn (gen_subsi3
344 (reg, reg,
345 GEN_INT (cfun->machine->callee_saved_reg_size)));
347 else
349 emit_move_insn (reg,
350 GEN_INT (-cfun->machine->callee_saved_reg_size));
351 emit_insn (gen_addsi3 (reg, reg, hard_frame_pointer_rtx));
353 for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
354 if (!fixed_regs[regno] && !call_used_regs[regno]
355 && df_regs_ever_live_p (regno))
357 rtx preg = gen_rtx_REG (Pmode, regno);
358 emit_insn (gen_movsi_pop (reg, preg));
362 emit_jump_insn (gen_returner ());
365 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
368 moxie_initial_elimination_offset (int from, int to)
370 int ret;
372 if ((from) == FRAME_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
374 /* Compute this since we need to use cfun->machine->local_vars_size. */
375 moxie_compute_frame ();
376 ret = -cfun->machine->callee_saved_reg_size;
378 else if ((from) == ARG_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
379 ret = 0x00;
380 else
381 abort ();
383 return ret;
386 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
388 static void
389 moxie_setup_incoming_varargs (cumulative_args_t cum_v,
390 enum machine_mode mode ATTRIBUTE_UNUSED,
391 tree type ATTRIBUTE_UNUSED,
392 int *pretend_size, int no_rtl)
394 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
395 int regno;
396 int regs = 8 - *cum;
398 *pretend_size = regs < 0 ? 0 : GET_MODE_SIZE (SImode) * regs;
400 if (no_rtl)
401 return;
403 for (regno = *cum; regno < 8; regno++)
405 rtx reg = gen_rtx_REG (SImode, regno);
406 rtx slot = gen_rtx_PLUS (Pmode,
407 gen_rtx_REG (SImode, ARG_POINTER_REGNUM),
408 GEN_INT (UNITS_PER_WORD * (3 + (regno-2))));
410 emit_move_insn (gen_rtx_MEM (SImode, slot), reg);
415 /* Return the fixed registers used for condition codes. */
417 static bool
418 moxie_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
420 *p1 = CC_REG;
421 *p2 = INVALID_REGNUM;
422 return true;
425 /* Return the next register to be used to hold a function argument or
426 NULL_RTX if there's no more space. */
428 static rtx
429 moxie_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
430 const_tree type ATTRIBUTE_UNUSED,
431 bool named ATTRIBUTE_UNUSED)
433 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
435 if (*cum < 8)
436 return gen_rtx_REG (mode, *cum);
437 else
438 return NULL_RTX;
441 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
442 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
443 : (unsigned) int_size_in_bytes (TYPE))
445 static void
446 moxie_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
447 const_tree type, bool named ATTRIBUTE_UNUSED)
449 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
451 *cum = (*cum < MOXIE_R6
452 ? *cum + ((3 + MOXIE_FUNCTION_ARG_SIZE (mode, type)) / 4)
453 : *cum);
456 /* Return non-zero if the function argument described by TYPE is to be
457 passed by reference. */
459 static bool
460 moxie_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
461 enum machine_mode mode, const_tree type,
462 bool named ATTRIBUTE_UNUSED)
464 unsigned HOST_WIDE_INT size;
466 if (type)
468 if (AGGREGATE_TYPE_P (type))
469 return true;
470 size = int_size_in_bytes (type);
472 else
473 size = GET_MODE_SIZE (mode);
475 return size > 4*6;
478 /* Some function arguments will only partially fit in the registers
479 that hold arguments. Given a new arg, return the number of bytes
480 that fit in argument passing registers. */
482 static int
483 moxie_arg_partial_bytes (cumulative_args_t cum_v,
484 enum machine_mode mode,
485 tree type, bool named)
487 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
488 int bytes_left, size;
490 if (*cum >= 8)
491 return 0;
493 if (moxie_pass_by_reference (cum_v, mode, type, named))
494 size = 4;
495 else if (type)
497 if (AGGREGATE_TYPE_P (type))
498 return 0;
499 size = int_size_in_bytes (type);
501 else
502 size = GET_MODE_SIZE (mode);
504 bytes_left = (4 * 6) - ((*cum - 2) * 4);
506 if (size > bytes_left)
507 return bytes_left;
508 else
509 return 0;
512 /* Worker function for TARGET_STATIC_CHAIN. */
514 static rtx
515 moxie_static_chain (const_tree fndecl, bool incoming_p)
517 rtx addr, mem;
519 if (!DECL_STATIC_CHAIN (fndecl))
520 return NULL;
522 if (incoming_p)
523 addr = plus_constant (Pmode, arg_pointer_rtx, 2 * UNITS_PER_WORD);
524 else
525 addr = plus_constant (Pmode, stack_pointer_rtx, -UNITS_PER_WORD);
527 mem = gen_rtx_MEM (Pmode, addr);
528 MEM_NOTRAP_P (mem) = 1;
530 return mem;
533 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
535 static void
536 moxie_asm_trampoline_template (FILE *f)
538 fprintf (f, "\tpush $sp, $r0\n");
539 fprintf (f, "\tldi.l $r0, 0x0\n");
540 fprintf (f, "\tsto.l 0x8($fp), $r0\n");
541 fprintf (f, "\tpop $sp, $r0\n");
542 fprintf (f, "\tnop\n");
543 fprintf (f, "\tjmpa 0x0\n");
546 /* Worker function for TARGET_TRAMPOLINE_INIT. */
548 static void
549 moxie_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
551 rtx mem, fnaddr = XEXP (DECL_RTL (fndecl), 0);
553 emit_block_move (m_tramp, assemble_trampoline_template (),
554 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
556 mem = adjust_address (m_tramp, SImode, 4);
557 emit_move_insn (mem, chain_value);
558 mem = adjust_address (m_tramp, SImode, 20);
559 emit_move_insn (mem, fnaddr);
562 /* The Global `targetm' Variable. */
564 /* Initialize the GCC target structure. */
566 #undef TARGET_PROMOTE_PROTOTYPES
567 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
569 #undef TARGET_RETURN_IN_MEMORY
570 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
571 #undef TARGET_MUST_PASS_IN_STACK
572 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
573 #undef TARGET_PASS_BY_REFERENCE
574 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
575 #undef TARGET_ARG_PARTIAL_BYTES
576 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
577 #undef TARGET_FUNCTION_ARG
578 #define TARGET_FUNCTION_ARG moxie_function_arg
579 #undef TARGET_FUNCTION_ARG_ADVANCE
580 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
583 #undef TARGET_SETUP_INCOMING_VARARGS
584 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
586 #undef TARGET_FIXED_CONDITION_CODE_REGS
587 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
589 /* Define this to return an RTX representing the place where a
590 function returns or receives a value of data type RET_TYPE, a tree
591 node node representing a data type. */
592 #undef TARGET_FUNCTION_VALUE
593 #define TARGET_FUNCTION_VALUE moxie_function_value
594 #undef TARGET_LIBCALL_VALUE
595 #define TARGET_LIBCALL_VALUE moxie_libcall_value
596 #undef TARGET_FUNCTION_VALUE_REGNO_P
597 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
599 #undef TARGET_FRAME_POINTER_REQUIRED
600 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
602 #undef TARGET_STATIC_CHAIN
603 #define TARGET_STATIC_CHAIN moxie_static_chain
604 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
605 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
606 #undef TARGET_TRAMPOLINE_INIT
607 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
609 #undef TARGET_OPTION_OVERRIDE
610 #define TARGET_OPTION_OVERRIDE moxie_option_override
612 struct gcc_target targetm = TARGET_INITIALIZER;
614 #include "gt-moxie.h"