1 /* Target Code for moxie
2 Copyright (C) 2008-2024 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 #define IN_TARGET_CODE 1
25 #include "coretypes.h"
30 #include "stringpool.h"
36 #include "diagnostic-core.h"
38 #include "stor-layout.h"
44 /* This file should be included last. */
45 #include "target-def.h"
47 #define LOSE_AND_RETURN(msgid, x) \
50 moxie_operand_lossage (msgid, x); \
54 /* Worker function for TARGET_RETURN_IN_MEMORY. */
57 moxie_return_in_memory (const_tree type
, const_tree fntype ATTRIBUTE_UNUSED
)
59 const HOST_WIDE_INT size
= int_size_in_bytes (type
);
60 return (size
== -1 || size
> 2 * UNITS_PER_WORD
);
63 /* Define how to find the value returned by a function.
64 VALTYPE is the data type of the value (as a tree).
65 If the precise function being called is known, FUNC is its
66 FUNCTION_DECL; otherwise, FUNC is 0.
68 We always return values in register $r0 for moxie. */
71 moxie_function_value (const_tree valtype
,
72 const_tree fntype_or_decl ATTRIBUTE_UNUSED
,
73 bool outgoing ATTRIBUTE_UNUSED
)
75 return gen_rtx_REG (TYPE_MODE (valtype
), MOXIE_R0
);
78 /* Define how to find the value returned by a library function.
80 We always return values in register $r0 for moxie. */
83 moxie_libcall_value (machine_mode mode
,
84 const_rtx fun ATTRIBUTE_UNUSED
)
86 return gen_rtx_REG (mode
, MOXIE_R0
);
89 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
91 We always return values in register $r0 for moxie. */
94 moxie_function_value_regno_p (const unsigned int regno
)
96 return (regno
== MOXIE_R0
);
99 /* Emit an error message when we're in an asm, and a fatal error for
100 "normal" insns. Formatted output isn't easily implemented, since we
101 use output_operand_lossage to output the actual message and handle the
102 categorization of the error. */
105 moxie_operand_lossage (const char *msgid
, rtx op
)
108 output_operand_lossage ("%s", msgid
);
111 /* The PRINT_OPERAND_ADDRESS worker. */
114 moxie_print_operand_address (FILE *file
, machine_mode
, rtx x
)
116 switch (GET_CODE (x
))
119 fprintf (file
, "(%s)", reg_names
[REGNO (x
)]);
123 switch (GET_CODE (XEXP (x
, 1)))
126 fprintf (file
, "%ld(%s)",
127 INTVAL(XEXP (x
, 1)), reg_names
[REGNO (XEXP (x
, 0))]);
130 output_addr_const (file
, XEXP (x
, 1));
131 fprintf (file
, "(%s)", reg_names
[REGNO (XEXP (x
, 0))]);
135 rtx plus
= XEXP (XEXP (x
, 1), 0);
136 if (GET_CODE (XEXP (plus
, 0)) == SYMBOL_REF
137 && CONST_INT_P (XEXP (plus
, 1)))
139 output_addr_const(file
, XEXP (plus
, 0));
140 fprintf (file
,"+%ld(%s)", INTVAL (XEXP (plus
, 1)),
141 reg_names
[REGNO (XEXP (x
, 0))]);
153 output_addr_const (file
, x
);
158 /* The PRINT_OPERAND worker. */
161 moxie_print_operand (FILE *file
, rtx x
, int code
)
165 /* New code entries should just be added to the switch below. If
166 handling is finished, just return. If handling was just a
167 modification of the operand, the modified operand should be put in
168 "operand", and then do a break to let default handling
169 (zero-modifier) output the operand. */
174 /* No code, print as usual. */
178 LOSE_AND_RETURN ("invalid operand modifier letter", x
);
181 /* Print an operand as without a modifier letter. */
182 switch (GET_CODE (operand
))
185 if (REGNO (operand
) > MOXIE_R13
)
186 internal_error ("internal error: bad register: %d", REGNO (operand
));
187 fprintf (file
, "%s", reg_names
[REGNO (operand
)]);
191 output_address (GET_MODE (XEXP (operand
, 0)), XEXP (operand
, 0));
195 /* No need to handle all strange variants, let output_addr_const
197 if (CONSTANT_P (operand
))
199 output_addr_const (file
, operand
);
203 LOSE_AND_RETURN ("unexpected operand", x
);
207 /* Per-function machine data. */
208 struct GTY(()) machine_function
210 /* Number of bytes saved on the stack for callee saved registers. */
211 int callee_saved_reg_size
;
213 /* Number of bytes saved on the stack for local variables. */
216 /* The sum of 2 sizes: locals vars and padding byte for saving the
217 * registers. Used in expand_prologue () and expand_epilogue(). */
218 int size_for_adjusting_sp
;
221 /* Zero initialization is OK for all current fields. */
223 static struct machine_function
*
224 moxie_init_machine_status (void)
226 return ggc_cleared_alloc
<machine_function
> ();
230 /* The TARGET_OPTION_OVERRIDE worker. */
232 moxie_option_override (void)
234 /* Set the per-function-data initializer. */
235 init_machine_status
= moxie_init_machine_status
;
237 #ifdef TARGET_MOXIEBOX
238 target_flags
|= MASK_HAS_MULX
;
242 /* Compute the size of the local area and the size to be adjusted by the
243 * prologue and epilogue. */
246 moxie_compute_frame (void)
248 /* For aligning the local variables. */
249 int stack_alignment
= STACK_BOUNDARY
/ BITS_PER_UNIT
;
253 /* Padding needed for each element of the frame. */
254 cfun
->machine
->local_vars_size
= get_frame_size ();
256 /* Align to the stack alignment. */
257 padding_locals
= cfun
->machine
->local_vars_size
% stack_alignment
;
259 padding_locals
= stack_alignment
- padding_locals
;
261 cfun
->machine
->local_vars_size
+= padding_locals
;
263 cfun
->machine
->callee_saved_reg_size
= 0;
265 /* Save callee-saved registers. */
266 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
267 if (df_regs_ever_live_p (regno
) && (! call_used_or_fixed_reg_p (regno
)))
268 cfun
->machine
->callee_saved_reg_size
+= 4;
270 cfun
->machine
->size_for_adjusting_sp
=
271 crtl
->args
.pretend_args_size
272 + cfun
->machine
->local_vars_size
273 + (ACCUMULATE_OUTGOING_ARGS
274 ? (HOST_WIDE_INT
) crtl
->outgoing_args_size
: 0);
278 moxie_expand_prologue (void)
283 moxie_compute_frame ();
285 if (flag_stack_usage_info
)
286 current_function_static_stack_size
= cfun
->machine
->size_for_adjusting_sp
;
288 /* Save callee-saved registers. */
289 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
291 if (df_regs_ever_live_p (regno
)
292 && !call_used_or_fixed_reg_p (regno
))
294 insn
= emit_insn (gen_movsi_push (gen_rtx_REG (Pmode
, regno
)));
295 RTX_FRAME_RELATED_P (insn
) = 1;
299 if (cfun
->machine
->size_for_adjusting_sp
> 0)
301 int i
= cfun
->machine
->size_for_adjusting_sp
;
302 while ((i
>= 255) && (i
<= 510))
304 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
307 RTX_FRAME_RELATED_P (insn
) = 1;
312 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
315 RTX_FRAME_RELATED_P (insn
) = 1;
319 rtx reg
= gen_rtx_REG (SImode
, MOXIE_R12
);
320 insn
= emit_move_insn (reg
, GEN_INT (i
));
321 RTX_FRAME_RELATED_P (insn
) = 1;
322 insn
= emit_insn (gen_subsi3 (stack_pointer_rtx
,
325 RTX_FRAME_RELATED_P (insn
) = 1;
331 moxie_expand_epilogue (void)
336 if (cfun
->machine
->callee_saved_reg_size
!= 0)
338 reg
= gen_rtx_REG (Pmode
, MOXIE_R12
);
339 if (cfun
->machine
->callee_saved_reg_size
<= 255)
341 emit_move_insn (reg
, hard_frame_pointer_rtx
);
342 emit_insn (gen_subsi3
344 GEN_INT (cfun
->machine
->callee_saved_reg_size
)));
349 GEN_INT (-cfun
->machine
->callee_saved_reg_size
));
350 emit_insn (gen_addsi3 (reg
, reg
, hard_frame_pointer_rtx
));
352 for (regno
= FIRST_PSEUDO_REGISTER
; regno
-- > 0; )
353 if (!call_used_or_fixed_reg_p (regno
)
354 && df_regs_ever_live_p (regno
))
356 rtx preg
= gen_rtx_REG (Pmode
, regno
);
357 emit_insn (gen_movsi_pop (reg
, preg
));
361 emit_jump_insn (gen_returner ());
364 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
367 moxie_initial_elimination_offset (int from
, int to
)
371 if ((from
) == FRAME_POINTER_REGNUM
&& (to
) == HARD_FRAME_POINTER_REGNUM
)
373 /* Compute this since we need to use cfun->machine->local_vars_size. */
374 moxie_compute_frame ();
375 ret
= -cfun
->machine
->callee_saved_reg_size
;
377 else if ((from
) == ARG_POINTER_REGNUM
&& (to
) == HARD_FRAME_POINTER_REGNUM
)
385 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
388 moxie_setup_incoming_varargs (cumulative_args_t cum_v
,
389 const function_arg_info
&,
390 int *pretend_size
, int no_rtl
)
392 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
396 *pretend_size
= regs
< 0 ? 0 : GET_MODE_SIZE (SImode
) * regs
;
401 for (regno
= *cum
; regno
< 8; regno
++)
403 rtx reg
= gen_rtx_REG (SImode
, regno
);
404 rtx slot
= gen_rtx_PLUS (Pmode
,
405 gen_rtx_REG (SImode
, ARG_POINTER_REGNUM
),
406 GEN_INT (UNITS_PER_WORD
* (3 + (regno
-2))));
408 emit_move_insn (gen_rtx_MEM (SImode
, slot
), reg
);
413 /* Return the fixed registers used for condition codes. */
416 moxie_fixed_condition_code_regs (unsigned int *p1
, unsigned int *p2
)
419 *p2
= INVALID_REGNUM
;
423 /* Return the next register to be used to hold a function argument or
424 NULL_RTX if there's no more space. */
427 moxie_function_arg (cumulative_args_t cum_v
, const function_arg_info
&arg
)
429 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
432 return gen_rtx_REG (arg
.mode
, *cum
);
437 #define MOXIE_FUNCTION_ARG_SIZE(MODE, TYPE) \
438 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
439 : (unsigned) int_size_in_bytes (TYPE))
442 moxie_function_arg_advance (cumulative_args_t cum_v
,
443 const function_arg_info
&arg
)
445 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
447 *cum
= (*cum
< MOXIE_R6
448 ? *cum
+ ((3 + MOXIE_FUNCTION_ARG_SIZE (arg
.mode
, arg
.type
)) / 4)
452 /* Return non-zero if the function argument described by ARG is to be
453 passed by reference. */
456 moxie_pass_by_reference (cumulative_args_t
, const function_arg_info
&arg
)
458 if (arg
.aggregate_type_p ())
460 unsigned HOST_WIDE_INT size
= arg
.type_size_in_bytes ();
464 /* Some function arguments will only partially fit in the registers
465 that hold arguments. Given a new arg, return the number of bytes
466 that fit in argument passing registers. */
469 moxie_arg_partial_bytes (cumulative_args_t cum_v
, const function_arg_info
&arg
)
471 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
472 int bytes_left
, size
;
477 if (moxie_pass_by_reference (cum_v
, arg
))
481 if (AGGREGATE_TYPE_P (arg
.type
))
483 size
= int_size_in_bytes (arg
.type
);
486 size
= GET_MODE_SIZE (arg
.mode
);
488 bytes_left
= (4 * 6) - ((*cum
- 2) * 4);
490 if (size
> bytes_left
)
496 /* Worker function for TARGET_STATIC_CHAIN. */
499 moxie_static_chain (const_tree
ARG_UNUSED (fndecl_or_type
), bool incoming_p
)
504 addr
= plus_constant (Pmode
, arg_pointer_rtx
, 2 * UNITS_PER_WORD
);
506 addr
= plus_constant (Pmode
, stack_pointer_rtx
, -UNITS_PER_WORD
);
508 mem
= gen_rtx_MEM (Pmode
, addr
);
509 MEM_NOTRAP_P (mem
) = 1;
514 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE. */
517 moxie_asm_trampoline_template (FILE *f
)
519 fprintf (f
, "\tpush $sp, $r0\n");
520 fprintf (f
, "\tldi.l $r0, 0x0\n");
521 fprintf (f
, "\tsto.l 0x8($fp), $r0\n");
522 fprintf (f
, "\tpop $sp, $r0\n");
523 fprintf (f
, "\tjmpa 0x0\n");
526 /* Worker function for TARGET_TRAMPOLINE_INIT. */
529 moxie_trampoline_init (rtx m_tramp
, tree fndecl
, rtx chain_value
)
531 rtx mem
, fnaddr
= XEXP (DECL_RTL (fndecl
), 0);
533 emit_block_move (m_tramp
, assemble_trampoline_template (),
534 GEN_INT (TRAMPOLINE_SIZE
), BLOCK_OP_NORMAL
);
536 mem
= adjust_address (m_tramp
, SImode
, 4);
537 emit_move_insn (mem
, chain_value
);
538 mem
= adjust_address (m_tramp
, SImode
, 16);
539 emit_move_insn (mem
, fnaddr
);
542 /* Return true for memory offset addresses between -32768 and 32767. */
544 moxie_offset_address_p (rtx x
)
548 if (GET_CODE (x
) == PLUS
)
551 if (GET_CODE (x
) == CONST_INT
)
553 unsigned int v
= INTVAL (x
) & 0xFFFF8000;
554 return (v
== 0xFFFF8000 || v
== 0x00000000);
560 /* Helper function for `moxie_legitimate_address_p'. */
563 moxie_reg_ok_for_base_p (const_rtx reg
, bool strict_p
)
565 int regno
= REGNO (reg
);
568 return HARD_REGNO_OK_FOR_BASE_P (regno
)
569 || HARD_REGNO_OK_FOR_BASE_P (reg_renumber
[regno
]);
571 return !HARD_REGISTER_NUM_P (regno
)
572 || HARD_REGNO_OK_FOR_BASE_P (regno
);
575 /* Worker function for TARGET_LEGITIMATE_ADDRESS_P. */
578 moxie_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED
,
579 rtx x
, bool strict_p
,
581 code_helper
= ERROR_MARK
)
583 gcc_assert (ADDR_SPACE_GENERIC_P (as
));
585 if (GET_CODE(x
) == PLUS
586 && REG_P (XEXP (x
, 0))
587 && moxie_reg_ok_for_base_p (XEXP (x
, 0), strict_p
)
588 && CONST_INT_P (XEXP (x
, 1))
589 && IN_RANGE (INTVAL (XEXP (x
, 1)), -32768, 32767))
591 if (REG_P (x
) && moxie_reg_ok_for_base_p (x
, strict_p
))
593 if (GET_CODE (x
) == SYMBOL_REF
594 || GET_CODE (x
) == LABEL_REF
595 || GET_CODE (x
) == CONST
)
600 /* The Global `targetm' Variable. */
602 /* Initialize the GCC target structure. */
604 #undef TARGET_PROMOTE_PROTOTYPES
605 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
607 #undef TARGET_RETURN_IN_MEMORY
608 #define TARGET_RETURN_IN_MEMORY moxie_return_in_memory
609 #undef TARGET_MUST_PASS_IN_STACK
610 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
611 #undef TARGET_PASS_BY_REFERENCE
612 #define TARGET_PASS_BY_REFERENCE moxie_pass_by_reference
613 #undef TARGET_ARG_PARTIAL_BYTES
614 #define TARGET_ARG_PARTIAL_BYTES moxie_arg_partial_bytes
615 #undef TARGET_FUNCTION_ARG
616 #define TARGET_FUNCTION_ARG moxie_function_arg
617 #undef TARGET_FUNCTION_ARG_ADVANCE
618 #define TARGET_FUNCTION_ARG_ADVANCE moxie_function_arg_advance
620 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
621 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P moxie_legitimate_address_p
623 #undef TARGET_SETUP_INCOMING_VARARGS
624 #define TARGET_SETUP_INCOMING_VARARGS moxie_setup_incoming_varargs
626 #undef TARGET_FIXED_CONDITION_CODE_REGS
627 #define TARGET_FIXED_CONDITION_CODE_REGS moxie_fixed_condition_code_regs
629 /* Define this to return an RTX representing the place where a
630 function returns or receives a value of data type RET_TYPE, a tree
631 node representing a data type. */
632 #undef TARGET_FUNCTION_VALUE
633 #define TARGET_FUNCTION_VALUE moxie_function_value
634 #undef TARGET_LIBCALL_VALUE
635 #define TARGET_LIBCALL_VALUE moxie_libcall_value
636 #undef TARGET_FUNCTION_VALUE_REGNO_P
637 #define TARGET_FUNCTION_VALUE_REGNO_P moxie_function_value_regno_p
639 #undef TARGET_FRAME_POINTER_REQUIRED
640 #define TARGET_FRAME_POINTER_REQUIRED hook_bool_void_true
642 #undef TARGET_STATIC_CHAIN
643 #define TARGET_STATIC_CHAIN moxie_static_chain
644 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
645 #define TARGET_ASM_TRAMPOLINE_TEMPLATE moxie_asm_trampoline_template
646 #undef TARGET_TRAMPOLINE_INIT
647 #define TARGET_TRAMPOLINE_INIT moxie_trampoline_init
649 #undef TARGET_OPTION_OVERRIDE
650 #define TARGET_OPTION_OVERRIDE moxie_option_override
652 #undef TARGET_PRINT_OPERAND
653 #define TARGET_PRINT_OPERAND moxie_print_operand
654 #undef TARGET_PRINT_OPERAND_ADDRESS
655 #define TARGET_PRINT_OPERAND_ADDRESS moxie_print_operand_address
657 #undef TARGET_CONSTANT_ALIGNMENT
658 #define TARGET_CONSTANT_ALIGNMENT constant_alignment_word_strings
660 struct gcc_target targetm
= TARGET_INITIALIZER
;
662 #include "gt-moxie.h"