1 /* Target Code for moxie
2 Copyright (C) 2008-2015 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/>. */
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"
45 #include "stor-layout.h"
50 #include "statistics.h"
57 #include "insn-codes.h"
62 #include "target-def.h"
64 #include "langhooks.h"
65 #include "dominance.h"
71 #include "cfgcleanup.h"
73 #include "basic-block.h"
77 #define LOSE_AND_RETURN(msgid, x) \
80 moxie_operand_lossage (msgid, x); \
84 /* Worker function for TARGET_RETURN_IN_MEMORY. */
87 moxie_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
89 const HOST_WIDE_INT size = int_size_in_bytes (type);
90 return (size == -1 || size > 2 * UNITS_PER_WORD);
93 /* Define how to find the value returned by a function.
94 VALTYPE is the data type of the value (as a tree).
95 If the precise function being called is known, FUNC is its
96 FUNCTION_DECL; otherwise, FUNC is 0.
98 We always return values in register $r0 for moxie. */
101 moxie_function_value (const_tree valtype,
102 const_tree fntype_or_decl ATTRIBUTE_UNUSED,
103 bool outgoing ATTRIBUTE_UNUSED)
105 return gen_rtx_REG (TYPE_MODE (valtype), MOXIE_R0);
108 /* Define how to find the value returned by a library function.
110 We always return values in register $r0 for moxie. */
113 moxie_libcall_value (machine_mode mode,
114 const_rtx fun ATTRIBUTE_UNUSED)
116 return gen_rtx_REG (mode, MOXIE_R0);
119 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
121 We always return values in register $r0 for moxie. */
124 moxie_function_value_regno_p (const unsigned int regno)
126 return (regno == MOXIE_R0);
129 /* Emit an error message when we're in an asm, and a fatal error for
130 "normal" insns. Formatted output isn't easily implemented, since we
131 use output_operand_lossage to output the actual message and handle the
132 categorization of the error. */
135 moxie_operand_lossage (const char *msgid, rtx op)
138 output_operand_lossage ("%s", msgid);
141 /* The PRINT_OPERAND_ADDRESS worker. */
144 moxie_print_operand_address (FILE *file, rtx x)
146 switch (GET_CODE (x))
149 fprintf (file, "(%s)", reg_names[REGNO (x)]);
153 switch (GET_CODE (XEXP (x, 1)))
156 fprintf (file, "%ld(%s)",
157 INTVAL(XEXP (x, 1)), reg_names[REGNO (XEXP (x, 0))]);
160 output_addr_const (file, XEXP (x, 1));
161 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
165 rtx plus = XEXP (XEXP (x, 1), 0);
166 if (GET_CODE (XEXP (plus, 0)) == SYMBOL_REF
167 && CONST_INT_P (XEXP (plus, 1)))
169 output_addr_const(file, XEXP (plus, 0));
170 fprintf (file,"+%ld(%s)", INTVAL (XEXP (plus, 1)),
171 reg_names[REGNO (XEXP (x, 0))]);
183 output_addr_const (file, x);
188 /* The PRINT_OPERAND worker. */
191 moxie_print_operand (FILE *file, rtx x, int code)
195 /* New code entries should just be added to the switch below. If
196 handling is finished, just return. If handling was just a
197 modification of the operand, the modified operand should be put in
198 "operand", and then do a break to let default handling
199 (zero-modifier) output the operand. */
204 /* No code, print as usual. */
208 LOSE_AND_RETURN ("invalid operand modifier letter", x);
211 /* Print an operand as without a modifier letter. */
212 switch (GET_CODE (operand))
215 if (REGNO (operand) > MOXIE_R13)
216 internal_error ("internal error: bad register: %d", REGNO (operand));
217 fprintf (file, "%s", reg_names[REGNO (operand)]);
221 output_address (XEXP (operand, 0));
225 /* No need to handle all strange variants, let output_addr_const
227 if (CONSTANT_P (operand))
229 output_addr_const (file, operand);
233 LOSE_AND_RETURN ("unexpected operand", x);
237 /* Per-function machine data. */
238 struct GTY(()) machine_function
240 /* Number of bytes saved on the stack for callee saved registers. */
241 int callee_saved_reg_size;
243 /* Number of bytes saved on the stack for local variables. */
246 /* The sum of 2 sizes: locals vars and padding byte for saving the
247 * registers. Used in expand_prologue () and expand_epilogue(). */
248 int size_for_adjusting_sp;
251 /* Zero initialization is OK for all current fields. */
253 static struct machine_function *
254 moxie_init_machine_status (void)
256 return ggc_cleared_alloc<machine_function> ();
260 /* The TARGET_OPTION_OVERRIDE worker. */
262 moxie_option_override (void)
264 /* Set the per-function-data initializer. */
265 init_machine_status = moxie_init_machine_status;
267 #ifdef TARGET_MOXIEBOX
268 target_flags |= MASK_HAS_MULX;
272 /* Compute the size of the local area and the size to be adjusted by the
273 * prologue and epilogue. */
276 moxie_compute_frame (void)
278 /* For aligning the local variables. */
279 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
283 /* Padding needed for each element of the frame. */
284 cfun->machine->local_vars_size = get_frame_size ();
286 /* Align to the stack alignment. */
287 padding_locals = cfun->machine->local_vars_size % stack_alignment;
289 padding_locals = stack_alignment - padding_locals;
291 cfun->machine->local_vars_size += padding_locals;
293 cfun->machine->callee_saved_reg_size = 0;
295 /* Save callee-saved registers. */
296 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
297 if (df_regs_ever_live_p (regno) && (! call_used_regs[regno]))
298 cfun->machine->callee_saved_reg_size += 4;
300 cfun->machine->size_for_adjusting_sp =
301 crtl->args.pretend_args_size
302 + cfun->machine->local_vars_size
303 + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0);
307 moxie_expand_prologue (void)
312 moxie_compute_frame ();
314 if (flag_stack_usage_info)
315 current_function_static_stack_size = cfun->machine->size_for_adjusting_sp;
317 /* Save callee-saved registers. */
318 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
320 if (!fixed_regs[regno] && df_regs_ever_live_p (regno) && !call_used_regs[regno])
322 insn = emit_insn (gen_movsi_push (gen_rtx_REG (Pmode, regno)));
323 RTX_FRAME_RELATED_P (insn) = 1;
327 if (cfun->machine->size_for_adjusting_sp > 0)
329 int i = cfun->machine->size_for_adjusting_sp;
330 while ((i >= 255) && (i <= 510))
332 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
335 RTX_FRAME_RELATED_P (insn) = 1;
340 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
343 RTX_FRAME_RELATED_P (insn) = 1;
347 rtx reg = gen_rtx_REG (SImode, MOXIE_R12);
348 insn = emit_move_insn (reg, GEN_INT (i));
349 RTX_FRAME_RELATED_P (insn) = 1;
350 insn = emit_insn (gen_subsi3 (stack_pointer_rtx,
353 RTX_FRAME_RELATED_P (insn) = 1;
359 moxie_expand_epilogue (void)
364 if (cfun->machine->callee_saved_reg_size != 0)
366 reg = gen_rtx_REG (Pmode, MOXIE_R12);
367 if (cfun->machine->callee_saved_reg_size <= 255)
369 emit_move_insn (reg, hard_frame_pointer_rtx);
370 emit_insn (gen_subsi3
372 GEN_INT (cfun->machine->callee_saved_reg_size)));
377 GEN_INT (-cfun->machine->callee_saved_reg_size));
378 emit_insn (gen_addsi3 (reg, reg, hard_frame_pointer_rtx));
380 for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
381 if (!fixed_regs[regno] && !call_used_regs[regno]
382 && df_regs_ever_live_p (regno))
384 rtx preg = gen_rtx_REG (Pmode, regno);
385 emit_insn (gen_movsi_pop (reg, preg));
389 emit_jump_insn (gen_returner ());
392 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
395 moxie_initial_elimination_offset (int from, int to)
399 if ((from) == FRAME_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
401 /* Compute this since we need to use cfun->machine->local_vars_size. */
402 moxie_compute_frame ();
403 ret = -cfun->machine->callee_saved_reg_size;
405 else if ((from) == ARG_POINTER_REGNUM && (to) == HARD_FRAME_POINTER_REGNUM)
413 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
416 moxie_setup_incoming_varargs (cumulative_args_t cum_v,
417 machine_mode mode ATTRIBUTE_UNUSED,
418 tree type ATTRIBUTE_UNUSED,
419 int *pretend_size, int no_rtl)
421 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
425 *pretend_size = regs < 0 ? 0 : GET_MODE_SIZE (SImode) * regs;
430 for (regno = *cum; regno < 8; regno++)
432 rtx reg = gen_rtx_REG (SImode, regno);
433 rtx slot = gen_rtx_PLUS (Pmode,
434 gen_rtx_REG (SImode, ARG_POINTER_REGNUM),
435 GEN_INT (UNITS_PER_WORD * (3 + (regno-2))));
437 emit_move_insn (gen_rtx_MEM (SImode, slot), reg);
442 /* Return the fixed registers used for condition codes. */
445 moxie_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
448 *p2 = INVALID_REGNUM;
452 /* Return the next register to be used to hold a function argument or
453 NULL_RTX if there's no more space. */
456 moxie_function_arg (cumulative_args_t cum_v, machine_mode mode,
457 const_tree type ATTRIBUTE_UNUSED,
458 bool named ATTRIBUTE_UNUSED)
460 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
463 return gen_rtx_REG (mode, *cum);
468 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
469 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
470 : (unsigned) int_size_in_bytes (TYPE))
473 moxie_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
474 const_tree type, bool named ATTRIBUTE_UNUSED)
476 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
478 *cum = (*cum < MOXIE_R6
479 ? *cum + ((3 + MOXIE_FUNCTION_ARG_SIZE (mode, type)) / 4)
483 /* Return non-zero if the function argument described by TYPE is to be
484 passed by reference. */
487 moxie_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
488 machine_mode mode, const_tree type,
489 bool named ATTRIBUTE_UNUSED)
491 unsigned HOST_WIDE_INT size;
495 if (AGGREGATE_TYPE_P (type))
497 size = int_size_in_bytes (type);
500 size = GET_MODE_SIZE (mode);
505 /* Some function arguments will only partially fit in the registers
506 that hold arguments. Given a new arg, return the number of bytes
507 that fit in argument passing registers. */
510 moxie_arg_partial_bytes (cumulative_args_t cum_v,
512 tree type, bool named)
514 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
515 int bytes_left, size;
520 if (moxie_pass_by_reference (cum_v, mode, type, named))
524 if (AGGREGATE_TYPE_P (type))
526 size = int_size_in_bytes (type);
529 size = GET_MODE_SIZE (mode);
531 bytes_left = (4 * 6) - ((*cum - 2) * 4);
533 if (size > bytes_left)
539 /* Worker function for TARGET_STATIC_CHAIN. */
542 moxie_static_chain (const_tree ARG_UNUSED (fndecl_or_type), bool incoming_p)
547 addr = plus_constant (Pmode, arg_pointer_rtx, 2 * UNITS_PER_WORD);
549 addr = plus_constant (Pmode, stack_pointer_rtx, -UNITS_PER_WORD);
551 mem = gen_rtx_MEM (Pmode, addr);
552 MEM_NOTRAP_P (mem) = 1;
557 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
560 moxie_asm_trampoline_template (FILE *f)
562 fprintf (f, "\tpush $sp, $r0\n");
563 fprintf (f, "\tldi.l $r0, 0x0\n");
564 fprintf (f, "\tsto.l 0x8($fp), $r0\n");
565 fprintf (f, "\tpop $sp, $r0\n");
566 fprintf (f, "\tjmpa 0x0\n");
569 /* Worker function for TARGET_TRAMPOLINE_INIT. */
572 moxie_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
574 rtx mem, fnaddr = XEXP (DECL_RTL (fndecl), 0);
576 emit_block_move (m_tramp, assemble_trampoline_template (),
577 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
579 mem = adjust_address (m_tramp, SImode, 4);
580 emit_move_insn (mem, chain_value);
581 mem = adjust_address (m_tramp, SImode, 16);
582 emit_move_insn (mem, fnaddr);
585 /* Return true for memory offset addresses between -32768 and 32767. */
587 moxie_offset_address_p (rtx x)
591 if (GET_CODE (x) == PLUS)
594 if (GET_CODE (x) == CONST_INT)
596 unsigned int v = INTVAL (x) & 0xFFFF8000;
597 return (v == 0xFFFF8000 || v == 0x00000000);
603 /* The Global `targetm' Variable. */
605 /* Initialize the GCC target structure. */
607 #undef TARGET_PROMOTE_PROTOTYPES
608 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
610 #undef TARGET_RETURN_IN_MEMORY
611 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
612 #undef TARGET_MUST_PASS_IN_STACK
613 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
614 #undef TARGET_PASS_BY_REFERENCE
615 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
616 #undef TARGET_ARG_PARTIAL_BYTES
617 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
618 #undef TARGET_FUNCTION_ARG
619 #define TARGET_FUNCTION_ARG moxie_function_arg
620 #undef TARGET_FUNCTION_ARG_ADVANCE
621 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
624 #undef TARGET_SETUP_INCOMING_VARARGS
625 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
627 #undef TARGET_FIXED_CONDITION_CODE_REGS
628 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
630 /* Define this to return an RTX representing the place where a
631 function returns or receives a value of data type RET_TYPE, a tree
632 node node representing a data type. */
633 #undef TARGET_FUNCTION_VALUE
634 #define TARGET_FUNCTION_VALUE moxie_function_value
635 #undef TARGET_LIBCALL_VALUE
636 #define TARGET_LIBCALL_VALUE moxie_libcall_value
637 #undef TARGET_FUNCTION_VALUE_REGNO_P
638 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
640 #undef TARGET_FRAME_POINTER_REQUIRED
641 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
643 #undef TARGET_STATIC_CHAIN
644 #define TARGET_STATIC_CHAIN moxie_static_chain
645 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
646 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
647 #undef TARGET_TRAMPOLINE_INIT
648 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
650 #undef TARGET_OPTION_OVERRIDE
651 #define TARGET_OPTION_OVERRIDE moxie_option_override
653 struct gcc_target targetm = TARGET_INITIALIZER;
655 #include "gt-moxie.h"