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"
41 #include "stor-layout.h"
51 #include "insn-codes.h"
56 #include "langhooks.h"
57 #include "dominance.h"
63 #include "cfgcleanup.h"
65 #include "basic-block.h"
69 #include "target-def.h"
71 #define LOSE_AND_RETURN(msgid, x) \
74 moxie_operand_lossage (msgid, x); \
78 /* Worker function for TARGET_RETURN_IN_MEMORY. */
81 moxie_return_in_memory (const_tree type
, const_tree fntype ATTRIBUTE_UNUSED
)
83 const HOST_WIDE_INT size
= int_size_in_bytes (type
);
84 return (size
== -1 || size
> 2 * UNITS_PER_WORD
);
87 /* Define how to find the value returned by a function.
88 VALTYPE is the data type of the value (as a tree).
89 If the precise function being called is known, FUNC is its
90 FUNCTION_DECL; otherwise, FUNC is 0.
92 We always return values in register $r0 for moxie. */
95 moxie_function_value (const_tree valtype
,
96 const_tree fntype_or_decl ATTRIBUTE_UNUSED
,
97 bool outgoing ATTRIBUTE_UNUSED
)
99 return gen_rtx_REG (TYPE_MODE (valtype
), MOXIE_R0
);
102 /* Define how to find the value returned by a library function.
104 We always return values in register $r0 for moxie. */
107 moxie_libcall_value (machine_mode mode
,
108 const_rtx fun ATTRIBUTE_UNUSED
)
110 return gen_rtx_REG (mode
, MOXIE_R0
);
113 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
115 We always return values in register $r0 for moxie. */
118 moxie_function_value_regno_p (const unsigned int regno
)
120 return (regno
== MOXIE_R0
);
123 /* Emit an error message when we're in an asm, and a fatal error for
124 "normal" insns. Formatted output isn't easily implemented, since we
125 use output_operand_lossage to output the actual message and handle the
126 categorization of the error. */
129 moxie_operand_lossage (const char *msgid
, rtx op
)
132 output_operand_lossage ("%s", msgid
);
135 /* The PRINT_OPERAND_ADDRESS worker. */
138 moxie_print_operand_address (FILE *file
, rtx x
)
140 switch (GET_CODE (x
))
143 fprintf (file
, "(%s)", reg_names
[REGNO (x
)]);
147 switch (GET_CODE (XEXP (x
, 1)))
150 fprintf (file
, "%ld(%s)",
151 INTVAL(XEXP (x
, 1)), reg_names
[REGNO (XEXP (x
, 0))]);
154 output_addr_const (file
, XEXP (x
, 1));
155 fprintf (file
, "(%s)", reg_names
[REGNO (XEXP (x
, 0))]);
159 rtx plus
= XEXP (XEXP (x
, 1), 0);
160 if (GET_CODE (XEXP (plus
, 0)) == SYMBOL_REF
161 && CONST_INT_P (XEXP (plus
, 1)))
163 output_addr_const(file
, XEXP (plus
, 0));
164 fprintf (file
,"+%ld(%s)", INTVAL (XEXP (plus
, 1)),
165 reg_names
[REGNO (XEXP (x
, 0))]);
177 output_addr_const (file
, x
);
182 /* The PRINT_OPERAND worker. */
185 moxie_print_operand (FILE *file
, rtx x
, int code
)
189 /* New code entries should just be added to the switch below. If
190 handling is finished, just return. If handling was just a
191 modification of the operand, the modified operand should be put in
192 "operand", and then do a break to let default handling
193 (zero-modifier) output the operand. */
198 /* No code, print as usual. */
202 LOSE_AND_RETURN ("invalid operand modifier letter", x
);
205 /* Print an operand as without a modifier letter. */
206 switch (GET_CODE (operand
))
209 if (REGNO (operand
) > MOXIE_R13
)
210 internal_error ("internal error: bad register: %d", REGNO (operand
));
211 fprintf (file
, "%s", reg_names
[REGNO (operand
)]);
215 output_address (XEXP (operand
, 0));
219 /* No need to handle all strange variants, let output_addr_const
221 if (CONSTANT_P (operand
))
223 output_addr_const (file
, operand
);
227 LOSE_AND_RETURN ("unexpected operand", x
);
231 /* Per-function machine data. */
232 struct GTY(()) machine_function
234 /* Number of bytes saved on the stack for callee saved registers. */
235 int callee_saved_reg_size
;
237 /* Number of bytes saved on the stack for local variables. */
240 /* The sum of 2 sizes: locals vars and padding byte for saving the
241 * registers. Used in expand_prologue () and expand_epilogue(). */
242 int size_for_adjusting_sp
;
245 /* Zero initialization is OK for all current fields. */
247 static struct machine_function
*
248 moxie_init_machine_status (void)
250 return ggc_cleared_alloc
<machine_function
> ();
254 /* The TARGET_OPTION_OVERRIDE worker. */
256 moxie_option_override (void)
258 /* Set the per-function-data initializer. */
259 init_machine_status
= moxie_init_machine_status
;
261 #ifdef TARGET_MOXIEBOX
262 target_flags
|= MASK_HAS_MULX
;
266 /* Compute the size of the local area and the size to be adjusted by the
267 * prologue and epilogue. */
270 moxie_compute_frame (void)
272 /* For aligning the local variables. */
273 int stack_alignment
= STACK_BOUNDARY
/ BITS_PER_UNIT
;
277 /* Padding needed for each element of the frame. */
278 cfun
->machine
->local_vars_size
= get_frame_size ();
280 /* Align to the stack alignment. */
281 padding_locals
= cfun
->machine
->local_vars_size
% stack_alignment
;
283 padding_locals
= stack_alignment
- padding_locals
;
285 cfun
->machine
->local_vars_size
+= padding_locals
;
287 cfun
->machine
->callee_saved_reg_size
= 0;
289 /* Save callee-saved registers. */
290 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
291 if (df_regs_ever_live_p (regno
) && (! call_used_regs
[regno
]))
292 cfun
->machine
->callee_saved_reg_size
+= 4;
294 cfun
->machine
->size_for_adjusting_sp
=
295 crtl
->args
.pretend_args_size
296 + cfun
->machine
->local_vars_size
297 + (ACCUMULATE_OUTGOING_ARGS
? crtl
->outgoing_args_size
: 0);
301 moxie_expand_prologue (void)
306 moxie_compute_frame ();
308 if (flag_stack_usage_info
)
309 current_function_static_stack_size
= cfun
->machine
->size_for_adjusting_sp
;
311 /* Save callee-saved registers. */
312 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
314 if (!fixed_regs
[regno
] && df_regs_ever_live_p (regno
) && !call_used_regs
[regno
])
316 insn
= emit_insn (gen_movsi_push (gen_rtx_REG (Pmode
, regno
)));
317 RTX_FRAME_RELATED_P (insn
) = 1;
321 if (cfun
->machine
->size_for_adjusting_sp
> 0)
323 int i
= cfun
->machine
->size_for_adjusting_sp
;
324 while ((i
>= 255) && (i
<= 510))
326 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
329 RTX_FRAME_RELATED_P (insn
) = 1;
334 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
337 RTX_FRAME_RELATED_P (insn
) = 1;
341 rtx reg
= gen_rtx_REG (SImode
, MOXIE_R12
);
342 insn
= emit_move_insn (reg
, GEN_INT (i
));
343 RTX_FRAME_RELATED_P (insn
) = 1;
344 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
347 RTX_FRAME_RELATED_P (insn
) = 1;
353 moxie_expand_epilogue (void)
358 if (cfun
->machine
->callee_saved_reg_size
!= 0)
360 reg
= gen_rtx_REG (Pmode
, MOXIE_R12
);
361 if (cfun
->machine
->callee_saved_reg_size
<= 255)
363 emit_move_insn (reg
, hard_frame_pointer_rtx
);
364 emit_insn (gen_subsi3
366 GEN_INT (cfun
->machine
->callee_saved_reg_size
)));
371 GEN_INT (-cfun
->machine
->callee_saved_reg_size
));
372 emit_insn (gen_addsi3 (reg
, reg
, hard_frame_pointer_rtx
));
374 for (regno
= FIRST_PSEUDO_REGISTER
; regno
-- > 0; )
375 if (!fixed_regs
[regno
] && !call_used_regs
[regno
]
376 && df_regs_ever_live_p (regno
))
378 rtx preg
= gen_rtx_REG (Pmode
, regno
);
379 emit_insn (gen_movsi_pop (reg
, preg
));
383 emit_jump_insn (gen_returner ());
386 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
389 moxie_initial_elimination_offset (int from
, int to
)
393 if ((from
) == FRAME_POINTER_REGNUM
&& (to
) == HARD_FRAME_POINTER_REGNUM
)
395 /* Compute this since we need to use cfun->machine->local_vars_size. */
396 moxie_compute_frame ();
397 ret
= -cfun
->machine
->callee_saved_reg_size
;
399 else if ((from
) == ARG_POINTER_REGNUM
&& (to
) == HARD_FRAME_POINTER_REGNUM
)
407 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
410 moxie_setup_incoming_varargs (cumulative_args_t cum_v
,
411 machine_mode mode ATTRIBUTE_UNUSED
,
412 tree type ATTRIBUTE_UNUSED
,
413 int *pretend_size
, int no_rtl
)
415 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
419 *pretend_size
= regs
< 0 ? 0 : GET_MODE_SIZE (SImode
) * regs
;
424 for (regno
= *cum
; regno
< 8; regno
++)
426 rtx reg
= gen_rtx_REG (SImode
, regno
);
427 rtx slot
= gen_rtx_PLUS (Pmode
,
428 gen_rtx_REG (SImode
, ARG_POINTER_REGNUM
),
429 GEN_INT (UNITS_PER_WORD
* (3 + (regno
-2))));
431 emit_move_insn (gen_rtx_MEM (SImode
, slot
), reg
);
436 /* Return the fixed registers used for condition codes. */
439 moxie_fixed_condition_code_regs (unsigned int *p1
, unsigned int *p2
)
442 *p2
= INVALID_REGNUM
;
446 /* Return the next register to be used to hold a function argument or
447 NULL_RTX if there's no more space. */
450 moxie_function_arg (cumulative_args_t cum_v
, machine_mode mode
,
451 const_tree type ATTRIBUTE_UNUSED
,
452 bool named ATTRIBUTE_UNUSED
)
454 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
457 return gen_rtx_REG (mode
, *cum
);
462 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
463 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
464 : (unsigned) int_size_in_bytes (TYPE))
467 moxie_function_arg_advance (cumulative_args_t cum_v
, machine_mode mode
,
468 const_tree type
, bool named ATTRIBUTE_UNUSED
)
470 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
472 *cum
= (*cum
< MOXIE_R6
473 ? *cum
+ ((3 + MOXIE_FUNCTION_ARG_SIZE (mode
, type
)) / 4)
477 /* Return non-zero if the function argument described by TYPE is to be
478 passed by reference. */
481 moxie_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED
,
482 machine_mode mode
, const_tree type
,
483 bool named ATTRIBUTE_UNUSED
)
485 unsigned HOST_WIDE_INT size
;
489 if (AGGREGATE_TYPE_P (type
))
491 size
= int_size_in_bytes (type
);
494 size
= GET_MODE_SIZE (mode
);
499 /* Some function arguments will only partially fit in the registers
500 that hold arguments. Given a new arg, return the number of bytes
501 that fit in argument passing registers. */
504 moxie_arg_partial_bytes (cumulative_args_t cum_v
,
506 tree type
, bool named
)
508 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
509 int bytes_left
, size
;
514 if (moxie_pass_by_reference (cum_v
, mode
, type
, named
))
518 if (AGGREGATE_TYPE_P (type
))
520 size
= int_size_in_bytes (type
);
523 size
= GET_MODE_SIZE (mode
);
525 bytes_left
= (4 * 6) - ((*cum
- 2) * 4);
527 if (size
> bytes_left
)
533 /* Worker function for TARGET_STATIC_CHAIN. */
536 moxie_static_chain (const_tree
ARG_UNUSED (fndecl_or_type
), bool incoming_p
)
541 addr
= plus_constant (Pmode
, arg_pointer_rtx
, 2 * UNITS_PER_WORD
);
543 addr
= plus_constant (Pmode
, stack_pointer_rtx
, -UNITS_PER_WORD
);
545 mem
= gen_rtx_MEM (Pmode
, addr
);
546 MEM_NOTRAP_P (mem
) = 1;
551 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
554 moxie_asm_trampoline_template (FILE *f
)
556 fprintf (f
, "\tpush $sp, $r0\n");
557 fprintf (f
, "\tldi.l $r0, 0x0\n");
558 fprintf (f
, "\tsto.l 0x8($fp), $r0\n");
559 fprintf (f
, "\tpop $sp, $r0\n");
560 fprintf (f
, "\tjmpa 0x0\n");
563 /* Worker function for TARGET_TRAMPOLINE_INIT. */
566 moxie_trampoline_init (rtx m_tramp
, tree fndecl
, rtx chain_value
)
568 rtx mem
, fnaddr
= XEXP (DECL_RTL (fndecl
), 0);
570 emit_block_move (m_tramp
, assemble_trampoline_template (),
571 GEN_INT (TRAMPOLINE_SIZE
), BLOCK_OP_NORMAL
);
573 mem
= adjust_address (m_tramp
, SImode
, 4);
574 emit_move_insn (mem
, chain_value
);
575 mem
= adjust_address (m_tramp
, SImode
, 16);
576 emit_move_insn (mem
, fnaddr
);
579 /* Return true for memory offset addresses between -32768 and 32767. */
581 moxie_offset_address_p (rtx x
)
585 if (GET_CODE (x
) == PLUS
)
588 if (GET_CODE (x
) == CONST_INT
)
590 unsigned int v
= INTVAL (x
) & 0xFFFF8000;
591 return (v
== 0xFFFF8000 || v
== 0x00000000);
597 /* The Global `targetm' Variable. */
599 /* Initialize the GCC target structure. */
601 #undef TARGET_PROMOTE_PROTOTYPES
602 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
604 #undef TARGET_RETURN_IN_MEMORY
605 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
606 #undef TARGET_MUST_PASS_IN_STACK
607 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
608 #undef TARGET_PASS_BY_REFERENCE
609 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
610 #undef TARGET_ARG_PARTIAL_BYTES
611 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
612 #undef TARGET_FUNCTION_ARG
613 #define TARGET_FUNCTION_ARG moxie_function_arg
614 #undef TARGET_FUNCTION_ARG_ADVANCE
615 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
618 #undef TARGET_SETUP_INCOMING_VARARGS
619 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
621 #undef TARGET_FIXED_CONDITION_CODE_REGS
622 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
624 /* Define this to return an RTX representing the place where a
625 function returns or receives a value of data type RET_TYPE, a tree
626 node node representing a data type. */
627 #undef TARGET_FUNCTION_VALUE
628 #define TARGET_FUNCTION_VALUE moxie_function_value
629 #undef TARGET_LIBCALL_VALUE
630 #define TARGET_LIBCALL_VALUE moxie_libcall_value
631 #undef TARGET_FUNCTION_VALUE_REGNO_P
632 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
634 #undef TARGET_FRAME_POINTER_REQUIRED
635 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
637 #undef TARGET_STATIC_CHAIN
638 #define TARGET_STATIC_CHAIN moxie_static_chain
639 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
640 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
641 #undef TARGET_TRAMPOLINE_INIT
642 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
644 #undef TARGET_OPTION_OVERRIDE
645 #define TARGET_OPTION_OVERRIDE moxie_option_override
647 struct gcc_target targetm
= TARGET_INITIALIZER
;
649 #include "gt-moxie.h"