1 /* Output routines for GCC for CRX.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
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 /*****************************************************************************/
23 /*****************************************************************************/
27 #include "coretypes.h"
33 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "conditions.h"
38 #include "insn-codes.h"
39 #include "insn-attr.h"
47 #include "basic-block.h"
49 #include "target-def.h"
51 /*****************************************************************************/
53 /*****************************************************************************/
55 /* Maximum number of register used for passing parameters. */
56 #define MAX_REG_FOR_PASSING_ARGS 6
58 /* Minimum number register used for passing parameters. */
59 #define MIN_REG_FOR_PASSING_ARGS 2
61 /* The maximum count of words supported in the assembly of the architecture in
62 * a push/pop instruction. */
65 /* Predicate is true if the current function is a 'noreturn' function, i.e. it
66 * is qualified as volatile. */
67 #define FUNC_IS_NORETURN_P(decl) (TREE_THIS_VOLATILE (decl))
69 /* The following macros are used in crx_decompose_address () */
71 /* Returns the factor of a scaled index address or -1 if invalid. */
72 #define SCALE_FOR_INDEX_P(X) \
73 (GET_CODE (X) == CONST_INT ? \
74 (INTVAL (X) == 1 ? 1 : \
75 INTVAL (X) == 2 ? 2 : \
76 INTVAL (X) == 4 ? 4 : \
77 INTVAL (X) == 8 ? 8 : \
81 /* Nonzero if the rtx X is a signed const int of n bits */
82 #define RTX_SIGNED_INT_FITS_N_BITS(X,n) \
83 ((GET_CODE (X) == CONST_INT \
84 && SIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
86 /* Nonzero if the rtx X is an unsigned const int of n bits. */
87 #define RTX_UNSIGNED_INT_FITS_N_BITS(X, n) \
88 ((GET_CODE (X) == CONST_INT \
89 && UNSIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
91 /*****************************************************************************/
92 /* STATIC VARIABLES */
93 /*****************************************************************************/
95 /* Nonzero if the last param processed is passed in a register. */
96 static int last_parm_in_reg
;
98 /* Will hold the number of the last register the prologue saves, -1 if no
99 * register is saved. */
100 static int last_reg_to_save
;
102 /* Each object in the array is a register number. Mark 1 for registers that
103 * need to be saved. */
104 static int save_regs
[FIRST_PSEUDO_REGISTER
];
106 /* Number of bytes saved on the stack for non-scratch registers */
107 static int sum_regs
= 0;
109 /* Number of bytes saved on the stack for local variables. */
110 static int local_vars_size
;
112 /* The sum of 2 sizes: locals vars and padding byte for saving the registers.
113 * Used in expand_prologue () and expand_epilogue (). */
114 static int size_for_adjusting_sp
;
116 /* In case of a POST_INC or POST_DEC memory reference, we must report the mode
117 * of the memory reference from PRINT_OPERAND to PRINT_OPERAND_ADDRESS. */
118 static enum machine_mode output_memory_reference_mode
;
120 /*****************************************************************************/
121 /* GLOBAL VARIABLES */
122 /*****************************************************************************/
124 /* Table of machine attributes. */
125 const struct attribute_spec crx_attribute_table
[];
127 /* Test and compare insns use these globals to generate branch insns. */
128 rtx crx_compare_op0
= NULL_RTX
;
129 rtx crx_compare_op1
= NULL_RTX
;
131 /*****************************************************************************/
132 /* TARGETM FUNCTION PROTOTYPES */
133 /*****************************************************************************/
135 static bool crx_fixed_condition_code_regs (unsigned int *, unsigned int *);
136 static rtx
crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED
,
137 int incoming ATTRIBUTE_UNUSED
);
138 static bool crx_return_in_memory (const_tree type
, const_tree fntype ATTRIBUTE_UNUSED
);
139 static int crx_address_cost (rtx
);
141 /*****************************************************************************/
142 /* STACK LAYOUT AND CALLING CONVENTIONS */
143 /*****************************************************************************/
145 #undef TARGET_FIXED_CONDITION_CODE_REGS
146 #define TARGET_FIXED_CONDITION_CODE_REGS crx_fixed_condition_code_regs
148 #undef TARGET_STRUCT_VALUE_RTX
149 #define TARGET_STRUCT_VALUE_RTX crx_struct_value_rtx
151 #undef TARGET_RETURN_IN_MEMORY
152 #define TARGET_RETURN_IN_MEMORY crx_return_in_memory
154 /*****************************************************************************/
155 /* RELATIVE COSTS OF OPERATIONS */
156 /*****************************************************************************/
158 #undef TARGET_ADDRESS_COST
159 #define TARGET_ADDRESS_COST crx_address_cost
161 /*****************************************************************************/
162 /* TARGET-SPECIFIC USES OF `__attribute__' */
163 /*****************************************************************************/
165 #undef TARGET_ATTRIBUTE_TABLE
166 #define TARGET_ATTRIBUTE_TABLE crx_attribute_table
168 const struct attribute_spec crx_attribute_table
[] = {
169 /* ISRs have special prologue and epilogue requirements. */
170 {"interrupt", 0, 0, false, true, true, NULL
},
171 {NULL
, 0, 0, false, false, false, NULL
}
175 /* Initialize 'targetm' variable which contains pointers to functions and data
176 * relating to the target machine. */
178 struct gcc_target targetm
= TARGET_INITIALIZER
;
181 /*****************************************************************************/
182 /* TARGET HOOK IMPLEMENTATIONS */
183 /*****************************************************************************/
185 /* Return the fixed registers used for condition codes. */
188 crx_fixed_condition_code_regs (unsigned int *p1
, unsigned int *p2
)
191 *p2
= INVALID_REGNUM
;
195 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
198 crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED
,
199 int incoming ATTRIBUTE_UNUSED
)
201 return gen_rtx_REG (Pmode
, CRX_STRUCT_VALUE_REGNUM
);
204 /* Implements hook TARGET_RETURN_IN_MEMORY. */
207 crx_return_in_memory (const_tree type
, const_tree fntype ATTRIBUTE_UNUSED
)
209 if (TYPE_MODE (type
) == BLKmode
)
211 HOST_WIDE_INT size
= int_size_in_bytes (type
);
212 return (size
== -1 || size
> 8);
219 /*****************************************************************************/
220 /* MACRO IMPLEMENTATIONS */
221 /*****************************************************************************/
223 /* STACK LAYOUT AND CALLING CONVENTIONS ROUTINES */
224 /* --------------------------------------------- */
226 /* Return nonzero if the current function being compiled is an interrupt
227 * function as specified by the "interrupt" attribute. */
230 crx_interrupt_function_p (void)
234 attributes
= TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl
));
235 return lookup_attribute ("interrupt", attributes
) != NULL_TREE
;
238 /* Compute values for the array save_regs and the variable sum_regs. The index
239 * of save_regs is numbers of register, each will get 1 if we need to save it
240 * in the current function, 0 if not. sum_regs is the total sum of the
241 * registers being saved. */
244 crx_compute_save_regs (void)
248 /* initialize here so in case the function is no-return it will be -1. */
249 last_reg_to_save
= -1;
251 /* No need to save any registers if the function never returns. */
252 if (FUNC_IS_NORETURN_P (current_function_decl
))
255 /* Initialize the number of bytes to be saved. */
258 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
260 if (fixed_regs
[regno
])
262 save_regs
[regno
] = 0;
266 /* If this reg is used and not call-used (except RA), save it. */
267 if (crx_interrupt_function_p ())
269 if (!current_function_is_leaf
&& call_used_regs
[regno
])
270 /* this is a volatile reg in a non-leaf interrupt routine - save it
271 * for the sake of its sons. */
272 save_regs
[regno
] = 1;
274 else if (df_regs_ever_live_p (regno
))
275 /* This reg is used - save it. */
276 save_regs
[regno
] = 1;
278 /* This reg is not used, and is not a volatile - don't save. */
279 save_regs
[regno
] = 0;
283 /* If this reg is used and not call-used (except RA), save it. */
284 if (df_regs_ever_live_p (regno
)
285 && (!call_used_regs
[regno
] || regno
== RETURN_ADDRESS_REGNUM
))
286 save_regs
[regno
] = 1;
288 save_regs
[regno
] = 0;
292 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
293 if (save_regs
[regno
] == 1)
295 last_reg_to_save
= regno
;
296 sum_regs
+= UNITS_PER_WORD
;
300 /* Compute the size of the local area and the size to be adjusted by the
301 * prologue and epilogue. */
304 crx_compute_frame (void)
306 /* For aligning the local variables. */
307 int stack_alignment
= STACK_BOUNDARY
/ BITS_PER_UNIT
;
310 /* Padding needed for each element of the frame. */
311 local_vars_size
= get_frame_size ();
313 /* Align to the stack alignment. */
314 padding_locals
= local_vars_size
% stack_alignment
;
316 padding_locals
= stack_alignment
- padding_locals
;
318 local_vars_size
+= padding_locals
;
320 size_for_adjusting_sp
= local_vars_size
+ (ACCUMULATE_OUTGOING_ARGS
?
321 crtl
->outgoing_args_size
: 0);
324 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
327 crx_initial_elimination_offset (int from
, int to
)
329 /* Compute this since we need to use sum_regs. */
330 crx_compute_save_regs ();
332 /* Compute this since we need to use local_vars_size. */
333 crx_compute_frame ();
335 if ((from
) == FRAME_POINTER_REGNUM
&& (to
) == STACK_POINTER_REGNUM
)
336 return (ACCUMULATE_OUTGOING_ARGS
?
337 crtl
->outgoing_args_size
: 0);
338 else if ((from
) == ARG_POINTER_REGNUM
&& (to
) == FRAME_POINTER_REGNUM
)
339 return (sum_regs
+ local_vars_size
);
340 else if ((from
) == ARG_POINTER_REGNUM
&& (to
) == STACK_POINTER_REGNUM
)
341 return (sum_regs
+ local_vars_size
+
342 (ACCUMULATE_OUTGOING_ARGS
?
343 crtl
->outgoing_args_size
: 0));
351 /* Return the class number of the smallest class containing reg number REGNO.
352 * This could be a conditional expression or could index an array. */
355 crx_regno_reg_class (int regno
)
357 if (regno
>= 0 && regno
< SP_REGNUM
)
360 if (regno
== SP_REGNUM
)
363 if (regno
== LO_REGNUM
)
365 if (regno
== HI_REGNUM
)
371 /* Transfer between HILO_REGS and memory via secondary reloading. */
374 crx_secondary_reload_class (enum reg_class
class,
375 enum machine_mode mode ATTRIBUTE_UNUSED
,
376 rtx x ATTRIBUTE_UNUSED
)
378 if (reg_classes_intersect_p (class, HILO_REGS
)
379 && true_regnum (x
) == -1)
385 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
388 crx_hard_regno_mode_ok (int regno
, enum machine_mode mode
)
390 /* CC can only hold CCmode values. */
391 if (regno
== CC_REGNUM
)
392 return GET_MODE_CLASS (mode
) == MODE_CC
;
393 if (GET_MODE_CLASS (mode
) == MODE_CC
)
395 /* HILO registers can only hold SImode and DImode */
396 if (HILO_REGNO_P (regno
))
397 return mode
== SImode
|| mode
== DImode
;
401 /* PASSING FUNCTION ARGUMENTS */
402 /* -------------------------- */
404 /* If enough param regs are available for passing the param of type TYPE return
405 * the number of registers needed else 0. */
408 enough_regs_for_param (CUMULATIVE_ARGS
* cum
, tree type
,
409 enum machine_mode mode
)
415 type_size
= GET_MODE_BITSIZE (mode
);
417 type_size
= int_size_in_bytes (type
) * BITS_PER_UNIT
;
420 BITS_PER_WORD
* (MAX_REG_FOR_PASSING_ARGS
-
421 (MIN_REG_FOR_PASSING_ARGS
+ cum
->ints
) + 1);
423 /* Any variable which is too big to pass in two registers, will pass on
425 if ((remaining_size
>= type_size
) && (type_size
<= 2 * BITS_PER_WORD
))
426 return (type_size
+ BITS_PER_WORD
- 1) / BITS_PER_WORD
;
431 /* Implements the macro FUNCTION_ARG defined in crx.h. */
434 crx_function_arg (CUMULATIVE_ARGS
* cum
, enum machine_mode mode
, tree type
,
435 int named ATTRIBUTE_UNUSED
)
437 last_parm_in_reg
= 0;
439 /* Function_arg () is called with this type just after all the args have had
440 * their registers assigned. The rtx that function_arg returns from this type
441 * is supposed to pass to 'gen_call' but currently it is not implemented (see
442 * macro GEN_CALL). */
443 if (type
== void_type_node
)
446 if (targetm
.calls
.must_pass_in_stack (mode
, type
) || (cum
->ints
< 0))
451 /* Enable structures that need padding bytes at the end to pass to a
452 * function in registers. */
453 if (enough_regs_for_param (cum
, type
, mode
) != 0)
455 last_parm_in_reg
= 1;
456 return gen_rtx_REG (mode
, MIN_REG_FOR_PASSING_ARGS
+ cum
->ints
);
460 if (MIN_REG_FOR_PASSING_ARGS
+ cum
->ints
> MAX_REG_FOR_PASSING_ARGS
)
464 if (enough_regs_for_param (cum
, type
, mode
) != 0)
466 last_parm_in_reg
= 1;
467 return gen_rtx_REG (mode
, MIN_REG_FOR_PASSING_ARGS
+ cum
->ints
);
474 /* Implements the macro INIT_CUMULATIVE_ARGS defined in crx.h. */
477 crx_init_cumulative_args (CUMULATIVE_ARGS
* cum
, tree fntype
,
478 rtx libfunc ATTRIBUTE_UNUSED
)
480 tree param
, next_param
;
484 /* Determine if this function has variable arguments. This is indicated by
485 * the last argument being 'void_type_mode' if there are no variable
486 * arguments. Change here for a different vararg. */
487 for (param
= (fntype
) ? TYPE_ARG_TYPES (fntype
) : 0;
488 param
!= (tree
) 0; param
= next_param
)
490 next_param
= TREE_CHAIN (param
);
491 if (next_param
== (tree
) 0 && TREE_VALUE (param
) != void_type_node
)
499 /* Implements the macro FUNCTION_ARG_ADVANCE defined in crx.h. */
502 crx_function_arg_advance (CUMULATIVE_ARGS
* cum
, enum machine_mode mode
,
503 tree type
, int named ATTRIBUTE_UNUSED
)
505 /* l holds the number of registers required */
506 int l
= GET_MODE_BITSIZE (mode
) / BITS_PER_WORD
;
508 /* If the parameter isn't passed on a register don't advance cum. */
509 if (!last_parm_in_reg
)
512 if (targetm
.calls
.must_pass_in_stack (mode
, type
) || (cum
->ints
< 0))
515 if (mode
== SImode
|| mode
== HImode
|| mode
== QImode
|| mode
== DImode
)
522 else if (mode
== SFmode
|| mode
== DFmode
)
524 else if ((mode
) == BLKmode
)
526 if ((l
= enough_regs_for_param (cum
, type
, mode
)) != 0)
532 /* Implements the macro FUNCTION_ARG_REGNO_P defined in crx.h. Return nonzero
533 * if N is a register used for passing parameters. */
536 crx_function_arg_regno_p (int n
)
538 return (n
<= MAX_REG_FOR_PASSING_ARGS
&& n
>= MIN_REG_FOR_PASSING_ARGS
);
541 /* ADDRESSING MODES */
542 /* ---------------- */
544 /* Implements the macro GO_IF_LEGITIMATE_ADDRESS defined in crx.h.
545 * The following addressing modes are supported on CRX:
547 * Relocations --> const | symbol_ref | label_ref
548 * Absolute address --> 32-bit absolute
549 * Post increment --> reg + 12-bit disp.
550 * Post modify --> reg + 12-bit disp.
551 * Register relative --> reg | 32-bit disp. + reg | 4 bit + reg
552 * Scaled index --> reg + reg | 22-bit disp. + reg + reg |
553 * 22-disp. + reg + reg + (2 | 4 | 8) */
555 static int crx_addr_reg_p (rtx addr_reg
)
559 if (REG_P (addr_reg
))
563 else if ((GET_CODE (addr_reg
) == SUBREG
564 && REG_P (SUBREG_REG (addr_reg
))
565 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg
)))
568 reg
= SUBREG_REG (addr_reg
);
573 if (GET_MODE (addr_reg
) != Pmode
)
582 crx_decompose_address (rtx addr
, struct crx_address
*out
)
584 rtx base
= NULL_RTX
, index
= NULL_RTX
, disp
= NULL_RTX
;
585 rtx scale_rtx
= NULL_RTX
, side_effect
= NULL_RTX
;
588 enum crx_addrtype retval
= CRX_INVALID
;
590 switch (GET_CODE (addr
))
593 /* Absolute address (known at compile time) */
594 retval
= CRX_ABSOLUTE
;
596 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp
), GET_MODE_BITSIZE (Pmode
)))
603 /* Absolute address (known at link time) */
604 retval
= CRX_ABSOLUTE
;
610 /* Register relative address */
611 retval
= CRX_REG_REL
;
616 switch (GET_CODE (XEXP (addr
, 0)))
620 if (REG_P (XEXP (addr
, 1)))
622 /* Scaled index with scale = 1 and disp. = 0 */
623 retval
= CRX_SCALED_INDX
;
624 base
= XEXP (addr
, 1);
625 index
= XEXP (addr
, 0);
628 else if (RTX_SIGNED_INT_FITS_N_BITS (XEXP (addr
, 1), 28))
630 /* Register relative address and <= 28-bit disp. */
631 retval
= CRX_REG_REL
;
632 base
= XEXP (addr
, 0);
633 disp
= XEXP (addr
, 1);
640 /* Scaled index and <= 22-bit disp. */
641 retval
= CRX_SCALED_INDX
;
642 base
= XEXP (XEXP (addr
, 0), 1);
643 disp
= XEXP (addr
, 1);
644 if (!RTX_SIGNED_INT_FITS_N_BITS (disp
, 22))
646 switch (GET_CODE (XEXP (XEXP (addr
, 0), 0)))
649 /* Scaled index with scale = 0 and <= 22-bit disp. */
650 index
= XEXP (XEXP (addr
, 0), 0);
655 /* Scaled index with scale >= 0 and <= 22-bit disp. */
656 index
= XEXP (XEXP (XEXP (addr
, 0), 0), 0);
657 scale_rtx
= XEXP (XEXP (XEXP (addr
, 0), 0), 1);
658 if ((scale
= SCALE_FOR_INDEX_P (scale_rtx
)) == -1)
668 /* Scaled index with scale >= 0 */
669 retval
= CRX_SCALED_INDX
;
670 base
= XEXP (addr
, 1);
671 index
= XEXP (XEXP (addr
, 0), 0);
672 scale_rtx
= XEXP (XEXP (addr
, 0), 1);
673 /* Scaled index with scale >= 0 and <= 22-bit disp. */
674 if ((scale
= SCALE_FOR_INDEX_P (scale_rtx
)) == -1)
685 /* Simple post-increment */
686 retval
= CRX_POST_INC
;
687 base
= XEXP (addr
, 0);
692 /* Generic post-increment with <= 12-bit disp. */
693 retval
= CRX_POST_INC
;
694 base
= XEXP (addr
, 0);
695 side_effect
= XEXP (addr
, 1);
696 if (base
!= XEXP (side_effect
, 0))
698 switch (GET_CODE (side_effect
))
702 disp
= XEXP (side_effect
, 1);
703 if (!RTX_SIGNED_INT_FITS_N_BITS (disp
, 12))
708 /* CRX only supports PLUS and MINUS */
717 if (base
&& !crx_addr_reg_p (base
)) return CRX_INVALID
;
718 if (index
&& !crx_addr_reg_p (index
)) return CRX_INVALID
;
724 out
->side_effect
= side_effect
;
730 crx_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED
,
731 rtx addr
, int strict
)
733 enum crx_addrtype addrtype
;
734 struct crx_address address
;
736 if (TARGET_DEBUG_ADDR
)
739 "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
740 GET_MODE_NAME (mode
), strict
);
744 addrtype
= crx_decompose_address (addr
, &address
);
746 if (addrtype
== CRX_POST_INC
&& GET_MODE_SIZE (mode
) > UNITS_PER_WORD
)
749 if (TARGET_DEBUG_ADDR
)
758 typestr
= "Register relative";
761 typestr
= "Post-increment";
763 case CRX_SCALED_INDX
:
764 typestr
= "Scaled index";
767 typestr
= "Absolute";
772 fprintf (stderr
, "CRX Address type: %s\n", typestr
);
775 if (addrtype
== CRX_INVALID
)
780 if (address
.base
&& !REGNO_OK_FOR_BASE_P (REGNO (address
.base
)))
782 if (TARGET_DEBUG_ADDR
)
783 fprintf (stderr
, "Base register not strict\n");
786 if (address
.index
&& !REGNO_OK_FOR_INDEX_P (REGNO (address
.index
)))
788 if (TARGET_DEBUG_ADDR
)
789 fprintf (stderr
, "Index register not strict\n");
797 /* ROUTINES TO COMPUTE COSTS */
798 /* ------------------------- */
800 /* Return cost of the memory address x. */
803 crx_address_cost (rtx addr
)
805 enum crx_addrtype addrtype
;
806 struct crx_address address
;
810 addrtype
= crx_decompose_address (addr
, &address
);
812 gcc_assert (addrtype
!= CRX_INVALID
);
814 /* An absolute address causes a 3-word instruction */
815 if (addrtype
== CRX_ABSOLUTE
)
818 /* Post-modifying addresses are more powerful. */
819 if (addrtype
== CRX_POST_INC
)
822 /* Attempt to minimize number of registers in the address. */
826 if (address
.index
&& address
.scale
== 1)
829 if (address
.disp
&& !INT_CST4 (INTVAL (address
.disp
)))
832 if (TARGET_DEBUG_ADDR
)
834 fprintf (stderr
, "\n======\nTARGET_ADDRESS_COST = %d\n", cost
);
841 /* Return the cost of moving data of mode MODE between a register of class
842 * CLASS and memory; IN is zero if the value is to be written to memory,
843 * nonzero if it is to be read in. This cost is relative to those in
844 * REGISTER_MOVE_COST. */
847 crx_memory_move_cost (enum machine_mode mode
,
848 enum reg_class
class ATTRIBUTE_UNUSED
,
849 int in ATTRIBUTE_UNUSED
)
851 /* One LD or ST takes twice the time of a simple reg-reg move */
852 if (reg_classes_intersect_p (class, GENERAL_REGS
))
854 /* printf ("GENERAL_REGS LD/ST = %d\n", 4 * HARD_REGNO_NREGS (0, mode));*/
855 return 4 * HARD_REGNO_NREGS (0, mode
);
857 else if (reg_classes_intersect_p (class, HILO_REGS
))
859 /* HILO to memory and vice versa */
860 /* printf ("HILO_REGS %s = %d\n", in ? "LD" : "ST",
861 (REGISTER_MOVE_COST (mode,
862 in ? GENERAL_REGS : HILO_REGS,
863 in ? HILO_REGS : GENERAL_REGS) + 4)
864 * HARD_REGNO_NREGS (0, mode)); */
865 return (REGISTER_MOVE_COST (mode
,
866 in
? GENERAL_REGS
: HILO_REGS
,
867 in
? HILO_REGS
: GENERAL_REGS
) + 4)
868 * HARD_REGNO_NREGS (0, mode
);
870 else /* default (like in i386) */
872 /* printf ("ANYREGS = 100\n"); */
877 /* INSTRUCTION OUTPUT */
878 /* ------------------ */
880 /* Check if a const_double is ok for crx store-immediate instructions */
883 crx_const_double_ok (rtx op
)
885 if (GET_MODE (op
) == DFmode
)
889 REAL_VALUE_FROM_CONST_DOUBLE (r
, op
);
890 REAL_VALUE_TO_TARGET_DOUBLE (r
, l
);
891 return (UNSIGNED_INT_FITS_N_BITS (l
[0], 4) &&
892 UNSIGNED_INT_FITS_N_BITS (l
[1], 4)) ? 1 : 0;
895 if (GET_MODE (op
) == SFmode
)
899 REAL_VALUE_FROM_CONST_DOUBLE (r
, op
);
900 REAL_VALUE_TO_TARGET_SINGLE (r
, l
);
901 return UNSIGNED_INT_FITS_N_BITS (l
, 4) ? 1 : 0;
904 return (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op
), 4) &&
905 UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op
), 4)) ? 1 : 0;
908 /* Implements the macro PRINT_OPERAND defined in crx.h. */
911 crx_print_operand (FILE * file
, rtx x
, int code
)
916 if (GET_CODE (x
) == REG
) {
917 if (GET_MODE (x
) == DImode
|| GET_MODE (x
) == DFmode
)
919 int regno
= REGNO (x
);
920 if (regno
+ 1 >= SP_REGNUM
) abort ();
921 fprintf (file
, "{%s, %s}", reg_names
[regno
], reg_names
[regno
+ 1]);
926 if (REGNO (x
) >= SP_REGNUM
) abort ();
927 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
934 const char *crx_cmp_str
;
935 switch (GET_CODE (x
))
936 { /* MD: compare (reg, reg or imm) but CRX: cmp (reg or imm, reg)
937 * -> swap all non symmetric ops */
938 case EQ
: crx_cmp_str
= "eq"; break;
939 case NE
: crx_cmp_str
= "ne"; break;
940 case GT
: crx_cmp_str
= "lt"; break;
941 case GTU
: crx_cmp_str
= "lo"; break;
942 case LT
: crx_cmp_str
= "gt"; break;
943 case LTU
: crx_cmp_str
= "hi"; break;
944 case GE
: crx_cmp_str
= "le"; break;
945 case GEU
: crx_cmp_str
= "ls"; break;
946 case LE
: crx_cmp_str
= "ge"; break;
947 case LEU
: crx_cmp_str
= "hs"; break;
950 fprintf (file
, "%s", crx_cmp_str
);
955 /* Print high part of a double precision value. */
956 switch (GET_CODE (x
))
959 if (GET_MODE (x
) == SFmode
) abort ();
960 if (GET_MODE (x
) == DFmode
)
962 /* High part of a DF const. */
966 REAL_VALUE_FROM_CONST_DOUBLE (r
, x
);
967 REAL_VALUE_TO_TARGET_DOUBLE (r
, l
);
969 fprintf (file
, "$0x%lx", l
[1]);
973 /* -- Fallthrough to handle DI consts -- */
978 split_double (x
, &low
, &high
);
980 output_addr_const (file
, high
);
985 if (REGNO (x
) + 1 >= FIRST_PSEUDO_REGISTER
) abort ();
986 fprintf (file
, "%s", reg_names
[REGNO (x
) + 1]);
990 /* Adjust memory address to high part. */
993 adj_mem
= adjust_address (adj_mem
, GET_MODE (adj_mem
), 4);
995 output_memory_reference_mode
= GET_MODE (adj_mem
);
996 output_address (XEXP (adj_mem
, 0));
1005 /* Print low part of a double precision value. */
1006 switch (GET_CODE (x
))
1009 if (GET_MODE (x
) == SFmode
) abort ();
1010 if (GET_MODE (x
) == DFmode
)
1012 /* High part of a DF const. */
1016 REAL_VALUE_FROM_CONST_DOUBLE (r
, x
);
1017 REAL_VALUE_TO_TARGET_DOUBLE (r
, l
);
1019 fprintf (file
, "$0x%lx", l
[0]);
1023 /* -- Fallthrough to handle DI consts -- */
1028 split_double (x
, &low
, &high
);
1030 output_addr_const (file
, low
);
1035 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
1039 output_memory_reference_mode
= GET_MODE (x
);
1040 output_address (XEXP (x
, 0));
1047 case 0 : /* default */
1048 switch (GET_CODE (x
))
1051 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
1055 output_memory_reference_mode
= GET_MODE (x
);
1056 output_address (XEXP (x
, 0));
1064 /* Always use H and L for double precision - see above */
1065 gcc_assert (GET_MODE (x
) == SFmode
);
1067 REAL_VALUE_FROM_CONST_DOUBLE (r
, x
);
1068 REAL_VALUE_TO_TARGET_SINGLE (r
, l
);
1070 fprintf (file
, "$0x%lx", l
);
1076 output_addr_const (file
, x
);
1081 output_operand_lossage ("invalid %%xn code");
1087 /* Implements the macro PRINT_OPERAND_ADDRESS defined in crx.h. */
1090 crx_print_operand_address (FILE * file
, rtx addr
)
1092 enum crx_addrtype addrtype
;
1093 struct crx_address address
;
1097 addrtype
= crx_decompose_address (addr
, &address
);
1100 offset
= INTVAL (address
.disp
);
1107 fprintf (file
, "%d(%s)", offset
, reg_names
[REGNO (address
.base
)]);
1111 switch (GET_CODE (address
.side_effect
))
1119 offset
= GET_MODE_SIZE (output_memory_reference_mode
);
1122 offset
= -GET_MODE_SIZE (output_memory_reference_mode
);
1127 fprintf (file
, "%d(%s)+", offset
, reg_names
[REGNO (address
.base
)]);
1130 case CRX_SCALED_INDX
:
1131 fprintf (file
, "%d(%s, %s, %d)", offset
, reg_names
[REGNO (address
.base
)],
1132 reg_names
[REGNO (address
.index
)], address
.scale
);
1136 output_addr_const (file
, address
.disp
);
1145 /*****************************************************************************/
1146 /* MACHINE DESCRIPTION HELPER-FUNCTIONS */
1147 /*****************************************************************************/
1149 void crx_expand_movmem_single (rtx src
, rtx srcbase
, rtx dst
, rtx dstbase
,
1150 rtx tmp_reg
, unsigned HOST_WIDE_INT
*offset_p
)
1153 unsigned HOST_WIDE_INT offset
= *offset_p
;
1156 addr
= plus_constant (src
, offset
);
1157 mem
= adjust_automodify_address (srcbase
, SImode
, addr
, offset
);
1158 emit_move_insn (tmp_reg
, mem
);
1161 addr
= plus_constant (dst
, offset
);
1162 mem
= adjust_automodify_address (dstbase
, SImode
, addr
, offset
);
1163 emit_move_insn (mem
, tmp_reg
);
1165 *offset_p
= offset
+ 4;
1169 crx_expand_movmem (rtx dstbase
, rtx srcbase
, rtx count_exp
, rtx align_exp
)
1171 unsigned HOST_WIDE_INT count
= 0, offset
, si_moves
, i
;
1172 HOST_WIDE_INT align
= 0;
1177 if (GET_CODE (align_exp
) == CONST_INT
)
1178 { /* Only if aligned */
1179 align
= INTVAL (align_exp
);
1184 if (GET_CODE (count_exp
) == CONST_INT
)
1185 { /* No more than 16 SImode moves */
1186 count
= INTVAL (count_exp
);
1191 tmp_reg
= gen_reg_rtx (SImode
);
1193 /* Create psrs for the src and dest pointers */
1194 dst
= copy_to_mode_reg (Pmode
, XEXP (dstbase
, 0));
1195 if (dst
!= XEXP (dstbase
, 0))
1196 dstbase
= replace_equiv_address_nv (dstbase
, dst
);
1197 src
= copy_to_mode_reg (Pmode
, XEXP (srcbase
, 0));
1198 if (src
!= XEXP (srcbase
, 0))
1199 srcbase
= replace_equiv_address_nv (srcbase
, src
);
1203 /* Emit SImode moves */
1204 si_moves
= count
>> 2;
1205 for (i
= 0; i
< si_moves
; i
++)
1206 crx_expand_movmem_single (src
, srcbase
, dst
, dstbase
, tmp_reg
, &offset
);
1212 crx_expand_movmem_single (src
, srcbase
, dst
, dstbase
, tmp_reg
, &offset
);
1215 gcc_assert (offset
== count
);
1221 crx_expand_compare (enum rtx_code code
, enum machine_mode mode
)
1223 rtx op0
, op1
, cc_reg
, ret
;
1225 op0
= crx_compare_op0
;
1226 op1
= crx_compare_op1
;
1228 /* Emit the compare that writes into CC_REGNUM) */
1229 cc_reg
= gen_rtx_REG (CCmode
, CC_REGNUM
);
1230 ret
= gen_rtx_COMPARE (CCmode
, op0
, op1
);
1231 emit_insn (gen_rtx_SET (VOIDmode
, cc_reg
, ret
));
1232 /* debug_rtx (get_last_insn ()); */
1234 /* Return the rtx for using the result in CC_REGNUM */
1235 return gen_rtx_fmt_ee (code
, mode
, cc_reg
, const0_rtx
);
1239 crx_expand_branch (enum rtx_code code
, rtx label
)
1241 rtx tmp
= crx_expand_compare (code
, VOIDmode
);
1242 tmp
= gen_rtx_IF_THEN_ELSE (VOIDmode
, tmp
,
1243 gen_rtx_LABEL_REF (VOIDmode
, label
),
1245 emit_jump_insn (gen_rtx_SET (VOIDmode
, pc_rtx
, tmp
));
1246 /* debug_rtx (get_last_insn ()); */
1250 crx_expand_scond (enum rtx_code code
, rtx dest
)
1252 rtx tmp
= crx_expand_compare (code
, GET_MODE (dest
));
1253 emit_move_insn (dest
, tmp
);
1254 /* debug_rtx (get_last_insn ()); */
1258 mpushpop_str (char *stringbuffer
, const char *mnemonic
, char *mask
)
1260 if (strlen (mask
) > 2 || crx_interrupt_function_p ()) /* needs 2-word instr. */
1261 sprintf (stringbuffer
, "\n\t%s\tsp, {%s}", mnemonic
, mask
);
1262 else /* single word instruction */
1263 sprintf (stringbuffer
, "\n\t%s\t%s", mnemonic
, mask
);
1266 /* Called from crx.md. The return value depends on the parameter push_or_pop:
1267 * When push_or_pop is zero -> string for push instructions of prologue.
1268 * When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1269 * Relies on the assumptions:
1270 * 1. RA is the last register to be saved.
1271 * 2. The maximal value of the counter is MAX_COUNT. */
1274 crx_prepare_push_pop_string (int push_or_pop
)
1276 /* j is the number of registers being saved, takes care that there won't be
1277 * more than 8 in one push/pop instruction */
1279 /* For the register mask string */
1280 static char mask_str
[50];
1282 /* i is the index of save_regs[], going from 0 until last_reg_to_save */
1285 int ra_in_bitmask
= 0;
1289 /* For reversing on the push instructions if there are more than one. */
1292 return_str
= (char *) xmalloc (120);
1293 temp_str
= (char *) xmalloc (120);
1296 memset (return_str
, 0, 3);
1298 while (i
<= last_reg_to_save
)
1300 /* Prepare mask for one instruction. */
1304 { /* Add regs unit full or SP register reached */
1306 while (j
< MAX_COUNT
&& i
<= SP_REGNUM
)
1310 /* TODO to use ra_in_bitmask for detecting last pop is not
1311 * smart it prevents things like: popret r5 */
1312 if (i
== RETURN_ADDRESS_REGNUM
) ra_in_bitmask
= 1;
1313 if (j
> 0) strcat (mask_str
, ", ");
1314 strcat (mask_str
, reg_names
[i
]);
1322 /* Handle hi/lo savings */
1323 while (i
<= last_reg_to_save
)
1327 strcat (mask_str
, "lo, hi");
1328 i
= last_reg_to_save
+ 1;
1335 if (strlen (mask_str
) == 0) continue;
1337 if (push_or_pop
== 1)
1339 if (crx_interrupt_function_p ())
1340 mpushpop_str (temp_str
, "popx", mask_str
);
1345 mpushpop_str (temp_str
, "popret", mask_str
);
1348 else mpushpop_str (temp_str
, "pop", mask_str
);
1351 strcat (return_str
, temp_str
);
1355 /* push - We need to reverse the order of the instructions if there
1356 * are more than one. (since the pop will not be reversed in the
1358 if (crx_interrupt_function_p ())
1359 mpushpop_str (temp_str
, "pushx", mask_str
);
1361 mpushpop_str (temp_str
, "push", mask_str
);
1362 strcat (temp_str
, return_str
);
1363 strcpy (strcat (return_str
, "\t"), temp_str
);
1368 if (push_or_pop
== 1)
1371 if (crx_interrupt_function_p ())
1372 strcat (return_str
, "\n\tretx\n");
1374 else if (!FUNC_IS_NORETURN_P (current_function_decl
)
1375 && !save_regs
[RETURN_ADDRESS_REGNUM
])
1376 strcat (return_str
, "\n\tjump\tra\n");
1379 /* Skip the newline and the tab in the start of return_str. */
1384 /* CompactRISC CRX Architecture stack layout:
1386 0 +---------------------
1391 +==================== Sp(x)=Ap(x+1)
1392 A | Args for functions
1393 | | called by X and Dynamically
1394 | | Dynamic allocations allocated and
1395 | | (alloca, variable deallocated
1396 Stack | length arrays).
1397 grows +-------------------- Fp(x)
1398 down| | Local variables of X
1399 ward| +--------------------
1400 | | Regs saved for X-1
1401 | +==================== Sp(x-1)=Ap(x)
1404 +-------------------- Fp(x-1)
1412 crx_expand_prologue (void)
1414 crx_compute_frame ();
1415 crx_compute_save_regs ();
1417 /* If there is no need in push and adjustment to sp, return. */
1418 if (size_for_adjusting_sp
+ sum_regs
== 0)
1421 if (last_reg_to_save
!= -1)
1422 /* If there are registers to push. */
1423 emit_insn (gen_push_for_prologue (GEN_INT (sum_regs
)));
1425 if (size_for_adjusting_sp
> 0)
1426 emit_insn (gen_addsi3 (stack_pointer_rtx
, stack_pointer_rtx
,
1427 GEN_INT (-size_for_adjusting_sp
)));
1429 if (frame_pointer_needed
)
1430 /* Initialize the frame pointer with the value of the stack pointer
1431 * pointing now to the locals. */
1432 emit_move_insn (frame_pointer_rtx
, stack_pointer_rtx
);
1435 /* Generate insn that updates the stack for local variables and padding for
1436 * registers we save. - Generate the appropriate return insn. */
1439 crx_expand_epilogue (void)
1443 /* Nonzero if we need to return and pop only RA. This will generate a
1444 * different insn. This differentiate is for the peepholes for call as last
1445 * statement in function. */
1446 int only_popret_RA
= (save_regs
[RETURN_ADDRESS_REGNUM
]
1447 && (sum_regs
== UNITS_PER_WORD
));
1449 /* Return register. */
1450 return_reg
= gen_rtx_REG (Pmode
, RETURN_ADDRESS_REGNUM
);
1452 if (frame_pointer_needed
)
1453 /* Restore the stack pointer with the frame pointers value */
1454 emit_move_insn (stack_pointer_rtx
, frame_pointer_rtx
);
1456 if (size_for_adjusting_sp
> 0)
1457 emit_insn (gen_addsi3 (stack_pointer_rtx
, stack_pointer_rtx
,
1458 GEN_INT (size_for_adjusting_sp
)));
1460 if (crx_interrupt_function_p ())
1461 emit_jump_insn (gen_interrupt_return ());
1462 else if (last_reg_to_save
== -1)
1463 /* Nothing to pop */
1464 /* Don't output jump for interrupt routine, only retx. */
1465 emit_jump_insn (gen_indirect_jump_return ());
1466 else if (only_popret_RA
)
1467 emit_jump_insn (gen_popret_RA_return ());
1469 emit_jump_insn (gen_pop_and_popret_return (GEN_INT (sum_regs
)));