* gcc.target/powerpc/altivec-volatile.c: Adjust expected warning.
[official-gcc.git] / gcc / config / crx / crx.c
blob3b5794242c563f7e45e8aaa53676891a5db0ace9
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
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 "toplev.h"
47 #include "basic-block.h"
48 #include "df.h"
49 #include "target.h"
50 #include "target-def.h"
52 /*****************************************************************************/
53 /* DEFINITIONS */
54 /*****************************************************************************/
56 /* Maximum number of register used for passing parameters. */
57 #define MAX_REG_FOR_PASSING_ARGS 6
59 /* Minimum number register used for passing parameters. */
60 #define MIN_REG_FOR_PASSING_ARGS 2
62 /* The maximum count of words supported in the assembly of the architecture in
63 * a push/pop instruction. */
64 #define MAX_COUNT 8
66 /* Predicate is true if the current function is a 'noreturn' function, i.e. it
67 * is qualified as volatile. */
68 #define FUNC_IS_NORETURN_P(decl) (TREE_THIS_VOLATILE (decl))
70 /* The following macros are used in crx_decompose_address () */
72 /* Returns the factor of a scaled index address or -1 if invalid. */
73 #define SCALE_FOR_INDEX_P(X) \
74 (GET_CODE (X) == CONST_INT ? \
75 (INTVAL (X) == 1 ? 1 : \
76 INTVAL (X) == 2 ? 2 : \
77 INTVAL (X) == 4 ? 4 : \
78 INTVAL (X) == 8 ? 8 : \
79 -1) : \
80 -1)
82 /* Nonzero if the rtx X is a signed const int of n bits */
83 #define RTX_SIGNED_INT_FITS_N_BITS(X,n) \
84 ((GET_CODE (X) == CONST_INT \
85 && SIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
87 /* Nonzero if the rtx X is an unsigned const int of n bits. */
88 #define RTX_UNSIGNED_INT_FITS_N_BITS(X, n) \
89 ((GET_CODE (X) == CONST_INT \
90 && UNSIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0)
92 /*****************************************************************************/
93 /* STATIC VARIABLES */
94 /*****************************************************************************/
96 /* Nonzero if the last param processed is passed in a register. */
97 static int last_parm_in_reg;
99 /* Will hold the number of the last register the prologue saves, -1 if no
100 * register is saved. */
101 static int last_reg_to_save;
103 /* Each object in the array is a register number. Mark 1 for registers that
104 * need to be saved. */
105 static int save_regs[FIRST_PSEUDO_REGISTER];
107 /* Number of bytes saved on the stack for non-scratch registers */
108 static int sum_regs = 0;
110 /* Number of bytes saved on the stack for local variables. */
111 static int local_vars_size;
113 /* The sum of 2 sizes: locals vars and padding byte for saving the registers.
114 * Used in expand_prologue () and expand_epilogue (). */
115 static int size_for_adjusting_sp;
117 /* In case of a POST_INC or POST_DEC memory reference, we must report the mode
118 * of the memory reference from PRINT_OPERAND to PRINT_OPERAND_ADDRESS. */
119 static enum machine_mode output_memory_reference_mode;
121 /*****************************************************************************/
122 /* TARGETM FUNCTION PROTOTYPES */
123 /*****************************************************************************/
125 static bool crx_fixed_condition_code_regs (unsigned int *, unsigned int *);
126 static rtx crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
127 int incoming ATTRIBUTE_UNUSED);
128 static bool crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED);
129 static int crx_address_cost (rtx, bool);
130 static bool crx_legitimate_address_p (enum machine_mode, rtx, bool);
131 static bool crx_can_eliminate (const int, const int);
133 /*****************************************************************************/
134 /* RTL VALIDITY */
135 /*****************************************************************************/
137 #undef TARGET_LEGITIMATE_ADDRESS_P
138 #define TARGET_LEGITIMATE_ADDRESS_P crx_legitimate_address_p
140 #undef TARGET_CAN_ELIMINATE
141 #define TARGET_CAN_ELIMINATE crx_can_eliminate
143 /*****************************************************************************/
144 /* STACK LAYOUT AND CALLING CONVENTIONS */
145 /*****************************************************************************/
147 #undef TARGET_FIXED_CONDITION_CODE_REGS
148 #define TARGET_FIXED_CONDITION_CODE_REGS crx_fixed_condition_code_regs
150 #undef TARGET_STRUCT_VALUE_RTX
151 #define TARGET_STRUCT_VALUE_RTX crx_struct_value_rtx
153 #undef TARGET_RETURN_IN_MEMORY
154 #define TARGET_RETURN_IN_MEMORY crx_return_in_memory
156 /*****************************************************************************/
157 /* RELATIVE COSTS OF OPERATIONS */
158 /*****************************************************************************/
160 #undef TARGET_ADDRESS_COST
161 #define TARGET_ADDRESS_COST crx_address_cost
163 /*****************************************************************************/
164 /* TARGET-SPECIFIC USES OF `__attribute__' */
165 /*****************************************************************************/
167 #undef TARGET_ATTRIBUTE_TABLE
168 #define TARGET_ATTRIBUTE_TABLE crx_attribute_table
170 static const struct attribute_spec crx_attribute_table[] = {
171 /* ISRs have special prologue and epilogue requirements. */
172 {"interrupt", 0, 0, false, true, true, NULL},
173 {NULL, 0, 0, false, false, false, NULL}
177 /* Initialize 'targetm' variable which contains pointers to functions and data
178 * relating to the target machine. */
180 struct gcc_target targetm = TARGET_INITIALIZER;
183 /*****************************************************************************/
184 /* TARGET HOOK IMPLEMENTATIONS */
185 /*****************************************************************************/
187 /* Return the fixed registers used for condition codes. */
189 static bool
190 crx_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
192 *p1 = CC_REGNUM;
193 *p2 = INVALID_REGNUM;
194 return true;
197 /* Implements hook TARGET_STRUCT_VALUE_RTX. */
199 static rtx
200 crx_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
201 int incoming ATTRIBUTE_UNUSED)
203 return gen_rtx_REG (Pmode, CRX_STRUCT_VALUE_REGNUM);
206 /* Implements hook TARGET_RETURN_IN_MEMORY. */
208 static bool
209 crx_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
211 if (TYPE_MODE (type) == BLKmode)
213 HOST_WIDE_INT size = int_size_in_bytes (type);
214 return (size == -1 || size > 8);
216 else
217 return false;
221 /*****************************************************************************/
222 /* MACRO IMPLEMENTATIONS */
223 /*****************************************************************************/
225 /* STACK LAYOUT AND CALLING CONVENTIONS ROUTINES */
226 /* --------------------------------------------- */
228 /* Return nonzero if the current function being compiled is an interrupt
229 * function as specified by the "interrupt" attribute. */
232 crx_interrupt_function_p (void)
234 tree attributes;
236 attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
237 return lookup_attribute ("interrupt", attributes) != NULL_TREE;
240 /* Compute values for the array save_regs and the variable sum_regs. The index
241 * of save_regs is numbers of register, each will get 1 if we need to save it
242 * in the current function, 0 if not. sum_regs is the total sum of the
243 * registers being saved. */
245 static void
246 crx_compute_save_regs (void)
248 unsigned int regno;
250 /* initialize here so in case the function is no-return it will be -1. */
251 last_reg_to_save = -1;
253 /* No need to save any registers if the function never returns. */
254 if (FUNC_IS_NORETURN_P (current_function_decl))
255 return;
257 /* Initialize the number of bytes to be saved. */
258 sum_regs = 0;
260 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
262 if (fixed_regs[regno])
264 save_regs[regno] = 0;
265 continue;
268 /* If this reg is used and not call-used (except RA), save it. */
269 if (crx_interrupt_function_p ())
271 if (!current_function_is_leaf && call_used_regs[regno])
272 /* this is a volatile reg in a non-leaf interrupt routine - save it
273 * for the sake of its sons. */
274 save_regs[regno] = 1;
276 else if (df_regs_ever_live_p (regno))
277 /* This reg is used - save it. */
278 save_regs[regno] = 1;
279 else
280 /* This reg is not used, and is not a volatile - don't save. */
281 save_regs[regno] = 0;
283 else
285 /* If this reg is used and not call-used (except RA), save it. */
286 if (df_regs_ever_live_p (regno)
287 && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM))
288 save_regs[regno] = 1;
289 else
290 save_regs[regno] = 0;
294 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
295 if (save_regs[regno] == 1)
297 last_reg_to_save = regno;
298 sum_regs += UNITS_PER_WORD;
302 /* Compute the size of the local area and the size to be adjusted by the
303 * prologue and epilogue. */
305 static void
306 crx_compute_frame (void)
308 /* For aligning the local variables. */
309 int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
310 int padding_locals;
312 /* Padding needed for each element of the frame. */
313 local_vars_size = get_frame_size ();
315 /* Align to the stack alignment. */
316 padding_locals = local_vars_size % stack_alignment;
317 if (padding_locals)
318 padding_locals = stack_alignment - padding_locals;
320 local_vars_size += padding_locals;
322 size_for_adjusting_sp = local_vars_size + (ACCUMULATE_OUTGOING_ARGS ?
323 crtl->outgoing_args_size : 0);
326 /* Worker function for TARGET_CAN_ELIMINATE. */
328 bool
329 crx_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
331 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
334 /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
337 crx_initial_elimination_offset (int from, int to)
339 /* Compute this since we need to use sum_regs. */
340 crx_compute_save_regs ();
342 /* Compute this since we need to use local_vars_size. */
343 crx_compute_frame ();
345 if ((from) == FRAME_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
346 return (ACCUMULATE_OUTGOING_ARGS ?
347 crtl->outgoing_args_size : 0);
348 else if ((from) == ARG_POINTER_REGNUM && (to) == FRAME_POINTER_REGNUM)
349 return (sum_regs + local_vars_size);
350 else if ((from) == ARG_POINTER_REGNUM && (to) == STACK_POINTER_REGNUM)
351 return (sum_regs + local_vars_size +
352 (ACCUMULATE_OUTGOING_ARGS ?
353 crtl->outgoing_args_size : 0));
354 else
355 abort ();
358 /* REGISTER USAGE */
359 /* -------------- */
361 /* Return the class number of the smallest class containing reg number REGNO.
362 * This could be a conditional expression or could index an array. */
364 enum reg_class
365 crx_regno_reg_class (int regno)
367 if (regno >= 0 && regno < SP_REGNUM)
368 return NOSP_REGS;
370 if (regno == SP_REGNUM)
371 return GENERAL_REGS;
373 if (regno == LO_REGNUM)
374 return LO_REGS;
375 if (regno == HI_REGNUM)
376 return HI_REGS;
378 return NO_REGS;
381 /* Transfer between HILO_REGS and memory via secondary reloading. */
383 enum reg_class
384 crx_secondary_reload_class (enum reg_class rclass,
385 enum machine_mode mode ATTRIBUTE_UNUSED,
386 rtx x ATTRIBUTE_UNUSED)
388 if (reg_classes_intersect_p (rclass, HILO_REGS)
389 && true_regnum (x) == -1)
390 return GENERAL_REGS;
392 return NO_REGS;
395 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */
398 crx_hard_regno_mode_ok (int regno, enum machine_mode mode)
400 /* CC can only hold CCmode values. */
401 if (regno == CC_REGNUM)
402 return GET_MODE_CLASS (mode) == MODE_CC;
403 if (GET_MODE_CLASS (mode) == MODE_CC)
404 return 0;
405 /* HILO registers can only hold SImode and DImode */
406 if (HILO_REGNO_P (regno))
407 return mode == SImode || mode == DImode;
408 return 1;
411 /* PASSING FUNCTION ARGUMENTS */
412 /* -------------------------- */
414 /* If enough param regs are available for passing the param of type TYPE return
415 * the number of registers needed else 0. */
417 static int
418 enough_regs_for_param (CUMULATIVE_ARGS * cum, tree type,
419 enum machine_mode mode)
421 int type_size;
422 int remaining_size;
424 if (mode != BLKmode)
425 type_size = GET_MODE_BITSIZE (mode);
426 else
427 type_size = int_size_in_bytes (type) * BITS_PER_UNIT;
429 remaining_size =
430 BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS -
431 (MIN_REG_FOR_PASSING_ARGS + cum->ints) + 1);
433 /* Any variable which is too big to pass in two registers, will pass on
434 * stack. */
435 if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD))
436 return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD;
438 return 0;
441 /* Implements the macro FUNCTION_ARG defined in crx.h. */
444 crx_function_arg (CUMULATIVE_ARGS * cum, enum machine_mode mode, tree type,
445 int named ATTRIBUTE_UNUSED)
447 last_parm_in_reg = 0;
449 /* Function_arg () is called with this type just after all the args have had
450 * their registers assigned. The rtx that function_arg returns from this type
451 * is supposed to pass to 'gen_call' but currently it is not implemented (see
452 * macro GEN_CALL). */
453 if (type == void_type_node)
454 return NULL_RTX;
456 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
457 return NULL_RTX;
459 if (mode == BLKmode)
461 /* Enable structures that need padding bytes at the end to pass to a
462 * function in registers. */
463 if (enough_regs_for_param (cum, type, mode) != 0)
465 last_parm_in_reg = 1;
466 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
470 if (MIN_REG_FOR_PASSING_ARGS + cum->ints > MAX_REG_FOR_PASSING_ARGS)
471 return NULL_RTX;
472 else
474 if (enough_regs_for_param (cum, type, mode) != 0)
476 last_parm_in_reg = 1;
477 return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints);
481 return NULL_RTX;
484 /* Implements the macro INIT_CUMULATIVE_ARGS defined in crx.h. */
486 void
487 crx_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype,
488 rtx libfunc ATTRIBUTE_UNUSED)
490 tree param, next_param;
492 cum->ints = 0;
494 /* Determine if this function has variable arguments. This is indicated by
495 * the last argument being 'void_type_mode' if there are no variable
496 * arguments. Change here for a different vararg. */
497 for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
498 param != (tree) 0; param = next_param)
500 next_param = TREE_CHAIN (param);
501 if (next_param == (tree) 0 && TREE_VALUE (param) != void_type_node)
503 cum->ints = -1;
504 return;
509 /* Implements the macro FUNCTION_ARG_ADVANCE defined in crx.h. */
511 void
512 crx_function_arg_advance (CUMULATIVE_ARGS * cum, enum machine_mode mode,
513 tree type, int named ATTRIBUTE_UNUSED)
515 /* l holds the number of registers required */
516 int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
518 /* If the parameter isn't passed on a register don't advance cum. */
519 if (!last_parm_in_reg)
520 return;
522 if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0))
523 return;
525 if (mode == SImode || mode == HImode || mode == QImode || mode == DImode)
527 if (l <= 1)
528 cum->ints += 1;
529 else
530 cum->ints += l;
532 else if (mode == SFmode || mode == DFmode)
533 cum->ints += l;
534 else if ((mode) == BLKmode)
536 if ((l = enough_regs_for_param (cum, type, mode)) != 0)
537 cum->ints += l;
542 /* Implements the macro FUNCTION_ARG_REGNO_P defined in crx.h. Return nonzero
543 * if N is a register used for passing parameters. */
546 crx_function_arg_regno_p (int n)
548 return (n <= MAX_REG_FOR_PASSING_ARGS && n >= MIN_REG_FOR_PASSING_ARGS);
551 /* ADDRESSING MODES */
552 /* ---------------- */
554 /* Implements the hook for TARGET_LEGITIMATE_ADDRESS_P defined in crx.h.
555 * The following addressing modes are supported on CRX:
557 * Relocations --> const | symbol_ref | label_ref
558 * Absolute address --> 32-bit absolute
559 * Post increment --> reg + 12-bit disp.
560 * Post modify --> reg + 12-bit disp.
561 * Register relative --> reg | 32-bit disp. + reg | 4 bit + reg
562 * Scaled index --> reg + reg | 22-bit disp. + reg + reg |
563 * 22-disp. + reg + reg + (2 | 4 | 8) */
565 static int crx_addr_reg_p (rtx addr_reg)
567 rtx reg;
569 if (REG_P (addr_reg))
571 reg = addr_reg;
573 else if ((GET_CODE (addr_reg) == SUBREG
574 && REG_P (SUBREG_REG (addr_reg))
575 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg)))
576 <= UNITS_PER_WORD))
578 reg = SUBREG_REG (addr_reg);
580 else
581 return FALSE;
583 if (GET_MODE (addr_reg) != Pmode)
585 return FALSE;
588 return TRUE;
591 enum crx_addrtype
592 crx_decompose_address (rtx addr, struct crx_address *out)
594 rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
595 rtx scale_rtx = NULL_RTX, side_effect = NULL_RTX;
596 int scale = -1;
598 enum crx_addrtype retval = CRX_INVALID;
600 switch (GET_CODE (addr))
602 case CONST_INT:
603 /* Absolute address (known at compile time) */
604 retval = CRX_ABSOLUTE;
605 disp = addr;
606 if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), GET_MODE_BITSIZE (Pmode)))
607 return CRX_INVALID;
608 break;
610 case CONST:
611 case SYMBOL_REF:
612 case LABEL_REF:
613 /* Absolute address (known at link time) */
614 retval = CRX_ABSOLUTE;
615 disp = addr;
616 break;
618 case REG:
619 case SUBREG:
620 /* Register relative address */
621 retval = CRX_REG_REL;
622 base = addr;
623 break;
625 case PLUS:
626 switch (GET_CODE (XEXP (addr, 0)))
628 case REG:
629 case SUBREG:
630 if (REG_P (XEXP (addr, 1)))
632 /* Scaled index with scale = 1 and disp. = 0 */
633 retval = CRX_SCALED_INDX;
634 base = XEXP (addr, 1);
635 index = XEXP (addr, 0);
636 scale = 1;
638 else if (RTX_SIGNED_INT_FITS_N_BITS (XEXP (addr, 1), 28))
640 /* Register relative address and <= 28-bit disp. */
641 retval = CRX_REG_REL;
642 base = XEXP (addr, 0);
643 disp = XEXP (addr, 1);
645 else
646 return CRX_INVALID;
647 break;
649 case PLUS:
650 /* Scaled index and <= 22-bit disp. */
651 retval = CRX_SCALED_INDX;
652 base = XEXP (XEXP (addr, 0), 1);
653 disp = XEXP (addr, 1);
654 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 22))
655 return CRX_INVALID;
656 switch (GET_CODE (XEXP (XEXP (addr, 0), 0)))
658 case REG:
659 /* Scaled index with scale = 0 and <= 22-bit disp. */
660 index = XEXP (XEXP (addr, 0), 0);
661 scale = 1;
662 break;
664 case MULT:
665 /* Scaled index with scale >= 0 and <= 22-bit disp. */
666 index = XEXP (XEXP (XEXP (addr, 0), 0), 0);
667 scale_rtx = XEXP (XEXP (XEXP (addr, 0), 0), 1);
668 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
669 return CRX_INVALID;
670 break;
672 default:
673 return CRX_INVALID;
675 break;
677 case MULT:
678 /* Scaled index with scale >= 0 */
679 retval = CRX_SCALED_INDX;
680 base = XEXP (addr, 1);
681 index = XEXP (XEXP (addr, 0), 0);
682 scale_rtx = XEXP (XEXP (addr, 0), 1);
683 /* Scaled index with scale >= 0 and <= 22-bit disp. */
684 if ((scale = SCALE_FOR_INDEX_P (scale_rtx)) == -1)
685 return CRX_INVALID;
686 break;
688 default:
689 return CRX_INVALID;
691 break;
693 case POST_INC:
694 case POST_DEC:
695 /* Simple post-increment */
696 retval = CRX_POST_INC;
697 base = XEXP (addr, 0);
698 side_effect = addr;
699 break;
701 case POST_MODIFY:
702 /* Generic post-increment with <= 12-bit disp. */
703 retval = CRX_POST_INC;
704 base = XEXP (addr, 0);
705 side_effect = XEXP (addr, 1);
706 if (base != XEXP (side_effect, 0))
707 return CRX_INVALID;
708 switch (GET_CODE (side_effect))
710 case PLUS:
711 case MINUS:
712 disp = XEXP (side_effect, 1);
713 if (!RTX_SIGNED_INT_FITS_N_BITS (disp, 12))
714 return CRX_INVALID;
715 break;
717 default:
718 /* CRX only supports PLUS and MINUS */
719 return CRX_INVALID;
721 break;
723 default:
724 return CRX_INVALID;
727 if (base && !crx_addr_reg_p (base)) return CRX_INVALID;
728 if (index && !crx_addr_reg_p (index)) return CRX_INVALID;
730 out->base = base;
731 out->index = index;
732 out->disp = disp;
733 out->scale = scale;
734 out->side_effect = side_effect;
736 return retval;
739 bool
740 crx_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
741 rtx addr, bool strict)
743 enum crx_addrtype addrtype;
744 struct crx_address address;
746 if (TARGET_DEBUG_ADDR)
748 fprintf (stderr,
749 "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
750 GET_MODE_NAME (mode), strict);
751 debug_rtx (addr);
754 addrtype = crx_decompose_address (addr, &address);
756 if (addrtype == CRX_POST_INC && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
757 return FALSE;
759 if (TARGET_DEBUG_ADDR)
761 const char *typestr;
762 switch (addrtype)
764 case CRX_INVALID:
765 typestr = "Invalid";
766 break;
767 case CRX_REG_REL:
768 typestr = "Register relative";
769 break;
770 case CRX_POST_INC:
771 typestr = "Post-increment";
772 break;
773 case CRX_SCALED_INDX:
774 typestr = "Scaled index";
775 break;
776 case CRX_ABSOLUTE:
777 typestr = "Absolute";
778 break;
779 default:
780 abort ();
782 fprintf (stderr, "CRX Address type: %s\n", typestr);
785 if (addrtype == CRX_INVALID)
786 return FALSE;
788 if (strict)
790 if (address.base && !REGNO_OK_FOR_BASE_P (REGNO (address.base)))
792 if (TARGET_DEBUG_ADDR)
793 fprintf (stderr, "Base register not strict\n");
794 return FALSE;
796 if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index)))
798 if (TARGET_DEBUG_ADDR)
799 fprintf (stderr, "Index register not strict\n");
800 return FALSE;
804 return TRUE;
807 /* ROUTINES TO COMPUTE COSTS */
808 /* ------------------------- */
810 /* Return cost of the memory address x. */
812 static int
813 crx_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
815 enum crx_addrtype addrtype;
816 struct crx_address address;
818 int cost = 2;
820 addrtype = crx_decompose_address (addr, &address);
822 gcc_assert (addrtype != CRX_INVALID);
824 /* An absolute address causes a 3-word instruction */
825 if (addrtype == CRX_ABSOLUTE)
826 cost+=2;
828 /* Post-modifying addresses are more powerful. */
829 if (addrtype == CRX_POST_INC)
830 cost-=2;
832 /* Attempt to minimize number of registers in the address. */
833 if (address.base)
834 cost++;
836 if (address.index && address.scale == 1)
837 cost+=5;
839 if (address.disp && !INT_CST4 (INTVAL (address.disp)))
840 cost+=2;
842 if (TARGET_DEBUG_ADDR)
844 fprintf (stderr, "\n======\nTARGET_ADDRESS_COST = %d\n", cost);
845 debug_rtx (addr);
848 return cost;
851 /* Return the cost of moving data of mode MODE between a register of class
852 * RCLASS and memory; IN is zero if the value is to be written to memory,
853 * nonzero if it is to be read in. This cost is relative to those in
854 * REGISTER_MOVE_COST. */
857 crx_memory_move_cost (enum machine_mode mode,
858 enum reg_class rclass ATTRIBUTE_UNUSED,
859 int in ATTRIBUTE_UNUSED)
861 /* One LD or ST takes twice the time of a simple reg-reg move */
862 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
864 /* printf ("GENERAL_REGS LD/ST = %d\n", 4 * HARD_REGNO_NREGS (0, mode));*/
865 return 4 * HARD_REGNO_NREGS (0, mode);
867 else if (reg_classes_intersect_p (rclass, HILO_REGS))
869 /* HILO to memory and vice versa */
870 /* printf ("HILO_REGS %s = %d\n", in ? "LD" : "ST",
871 (REGISTER_MOVE_COST (mode,
872 in ? GENERAL_REGS : HILO_REGS,
873 in ? HILO_REGS : GENERAL_REGS) + 4)
874 * HARD_REGNO_NREGS (0, mode)); */
875 return (REGISTER_MOVE_COST (mode,
876 in ? GENERAL_REGS : HILO_REGS,
877 in ? HILO_REGS : GENERAL_REGS) + 4)
878 * HARD_REGNO_NREGS (0, mode);
880 else /* default (like in i386) */
882 /* printf ("ANYREGS = 100\n"); */
883 return 100;
887 /* INSTRUCTION OUTPUT */
888 /* ------------------ */
890 /* Check if a const_double is ok for crx store-immediate instructions */
893 crx_const_double_ok (rtx op)
895 if (GET_MODE (op) == DFmode)
897 REAL_VALUE_TYPE r;
898 long l[2];
899 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
900 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
901 return (UNSIGNED_INT_FITS_N_BITS (l[0], 4) &&
902 UNSIGNED_INT_FITS_N_BITS (l[1], 4)) ? 1 : 0;
905 if (GET_MODE (op) == SFmode)
907 REAL_VALUE_TYPE r;
908 long l;
909 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
910 REAL_VALUE_TO_TARGET_SINGLE (r, l);
911 return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0;
914 return (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4) &&
915 UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4)) ? 1 : 0;
918 /* Implements the macro PRINT_OPERAND defined in crx.h. */
920 void
921 crx_print_operand (FILE * file, rtx x, int code)
923 switch (code)
925 case 'p' :
926 if (GET_CODE (x) == REG) {
927 if (GET_MODE (x) == DImode || GET_MODE (x) == DFmode)
929 int regno = REGNO (x);
930 if (regno + 1 >= SP_REGNUM) abort ();
931 fprintf (file, "{%s, %s}", reg_names[regno], reg_names[regno + 1]);
932 return;
934 else
936 if (REGNO (x) >= SP_REGNUM) abort ();
937 fprintf (file, "%s", reg_names[REGNO (x)]);
938 return;
942 case 'd' :
944 const char *crx_cmp_str;
945 switch (GET_CODE (x))
946 { /* MD: compare (reg, reg or imm) but CRX: cmp (reg or imm, reg)
947 * -> swap all non symmetric ops */
948 case EQ : crx_cmp_str = "eq"; break;
949 case NE : crx_cmp_str = "ne"; break;
950 case GT : crx_cmp_str = "lt"; break;
951 case GTU : crx_cmp_str = "lo"; break;
952 case LT : crx_cmp_str = "gt"; break;
953 case LTU : crx_cmp_str = "hi"; break;
954 case GE : crx_cmp_str = "le"; break;
955 case GEU : crx_cmp_str = "ls"; break;
956 case LE : crx_cmp_str = "ge"; break;
957 case LEU : crx_cmp_str = "hs"; break;
958 default : abort ();
960 fprintf (file, "%s", crx_cmp_str);
961 return;
964 case 'H':
965 /* Print high part of a double precision value. */
966 switch (GET_CODE (x))
968 case CONST_DOUBLE:
969 if (GET_MODE (x) == SFmode) abort ();
970 if (GET_MODE (x) == DFmode)
972 /* High part of a DF const. */
973 REAL_VALUE_TYPE r;
974 long l[2];
976 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
977 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
979 fprintf (file, "$0x%lx", l[1]);
980 return;
983 /* -- Fallthrough to handle DI consts -- */
985 case CONST_INT:
987 rtx high, low;
988 split_double (x, &low, &high);
989 putc ('$', file);
990 output_addr_const (file, high);
991 return;
994 case REG:
995 if (REGNO (x) + 1 >= FIRST_PSEUDO_REGISTER) abort ();
996 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
997 return;
999 case MEM:
1000 /* Adjust memory address to high part. */
1002 rtx adj_mem = x;
1003 adj_mem = adjust_address (adj_mem, GET_MODE (adj_mem), 4);
1005 output_memory_reference_mode = GET_MODE (adj_mem);
1006 output_address (XEXP (adj_mem, 0));
1007 return;
1010 default:
1011 abort ();
1014 case 'L':
1015 /* Print low part of a double precision value. */
1016 switch (GET_CODE (x))
1018 case CONST_DOUBLE:
1019 if (GET_MODE (x) == SFmode) abort ();
1020 if (GET_MODE (x) == DFmode)
1022 /* High part of a DF const. */
1023 REAL_VALUE_TYPE r;
1024 long l[2];
1026 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1027 REAL_VALUE_TO_TARGET_DOUBLE (r, l);
1029 fprintf (file, "$0x%lx", l[0]);
1030 return;
1033 /* -- Fallthrough to handle DI consts -- */
1035 case CONST_INT:
1037 rtx high, low;
1038 split_double (x, &low, &high);
1039 putc ('$', file);
1040 output_addr_const (file, low);
1041 return;
1044 case REG:
1045 fprintf (file, "%s", reg_names[REGNO (x)]);
1046 return;
1048 case MEM:
1049 output_memory_reference_mode = GET_MODE (x);
1050 output_address (XEXP (x, 0));
1051 return;
1053 default:
1054 abort ();
1057 case 0 : /* default */
1058 switch (GET_CODE (x))
1060 case REG:
1061 fprintf (file, "%s", reg_names[REGNO (x)]);
1062 return;
1064 case MEM:
1065 output_memory_reference_mode = GET_MODE (x);
1066 output_address (XEXP (x, 0));
1067 return;
1069 case CONST_DOUBLE:
1071 REAL_VALUE_TYPE r;
1072 long l;
1074 /* Always use H and L for double precision - see above */
1075 gcc_assert (GET_MODE (x) == SFmode);
1077 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
1078 REAL_VALUE_TO_TARGET_SINGLE (r, l);
1080 fprintf (file, "$0x%lx", l);
1081 return;
1084 default:
1085 putc ('$', file);
1086 output_addr_const (file, x);
1087 return;
1090 default:
1091 output_operand_lossage ("invalid %%xn code");
1094 abort ();
1097 /* Implements the macro PRINT_OPERAND_ADDRESS defined in crx.h. */
1099 void
1100 crx_print_operand_address (FILE * file, rtx addr)
1102 enum crx_addrtype addrtype;
1103 struct crx_address address;
1105 int offset;
1107 addrtype = crx_decompose_address (addr, &address);
1109 if (address.disp)
1110 offset = INTVAL (address.disp);
1111 else
1112 offset = 0;
1114 switch (addrtype)
1116 case CRX_REG_REL:
1117 fprintf (file, "%d(%s)", offset, reg_names[REGNO (address.base)]);
1118 return;
1120 case CRX_POST_INC:
1121 switch (GET_CODE (address.side_effect))
1123 case PLUS:
1124 break;
1125 case MINUS:
1126 offset = -offset;
1127 break;
1128 case POST_INC:
1129 offset = GET_MODE_SIZE (output_memory_reference_mode);
1130 break;
1131 case POST_DEC:
1132 offset = -GET_MODE_SIZE (output_memory_reference_mode);
1133 break;
1134 default:
1135 abort ();
1137 fprintf (file, "%d(%s)+", offset, reg_names[REGNO (address.base)]);
1138 return;
1140 case CRX_SCALED_INDX:
1141 fprintf (file, "%d(%s, %s, %d)", offset, reg_names[REGNO (address.base)],
1142 reg_names[REGNO (address.index)], address.scale);
1143 return;
1145 case CRX_ABSOLUTE:
1146 output_addr_const (file, address.disp);
1147 return;
1149 default:
1150 abort ();
1155 /*****************************************************************************/
1156 /* MACHINE DESCRIPTION HELPER-FUNCTIONS */
1157 /*****************************************************************************/
1159 void crx_expand_movmem_single (rtx src, rtx srcbase, rtx dst, rtx dstbase,
1160 rtx tmp_reg, unsigned HOST_WIDE_INT *offset_p)
1162 rtx addr, mem;
1163 unsigned HOST_WIDE_INT offset = *offset_p;
1165 /* Load */
1166 addr = plus_constant (src, offset);
1167 mem = adjust_automodify_address (srcbase, SImode, addr, offset);
1168 emit_move_insn (tmp_reg, mem);
1170 /* Store */
1171 addr = plus_constant (dst, offset);
1172 mem = adjust_automodify_address (dstbase, SImode, addr, offset);
1173 emit_move_insn (mem, tmp_reg);
1175 *offset_p = offset + 4;
1179 crx_expand_movmem (rtx dstbase, rtx srcbase, rtx count_exp, rtx align_exp)
1181 unsigned HOST_WIDE_INT count = 0, offset, si_moves, i;
1182 HOST_WIDE_INT align = 0;
1184 rtx src, dst;
1185 rtx tmp_reg;
1187 if (GET_CODE (align_exp) == CONST_INT)
1188 { /* Only if aligned */
1189 align = INTVAL (align_exp);
1190 if (align & 3)
1191 return 0;
1194 if (GET_CODE (count_exp) == CONST_INT)
1195 { /* No more than 16 SImode moves */
1196 count = INTVAL (count_exp);
1197 if (count > 64)
1198 return 0;
1201 tmp_reg = gen_reg_rtx (SImode);
1203 /* Create psrs for the src and dest pointers */
1204 dst = copy_to_mode_reg (Pmode, XEXP (dstbase, 0));
1205 if (dst != XEXP (dstbase, 0))
1206 dstbase = replace_equiv_address_nv (dstbase, dst);
1207 src = copy_to_mode_reg (Pmode, XEXP (srcbase, 0));
1208 if (src != XEXP (srcbase, 0))
1209 srcbase = replace_equiv_address_nv (srcbase, src);
1211 offset = 0;
1213 /* Emit SImode moves */
1214 si_moves = count >> 2;
1215 for (i = 0; i < si_moves; i++)
1216 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1218 /* Special cases */
1219 if (count & 3)
1221 offset = count - 4;
1222 crx_expand_movmem_single (src, srcbase, dst, dstbase, tmp_reg, &offset);
1225 gcc_assert (offset == count);
1227 return 1;
1230 static void
1231 mpushpop_str (char *stringbuffer, const char *mnemonic, char *mask)
1233 if (strlen (mask) > 2 || crx_interrupt_function_p ()) /* needs 2-word instr. */
1234 sprintf (stringbuffer, "\n\t%s\tsp, {%s}", mnemonic, mask);
1235 else /* single word instruction */
1236 sprintf (stringbuffer, "\n\t%s\t%s", mnemonic, mask);
1239 /* Called from crx.md. The return value depends on the parameter push_or_pop:
1240 * When push_or_pop is zero -> string for push instructions of prologue.
1241 * When push_or_pop is nonzero -> string for pop/popret/retx in epilogue.
1242 * Relies on the assumptions:
1243 * 1. RA is the last register to be saved.
1244 * 2. The maximal value of the counter is MAX_COUNT. */
1246 char *
1247 crx_prepare_push_pop_string (int push_or_pop)
1249 /* j is the number of registers being saved, takes care that there won't be
1250 * more than 8 in one push/pop instruction */
1252 /* For the register mask string */
1253 static char mask_str[50];
1255 /* i is the index of save_regs[], going from 0 until last_reg_to_save */
1256 int i = 0;
1258 int ra_in_bitmask = 0;
1260 char *return_str;
1262 /* For reversing on the push instructions if there are more than one. */
1263 char *temp_str;
1265 return_str = (char *) xmalloc (120);
1266 temp_str = (char *) xmalloc (120);
1268 /* Initialize */
1269 memset (return_str, 0, 3);
1271 while (i <= last_reg_to_save)
1273 /* Prepare mask for one instruction. */
1274 mask_str[0] = 0;
1276 if (i <= SP_REGNUM)
1277 { /* Add regs unit full or SP register reached */
1278 int j = 0;
1279 while (j < MAX_COUNT && i <= SP_REGNUM)
1281 if (save_regs[i])
1283 /* TODO to use ra_in_bitmask for detecting last pop is not
1284 * smart it prevents things like: popret r5 */
1285 if (i == RETURN_ADDRESS_REGNUM) ra_in_bitmask = 1;
1286 if (j > 0) strcat (mask_str, ", ");
1287 strcat (mask_str, reg_names[i]);
1288 ++j;
1290 ++i;
1293 else
1295 /* Handle hi/lo savings */
1296 while (i <= last_reg_to_save)
1298 if (save_regs[i])
1300 strcat (mask_str, "lo, hi");
1301 i = last_reg_to_save + 1;
1302 break;
1304 ++i;
1308 if (strlen (mask_str) == 0) continue;
1310 if (push_or_pop == 1)
1312 if (crx_interrupt_function_p ())
1313 mpushpop_str (temp_str, "popx", mask_str);
1314 else
1316 if (ra_in_bitmask)
1318 mpushpop_str (temp_str, "popret", mask_str);
1319 ra_in_bitmask = 0;
1321 else mpushpop_str (temp_str, "pop", mask_str);
1324 strcat (return_str, temp_str);
1326 else
1328 /* push - We need to reverse the order of the instructions if there
1329 * are more than one. (since the pop will not be reversed in the
1330 * epilogue */
1331 if (crx_interrupt_function_p ())
1332 mpushpop_str (temp_str, "pushx", mask_str);
1333 else
1334 mpushpop_str (temp_str, "push", mask_str);
1335 strcat (temp_str, return_str);
1336 strcpy (strcat (return_str, "\t"), temp_str);
1341 if (push_or_pop == 1)
1343 /* pop */
1344 if (crx_interrupt_function_p ())
1345 strcat (return_str, "\n\tretx\n");
1347 else if (!FUNC_IS_NORETURN_P (current_function_decl)
1348 && !save_regs[RETURN_ADDRESS_REGNUM])
1349 strcat (return_str, "\n\tjump\tra\n");
1352 /* Skip the newline and the tab in the start of return_str. */
1353 return_str += 2;
1354 return return_str;
1357 /* CompactRISC CRX Architecture stack layout:
1359 0 +---------------------
1364 +==================== Sp(x)=Ap(x+1)
1365 A | Args for functions
1366 | | called by X and Dynamically
1367 | | Dynamic allocations allocated and
1368 | | (alloca, variable deallocated
1369 Stack | length arrays).
1370 grows +-------------------- Fp(x)
1371 down| | Local variables of X
1372 ward| +--------------------
1373 | | Regs saved for X-1
1374 | +==================== Sp(x-1)=Ap(x)
1375 | Args for func X
1376 | pushed by X-1
1377 +-------------------- Fp(x-1)
1384 void
1385 crx_expand_prologue (void)
1387 crx_compute_frame ();
1388 crx_compute_save_regs ();
1390 /* If there is no need in push and adjustment to sp, return. */
1391 if (size_for_adjusting_sp + sum_regs == 0)
1392 return;
1394 if (last_reg_to_save != -1)
1395 /* If there are registers to push. */
1396 emit_insn (gen_push_for_prologue (GEN_INT (sum_regs)));
1398 if (size_for_adjusting_sp > 0)
1399 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1400 GEN_INT (-size_for_adjusting_sp)));
1402 if (frame_pointer_needed)
1403 /* Initialize the frame pointer with the value of the stack pointer
1404 * pointing now to the locals. */
1405 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
1408 /* Generate insn that updates the stack for local variables and padding for
1409 * registers we save. - Generate the appropriate return insn. */
1411 void
1412 crx_expand_epilogue (void)
1414 rtx return_reg;
1416 /* Nonzero if we need to return and pop only RA. This will generate a
1417 * different insn. This differentiate is for the peepholes for call as last
1418 * statement in function. */
1419 int only_popret_RA = (save_regs[RETURN_ADDRESS_REGNUM]
1420 && (sum_regs == UNITS_PER_WORD));
1422 /* Return register. */
1423 return_reg = gen_rtx_REG (Pmode, RETURN_ADDRESS_REGNUM);
1425 if (frame_pointer_needed)
1426 /* Restore the stack pointer with the frame pointers value */
1427 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1429 if (size_for_adjusting_sp > 0)
1430 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
1431 GEN_INT (size_for_adjusting_sp)));
1433 if (crx_interrupt_function_p ())
1434 emit_jump_insn (gen_interrupt_return ());
1435 else if (last_reg_to_save == -1)
1436 /* Nothing to pop */
1437 /* Don't output jump for interrupt routine, only retx. */
1438 emit_jump_insn (gen_indirect_jump_return ());
1439 else if (only_popret_RA)
1440 emit_jump_insn (gen_popret_RA_return ());
1441 else
1442 emit_jump_insn (gen_pop_and_popret_return (GEN_INT (sum_regs)));