Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / gcc / config / crx / crx.c
blob8ae552fe5db8259869d33eb6f6ceced231776aaf
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 void crx_option_optimization (int, int);
135 /*****************************************************************************/
136 /* RTL VALIDITY */
137 /*****************************************************************************/
139 #undef TARGET_LEGITIMATE_ADDRESS_P
140 #define TARGET_LEGITIMATE_ADDRESS_P crx_legitimate_address_p
142 #undef TARGET_CAN_ELIMINATE
143 #define TARGET_CAN_ELIMINATE crx_can_eliminate
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 static 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}
178 /* Option handling. */
180 #undef TARGET_OPTION_OPTIMIZATION
181 #define TARGET_OPTION_OPTIMIZATION crx_option_optimization
183 /* Initialize 'targetm' variable which contains pointers to functions and data
184 * relating to the target machine. */
186 struct gcc_target targetm = TARGET_INITIALIZER;
189 /*****************************************************************************/
190 /* TARGET HOOK IMPLEMENTATIONS */
191 /*****************************************************************************/
193 /* Return the fixed registers used for condition codes. */
195 static bool
196 crx_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
198 *p1 = CC_REGNUM;
199 *p2 = INVALID_REGNUM;
200 return true;
203 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
205 static rtx
206 crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
207 int incoming ATTRIBUTE_UNUSED)
209 return gen_rtx_REG (Pmode, CRX_STRUCT_VALUE_REGNUM);
212 /* Implements hook TARGET_RETURN_IN_MEMORY. */
214 static bool
215 crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
217 if (TYPE_MODE (type) == BLKmode)
219 HOST_WIDE_INT size = int_size_in_bytes (type);
220 return (size == -1 || size > 8);
222 else
223 return false;
227 /*****************************************************************************/
228 /* MACRO IMPLEMENTATIONS */
229 /*****************************************************************************/
231 /* STACK LAYOUT AND CALLING CONVENTIONS ROUTINES */
232 /* --------------------------------------------- */
234 /* Return nonzero if the current function being compiled is an interrupt
235 * function as specified by the "interrupt" attribute. */
238 crx_interrupt_function_p (void)
240 tree attributes;
242 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
243 return lookup_attribute ("interrupt", attributes) != NULL_TREE;
246 /* Compute values for the array save_regs and the variable sum_regs. The index
247 * of save_regs is numbers of register, each will get 1 if we need to save it
248 * in the current function, 0 if not. sum_regs is the total sum of the
249 * registers being saved. */
251 static void
252 crx_compute_save_regs (void)
254 unsigned int regno;
256 /* initialize here so in case the function is no-return it will be -1. */
257 last_reg_to_save = -1;
259 /* No need to save any registers if the function never returns. */
260 if (FUNC_IS_NORETURN_P (current_function_decl))
261 return;
263 /* Initialize the number of bytes to be saved. */
264 sum_regs = 0;
266 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
268 if (fixed_regs[regno])
270 save_regs[regno] = 0;
271 continue;
274 /* If this reg is used and not call-used (except RA), save it. */
275 if (crx_interrupt_function_p ())
277 if (!current_function_is_leaf && call_used_regs[regno])
278 /* this is a volatile reg in a non-leaf interrupt routine - save it
279 * for the sake of its sons. */
280 save_regs[regno] = 1;
282 else if (df_regs_ever_live_p (regno))
283 /* This reg is used - save it. */
284 save_regs[regno] = 1;
285 else
286 /* This reg is not used, and is not a volatile - don't save. */
287 save_regs[regno] = 0;
289 else
291 /* If this reg is used and not call-used (except RA), save it. */
292 if (df_regs_ever_live_p (regno)
293 && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM))
294 save_regs[regno] = 1;
295 else
296 save_regs[regno] = 0;
300 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
301 if (save_regs[regno] == 1)
303 last_reg_to_save = regno;
304 sum_regs += UNITS_PER_WORD;
308 /* Compute the size of the local area and the size to be adjusted by the
309 * prologue and epilogue. */
311 static void
312 crx_compute_frame (void)
314 /* For aligning the local variables. */
315 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
316 int padding_locals;
318 /* Padding needed for each element of the frame. */
319 local_vars_size = get_frame_size ();
321 /* Align to the stack alignment. */
322 padding_locals = local_vars_size % stack_alignment;
323 if (padding_locals)
324 padding_locals = stack_alignment - padding_locals;
326 local_vars_size += padding_locals;
328 size_for_adjusting_sp = local_vars_size + (ACCUMULATE_OUTGOING_ARGS ?
329 crtl->outgoing_args_size : 0);
332 /* Worker function for TARGET_CAN_ELIMINATE. */
334 bool
335 crx_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
337 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
340 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
343 crx_initial_elimination_offset (int from, int to)
345 /* Compute this since we need to use sum_regs. */
346 crx_compute_save_regs ();
348 /* Compute this since we need to use local_vars_size. */
349 crx_compute_frame ();
351 if ((from) == FRAME_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
352 return (ACCUMULATE_OUTGOING_ARGS ?
353 crtl->outgoing_args_size : 0);
354 else if ((from) == ARG_POINTER_REGNUM && (to) == FRAME_POINTER_REGNUM)
355 return (sum_regs + local_vars_size);
356 else if ((from) == ARG_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
357 return (sum_regs + local_vars_size +
358 (ACCUMULATE_OUTGOING_ARGS ?
359 crtl->outgoing_args_size : 0));
360 else
361 abort ();
364 /* REGISTER USAGE */
365 /* -------------- */
367 /* Return the class number of the smallest class containing reg number REGNO.
368 * This could be a conditional expression or could index an array. */
370 enum reg_class
371 crx_regno_reg_class (int regno)
373 if (regno >= 0 && regno < SP_REGNUM)
374 return NOSP_REGS;
376 if (regno == SP_REGNUM)
377 return GENERAL_REGS;
379 if (regno == LO_REGNUM)
380 return LO_REGS;
381 if (regno == HI_REGNUM)
382 return HI_REGS;
384 return NO_REGS;
387 /* Transfer between HILO_REGS and memory via secondary reloading. */
389 enum reg_class
390 crx_secondary_reload_class (enum reg_class rclass,
391 enum machine_mode mode ATTRIBUTE_UNUSED,
392 rtx x ATTRIBUTE_UNUSED)
394 if (reg_classes_intersect_p (rclass, HILO_REGS)
395 && true_regnum (x) == -1)
396 return GENERAL_REGS;
398 return NO_REGS;
401 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
404 crx_hard_regno_mode_ok (int regno, enum machine_mode mode)
406 /* CC can only hold CCmode values. */
407 if (regno == CC_REGNUM)
408 return GET_MODE_CLASS (mode) == MODE_CC;
409 if (GET_MODE_CLASS (mode) == MODE_CC)
410 return 0;
411 /* HILO registers can only hold SImode and DImode */
412 if (HILO_REGNO_P (regno))
413 return mode == SImode || mode == DImode;
414 return 1;
417 /* PASSING FUNCTION ARGUMENTS */
418 /* -------------------------- */
420 /* If enough param regs are available for passing the param of type TYPE return
421 * the number of registers needed else 0. */
423 static int
424 enough_regs_for_param (CUMULATIVE_ARGS * cum, tree type,
425 enum machine_mode mode)
427 int type_size;
428 int remaining_size;
430 if (mode != BLKmode)
431 type_size = GET_MODE_BITSIZE (mode);
432 else
433 type_size = int_size_in_bytes (type) * BITS_PER_UNIT;
435 remaining_size =
436 BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS -
437 (MIN_REG_FOR_PASSING_ARGS + cum->ints) + 1);
439 /* Any variable which is too big to pass in two registers, will pass on
440 * stack. */
441 if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD))
442 return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD;
444 return 0;
447 /* Implements the macro FUNCTION_ARG defined in crx.h. */
450 crx_function_arg (CUMULATIVE_ARGS * cum, enum machine_mode mode, tree type,
451 int named ATTRIBUTE_UNUSED)
453 last_parm_in_reg = 0;
455 /* Function_arg () is called with this type just after all the args have had
456 * their registers assigned. The rtx that function_arg returns from this type
457 * is supposed to pass to 'gen_call' but currently it is not implemented (see
458 * macro GEN_CALL). */
459 if (type == void_type_node)
460 return NULL_RTX;
462 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
463 return NULL_RTX;
465 if (mode == BLKmode)
467 /* Enable structures that need padding bytes at the end to pass to a
468 * function in registers. */
469 if (enough_regs_for_param (cum, type, mode) != 0)
471 last_parm_in_reg = 1;
472 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
476 if (MIN_REG_FOR_PASSING_ARGS + cum->ints > MAX_REG_FOR_PASSING_ARGS)
477 return NULL_RTX;
478 else
480 if (enough_regs_for_param (cum, type, mode) != 0)
482 last_parm_in_reg = 1;
483 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
487 return NULL_RTX;
490 /* Implements the macro INIT_CUMULATIVE_ARGS defined in crx.h. */
492 void
493 crx_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype,
494 rtx libfunc ATTRIBUTE_UNUSED)
496 tree param, next_param;
498 cum->ints = 0;
500 /* Determine if this function has variable arguments. This is indicated by
501 * the last argument being 'void_type_mode' if there are no variable
502 * arguments. Change here for a different vararg. */
503 for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
504 param != (tree) 0; param = next_param)
506 next_param = TREE_CHAIN (param);
507 if (next_param == (tree) 0 && TREE_VALUE (param) != void_type_node)
509 cum->ints = -1;
510 return;
515 /* Implements the macro FUNCTION_ARG_ADVANCE defined in crx.h. */
517 void
518 crx_function_arg_advance (CUMULATIVE_ARGS * cum, enum machine_mode mode,
519 tree type, int named ATTRIBUTE_UNUSED)
521 /* l holds the number of registers required */
522 int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
524 /* If the parameter isn't passed on a register don't advance cum. */
525 if (!last_parm_in_reg)
526 return;
528 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
529 return;
531 if (mode == SImode || mode == HImode || mode == QImode || mode == DImode)
533 if (l <= 1)
534 cum->ints += 1;
535 else
536 cum->ints += l;
538 else if (mode == SFmode || mode == DFmode)
539 cum->ints += l;
540 else if ((mode) == BLKmode)
542 if ((l = enough_regs_for_param (cum, type, mode)) != 0)
543 cum->ints += l;
548 /* Implements the macro FUNCTION_ARG_REGNO_P defined in crx.h. Return nonzero
549 * if N is a register used for passing parameters. */
552 crx_function_arg_regno_p (int n)
554 return (n <= MAX_REG_FOR_PASSING_ARGS && n >= MIN_REG_FOR_PASSING_ARGS);
557 /* ADDRESSING MODES */
558 /* ---------------- */
560 /* Implements the hook for TARGET_LEGITIMATE_ADDRESS_P defined in crx.h.
561 * The following addressing modes are supported on CRX:
563 * Relocations --> const | symbol_ref | label_ref
564 * Absolute address --> 32-bit absolute
565 * Post increment --> reg + 12-bit disp.
566 * Post modify --> reg + 12-bit disp.
567 * Register relative --> reg | 32-bit disp. + reg | 4 bit + reg
568 * Scaled index --> reg + reg | 22-bit disp. + reg + reg |
569 * 22-disp. + reg + reg + (2 | 4 | 8) */
571 static int crx_addr_reg_p (rtx addr_reg)
573 rtx reg;
575 if (REG_P (addr_reg))
577 reg = addr_reg;
579 else if ((GET_CODE (addr_reg) == SUBREG
580 && REG_P (SUBREG_REG (addr_reg))
581 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg)))
582 <= UNITS_PER_WORD))
584 reg = SUBREG_REG (addr_reg);
586 else
587 return FALSE;
589 if (GET_MODE (addr_reg) != Pmode)
591 return FALSE;
594 return TRUE;
597 enum crx_addrtype
598 crx_decompose_address (rtx addr, struct crx_address *out)
600 rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
601 rtx scale_rtx = NULL_RTX, side_effect = NULL_RTX;
602 int scale = -1;
604 enum crx_addrtype retval = CRX_INVALID;
606 switch (GET_CODE (addr))
608 case CONST_INT:
609 /* Absolute address (known at compile time) */
610 retval = CRX_ABSOLUTE;
611 disp = addr;
612 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), GET_MODE_BITSIZE (Pmode)))
613 return CRX_INVALID;
614 break;
616 case CONST:
617 case SYMBOL_REF:
618 case LABEL_REF:
619 /* Absolute address (known at link time) */
620 retval = CRX_ABSOLUTE;
621 disp = addr;
622 break;
624 case REG:
625 case SUBREG:
626 /* Register relative address */
627 retval = CRX_REG_REL;
628 base = addr;
629 break;
631 case PLUS:
632 switch (GET_CODE (XEXP (addr, 0)))
634 case REG:
635 case SUBREG:
636 if (REG_P (XEXP (addr, 1)))
638 /* Scaled index with scale = 1 and disp. = 0 */
639 retval = CRX_SCALED_INDX;
640 base = XEXP (addr, 1);
641 index = XEXP (addr, 0);
642 scale = 1;
644 else if (RTX_SIGNED_INT_FITS_N_BITS (XEXP (addr, 1), 28))
646 /* Register relative address and <= 28-bit disp. */
647 retval = CRX_REG_REL;
648 base = XEXP (addr, 0);
649 disp = XEXP (addr, 1);
651 else
652 return CRX_INVALID;
653 break;
655 case PLUS:
656 /* Scaled index and <= 22-bit disp. */
657 retval = CRX_SCALED_INDX;
658 base = XEXP (XEXP (addr, 0), 1);
659 disp = XEXP (addr, 1);
660 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 22))
661 return CRX_INVALID;
662 switch (GET_CODE (XEXP (XEXP (addr, 0), 0)))
664 case REG:
665 /* Scaled index with scale = 0 and <= 22-bit disp. */
666 index = XEXP (XEXP (addr, 0), 0);
667 scale = 1;
668 break;
670 case MULT:
671 /* Scaled index with scale >= 0 and <= 22-bit disp. */
672 index = XEXP (XEXP (XEXP (addr, 0), 0), 0);
673 scale_rtx = XEXP (XEXP (XEXP (addr, 0), 0), 1);
674 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
675 return CRX_INVALID;
676 break;
678 default:
679 return CRX_INVALID;
681 break;
683 case MULT:
684 /* Scaled index with scale >= 0 */
685 retval = CRX_SCALED_INDX;
686 base = XEXP (addr, 1);
687 index = XEXP (XEXP (addr, 0), 0);
688 scale_rtx = XEXP (XEXP (addr, 0), 1);
689 /* Scaled index with scale >= 0 and <= 22-bit disp. */
690 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
691 return CRX_INVALID;
692 break;
694 default:
695 return CRX_INVALID;
697 break;
699 case POST_INC:
700 case POST_DEC:
701 /* Simple post-increment */
702 retval = CRX_POST_INC;
703 base = XEXP (addr, 0);
704 side_effect = addr;
705 break;
707 case POST_MODIFY:
708 /* Generic post-increment with <= 12-bit disp. */
709 retval = CRX_POST_INC;
710 base = XEXP (addr, 0);
711 side_effect = XEXP (addr, 1);
712 if (base != XEXP (side_effect, 0))
713 return CRX_INVALID;
714 switch (GET_CODE (side_effect))
716 case PLUS:
717 case MINUS:
718 disp = XEXP (side_effect, 1);
719 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 12))
720 return CRX_INVALID;
721 break;
723 default:
724 /* CRX only supports PLUS and MINUS */
725 return CRX_INVALID;
727 break;
729 default:
730 return CRX_INVALID;
733 if (base && !crx_addr_reg_p (base)) return CRX_INVALID;
734 if (index && !crx_addr_reg_p (index)) return CRX_INVALID;
736 out->base = base;
737 out->index = index;
738 out->disp = disp;
739 out->scale = scale;
740 out->side_effect = side_effect;
742 return retval;
745 bool
746 crx_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
747 rtx addr, bool strict)
749 enum crx_addrtype addrtype;
750 struct crx_address address;
752 if (TARGET_DEBUG_ADDR)
754 fprintf (stderr,
755 "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
756 GET_MODE_NAME (mode), strict);
757 debug_rtx (addr);
760 addrtype = crx_decompose_address (addr, &address);
762 if (addrtype == CRX_POST_INC && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
763 return FALSE;
765 if (TARGET_DEBUG_ADDR)
767 const char *typestr;
768 switch (addrtype)
770 case CRX_INVALID:
771 typestr = "Invalid";
772 break;
773 case CRX_REG_REL:
774 typestr = "Register relative";
775 break;
776 case CRX_POST_INC:
777 typestr = "Post-increment";
778 break;
779 case CRX_SCALED_INDX:
780 typestr = "Scaled index";
781 break;
782 case CRX_ABSOLUTE:
783 typestr = "Absolute";
784 break;
785 default:
786 abort ();
788 fprintf (stderr, "CRX Address type: %s\n", typestr);
791 if (addrtype == CRX_INVALID)
792 return FALSE;
794 if (strict)
796 if (address.base && !REGNO_OK_FOR_BASE_P (REGNO (address.base)))
798 if (TARGET_DEBUG_ADDR)
799 fprintf (stderr, "Base register not strict\n");
800 return FALSE;
802 if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index)))
804 if (TARGET_DEBUG_ADDR)
805 fprintf (stderr, "Index register not strict\n");
806 return FALSE;
810 return TRUE;
813 /* ROUTINES TO COMPUTE COSTS */
814 /* ------------------------- */
816 /* Return cost of the memory address x. */
818 static int
819 crx_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
821 enum crx_addrtype addrtype;
822 struct crx_address address;
824 int cost = 2;
826 addrtype = crx_decompose_address (addr, &address);
828 gcc_assert (addrtype != CRX_INVALID);
830 /* An absolute address causes a 3-word instruction */
831 if (addrtype == CRX_ABSOLUTE)
832 cost+=2;
834 /* Post-modifying addresses are more powerful. */
835 if (addrtype == CRX_POST_INC)
836 cost-=2;
838 /* Attempt to minimize number of registers in the address. */
839 if (address.base)
840 cost++;
842 if (address.index && address.scale == 1)
843 cost+=5;
845 if (address.disp && !INT_CST4 (INTVAL (address.disp)))
846 cost+=2;
848 if (TARGET_DEBUG_ADDR)
850 fprintf (stderr, "\n======\nTARGET_ADDRESS_COST = %d\n", cost);
851 debug_rtx (addr);
854 return cost;
857 /* Return the cost of moving data of mode MODE between a register of class
858 * RCLASS and memory; IN is zero if the value is to be written to memory,
859 * nonzero if it is to be read in. This cost is relative to those in
860 * REGISTER_MOVE_COST. */
863 crx_memory_move_cost (enum machine_mode mode,
864 enum reg_class rclass ATTRIBUTE_UNUSED,
865 int in ATTRIBUTE_UNUSED)
867 /* One LD or ST takes twice the time of a simple reg-reg move */
868 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
870 /* printf ("GENERAL_REGS LD/ST = %d\n", 4 * HARD_REGNO_NREGS (0, mode));*/
871 return 4 * HARD_REGNO_NREGS (0, mode);
873 else if (reg_classes_intersect_p (rclass, HILO_REGS))
875 /* HILO to memory and vice versa */
876 /* printf ("HILO_REGS %s = %d\n", in ? "LD" : "ST",
877 (REGISTER_MOVE_COST (mode,
878 in ? GENERAL_REGS : HILO_REGS,
879 in ? HILO_REGS : GENERAL_REGS) + 4)
880 * HARD_REGNO_NREGS (0, mode)); */
881 return (REGISTER_MOVE_COST (mode,
882 in ? GENERAL_REGS : HILO_REGS,
883 in ? HILO_REGS : GENERAL_REGS) + 4)
884 * HARD_REGNO_NREGS (0, mode);
886 else /* default (like in i386) */
888 /* printf ("ANYREGS = 100\n"); */
889 return 100;
893 /* INSTRUCTION OUTPUT */
894 /* ------------------ */
896 /* Check if a const_double is ok for crx store-immediate instructions */
899 crx_const_double_ok (rtx op)
901 if (GET_MODE (op) == DFmode)
903 REAL_VALUE_TYPE r;
904 long l[2];
905 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
906 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
907 return (UNSIGNED_INT_FITS_N_BITS (l[0], 4) &&
908 UNSIGNED_INT_FITS_N_BITS (l[1], 4)) ? 1 : 0;
911 if (GET_MODE (op) == SFmode)
913 REAL_VALUE_TYPE r;
914 long l;
915 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
916 REAL_VALUE_TO_TARGET_SINGLE (r, l);
917 return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0;
920 return (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4) &&
921 UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4)) ? 1 : 0;
924 /* Implements the macro PRINT_OPERAND defined in crx.h. */
926 void
927 crx_print_operand (FILE * file, rtx x, int code)
929 switch (code)
931 case 'p' :
932 if (GET_CODE (x) == REG) {
933 if (GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
935 int regno = REGNO (x);
936 if (regno + 1 >= SP_REGNUM) abort ();
937 fprintf (file, "{%s, %s}", reg_names[regno], reg_names[regno + 1]);
938 return;
940 else
942 if (REGNO (x) >= SP_REGNUM) abort ();
943 fprintf (file, "%s", reg_names[REGNO (x)]);
944 return;
948 case 'd' :
950 const char *crx_cmp_str;
951 switch (GET_CODE (x))
952 { /* MD: compare (reg, reg or imm) but CRX: cmp (reg or imm, reg)
953 * -> swap all non symmetric ops */
954 case EQ : crx_cmp_str = "eq"; break;
955 case NE : crx_cmp_str = "ne"; break;
956 case GT : crx_cmp_str = "lt"; break;
957 case GTU : crx_cmp_str = "lo"; break;
958 case LT : crx_cmp_str = "gt"; break;
959 case LTU : crx_cmp_str = "hi"; break;
960 case GE : crx_cmp_str = "le"; break;
961 case GEU : crx_cmp_str = "ls"; break;
962 case LE : crx_cmp_str = "ge"; break;
963 case LEU : crx_cmp_str = "hs"; break;
964 default : abort ();
966 fprintf (file, "%s", crx_cmp_str);
967 return;
970 case 'H':
971 /* Print high part of a double precision value. */
972 switch (GET_CODE (x))
974 case CONST_DOUBLE:
975 if (GET_MODE (x) == SFmode) abort ();
976 if (GET_MODE (x) == DFmode)
978 /* High part of a DF const. */
979 REAL_VALUE_TYPE r;
980 long l[2];
982 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
983 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
985 fprintf (file, "$0x%lx", l[1]);
986 return;
989 /* -- Fallthrough to handle DI consts -- */
991 case CONST_INT:
993 rtx high, low;
994 split_double (x, &low, &high);
995 putc ('$', file);
996 output_addr_const (file, high);
997 return;
1000 case REG:
1001 if (REGNO (x) + 1 >= FIRST_PSEUDO_REGISTER) abort ();
1002 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
1003 return;
1005 case MEM:
1006 /* Adjust memory address to high part. */
1008 rtx adj_mem = x;
1009 adj_mem = adjust_address (adj_mem, GET_MODE (adj_mem), 4);
1011 output_memory_reference_mode = GET_MODE (adj_mem);
1012 output_address (XEXP (adj_mem, 0));
1013 return;
1016 default:
1017 abort ();
1020 case 'L':
1021 /* Print low part of a double precision value. */
1022 switch (GET_CODE (x))
1024 case CONST_DOUBLE:
1025 if (GET_MODE (x) == SFmode) abort ();
1026 if (GET_MODE (x) == DFmode)
1028 /* High part of a DF const. */
1029 REAL_VALUE_TYPE r;
1030 long l[2];
1032 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1033 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
1035 fprintf (file, "$0x%lx", l[0]);
1036 return;
1039 /* -- Fallthrough to handle DI consts -- */
1041 case CONST_INT:
1043 rtx high, low;
1044 split_double (x, &low, &high);
1045 putc ('$', file);
1046 output_addr_const (file, low);
1047 return;
1050 case REG:
1051 fprintf (file, "%s", reg_names[REGNO (x)]);
1052 return;
1054 case MEM:
1055 output_memory_reference_mode = GET_MODE (x);
1056 output_address (XEXP (x, 0));
1057 return;
1059 default:
1060 abort ();
1063 case 0 : /* default */
1064 switch (GET_CODE (x))
1066 case REG:
1067 fprintf (file, "%s", reg_names[REGNO (x)]);
1068 return;
1070 case MEM:
1071 output_memory_reference_mode = GET_MODE (x);
1072 output_address (XEXP (x, 0));
1073 return;
1075 case CONST_DOUBLE:
1077 REAL_VALUE_TYPE r;
1078 long l;
1080 /* Always use H and L for double precision - see above */
1081 gcc_assert (GET_MODE (x) == SFmode);
1083 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1084 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1086 fprintf (file, "$0x%lx", l);
1087 return;
1090 default:
1091 putc ('$', file);
1092 output_addr_const (file, x);
1093 return;
1096 default:
1097 output_operand_lossage ("invalid %%xn code");
1100 abort ();
1103 /* Implements the macro PRINT_OPERAND_ADDRESS defined in crx.h. */
1105 void
1106 crx_print_operand_address (FILE * file, rtx addr)
1108 enum crx_addrtype addrtype;
1109 struct crx_address address;
1111 int offset;
1113 addrtype = crx_decompose_address (addr, &address);
1115 if (address.disp)
1116 offset = INTVAL (address.disp);
1117 else
1118 offset = 0;
1120 switch (addrtype)
1122 case CRX_REG_REL:
1123 fprintf (file, "%d(%s)", offset, reg_names[REGNO (address.base)]);
1124 return;
1126 case CRX_POST_INC:
1127 switch (GET_CODE (address.side_effect))
1129 case PLUS:
1130 break;
1131 case MINUS:
1132 offset = -offset;
1133 break;
1134 case POST_INC:
1135 offset = GET_MODE_SIZE (output_memory_reference_mode);
1136 break;
1137 case POST_DEC:
1138 offset = -GET_MODE_SIZE (output_memory_reference_mode);
1139 break;
1140 default:
1141 abort ();
1143 fprintf (file, "%d(%s)+", offset, reg_names[REGNO (address.base)]);
1144 return;
1146 case CRX_SCALED_INDX:
1147 fprintf (file, "%d(%s, %s, %d)", offset, reg_names[REGNO (address.base)],
1148 reg_names[REGNO (address.index)], address.scale);
1149 return;
1151 case CRX_ABSOLUTE:
1152 output_addr_const (file, address.disp);
1153 return;
1155 default:
1156 abort ();
1161 /*****************************************************************************/
1162 /* MACHINE DESCRIPTION HELPER-FUNCTIONS */
1163 /*****************************************************************************/
1165 void crx_expand_movmem_single (rtx src, rtx srcbase, rtx dst, rtx dstbase,
1166 rtx tmp_reg, unsigned HOST_WIDE_INT *offset_p)
1168 rtx addr, mem;
1169 unsigned HOST_WIDE_INT offset = *offset_p;
1171 /* Load */
1172 addr = plus_constant (src, offset);
1173 mem = adjust_automodify_address (srcbase, SImode, addr, offset);
1174 emit_move_insn (tmp_reg, mem);
1176 /* Store */
1177 addr = plus_constant (dst, offset);
1178 mem = adjust_automodify_address (dstbase, SImode, addr, offset);
1179 emit_move_insn (mem, tmp_reg);
1181 *offset_p = offset + 4;
1185 crx_expand_movmem (rtx dstbase, rtx srcbase, rtx count_exp, rtx align_exp)
1187 unsigned HOST_WIDE_INT count = 0, offset, si_moves, i;
1188 HOST_WIDE_INT align = 0;
1190 rtx src, dst;
1191 rtx tmp_reg;
1193 if (GET_CODE (align_exp) == CONST_INT)
1194 { /* Only if aligned */
1195 align = INTVAL (align_exp);
1196 if (align & 3)
1197 return 0;
1200 if (GET_CODE (count_exp) == CONST_INT)
1201 { /* No more than 16 SImode moves */
1202 count = INTVAL (count_exp);
1203 if (count > 64)
1204 return 0;
1207 tmp_reg = gen_reg_rtx (SImode);
1209 /* Create psrs for the src and dest pointers */
1210 dst = copy_to_mode_reg (Pmode, XEXP (dstbase, 0));
1211 if (dst != XEXP (dstbase, 0))
1212 dstbase = replace_equiv_address_nv (dstbase, dst);
1213 src = copy_to_mode_reg (Pmode, XEXP (srcbase, 0));
1214 if (src != XEXP (srcbase, 0))
1215 srcbase = replace_equiv_address_nv (srcbase, src);
1217 offset = 0;
1219 /* Emit SImode moves */
1220 si_moves = count >> 2;
1221 for (i = 0; i < si_moves; i++)
1222 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1224 /* Special cases */
1225 if (count & 3)
1227 offset = count - 4;
1228 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1231 gcc_assert (offset == count);
1233 return 1;
1236 static void
1237 mpushpop_str (char *stringbuffer, const char *mnemonic, char *mask)
1239 if (strlen (mask) > 2 || crx_interrupt_function_p ()) /* needs 2-word instr. */
1240 sprintf (stringbuffer, "\n\t%s\tsp, {%s}", mnemonic, mask);
1241 else /* single word instruction */
1242 sprintf (stringbuffer, "\n\t%s\t%s", mnemonic, mask);
1245 /* Called from crx.md. The return value depends on the parameter push_or_pop:
1246 * When push_or_pop is zero -> string for push instructions of prologue.
1247 * When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1248 * Relies on the assumptions:
1249 * 1. RA is the last register to be saved.
1250 * 2. The maximal value of the counter is MAX_COUNT. */
1252 char *
1253 crx_prepare_push_pop_string (int push_or_pop)
1255 /* j is the number of registers being saved, takes care that there won't be
1256 * more than 8 in one push/pop instruction */
1258 /* For the register mask string */
1259 static char mask_str[50];
1261 /* i is the index of save_regs[], going from 0 until last_reg_to_save */
1262 int i = 0;
1264 int ra_in_bitmask = 0;
1266 char *return_str;
1268 /* For reversing on the push instructions if there are more than one. */
1269 char *temp_str;
1271 return_str = (char *) xmalloc (120);
1272 temp_str = (char *) xmalloc (120);
1274 /* Initialize */
1275 memset (return_str, 0, 3);
1277 while (i <= last_reg_to_save)
1279 /* Prepare mask for one instruction. */
1280 mask_str[0] = 0;
1282 if (i <= SP_REGNUM)
1283 { /* Add regs unit full or SP register reached */
1284 int j = 0;
1285 while (j < MAX_COUNT && i <= SP_REGNUM)
1287 if (save_regs[i])
1289 /* TODO to use ra_in_bitmask for detecting last pop is not
1290 * smart it prevents things like: popret r5 */
1291 if (i == RETURN_ADDRESS_REGNUM) ra_in_bitmask = 1;
1292 if (j > 0) strcat (mask_str, ", ");
1293 strcat (mask_str, reg_names[i]);
1294 ++j;
1296 ++i;
1299 else
1301 /* Handle hi/lo savings */
1302 while (i <= last_reg_to_save)
1304 if (save_regs[i])
1306 strcat (mask_str, "lo, hi");
1307 i = last_reg_to_save + 1;
1308 break;
1310 ++i;
1314 if (strlen (mask_str) == 0) continue;
1316 if (push_or_pop == 1)
1318 if (crx_interrupt_function_p ())
1319 mpushpop_str (temp_str, "popx", mask_str);
1320 else
1322 if (ra_in_bitmask)
1324 mpushpop_str (temp_str, "popret", mask_str);
1325 ra_in_bitmask = 0;
1327 else mpushpop_str (temp_str, "pop", mask_str);
1330 strcat (return_str, temp_str);
1332 else
1334 /* push - We need to reverse the order of the instructions if there
1335 * are more than one. (since the pop will not be reversed in the
1336 * epilogue */
1337 if (crx_interrupt_function_p ())
1338 mpushpop_str (temp_str, "pushx", mask_str);
1339 else
1340 mpushpop_str (temp_str, "push", mask_str);
1341 strcat (temp_str, return_str);
1342 strcpy (strcat (return_str, "\t"), temp_str);
1347 if (push_or_pop == 1)
1349 /* pop */
1350 if (crx_interrupt_function_p ())
1351 strcat (return_str, "\n\tretx\n");
1353 else if (!FUNC_IS_NORETURN_P (current_function_decl)
1354 && !save_regs[RETURN_ADDRESS_REGNUM])
1355 strcat (return_str, "\n\tjump\tra\n");
1358 /* Skip the newline and the tab in the start of return_str. */
1359 return_str += 2;
1360 return return_str;
1363 /* CompactRISC CRX Architecture stack layout:
1365 0 +---------------------
1370 +==================== Sp(x)=Ap(x+1)
1371 A | Args for functions
1372 | | called by X and Dynamically
1373 | | Dynamic allocations allocated and
1374 | | (alloca, variable deallocated
1375 Stack | length arrays).
1376 grows +-------------------- Fp(x)
1377 down| | Local variables of X
1378 ward| +--------------------
1379 | | Regs saved for X-1
1380 | +==================== Sp(x-1)=Ap(x)
1381 | Args for func X
1382 | pushed by X-1
1383 +-------------------- Fp(x-1)
1390 void
1391 crx_expand_prologue (void)
1393 crx_compute_frame ();
1394 crx_compute_save_regs ();
1396 /* If there is no need in push and adjustment to sp, return. */
1397 if (size_for_adjusting_sp + sum_regs == 0)
1398 return;
1400 if (last_reg_to_save != -1)
1401 /* If there are registers to push. */
1402 emit_insn (gen_push_for_prologue (GEN_INT (sum_regs)));
1404 if (size_for_adjusting_sp > 0)
1405 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1406 GEN_INT (-size_for_adjusting_sp)));
1408 if (frame_pointer_needed)
1409 /* Initialize the frame pointer with the value of the stack pointer
1410 * pointing now to the locals. */
1411 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1414 /* Generate insn that updates the stack for local variables and padding for
1415 * registers we save. - Generate the appropriate return insn. */
1417 void
1418 crx_expand_epilogue (void)
1420 rtx return_reg;
1422 /* Nonzero if we need to return and pop only RA. This will generate a
1423 * different insn. This differentiate is for the peepholes for call as last
1424 * statement in function. */
1425 int only_popret_RA = (save_regs[RETURN_ADDRESS_REGNUM]
1426 && (sum_regs == UNITS_PER_WORD));
1428 /* Return register. */
1429 return_reg = gen_rtx_REG (Pmode, RETURN_ADDRESS_REGNUM);
1431 if (frame_pointer_needed)
1432 /* Restore the stack pointer with the frame pointers value */
1433 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1435 if (size_for_adjusting_sp > 0)
1436 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1437 GEN_INT (size_for_adjusting_sp)));
1439 if (crx_interrupt_function_p ())
1440 emit_jump_insn (gen_interrupt_return ());
1441 else if (last_reg_to_save == -1)
1442 /* Nothing to pop */
1443 /* Don't output jump for interrupt routine, only retx. */
1444 emit_jump_insn (gen_indirect_jump_return ());
1445 else if (only_popret_RA)
1446 emit_jump_insn (gen_popret_RA_return ());
1447 else
1448 emit_jump_insn (gen_pop_and_popret_return (GEN_INT (sum_regs)));
1451 /* Implement TARGET_OPTION_OPTIMIZATION. */
1452 static void
1453 crx_option_optimization (int level, int size)
1455 /* Put each function in its own section so that PAGE-instruction
1456 relaxation can do its best. */
1457 if (level || size)
1458 flag_function_sections = 1;