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, 2008 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 /*****************************************************************************/
128 /* TARGETM FUNCTION PROTOTYPES */
129 /*****************************************************************************/
131 static bool crx_fixed_condition_code_regs (unsigned int *, unsigned int *);
132 static rtx crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
133 int incoming ATTRIBUTE_UNUSED);
134 static bool crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED);
135 static int crx_address_cost (rtx, bool);
136 static bool crx_legitimate_address_p (enum machine_mode, rtx, bool);
138 /*****************************************************************************/
140 /*****************************************************************************/
142 #undef TARGET_LEGITIMATE_ADDRESS_P
143 #define TARGET_LEGITIMATE_ADDRESS_P crx_legitimate_address_p
145 /*****************************************************************************/
146 /* STACK LAYOUT AND CALLING CONVENTIONS */
147 /*****************************************************************************/
149 #undef TARGET_FIXED_CONDITION_CODE_REGS
150 #define TARGET_FIXED_CONDITION_CODE_REGS crx_fixed_condition_code_regs
152 #undef TARGET_STRUCT_VALUE_RTX
153 #define TARGET_STRUCT_VALUE_RTX crx_struct_value_rtx
155 #undef TARGET_RETURN_IN_MEMORY
156 #define TARGET_RETURN_IN_MEMORY crx_return_in_memory
158 /*****************************************************************************/
159 /* RELATIVE COSTS OF OPERATIONS */
160 /*****************************************************************************/
162 #undef TARGET_ADDRESS_COST
163 #define TARGET_ADDRESS_COST crx_address_cost
165 /*****************************************************************************/
166 /* TARGET-SPECIFIC USES OF `__attribute__' */
167 /*****************************************************************************/
169 #undef TARGET_ATTRIBUTE_TABLE
170 #define TARGET_ATTRIBUTE_TABLE crx_attribute_table
172 const struct attribute_spec crx_attribute_table[] = {
173 /* ISRs have special prologue and epilogue requirements. */
174 {"interrupt", 0, 0, false, true, true, NULL},
175 {NULL, 0, 0, false, false, false, NULL}
179 /* Initialize 'targetm' variable which contains pointers to functions and data
180 * relating to the target machine. */
182 struct gcc_target targetm = TARGET_INITIALIZER;
185 /*****************************************************************************/
186 /* TARGET HOOK IMPLEMENTATIONS */
187 /*****************************************************************************/
189 /* Return the fixed registers used for condition codes. */
192 crx_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
195 *p2 = INVALID_REGNUM;
199 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
202 crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
203 int incoming ATTRIBUTE_UNUSED)
205 return gen_rtx_REG (Pmode, CRX_STRUCT_VALUE_REGNUM);
208 /* Implements hook TARGET_RETURN_IN_MEMORY. */
211 crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
213 if (TYPE_MODE (type) == BLKmode)
215 HOST_WIDE_INT size = int_size_in_bytes (type);
216 return (size == -1 || size > 8);
223 /*****************************************************************************/
224 /* MACRO IMPLEMENTATIONS */
225 /*****************************************************************************/
227 /* STACK LAYOUT AND CALLING CONVENTIONS ROUTINES */
228 /* --------------------------------------------- */
230 /* Return nonzero if the current function being compiled is an interrupt
231 * function as specified by the "interrupt" attribute. */
234 crx_interrupt_function_p (void)
238 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
239 return lookup_attribute ("interrupt", attributes) != NULL_TREE;
242 /* Compute values for the array save_regs and the variable sum_regs. The index
243 * of save_regs is numbers of register, each will get 1 if we need to save it
244 * in the current function, 0 if not. sum_regs is the total sum of the
245 * registers being saved. */
248 crx_compute_save_regs (void)
252 /* initialize here so in case the function is no-return it will be -1. */
253 last_reg_to_save = -1;
255 /* No need to save any registers if the function never returns. */
256 if (FUNC_IS_NORETURN_P (current_function_decl))
259 /* Initialize the number of bytes to be saved. */
262 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
264 if (fixed_regs[regno])
266 save_regs[regno] = 0;
270 /* If this reg is used and not call-used (except RA), save it. */
271 if (crx_interrupt_function_p ())
273 if (!current_function_is_leaf && call_used_regs[regno])
274 /* this is a volatile reg in a non-leaf interrupt routine - save it
275 * for the sake of its sons. */
276 save_regs[regno] = 1;
278 else if (df_regs_ever_live_p (regno))
279 /* This reg is used - save it. */
280 save_regs[regno] = 1;
282 /* This reg is not used, and is not a volatile - don't save. */
283 save_regs[regno] = 0;
287 /* If this reg is used and not call-used (except RA), save it. */
288 if (df_regs_ever_live_p (regno)
289 && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM))
290 save_regs[regno] = 1;
292 save_regs[regno] = 0;
296 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
297 if (save_regs[regno] == 1)
299 last_reg_to_save = regno;
300 sum_regs += UNITS_PER_WORD;
304 /* Compute the size of the local area and the size to be adjusted by the
305 * prologue and epilogue. */
308 crx_compute_frame (void)
310 /* For aligning the local variables. */
311 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
314 /* Padding needed for each element of the frame. */
315 local_vars_size = get_frame_size ();
317 /* Align to the stack alignment. */
318 padding_locals = local_vars_size % stack_alignment;
320 padding_locals = stack_alignment - padding_locals;
322 local_vars_size += padding_locals;
324 size_for_adjusting_sp = local_vars_size + (ACCUMULATE_OUTGOING_ARGS ?
325 crtl->outgoing_args_size : 0);
328 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
331 crx_initial_elimination_offset (int from, int to)
333 /* Compute this since we need to use sum_regs. */
334 crx_compute_save_regs ();
336 /* Compute this since we need to use local_vars_size. */
337 crx_compute_frame ();
339 if ((from) == FRAME_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
340 return (ACCUMULATE_OUTGOING_ARGS ?
341 crtl->outgoing_args_size : 0);
342 else if ((from) == ARG_POINTER_REGNUM && (to) == FRAME_POINTER_REGNUM)
343 return (sum_regs + local_vars_size);
344 else if ((from) == ARG_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
345 return (sum_regs + local_vars_size +
346 (ACCUMULATE_OUTGOING_ARGS ?
347 crtl->outgoing_args_size : 0));
355 /* Return the class number of the smallest class containing reg number REGNO.
356 * This could be a conditional expression or could index an array. */
359 crx_regno_reg_class (int regno)
361 if (regno >= 0 && regno < SP_REGNUM)
364 if (regno == SP_REGNUM)
367 if (regno == LO_REGNUM)
369 if (regno == HI_REGNUM)
375 /* Transfer between HILO_REGS and memory via secondary reloading. */
378 crx_secondary_reload_class (enum reg_class rclass,
379 enum machine_mode mode ATTRIBUTE_UNUSED,
380 rtx x ATTRIBUTE_UNUSED)
382 if (reg_classes_intersect_p (rclass, HILO_REGS)
383 && true_regnum (x) == -1)
389 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
392 crx_hard_regno_mode_ok (int regno, enum machine_mode mode)
394 /* CC can only hold CCmode values. */
395 if (regno == CC_REGNUM)
396 return GET_MODE_CLASS (mode) == MODE_CC;
397 if (GET_MODE_CLASS (mode) == MODE_CC)
399 /* HILO registers can only hold SImode and DImode */
400 if (HILO_REGNO_P (regno))
401 return mode == SImode || mode == DImode;
405 /* PASSING FUNCTION ARGUMENTS */
406 /* -------------------------- */
408 /* If enough param regs are available for passing the param of type TYPE return
409 * the number of registers needed else 0. */
412 enough_regs_for_param (CUMULATIVE_ARGS * cum, tree type,
413 enum machine_mode mode)
419 type_size = GET_MODE_BITSIZE (mode);
421 type_size = int_size_in_bytes (type) * BITS_PER_UNIT;
424 BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS -
425 (MIN_REG_FOR_PASSING_ARGS + cum->ints) + 1);
427 /* Any variable which is too big to pass in two registers, will pass on
429 if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD))
430 return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD;
435 /* Implements the macro FUNCTION_ARG defined in crx.h. */
438 crx_function_arg (CUMULATIVE_ARGS * cum, enum machine_mode mode, tree type,
439 int named ATTRIBUTE_UNUSED)
441 last_parm_in_reg = 0;
443 /* Function_arg () is called with this type just after all the args have had
444 * their registers assigned. The rtx that function_arg returns from this type
445 * is supposed to pass to 'gen_call' but currently it is not implemented (see
446 * macro GEN_CALL). */
447 if (type == void_type_node)
450 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
455 /* Enable structures that need padding bytes at the end to pass to a
456 * function in registers. */
457 if (enough_regs_for_param (cum, type, mode) != 0)
459 last_parm_in_reg = 1;
460 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
464 if (MIN_REG_FOR_PASSING_ARGS + cum->ints > MAX_REG_FOR_PASSING_ARGS)
468 if (enough_regs_for_param (cum, type, mode) != 0)
470 last_parm_in_reg = 1;
471 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
478 /* Implements the macro INIT_CUMULATIVE_ARGS defined in crx.h. */
481 crx_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype,
482 rtx libfunc ATTRIBUTE_UNUSED)
484 tree param, next_param;
488 /* Determine if this function has variable arguments. This is indicated by
489 * the last argument being 'void_type_mode' if there are no variable
490 * arguments. Change here for a different vararg. */
491 for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
492 param != (tree) 0; param = next_param)
494 next_param = TREE_CHAIN (param);
495 if (next_param == (tree) 0 && TREE_VALUE (param) != void_type_node)
503 /* Implements the macro FUNCTION_ARG_ADVANCE defined in crx.h. */
506 crx_function_arg_advance (CUMULATIVE_ARGS * cum, enum machine_mode mode,
507 tree type, int named ATTRIBUTE_UNUSED)
509 /* l holds the number of registers required */
510 int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
512 /* If the parameter isn't passed on a register don't advance cum. */
513 if (!last_parm_in_reg)
516 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
519 if (mode == SImode || mode == HImode || mode == QImode || mode == DImode)
526 else if (mode == SFmode || mode == DFmode)
528 else if ((mode) == BLKmode)
530 if ((l = enough_regs_for_param (cum, type, mode)) != 0)
536 /* Implements the macro FUNCTION_ARG_REGNO_P defined in crx.h. Return nonzero
537 * if N is a register used for passing parameters. */
540 crx_function_arg_regno_p (int n)
542 return (n <= MAX_REG_FOR_PASSING_ARGS && n >= MIN_REG_FOR_PASSING_ARGS);
545 /* ADDRESSING MODES */
546 /* ---------------- */
548 /* Implements the macro GO_IF_LEGITIMATE_ADDRESS defined in crx.h.
549 * The following addressing modes are supported on CRX:
551 * Relocations --> const | symbol_ref | label_ref
552 * Absolute address --> 32-bit absolute
553 * Post increment --> reg + 12-bit disp.
554 * Post modify --> reg + 12-bit disp.
555 * Register relative --> reg | 32-bit disp. + reg | 4 bit + reg
556 * Scaled index --> reg + reg | 22-bit disp. + reg + reg |
557 * 22-disp. + reg + reg + (2 | 4 | 8) */
559 static int crx_addr_reg_p (rtx addr_reg)
563 if (REG_P (addr_reg))
567 else if ((GET_CODE (addr_reg) == SUBREG
568 && REG_P (SUBREG_REG (addr_reg))
569 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg)))
572 reg = SUBREG_REG (addr_reg);
577 if (GET_MODE (addr_reg) != Pmode)
586 crx_decompose_address (rtx addr, struct crx_address *out)
588 rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
589 rtx scale_rtx = NULL_RTX, side_effect = NULL_RTX;
592 enum crx_addrtype retval = CRX_INVALID;
594 switch (GET_CODE (addr))
597 /* Absolute address (known at compile time) */
598 retval = CRX_ABSOLUTE;
600 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), GET_MODE_BITSIZE (Pmode)))
607 /* Absolute address (known at link time) */
608 retval = CRX_ABSOLUTE;
614 /* Register relative address */
615 retval = CRX_REG_REL;
620 switch (GET_CODE (XEXP (addr, 0)))
624 if (REG_P (XEXP (addr, 1)))
626 /* Scaled index with scale = 1 and disp. = 0 */
627 retval = CRX_SCALED_INDX;
628 base = XEXP (addr, 1);
629 index = XEXP (addr, 0);
632 else if (RTX_SIGNED_INT_FITS_N_BITS (XEXP (addr, 1), 28))
634 /* Register relative address and <= 28-bit disp. */
635 retval = CRX_REG_REL;
636 base = XEXP (addr, 0);
637 disp = XEXP (addr, 1);
644 /* Scaled index and <= 22-bit disp. */
645 retval = CRX_SCALED_INDX;
646 base = XEXP (XEXP (addr, 0), 1);
647 disp = XEXP (addr, 1);
648 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 22))
650 switch (GET_CODE (XEXP (XEXP (addr, 0), 0)))
653 /* Scaled index with scale = 0 and <= 22-bit disp. */
654 index = XEXP (XEXP (addr, 0), 0);
659 /* Scaled index with scale >= 0 and <= 22-bit disp. */
660 index = XEXP (XEXP (XEXP (addr, 0), 0), 0);
661 scale_rtx = XEXP (XEXP (XEXP (addr, 0), 0), 1);
662 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
672 /* Scaled index with scale >= 0 */
673 retval = CRX_SCALED_INDX;
674 base = XEXP (addr, 1);
675 index = XEXP (XEXP (addr, 0), 0);
676 scale_rtx = XEXP (XEXP (addr, 0), 1);
677 /* Scaled index with scale >= 0 and <= 22-bit disp. */
678 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
689 /* Simple post-increment */
690 retval = CRX_POST_INC;
691 base = XEXP (addr, 0);
696 /* Generic post-increment with <= 12-bit disp. */
697 retval = CRX_POST_INC;
698 base = XEXP (addr, 0);
699 side_effect = XEXP (addr, 1);
700 if (base != XEXP (side_effect, 0))
702 switch (GET_CODE (side_effect))
706 disp = XEXP (side_effect, 1);
707 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 12))
712 /* CRX only supports PLUS and MINUS */
721 if (base && !crx_addr_reg_p (base)) return CRX_INVALID;
722 if (index && !crx_addr_reg_p (index)) return CRX_INVALID;
728 out->side_effect = side_effect;
734 crx_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
735 rtx addr, bool strict)
737 enum crx_addrtype addrtype;
738 struct crx_address address;
740 if (TARGET_DEBUG_ADDR)
743 "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
744 GET_MODE_NAME (mode), strict);
748 addrtype = crx_decompose_address (addr, &address);
750 if (addrtype == CRX_POST_INC && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
753 if (TARGET_DEBUG_ADDR)
762 typestr = "Register relative";
765 typestr = "Post-increment";
767 case CRX_SCALED_INDX:
768 typestr = "Scaled index";
771 typestr = "Absolute";
776 fprintf (stderr, "CRX Address type: %s\n", typestr);
779 if (addrtype == CRX_INVALID)
784 if (address.base && !REGNO_OK_FOR_BASE_P (REGNO (address.base)))
786 if (TARGET_DEBUG_ADDR)
787 fprintf (stderr, "Base register not strict\n");
790 if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index)))
792 if (TARGET_DEBUG_ADDR)
793 fprintf (stderr, "Index register not strict\n");
801 /* ROUTINES TO COMPUTE COSTS */
802 /* ------------------------- */
804 /* Return cost of the memory address x. */
807 crx_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
809 enum crx_addrtype addrtype;
810 struct crx_address address;
814 addrtype = crx_decompose_address (addr, &address);
816 gcc_assert (addrtype != CRX_INVALID);
818 /* An absolute address causes a 3-word instruction */
819 if (addrtype == CRX_ABSOLUTE)
822 /* Post-modifying addresses are more powerful. */
823 if (addrtype == CRX_POST_INC)
826 /* Attempt to minimize number of registers in the address. */
830 if (address.index && address.scale == 1)
833 if (address.disp && !INT_CST4 (INTVAL (address.disp)))
836 if (TARGET_DEBUG_ADDR)
838 fprintf (stderr, "\n======\nTARGET_ADDRESS_COST = %d\n", cost);
845 /* Return the cost of moving data of mode MODE between a register of class
846 * RCLASS and memory; IN is zero if the value is to be written to memory,
847 * nonzero if it is to be read in. This cost is relative to those in
848 * REGISTER_MOVE_COST. */
851 crx_memory_move_cost (enum machine_mode mode,
852 enum reg_class rclass ATTRIBUTE_UNUSED,
853 int in ATTRIBUTE_UNUSED)
855 /* One LD or ST takes twice the time of a simple reg-reg move */
856 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
858 /* printf ("GENERAL_REGS LD/ST = %d\n", 4 * HARD_REGNO_NREGS (0, mode));*/
859 return 4 * HARD_REGNO_NREGS (0, mode);
861 else if (reg_classes_intersect_p (rclass, HILO_REGS))
863 /* HILO to memory and vice versa */
864 /* printf ("HILO_REGS %s = %d\n", in ? "LD" : "ST",
865 (REGISTER_MOVE_COST (mode,
866 in ? GENERAL_REGS : HILO_REGS,
867 in ? HILO_REGS : GENERAL_REGS) + 4)
868 * HARD_REGNO_NREGS (0, mode)); */
869 return (REGISTER_MOVE_COST (mode,
870 in ? GENERAL_REGS : HILO_REGS,
871 in ? HILO_REGS : GENERAL_REGS) + 4)
872 * HARD_REGNO_NREGS (0, mode);
874 else /* default (like in i386) */
876 /* printf ("ANYREGS = 100\n"); */
881 /* INSTRUCTION OUTPUT */
882 /* ------------------ */
884 /* Check if a const_double is ok for crx store-immediate instructions */
887 crx_const_double_ok (rtx op)
889 if (GET_MODE (op) == DFmode)
893 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
894 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
895 return (UNSIGNED_INT_FITS_N_BITS (l[0], 4) &&
896 UNSIGNED_INT_FITS_N_BITS (l[1], 4)) ? 1 : 0;
899 if (GET_MODE (op) == SFmode)
903 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
904 REAL_VALUE_TO_TARGET_SINGLE (r, l);
905 return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0;
908 return (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4) &&
909 UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4)) ? 1 : 0;
912 /* Implements the macro PRINT_OPERAND defined in crx.h. */
915 crx_print_operand (FILE * file, rtx x, int code)
920 if (GET_CODE (x) == REG) {
921 if (GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
923 int regno = REGNO (x);
924 if (regno + 1 >= SP_REGNUM) abort ();
925 fprintf (file, "{%s, %s}", reg_names[regno], reg_names[regno + 1]);
930 if (REGNO (x) >= SP_REGNUM) abort ();
931 fprintf (file, "%s", reg_names[REGNO (x)]);
938 const char *crx_cmp_str;
939 switch (GET_CODE (x))
940 { /* MD: compare (reg, reg or imm) but CRX: cmp (reg or imm, reg)
941 * -> swap all non symmetric ops */
942 case EQ : crx_cmp_str = "eq"; break;
943 case NE : crx_cmp_str = "ne"; break;
944 case GT : crx_cmp_str = "lt"; break;
945 case GTU : crx_cmp_str = "lo"; break;
946 case LT : crx_cmp_str = "gt"; break;
947 case LTU : crx_cmp_str = "hi"; break;
948 case GE : crx_cmp_str = "le"; break;
949 case GEU : crx_cmp_str = "ls"; break;
950 case LE : crx_cmp_str = "ge"; break;
951 case LEU : crx_cmp_str = "hs"; break;
954 fprintf (file, "%s", crx_cmp_str);
959 /* Print high part of a double precision value. */
960 switch (GET_CODE (x))
963 if (GET_MODE (x) == SFmode) abort ();
964 if (GET_MODE (x) == DFmode)
966 /* High part of a DF const. */
970 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
971 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
973 fprintf (file, "$0x%lx", l[1]);
977 /* -- Fallthrough to handle DI consts -- */
982 split_double (x, &low, &high);
984 output_addr_const (file, high);
989 if (REGNO (x) + 1 >= FIRST_PSEUDO_REGISTER) abort ();
990 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
994 /* Adjust memory address to high part. */
997 adj_mem = adjust_address (adj_mem, GET_MODE (adj_mem), 4);
999 output_memory_reference_mode = GET_MODE (adj_mem);
1000 output_address (XEXP (adj_mem, 0));
1009 /* Print low part of a double precision value. */
1010 switch (GET_CODE (x))
1013 if (GET_MODE (x) == SFmode) abort ();
1014 if (GET_MODE (x) == DFmode)
1016 /* High part of a DF const. */
1020 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1021 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
1023 fprintf (file, "$0x%lx", l[0]);
1027 /* -- Fallthrough to handle DI consts -- */
1032 split_double (x, &low, &high);
1034 output_addr_const (file, low);
1039 fprintf (file, "%s", reg_names[REGNO (x)]);
1043 output_memory_reference_mode = GET_MODE (x);
1044 output_address (XEXP (x, 0));
1051 case 0 : /* default */
1052 switch (GET_CODE (x))
1055 fprintf (file, "%s", reg_names[REGNO (x)]);
1059 output_memory_reference_mode = GET_MODE (x);
1060 output_address (XEXP (x, 0));
1068 /* Always use H and L for double precision - see above */
1069 gcc_assert (GET_MODE (x) == SFmode);
1071 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1072 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1074 fprintf (file, "$0x%lx", l);
1080 output_addr_const (file, x);
1085 output_operand_lossage ("invalid %%xn code");
1091 /* Implements the macro PRINT_OPERAND_ADDRESS defined in crx.h. */
1094 crx_print_operand_address (FILE * file, rtx addr)
1096 enum crx_addrtype addrtype;
1097 struct crx_address address;
1101 addrtype = crx_decompose_address (addr, &address);
1104 offset = INTVAL (address.disp);
1111 fprintf (file, "%d(%s)", offset, reg_names[REGNO (address.base)]);
1115 switch (GET_CODE (address.side_effect))
1123 offset = GET_MODE_SIZE (output_memory_reference_mode);
1126 offset = -GET_MODE_SIZE (output_memory_reference_mode);
1131 fprintf (file, "%d(%s)+", offset, reg_names[REGNO (address.base)]);
1134 case CRX_SCALED_INDX:
1135 fprintf (file, "%d(%s, %s, %d)", offset, reg_names[REGNO (address.base)],
1136 reg_names[REGNO (address.index)], address.scale);
1140 output_addr_const (file, address.disp);
1149 /*****************************************************************************/
1150 /* MACHINE DESCRIPTION HELPER-FUNCTIONS */
1151 /*****************************************************************************/
1153 void crx_expand_movmem_single (rtx src, rtx srcbase, rtx dst, rtx dstbase,
1154 rtx tmp_reg, unsigned HOST_WIDE_INT *offset_p)
1157 unsigned HOST_WIDE_INT offset = *offset_p;
1160 addr = plus_constant (src, offset);
1161 mem = adjust_automodify_address (srcbase, SImode, addr, offset);
1162 emit_move_insn (tmp_reg, mem);
1165 addr = plus_constant (dst, offset);
1166 mem = adjust_automodify_address (dstbase, SImode, addr, offset);
1167 emit_move_insn (mem, tmp_reg);
1169 *offset_p = offset + 4;
1173 crx_expand_movmem (rtx dstbase, rtx srcbase, rtx count_exp, rtx align_exp)
1175 unsigned HOST_WIDE_INT count = 0, offset, si_moves, i;
1176 HOST_WIDE_INT align = 0;
1181 if (GET_CODE (align_exp) == CONST_INT)
1182 { /* Only if aligned */
1183 align = INTVAL (align_exp);
1188 if (GET_CODE (count_exp) == CONST_INT)
1189 { /* No more than 16 SImode moves */
1190 count = INTVAL (count_exp);
1195 tmp_reg = gen_reg_rtx (SImode);
1197 /* Create psrs for the src and dest pointers */
1198 dst = copy_to_mode_reg (Pmode, XEXP (dstbase, 0));
1199 if (dst != XEXP (dstbase, 0))
1200 dstbase = replace_equiv_address_nv (dstbase, dst);
1201 src = copy_to_mode_reg (Pmode, XEXP (srcbase, 0));
1202 if (src != XEXP (srcbase, 0))
1203 srcbase = replace_equiv_address_nv (srcbase, src);
1207 /* Emit SImode moves */
1208 si_moves = count >> 2;
1209 for (i = 0; i < si_moves; i++)
1210 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1216 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1219 gcc_assert (offset == count);
1225 mpushpop_str (char *stringbuffer, const char *mnemonic, char *mask)
1227 if (strlen (mask) > 2 || crx_interrupt_function_p ()) /* needs 2-word instr. */
1228 sprintf (stringbuffer, "\n\t%s\tsp, {%s}", mnemonic, mask);
1229 else /* single word instruction */
1230 sprintf (stringbuffer, "\n\t%s\t%s", mnemonic, mask);
1233 /* Called from crx.md. The return value depends on the parameter push_or_pop:
1234 * When push_or_pop is zero -> string for push instructions of prologue.
1235 * When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1236 * Relies on the assumptions:
1237 * 1. RA is the last register to be saved.
1238 * 2. The maximal value of the counter is MAX_COUNT. */
1241 crx_prepare_push_pop_string (int push_or_pop)
1243 /* j is the number of registers being saved, takes care that there won't be
1244 * more than 8 in one push/pop instruction */
1246 /* For the register mask string */
1247 static char mask_str[50];
1249 /* i is the index of save_regs[], going from 0 until last_reg_to_save */
1252 int ra_in_bitmask = 0;
1256 /* For reversing on the push instructions if there are more than one. */
1259 return_str = (char *) xmalloc (120);
1260 temp_str = (char *) xmalloc (120);
1263 memset (return_str, 0, 3);
1265 while (i <= last_reg_to_save)
1267 /* Prepare mask for one instruction. */
1271 { /* Add regs unit full or SP register reached */
1273 while (j < MAX_COUNT && i <= SP_REGNUM)
1277 /* TODO to use ra_in_bitmask for detecting last pop is not
1278 * smart it prevents things like: popret r5 */
1279 if (i == RETURN_ADDRESS_REGNUM) ra_in_bitmask = 1;
1280 if (j > 0) strcat (mask_str, ", ");
1281 strcat (mask_str, reg_names[i]);
1289 /* Handle hi/lo savings */
1290 while (i <= last_reg_to_save)
1294 strcat (mask_str, "lo, hi");
1295 i = last_reg_to_save + 1;
1302 if (strlen (mask_str) == 0) continue;
1304 if (push_or_pop == 1)
1306 if (crx_interrupt_function_p ())
1307 mpushpop_str (temp_str, "popx", mask_str);
1312 mpushpop_str (temp_str, "popret", mask_str);
1315 else mpushpop_str (temp_str, "pop", mask_str);
1318 strcat (return_str, temp_str);
1322 /* push - We need to reverse the order of the instructions if there
1323 * are more than one. (since the pop will not be reversed in the
1325 if (crx_interrupt_function_p ())
1326 mpushpop_str (temp_str, "pushx", mask_str);
1328 mpushpop_str (temp_str, "push", mask_str);
1329 strcat (temp_str, return_str);
1330 strcpy (strcat (return_str, "\t"), temp_str);
1335 if (push_or_pop == 1)
1338 if (crx_interrupt_function_p ())
1339 strcat (return_str, "\n\tretx\n");
1341 else if (!FUNC_IS_NORETURN_P (current_function_decl)
1342 && !save_regs[RETURN_ADDRESS_REGNUM])
1343 strcat (return_str, "\n\tjump\tra\n");
1346 /* Skip the newline and the tab in the start of return_str. */
1351 /* CompactRISC CRX Architecture stack layout:
1353 0 +---------------------
1358 +==================== Sp(x)=Ap(x+1)
1359 A | Args for functions
1360 | | called by X and Dynamically
1361 | | Dynamic allocations allocated and
1362 | | (alloca, variable deallocated
1363 Stack | length arrays).
1364 grows +-------------------- Fp(x)
1365 down| | Local variables of X
1366 ward| +--------------------
1367 | | Regs saved for X-1
1368 | +==================== Sp(x-1)=Ap(x)
1371 +-------------------- Fp(x-1)
1379 crx_expand_prologue (void)
1381 crx_compute_frame ();
1382 crx_compute_save_regs ();
1384 /* If there is no need in push and adjustment to sp, return. */
1385 if (size_for_adjusting_sp + sum_regs == 0)
1388 if (last_reg_to_save != -1)
1389 /* If there are registers to push. */
1390 emit_insn (gen_push_for_prologue (GEN_INT (sum_regs)));
1392 if (size_for_adjusting_sp > 0)
1393 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1394 GEN_INT (-size_for_adjusting_sp)));
1396 if (frame_pointer_needed)
1397 /* Initialize the frame pointer with the value of the stack pointer
1398 * pointing now to the locals. */
1399 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1402 /* Generate insn that updates the stack for local variables and padding for
1403 * registers we save. - Generate the appropriate return insn. */
1406 crx_expand_epilogue (void)
1410 /* Nonzero if we need to return and pop only RA. This will generate a
1411 * different insn. This differentiate is for the peepholes for call as last
1412 * statement in function. */
1413 int only_popret_RA = (save_regs[RETURN_ADDRESS_REGNUM]
1414 && (sum_regs == UNITS_PER_WORD));
1416 /* Return register. */
1417 return_reg = gen_rtx_REG (Pmode, RETURN_ADDRESS_REGNUM);
1419 if (frame_pointer_needed)
1420 /* Restore the stack pointer with the frame pointers value */
1421 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1423 if (size_for_adjusting_sp > 0)
1424 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1425 GEN_INT (size_for_adjusting_sp)));
1427 if (crx_interrupt_function_p ())
1428 emit_jump_insn (gen_interrupt_return ());
1429 else if (last_reg_to_save == -1)
1430 /* Nothing to pop */
1431 /* Don't output jump for interrupt routine, only retx. */
1432 emit_jump_insn (gen_indirect_jump_return ());
1433 else if (only_popret_RA)
1434 emit_jump_insn (gen_popret_RA_return ());
1436 emit_jump_insn (gen_pop_and_popret_return (GEN_INT (sum_regs)));