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/>. */
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"
39 #include "stor-layout.h"
48 #include "target-def.h"
50 #include "langhooks.h"
53 #define LOSE_AND_RETURN(msgid, x) \
56 moxie_operand_lossage (msgid, x); \
60 /* Worker function for TARGET_RETURN_IN_MEMORY. */
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. */
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. */
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. */
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. */
111 moxie_operand_lossage (const char *msgid
, rtx op
)
114 output_operand_lossage ("%s", msgid
);
117 /* The PRINT_OPERAND_ADDRESS worker. */
120 moxie_print_operand_address (FILE *file
, rtx x
)
122 switch (GET_CODE (x
))
125 fprintf (file
, "(%s)", reg_names
[REGNO (x
)]);
129 switch (GET_CODE (XEXP (x
, 1)))
132 fprintf (file
, "%ld(%s)",
133 INTVAL(XEXP (x
, 1)), reg_names
[REGNO (XEXP (x
, 0))]);
136 output_addr_const (file
, XEXP (x
, 1));
137 fprintf (file
, "(%s)", reg_names
[REGNO (XEXP (x
, 0))]);
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))]);
159 output_addr_const (file
, x
);
164 /* The PRINT_OPERAND worker. */
167 moxie_print_operand (FILE *file
, rtx x
, int code
)
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. */
180 /* No code, print as usual. */
184 LOSE_AND_RETURN ("invalid operand modifier letter", x
);
187 /* Print an operand as without a modifier letter. */
188 switch (GET_CODE (operand
))
191 if (REGNO (operand
) > MOXIE_R13
)
192 internal_error ("internal error: bad register: %d", REGNO (operand
));
193 fprintf (file
, "%s", reg_names
[REGNO (operand
)]);
197 output_address (XEXP (operand
, 0));
201 /* No need to handle all strange variants, let output_addr_const
203 if (CONSTANT_P (operand
))
205 output_addr_const (file
, operand
);
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. */
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. */
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. */
249 moxie_compute_frame (void)
251 /* For aligning the local variables. */
252 int stack_alignment
= STACK_BOUNDARY
/ BITS_PER_UNIT
;
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
;
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);
280 moxie_expand_prologue (void)
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
,
308 RTX_FRAME_RELATED_P (insn
) = 1;
313 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
316 RTX_FRAME_RELATED_P (insn
) = 1;
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
,
326 RTX_FRAME_RELATED_P (insn
) = 1;
332 moxie_expand_epilogue (void)
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
345 GEN_INT (cfun
->machine
->callee_saved_reg_size
)));
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
)
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
)
386 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
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
);
398 *pretend_size
= regs
< 0 ? 0 : GET_MODE_SIZE (SImode
) * regs
;
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. */
418 moxie_fixed_condition_code_regs (unsigned int *p1
, unsigned int *p2
)
421 *p2
= INVALID_REGNUM
;
425 /* Return the next register to be used to hold a function argument or
426 NULL_RTX if there's no more space. */
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
);
436 return gen_rtx_REG (mode
, *cum
);
441 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
442 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
443 : (unsigned) int_size_in_bytes (TYPE))
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)
456 /* Return non-zero if the function argument described by TYPE is to be
457 passed by reference. */
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
;
468 if (AGGREGATE_TYPE_P (type
))
470 size
= int_size_in_bytes (type
);
473 size
= GET_MODE_SIZE (mode
);
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. */
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
;
493 if (moxie_pass_by_reference (cum_v
, mode
, type
, named
))
497 if (AGGREGATE_TYPE_P (type
))
499 size
= int_size_in_bytes (type
);
502 size
= GET_MODE_SIZE (mode
);
504 bytes_left
= (4 * 6) - ((*cum
- 2) * 4);
506 if (size
> bytes_left
)
512 /* Worker function for TARGET_STATIC_CHAIN. */
515 moxie_static_chain (const_tree fndecl
, bool incoming_p
)
519 if (!DECL_STATIC_CHAIN (fndecl
))
523 addr
= plus_constant (Pmode
, arg_pointer_rtx
, 2 * UNITS_PER_WORD
);
525 addr
= plus_constant (Pmode
, stack_pointer_rtx
, -UNITS_PER_WORD
);
527 mem
= gen_rtx_MEM (Pmode
, addr
);
528 MEM_NOTRAP_P (mem
) = 1;
533 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
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. */
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"