1 /* Target Code for ft32
2 Copyright (C) 2015 Free Software Foundation
3 Contributed by FTDI <support@ftdi.com>
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"
32 #include "diagnostic-core.h"
34 #include "stor-layout.h"
39 /* This file should be included last. */
40 #include "target-def.h"
44 #define LOSE_AND_RETURN(msgid, x) \
47 ft32_operand_lossage (msgid, x); \
51 /* Worker function for TARGET_RETURN_IN_MEMORY. */
54 ft32_return_in_memory (const_tree type
, const_tree fntype ATTRIBUTE_UNUSED
)
56 const HOST_WIDE_INT size
= int_size_in_bytes (type
);
57 return (size
== -1 || size
> 2 * UNITS_PER_WORD
);
60 /* Define how to find the value returned by a function.
61 VALTYPE is the data type of the value (as a tree).
62 If the precise function being called is known, FUNC is its
63 FUNCTION_DECL; otherwise, FUNC is 0.
65 We always return values in register $r0 for ft32. */
68 ft32_function_value (const_tree valtype
,
69 const_tree fntype_or_decl ATTRIBUTE_UNUSED
,
70 bool outgoing ATTRIBUTE_UNUSED
)
72 return gen_rtx_REG (TYPE_MODE (valtype
), FT32_R0
);
75 /* Define how to find the value returned by a library function.
77 We always return values in register $r0 for ft32. */
80 ft32_libcall_value (enum machine_mode mode
, const_rtx fun ATTRIBUTE_UNUSED
)
82 return gen_rtx_REG (mode
, FT32_R0
);
85 /* Handle TARGET_FUNCTION_VALUE_REGNO_P.
87 We always return values in register $r0 for ft32. */
90 ft32_function_value_regno_p (const unsigned int regno
)
92 return (regno
== FT32_R0
);
95 /* Emit an error message when we're in an asm, and a fatal error for
96 "normal" insns. Formatted output isn't easily implemented, since we
97 use output_operand_lossage to output the actual message and handle the
98 categorization of the error. */
101 ft32_operand_lossage (const char *msgid
, rtx op
)
104 output_operand_lossage ("%s", msgid
);
107 /* The PRINT_OPERAND_ADDRESS worker. */
110 ft32_print_operand_address (FILE * file
, rtx x
)
112 switch (GET_CODE (x
))
115 fprintf (file
, "%s,0", reg_names
[REGNO (x
)]);
119 switch (GET_CODE (XEXP (x
, 1)))
122 fprintf (file
, "%s,%ld",
123 reg_names
[REGNO (XEXP (x
, 0))], INTVAL (XEXP (x
, 1)));
126 output_addr_const (file
, XEXP (x
, 1));
127 fprintf (file
, "(%s)", reg_names
[REGNO (XEXP (x
, 0))]);
131 rtx plus
= XEXP (XEXP (x
, 1), 0);
132 if (GET_CODE (XEXP (plus
, 0)) == SYMBOL_REF
133 && CONST_INT_P (XEXP (plus
, 1)))
135 output_addr_const (file
, XEXP (plus
, 0));
136 fprintf (file
, "+%ld(%s)", INTVAL (XEXP (plus
, 1)),
137 reg_names
[REGNO (XEXP (x
, 0))]);
149 output_addr_const (file
, x
);
154 /* The PRINT_OPERAND worker. */
157 ft32_print_operand (FILE * file
, rtx x
, int code
)
161 /* New code entries should just be added to the switch below. If
162 handling is finished, just return. If handling was just a
163 modification of the operand, the modified operand should be put in
164 "operand", and then do a break to let default handling
165 (zero-modifier) output the operand. */
170 /* No code, print as usual. */
174 if (GET_CODE (operand
) != REG
)
175 internal_error ("'h' applied to non-register operand");
176 fprintf (file
, "%s", reg_names
[REGNO (operand
) + 1]);
180 fprintf (file
, "%ld", (long) (- INTVAL(x
)));
183 case 'd': // a DW spec, from an integer alignment (for BLKmode insns)
200 internal_error ("bad alignment: %d", i
);
205 fprintf (file
, "%c", dwspec
);
211 int bf
= ft32_as_bitfield (INTVAL (x
));
212 fprintf (file
, "512|(%d<<5)|%d", bf
>> 5, bf
& 31);
218 int bf
= ft32_as_bitfield (0xffffffff ^ INTVAL (x
));
219 fprintf (file
, "(%d<<5)|%d", bf
>> 5, bf
& 31);
225 ft32_print_operand (file
, XEXP (x
, 0), 0);
230 LOSE_AND_RETURN ("invalid operand modifier letter", x
);
233 /* Print an operand as without a modifier letter. */
234 switch (GET_CODE (operand
))
237 fprintf (file
, "%s", reg_names
[REGNO (operand
)]);
241 output_address (XEXP (operand
, 0));
245 /* No need to handle all strange variants, let output_addr_const
247 if (CONSTANT_P (operand
))
249 output_addr_const (file
, operand
);
253 LOSE_AND_RETURN ("unexpected operand", x
);
258 ft32_load_immediate (rtx dst
, int32_t i
)
262 if ((-524288 <= i
) && (i
<= 524287))
264 sprintf (pattern
, "ldk.l %%0,%d", i
);
265 output_asm_insn (pattern
, &dst
);
267 else if ((-536870912 <= i
) && (i
<= 536870911))
269 ft32_load_immediate (dst
, i
>> 10);
270 sprintf (pattern
, "ldl.l %%0,%%0,%d", i
& 1023);
271 output_asm_insn (pattern
, &dst
);
275 int rd
; // rotate distance
277 for (rd
= 1; rd
< 32; rd
++)
279 u
= ((u
>> 31) & 1) | (u
<< 1);
280 if ((-524288 <= (int32_t) u
) && ((int32_t) u
<= 524287))
282 ft32_load_immediate (dst
, (int32_t) u
);
283 sprintf (pattern
, "ror.l %%0,%%0,%d", rd
);
284 output_asm_insn (pattern
, &dst
);
288 ft32_load_immediate (dst
, i
>> 10);
289 sprintf (pattern
, "ldl.l %%0,%%0,%d", i
& 1023);
290 output_asm_insn (pattern
, &dst
);
296 // x is a bit mask, for example:
297 // 00000000000000000000001111111110
298 // If x contains a single bit mask, return the bitfield spec.
299 // in the above case it returns ((9 << 5) | 1)
300 // Otherwise return -1.
303 #define NBITS(n) ((1U << (n)) - 1U)
306 ft32_as_bitfield (unsigned int x
)
313 for (lobit
= 0; lobit
< 32; lobit
++)
314 if (x
& (1 << lobit
))
316 for (hibit
= 31; hibit
>= 0; hibit
--)
317 if (x
& (1 << hibit
))
320 int width
= 1 + hibit
- lobit
;
324 if (x
!= (NBITS (width
) << lobit
))
325 return -1; // not a clean bitfield
327 return ((width
& 15) << 5) | lobit
;
330 /* Per-function machine data. */
331 struct GTY (()) machine_function
333 /* Number of bytes saved on the stack for callee saved registers. */
334 int callee_saved_reg_size
;
336 /* Number of bytes saved on the stack for local variables. */
339 /* The sum of 2 sizes: locals vars and padding byte for saving the
340 * registers. Used in expand_prologue () and expand_epilogue (). */
341 int size_for_adjusting_sp
;
344 /* Zero initialization is OK for all current fields. */
346 static struct machine_function
*
347 ft32_init_machine_status (void)
349 return ggc_cleared_alloc
< machine_function
> ();
353 /* The TARGET_OPTION_OVERRIDE worker.
354 All this curently does is set init_machine_status. */
356 ft32_option_override (void)
358 /* Set the per-function-data initializer. */
359 init_machine_status
= ft32_init_machine_status
;
362 /* Implement targetm.select_section. */
364 ft32_select_section (tree decl
, int reloc
, unsigned HOST_WIDE_INT align
)
366 /* Variables and constants defined in the __ea address space
367 go into a special section named "._ea". */
368 if (TREE_TYPE (decl
) != error_mark_node
369 && TYPE_ADDR_SPACE (TREE_TYPE (decl
)) == ADDR_SPACE_PM
)
371 /* We might get called with string constants, but get_named_section
372 doesn't like them as they are not DECLs. Also, we need to set
373 flags in that case. */
375 return get_section ("._pm", SECTION_WRITE
| SECTION_DEBUG
, NULL
);
377 return get_named_section (decl
, "._pm", reloc
);
380 return default_elf_select_section (decl
, reloc
, align
);
383 /* Compute the size of the local area and the size to be adjusted by the
384 * prologue and epilogue. */
387 ft32_compute_frame (void)
389 /* For aligning the local variables. */
390 int stack_alignment
= STACK_BOUNDARY
/ BITS_PER_UNIT
;
394 /* Padding needed for each element of the frame. */
395 cfun
->machine
->local_vars_size
= get_frame_size ();
397 /* Align to the stack alignment. */
398 padding_locals
= cfun
->machine
->local_vars_size
% stack_alignment
;
400 padding_locals
= stack_alignment
- padding_locals
;
402 cfun
->machine
->local_vars_size
+= padding_locals
;
404 cfun
->machine
->callee_saved_reg_size
= 0;
406 /* Save callee-saved registers. */
407 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
408 if (df_regs_ever_live_p (regno
) && (!call_used_regs
[regno
]))
409 cfun
->machine
->callee_saved_reg_size
+= 4;
411 cfun
->machine
->size_for_adjusting_sp
=
412 crtl
->args
.pretend_args_size
413 + cfun
->machine
->local_vars_size
414 + (ACCUMULATE_OUTGOING_ARGS
? crtl
->outgoing_args_size
: 0);
417 // Must use LINK/UNLINK when...
418 // the frame is bigger than 512 bytes so cannot just "SUB" from SP
419 // the function actually uses $fp
424 int bigframe
= (cfun
->machine
->size_for_adjusting_sp
>= 512);
425 return (bigframe
|| frame_pointer_needed
|| df_regs_ever_live_p (FT32_FP
)
426 || df_regs_ever_live_p (FT32_FP
));
430 ft32_expand_prologue (void)
435 ft32_compute_frame ();
437 if (flag_stack_usage_info
)
438 current_function_static_stack_size
= cfun
->machine
->size_for_adjusting_sp
;
440 if (!must_link () && (cfun
->machine
->callee_saved_reg_size
== 4))
444 (gen_rtx_REG (Pmode
, FT32_R13
),
445 GEN_INT (-cfun
->machine
->size_for_adjusting_sp
)));
446 RTX_FRAME_RELATED_P (insn
) = 1;
449 /* Save callee-saved registers. */
452 for (regno
= FIRST_PSEUDO_REGISTER
; regno
-- > 0;)
454 if (!fixed_regs
[regno
] && !call_used_regs
[regno
]
455 && df_regs_ever_live_p (regno
))
457 rtx preg
= gen_rtx_REG (Pmode
, regno
);
458 emit_insn (gen_call_prolog (preg
));
465 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
467 if (!fixed_regs
[regno
] && df_regs_ever_live_p (regno
)
468 && !call_used_regs
[regno
])
470 insn
= emit_insn (gen_movsi_push (gen_rtx_REG (Pmode
, regno
)));
471 RTX_FRAME_RELATED_P (insn
) = 1;
476 if (65536 <= cfun
->machine
->size_for_adjusting_sp
)
478 error ("stack frame must be smaller than 64K");
485 (gen_rtx_REG (Pmode
, FT32_FP
),
486 GEN_INT (-cfun
->machine
->size_for_adjusting_sp
)));
487 RTX_FRAME_RELATED_P (insn
) = 1;
489 else if (cfun
->machine
->size_for_adjusting_sp
> 0)
491 insn
= emit_insn (gen_addsi3 (gen_rtx_REG (SImode
, FT32_SP
),
492 gen_rtx_REG (SImode
, FT32_SP
),
493 GEN_INT (-(cfun
->machine
->
494 size_for_adjusting_sp
))));
495 RTX_FRAME_RELATED_P (insn
) = 1;
500 ft32_expand_epilogue (void)
505 && (cfun
->machine
->size_for_adjusting_sp
== 24)
506 && (cfun
->machine
->callee_saved_reg_size
== 0))
508 emit_jump_insn (gen_returner24 ());
512 // Set when the epilog code will also add 24 to $sp
513 int epilog24
= (!must_link ()
514 && (cfun
->machine
->size_for_adjusting_sp
== 24)
519 emit_insn (gen_unlink ());
521 else if (!epilog24
&& (cfun
->machine
->size_for_adjusting_sp
> 0))
523 emit_insn (gen_addsi3 (gen_rtx_REG (SImode
, FT32_SP
),
524 gen_rtx_REG (SImode
, FT32_SP
),
525 GEN_INT (cfun
->machine
->size_for_adjusting_sp
)));
528 if (cfun
->machine
->callee_saved_reg_size
!= 0)
530 for (regno
= FIRST_PSEUDO_REGISTER
; regno
-- > 0;)
532 if (!fixed_regs
[regno
] && !call_used_regs
[regno
]
533 && df_regs_ever_live_p (regno
))
535 rtx preg
= gen_rtx_REG (Pmode
, regno
);
539 emit_insn (gen_jump_epilog24 (preg
));
541 emit_insn (gen_jump_epilog (preg
));
544 emit_insn (gen_movsi_pop (preg
));
549 emit_jump_insn (gen_returner ());
552 #undef TARGET_FRAME_POINTER_REQUIRED
553 #define TARGET_FRAME_POINTER_REQUIRED ft32_frame_pointer_required
555 ft32_frame_pointer_required (void)
557 return cfun
->calls_alloca
;
560 #undef TARGET_CAN_ELIMINATE
561 #define TARGET_CAN_ELIMINATE ft32_can_eliminate
563 /* Return true if register FROM can be eliminated via register TO. */
566 ft32_can_eliminate (const int from ATTRIBUTE_UNUSED
, const int to
)
569 return (to
== FRAME_POINTER_REGNUM
) || !ft32_frame_pointer_required ();
572 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
575 ft32_initial_elimination_offset (int from
, int to
)
577 ft32_compute_frame ();
579 if (from
== ARG_POINTER_REGNUM
&& to
== FRAME_POINTER_REGNUM
)
581 return cfun
->machine
->callee_saved_reg_size
+ 2 * UNITS_PER_WORD
;
584 if (from
== ARG_POINTER_REGNUM
&& to
== STACK_POINTER_REGNUM
)
587 arg_offset
= must_link ()? 2 : 1;
588 return ((cfun
->machine
->callee_saved_reg_size
589 + arg_offset
* UNITS_PER_WORD
)
590 + cfun
->machine
->size_for_adjusting_sp
);
593 if ((from
== FRAME_POINTER_REGNUM
) && (to
== STACK_POINTER_REGNUM
))
595 return cfun
->machine
->size_for_adjusting_sp
;
601 /* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
604 ft32_setup_incoming_varargs (cumulative_args_t cum_v
,
605 enum machine_mode mode ATTRIBUTE_UNUSED
,
606 tree type ATTRIBUTE_UNUSED
,
607 int *pretend_size
, int no_rtl
)
609 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
613 *pretend_size
= regs
< 0 ? 0 : GET_MODE_SIZE (SImode
) * regs
;
618 for (regno
= *cum
; regno
< 8; regno
++)
620 rtx reg
= gen_rtx_REG (SImode
, regno
);
621 rtx slot
= gen_rtx_PLUS (Pmode
,
622 gen_rtx_REG (SImode
, ARG_POINTER_REGNUM
),
623 GEN_INT (UNITS_PER_WORD
* (regno
- FT32_R0
)));
625 emit_move_insn (gen_rtx_MEM (SImode
, slot
), reg
);
630 /* Return the fixed registers used for condition codes. */
633 ft32_fixed_condition_code_regs (unsigned int *p1
, unsigned int *p2
)
636 *p2
= INVALID_REGNUM
;
640 /* Return the next register to be used to hold a function argument or
641 NULL_RTX if there's no more space. */
644 ft32_function_arg (cumulative_args_t cum_v
, enum machine_mode mode
,
645 const_tree type ATTRIBUTE_UNUSED
,
646 bool named ATTRIBUTE_UNUSED
)
648 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
651 return gen_rtx_REG (mode
, *cum
);
656 #define FT32_FUNCTION_ARG_SIZE(MODE, TYPE) \
657 ((MODE) != BLKmode ? GET_MODE_SIZE (MODE) \
658 : (unsigned) int_size_in_bytes (TYPE))
661 ft32_function_arg_advance (cumulative_args_t cum_v
, enum machine_mode mode
,
662 const_tree type
, bool named ATTRIBUTE_UNUSED
)
664 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
666 *cum
= (*cum
< FT32_R6
667 ? *cum
+ ((3 + FT32_FUNCTION_ARG_SIZE (mode
, type
)) / 4) : *cum
);
670 /* Return non-zero if the function argument described by TYPE is to be
671 passed by reference. */
674 ft32_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED
,
675 enum machine_mode mode
, const_tree type
,
676 bool named ATTRIBUTE_UNUSED
)
678 unsigned HOST_WIDE_INT size
;
682 if (AGGREGATE_TYPE_P (type
))
684 size
= int_size_in_bytes (type
);
687 size
= GET_MODE_SIZE (mode
);
692 /* Some function arguments will only partially fit in the registers
693 that hold arguments. Given a new arg, return the number of bytes
694 that fit in argument passing registers. */
697 ft32_arg_partial_bytes (cumulative_args_t cum_v
,
698 enum machine_mode mode
, tree type
, bool named
)
700 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
701 int bytes_left
, size
;
706 if (ft32_pass_by_reference (cum_v
, mode
, type
, named
))
710 if (AGGREGATE_TYPE_P (type
))
712 size
= int_size_in_bytes (type
);
715 size
= GET_MODE_SIZE (mode
);
717 bytes_left
= (4 * 6) - ((*cum
- 2) * 4);
719 if (size
> bytes_left
)
725 /* Used by constraints.md to distinguish between GENERIC and PM
729 ft32_is_mem_pm (rtx o
)
732 && !ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (o
)));
735 /* The Global `targetm' Variable. */
737 /* Initialize the GCC target structure. */
739 #undef TARGET_PROMOTE_PROTOTYPES
740 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
742 #undef TARGET_RETURN_IN_MEMORY
743 #define TARGET_RETURN_IN_MEMORY ft32_return_in_memory
744 #undef TARGET_MUST_PASS_IN_STACK
745 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
746 #undef TARGET_PASS_BY_REFERENCE
747 #define TARGET_PASS_BY_REFERENCE ft32_pass_by_reference
748 #undef TARGET_ARG_PARTIAL_BYTES
749 #define TARGET_ARG_PARTIAL_BYTES ft32_arg_partial_bytes
750 #undef TARGET_FUNCTION_ARG
751 #define TARGET_FUNCTION_ARG ft32_function_arg
752 #undef TARGET_FUNCTION_ARG_ADVANCE
753 #define TARGET_FUNCTION_ARG_ADVANCE ft32_function_arg_advance
756 #undef TARGET_SETUP_INCOMING_VARARGS
757 #define TARGET_SETUP_INCOMING_VARARGS ft32_setup_incoming_varargs
759 #undef TARGET_FIXED_CONDITION_CODE_REGS
760 #define TARGET_FIXED_CONDITION_CODE_REGS ft32_fixed_condition_code_regs
762 /* Define this to return an RTX representing the place where a
763 function returns or receives a value of data type RET_TYPE, a tree
764 node representing a data type. */
765 #undef TARGET_FUNCTION_VALUE
766 #define TARGET_FUNCTION_VALUE ft32_function_value
767 #undef TARGET_LIBCALL_VALUE
768 #define TARGET_LIBCALL_VALUE ft32_libcall_value
769 #undef TARGET_FUNCTION_VALUE_REGNO_P
770 #define TARGET_FUNCTION_VALUE_REGNO_P ft32_function_value_regno_p
772 #undef TARGET_OPTION_OVERRIDE
773 #define TARGET_OPTION_OVERRIDE ft32_option_override
775 #undef TARGET_ASM_SELECT_SECTION
776 #define TARGET_ASM_SELECT_SECTION ft32_select_section
778 #undef TARGET_VALID_POINTER_MODE
779 #define TARGET_VALID_POINTER_MODE ft32_valid_pointer_mode
781 ft32_valid_pointer_mode (enum machine_mode mode
)
788 #undef TARGET_ADDR_SPACE_POINTER_MODE
789 #define TARGET_ADDR_SPACE_POINTER_MODE ft32_addr_space_pointer_mode
790 static enum machine_mode
791 ft32_addr_space_pointer_mode (addr_space_t addrspace ATTRIBUTE_UNUSED
)
796 #undef TARGET_ADDR_SPACE_ADDRESS_MODE
797 #define TARGET_ADDR_SPACE_ADDRESS_MODE ft32_addr_space_address_mode
798 static enum machine_mode
799 ft32_addr_space_address_mode (addr_space_t addrspace ATTRIBUTE_UNUSED
)
804 #undef TARGET_ADDR_SPACE_SUBSET_P
805 #define TARGET_ADDR_SPACE_SUBSET_P ft32_addr_space_subset_p
807 ft32_addr_space_subset_p (addr_space_t subset ATTRIBUTE_UNUSED
,
808 addr_space_t superset ATTRIBUTE_UNUSED
)
813 #undef TARGET_CASE_VALUES_THRESHOLD
814 #define TARGET_CASE_VALUES_THRESHOLD ft32_target_case_values_threshold
817 ft32_target_case_values_threshold (void)
822 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
823 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P \
824 ft32_addr_space_legitimate_address_p
827 // Enabling LRA gives the infamous
828 // internal compiler error: Max. number of generated reload insns per insn is achieved (90)
829 // errors e.g. when compiling sieve.c
834 return ft32_lra_flag
;
838 #define TARGET_LRA_P ft32_lra_p
841 reg_ok_for_base_p (rtx r
, bool strict
)
845 return (HARD_REGNO_OK_FOR_BASE_P (NUM
)
846 || HARD_REGNO_OK_FOR_BASE_P (reg_renumber
[(NUM
)]));
848 return ((NUM
) >= FIRST_PSEUDO_REGISTER
|| HARD_REGNO_OK_FOR_BASE_P (NUM
));
852 ft32_addr_space_legitimate_address_p (enum machine_mode mode
, rtx x
,
854 addr_space_t as ATTRIBUTE_UNUSED
)
858 if (GET_CODE (x
) == PLUS
)
863 if (GET_CODE (op1
) == REG
865 && INTVAL (op2
) >= -128
866 && INTVAL (op2
) < 128 && reg_ok_for_base_p (op1
, strict
))
868 if (GET_CODE (op1
) == SYMBOL_REF
&& CONST_INT_P (op2
))
871 if (REG_P (x
) && reg_ok_for_base_p (x
, strict
))
873 if (GET_CODE (x
) == SYMBOL_REF
874 || GET_CODE (x
) == LABEL_REF
|| CONST_INT_P (x
))
879 if (REG_P (x
) && reg_ok_for_base_p (x
, strict
))
888 struct gcc_target targetm
= TARGET_INITIALIZER
;