1 /* Target Code for moxie
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation
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/>. */
23 #include "coretypes.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
32 #include "insn-attr.h"
36 #include "diagnostic-core.h"
46 #include "target-def.h"
48 #include "langhooks.h"
51 #define LOSE_AND_RETURN(msgid, x) \
54 moxie_operand_lossage (msgid, x); \
58 /* Worker function for TARGET_RETURN_IN_MEMORY. */
61 moxie_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
63 const HOST_WIDE_INT size = int_size_in_bytes (type);
64 return (size == -1 || size > 2 * UNITS_PER_WORD);
67 /* Define how to find the value returned by a function.
68 VALTYPE is the data type of the value (as a tree).
69 If the precise function being called is known, FUNC is its
70 FUNCTION_DECL; otherwise, FUNC is 0.
72 We always return values in register $r0 for moxie. */
75 moxie_function_value (const_tree valtype,
76 const_tree fntype_or_decl ATTRIBUTE_UNUSED,
77 bool outgoing ATTRIBUTE_UNUSED)
79 return gen_rtx_REG (TYPE_MODE (valtype), MOXIE_R0);
82 /* Define how to find the value returned by a library function.
84 We always return values in register $r0 for moxie. */
87 moxie_libcall_value (enum machine_mode mode,
88 const_rtx fun ATTRIBUTE_UNUSED)
90 return gen_rtx_REG (mode, MOXIE_R0);
93 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
95 We always return values in register $r0 for moxie. */
98 moxie_function_value_regno_p (const unsigned int regno)
100 return (regno == MOXIE_R0);
103 /* Emit an error message when we're in an asm, and a fatal error for
104 "normal" insns. Formatted output isn't easily implemented, since we
105 use output_operand_lossage to output the actual message and handle the
106 categorization of the error. */
109 moxie_operand_lossage (const char *msgid, rtx op)
112 output_operand_lossage ("%s", msgid);
115 /* The PRINT_OPERAND_ADDRESS worker. */
118 moxie_print_operand_address (FILE *file, rtx x)
120 switch (GET_CODE (x))
123 fprintf (file, "(%s)", reg_names[REGNO (x)]);
127 switch (GET_CODE (XEXP (x, 1)))
130 fprintf (file, "%ld(%s)",
131 INTVAL(XEXP (x, 1)), reg_names[REGNO (XEXP (x, 0))]);
134 output_addr_const (file, XEXP (x, 1));
135 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
139 rtx plus = XEXP (XEXP (x, 1), 0);
140 if (GET_CODE (XEXP (plus, 0)) == SYMBOL_REF
141 && CONST_INT_P (XEXP (plus, 1)))
143 output_addr_const(file, XEXP (plus, 0));
144 fprintf (file,"+%ld(%s)", INTVAL (XEXP (plus, 1)),
145 reg_names[REGNO (XEXP (x, 0))]);
157 output_addr_const (file, x);
162 /* The PRINT_OPERAND worker. */
165 moxie_print_operand (FILE *file, rtx x, int code)
169 /* New code entries should just be added to the switch below. If
170 handling is finished, just return. If handling was just a
171 modification of the operand, the modified operand should be put in
172 "operand", and then do a break to let default handling
173 (zero-modifier) output the operand. */
178 /* No code, print as usual. */
182 LOSE_AND_RETURN ("invalid operand modifier letter", x);
185 /* Print an operand as without a modifier letter. */
186 switch (GET_CODE (operand))
189 if (REGNO (operand) > MOXIE_R13)
190 internal_error ("internal error: bad register: %d", REGNO (operand));
191 fprintf (file, "%s", reg_names[REGNO (operand)]);
195 output_address (XEXP (operand, 0));
199 /* No need to handle all strange variants, let output_addr_const
201 if (CONSTANT_P (operand))
203 output_addr_const (file, operand);
207 LOSE_AND_RETURN ("unexpected operand", x);
211 /* Per-function machine data. */
212 struct GTY(()) machine_function
214 /* Number of bytes saved on the stack for callee saved registers. */
215 int callee_saved_reg_size;
217 /* Number of bytes saved on the stack for local variables. */
220 /* The sum of 2 sizes: locals vars and padding byte for saving the
221 * registers. Used in expand_prologue () and expand_epilogue(). */
222 int size_for_adjusting_sp;
225 /* Zero initialization is OK for all current fields. */
227 static struct machine_function *
228 moxie_init_machine_status (void)
230 return ggc_alloc_cleared_machine_function ();
234 /* The TARGET_OPTION_OVERRIDE worker.
235 All this curently does is set init_machine_status. */
237 moxie_option_override (void)
239 /* Set the per-function-data initializer. */
240 init_machine_status = moxie_init_machine_status;
243 /* Compute the size of the local area and the size to be adjusted by the
244 * prologue and epilogue. */
247 moxie_compute_frame (void)
249 /* For aligning the local variables. */
250 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
254 /* Padding needed for each element of the frame. */
255 cfun->machine->local_vars_size = get_frame_size ();
257 /* Align to the stack alignment. */
258 padding_locals = cfun->machine->local_vars_size % stack_alignment;
260 padding_locals = stack_alignment - padding_locals;
262 cfun->machine->local_vars_size += padding_locals;
264 cfun->machine->callee_saved_reg_size = 0;
266 /* Save callee-saved registers. */
267 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
268 if (df_regs_ever_live_p (regno) && (! call_used_regs[regno]))
269 cfun->machine->callee_saved_reg_size += 4;
271 cfun->machine->size_for_adjusting_sp =
272 crtl->args.pretend_args_size
273 + cfun->machine->local_vars_size
274 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
278 moxie_expand_prologue (void)
283 moxie_compute_frame ();
285 /* Save callee-saved registers. */
286 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
288 if (!fixed_regs[regno] && df_regs_ever_live_p (regno) && !call_used_regs[regno])
290 insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
291 RTX_FRAME_RELATED_P (insn) = 1;
295 if (cfun->machine->size_for_adjusting_sp > 0)
297 int i = cfun->machine->size_for_adjusting_sp;
300 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
303 RTX_FRAME_RELATED_P (insn) = 1;
308 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
311 RTX_FRAME_RELATED_P (insn) = 1;
317 moxie_expand_epilogue (void)
320 rtx insn, reg, cfa_restores = NULL;
322 if (cfun->machine->callee_saved_reg_size != 0)
324 reg = gen_rtx_REG (Pmode, MOXIE_R5);
325 if (cfun->machine->callee_saved_reg_size <= 255)
327 emit_move_insn (reg, hard_frame_pointer_rtx);
328 emit_insn (gen_subsi3
330 GEN_INT (cfun->machine->callee_saved_reg_size)));
335 GEN_INT (-cfun->machine->callee_saved_reg_size));
336 emit_insn (gen_addsi3 (reg, reg, hard_frame_pointer_rtx));
338 for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
339 if (!fixed_regs[regno] && !call_used_regs[regno]
340 && df_regs_ever_live_p (regno))
342 rtx preg = gen_rtx_REG (Pmode, regno);
343 insn = emit_insn (gen_movsi_pop (reg, preg));
347 emit_jump_insn (gen_returner ());
350 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
353 moxie_initial_elimination_offset (int from, int to)
357 if ((from) == FRAME_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
359 /* Compute this since we need to use cfun->machine->local_vars_size. */
360 moxie_compute_frame ();
361 ret = -cfun->machine->callee_saved_reg_size;
363 else if ((from) == ARG_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
371 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
374 moxie_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
375 enum machine_mode mode ATTRIBUTE_UNUSED,
376 tree type ATTRIBUTE_UNUSED,
377 int *pretend_size, int no_rtl)
382 *pretend_size = regs < 0 ? 0 : GET_MODE_SIZE (SImode) * regs;
387 for (regno = *cum; regno < 8; regno++)
389 rtx reg = gen_rtx_REG (SImode, regno);
390 rtx slot = gen_rtx_PLUS (Pmode,
391 gen_rtx_REG (SImode, ARG_POINTER_REGNUM),
392 GEN_INT (UNITS_PER_WORD * (3 + (regno-2))));
394 emit_move_insn (gen_rtx_MEM (SImode, slot), reg);
399 /* Return the fixed registers used for condition codes. */
402 moxie_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
405 *p2 = INVALID_REGNUM;
409 /* Return the next register to be used to hold a function argument or
410 NULL_RTX if there's no more space. */
413 moxie_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
414 const_tree type ATTRIBUTE_UNUSED,
415 bool named ATTRIBUTE_UNUSED)
418 return gen_rtx_REG (mode, *cum);
423 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
424 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
425 : (unsigned) int_size_in_bytes (TYPE))
428 moxie_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
429 const_tree type, bool named ATTRIBUTE_UNUSED)
431 *cum = (*cum < MOXIE_R6
432 ? *cum + ((3 + MOXIE_FUNCTION_ARG_SIZE (mode, type)) / 4)
436 /* Return non-zero if the function argument described by TYPE is to be
437 passed by reference. */
440 moxie_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
441 enum machine_mode mode, const_tree type,
442 bool named ATTRIBUTE_UNUSED)
444 unsigned HOST_WIDE_INT size;
448 if (AGGREGATE_TYPE_P (type))
450 size = int_size_in_bytes (type);
453 size = GET_MODE_SIZE (mode);
458 /* Some function arguments will only partially fit in the registers
459 that hold arguments. Given a new arg, return the number of bytes
460 that fit in argument passing registers. */
463 moxie_arg_partial_bytes (CUMULATIVE_ARGS *cum,
464 enum machine_mode mode,
465 tree type, bool named)
467 int bytes_left, size;
472 if (moxie_pass_by_reference (cum, mode, type, named))
476 if (AGGREGATE_TYPE_P (type))
478 size = int_size_in_bytes (type);
481 size = GET_MODE_SIZE (mode);
483 bytes_left = (4 * 6) - ((*cum - 2) * 4);
485 if (size > bytes_left)
491 /* Worker function for TARGET_STATIC_CHAIN. */
494 moxie_static_chain (const_tree fndecl, bool incoming_p)
498 if (!DECL_STATIC_CHAIN (fndecl))
502 addr = plus_constant (arg_pointer_rtx, 2 * UNITS_PER_WORD);
504 addr = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
506 mem = gen_rtx_MEM (Pmode, addr);
507 MEM_NOTRAP_P (mem) = 1;
512 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
515 moxie_asm_trampoline_template (FILE *f)
517 fprintf (f, "\tpush $sp, $r0\n");
518 fprintf (f, "\tldi.l $r0, 0x0\n");
519 fprintf (f, "\tsto.l 0x8($fp), $r0\n");
520 fprintf (f, "\tpop $sp, $r0\n");
521 fprintf (f, "\tnop\n");
522 fprintf (f, "\tjmpa 0x0\n");
525 /* Worker function for TARGET_TRAMPOLINE_INIT. */
528 moxie_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
530 rtx mem, fnaddr = XEXP (DECL_RTL (fndecl), 0);
532 emit_block_move (m_tramp, assemble_trampoline_template (),
533 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
535 mem = adjust_address (m_tramp, SImode, 4);
536 emit_move_insn (mem, chain_value);
537 mem = adjust_address (m_tramp, SImode, 20);
538 emit_move_insn (mem, fnaddr);
541 /* The Global `targetm' Variable. */
543 /* Initialize the GCC target structure. */
545 #undef TARGET_PROMOTE_PROTOTYPES
546 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
548 #undef TARGET_RETURN_IN_MEMORY
549 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
550 #undef TARGET_MUST_PASS_IN_STACK
551 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
552 #undef TARGET_PASS_BY_REFERENCE
553 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
554 #undef TARGET_ARG_PARTIAL_BYTES
555 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
556 #undef TARGET_FUNCTION_ARG
557 #define TARGET_FUNCTION_ARG moxie_function_arg
558 #undef TARGET_FUNCTION_ARG_ADVANCE
559 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
562 #undef TARGET_SETUP_INCOMING_VARARGS
563 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
565 #undef TARGET_FIXED_CONDITION_CODE_REGS
566 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
568 /* Define this to return an RTX representing the place where a
569 function returns or receives a value of data type RET_TYPE, a tree
570 node node representing a data type. */
571 #undef TARGET_FUNCTION_VALUE
572 #define TARGET_FUNCTION_VALUE moxie_function_value
573 #undef TARGET_LIBCALL_VALUE
574 #define TARGET_LIBCALL_VALUE moxie_libcall_value
575 #undef TARGET_FUNCTION_VALUE_REGNO_P
576 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
578 #undef TARGET_FRAME_POINTER_REQUIRED
579 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
581 #undef TARGET_STATIC_CHAIN
582 #define TARGET_STATIC_CHAIN moxie_static_chain
583 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
584 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
585 #undef TARGET_TRAMPOLINE_INIT
586 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
588 #undef TARGET_OPTION_OVERRIDE
589 #define TARGET_OPTION_OVERRIDE moxie_option_override
591 struct gcc_target targetm = TARGET_INITIALIZER;
593 #include "gt-moxie.h"