Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / config / crx / crx.c
blobd6b9022b239e1ddeb784739f2f49c0ccb792140c
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, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /*****************************************************************************/
23 /* HEADER INCLUDES */
24 /*****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tm_p.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "output.h"
38 #include "insn-codes.h"
39 #include "insn-attr.h"
40 #include "flags.h"
41 #include "except.h"
42 #include "function.h"
43 #include "recog.h"
44 #include "expr.h"
45 #include "optabs.h"
46 #include "diagnostic-core.h"
47 #include "toplev.h"
48 #include "basic-block.h"
49 #include "df.h"
50 #include "target.h"
51 #include "target-def.h"
53 /*****************************************************************************/
54 /* DEFINITIONS */
55 /*****************************************************************************/
57 /* Maximum number of register used for passing parameters. */
58 #define MAX_REG_FOR_PASSING_ARGS 6
60 /* Minimum number register used for passing parameters. */
61 #define MIN_REG_FOR_PASSING_ARGS 2
63 /* The maximum count of words supported in the assembly of the architecture in
64 * a push/pop instruction. */
65 #define MAX_COUNT 8
67 /* Predicate is true if the current function is a 'noreturn' function, i.e. it
68 * is qualified as volatile. */
69 #define FUNC_IS_NORETURN_P(decl) (TREE_THIS_VOLATILE (decl))
71 /* The following macros are used in crx_decompose_address () */
73 /* Returns the factor of a scaled index address or -1 if invalid. */
74 #define SCALE_FOR_INDEX_P(X) \
75 (GET_CODE (X) == CONST_INT ? \
76 (INTVAL (X) == 1 ? 1 : \
77 INTVAL (X) == 2 ? 2 : \
78 INTVAL (X) == 4 ? 4 : \
79 INTVAL (X) == 8 ? 8 : \
80 -1) : \
81 -1)
83 /* Nonzero if the rtx X is a signed const int of n bits */
84 #define RTX_SIGNED_INT_FITS_N_BITS(X,n) \
85 ((GET_CODE (X) == CONST_INT \
86 && SIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
88 /* Nonzero if the rtx X is an unsigned const int of n bits. */
89 #define RTX_UNSIGNED_INT_FITS_N_BITS(X, n) \
90 ((GET_CODE (X) == CONST_INT \
91 && UNSIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
93 /*****************************************************************************/
94 /* STATIC VARIABLES */
95 /*****************************************************************************/
97 /* Nonzero if the last param processed is passed in a register. */
98 static int last_parm_in_reg;
100 /* Will hold the number of the last register the prologue saves, -1 if no
101 * register is saved. */
102 static int last_reg_to_save;
104 /* Each object in the array is a register number. Mark 1 for registers that
105 * need to be saved. */
106 static int save_regs[FIRST_PSEUDO_REGISTER];
108 /* Number of bytes saved on the stack for non-scratch registers */
109 static int sum_regs = 0;
111 /* Number of bytes saved on the stack for local variables. */
112 static int local_vars_size;
114 /* The sum of 2 sizes: locals vars and padding byte for saving the registers.
115 * Used in expand_prologue () and expand_epilogue (). */
116 static int size_for_adjusting_sp;
118 /* In case of a POST_INC or POST_DEC memory reference, we must report the mode
119 * of the memory reference from PRINT_OPERAND to PRINT_OPERAND_ADDRESS. */
120 static enum machine_mode output_memory_reference_mode;
122 /*****************************************************************************/
123 /* TARGETM FUNCTION PROTOTYPES */
124 /*****************************************************************************/
126 static bool crx_fixed_condition_code_regs (unsigned int *, unsigned int *);
127 static rtx crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
128 int incoming ATTRIBUTE_UNUSED);
129 static bool crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED);
130 static int crx_address_cost (rtx, bool);
131 static bool crx_legitimate_address_p (enum machine_mode, rtx, bool);
132 static bool crx_can_eliminate (const int, const int);
133 static rtx crx_function_arg (CUMULATIVE_ARGS *, enum machine_mode,
134 const_tree, bool);
135 static void crx_function_arg_advance (CUMULATIVE_ARGS *, enum machine_mode,
136 const_tree, bool);
138 /*****************************************************************************/
139 /* RTL VALIDITY */
140 /*****************************************************************************/
142 #undef TARGET_LEGITIMATE_ADDRESS_P
143 #define TARGET_LEGITIMATE_ADDRESS_P crx_legitimate_address_p
145 #undef TARGET_CAN_ELIMINATE
146 #define TARGET_CAN_ELIMINATE crx_can_eliminate
148 /*****************************************************************************/
149 /* STACK LAYOUT AND CALLING CONVENTIONS */
150 /*****************************************************************************/
152 #undef TARGET_FIXED_CONDITION_CODE_REGS
153 #define TARGET_FIXED_CONDITION_CODE_REGS crx_fixed_condition_code_regs
155 #undef TARGET_STRUCT_VALUE_RTX
156 #define TARGET_STRUCT_VALUE_RTX crx_struct_value_rtx
158 #undef TARGET_RETURN_IN_MEMORY
159 #define TARGET_RETURN_IN_MEMORY crx_return_in_memory
161 /*****************************************************************************/
162 /* PASSING FUNCTION ARGUMENTS */
163 /*****************************************************************************/
165 #undef TARGET_FUNCTION_ARG
166 #define TARGET_FUNCTION_ARG crx_function_arg
168 #undef TARGET_FUNCTION_ARG_ADVANCE
169 #define TARGET_FUNCTION_ARG_ADVANCE crx_function_arg_advance
171 /*****************************************************************************/
172 /* RELATIVE COSTS OF OPERATIONS */
173 /*****************************************************************************/
175 #undef TARGET_ADDRESS_COST
176 #define TARGET_ADDRESS_COST crx_address_cost
178 /*****************************************************************************/
179 /* TARGET-SPECIFIC USES OF `__attribute__' */
180 /*****************************************************************************/
182 #undef TARGET_ATTRIBUTE_TABLE
183 #define TARGET_ATTRIBUTE_TABLE crx_attribute_table
185 static const struct attribute_spec crx_attribute_table[] = {
186 /* ISRs have special prologue and epilogue requirements. */
187 {"interrupt", 0, 0, false, true, true, NULL},
188 {NULL, 0, 0, false, false, false, NULL}
191 /* Option handling. */
193 #undef TARGET_OPTION_OPTIMIZATION_TABLE
194 #define TARGET_OPTION_OPTIMIZATION_TABLE crx_option_optimization_table
196 static const struct default_options crx_option_optimization_table[] =
198 /* Put each function in its own section so that PAGE-instruction
199 relaxation can do its best. */
200 { OPT_LEVELS_1_PLUS, OPT_ffunction_sections, NULL, 1 },
201 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
202 { OPT_LEVELS_NONE, 0, NULL, 0 }
205 /* Initialize 'targetm' variable which contains pointers to functions and data
206 * relating to the target machine. */
208 struct gcc_target targetm = TARGET_INITIALIZER;
211 /*****************************************************************************/
212 /* TARGET HOOK IMPLEMENTATIONS */
213 /*****************************************************************************/
215 /* Return the fixed registers used for condition codes. */
217 static bool
218 crx_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
220 *p1 = CC_REGNUM;
221 *p2 = INVALID_REGNUM;
222 return true;
225 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
227 static rtx
228 crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
229 int incoming ATTRIBUTE_UNUSED)
231 return gen_rtx_REG (Pmode, CRX_STRUCT_VALUE_REGNUM);
234 /* Implements hook TARGET_RETURN_IN_MEMORY. */
236 static bool
237 crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
239 if (TYPE_MODE (type) == BLKmode)
241 HOST_WIDE_INT size = int_size_in_bytes (type);
242 return (size == -1 || size > 8);
244 else
245 return false;
249 /*****************************************************************************/
250 /* MACRO IMPLEMENTATIONS */
251 /*****************************************************************************/
253 /* STACK LAYOUT AND CALLING CONVENTIONS ROUTINES */
254 /* --------------------------------------------- */
256 /* Return nonzero if the current function being compiled is an interrupt
257 * function as specified by the "interrupt" attribute. */
260 crx_interrupt_function_p (void)
262 tree attributes;
264 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
265 return lookup_attribute ("interrupt", attributes) != NULL_TREE;
268 /* Compute values for the array save_regs and the variable sum_regs. The index
269 * of save_regs is numbers of register, each will get 1 if we need to save it
270 * in the current function, 0 if not. sum_regs is the total sum of the
271 * registers being saved. */
273 static void
274 crx_compute_save_regs (void)
276 unsigned int regno;
278 /* initialize here so in case the function is no-return it will be -1. */
279 last_reg_to_save = -1;
281 /* No need to save any registers if the function never returns. */
282 if (FUNC_IS_NORETURN_P (current_function_decl))
283 return;
285 /* Initialize the number of bytes to be saved. */
286 sum_regs = 0;
288 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
290 if (fixed_regs[regno])
292 save_regs[regno] = 0;
293 continue;
296 /* If this reg is used and not call-used (except RA), save it. */
297 if (crx_interrupt_function_p ())
299 if (!current_function_is_leaf && call_used_regs[regno])
300 /* this is a volatile reg in a non-leaf interrupt routine - save it
301 * for the sake of its sons. */
302 save_regs[regno] = 1;
304 else if (df_regs_ever_live_p (regno))
305 /* This reg is used - save it. */
306 save_regs[regno] = 1;
307 else
308 /* This reg is not used, and is not a volatile - don't save. */
309 save_regs[regno] = 0;
311 else
313 /* If this reg is used and not call-used (except RA), save it. */
314 if (df_regs_ever_live_p (regno)
315 && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM))
316 save_regs[regno] = 1;
317 else
318 save_regs[regno] = 0;
322 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
323 if (save_regs[regno] == 1)
325 last_reg_to_save = regno;
326 sum_regs += UNITS_PER_WORD;
330 /* Compute the size of the local area and the size to be adjusted by the
331 * prologue and epilogue. */
333 static void
334 crx_compute_frame (void)
336 /* For aligning the local variables. */
337 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
338 int padding_locals;
340 /* Padding needed for each element of the frame. */
341 local_vars_size = get_frame_size ();
343 /* Align to the stack alignment. */
344 padding_locals = local_vars_size % stack_alignment;
345 if (padding_locals)
346 padding_locals = stack_alignment - padding_locals;
348 local_vars_size += padding_locals;
350 size_for_adjusting_sp = local_vars_size + (ACCUMULATE_OUTGOING_ARGS ?
351 crtl->outgoing_args_size : 0);
354 /* Worker function for TARGET_CAN_ELIMINATE. */
356 bool
357 crx_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
359 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
362 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
365 crx_initial_elimination_offset (int from, int to)
367 /* Compute this since we need to use sum_regs. */
368 crx_compute_save_regs ();
370 /* Compute this since we need to use local_vars_size. */
371 crx_compute_frame ();
373 if ((from) == FRAME_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
374 return (ACCUMULATE_OUTGOING_ARGS ?
375 crtl->outgoing_args_size : 0);
376 else if ((from) == ARG_POINTER_REGNUM && (to) == FRAME_POINTER_REGNUM)
377 return (sum_regs + local_vars_size);
378 else if ((from) == ARG_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
379 return (sum_regs + local_vars_size +
380 (ACCUMULATE_OUTGOING_ARGS ?
381 crtl->outgoing_args_size : 0));
382 else
383 abort ();
386 /* REGISTER USAGE */
387 /* -------------- */
389 /* Return the class number of the smallest class containing reg number REGNO.
390 * This could be a conditional expression or could index an array. */
392 enum reg_class
393 crx_regno_reg_class (int regno)
395 if (regno >= 0 && regno < SP_REGNUM)
396 return NOSP_REGS;
398 if (regno == SP_REGNUM)
399 return GENERAL_REGS;
401 if (regno == LO_REGNUM)
402 return LO_REGS;
403 if (regno == HI_REGNUM)
404 return HI_REGS;
406 return NO_REGS;
409 /* Transfer between HILO_REGS and memory via secondary reloading. */
411 enum reg_class
412 crx_secondary_reload_class (enum reg_class rclass,
413 enum machine_mode mode ATTRIBUTE_UNUSED,
414 rtx x ATTRIBUTE_UNUSED)
416 if (reg_classes_intersect_p (rclass, HILO_REGS)
417 && true_regnum (x) == -1)
418 return GENERAL_REGS;
420 return NO_REGS;
423 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
426 crx_hard_regno_mode_ok (int regno, enum machine_mode mode)
428 /* CC can only hold CCmode values. */
429 if (regno == CC_REGNUM)
430 return GET_MODE_CLASS (mode) == MODE_CC;
431 if (GET_MODE_CLASS (mode) == MODE_CC)
432 return 0;
433 /* HILO registers can only hold SImode and DImode */
434 if (HILO_REGNO_P (regno))
435 return mode == SImode || mode == DImode;
436 return 1;
439 /* PASSING FUNCTION ARGUMENTS */
440 /* -------------------------- */
442 /* If enough param regs are available for passing the param of type TYPE return
443 * the number of registers needed else 0. */
445 static int
446 enough_regs_for_param (CUMULATIVE_ARGS * cum, const_tree type,
447 enum machine_mode mode)
449 int type_size;
450 int remaining_size;
452 if (mode != BLKmode)
453 type_size = GET_MODE_BITSIZE (mode);
454 else
455 type_size = int_size_in_bytes (type) * BITS_PER_UNIT;
457 remaining_size =
458 BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS -
459 (MIN_REG_FOR_PASSING_ARGS + cum->ints) + 1);
461 /* Any variable which is too big to pass in two registers, will pass on
462 * stack. */
463 if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD))
464 return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD;
466 return 0;
469 /* Implements TARGET_FUNCTION_ARG. */
471 static rtx
472 crx_function_arg (CUMULATIVE_ARGS * cum, enum machine_mode mode,
473 const_tree type, bool named ATTRIBUTE_UNUSED)
475 last_parm_in_reg = 0;
477 /* Function_arg () is called with this type just after all the args have had
478 * their registers assigned. The rtx that function_arg returns from this type
479 * is supposed to pass to 'gen_call' but currently it is not implemented (see
480 * macro GEN_CALL). */
481 if (type == void_type_node)
482 return NULL_RTX;
484 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
485 return NULL_RTX;
487 if (mode == BLKmode)
489 /* Enable structures that need padding bytes at the end to pass to a
490 * function in registers. */
491 if (enough_regs_for_param (cum, type, mode) != 0)
493 last_parm_in_reg = 1;
494 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
498 if (MIN_REG_FOR_PASSING_ARGS + cum->ints > MAX_REG_FOR_PASSING_ARGS)
499 return NULL_RTX;
500 else
502 if (enough_regs_for_param (cum, type, mode) != 0)
504 last_parm_in_reg = 1;
505 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
509 return NULL_RTX;
512 /* Implements the macro INIT_CUMULATIVE_ARGS defined in crx.h. */
514 void
515 crx_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype,
516 rtx libfunc ATTRIBUTE_UNUSED)
518 tree param, next_param;
520 cum->ints = 0;
522 /* Determine if this function has variable arguments. This is indicated by
523 * the last argument being 'void_type_mode' if there are no variable
524 * arguments. Change here for a different vararg. */
525 for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
526 param != (tree) 0; param = next_param)
528 next_param = TREE_CHAIN (param);
529 if (next_param == (tree) 0 && TREE_VALUE (param) != void_type_node)
531 cum->ints = -1;
532 return;
537 /* Implements TARGET_FUNCTION_ARG_ADVANCE. */
539 static void
540 crx_function_arg_advance (CUMULATIVE_ARGS * cum, enum machine_mode mode,
541 const_tree type, bool named ATTRIBUTE_UNUSED)
543 /* l holds the number of registers required */
544 int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
546 /* If the parameter isn't passed on a register don't advance cum. */
547 if (!last_parm_in_reg)
548 return;
550 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
551 return;
553 if (mode == SImode || mode == HImode || mode == QImode || mode == DImode)
555 if (l <= 1)
556 cum->ints += 1;
557 else
558 cum->ints += l;
560 else if (mode == SFmode || mode == DFmode)
561 cum->ints += l;
562 else if ((mode) == BLKmode)
564 if ((l = enough_regs_for_param (cum, type, mode)) != 0)
565 cum->ints += l;
570 /* Implements the macro FUNCTION_ARG_REGNO_P defined in crx.h. Return nonzero
571 * if N is a register used for passing parameters. */
574 crx_function_arg_regno_p (int n)
576 return (n <= MAX_REG_FOR_PASSING_ARGS && n >= MIN_REG_FOR_PASSING_ARGS);
579 /* ADDRESSING MODES */
580 /* ---------------- */
582 /* Implements the hook for TARGET_LEGITIMATE_ADDRESS_P defined in crx.h.
583 * The following addressing modes are supported on CRX:
585 * Relocations --> const | symbol_ref | label_ref
586 * Absolute address --> 32-bit absolute
587 * Post increment --> reg + 12-bit disp.
588 * Post modify --> reg + 12-bit disp.
589 * Register relative --> reg | 32-bit disp. + reg | 4 bit + reg
590 * Scaled index --> reg + reg | 22-bit disp. + reg + reg |
591 * 22-disp. + reg + reg + (2 | 4 | 8) */
593 static rtx
594 crx_addr_reg (rtx addr_reg)
596 if (GET_MODE (addr_reg) != Pmode)
597 return NULL_RTX;
599 if (REG_P (addr_reg))
600 return addr_reg;
601 else if (GET_CODE (addr_reg) == SUBREG
602 && REG_P (SUBREG_REG (addr_reg))
603 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg)))
604 <= UNITS_PER_WORD))
605 return SUBREG_REG (addr_reg);
606 else
607 return NULL_RTX;
610 enum crx_addrtype
611 crx_decompose_address (rtx addr, struct crx_address *out)
613 rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
614 rtx scale_rtx = NULL_RTX, side_effect = NULL_RTX;
615 int scale = -1;
617 enum crx_addrtype retval = CRX_INVALID;
619 switch (GET_CODE (addr))
621 case CONST_INT:
622 /* Absolute address (known at compile time) */
623 retval = CRX_ABSOLUTE;
624 disp = addr;
625 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), GET_MODE_BITSIZE (Pmode)))
626 return CRX_INVALID;
627 break;
629 case CONST:
630 case SYMBOL_REF:
631 case LABEL_REF:
632 /* Absolute address (known at link time) */
633 retval = CRX_ABSOLUTE;
634 disp = addr;
635 break;
637 case REG:
638 case SUBREG:
639 /* Register relative address */
640 retval = CRX_REG_REL;
641 base = addr;
642 break;
644 case PLUS:
645 switch (GET_CODE (XEXP (addr, 0)))
647 case REG:
648 case SUBREG:
649 if (REG_P (XEXP (addr, 1)))
651 /* Scaled index with scale = 1 and disp. = 0 */
652 retval = CRX_SCALED_INDX;
653 base = XEXP (addr, 1);
654 index = XEXP (addr, 0);
655 scale = 1;
657 else if (RTX_SIGNED_INT_FITS_N_BITS (XEXP (addr, 1), 28))
659 /* Register relative address and <= 28-bit disp. */
660 retval = CRX_REG_REL;
661 base = XEXP (addr, 0);
662 disp = XEXP (addr, 1);
664 else
665 return CRX_INVALID;
666 break;
668 case PLUS:
669 /* Scaled index and <= 22-bit disp. */
670 retval = CRX_SCALED_INDX;
671 base = XEXP (XEXP (addr, 0), 1);
672 disp = XEXP (addr, 1);
673 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 22))
674 return CRX_INVALID;
675 switch (GET_CODE (XEXP (XEXP (addr, 0), 0)))
677 case REG:
678 /* Scaled index with scale = 0 and <= 22-bit disp. */
679 index = XEXP (XEXP (addr, 0), 0);
680 scale = 1;
681 break;
683 case MULT:
684 /* Scaled index with scale >= 0 and <= 22-bit disp. */
685 index = XEXP (XEXP (XEXP (addr, 0), 0), 0);
686 scale_rtx = XEXP (XEXP (XEXP (addr, 0), 0), 1);
687 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
688 return CRX_INVALID;
689 break;
691 default:
692 return CRX_INVALID;
694 break;
696 case MULT:
697 /* Scaled index with scale >= 0 */
698 retval = CRX_SCALED_INDX;
699 base = XEXP (addr, 1);
700 index = XEXP (XEXP (addr, 0), 0);
701 scale_rtx = XEXP (XEXP (addr, 0), 1);
702 /* Scaled index with scale >= 0 and <= 22-bit disp. */
703 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
704 return CRX_INVALID;
705 break;
707 default:
708 return CRX_INVALID;
710 break;
712 case POST_INC:
713 case POST_DEC:
714 /* Simple post-increment */
715 retval = CRX_POST_INC;
716 base = XEXP (addr, 0);
717 side_effect = addr;
718 break;
720 case POST_MODIFY:
721 /* Generic post-increment with <= 12-bit disp. */
722 retval = CRX_POST_INC;
723 base = XEXP (addr, 0);
724 side_effect = XEXP (addr, 1);
725 if (base != XEXP (side_effect, 0))
726 return CRX_INVALID;
727 switch (GET_CODE (side_effect))
729 case PLUS:
730 case MINUS:
731 disp = XEXP (side_effect, 1);
732 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 12))
733 return CRX_INVALID;
734 break;
736 default:
737 /* CRX only supports PLUS and MINUS */
738 return CRX_INVALID;
740 break;
742 default:
743 return CRX_INVALID;
746 if (base)
748 base = crx_addr_reg (base);
749 if (!base)
750 return CRX_INVALID;
752 if (index)
754 index = crx_addr_reg (index);
755 if (!index)
756 return CRX_INVALID;
759 out->base = base;
760 out->index = index;
761 out->disp = disp;
762 out->scale = scale;
763 out->side_effect = side_effect;
765 return retval;
768 bool
769 crx_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
770 rtx addr, bool strict)
772 enum crx_addrtype addrtype;
773 struct crx_address address;
775 if (TARGET_DEBUG_ADDR)
777 fprintf (stderr,
778 "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
779 GET_MODE_NAME (mode), strict);
780 debug_rtx (addr);
783 addrtype = crx_decompose_address (addr, &address);
785 if (addrtype == CRX_POST_INC && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
786 return FALSE;
788 if (TARGET_DEBUG_ADDR)
790 const char *typestr;
791 switch (addrtype)
793 case CRX_INVALID:
794 typestr = "Invalid";
795 break;
796 case CRX_REG_REL:
797 typestr = "Register relative";
798 break;
799 case CRX_POST_INC:
800 typestr = "Post-increment";
801 break;
802 case CRX_SCALED_INDX:
803 typestr = "Scaled index";
804 break;
805 case CRX_ABSOLUTE:
806 typestr = "Absolute";
807 break;
808 default:
809 abort ();
811 fprintf (stderr, "CRX Address type: %s\n", typestr);
814 if (addrtype == CRX_INVALID)
815 return FALSE;
817 if (strict)
819 if (address.base && !REGNO_OK_FOR_BASE_P (REGNO (address.base)))
821 if (TARGET_DEBUG_ADDR)
822 fprintf (stderr, "Base register not strict\n");
823 return FALSE;
825 if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index)))
827 if (TARGET_DEBUG_ADDR)
828 fprintf (stderr, "Index register not strict\n");
829 return FALSE;
833 return TRUE;
836 /* ROUTINES TO COMPUTE COSTS */
837 /* ------------------------- */
839 /* Return cost of the memory address x. */
841 static int
842 crx_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
844 enum crx_addrtype addrtype;
845 struct crx_address address;
847 int cost = 2;
849 addrtype = crx_decompose_address (addr, &address);
851 gcc_assert (addrtype != CRX_INVALID);
853 /* An absolute address causes a 3-word instruction */
854 if (addrtype == CRX_ABSOLUTE)
855 cost+=2;
857 /* Post-modifying addresses are more powerful. */
858 if (addrtype == CRX_POST_INC)
859 cost-=2;
861 /* Attempt to minimize number of registers in the address. */
862 if (address.base)
863 cost++;
865 if (address.index && address.scale == 1)
866 cost+=5;
868 if (address.disp && !INT_CST4 (INTVAL (address.disp)))
869 cost+=2;
871 if (TARGET_DEBUG_ADDR)
873 fprintf (stderr, "\n======\nTARGET_ADDRESS_COST = %d\n", cost);
874 debug_rtx (addr);
877 return cost;
880 /* Return the cost of moving data of mode MODE between a register of class
881 * RCLASS and memory; IN is zero if the value is to be written to memory,
882 * nonzero if it is to be read in. This cost is relative to those in
883 * REGISTER_MOVE_COST. */
886 crx_memory_move_cost (enum machine_mode mode,
887 enum reg_class rclass ATTRIBUTE_UNUSED,
888 int in ATTRIBUTE_UNUSED)
890 /* One LD or ST takes twice the time of a simple reg-reg move */
891 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
893 /* printf ("GENERAL_REGS LD/ST = %d\n", 4 * HARD_REGNO_NREGS (0, mode));*/
894 return 4 * HARD_REGNO_NREGS (0, mode);
896 else if (reg_classes_intersect_p (rclass, HILO_REGS))
898 /* HILO to memory and vice versa */
899 /* printf ("HILO_REGS %s = %d\n", in ? "LD" : "ST",
900 (REGISTER_MOVE_COST (mode,
901 in ? GENERAL_REGS : HILO_REGS,
902 in ? HILO_REGS : GENERAL_REGS) + 4)
903 * HARD_REGNO_NREGS (0, mode)); */
904 return (REGISTER_MOVE_COST (mode,
905 in ? GENERAL_REGS : HILO_REGS,
906 in ? HILO_REGS : GENERAL_REGS) + 4)
907 * HARD_REGNO_NREGS (0, mode);
909 else /* default (like in i386) */
911 /* printf ("ANYREGS = 100\n"); */
912 return 100;
916 /* INSTRUCTION OUTPUT */
917 /* ------------------ */
919 /* Check if a const_double is ok for crx store-immediate instructions */
922 crx_const_double_ok (rtx op)
924 if (GET_MODE (op) == DFmode)
926 REAL_VALUE_TYPE r;
927 long l[2];
928 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
929 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
930 return (UNSIGNED_INT_FITS_N_BITS (l[0], 4) &&
931 UNSIGNED_INT_FITS_N_BITS (l[1], 4)) ? 1 : 0;
934 if (GET_MODE (op) == SFmode)
936 REAL_VALUE_TYPE r;
937 long l;
938 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
939 REAL_VALUE_TO_TARGET_SINGLE (r, l);
940 return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0;
943 return (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4) &&
944 UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4)) ? 1 : 0;
947 /* Implements the macro PRINT_OPERAND defined in crx.h. */
949 void
950 crx_print_operand (FILE * file, rtx x, int code)
952 switch (code)
954 case 'p' :
955 if (GET_CODE (x) == REG) {
956 if (GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
958 int regno = REGNO (x);
959 if (regno + 1 >= SP_REGNUM) abort ();
960 fprintf (file, "{%s, %s}", reg_names[regno], reg_names[regno + 1]);
961 return;
963 else
965 if (REGNO (x) >= SP_REGNUM) abort ();
966 fprintf (file, "%s", reg_names[REGNO (x)]);
967 return;
971 case 'd' :
973 const char *crx_cmp_str;
974 switch (GET_CODE (x))
975 { /* MD: compare (reg, reg or imm) but CRX: cmp (reg or imm, reg)
976 * -> swap all non symmetric ops */
977 case EQ : crx_cmp_str = "eq"; break;
978 case NE : crx_cmp_str = "ne"; break;
979 case GT : crx_cmp_str = "lt"; break;
980 case GTU : crx_cmp_str = "lo"; break;
981 case LT : crx_cmp_str = "gt"; break;
982 case LTU : crx_cmp_str = "hi"; break;
983 case GE : crx_cmp_str = "le"; break;
984 case GEU : crx_cmp_str = "ls"; break;
985 case LE : crx_cmp_str = "ge"; break;
986 case LEU : crx_cmp_str = "hs"; break;
987 default : abort ();
989 fprintf (file, "%s", crx_cmp_str);
990 return;
993 case 'H':
994 /* Print high part of a double precision value. */
995 switch (GET_CODE (x))
997 case CONST_DOUBLE:
998 if (GET_MODE (x) == SFmode) abort ();
999 if (GET_MODE (x) == DFmode)
1001 /* High part of a DF const. */
1002 REAL_VALUE_TYPE r;
1003 long l[2];
1005 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1006 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
1008 fprintf (file, "$0x%lx", l[1]);
1009 return;
1012 /* -- Fallthrough to handle DI consts -- */
1014 case CONST_INT:
1016 rtx high, low;
1017 split_double (x, &low, &high);
1018 putc ('$', file);
1019 output_addr_const (file, high);
1020 return;
1023 case REG:
1024 if (REGNO (x) + 1 >= FIRST_PSEUDO_REGISTER) abort ();
1025 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
1026 return;
1028 case MEM:
1029 /* Adjust memory address to high part. */
1031 rtx adj_mem = x;
1032 adj_mem = adjust_address (adj_mem, GET_MODE (adj_mem), 4);
1034 output_memory_reference_mode = GET_MODE (adj_mem);
1035 output_address (XEXP (adj_mem, 0));
1036 return;
1039 default:
1040 abort ();
1043 case 'L':
1044 /* Print low part of a double precision value. */
1045 switch (GET_CODE (x))
1047 case CONST_DOUBLE:
1048 if (GET_MODE (x) == SFmode) abort ();
1049 if (GET_MODE (x) == DFmode)
1051 /* High part of a DF const. */
1052 REAL_VALUE_TYPE r;
1053 long l[2];
1055 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1056 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
1058 fprintf (file, "$0x%lx", l[0]);
1059 return;
1062 /* -- Fallthrough to handle DI consts -- */
1064 case CONST_INT:
1066 rtx high, low;
1067 split_double (x, &low, &high);
1068 putc ('$', file);
1069 output_addr_const (file, low);
1070 return;
1073 case REG:
1074 fprintf (file, "%s", reg_names[REGNO (x)]);
1075 return;
1077 case MEM:
1078 output_memory_reference_mode = GET_MODE (x);
1079 output_address (XEXP (x, 0));
1080 return;
1082 default:
1083 abort ();
1086 case 0 : /* default */
1087 switch (GET_CODE (x))
1089 case REG:
1090 fprintf (file, "%s", reg_names[REGNO (x)]);
1091 return;
1093 case MEM:
1094 output_memory_reference_mode = GET_MODE (x);
1095 output_address (XEXP (x, 0));
1096 return;
1098 case CONST_DOUBLE:
1100 REAL_VALUE_TYPE r;
1101 long l;
1103 /* Always use H and L for double precision - see above */
1104 gcc_assert (GET_MODE (x) == SFmode);
1106 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1107 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1109 fprintf (file, "$0x%lx", l);
1110 return;
1113 default:
1114 putc ('$', file);
1115 output_addr_const (file, x);
1116 return;
1119 default:
1120 output_operand_lossage ("invalid %%xn code");
1123 abort ();
1126 /* Implements the macro PRINT_OPERAND_ADDRESS defined in crx.h. */
1128 void
1129 crx_print_operand_address (FILE * file, rtx addr)
1131 enum crx_addrtype addrtype;
1132 struct crx_address address;
1134 int offset;
1136 addrtype = crx_decompose_address (addr, &address);
1138 if (address.disp)
1139 offset = INTVAL (address.disp);
1140 else
1141 offset = 0;
1143 switch (addrtype)
1145 case CRX_REG_REL:
1146 fprintf (file, "%d(%s)", offset, reg_names[REGNO (address.base)]);
1147 return;
1149 case CRX_POST_INC:
1150 switch (GET_CODE (address.side_effect))
1152 case PLUS:
1153 break;
1154 case MINUS:
1155 offset = -offset;
1156 break;
1157 case POST_INC:
1158 offset = GET_MODE_SIZE (output_memory_reference_mode);
1159 break;
1160 case POST_DEC:
1161 offset = -GET_MODE_SIZE (output_memory_reference_mode);
1162 break;
1163 default:
1164 abort ();
1166 fprintf (file, "%d(%s)+", offset, reg_names[REGNO (address.base)]);
1167 return;
1169 case CRX_SCALED_INDX:
1170 fprintf (file, "%d(%s, %s, %d)", offset, reg_names[REGNO (address.base)],
1171 reg_names[REGNO (address.index)], address.scale);
1172 return;
1174 case CRX_ABSOLUTE:
1175 output_addr_const (file, address.disp);
1176 return;
1178 default:
1179 abort ();
1184 /*****************************************************************************/
1185 /* MACHINE DESCRIPTION HELPER-FUNCTIONS */
1186 /*****************************************************************************/
1188 void crx_expand_movmem_single (rtx src, rtx srcbase, rtx dst, rtx dstbase,
1189 rtx tmp_reg, unsigned HOST_WIDE_INT *offset_p)
1191 rtx addr, mem;
1192 unsigned HOST_WIDE_INT offset = *offset_p;
1194 /* Load */
1195 addr = plus_constant (src, offset);
1196 mem = adjust_automodify_address (srcbase, SImode, addr, offset);
1197 emit_move_insn (tmp_reg, mem);
1199 /* Store */
1200 addr = plus_constant (dst, offset);
1201 mem = adjust_automodify_address (dstbase, SImode, addr, offset);
1202 emit_move_insn (mem, tmp_reg);
1204 *offset_p = offset + 4;
1208 crx_expand_movmem (rtx dstbase, rtx srcbase, rtx count_exp, rtx align_exp)
1210 unsigned HOST_WIDE_INT count = 0, offset, si_moves, i;
1211 HOST_WIDE_INT align = 0;
1213 rtx src, dst;
1214 rtx tmp_reg;
1216 if (GET_CODE (align_exp) == CONST_INT)
1217 { /* Only if aligned */
1218 align = INTVAL (align_exp);
1219 if (align & 3)
1220 return 0;
1223 if (GET_CODE (count_exp) == CONST_INT)
1224 { /* No more than 16 SImode moves */
1225 count = INTVAL (count_exp);
1226 if (count > 64)
1227 return 0;
1230 tmp_reg = gen_reg_rtx (SImode);
1232 /* Create psrs for the src and dest pointers */
1233 dst = copy_to_mode_reg (Pmode, XEXP (dstbase, 0));
1234 if (dst != XEXP (dstbase, 0))
1235 dstbase = replace_equiv_address_nv (dstbase, dst);
1236 src = copy_to_mode_reg (Pmode, XEXP (srcbase, 0));
1237 if (src != XEXP (srcbase, 0))
1238 srcbase = replace_equiv_address_nv (srcbase, src);
1240 offset = 0;
1242 /* Emit SImode moves */
1243 si_moves = count >> 2;
1244 for (i = 0; i < si_moves; i++)
1245 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1247 /* Special cases */
1248 if (count & 3)
1250 offset = count - 4;
1251 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1254 gcc_assert (offset == count);
1256 return 1;
1259 static void
1260 mpushpop_str (char *stringbuffer, const char *mnemonic, char *mask)
1262 if (strlen (mask) > 2 || crx_interrupt_function_p ()) /* needs 2-word instr. */
1263 sprintf (stringbuffer, "\n\t%s\tsp, {%s}", mnemonic, mask);
1264 else /* single word instruction */
1265 sprintf (stringbuffer, "\n\t%s\t%s", mnemonic, mask);
1268 /* Called from crx.md. The return value depends on the parameter push_or_pop:
1269 * When push_or_pop is zero -> string for push instructions of prologue.
1270 * When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1271 * Relies on the assumptions:
1272 * 1. RA is the last register to be saved.
1273 * 2. The maximal value of the counter is MAX_COUNT. */
1275 char *
1276 crx_prepare_push_pop_string (int push_or_pop)
1278 /* j is the number of registers being saved, takes care that there won't be
1279 * more than 8 in one push/pop instruction */
1281 /* For the register mask string */
1282 static char mask_str[50];
1284 /* i is the index of save_regs[], going from 0 until last_reg_to_save */
1285 int i = 0;
1287 int ra_in_bitmask = 0;
1289 char *return_str;
1291 /* For reversing on the push instructions if there are more than one. */
1292 char *temp_str;
1294 return_str = (char *) xmalloc (120);
1295 temp_str = (char *) xmalloc (120);
1297 /* Initialize */
1298 memset (return_str, 0, 3);
1300 while (i <= last_reg_to_save)
1302 /* Prepare mask for one instruction. */
1303 mask_str[0] = 0;
1305 if (i <= SP_REGNUM)
1306 { /* Add regs unit full or SP register reached */
1307 int j = 0;
1308 while (j < MAX_COUNT && i <= SP_REGNUM)
1310 if (save_regs[i])
1312 /* TODO to use ra_in_bitmask for detecting last pop is not
1313 * smart it prevents things like: popret r5 */
1314 if (i == RETURN_ADDRESS_REGNUM) ra_in_bitmask = 1;
1315 if (j > 0) strcat (mask_str, ", ");
1316 strcat (mask_str, reg_names[i]);
1317 ++j;
1319 ++i;
1322 else
1324 /* Handle hi/lo savings */
1325 while (i <= last_reg_to_save)
1327 if (save_regs[i])
1329 strcat (mask_str, "lo, hi");
1330 i = last_reg_to_save + 1;
1331 break;
1333 ++i;
1337 if (strlen (mask_str) == 0) continue;
1339 if (push_or_pop == 1)
1341 if (crx_interrupt_function_p ())
1342 mpushpop_str (temp_str, "popx", mask_str);
1343 else
1345 if (ra_in_bitmask)
1347 mpushpop_str (temp_str, "popret", mask_str);
1348 ra_in_bitmask = 0;
1350 else mpushpop_str (temp_str, "pop", mask_str);
1353 strcat (return_str, temp_str);
1355 else
1357 /* push - We need to reverse the order of the instructions if there
1358 * are more than one. (since the pop will not be reversed in the
1359 * epilogue */
1360 if (crx_interrupt_function_p ())
1361 mpushpop_str (temp_str, "pushx", mask_str);
1362 else
1363 mpushpop_str (temp_str, "push", mask_str);
1364 strcat (temp_str, return_str);
1365 strcpy (strcat (return_str, "\t"), temp_str);
1370 if (push_or_pop == 1)
1372 /* pop */
1373 if (crx_interrupt_function_p ())
1374 strcat (return_str, "\n\tretx\n");
1376 else if (!FUNC_IS_NORETURN_P (current_function_decl)
1377 && !save_regs[RETURN_ADDRESS_REGNUM])
1378 strcat (return_str, "\n\tjump\tra\n");
1381 /* Skip the newline and the tab in the start of return_str. */
1382 return_str += 2;
1383 return return_str;
1386 /* CompactRISC CRX Architecture stack layout:
1388 0 +---------------------
1393 +==================== Sp(x)=Ap(x+1)
1394 A | Args for functions
1395 | | called by X and Dynamically
1396 | | Dynamic allocations allocated and
1397 | | (alloca, variable deallocated
1398 Stack | length arrays).
1399 grows +-------------------- Fp(x)
1400 down| | Local variables of X
1401 ward| +--------------------
1402 | | Regs saved for X-1
1403 | +==================== Sp(x-1)=Ap(x)
1404 | Args for func X
1405 | pushed by X-1
1406 +-------------------- Fp(x-1)
1413 void
1414 crx_expand_prologue (void)
1416 crx_compute_frame ();
1417 crx_compute_save_regs ();
1419 /* If there is no need in push and adjustment to sp, return. */
1420 if (size_for_adjusting_sp + sum_regs == 0)
1421 return;
1423 if (last_reg_to_save != -1)
1424 /* If there are registers to push. */
1425 emit_insn (gen_push_for_prologue (GEN_INT (sum_regs)));
1427 if (size_for_adjusting_sp > 0)
1428 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1429 GEN_INT (-size_for_adjusting_sp)));
1431 if (frame_pointer_needed)
1432 /* Initialize the frame pointer with the value of the stack pointer
1433 * pointing now to the locals. */
1434 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1437 /* Generate insn that updates the stack for local variables and padding for
1438 * registers we save. - Generate the appropriate return insn. */
1440 void
1441 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 if (frame_pointer_needed)
1450 /* Restore the stack pointer with the frame pointers value */
1451 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1453 if (size_for_adjusting_sp > 0)
1454 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1455 GEN_INT (size_for_adjusting_sp)));
1457 if (crx_interrupt_function_p ())
1458 emit_jump_insn (gen_interrupt_return ());
1459 else if (last_reg_to_save == -1)
1460 /* Nothing to pop */
1461 /* Don't output jump for interrupt routine, only retx. */
1462 emit_jump_insn (gen_indirect_jump_return ());
1463 else if (only_popret_RA)
1464 emit_jump_insn (gen_popret_RA_return ());
1465 else
1466 emit_jump_insn (gen_pop_and_popret_return (GEN_INT (sum_regs)));