OSDN Git Service

PR target/24342
[pf3gnuchains/gcc-fork.git] / gcc / config / cris / cris.c
1 /* Definitions for GCC.  Part of the machine description for CRIS.
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3    Free Software Foundation, Inc.
4    Contributed by Axis Communications.  Written by Hans-Peter Nilsson.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "expr.h"
37 #include "except.h"
38 #include "function.h"
39 #include "toplev.h"
40 #include "recog.h"
41 #include "reload.h"
42 #include "tm_p.h"
43 #include "debug.h"
44 #include "output.h"
45 #include "target.h"
46 #include "target-def.h"
47 #include "ggc.h"
48 #include "optabs.h"
49
50 /* Usable when we have an amount to add or subtract, and want the
51    optimal size of the insn.  */
52 #define ADDITIVE_SIZE_MODIFIER(size) \
53  ((size) <= 63 ? "q" : (size) <= 255 ? "u.b" : (size) <= 65535 ? "u.w" : ".d")
54
55 #define ASSERT_PLT_UNSPEC(x)                                            \
56   CRIS_ASSERT (XINT (x, 1) == CRIS_UNSPEC_PLT                           \
57                && ((GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF)         \
58                    || GET_CODE (XVECEXP (x, 0, 0)) == LABEL_REF))
59
60 #define LOSE_AND_RETURN(msgid, x)                       \
61   do                                            \
62     {                                           \
63       cris_operand_lossage (msgid, x);          \
64       return;                                   \
65     } while (0)
66
67 enum cris_retinsn_type
68  { CRIS_RETINSN_UNKNOWN = 0, CRIS_RETINSN_RET, CRIS_RETINSN_JUMP };
69
70 /* Per-function machine data.  */
71 struct machine_function GTY(())
72  {
73    int needs_return_address_on_stack;
74
75    /* This is the number of registers we save in the prologue due to
76       stdarg.  */
77    int stdarg_regs;
78
79    enum cris_retinsn_type return_type;
80  };
81
82 /* This little fix suppresses the 'u' or 's' when '%e' in assembly
83    pattern.  */
84 static char cris_output_insn_is_bound = 0;
85
86 /* In code for output macros, this is how we know whether e.g. constant
87    goes in code or in a static initializer.  */
88 static int in_code = 0;
89
90 /* Fix for reg_overlap_mentioned_p.  */
91 static int cris_reg_overlap_mentioned_p (rtx, rtx);
92
93 static void cris_print_base (rtx, FILE *);
94
95 static void cris_print_index (rtx, FILE *);
96
97 static void cris_output_addr_const (FILE *, rtx);
98
99 static struct machine_function * cris_init_machine_status (void);
100
101 static rtx cris_struct_value_rtx (tree, int);
102
103 static void cris_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
104                                          tree type, int *, int);
105
106 static int cris_initial_frame_pointer_offset (void);
107
108 static int saved_regs_mentioned (rtx);
109
110 static void cris_operand_lossage (const char *, rtx);
111
112 static int cris_reg_saved_in_regsave_area  (unsigned int, bool);
113
114 static void cris_asm_output_mi_thunk
115   (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
116
117 static void cris_file_start (void);
118 static void cris_init_libfuncs (void);
119
120 static bool cris_rtx_costs (rtx, int, int, int *);
121 static int cris_address_cost (rtx);
122 static bool cris_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
123                                     tree, bool);
124 static int cris_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
125                                    tree, bool);
126 static tree cris_md_asm_clobbers (tree, tree, tree);
127
128 static bool cris_handle_option (size_t, const char *, int);
129
130 /* This is the parsed result of the "-max-stack-stackframe=" option.  If
131    it (still) is zero, then there was no such option given.  */
132 int cris_max_stackframe = 0;
133
134 /* This is the parsed result of the "-march=" option, if given.  */
135 int cris_cpu_version = CRIS_DEFAULT_CPU_VERSION;
136
137 #undef TARGET_ASM_ALIGNED_HI_OP
138 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
139 #undef TARGET_ASM_ALIGNED_SI_OP
140 #define TARGET_ASM_ALIGNED_SI_OP "\t.dword\t"
141 #undef TARGET_ASM_ALIGNED_DI_OP
142 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
143
144 /* We need to define these, since the 2byte, 4byte, 8byte op:s are only
145    available in ELF.  These "normal" pseudos do not have any alignment
146    constraints or side-effects.  */
147 #undef TARGET_ASM_UNALIGNED_HI_OP
148 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
149
150 #undef TARGET_ASM_UNALIGNED_SI_OP
151 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
152
153 #undef TARGET_ASM_UNALIGNED_DI_OP
154 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
155
156 #undef TARGET_ASM_OUTPUT_MI_THUNK
157 #define TARGET_ASM_OUTPUT_MI_THUNK cris_asm_output_mi_thunk
158 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
159 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
160
161 #undef TARGET_ASM_FILE_START
162 #define TARGET_ASM_FILE_START cris_file_start
163
164 #undef TARGET_INIT_LIBFUNCS
165 #define TARGET_INIT_LIBFUNCS cris_init_libfuncs
166
167 #undef TARGET_RTX_COSTS
168 #define TARGET_RTX_COSTS cris_rtx_costs
169 #undef TARGET_ADDRESS_COST
170 #define TARGET_ADDRESS_COST cris_address_cost
171
172 #undef TARGET_PROMOTE_FUNCTION_ARGS
173 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
174 #undef TARGET_STRUCT_VALUE_RTX
175 #define TARGET_STRUCT_VALUE_RTX cris_struct_value_rtx
176 #undef TARGET_SETUP_INCOMING_VARARGS
177 #define TARGET_SETUP_INCOMING_VARARGS cris_setup_incoming_varargs
178 #undef TARGET_PASS_BY_REFERENCE
179 #define TARGET_PASS_BY_REFERENCE cris_pass_by_reference
180 #undef TARGET_ARG_PARTIAL_BYTES
181 #define TARGET_ARG_PARTIAL_BYTES cris_arg_partial_bytes
182 #undef TARGET_MD_ASM_CLOBBERS
183 #define TARGET_MD_ASM_CLOBBERS cris_md_asm_clobbers
184 #undef TARGET_DEFAULT_TARGET_FLAGS
185 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | CRIS_SUBTARGET_DEFAULT)
186 #undef TARGET_HANDLE_OPTION
187 #define TARGET_HANDLE_OPTION cris_handle_option
188
189 struct gcc_target targetm = TARGET_INITIALIZER;
190
191 /* Helper for cris_load_multiple_op and cris_ret_movem_op.  */
192
193 bool
194 cris_movem_load_rest_p (rtx op, int offs)
195 {
196   unsigned int reg_count = XVECLEN (op, 0) - offs;
197   rtx src_addr;
198   int i;
199   rtx elt;
200   int setno;
201   int regno_dir = 1;
202   unsigned int regno = 0;
203
204   /* Perform a quick check so we don't blow up below.  FIXME: Adjust for
205      other than (MEM reg).  */
206   if (reg_count <= 1
207       || GET_CODE (XVECEXP (op, 0, offs)) != SET
208       || GET_CODE (SET_DEST (XVECEXP (op, 0, offs))) != REG
209       || GET_CODE (SET_SRC (XVECEXP (op, 0, offs))) != MEM)
210     return false;
211
212   /* Check a possible post-inc indicator.  */
213   if (GET_CODE (SET_SRC (XVECEXP (op, 0, offs + 1))) == PLUS)
214     {
215       rtx reg = XEXP (SET_SRC (XVECEXP (op, 0, offs + 1)), 0);
216       rtx inc = XEXP (SET_SRC (XVECEXP (op, 0, offs + 1)), 1);
217
218       reg_count--;
219
220       if (reg_count == 1
221           || !REG_P (reg)
222           || !REG_P (SET_DEST (XVECEXP (op, 0, offs + 1)))
223           || REGNO (reg) != REGNO (SET_DEST (XVECEXP (op, 0, offs + 1)))
224           || GET_CODE (inc) != CONST_INT
225           || INTVAL (inc) != (HOST_WIDE_INT) reg_count * 4)
226         return false;
227       i = offs + 2;
228     }
229   else
230     i = offs + 1;
231
232   /* FIXME: These two only for pre-v32.  */
233   regno_dir = -1;
234   regno = reg_count - 1;
235
236   elt = XVECEXP (op, 0, offs);
237   src_addr = XEXP (SET_SRC (elt), 0);
238
239   if (GET_CODE (elt) != SET
240       || GET_CODE (SET_DEST (elt)) != REG
241       || GET_MODE (SET_DEST (elt)) != SImode
242       || REGNO (SET_DEST (elt)) != regno
243       || GET_CODE (SET_SRC (elt)) != MEM
244       || GET_MODE (SET_SRC (elt)) != SImode
245       || !memory_address_p (SImode, src_addr))
246     return false;
247
248   for (setno = 1; i < XVECLEN (op, 0); setno++, i++)
249     {
250       rtx elt = XVECEXP (op, 0, i);
251       regno += regno_dir;
252
253       if (GET_CODE (elt) != SET
254           || GET_CODE (SET_DEST (elt)) != REG
255           || GET_MODE (SET_DEST (elt)) != SImode
256           || REGNO (SET_DEST (elt)) != regno
257           || GET_CODE (SET_SRC (elt)) != MEM
258           || GET_MODE (SET_SRC (elt)) != SImode
259           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
260           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
261           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
262           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != setno * 4)
263         return false;
264     }
265
266   return true;
267 }
268
269 /* Worker function for predicate for the parallel contents in a movem
270    to-memory.  */
271
272 bool
273 cris_store_multiple_op_p (rtx op)
274 {
275   int reg_count = XVECLEN (op, 0);
276   rtx dest;
277   rtx dest_addr;
278   rtx dest_base;
279   int i;
280   rtx elt;
281   int setno;
282   int regno_dir = 1;
283   int regno = 0;
284   int offset = 0;
285
286   /* Perform a quick check so we don't blow up below.  FIXME: Adjust for
287      other than (MEM reg) and (MEM (PLUS reg const)).  */
288   if (reg_count <= 1)
289     return false;
290
291   elt = XVECEXP (op, 0, 0);
292
293   if (GET_CODE (elt) != SET)
294     return  false;
295
296   dest = SET_DEST (elt);
297
298   if (GET_CODE (SET_SRC (elt)) != REG
299       || GET_CODE (dest) != MEM)
300     return false;
301
302   dest_addr = XEXP (dest, 0);
303
304   /* Check a possible post-inc indicator.  */
305   if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) == PLUS)
306     {
307       rtx reg = XEXP (SET_SRC (XVECEXP (op, 0, 1)), 0);
308       rtx inc = XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1);
309
310       reg_count--;
311
312       if (reg_count == 1
313           || !REG_P (reg)
314           || !REG_P (SET_DEST (XVECEXP (op, 0, 1)))
315           || REGNO (reg) != REGNO (SET_DEST (XVECEXP (op, 0, 1)))
316           || GET_CODE (inc) != CONST_INT
317           /* Support increment by number of registers, and by the offset
318              of the destination, if it has the form (MEM (PLUS reg
319              offset)).  */
320           || !((REG_P (dest_addr)
321                 && REGNO (dest_addr) == REGNO (reg)
322                 && INTVAL (inc) == (HOST_WIDE_INT) reg_count * 4)
323                || (GET_CODE (dest_addr) == PLUS
324                    && REG_P (XEXP (dest_addr, 0))
325                    && REGNO (XEXP (dest_addr, 0)) == REGNO (reg)
326                    && GET_CODE (XEXP (dest_addr, 1)) == CONST_INT
327                    && INTVAL (XEXP (dest_addr, 1)) == INTVAL (inc))))
328         return false;
329
330       i = 2;
331     }
332   else
333     i = 1;
334
335   /* FIXME: These two only for pre-v32.  */
336   regno_dir = -1;
337   regno = reg_count - 1;
338
339   if (GET_CODE (elt) != SET
340       || GET_CODE (SET_SRC (elt)) != REG
341       || GET_MODE (SET_SRC (elt)) != SImode
342       || REGNO (SET_SRC (elt)) != (unsigned int) regno
343       || GET_CODE (SET_DEST (elt)) != MEM
344       || GET_MODE (SET_DEST (elt)) != SImode)
345     return false;
346
347   if (REG_P (dest_addr))
348     {
349       dest_base = dest_addr;
350       offset = 0;
351     }
352   else if (GET_CODE (dest_addr) == PLUS
353            && REG_P (XEXP (dest_addr, 0))
354            && GET_CODE (XEXP (dest_addr, 1)) == CONST_INT)
355     {
356       dest_base = XEXP (dest_addr, 0);
357       offset = INTVAL (XEXP (dest_addr, 1));
358     }
359   else
360     return false;
361
362   for (setno = 1; i < XVECLEN (op, 0); setno++, i++)
363     {
364       rtx elt = XVECEXP (op, 0, i);
365       regno += regno_dir;
366
367       if (GET_CODE (elt) != SET
368           || GET_CODE (SET_SRC (elt)) != REG
369           || GET_MODE (SET_SRC (elt)) != SImode
370           || REGNO (SET_SRC (elt)) != (unsigned int) regno
371           || GET_CODE (SET_DEST (elt)) != MEM
372           || GET_MODE (SET_DEST (elt)) != SImode
373           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
374           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_base)
375           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
376           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != setno * 4 + offset)
377         return false;
378     }
379
380   return true;
381 }
382
383 /* The CONDITIONAL_REGISTER_USAGE worker.  */
384
385 void
386 cris_conditional_register_usage (void)
387 {
388   /* FIXME: This isn't nice.  We should be able to use that register for
389      something else if the PIC table isn't needed.  */
390   if (flag_pic)
391     fixed_regs[PIC_OFFSET_TABLE_REGNUM]
392       = call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
393
394   if (TARGET_HAS_MUL_INSNS)
395     fixed_regs[CRIS_MOF_REGNUM] = 0;
396
397   /* On early versions, we must use the 16-bit condition-code register,
398      which has another name.  */
399   if (cris_cpu_version < 8)
400     reg_names[CRIS_CC0_REGNUM] = "ccr";
401 }
402
403 /* Return current_function_uses_pic_offset_table.  For use in cris.md,
404    since some generated files do not include function.h.  */
405
406 int
407 cris_cfun_uses_pic_table (void)
408 {
409   return current_function_uses_pic_offset_table;
410 }
411
412 /* Given an rtx, return the text string corresponding to the CODE of X.
413    Intended for use in the assembly language output section of a
414    define_insn.  */
415
416 const char *
417 cris_op_str (rtx x)
418 {
419   cris_output_insn_is_bound = 0;
420   switch (GET_CODE (x))
421     {
422     case PLUS:
423       return "add";
424       break;
425
426     case MINUS:
427       return "sub";
428       break;
429
430     case MULT:
431       /* This function is for retrieving a part of an instruction name for
432          an operator, for immediate output.  If that ever happens for
433          MULT, we need to apply TARGET_MUL_BUG in the caller.  Make sure
434          we notice.  */
435       internal_error ("MULT case in cris_op_str");
436       break;
437
438     case DIV:
439       return "div";
440       break;
441
442     case AND:
443       return "and";
444       break;
445
446     case IOR:
447       return "or";
448       break;
449
450     case XOR:
451       return "xor";
452       break;
453
454     case NOT:
455       return "not";
456       break;
457
458     case ASHIFT:
459       return "lsl";
460       break;
461
462     case LSHIFTRT:
463       return "lsr";
464       break;
465
466     case ASHIFTRT:
467       return "asr";
468       break;
469
470     case UMIN:
471       /* Used to control the sign/zero-extend character for the 'e' modifier.
472          BOUND has none.  */
473       cris_output_insn_is_bound = 1;
474       return "bound";
475       break;
476
477     default:
478       return "Unknown operator";
479       break;
480   }
481 }
482
483 /* Emit an error message when we're in an asm, and a fatal error for
484    "normal" insns.  Formatted output isn't easily implemented, since we
485    use output_operand_lossage to output the actual message and handle the
486    categorization of the error.  */
487
488 static void
489 cris_operand_lossage (const char *msgid, rtx op)
490 {
491   debug_rtx (op);
492   output_operand_lossage ("%s", msgid);
493 }
494
495 /* Print an index part of an address to file.  */
496
497 static void
498 cris_print_index (rtx index, FILE *file)
499 {
500   rtx inner = XEXP (index, 0);
501
502   /* Make the index "additive" unless we'll output a negative number, in
503      which case the sign character is free (as in free beer).  */
504   if (GET_CODE (index) != CONST_INT || INTVAL (index) >= 0)
505     putc ('+', file);
506
507   if (REG_P (index))
508     fprintf (file, "$%s.b", reg_names[REGNO (index)]);
509   else if (CONSTANT_P (index))
510     cris_output_addr_const (file, index);
511   else if (GET_CODE (index) == MULT)
512     {
513       fprintf (file, "$%s.",
514                reg_names[REGNO (XEXP (index, 0))]);
515
516       putc (INTVAL (XEXP (index, 1)) == 2 ? 'w' : 'd', file);
517     }
518   else if (GET_CODE (index) == SIGN_EXTEND &&
519            GET_CODE (inner) == MEM)
520     {
521       rtx inner_inner = XEXP (inner, 0);
522
523       if (GET_CODE (inner_inner) == POST_INC)
524         {
525           fprintf (file, "[$%s+].",
526                    reg_names[REGNO (XEXP (inner_inner, 0))]);
527           putc (GET_MODE (inner) == HImode ? 'w' : 'b', file);
528         }
529       else
530         {
531           fprintf (file, "[$%s].", reg_names[REGNO (inner_inner)]);
532
533           putc (GET_MODE (inner) == HImode ? 'w' : 'b', file);
534         }
535     }
536   else if (GET_CODE (index) == MEM)
537     {
538       if (GET_CODE (inner) == POST_INC)
539         fprintf (file, "[$%s+].d", reg_names[REGNO (XEXP (inner, 0))]);
540       else
541         fprintf (file, "[$%s].d", reg_names[REGNO (inner)]);
542     }
543   else
544     cris_operand_lossage ("unexpected index-type in cris_print_index",
545                           index);
546 }
547
548 /* Print a base rtx of an address to file.  */
549
550 static void
551 cris_print_base (rtx base, FILE *file)
552 {
553   if (REG_P (base))
554     fprintf (file, "$%s", reg_names[REGNO (base)]);
555   else if (GET_CODE (base) == POST_INC)
556     fprintf (file, "$%s+", reg_names[REGNO (XEXP (base, 0))]);
557   else
558     cris_operand_lossage ("unexpected base-type in cris_print_base",
559                           base);
560 }
561
562 /* Usable as a guard in expressions.  */
563
564 int
565 cris_fatal (char *arg)
566 {
567   internal_error (arg);
568
569   /* We'll never get here; this is just to appease compilers.  */
570   return 0;
571 }
572
573 /* Return nonzero if REGNO is an ordinary register that *needs* to be
574    saved together with other registers, possibly by a MOVEM instruction,
575    or is saved for target-independent reasons.  There may be
576    target-dependent reasons to save the register anyway; this is just a
577    wrapper for a complicated conditional.  */
578
579 static int
580 cris_reg_saved_in_regsave_area (unsigned int regno, bool got_really_used)
581 {
582   return
583     (((regs_ever_live[regno]
584        && !call_used_regs[regno])
585       || (regno == PIC_OFFSET_TABLE_REGNUM
586           && (got_really_used
587               /* It is saved anyway, if there would be a gap.  */
588               || (flag_pic
589                   && regs_ever_live[regno + 1]
590                   && !call_used_regs[regno + 1]))))
591      && (regno != FRAME_POINTER_REGNUM || !frame_pointer_needed)
592      && regno != CRIS_SRP_REGNUM)
593     || (current_function_calls_eh_return
594         && (regno == EH_RETURN_DATA_REGNO (0)
595             || regno == EH_RETURN_DATA_REGNO (1)
596             || regno == EH_RETURN_DATA_REGNO (2)
597             || regno == EH_RETURN_DATA_REGNO (3)));
598 }
599
600 /* Return nonzero if there are regs mentioned in the insn that are not all
601    in the call_used regs.  This is part of the decision whether an insn
602    can be put in the epilogue.  */
603
604 static int
605 saved_regs_mentioned (rtx x)
606 {
607   int i;
608   const char *fmt;
609   RTX_CODE code;
610
611   /* Mainly stolen from refers_to_regno_p in rtlanal.c.  */
612
613   code = GET_CODE (x);
614
615   switch (code)
616     {
617     case REG:
618       i = REGNO (x);
619       return !call_used_regs[i];
620
621     case SUBREG:
622       /* If this is a SUBREG of a hard reg, we can see exactly which
623          registers are being modified.  Otherwise, handle normally.  */
624       i = REGNO (SUBREG_REG (x));
625       return !call_used_regs[i];
626
627     default:
628       ;
629     }
630
631   fmt = GET_RTX_FORMAT (code);
632   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
633     {
634       if (fmt[i] == 'e')
635         {
636           if (saved_regs_mentioned (XEXP (x, i)))
637             return 1;
638         }
639       else if (fmt[i] == 'E')
640         {
641           int j;
642           for (j = XVECLEN (x, i) - 1; j >=0; j--)
643             if (saved_regs_mentioned (XEXP (x, i)))
644               return 1;
645         }
646     }
647
648   return 0;
649 }
650
651 /* The PRINT_OPERAND worker.  */
652
653 void
654 cris_print_operand (FILE *file, rtx x, int code)
655 {
656   rtx operand = x;
657
658   /* Size-strings corresponding to MULT expressions.  */
659   static const char *const mults[] = { "BAD:0", ".b", ".w", "BAD:3", ".d" };
660
661   /* New code entries should just be added to the switch below.  If
662      handling is finished, just return.  If handling was just a
663      modification of the operand, the modified operand should be put in
664      "operand", and then do a break to let default handling
665      (zero-modifier) output the operand.  */
666
667   switch (code)
668     {
669     case 'b':
670       /* Print the unsigned supplied integer as if it were signed
671          and < 0, i.e print 255 or 65535 as -1, 254, 65534 as -2, etc.  */
672       if (GET_CODE (x) != CONST_INT
673           || ! CONST_OK_FOR_LETTER_P (INTVAL (x), 'O'))
674         LOSE_AND_RETURN ("invalid operand for 'b' modifier", x);
675       fprintf (file, HOST_WIDE_INT_PRINT_DEC,
676                INTVAL (x)| (INTVAL (x) <= 255 ? ~255 : ~65535));
677       return;
678
679     case 'x':
680       /* Print assembler code for operator.  */
681       fprintf (file, "%s", cris_op_str (operand));
682       return;
683
684     case 'o':
685       {
686         /* A movem modifier working on a parallel; output the register
687            name.  */
688         int regno;
689
690         if (GET_CODE (x) != PARALLEL)
691           LOSE_AND_RETURN ("invalid operand for 'o' modifier", x);
692
693         /* The second item can be (set reg (plus reg const)) to denote a
694            postincrement.  */
695         regno
696           = (GET_CODE (SET_SRC (XVECEXP (x, 0, 1))) == PLUS
697              ? XVECLEN (x, 0) - 2
698              : XVECLEN (x, 0) - 1);
699
700         fprintf (file, "$%s", reg_names [regno]);
701       }
702       return;
703
704     case 'O':
705       {
706         /* A similar movem modifier; output the memory operand.  */
707         rtx addr;
708
709         if (GET_CODE (x) != PARALLEL)
710           LOSE_AND_RETURN ("invalid operand for 'O' modifier", x);
711
712         /* The lowest mem operand is in the first item, but perhaps it
713            needs to be output as postincremented.  */
714         addr = GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == MEM
715           ? XEXP (SET_SRC (XVECEXP (x, 0, 0)), 0)
716           : XEXP (SET_DEST (XVECEXP (x, 0, 0)), 0);
717
718         /* The second item can be a (set reg (plus reg const)) to denote
719            a modification.  */
720         if (GET_CODE (SET_SRC (XVECEXP (x, 0, 1))) == PLUS)
721           {
722             /* It's a post-increment, if the address is a naked (reg).  */
723             if (REG_P (addr))
724               addr = gen_rtx_POST_INC (SImode, addr);
725             else
726               {
727                 /* Otherwise, it's a side-effect; RN=RN+M.  */
728                 fprintf (file, "[$%s=$%s%s%d]",
729                          reg_names [REGNO (SET_DEST (XVECEXP (x, 0, 1)))],
730                          reg_names [REGNO (XEXP (addr, 0))],
731                          INTVAL (XEXP (addr, 1)) < 0 ? "" : "+",
732                          (int) INTVAL (XEXP (addr, 1)));
733                 return;
734               }
735           }
736         output_address (addr);
737       }
738       return;
739
740     case 'p':
741       /* Adjust a power of two to its log2.  */
742       if (GET_CODE (x) != CONST_INT || exact_log2 (INTVAL (x)) < 0 )
743         LOSE_AND_RETURN ("invalid operand for 'p' modifier", x);
744       fprintf (file, "%d", exact_log2 (INTVAL (x)));
745       return;
746
747     case 's':
748       /* For an integer, print 'b' or 'w' if <= 255 or <= 65535
749          respectively.  This modifier also terminates the inhibiting
750          effects of the 'x' modifier.  */
751       cris_output_insn_is_bound = 0;
752       if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
753         {
754           if (INTVAL (x) >= 0)
755             {
756               if (INTVAL (x) <= 255)
757                 putc ('b', file);
758               else if (INTVAL (x) <= 65535)
759                 putc ('w', file);
760               else
761                 putc ('d', file);
762             }
763           else
764             putc ('d', file);
765           return;
766         }
767
768       /* For a non-integer, print the size of the operand.  */
769       putc ((GET_MODE (x) == SImode || GET_MODE (x) == SFmode)
770             ? 'd' : GET_MODE (x) == HImode ? 'w'
771             : GET_MODE (x) == QImode ? 'b'
772             /* If none of the above, emit an erroneous size letter.  */
773             : 'X',
774             file);
775       return;
776
777     case 'z':
778       /* Const_int: print b for -127 <= x <= 255,
779          w for -32768 <= x <= 65535, else die.  */
780       if (GET_CODE (x) != CONST_INT
781           || INTVAL (x) < -32768 || INTVAL (x) > 65535)
782         LOSE_AND_RETURN ("invalid operand for 'z' modifier", x);
783       putc (INTVAL (x) >= -128 && INTVAL (x) <= 255 ? 'b' : 'w', file);
784       return;
785
786     case '#':
787       /* Output a 'nop' if there's nothing for the delay slot.
788          This method stolen from the sparc files.  */
789       if (dbr_sequence_length () == 0)
790         fputs ("\n\tnop", file);
791       return;
792
793     case '!':
794       /* Output directive for alignment padded with "nop" insns.
795          Optimizing for size, it's plain 4-byte alignment, otherwise we
796          align the section to a cache-line (32 bytes) and skip at max 2
797          bytes, i.e. we skip if it's the last insn on a cache-line.  The
798          latter is faster by a small amount (for two test-programs 99.6%
799          and 99.9%) and larger by a small amount (ditto 100.1% and
800          100.2%).  This is supposed to be the simplest yet performance-
801          wise least intrusive way to make sure the immediately following
802          (supposed) muls/mulu insn isn't located at the end of a
803          cache-line.  */
804       if (TARGET_MUL_BUG)
805         fputs (optimize_size
806                ? ".p2alignw 2,0x050f\n\t"
807                : ".p2alignw 5,0x050f,2\n\t", file);
808       return;
809
810     case ':':
811       /* The PIC register.  */
812       if (! flag_pic)
813         internal_error ("invalid use of ':' modifier");
814       fprintf (file, "$%s", reg_names [PIC_OFFSET_TABLE_REGNUM]);
815       return;
816
817     case 'H':
818       /* Print high (most significant) part of something.  */
819       switch (GET_CODE (operand))
820         {
821         case CONST_INT:
822           /* If we're having 64-bit HOST_WIDE_INTs, the whole (DImode)
823              value is kept here, and so may be other than 0 or -1.  */
824           fprintf (file, HOST_WIDE_INT_PRINT_DEC,
825                    INTVAL (operand_subword (operand, 1, 0, DImode)));
826           return;
827
828         case CONST_DOUBLE:
829           /* High part of a long long constant.  */
830           if (GET_MODE (operand) == VOIDmode)
831             {
832               fprintf (file, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_HIGH (x));
833               return;
834             }
835           else
836             LOSE_AND_RETURN ("invalid operand for 'H' modifier", x);
837
838         case REG:
839           /* Print reg + 1.  Check that there's not an attempt to print
840              high-parts of registers like stack-pointer or higher.  */
841           if (REGNO (operand) > STACK_POINTER_REGNUM - 2)
842             LOSE_AND_RETURN ("bad register", operand);
843           fprintf (file, "$%s", reg_names[REGNO (operand) + 1]);
844           return;
845
846         case MEM:
847           /* Adjust memory address to high part.  */
848           {
849             rtx adj_mem = operand;
850             int size
851               = GET_MODE_BITSIZE (GET_MODE (operand)) / BITS_PER_UNIT;
852
853             /* Adjust so we can use two SImode in DImode.
854                Calling adj_offsettable_operand will make sure it is an
855                offsettable address.  Don't do this for a postincrement
856                though; it should remain as it was.  */
857             if (GET_CODE (XEXP (adj_mem, 0)) != POST_INC)
858               adj_mem
859                 = adjust_address (adj_mem, GET_MODE (adj_mem), size / 2);
860
861             output_address (XEXP (adj_mem, 0));
862             return;
863           }
864
865         default:
866           LOSE_AND_RETURN ("invalid operand for 'H' modifier", x);
867         }
868
869     case 'L':
870       /* Strip the MEM expression.  */
871       operand = XEXP (operand, 0);
872       break;
873
874     case 'e':
875       /* Print 's' if operand is SIGN_EXTEND or 'u' if ZERO_EXTEND unless
876          cris_output_insn_is_bound is nonzero.  */
877       if (GET_CODE (operand) != SIGN_EXTEND
878           && GET_CODE (operand) != ZERO_EXTEND
879           && GET_CODE (operand) != CONST_INT)
880         LOSE_AND_RETURN ("invalid operand for 'e' modifier", x);
881
882       if (cris_output_insn_is_bound)
883         {
884           cris_output_insn_is_bound = 0;
885           return;
886         }
887
888       putc (GET_CODE (operand) == SIGN_EXTEND
889             || (GET_CODE (operand) == CONST_INT && INTVAL (operand) < 0)
890             ? 's' : 'u', file);
891       return;
892
893     case 'm':
894       /* Print the size letter of the inner element.  We can do it by
895          calling ourselves with the 's' modifier.  */
896       if (GET_CODE (operand) != SIGN_EXTEND && GET_CODE (operand) != ZERO_EXTEND)
897         LOSE_AND_RETURN ("invalid operand for 'm' modifier", x);
898       cris_print_operand (file, XEXP (operand, 0), 's');
899       return;
900
901     case 'M':
902       /* Print the least significant part of operand.  */
903       if (GET_CODE (operand) == CONST_DOUBLE)
904         {
905           fprintf (file, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_LOW (x));
906           return;
907         }
908       else if (HOST_BITS_PER_WIDE_INT > 32 && GET_CODE (operand) == CONST_INT)
909         {
910           fprintf (file, HOST_WIDE_INT_PRINT_HEX,
911                    INTVAL (x) & ((unsigned int) 0x7fffffff * 2 + 1));
912           return;
913         }
914       /* Otherwise the least significant part equals the normal part,
915          so handle it normally.  */
916       break;
917
918     case 'A':
919       /* When emitting an add for the high part of a DImode constant, we
920          want to use addq for 0 and adds.w for -1.  */
921       if (GET_CODE (operand) != CONST_INT)
922         LOSE_AND_RETURN ("invalid operand for 'A' modifier", x);
923       fprintf (file, INTVAL (operand) < 0 ? "adds.w" : "addq");
924       return;
925
926     case 'd':
927       /* If this is a GOT symbol, force it to be emitted as :GOT and
928          :GOTPLT regardless of -fpic (i.e. not as :GOT16, :GOTPLT16).
929          Avoid making this too much of a special case.  */
930       if (flag_pic == 1 && CONSTANT_P (operand))
931         {
932           int flag_pic_save = flag_pic;
933
934           flag_pic = 2;
935           cris_output_addr_const (file, operand);
936           flag_pic = flag_pic_save;
937           return;
938         }
939       break;
940
941     case 'D':
942       /* When emitting an sub for the high part of a DImode constant, we
943          want to use subq for 0 and subs.w for -1.  */
944       if (GET_CODE (operand) != CONST_INT)
945         LOSE_AND_RETURN ("invalid operand for 'D' modifier", x);
946       fprintf (file, INTVAL (operand) < 0 ? "subs.w" : "subq");
947       return;
948
949     case 'S':
950       /* Print the operand as the index-part of an address.
951          Easiest way out is to use cris_print_index.  */
952       cris_print_index (operand, file);
953       return;
954
955     case 'T':
956       /* Print the size letter for an operand to a MULT, which must be a
957          const_int with a suitable value.  */
958       if (GET_CODE (operand) != CONST_INT || INTVAL (operand) > 4)
959         LOSE_AND_RETURN ("invalid operand for 'T' modifier", x);
960       fprintf (file, "%s", mults[INTVAL (operand)]);
961       return;
962
963     case 0:
964       /* No code, print as usual.  */
965       break;
966
967     default:
968       LOSE_AND_RETURN ("invalid operand modifier letter", x);
969     }
970
971   /* Print an operand as without a modifier letter.  */
972   switch (GET_CODE (operand))
973     {
974     case REG:
975       if (REGNO (operand) > 15
976           && REGNO (operand) != CRIS_MOF_REGNUM
977           && REGNO (operand) != CRIS_SRP_REGNUM
978           && REGNO (operand) != CRIS_CC0_REGNUM)
979         internal_error ("internal error: bad register: %d", REGNO (operand));
980       fprintf (file, "$%s", reg_names[REGNO (operand)]);
981       return;
982
983     case MEM:
984       output_address (XEXP (operand, 0));
985       return;
986
987     case CONST_DOUBLE:
988       if (GET_MODE (operand) == VOIDmode)
989         /* A long long constant.  */
990         output_addr_const (file, operand);
991       else
992         {
993           /* Only single precision is allowed as plain operands the
994              moment.  FIXME:  REAL_VALUE_FROM_CONST_DOUBLE isn't
995              documented.  */
996           REAL_VALUE_TYPE r;
997           long l;
998
999           /* FIXME:  Perhaps check overflow of the "single".  */
1000           REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
1001           REAL_VALUE_TO_TARGET_SINGLE (r, l);
1002
1003           fprintf (file, "0x%lx", l);
1004         }
1005       return;
1006
1007     case UNSPEC:
1008       /* Fall through.  */
1009     case CONST:
1010       cris_output_addr_const (file, operand);
1011       return;
1012
1013     case MULT:
1014     case ASHIFT:
1015       {
1016         /* For a (MULT (reg X) const_int) we output "rX.S".  */
1017         int i = GET_CODE (XEXP (operand, 1)) == CONST_INT
1018           ? INTVAL (XEXP (operand, 1)) : INTVAL (XEXP (operand, 0));
1019         rtx reg = GET_CODE (XEXP (operand, 1)) == CONST_INT
1020           ? XEXP (operand, 0) : XEXP (operand, 1);
1021
1022         if (GET_CODE (reg) != REG
1023             || (GET_CODE (XEXP (operand, 0)) != CONST_INT
1024                 && GET_CODE (XEXP (operand, 1)) != CONST_INT))
1025           LOSE_AND_RETURN ("unexpected multiplicative operand", x);
1026
1027         cris_print_base (reg, file);
1028         fprintf (file, ".%c",
1029                  i == 0 || (i == 1 && GET_CODE (operand) == MULT) ? 'b'
1030                  : i == 4 ? 'd'
1031                  : (i == 2 && GET_CODE (operand) == MULT) || i == 1 ? 'w'
1032                  : 'd');
1033         return;
1034       }
1035
1036     default:
1037       /* No need to handle all strange variants, let output_addr_const
1038          do it for us.  */
1039       if (CONSTANT_P (operand))
1040         {
1041           cris_output_addr_const (file, operand);
1042           return;
1043         }
1044
1045       LOSE_AND_RETURN ("unexpected operand", x);
1046     }
1047 }
1048
1049 /* The PRINT_OPERAND_ADDRESS worker.  */
1050
1051 void
1052 cris_print_operand_address (FILE *file, rtx x)
1053 {
1054   /* All these were inside MEM:s so output indirection characters.  */
1055   putc ('[', file);
1056
1057   if (CONSTANT_ADDRESS_P (x))
1058     cris_output_addr_const (file, x);
1059   else if (BASE_OR_AUTOINCR_P (x))
1060     cris_print_base (x, file);
1061   else if (GET_CODE (x) == PLUS)
1062     {
1063       rtx x1, x2;
1064
1065       x1 = XEXP (x, 0);
1066       x2 = XEXP (x, 1);
1067       if (BASE_P (x1))
1068         {
1069           cris_print_base (x1, file);
1070           cris_print_index (x2, file);
1071         }
1072       else if (BASE_P (x2))
1073         {
1074           cris_print_base (x2, file);
1075           cris_print_index (x1, file);
1076         }
1077       else
1078         LOSE_AND_RETURN ("unrecognized address", x);
1079     }
1080   else if (GET_CODE (x) == MEM)
1081     {
1082       /* A DIP.  Output more indirection characters.  */
1083       putc ('[', file);
1084       cris_print_base (XEXP (x, 0), file);
1085       putc (']', file);
1086     }
1087   else
1088     LOSE_AND_RETURN ("unrecognized address", x);
1089
1090   putc (']', file);
1091 }
1092
1093 /* The RETURN_ADDR_RTX worker.
1094    We mark that the return address is used, either by EH or
1095    __builtin_return_address, for use by the function prologue and
1096    epilogue.  FIXME: This isn't optimal; we just use the mark in the
1097    prologue and epilogue to say that the return address is to be stored
1098    in the stack frame.  We could return SRP for leaf-functions and use the
1099    initial-value machinery.  */
1100
1101 rtx
1102 cris_return_addr_rtx (int count, rtx frameaddr ATTRIBUTE_UNUSED)
1103 {
1104   cfun->machine->needs_return_address_on_stack = 1;
1105
1106   /* The return-address is stored just above the saved frame-pointer (if
1107      present).  Apparently we can't eliminate from the frame-pointer in
1108      that direction, so use the incoming args (maybe pretended) pointer.  */
1109   return count == 0
1110     ? gen_rtx_MEM (Pmode, plus_constant (virtual_incoming_args_rtx, -4))
1111     : NULL_RTX;
1112 }
1113
1114 /* Accessor used in cris.md:return because cfun->machine isn't available
1115    there.  */
1116
1117 bool
1118 cris_return_address_on_stack (void)
1119 {
1120   return regs_ever_live[CRIS_SRP_REGNUM]
1121     || cfun->machine->needs_return_address_on_stack;
1122 }
1123
1124 /* Accessor used in cris.md:return because cfun->machine isn't available
1125    there.  */
1126
1127 bool
1128 cris_return_address_on_stack_for_return (void)
1129 {
1130   return cfun->machine->return_type == CRIS_RETINSN_RET ? false
1131     : cris_return_address_on_stack ();
1132 }
1133
1134 /* This used to be the INITIAL_FRAME_POINTER_OFFSET worker; now only
1135    handles FP -> SP elimination offset.  */
1136
1137 static int
1138 cris_initial_frame_pointer_offset (void)
1139 {
1140   int regno;
1141
1142   /* Initial offset is 0 if we don't have a frame pointer.  */
1143   int offs = 0;
1144   bool got_really_used = false;
1145
1146   if (current_function_uses_pic_offset_table)
1147     {
1148       push_topmost_sequence ();
1149       got_really_used
1150         = reg_used_between_p (pic_offset_table_rtx, get_insns (),
1151                               NULL_RTX);
1152       pop_topmost_sequence ();
1153     }
1154
1155   /* And 4 for each register pushed.  */
1156   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1157     if (cris_reg_saved_in_regsave_area (regno, got_really_used))
1158       offs += 4;
1159
1160   /* And then, last, we add the locals allocated.  */
1161   offs += get_frame_size ();
1162
1163   /* And more; the accumulated args size.  */
1164   offs += current_function_outgoing_args_size;
1165
1166   /* Then round it off, in case we use aligned stack.  */
1167   if (TARGET_STACK_ALIGN)
1168     offs = TARGET_ALIGN_BY_32 ? (offs + 3) & ~3 : (offs + 1) & ~1;
1169
1170   return offs;
1171 }
1172
1173 /* The INITIAL_ELIMINATION_OFFSET worker.
1174    Calculate the difference between imaginary registers such as frame
1175    pointer and the stack pointer.  Used to eliminate the frame pointer
1176    and imaginary arg pointer.  */
1177
1178 int
1179 cris_initial_elimination_offset (int fromreg, int toreg)
1180 {
1181   int fp_sp_offset
1182     = cris_initial_frame_pointer_offset ();
1183
1184   /* We should be able to use regs_ever_live and related prologue
1185      information here, or alpha should not as well.  */
1186   bool return_address_on_stack = cris_return_address_on_stack ();
1187
1188   /* Here we act as if the frame-pointer were needed.  */
1189   int ap_fp_offset = 4 + (return_address_on_stack ? 4 : 0);
1190
1191   if (fromreg == ARG_POINTER_REGNUM
1192       && toreg == FRAME_POINTER_REGNUM)
1193     return ap_fp_offset;
1194
1195   /* Between the frame pointer and the stack are only "normal" stack
1196      variables and saved registers.  */
1197   if (fromreg == FRAME_POINTER_REGNUM
1198       && toreg == STACK_POINTER_REGNUM)
1199     return fp_sp_offset;
1200
1201   /* We need to balance out the frame pointer here.  */
1202   if (fromreg == ARG_POINTER_REGNUM
1203       && toreg == STACK_POINTER_REGNUM)
1204     return ap_fp_offset + fp_sp_offset - 4;
1205
1206   gcc_unreachable ();
1207 }
1208
1209 /* Worker function for LEGITIMIZE_RELOAD_ADDRESS.  */
1210
1211 bool
1212 cris_reload_address_legitimized (rtx x,
1213                                  enum machine_mode mode ATTRIBUTE_UNUSED,
1214                                  int opnum ATTRIBUTE_UNUSED,
1215                                  int itype,
1216                                  int ind_levels ATTRIBUTE_UNUSED)
1217 {
1218   enum reload_type type = itype;
1219   rtx op0, op1;
1220   rtx *op0p;
1221   rtx *op1p;
1222
1223   if (GET_CODE (x) != PLUS)
1224     return false;
1225
1226   op0 = XEXP (x, 0);
1227   op0p = &XEXP (x, 0);
1228   op1 = XEXP (x, 1);
1229   op1p = &XEXP (x, 1);
1230
1231   if (!REG_P (op1))
1232     return false;
1233
1234   if (GET_CODE (op0) == SIGN_EXTEND
1235       && GET_CODE (XEXP (op0, 0)) == MEM)
1236     {
1237       rtx op00 = XEXP (op0, 0);
1238       rtx op000 = XEXP (op00, 0);
1239       rtx *op000p = &XEXP (op00, 0);
1240
1241       if ((GET_MODE (op00) == HImode || GET_MODE (op00) == QImode)
1242           && (REG_P (op000)
1243               || (GET_CODE (op000) == POST_INC && REG_P (XEXP (op000, 0)))))
1244         {
1245           bool something_reloaded = false;
1246
1247           if (GET_CODE (op000) == POST_INC
1248               && REG_P (XEXP (op000, 0))
1249               && REGNO (XEXP (op000, 0)) > CRIS_LAST_GENERAL_REGISTER)
1250             /* No, this gets too complicated and is too rare to care
1251                about trying to improve on the general code Here.
1252                As the return-value is an all-or-nothing indicator, we
1253                punt on the other register too.  */
1254             return false;
1255
1256           if ((REG_P (op000)
1257                && REGNO (op000) > CRIS_LAST_GENERAL_REGISTER))
1258             {
1259               /* The address of the inner mem is a pseudo or wrong
1260                  reg: reload that.  */
1261               push_reload (op000, NULL_RTX, op000p, NULL, GENERAL_REGS,
1262                            GET_MODE (x), VOIDmode, 0, 0, opnum, type);
1263               something_reloaded = true;
1264             }
1265
1266           if (REGNO (op1) > CRIS_LAST_GENERAL_REGISTER)
1267             {
1268               /* Base register is a pseudo or wrong reg: reload it.  */
1269               push_reload (op1, NULL_RTX, op1p, NULL, GENERAL_REGS,
1270                            GET_MODE (x), VOIDmode, 0, 0,
1271                            opnum, type);
1272               something_reloaded = true;
1273             }
1274
1275           gcc_assert (something_reloaded);
1276
1277           return true;
1278         }
1279     }
1280
1281   return false;
1282 }
1283
1284 /*  This function looks into the pattern to see how this insn affects
1285     condition codes.
1286
1287     Used when to eliminate test insns before a condition-code user,
1288     such as a "scc" insn or a conditional branch.  This includes
1289     checking if the entities that cc was updated by, are changed by the
1290     operation.
1291
1292     Currently a jumble of the old peek-inside-the-insn and the newer
1293     check-cc-attribute methods.  */
1294
1295 void
1296 cris_notice_update_cc (rtx exp, rtx insn)
1297 {
1298   /* Check if user specified "-mcc-init" as a bug-workaround.  FIXME:
1299      TARGET_CCINIT does not work; we must set CC_REVERSED as below.
1300      Several testcases will otherwise fail, for example
1301      gcc.c-torture/execute/20000217-1.c -O0 and -O1.  */
1302   if (TARGET_CCINIT)
1303     {
1304       CC_STATUS_INIT;
1305       return;
1306     }
1307
1308   /* Slowly, we're converting to using attributes to control the setting
1309      of condition-code status.  */
1310   switch (get_attr_cc (insn))
1311     {
1312     case CC_NONE:
1313       /* Even if it is "none", a setting may clobber a previous
1314          cc-value, so check.  */
1315       if (GET_CODE (exp) == SET)
1316         {
1317           if (cc_status.value1
1318               && modified_in_p (cc_status.value1, insn))
1319             cc_status.value1 = 0;
1320
1321           if (cc_status.value2
1322               && modified_in_p (cc_status.value2, insn))
1323             cc_status.value2 = 0;
1324         }
1325       return;
1326
1327     case CC_CLOBBER:
1328       CC_STATUS_INIT;
1329       break;
1330
1331     case CC_NORMAL:
1332       /* Which means, for:
1333          (set (cc0) (...)):
1334          CC is (...).
1335
1336          (set (reg) (...)):
1337          CC is (reg) and (...) - unless (...) is 0, then CC does not change.
1338          CC_NO_OVERFLOW unless (...) is reg or mem.
1339
1340          (set (mem) (...)):
1341          CC does not change.
1342
1343          (set (pc) (...)):
1344          CC does not change.
1345
1346          (parallel
1347           (set (reg1) (mem (bdap/biap)))
1348           (set (reg2) (bdap/biap))):
1349          CC is (reg1) and (mem (reg2))
1350
1351          (parallel
1352           (set (mem (bdap/biap)) (reg1)) [or 0]
1353           (set (reg2) (bdap/biap))):
1354          CC does not change.
1355
1356          (where reg and mem includes strict_low_parts variants thereof)
1357
1358          For all others, assume CC is clobbered.
1359          Note that we do not have to care about setting CC_NO_OVERFLOW,
1360          since the overflow flag is set to 0 (i.e. right) for
1361          instructions where it does not have any sane sense, but where
1362          other flags have meanings.  (This includes shifts; the carry is
1363          not set by them).
1364
1365          Note that there are other parallel constructs we could match,
1366          but we don't do that yet.  */
1367
1368       if (GET_CODE (exp) == SET)
1369         {
1370           /* FIXME: Check when this happens.  It looks like we should
1371              actually do a CC_STATUS_INIT here to be safe.  */
1372           if (SET_DEST (exp) == pc_rtx)
1373             return;
1374
1375           /* Record CC0 changes, so we do not have to output multiple
1376              test insns.  */
1377           if (SET_DEST (exp) == cc0_rtx)
1378             {
1379               cc_status.value1 = SET_SRC (exp);
1380               cc_status.value2 = 0;
1381
1382               /* Handle flags for the special btstq on one bit.  */
1383               if (GET_CODE (SET_SRC (exp)) == ZERO_EXTRACT
1384                   && XEXP (SET_SRC (exp), 1) == const1_rtx)
1385                 {
1386                   if (GET_CODE (XEXP (SET_SRC (exp), 0)) == CONST_INT)
1387                     /* Using cmpq.  */
1388                     cc_status.flags = CC_INVERTED;
1389                   else
1390                     /* A one-bit btstq.  */
1391                     cc_status.flags = CC_Z_IN_NOT_N;
1392                 }
1393               else
1394                 cc_status.flags = 0;
1395
1396               if (GET_CODE (SET_SRC (exp)) == COMPARE)
1397                 {
1398                   if (!REG_P (XEXP (SET_SRC (exp), 0))
1399                       && XEXP (SET_SRC (exp), 1) != const0_rtx)
1400                     /* For some reason gcc will not canonicalize compare
1401                        operations, reversing the sign by itself if
1402                        operands are in wrong order.  */
1403                     /* (But NOT inverted; eq is still eq.) */
1404                     cc_status.flags = CC_REVERSED;
1405
1406                   /* This seems to be overlooked by gcc.  FIXME: Check again.
1407                      FIXME:  Is it really safe?  */
1408                   cc_status.value2
1409                     = gen_rtx_MINUS (GET_MODE (SET_SRC (exp)),
1410                                      XEXP (SET_SRC (exp), 0),
1411                                      XEXP (SET_SRC (exp), 1));
1412                 }
1413               return;
1414             }
1415           else if (REG_P (SET_DEST (exp))
1416                    || (GET_CODE (SET_DEST (exp)) == STRICT_LOW_PART
1417                        && REG_P (XEXP (SET_DEST (exp), 0))))
1418             {
1419               /* A register is set; normally CC is set to show that no
1420                  test insn is needed.  Catch the exceptions.  */
1421
1422               /* If not to cc0, then no "set"s in non-natural mode give
1423                  ok cc0...  */
1424               if (GET_MODE_SIZE (GET_MODE (SET_DEST (exp))) > UNITS_PER_WORD
1425                   || GET_MODE_CLASS (GET_MODE (SET_DEST (exp))) == MODE_FLOAT)
1426                 {
1427                   /* ... except add:s and sub:s in DImode.  */
1428                   if (GET_MODE (SET_DEST (exp)) == DImode
1429                       && (GET_CODE (SET_SRC (exp)) == PLUS
1430                           || GET_CODE (SET_SRC (exp)) == MINUS))
1431                     {
1432                       cc_status.flags = 0;
1433                       cc_status.value1 = SET_DEST (exp);
1434                       cc_status.value2 = SET_SRC (exp);
1435
1436                       if (cris_reg_overlap_mentioned_p (cc_status.value1,
1437                                                         cc_status.value2))
1438                         cc_status.value2 = 0;
1439
1440                       /* Add and sub may set V, which gets us
1441                          unoptimizable results in "gt" and "le" condition
1442                          codes.  */
1443                       cc_status.flags |= CC_NO_OVERFLOW;
1444
1445                       return;
1446                     }
1447                 }
1448               else if (SET_SRC (exp) == const0_rtx)
1449                 {
1450                   /* There's no CC0 change when clearing a register or
1451                      memory.  Just check for overlap.  */
1452                   if (cc_status.value1
1453                       && modified_in_p (cc_status.value1, insn))
1454                     cc_status.value1 = 0;
1455
1456                   if (cc_status.value2
1457                       && modified_in_p (cc_status.value2, insn))
1458                     cc_status.value2 = 0;
1459
1460                   return;
1461                 }
1462               else
1463                 {
1464                   cc_status.flags = 0;
1465                   cc_status.value1 = SET_DEST (exp);
1466                   cc_status.value2 = SET_SRC (exp);
1467
1468                   if (cris_reg_overlap_mentioned_p (cc_status.value1,
1469                                                     cc_status.value2))
1470                     cc_status.value2 = 0;
1471
1472                   /* Some operations may set V, which gets us
1473                      unoptimizable results in "gt" and "le" condition
1474                      codes.  */
1475                   if (GET_CODE (SET_SRC (exp)) == PLUS
1476                       || GET_CODE (SET_SRC (exp)) == MINUS
1477                       || GET_CODE (SET_SRC (exp)) == NEG)
1478                     cc_status.flags |= CC_NO_OVERFLOW;
1479
1480                   return;
1481                 }
1482             }
1483           else if (GET_CODE (SET_DEST (exp)) == MEM
1484                    || (GET_CODE (SET_DEST (exp)) == STRICT_LOW_PART
1485                        && GET_CODE (XEXP (SET_DEST (exp), 0)) == MEM))
1486             {
1487               /* When SET to MEM, then CC is not changed (except for
1488                  overlap).  */
1489               if (cc_status.value1
1490                   && modified_in_p (cc_status.value1, insn))
1491                 cc_status.value1 = 0;
1492
1493               if (cc_status.value2
1494                   && modified_in_p (cc_status.value2, insn))
1495                 cc_status.value2 = 0;
1496
1497               return;
1498             }
1499         }
1500       else if (GET_CODE (exp) == PARALLEL)
1501         {
1502           if (GET_CODE (XVECEXP (exp, 0, 0)) == SET
1503               && GET_CODE (XVECEXP (exp, 0, 1)) == SET
1504               && REG_P (XEXP (XVECEXP (exp, 0, 1), 0)))
1505             {
1506               if (REG_P (XEXP (XVECEXP (exp, 0, 0), 0))
1507                   && GET_CODE (XEXP (XVECEXP (exp, 0, 0), 1)) == MEM)
1508                 {
1509                   /* For "move.S [rx=ry+o],rz", say CC reflects
1510                      value1=rz and value2=[rx] */
1511                   cc_status.value1 = XEXP (XVECEXP (exp, 0, 0), 0);
1512                   cc_status.value2
1513                     = replace_equiv_address (XEXP (XVECEXP (exp, 0, 0), 1),
1514                                              XEXP (XVECEXP (exp, 0, 1), 0));
1515                   cc_status.flags = 0;
1516
1517                   /* Huh?  A side-effect cannot change the destination
1518                      register.  */
1519                   if (cris_reg_overlap_mentioned_p (cc_status.value1,
1520                                                     cc_status.value2))
1521                     internal_error ("internal error: sideeffect-insn affecting main effect");
1522                   return;
1523                 }
1524               else if ((REG_P (XEXP (XVECEXP (exp, 0, 0), 1))
1525                         || XEXP (XVECEXP (exp, 0, 0), 1) == const0_rtx)
1526                        && GET_CODE (XEXP (XVECEXP (exp, 0, 0), 0)) == MEM)
1527                 {
1528                   /* For "move.S rz,[rx=ry+o]" and "clear.S [rx=ry+o]",
1529                      say flags are not changed, except for overlap.  */
1530                   if (cc_status.value1
1531                       && modified_in_p (cc_status.value1, insn))
1532                     cc_status.value1 = 0;
1533
1534                   if (cc_status.value2
1535                       && modified_in_p (cc_status.value2, insn))
1536                     cc_status.value2 = 0;
1537
1538                   return;
1539                 }
1540             }
1541         }
1542       break;
1543
1544     default:
1545       internal_error ("unknown cc_attr value");
1546     }
1547
1548   CC_STATUS_INIT;
1549 }
1550
1551 /* Return != 0 if the return sequence for the current function is short,
1552    like "ret" or "jump [sp+]".  Prior to reloading, we can't tell if
1553    registers must be saved, so return 0 then.  */
1554
1555 bool
1556 cris_simple_epilogue (void)
1557 {
1558   unsigned int regno;
1559   unsigned int reglimit = STACK_POINTER_REGNUM;
1560   bool got_really_used = false;
1561
1562   if (! reload_completed
1563       || frame_pointer_needed
1564       || get_frame_size () != 0
1565       || current_function_pretend_args_size
1566       || current_function_args_size
1567       || current_function_outgoing_args_size
1568       || current_function_calls_eh_return
1569
1570       /* If we're not supposed to emit prologue and epilogue, we must
1571          not emit return-type instructions.  */
1572       || !TARGET_PROLOGUE_EPILOGUE)
1573     return false;
1574
1575   if (current_function_uses_pic_offset_table)
1576     {
1577       push_topmost_sequence ();
1578       got_really_used
1579         = reg_used_between_p (pic_offset_table_rtx, get_insns (), NULL_RTX);
1580       pop_topmost_sequence ();
1581     }
1582
1583   /* No simple epilogue if there are saved registers.  */
1584   for (regno = 0; regno < reglimit; regno++)
1585     if (cris_reg_saved_in_regsave_area (regno, got_really_used))
1586       return false;
1587
1588   return true;
1589 }
1590
1591 /* Expand a return insn (just one insn) marked as using SRP or stack
1592    slot depending on parameter ON_STACK.  */
1593
1594 void
1595 cris_expand_return (bool on_stack)
1596 {
1597   /* FIXME: emit a parallel with a USE for SRP or the stack-slot, to
1598      tell "ret" from "jump [sp+]".  Some, but not all, other parts of
1599      GCC expect just (return) to do the right thing when optimizing, so
1600      we do that until they're fixed.  Currently, all return insns in a
1601      function must be the same (not really a limiting factor) so we need
1602      to check that it doesn't change half-way through.  */
1603   emit_jump_insn (gen_rtx_RETURN (VOIDmode));
1604
1605   CRIS_ASSERT (cfun->machine->return_type != CRIS_RETINSN_RET || !on_stack);
1606   CRIS_ASSERT (cfun->machine->return_type != CRIS_RETINSN_JUMP || on_stack);
1607
1608   cfun->machine->return_type
1609     = on_stack ? CRIS_RETINSN_JUMP : CRIS_RETINSN_RET;
1610 }
1611
1612 /* Compute a (partial) cost for rtx X.  Return true if the complete
1613    cost has been computed, and false if subexpressions should be
1614    scanned.  In either case, *TOTAL contains the cost result.  */
1615
1616 static bool
1617 cris_rtx_costs (rtx x, int code, int outer_code, int *total)
1618 {
1619   switch (code)
1620     {
1621     case CONST_INT:
1622       {
1623         HOST_WIDE_INT val = INTVAL (x);
1624         if (val == 0)
1625           *total = 0;
1626         else if (val < 32 && val >= -32)
1627           *total = 1;
1628         /* Eight or 16 bits are a word and cycle more expensive.  */
1629         else if (val <= 32767 && val >= -32768)
1630           *total = 2;
1631         /* A 32 bit constant (or very seldom, unsigned 16 bits) costs
1632            another word.  FIXME: This isn't linear to 16 bits.  */
1633         else
1634           *total = 4;
1635         return true;
1636       }
1637
1638     case LABEL_REF:
1639       *total = 6;
1640       return true;
1641
1642     case CONST:
1643     case SYMBOL_REF:
1644       *total = 6;
1645       return true;
1646
1647     case CONST_DOUBLE:
1648       if (x != CONST0_RTX (GET_MODE (x) == VOIDmode ? DImode : GET_MODE (x)))
1649         *total = 12;
1650       else
1651         /* Make 0.0 cheap, else test-insns will not be used.  */
1652         *total = 0;
1653       return true;
1654
1655     case MULT:
1656       /* Identify values that are no powers of two.  Powers of 2 are
1657          taken care of already and those values should not be changed.  */
1658       if (GET_CODE (XEXP (x, 1)) != CONST_INT
1659           || exact_log2 (INTVAL (XEXP (x, 1)) < 0))
1660         {
1661           /* If we have a multiply insn, then the cost is between
1662              1 and 2 "fast" instructions.  */
1663           if (TARGET_HAS_MUL_INSNS)
1664             {
1665               *total = COSTS_N_INSNS (1) + COSTS_N_INSNS (1) / 2;
1666               return true;
1667             }
1668
1669           /* Estimate as 4 + 4 * #ofbits.  */
1670           *total = COSTS_N_INSNS (132);
1671           return true;
1672         }
1673       return false;
1674
1675     case UDIV:
1676     case MOD:
1677     case UMOD:
1678     case DIV:
1679       if (GET_CODE (XEXP (x, 1)) != CONST_INT
1680           || exact_log2 (INTVAL (XEXP (x, 1)) < 0))
1681         {
1682           /* Estimate this as 4 + 8 * #of bits.  */
1683           *total = COSTS_N_INSNS (260);
1684           return true;
1685         }
1686       return false;
1687
1688     case AND:
1689       if (GET_CODE (XEXP (x, 1)) == CONST_INT
1690           /* Two constants may actually happen before optimization.  */
1691           && GET_CODE (XEXP (x, 0)) != CONST_INT
1692           && !CONST_OK_FOR_LETTER_P (INTVAL (XEXP (x, 1)), 'I'))
1693         {
1694           *total = (rtx_cost (XEXP (x, 0), outer_code) + 2
1695                     + 2 * GET_MODE_NUNITS (GET_MODE (XEXP (x, 0))));
1696           return true;
1697         }
1698       return false;
1699
1700     case ZERO_EXTEND: case SIGN_EXTEND:
1701       *total = rtx_cost (XEXP (x, 0), outer_code);
1702       return true;
1703
1704     default:
1705       return false;
1706     }
1707 }
1708
1709 /* The ADDRESS_COST worker.  */
1710
1711 static int
1712 cris_address_cost (rtx x)
1713 {
1714   /* The metric to use for the cost-macros is unclear.
1715      The metric used here is (the number of cycles needed) / 2,
1716      where we consider equal a cycle for a word of code and a cycle to
1717      read memory.  */
1718
1719   /* The cheapest addressing modes get 0, since nothing extra is needed.  */
1720   if (BASE_OR_AUTOINCR_P (x))
1721     return 0;
1722
1723   /* An indirect mem must be a DIP.  This means two bytes extra for code,
1724      and 4 bytes extra for memory read, i.e.  (2 + 4) / 2.  */
1725   if (GET_CODE (x) == MEM)
1726     return (2 + 4) / 2;
1727
1728   /* Assume (2 + 4) / 2 for a single constant; a dword, since it needs
1729      an extra DIP prefix and 4 bytes of constant in most cases.  */
1730   if (CONSTANT_P (x))
1731     return (2 + 4) / 2;
1732
1733   /* Handle BIAP and BDAP prefixes.  */
1734   if (GET_CODE (x) == PLUS)
1735     {
1736       rtx tem1 = XEXP (x, 0);
1737       rtx tem2 = XEXP (x, 1);
1738
1739     /* A BIAP is 2 extra bytes for the prefix insn, nothing more.  We
1740        recognize the typical MULT which is always in tem1 because of
1741        insn canonicalization.  */
1742     if ((GET_CODE (tem1) == MULT && BIAP_INDEX_P (tem1))
1743         || REG_P (tem1))
1744       return 2 / 2;
1745
1746     /* A BDAP (quick) is 2 extra bytes.  Any constant operand to the
1747        PLUS is always found in tem2.  */
1748     if (GET_CODE (tem2) == CONST_INT
1749         && INTVAL (tem2) < 128 && INTVAL (tem2) >= -128)
1750       return 2 / 2;
1751
1752     /* A BDAP -32768 .. 32767 is like BDAP quick, but with 2 extra
1753        bytes.  */
1754     if (GET_CODE (tem2) == CONST_INT
1755         && CONST_OK_FOR_LETTER_P (INTVAL (tem2), 'L'))
1756       return (2 + 2) / 2;
1757
1758     /* A BDAP with some other constant is 2 bytes extra.  */
1759     if (CONSTANT_P (tem2))
1760       return (2 + 2 + 2) / 2;
1761
1762     /* BDAP with something indirect should have a higher cost than
1763        BIAP with register.   FIXME: Should it cost like a MEM or more?  */
1764     /* Don't need to check it, it's the only one left.
1765        FIXME:  There was a REG test missing, perhaps there are others.
1766        Think more.  */
1767     return (2 + 2 + 2) / 2;
1768   }
1769
1770   /* What else?  Return a high cost.  It matters only for valid
1771      addressing modes.  */
1772   return 10;
1773 }
1774
1775 /* Check various objections to the side-effect.  Used in the test-part
1776    of an anonymous insn describing an insn with a possible side-effect.
1777    Returns nonzero if the implied side-effect is ok.
1778
1779    code     : PLUS or MULT
1780    ops      : An array of rtx:es. lreg, rreg, rval,
1781               The variables multop and other_op are indexes into this,
1782               or -1 if they are not applicable.
1783    lreg     : The register that gets assigned in the side-effect.
1784    rreg     : One register in the side-effect expression
1785    rval     : The other register, or an int.
1786    multop   : An integer to multiply rval with.
1787    other_op : One of the entities of the main effect,
1788               whose mode we must consider.  */
1789
1790 int
1791 cris_side_effect_mode_ok (enum rtx_code code, rtx *ops,
1792                           int lreg, int rreg, int rval,
1793                           int multop, int other_op)
1794 {
1795   /* Find what value to multiply with, for rx =ry + rz * n.  */
1796   int mult = multop < 0 ? 1 : INTVAL (ops[multop]);
1797
1798   rtx reg_rtx = ops[rreg];
1799   rtx val_rtx = ops[rval];
1800
1801   /* The operands may be swapped.  Canonicalize them in reg_rtx and
1802      val_rtx, where reg_rtx always is a reg (for this constraint to
1803      match).  */
1804   if (! BASE_P (reg_rtx))
1805     reg_rtx = val_rtx, val_rtx = ops[rreg];
1806
1807   /* Don't forget to check that reg_rtx really is a reg.  If it isn't,
1808      we have no business.  */
1809   if (! BASE_P (reg_rtx))
1810     return 0;
1811
1812   /* Don't do this when -mno-split.  */
1813   if (!TARGET_SIDE_EFFECT_PREFIXES)
1814     return 0;
1815
1816   /* The mult expression may be hidden in lreg.  FIXME:  Add more
1817      commentary about that.  */
1818   if (GET_CODE (val_rtx) == MULT)
1819     {
1820       mult = INTVAL (XEXP (val_rtx, 1));
1821       val_rtx = XEXP (val_rtx, 0);
1822       code = MULT;
1823     }
1824
1825   /* First check the "other operand".  */
1826   if (other_op >= 0)
1827     {
1828       if (GET_MODE_SIZE (GET_MODE (ops[other_op])) > UNITS_PER_WORD)
1829         return 0;
1830
1831       /* Check if the lvalue register is the same as the "other
1832          operand".  If so, the result is undefined and we shouldn't do
1833          this.  FIXME:  Check again.  */
1834       if ((BASE_P (ops[lreg])
1835            && BASE_P (ops[other_op])
1836            && REGNO (ops[lreg]) == REGNO (ops[other_op]))
1837           || rtx_equal_p (ops[other_op], ops[lreg]))
1838       return 0;
1839     }
1840
1841   /* Do not accept frame_pointer_rtx as any operand.  */
1842   if (ops[lreg] == frame_pointer_rtx || ops[rreg] == frame_pointer_rtx
1843       || ops[rval] == frame_pointer_rtx
1844       || (other_op >= 0 && ops[other_op] == frame_pointer_rtx))
1845     return 0;
1846
1847   if (code == PLUS
1848       && ! BASE_P (val_rtx))
1849     {
1850
1851       /* Do not allow rx = rx + n if a normal add or sub with same size
1852          would do.  */
1853       if (rtx_equal_p (ops[lreg], reg_rtx)
1854           && GET_CODE (val_rtx) == CONST_INT
1855           && (INTVAL (val_rtx) <= 63 && INTVAL (val_rtx) >= -63))
1856         return 0;
1857
1858       /* Check allowed cases, like [r(+)?].[bwd] and const.  */
1859       if (CONSTANT_P (val_rtx))
1860         return 1;
1861
1862       if (GET_CODE (val_rtx) == MEM
1863           && BASE_OR_AUTOINCR_P (XEXP (val_rtx, 0)))
1864         return 1;
1865
1866       if (GET_CODE (val_rtx) == SIGN_EXTEND
1867           && GET_CODE (XEXP (val_rtx, 0)) == MEM
1868           && BASE_OR_AUTOINCR_P (XEXP (XEXP (val_rtx, 0), 0)))
1869         return 1;
1870
1871       /* If we got here, it's not a valid addressing mode.  */
1872       return 0;
1873     }
1874   else if (code == MULT
1875            || (code == PLUS && BASE_P (val_rtx)))
1876     {
1877       /* Do not allow rx = rx + ry.S, since it doesn't give better code.  */
1878       if (rtx_equal_p (ops[lreg], reg_rtx)
1879           || (mult == 1 && rtx_equal_p (ops[lreg], val_rtx)))
1880         return 0;
1881
1882       /* Do not allow bad multiply-values.  */
1883       if (mult != 1 && mult != 2 && mult != 4)
1884         return 0;
1885
1886       /* Only allow  r + ...  */
1887       if (! BASE_P (reg_rtx))
1888         return 0;
1889
1890       /* If we got here, all seems ok.
1891          (All checks need to be done above).  */
1892       return 1;
1893     }
1894
1895   /* If we get here, the caller got its initial tests wrong.  */
1896   internal_error ("internal error: cris_side_effect_mode_ok with bad operands");
1897 }
1898
1899 /* The function reg_overlap_mentioned_p in CVS (still as of 2001-05-16)
1900    does not handle the case where the IN operand is strict_low_part; it
1901    does handle it for X.  Test-case in Axis-20010516.  This function takes
1902    care of that for THIS port.  FIXME: strict_low_part is going away
1903    anyway.  */
1904
1905 static int
1906 cris_reg_overlap_mentioned_p (rtx x, rtx in)
1907 {
1908   /* The function reg_overlap_mentioned now handles when X is
1909      strict_low_part, but not when IN is a STRICT_LOW_PART.  */
1910   if (GET_CODE (in) == STRICT_LOW_PART)
1911     in = XEXP (in, 0);
1912
1913   return reg_overlap_mentioned_p (x, in);
1914 }
1915
1916 /* The TARGET_ASM_NAMED_SECTION worker.
1917    We just dispatch to the functions for ELF and a.out.  */
1918
1919 void
1920 cris_target_asm_named_section (const char *name, unsigned int flags,
1921                                tree decl)
1922 {
1923   if (! TARGET_ELF)
1924     default_no_named_section (name, flags, decl);
1925   else
1926     default_elf_asm_named_section (name, flags, decl);
1927 }
1928
1929 /* Return TRUE iff X is a CONST valid for e.g. indexing.  */
1930
1931 bool
1932 cris_valid_pic_const (rtx x)
1933 {
1934   gcc_assert (flag_pic);
1935
1936   switch (GET_CODE (x))
1937     {
1938     case CONST_INT:
1939     case CONST_DOUBLE:
1940       return true;
1941     default:
1942       ;
1943     }
1944
1945   if (GET_CODE (x) != CONST)
1946     return false;
1947
1948   x = XEXP (x, 0);
1949
1950   /* Handle (const (plus (unspec .. UNSPEC_GOTREL) (const_int ...))).  */
1951   if (GET_CODE (x) == PLUS
1952       && GET_CODE (XEXP (x, 0)) == UNSPEC
1953       && XINT (XEXP (x, 0), 1) == CRIS_UNSPEC_GOTREL
1954       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1955     x = XEXP (x, 0);
1956
1957   if (GET_CODE (x) == UNSPEC)
1958     switch (XINT (x, 1))
1959       {
1960       case CRIS_UNSPEC_PLT:
1961       case CRIS_UNSPEC_PLTGOTREAD:
1962       case CRIS_UNSPEC_GOTREAD:
1963       case CRIS_UNSPEC_GOTREL:
1964         return true;
1965       default:
1966         gcc_unreachable ();
1967       }
1968
1969   return cris_pic_symbol_type_of (x) == cris_no_symbol;
1970 }
1971
1972 /* Helper function to find the right PIC-type symbol to generate,
1973    given the original (non-PIC) representation.  */
1974
1975 enum cris_pic_symbol_type
1976 cris_pic_symbol_type_of (rtx x)
1977 {
1978   switch (GET_CODE (x))
1979     {
1980     case SYMBOL_REF:
1981       return SYMBOL_REF_LOCAL_P (x)
1982         ? cris_gotrel_symbol : cris_got_symbol;
1983
1984     case LABEL_REF:
1985       return cris_gotrel_symbol;
1986
1987     case CONST:
1988       return cris_pic_symbol_type_of (XEXP (x, 0));
1989
1990     case PLUS:
1991     case MINUS:
1992       {
1993         enum cris_pic_symbol_type t1 = cris_pic_symbol_type_of (XEXP (x, 0));
1994         enum cris_pic_symbol_type t2 = cris_pic_symbol_type_of (XEXP (x, 1));
1995
1996         gcc_assert (t1 == cris_no_symbol || t2 == cris_no_symbol);
1997
1998         if (t1 == cris_got_symbol || t1 == cris_got_symbol)
1999           return cris_got_symbol_needing_fixup;
2000
2001         return t1 != cris_no_symbol ? t1 : t2;
2002       }
2003
2004     case CONST_INT:
2005     case CONST_DOUBLE:
2006       return cris_no_symbol;
2007
2008     case UNSPEC:
2009       /* Likely an offsettability-test attempting to add a constant to
2010          a GOTREAD symbol, which can't be handled.  */
2011       return cris_invalid_pic_symbol;
2012
2013     default:
2014       fatal_insn ("unrecognized supposed constant", x);
2015     }
2016
2017   gcc_unreachable ();
2018 }
2019
2020 /* The LEGITIMATE_PIC_OPERAND_P worker.  */
2021
2022 int
2023 cris_legitimate_pic_operand (rtx x)
2024 {
2025   /* Symbols are not valid PIC operands as-is; just constants.  */
2026   return cris_valid_pic_const (x);
2027 }
2028
2029 /* TARGET_HANDLE_OPTION worker.  We just store the values into local
2030    variables here.  Checks for correct semantics are in
2031    cris_override_options.  */
2032
2033 static bool
2034 cris_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED,
2035                     int value ATTRIBUTE_UNUSED)
2036 {
2037   switch (code)
2038     {
2039     case OPT_metrax100:
2040       target_flags
2041         |= (MASK_SVINTO
2042             + MASK_ETRAX4_ADD
2043             + MASK_ALIGN_BY_32);
2044       break;
2045
2046     case OPT_mno_etrax100:
2047       target_flags
2048         &= ~(MASK_SVINTO
2049              + MASK_ETRAX4_ADD
2050              + MASK_ALIGN_BY_32);
2051       break;
2052
2053     case OPT_m32_bit:
2054     case OPT_m32bit:
2055       target_flags
2056         |= (MASK_STACK_ALIGN
2057             + MASK_CONST_ALIGN
2058             + MASK_DATA_ALIGN
2059             + MASK_ALIGN_BY_32);
2060       break;
2061
2062     case OPT_m16_bit:
2063     case OPT_m16bit:
2064       target_flags
2065         |= (MASK_STACK_ALIGN
2066             + MASK_CONST_ALIGN
2067             + MASK_DATA_ALIGN);
2068       break;
2069
2070     case OPT_m8_bit:
2071     case OPT_m8bit:
2072       target_flags
2073         &= ~(MASK_STACK_ALIGN
2074              + MASK_CONST_ALIGN
2075              + MASK_DATA_ALIGN);
2076       break;
2077
2078     default:
2079       break;
2080     }
2081
2082   CRIS_SUBTARGET_HANDLE_OPTION(code, arg, value);
2083
2084   return true;
2085 }
2086
2087 /* The OVERRIDE_OPTIONS worker.
2088    As is the norm, this also parses -mfoo=bar type parameters.  */
2089
2090 void
2091 cris_override_options (void)
2092 {
2093   if (cris_max_stackframe_str)
2094     {
2095       cris_max_stackframe = atoi (cris_max_stackframe_str);
2096
2097       /* Do some sanity checking.  */
2098       if (cris_max_stackframe < 0 || cris_max_stackframe > 0x20000000)
2099         internal_error ("-max-stackframe=%d is not usable, not between 0 and %d",
2100                         cris_max_stackframe, 0x20000000);
2101     }
2102
2103   /* Let "-metrax4" and "-metrax100" change the cpu version.  */
2104   if (TARGET_SVINTO && cris_cpu_version < CRIS_CPU_SVINTO)
2105     cris_cpu_version = CRIS_CPU_SVINTO;
2106   else if (TARGET_ETRAX4_ADD && cris_cpu_version < CRIS_CPU_ETRAX4)
2107     cris_cpu_version = CRIS_CPU_ETRAX4;
2108
2109   /* Parse -march=... and its synonym, the deprecated -mcpu=...  */
2110   if (cris_cpu_str)
2111     {
2112       cris_cpu_version
2113         = (*cris_cpu_str == 'v' ? atoi (cris_cpu_str + 1) : -1);
2114
2115       if (strcmp ("etrax4", cris_cpu_str) == 0)
2116         cris_cpu_version = 3;
2117
2118       if (strcmp ("svinto", cris_cpu_str) == 0
2119           || strcmp ("etrax100", cris_cpu_str) == 0)
2120         cris_cpu_version = 8;
2121
2122       if (strcmp ("ng", cris_cpu_str) == 0
2123           || strcmp ("etrax100lx", cris_cpu_str) == 0)
2124         cris_cpu_version = 10;
2125
2126       if (cris_cpu_version < 0 || cris_cpu_version > 10)
2127         error ("unknown CRIS version specification in -march= or -mcpu= : %s",
2128                cris_cpu_str);
2129
2130       /* Set the target flags.  */
2131       if (cris_cpu_version >= CRIS_CPU_ETRAX4)
2132         target_flags |= MASK_ETRAX4_ADD;
2133
2134       /* If this is Svinto or higher, align for 32 bit accesses.  */
2135       if (cris_cpu_version >= CRIS_CPU_SVINTO)
2136         target_flags
2137           |= (MASK_SVINTO | MASK_ALIGN_BY_32
2138               | MASK_STACK_ALIGN | MASK_CONST_ALIGN
2139               | MASK_DATA_ALIGN);
2140
2141       /* Note that we do not add new flags when it can be completely
2142          described with a macro that uses -mcpu=X.  So
2143          TARGET_HAS_MUL_INSNS is (cris_cpu_version >= CRIS_CPU_NG).  */
2144     }
2145
2146   if (cris_tune_str)
2147     {
2148       int cris_tune
2149         = (*cris_tune_str == 'v' ? atoi (cris_tune_str + 1) : -1);
2150
2151       if (strcmp ("etrax4", cris_tune_str) == 0)
2152         cris_tune = 3;
2153
2154       if (strcmp ("svinto", cris_tune_str) == 0
2155           || strcmp ("etrax100", cris_tune_str) == 0)
2156         cris_tune = 8;
2157
2158       if (strcmp ("ng", cris_tune_str) == 0
2159           || strcmp ("etrax100lx", cris_tune_str) == 0)
2160         cris_tune = 10;
2161
2162       if (cris_tune < 0 || cris_tune > 10)
2163         error ("unknown CRIS cpu version specification in -mtune= : %s",
2164                cris_tune_str);
2165
2166       if (cris_tune >= CRIS_CPU_SVINTO)
2167         /* We have currently nothing more to tune than alignment for
2168            memory accesses.  */
2169         target_flags
2170           |= (MASK_STACK_ALIGN | MASK_CONST_ALIGN
2171               | MASK_DATA_ALIGN | MASK_ALIGN_BY_32);
2172     }
2173
2174   if (flag_pic)
2175     {
2176       /* Use error rather than warning, so invalid use is easily
2177          detectable.  Still change to the values we expect, to avoid
2178          further errors.  */
2179       if (! TARGET_LINUX)
2180         {
2181           error ("-fPIC and -fpic are not supported in this configuration");
2182           flag_pic = 0;
2183         }
2184
2185       /* Turn off function CSE.  We need to have the addresses reach the
2186          call expanders to get PLT-marked, as they could otherwise be
2187          compared against zero directly or indirectly.  After visiting the
2188          call expanders they will then be cse:ed, as the call expanders
2189          force_reg the addresses, effectively forcing flag_no_function_cse
2190          to 0.  */
2191       flag_no_function_cse = 1;
2192     }
2193
2194   if (write_symbols == DWARF2_DEBUG && ! TARGET_ELF)
2195     {
2196       warning (0, "that particular -g option is invalid with -maout and -melinux");
2197       write_symbols = DBX_DEBUG;
2198     }
2199
2200   /* Set the per-function-data initializer.  */
2201   init_machine_status = cris_init_machine_status;
2202 }
2203
2204 /* The TARGET_ASM_OUTPUT_MI_THUNK worker.  */
2205
2206 static void
2207 cris_asm_output_mi_thunk (FILE *stream,
2208                           tree thunkdecl ATTRIBUTE_UNUSED,
2209                           HOST_WIDE_INT delta,
2210                           HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2211                           tree funcdecl)
2212 {
2213   if (delta > 0)
2214     fprintf (stream, "\tadd%s " HOST_WIDE_INT_PRINT_DEC ",$%s\n",
2215              ADDITIVE_SIZE_MODIFIER (delta), delta,
2216              reg_names[CRIS_FIRST_ARG_REG]);
2217   else if (delta < 0)
2218     fprintf (stream, "\tsub%s " HOST_WIDE_INT_PRINT_DEC ",$%s\n",
2219              ADDITIVE_SIZE_MODIFIER (-delta), -delta,
2220              reg_names[CRIS_FIRST_ARG_REG]);
2221
2222   if (flag_pic)
2223     {
2224       const char *name = XSTR (XEXP (DECL_RTL (funcdecl), 0), 0);
2225
2226       name = (* targetm.strip_name_encoding) (name);
2227       fprintf (stream, "add.d ");
2228       assemble_name (stream, name);
2229       fprintf (stream, "%s,$pc\n", CRIS_PLT_PCOFFSET_SUFFIX);
2230     }
2231   else
2232     {
2233       fprintf (stream, "jump ");
2234       assemble_name (stream, XSTR (XEXP (DECL_RTL (funcdecl), 0), 0));
2235       fprintf (stream, "\n");
2236     }
2237 }
2238
2239 /* Boilerplate emitted at start of file.
2240
2241    NO_APP *only at file start* means faster assembly.  It also means
2242    comments are not allowed.  In some cases comments will be output
2243    for debugging purposes.  Make sure they are allowed then.
2244
2245    We want a .file directive only if TARGET_ELF.  */
2246 static void
2247 cris_file_start (void)
2248 {
2249   /* These expressions can vary at run time, so we cannot put
2250      them into TARGET_INITIALIZER.  */
2251   targetm.file_start_app_off = !(TARGET_PDEBUG || flag_print_asm_name);
2252   targetm.file_start_file_directive = TARGET_ELF;
2253
2254   default_file_start ();
2255 }
2256
2257 /* Rename the function calls for integer multiply and divide.  */
2258 static void
2259 cris_init_libfuncs (void)
2260 {
2261   set_optab_libfunc (smul_optab, SImode, "__Mul");
2262   set_optab_libfunc (sdiv_optab, SImode, "__Div");
2263   set_optab_libfunc (udiv_optab, SImode, "__Udiv");
2264   set_optab_libfunc (smod_optab, SImode, "__Mod");
2265   set_optab_libfunc (umod_optab, SImode, "__Umod");
2266 }
2267
2268 /* The INIT_EXPANDERS worker sets the per-function-data initializer and
2269    mark functions.  */
2270
2271 void
2272 cris_init_expanders (void)
2273 {
2274   /* Nothing here at the moment.  */
2275 }
2276
2277 /* Zero initialization is OK for all current fields.  */
2278
2279 static struct machine_function *
2280 cris_init_machine_status (void)
2281 {
2282   return ggc_alloc_cleared (sizeof (struct machine_function));
2283 }
2284
2285 /* Split a 2 word move (DI or presumably DF) into component parts.
2286    Originally a copy of gen_split_move_double in m32r.c.  */
2287
2288 rtx
2289 cris_split_movdx (rtx *operands)
2290 {
2291   enum machine_mode mode = GET_MODE (operands[0]);
2292   rtx dest = operands[0];
2293   rtx src  = operands[1];
2294   rtx val;
2295
2296   /* We used to have to handle (SUBREG (MEM)) here, but that should no
2297      longer happen; after reload there are no SUBREGs any more, and we're
2298      only called after reload.  */
2299   CRIS_ASSERT (GET_CODE (dest) != SUBREG && GET_CODE (src) != SUBREG);
2300
2301   start_sequence ();
2302   if (GET_CODE (dest) == REG)
2303     {
2304       int dregno = REGNO (dest);
2305
2306       /* Reg-to-reg copy.  */
2307       if (GET_CODE (src) == REG)
2308         {
2309           int sregno = REGNO (src);
2310
2311           int reverse = (dregno == sregno + 1);
2312
2313           /* We normally copy the low-numbered register first.  However, if
2314              the first register operand 0 is the same as the second register of
2315              operand 1, we must copy in the opposite order.  */
2316           emit_insn (gen_rtx_SET (VOIDmode,
2317                                   operand_subword (dest, reverse, TRUE, mode),
2318                                   operand_subword (src, reverse, TRUE, mode)));
2319
2320           emit_insn (gen_rtx_SET (VOIDmode,
2321                                   operand_subword (dest, !reverse, TRUE, mode),
2322                                   operand_subword (src, !reverse, TRUE, mode)));
2323         }
2324       /* Constant-to-reg copy.  */
2325       else if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
2326         {
2327           rtx words[2];
2328           split_double (src, &words[0], &words[1]);
2329           emit_insn (gen_rtx_SET (VOIDmode,
2330                                   operand_subword (dest, 0, TRUE, mode),
2331                                   words[0]));
2332
2333           emit_insn (gen_rtx_SET (VOIDmode,
2334                                   operand_subword (dest, 1, TRUE, mode),
2335                                   words[1]));
2336         }
2337       /* Mem-to-reg copy.  */
2338       else if (GET_CODE (src) == MEM)
2339         {
2340           /* If the high-address word is used in the address, we must load it
2341              last.  Otherwise, load it first.  */
2342           rtx addr = XEXP (src, 0);
2343           int reverse
2344             = (refers_to_regno_p (dregno, dregno + 1, addr, NULL) != 0);
2345
2346           /* The original code implies that we can't do
2347              move.x [rN+],rM  move.x [rN],rM+1
2348              when rN is dead, because of REG_NOTES damage.  That is
2349              consistent with what I've seen, so don't try it.
2350
2351              We have two different cases here; if the addr is POST_INC,
2352              just pass it through, otherwise add constants.  */
2353
2354           if (GET_CODE (addr) == POST_INC)
2355             {
2356               rtx mem;
2357               rtx insn;
2358
2359               /* Whenever we emit insns with post-incremented
2360                  addresses ourselves, we must add a post-inc note
2361                  manually.  */
2362               mem = change_address (src, SImode, addr);
2363               insn
2364                 = gen_rtx_SET (VOIDmode,
2365                                operand_subword (dest, 0, TRUE, mode), mem);
2366               insn = emit_insn (insn);
2367               if (GET_CODE (XEXP (mem, 0)) == POST_INC)
2368                 REG_NOTES (insn)
2369                   = alloc_EXPR_LIST (REG_INC, XEXP (XEXP (mem, 0), 0),
2370                                      REG_NOTES (insn));
2371
2372               mem = change_address (src, SImode, addr);
2373               insn
2374                 = gen_rtx_SET (VOIDmode,
2375                                operand_subword (dest, 1, TRUE, mode), mem);
2376               insn = emit_insn (insn);
2377               if (GET_CODE (XEXP (mem, 0)) == POST_INC)
2378                 REG_NOTES (insn)
2379                   = alloc_EXPR_LIST (REG_INC, XEXP (XEXP (mem, 0), 0),
2380                                      REG_NOTES (insn));
2381             }
2382           else
2383             {
2384               /* Make sure we don't get any other addresses with
2385                  embedded postincrements.  They should be stopped in
2386                  GO_IF_LEGITIMATE_ADDRESS, but we're here for your
2387                  safety.  */
2388               if (side_effects_p (addr))
2389                 fatal_insn ("unexpected side-effects in address", addr);
2390
2391               emit_insn (gen_rtx_SET
2392                          (VOIDmode,
2393                           operand_subword (dest, reverse, TRUE, mode),
2394                           change_address
2395                           (src, SImode,
2396                            plus_constant (addr,
2397                                           reverse * UNITS_PER_WORD))));
2398               emit_insn (gen_rtx_SET
2399                          (VOIDmode,
2400                           operand_subword (dest, ! reverse, TRUE, mode),
2401                           change_address
2402                           (src, SImode,
2403                            plus_constant (addr,
2404                                           (! reverse) *
2405                                           UNITS_PER_WORD))));
2406             }
2407         }
2408       else
2409         internal_error ("Unknown src");
2410     }
2411   /* Reg-to-mem copy or clear mem.  */
2412   else if (GET_CODE (dest) == MEM
2413            && (GET_CODE (src) == REG
2414                || src == const0_rtx
2415                || src == CONST0_RTX (DFmode)))
2416     {
2417       rtx addr = XEXP (dest, 0);
2418
2419       if (GET_CODE (addr) == POST_INC)
2420         {
2421           rtx mem;
2422           rtx insn;
2423           
2424           /* Whenever we emit insns with post-incremented addresses
2425              ourselves, we must add a post-inc note manually.  */
2426           mem = change_address (dest, SImode, addr);
2427           insn
2428             = gen_rtx_SET (VOIDmode,
2429                            mem, operand_subword (src, 0, TRUE, mode));
2430           insn = emit_insn (insn);
2431           if (GET_CODE (XEXP (mem, 0)) == POST_INC)
2432             REG_NOTES (insn)
2433               = alloc_EXPR_LIST (REG_INC, XEXP (XEXP (mem, 0), 0),
2434                                  REG_NOTES (insn));
2435
2436           mem = change_address (dest, SImode, addr);
2437           insn
2438             = gen_rtx_SET (VOIDmode,
2439                            mem,
2440                            operand_subword (src, 1, TRUE, mode));
2441           insn = emit_insn (insn);
2442           if (GET_CODE (XEXP (mem, 0)) == POST_INC)
2443             REG_NOTES (insn)
2444               = alloc_EXPR_LIST (REG_INC, XEXP (XEXP (mem, 0), 0),
2445                                  REG_NOTES (insn));
2446         }
2447       else
2448         {
2449           /* Make sure we don't get any other addresses with embedded
2450              postincrements.  They should be stopped in
2451              GO_IF_LEGITIMATE_ADDRESS, but we're here for your safety.  */
2452           if (side_effects_p (addr))
2453             fatal_insn ("unexpected side-effects in address", addr);
2454
2455           emit_insn (gen_rtx_SET
2456                      (VOIDmode,
2457                       change_address (dest, SImode, addr),
2458                       operand_subword (src, 0, TRUE, mode)));
2459
2460           emit_insn (gen_rtx_SET
2461                      (VOIDmode,
2462                       change_address (dest, SImode,
2463                                       plus_constant (addr,
2464                                                      UNITS_PER_WORD)),
2465                       operand_subword (src, 1, TRUE, mode)));
2466         }
2467     }
2468
2469   else
2470     internal_error ("Unknown dest");
2471
2472   val = get_insns ();
2473   end_sequence ();
2474   return val;
2475 }
2476
2477 /* The expander for the prologue pattern name.  */
2478
2479 void
2480 cris_expand_prologue (void)
2481 {
2482   int regno;
2483   int size = get_frame_size ();
2484   /* Shorten the used name for readability.  */
2485   int cfoa_size = current_function_outgoing_args_size;
2486   int last_movem_reg = -1;
2487   int framesize = 0;
2488   rtx mem, insn;
2489   int return_address_on_stack = cris_return_address_on_stack ();
2490   int got_really_used = false;
2491   int n_movem_regs = 0;
2492   int pretend = current_function_pretend_args_size;
2493
2494   /* Don't do anything if no prologues or epilogues are wanted.  */
2495   if (!TARGET_PROLOGUE_EPILOGUE)
2496     return;
2497
2498   CRIS_ASSERT (size >= 0);
2499
2500   if (current_function_uses_pic_offset_table)
2501     {
2502       /* A reference may have been optimized out (like the abort () in
2503          fde_split in unwind-dw2-fde.c, at least 3.2.1) so check that
2504          it's still used.  */
2505       push_topmost_sequence ();
2506       got_really_used
2507         = reg_used_between_p (pic_offset_table_rtx, get_insns (), NULL_RTX);
2508       pop_topmost_sequence ();
2509     }
2510
2511   /* Align the size to what's best for the CPU model.  */
2512   if (TARGET_STACK_ALIGN)
2513     size = TARGET_ALIGN_BY_32 ? (size + 3) & ~3 : (size + 1) & ~1;
2514
2515   if (pretend)
2516     {
2517       /* See also cris_setup_incoming_varargs where
2518          cfun->machine->stdarg_regs is set.  There are other setters of
2519          current_function_pretend_args_size than stdarg handling, like
2520          for an argument passed with parts in R13 and stack.  We must
2521          not store R13 into the pretend-area for that case, as GCC does
2522          that itself.  "Our" store would be marked as redundant and GCC
2523          will attempt to remove it, which will then be flagged as an
2524          internal error; trying to remove a frame-related insn.  */
2525       int stdarg_regs = cfun->machine->stdarg_regs;
2526
2527       framesize += pretend;
2528
2529       for (regno = CRIS_FIRST_ARG_REG + CRIS_MAX_ARGS_IN_REGS - 1;
2530            stdarg_regs > 0;
2531            regno--, pretend -= 4, stdarg_regs--)
2532         {
2533           insn = emit_insn (gen_rtx_SET (VOIDmode,
2534                                          stack_pointer_rtx,
2535                                          plus_constant (stack_pointer_rtx,
2536                                                         -4)));
2537           /* FIXME: When dwarf2 frame output and unless asynchronous
2538              exceptions, make dwarf2 bundle together all stack
2539              adjustments like it does for registers between stack
2540              adjustments.  */
2541           RTX_FRAME_RELATED_P (insn) = 1;
2542
2543           mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2544           set_mem_alias_set (mem, get_varargs_alias_set ());
2545           insn = emit_move_insn (mem, gen_rtx_raw_REG (SImode, regno));
2546
2547           /* Note the absence of RTX_FRAME_RELATED_P on the above insn:
2548              the value isn't restored, so we don't want to tell dwarf2
2549              that it's been stored to stack, else EH handling info would
2550              get confused.  */
2551         }
2552
2553       /* For other setters of current_function_pretend_args_size, we
2554          just adjust the stack by leaving the remaining size in
2555          "pretend", handled below.  */
2556     }
2557
2558   /* Save SRP if not a leaf function.  */
2559   if (return_address_on_stack)
2560     {
2561       insn = emit_insn (gen_rtx_SET (VOIDmode,
2562                                      stack_pointer_rtx,
2563                                      plus_constant (stack_pointer_rtx,
2564                                                     -4 - pretend)));
2565       pretend = 0;
2566       RTX_FRAME_RELATED_P (insn) = 1;
2567
2568       mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2569       set_mem_alias_set (mem, get_frame_alias_set ());
2570       insn = emit_move_insn (mem, gen_rtx_raw_REG (SImode, CRIS_SRP_REGNUM));
2571       RTX_FRAME_RELATED_P (insn) = 1;
2572       framesize += 4;
2573     }
2574
2575   /* Set up the frame pointer, if needed.  */
2576   if (frame_pointer_needed)
2577     {
2578       insn = emit_insn (gen_rtx_SET (VOIDmode,
2579                                      stack_pointer_rtx,
2580                                      plus_constant (stack_pointer_rtx,
2581                                                     -4 - pretend)));
2582       pretend = 0;
2583       RTX_FRAME_RELATED_P (insn) = 1;
2584
2585       mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2586       set_mem_alias_set (mem, get_frame_alias_set ());
2587       insn = emit_move_insn (mem, frame_pointer_rtx);
2588       RTX_FRAME_RELATED_P (insn) = 1;
2589
2590       insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
2591       RTX_FRAME_RELATED_P (insn) = 1;
2592
2593       framesize += 4;
2594     }
2595
2596   /* Between frame-pointer and saved registers lie the area for local
2597      variables.  If we get here with "pretended" size remaining, count
2598      it into the general stack size.  */
2599   size += pretend;
2600
2601   /* Get a contiguous sequence of registers, starting with R0, that need
2602      to be saved.  */
2603   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2604     {
2605       if (cris_reg_saved_in_regsave_area (regno, got_really_used))
2606         {
2607           n_movem_regs++;
2608
2609           /* Check if movem may be used for registers so far.  */
2610           if (regno == last_movem_reg + 1)
2611             /* Yes, update next expected register.  */
2612             last_movem_reg = regno;
2613           else
2614             {
2615               /* We cannot use movem for all registers.  We have to flush
2616                  any movem:ed registers we got so far.  */
2617               if (last_movem_reg != -1)
2618                 {
2619                   int n_saved
2620                     = (n_movem_regs == 1) ? 1 : last_movem_reg + 1;
2621
2622                   /* It is a win to use a side-effect assignment for
2623                      64 <= size <= 128.  But side-effect on movem was
2624                      not usable for CRIS v0..3.  Also only do it if
2625                      side-effects insns are allowed.  */
2626                   if ((last_movem_reg + 1) * 4 + size >= 64
2627                       && (last_movem_reg + 1) * 4 + size <= 128
2628                       && (cris_cpu_version >= CRIS_CPU_SVINTO || n_saved == 1)
2629                       && TARGET_SIDE_EFFECT_PREFIXES)
2630                     {
2631                       mem
2632                         = gen_rtx_MEM (SImode,
2633                                        plus_constant (stack_pointer_rtx,
2634                                                       -(n_saved * 4 + size)));
2635                       set_mem_alias_set (mem, get_frame_alias_set ());
2636                       insn
2637                         = cris_emit_movem_store (mem, GEN_INT (n_saved),
2638                                                  -(n_saved * 4 + size),
2639                                                  true);
2640                     }
2641                   else
2642                     {
2643                       insn
2644                         = gen_rtx_SET (VOIDmode,
2645                                        stack_pointer_rtx,
2646                                        plus_constant (stack_pointer_rtx,
2647                                                       -(n_saved * 4 + size)));
2648                       insn = emit_insn (insn);
2649                       RTX_FRAME_RELATED_P (insn) = 1;
2650
2651                       mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2652                       set_mem_alias_set (mem, get_frame_alias_set ());
2653                       insn = cris_emit_movem_store (mem, GEN_INT (n_saved),
2654                                                     0, true);
2655                     }
2656
2657                   framesize += n_saved * 4 + size;
2658                   last_movem_reg = -1;
2659                   size = 0;
2660                 }
2661
2662               insn = emit_insn (gen_rtx_SET (VOIDmode,
2663                                              stack_pointer_rtx,
2664                                              plus_constant (stack_pointer_rtx,
2665                                                             -4 - size)));
2666               RTX_FRAME_RELATED_P (insn) = 1;
2667
2668               mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2669               set_mem_alias_set (mem, get_frame_alias_set ());
2670               insn = emit_move_insn (mem, gen_rtx_raw_REG (SImode, regno));
2671               RTX_FRAME_RELATED_P (insn) = 1;
2672
2673               framesize += 4 + size;
2674               size = 0;
2675             }
2676         }
2677     }
2678
2679   /* Check after, if we could movem all registers.  This is the normal case.  */
2680   if (last_movem_reg != -1)
2681     {
2682       int n_saved
2683         = (n_movem_regs == 1) ? 1 : last_movem_reg + 1;
2684
2685       /* Side-effect on movem was not usable for CRIS v0..3.  Also only
2686          do it if side-effects insns are allowed.  */
2687       if ((last_movem_reg + 1) * 4 + size >= 64
2688           && (last_movem_reg + 1) * 4 + size <= 128
2689           && (cris_cpu_version >= CRIS_CPU_SVINTO || n_saved == 1)
2690           && TARGET_SIDE_EFFECT_PREFIXES)
2691         {
2692           mem
2693             = gen_rtx_MEM (SImode,
2694                            plus_constant (stack_pointer_rtx,
2695                                           -(n_saved * 4 + size)));
2696           set_mem_alias_set (mem, get_frame_alias_set ());
2697           insn = cris_emit_movem_store (mem, GEN_INT (n_saved),
2698                                         -(n_saved * 4 + size), true);
2699         }
2700       else
2701         {
2702           insn
2703             = gen_rtx_SET (VOIDmode,
2704                            stack_pointer_rtx,
2705                            plus_constant (stack_pointer_rtx,
2706                                           -(n_saved * 4 + size)));
2707           insn = emit_insn (insn);
2708           RTX_FRAME_RELATED_P (insn) = 1;
2709
2710           mem = gen_rtx_MEM (SImode, stack_pointer_rtx);
2711           set_mem_alias_set (mem, get_frame_alias_set ());
2712           insn = cris_emit_movem_store (mem, GEN_INT (n_saved), 0, true);
2713         }
2714
2715       framesize += n_saved * 4 + size;
2716       /* We have to put outgoing argument space after regs.  */
2717       if (cfoa_size)
2718         {
2719           insn = emit_insn (gen_rtx_SET (VOIDmode,
2720                                          stack_pointer_rtx,
2721                                          plus_constant (stack_pointer_rtx,
2722                                                         -cfoa_size)));
2723           RTX_FRAME_RELATED_P (insn) = 1;
2724           framesize += cfoa_size;
2725         }
2726     }
2727   else if ((size + cfoa_size) > 0)
2728     {
2729       insn = emit_insn (gen_rtx_SET (VOIDmode,
2730                                      stack_pointer_rtx,
2731                                      plus_constant (stack_pointer_rtx,
2732                                                     -(cfoa_size + size))));
2733       RTX_FRAME_RELATED_P (insn) = 1;
2734       framesize += size + cfoa_size;
2735     }
2736
2737   /* Set up the PIC register, if it is used.  */
2738   if (got_really_used)
2739     {
2740       rtx got
2741         = gen_rtx_UNSPEC (SImode, gen_rtvec (1, const0_rtx), CRIS_UNSPEC_GOT);
2742       emit_move_insn (pic_offset_table_rtx, got);
2743
2744       /* FIXME: This is a cover-up for flow2 messing up; it doesn't
2745          follow exceptional paths and tries to delete the GOT load as
2746          unused, if it isn't used on the non-exceptional paths.  Other
2747          ports have similar or other cover-ups, or plain bugs marking
2748          the GOT register load as maybe-dead.  To see this, remove the
2749          line below and try libsupc++/vec.cc or a trivial
2750          "static void y (); void x () {try {y ();} catch (...) {}}".  */
2751       emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2752     }
2753
2754   if (cris_max_stackframe && framesize > cris_max_stackframe)
2755     warning (0, "stackframe too big: %d bytes", framesize);
2756 }
2757
2758 /* The expander for the epilogue pattern.  */
2759
2760 void
2761 cris_expand_epilogue (void)
2762 {
2763   int regno;
2764   int size = get_frame_size ();
2765   int last_movem_reg = -1;
2766   int argspace_offset = current_function_outgoing_args_size;
2767   int pretend =  current_function_pretend_args_size;
2768   rtx mem;
2769   bool return_address_on_stack = cris_return_address_on_stack ();
2770   /* A reference may have been optimized out
2771      (like the abort () in fde_split in unwind-dw2-fde.c, at least 3.2.1)
2772      so check that it's still used.  */
2773   int got_really_used = false;
2774   int n_movem_regs = 0;
2775
2776   if (!TARGET_PROLOGUE_EPILOGUE)
2777     return;
2778
2779   if (current_function_uses_pic_offset_table)
2780     {
2781       /* A reference may have been optimized out (like the abort () in
2782          fde_split in unwind-dw2-fde.c, at least 3.2.1) so check that
2783          it's still used.  */
2784       push_topmost_sequence ();
2785       got_really_used
2786         = reg_used_between_p (pic_offset_table_rtx, get_insns (), NULL_RTX);
2787       pop_topmost_sequence ();
2788     }
2789
2790   /* Align byte count of stack frame.  */
2791   if (TARGET_STACK_ALIGN)
2792     size = TARGET_ALIGN_BY_32 ? (size + 3) & ~3 : (size + 1) & ~1;
2793
2794   /* Check how many saved regs we can movem.  They start at r0 and must
2795      be contiguous.  */
2796   for (regno = 0;
2797        regno < FIRST_PSEUDO_REGISTER;
2798        regno++)
2799     if (cris_reg_saved_in_regsave_area (regno, got_really_used))
2800       {
2801         n_movem_regs++;
2802
2803         if (regno == last_movem_reg + 1)
2804           last_movem_reg = regno;
2805         else
2806           break;
2807       }
2808
2809   /* If there was only one register that really needed to be saved
2810      through movem, don't use movem.  */
2811   if (n_movem_regs == 1)
2812     last_movem_reg = -1;
2813
2814   /* Now emit "normal" move insns for all regs higher than the movem
2815      regs.  */
2816   for (regno = FIRST_PSEUDO_REGISTER - 1;
2817        regno > last_movem_reg;
2818        regno--)
2819     if (cris_reg_saved_in_regsave_area (regno, got_really_used))
2820       {
2821         rtx insn;
2822
2823         if (argspace_offset)
2824           {
2825             /* There is an area for outgoing parameters located before
2826                the saved registers.  We have to adjust for that.  */
2827             emit_insn (gen_rtx_SET (VOIDmode,
2828                                     stack_pointer_rtx,
2829                                     plus_constant (stack_pointer_rtx,
2830                                                    argspace_offset)));
2831             /* Make sure we only do this once.  */
2832             argspace_offset = 0;
2833           }
2834
2835         mem = gen_rtx_MEM (SImode, gen_rtx_POST_INC (SImode,
2836                                                      stack_pointer_rtx));
2837         set_mem_alias_set (mem, get_frame_alias_set ());
2838         insn = emit_move_insn (gen_rtx_raw_REG (SImode, regno), mem);
2839
2840         /* Whenever we emit insns with post-incremented addresses
2841            ourselves, we must add a post-inc note manually.  */
2842         REG_NOTES (insn)
2843           = alloc_EXPR_LIST (REG_INC, stack_pointer_rtx, REG_NOTES (insn));
2844       }
2845
2846   /* If we have any movem-restore, do it now.  */
2847   if (last_movem_reg != -1)
2848     {
2849       rtx insn;
2850
2851       if (argspace_offset)
2852         {
2853           emit_insn (gen_rtx_SET (VOIDmode,
2854                                   stack_pointer_rtx,
2855                                   plus_constant (stack_pointer_rtx,
2856                                                  argspace_offset)));
2857           argspace_offset = 0;
2858         }
2859
2860       mem = gen_rtx_MEM (SImode,
2861                          gen_rtx_POST_INC (SImode, stack_pointer_rtx));
2862       set_mem_alias_set (mem, get_frame_alias_set ());
2863       insn
2864         = emit_insn (cris_gen_movem_load (mem,
2865                                           GEN_INT (last_movem_reg + 1), 0));
2866       /* Whenever we emit insns with post-incremented addresses
2867          ourselves, we must add a post-inc note manually.  */
2868       if (side_effects_p (PATTERN (insn)))
2869         REG_NOTES (insn)
2870           = alloc_EXPR_LIST (REG_INC, stack_pointer_rtx, REG_NOTES (insn));
2871     }
2872
2873   /* If we don't clobber all of the allocated stack area (we've already
2874      deallocated saved registers), GCC might want to schedule loads from
2875      the stack to *after* the stack-pointer restore, which introduces an
2876      interrupt race condition.  This happened for the initial-value
2877      SRP-restore for g++.dg/eh/registers1.C (noticed by inspection of
2878      other failure for that test).  It also happened for the stack slot
2879      for the return value in (one version of)
2880      linux/fs/dcache.c:__d_lookup, at least with "-O2
2881      -fno-omit-frame-pointer".  */
2882
2883   /* Restore frame pointer if necessary.  */
2884   if (frame_pointer_needed)
2885     {
2886       rtx insn;
2887
2888       emit_insn (gen_cris_frame_deallocated_barrier ());
2889
2890       emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
2891       mem = gen_rtx_MEM (SImode, gen_rtx_POST_INC (SImode,
2892                                                    stack_pointer_rtx));
2893       set_mem_alias_set (mem, get_frame_alias_set ());
2894       insn = emit_move_insn (frame_pointer_rtx, mem);
2895
2896       /* Whenever we emit insns with post-incremented addresses
2897          ourselves, we must add a post-inc note manually.  */
2898       REG_NOTES (insn)
2899         = alloc_EXPR_LIST (REG_INC, stack_pointer_rtx, REG_NOTES (insn));
2900     }
2901   else if ((size + argspace_offset) != 0)
2902     {
2903       emit_insn (gen_cris_frame_deallocated_barrier ());
2904
2905       /* If there was no frame-pointer to restore sp from, we must
2906          explicitly deallocate local variables.  */
2907
2908       /* Handle space for outgoing parameters that hasn't been handled
2909          yet.  */
2910       size += argspace_offset;
2911
2912       emit_insn (gen_rtx_SET (VOIDmode,
2913                               stack_pointer_rtx,
2914                               plus_constant (stack_pointer_rtx, size)));
2915     }
2916
2917   /* If this function has no pushed register parameters
2918      (stdargs/varargs), and if it is not a leaf function, then we have
2919      the return address on the stack.  */
2920   if (return_address_on_stack && pretend == 0)
2921     {
2922       if (current_function_calls_eh_return)
2923         {
2924           rtx mem;
2925           rtx insn;
2926           rtx srpreg = gen_rtx_raw_REG (SImode, CRIS_SRP_REGNUM);
2927           mem = gen_rtx_MEM (SImode,
2928                              gen_rtx_POST_INC (SImode,
2929                                                stack_pointer_rtx));
2930           set_mem_alias_set (mem, get_frame_alias_set ());
2931           insn = emit_move_insn (srpreg, mem);
2932
2933           /* Whenever we emit insns with post-incremented addresses
2934              ourselves, we must add a post-inc note manually.  */
2935           REG_NOTES (insn)
2936             = alloc_EXPR_LIST (REG_INC, stack_pointer_rtx, REG_NOTES (insn));
2937
2938           emit_insn (gen_addsi3 (stack_pointer_rtx,
2939                                  stack_pointer_rtx,
2940                                  gen_rtx_raw_REG (SImode,
2941                                                   CRIS_STACKADJ_REG)));
2942           cris_expand_return (false);
2943         }
2944       else
2945         cris_expand_return (true);
2946
2947       return;
2948     }
2949
2950   /* If we pushed some register parameters, then adjust the stack for
2951      them.  */
2952   if (pretend != 0)
2953     {
2954       /* If SRP is stored on the way, we need to restore it first.  */
2955       if (return_address_on_stack)
2956         {
2957           rtx mem;
2958           rtx srpreg = gen_rtx_raw_REG (SImode, CRIS_SRP_REGNUM);
2959           rtx insn;
2960
2961           mem = gen_rtx_MEM (SImode,
2962                              gen_rtx_POST_INC (SImode,
2963                                                stack_pointer_rtx));
2964           set_mem_alias_set (mem, get_frame_alias_set ());
2965           insn = emit_move_insn (srpreg, mem);
2966
2967           /* Whenever we emit insns with post-incremented addresses
2968              ourselves, we must add a post-inc note manually.  */
2969           REG_NOTES (insn)
2970             = alloc_EXPR_LIST (REG_INC, stack_pointer_rtx, REG_NOTES (insn));
2971         }
2972
2973       emit_insn (gen_rtx_SET (VOIDmode,
2974                               stack_pointer_rtx,
2975                               plus_constant (stack_pointer_rtx, pretend)));
2976     }
2977
2978   /* Perform the "physical" unwinding that the EH machinery calculated.  */
2979   if (current_function_calls_eh_return)
2980     emit_insn (gen_addsi3 (stack_pointer_rtx,
2981                            stack_pointer_rtx,
2982                            gen_rtx_raw_REG (SImode,
2983                                             CRIS_STACKADJ_REG)));
2984   cris_expand_return (false);
2985 }
2986
2987 /* Worker function for generating movem from mem for load_multiple.  */
2988
2989 rtx
2990 cris_gen_movem_load (rtx src, rtx nregs_rtx, int nprefix)
2991 {
2992   int nregs = INTVAL (nregs_rtx);
2993   rtvec vec;
2994   int eltno = 1;
2995   int i;
2996   rtx srcreg = XEXP (src, 0);
2997   unsigned int regno = nregs - 1;
2998   int regno_inc = -1;
2999
3000   if (GET_CODE (srcreg) == POST_INC)
3001     srcreg = XEXP (srcreg, 0);
3002
3003   CRIS_ASSERT (REG_P (srcreg));
3004
3005   /* Don't use movem for just one insn.  The insns are equivalent except
3006      for the pipeline hazard (on v32); movem does not forward the loaded
3007      registers so there's a three cycles penalty for their use.  */
3008   if (nregs == 1)
3009     return gen_movsi (gen_rtx_REG (SImode, 0), src);
3010
3011   vec = rtvec_alloc (nprefix + nregs
3012                      + (GET_CODE (XEXP (src, 0)) == POST_INC));
3013
3014   if (GET_CODE (XEXP (src, 0)) == POST_INC)
3015     {
3016       RTVEC_ELT (vec, nprefix + 1)
3017         = gen_rtx_SET (VOIDmode, srcreg, plus_constant (srcreg, nregs * 4));
3018       eltno++;
3019     }
3020
3021   src = replace_equiv_address (src, srcreg);
3022   RTVEC_ELT (vec, nprefix)
3023     = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regno), src);
3024   regno += regno_inc;
3025
3026   for (i = 1; i < nregs; i++, eltno++)
3027     {
3028       RTVEC_ELT (vec, nprefix + eltno)
3029         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, regno),
3030                        adjust_address_nv (src, SImode, i * 4));
3031       regno += regno_inc;
3032     }
3033
3034   return gen_rtx_PARALLEL (VOIDmode, vec);
3035 }
3036
3037 /* Worker function for generating movem to mem.  If FRAME_RELATED, notes
3038    are added that the dwarf2 machinery understands.  */
3039
3040 rtx
3041 cris_emit_movem_store (rtx dest, rtx nregs_rtx, int increment,
3042                        bool frame_related)
3043 {
3044   int nregs = INTVAL (nregs_rtx);
3045   rtvec vec;
3046   int eltno = 1;
3047   int i;
3048   rtx insn;
3049   rtx destreg = XEXP (dest, 0);
3050   unsigned int regno = nregs - 1;
3051   int regno_inc = -1;
3052
3053   if (GET_CODE (destreg) == POST_INC)
3054     increment += nregs * 4;
3055
3056   if (GET_CODE (destreg) == POST_INC || GET_CODE (destreg) == PLUS)
3057     destreg = XEXP (destreg, 0);
3058
3059   CRIS_ASSERT (REG_P (destreg));
3060
3061   /* Don't use movem for just one insn.  The insns are equivalent except
3062      for the pipeline hazard (on v32); movem does not forward the loaded
3063      registers so there's a three cycles penalty for use.  */
3064   if (nregs == 1)
3065     {
3066       rtx mov = gen_rtx_SET (VOIDmode, dest, gen_rtx_REG (SImode, 0));
3067
3068       if (increment == 0)
3069         {
3070           insn = emit_insn (mov);
3071           if (frame_related)
3072             RTX_FRAME_RELATED_P (insn) = 1;
3073           return insn;
3074         }
3075
3076       /* If there was a request for a side-effect, create the ordinary
3077          parallel.  */
3078       vec = rtvec_alloc (2);
3079
3080       RTVEC_ELT (vec, 0) = mov;
3081       RTVEC_ELT (vec, 1) = gen_rtx_SET (VOIDmode, destreg,
3082                                         plus_constant (destreg, increment));
3083       if (frame_related)
3084         {
3085           RTX_FRAME_RELATED_P (mov) = 1;
3086           RTX_FRAME_RELATED_P (RTVEC_ELT (vec, 1)) = 1;
3087         }
3088     }
3089   else
3090     {
3091       vec = rtvec_alloc (nregs + (increment != 0 ? 1 : 0));
3092       RTVEC_ELT (vec, 0)
3093         = gen_rtx_SET (VOIDmode,
3094                        replace_equiv_address (dest,
3095                                               plus_constant (destreg,
3096                                                              increment)),
3097                        gen_rtx_REG (SImode, regno));
3098       regno += regno_inc;
3099
3100       /* The dwarf2 info wants this mark on each component in a parallel
3101          that's part of the prologue (though it's optional on the first
3102          component).  */
3103       if (frame_related)
3104         RTX_FRAME_RELATED_P (RTVEC_ELT (vec, 0)) = 1;
3105
3106       if (increment != 0)
3107         {
3108           RTVEC_ELT (vec, 1)
3109             = gen_rtx_SET (VOIDmode, destreg,
3110                            plus_constant (destreg,
3111                                           increment != 0
3112                                           ? increment : nregs * 4));
3113           eltno++;
3114
3115           if (frame_related)
3116             RTX_FRAME_RELATED_P (RTVEC_ELT (vec, 1)) = 1;
3117
3118           /* Don't call adjust_address_nv on a post-incremented address if
3119              we can help it.  */
3120           if (GET_CODE (XEXP (dest, 0)) == POST_INC)
3121             dest = replace_equiv_address (dest, destreg);
3122         }
3123
3124       for (i = 1; i < nregs; i++, eltno++)
3125         {
3126           RTVEC_ELT (vec, eltno)
3127             = gen_rtx_SET (VOIDmode, adjust_address_nv (dest, SImode, i * 4),
3128                            gen_rtx_REG (SImode, regno));
3129           if (frame_related)
3130             RTX_FRAME_RELATED_P (RTVEC_ELT (vec, eltno)) = 1;
3131           regno += regno_inc;
3132         }
3133     }
3134
3135   insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, vec));
3136
3137   /* Because dwarf2out.c handles the insns in a parallel as a sequence,
3138      we need to keep the stack adjustment separate, after the
3139      MEM-setters.  Else the stack-adjustment in the second component of
3140      the parallel would be mishandled; the offsets for the SETs that
3141      follow it would be wrong.  We prepare for this by adding a
3142      REG_FRAME_RELATED_EXPR with the MEM-setting parts in a SEQUENCE
3143      followed by the increment.  Note that we have FRAME_RELATED_P on
3144      all the SETs, including the original stack adjustment SET in the
3145      parallel.  */
3146   if (frame_related)
3147     {
3148       if (increment != 0)
3149         {
3150           rtx seq = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (nregs + 1));
3151           XVECEXP (seq, 0, 0) = XVECEXP (PATTERN (insn), 0, 0);
3152           for (i = 1; i < nregs; i++)
3153             XVECEXP (seq, 0, i) = XVECEXP (PATTERN (insn), 0, i + 1);
3154           XVECEXP (seq, 0, nregs) = XVECEXP (PATTERN (insn), 0, 1);
3155           REG_NOTES (insn)
3156             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, seq,
3157                                  REG_NOTES (insn));
3158         }
3159
3160       RTX_FRAME_RELATED_P (insn) = 1;
3161     }
3162
3163   return insn;
3164 }
3165
3166 /* Worker function for expanding the address for PIC function calls.  */
3167
3168 void
3169 cris_expand_pic_call_address (rtx *opp)
3170 {
3171   rtx op = *opp;
3172
3173   gcc_assert (MEM_P (op));
3174   op = XEXP (op, 0);
3175
3176   /* It might be that code can be generated that jumps to 0 (or to a
3177      specific address).  Don't die on that.  (There is a
3178      testcase.)  */
3179   if (CONSTANT_ADDRESS_P (op) && GET_CODE (op) != CONST_INT)
3180     {
3181       enum cris_pic_symbol_type t = cris_pic_symbol_type_of (op);
3182
3183       CRIS_ASSERT (!no_new_pseudos);
3184
3185       /* For local symbols (non-PLT), just get the plain symbol
3186          reference into a register.  For symbols that can be PLT, make
3187          them PLT.  */
3188       if (t == cris_gotrel_symbol)
3189         op = force_reg (Pmode, op);
3190       else if (t == cris_got_symbol)
3191         {
3192           if (TARGET_AVOID_GOTPLT)
3193             {
3194               /* Change a "jsr sym" into (allocate register rM, rO)
3195                  "move.d (const (unspec [sym] CRIS_UNSPEC_PLT)),rM"
3196                  "add.d rPIC,rM,rO", "jsr rO".  */
3197               rtx tem, rm, ro;
3198               gcc_assert (! no_new_pseudos);
3199               current_function_uses_pic_offset_table = 1;
3200               tem = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op), CRIS_UNSPEC_PLT);
3201               rm = gen_reg_rtx (Pmode);
3202               emit_move_insn (rm, gen_rtx_CONST (Pmode, tem));
3203               ro = gen_reg_rtx (Pmode);
3204               if (expand_binop (Pmode, add_optab, rm,
3205                                 pic_offset_table_rtx,
3206                                 ro, 0, OPTAB_LIB_WIDEN) != ro)
3207                 internal_error ("expand_binop failed in movsi got");
3208               op = ro;
3209             }
3210           else
3211             {
3212               /* Change a "jsr sym" into (allocate register rM, rO)
3213                  "move.d (const (unspec [sym] CRIS_UNSPEC_PLTGOT)),rM"
3214                  "add.d rPIC,rM,rO" "jsr [rO]" with the memory access
3215                  marked as not trapping and not aliasing.  No "move.d
3216                  [rO],rP" as that would invite to re-use of a value
3217                  that should not be reused.  FIXME: Need a peephole2
3218                  for cases when this is cse:d from the call, to change
3219                  back to just get the PLT entry address, so we don't
3220                  resolve the same symbol over and over (the memory
3221                  access of the PLTGOT isn't constant).  */
3222               rtx tem, mem, rm, ro;
3223
3224               gcc_assert (! no_new_pseudos);
3225               current_function_uses_pic_offset_table = 1;
3226               tem = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op),
3227                                     CRIS_UNSPEC_PLTGOTREAD);
3228               rm = gen_reg_rtx (Pmode);
3229               emit_move_insn (rm, gen_rtx_CONST (Pmode, tem));
3230               ro = gen_reg_rtx (Pmode);
3231               if (expand_binop (Pmode, add_optab, rm,
3232                                 pic_offset_table_rtx,
3233                                 ro, 0, OPTAB_LIB_WIDEN) != ro)
3234                 internal_error ("expand_binop failed in movsi got");
3235               mem = gen_rtx_MEM (Pmode, ro);
3236
3237               /* This MEM doesn't alias anything.  Whether it aliases
3238                  other same symbols is unimportant.  */
3239               set_mem_alias_set (mem, new_alias_set ());
3240               MEM_NOTRAP_P (mem) = 1;
3241               op = mem;
3242             }
3243         }
3244       else
3245         /* Can't possibly get a GOT-needing-fixup for a function-call,
3246            right?  */
3247         fatal_insn ("Unidentifiable call op", op);
3248
3249       *opp = replace_equiv_address (*opp, op);
3250     }
3251 }
3252
3253 /* Use from within code, from e.g. PRINT_OPERAND and
3254    PRINT_OPERAND_ADDRESS.  Macros used in output_addr_const need to emit
3255    different things depending on whether code operand or constant is
3256    emitted.  */
3257
3258 static void
3259 cris_output_addr_const (FILE *file, rtx x)
3260 {
3261   in_code++;
3262   output_addr_const (file, x);
3263   in_code--;
3264 }
3265
3266 /* Worker function for ASM_OUTPUT_SYMBOL_REF.  */
3267
3268 void
3269 cris_asm_output_symbol_ref (FILE *file, rtx x)
3270 {
3271   gcc_assert (GET_CODE (x) == SYMBOL_REF);
3272
3273   if (flag_pic && in_code > 0)
3274     {
3275      const char *origstr = XSTR (x, 0);
3276      const char *str;
3277      str = (* targetm.strip_name_encoding) (origstr);
3278      assemble_name (file, str);
3279
3280      /* Sanity check.  */
3281      if (! current_function_uses_pic_offset_table)
3282        output_operand_lossage ("PIC register isn't set up");
3283     }
3284   else
3285     assemble_name (file, XSTR (x, 0));
3286 }
3287
3288 /* Worker function for ASM_OUTPUT_LABEL_REF.  */
3289
3290 void
3291 cris_asm_output_label_ref (FILE *file, char *buf)
3292 {
3293   if (flag_pic && in_code > 0)
3294     {
3295       assemble_name (file, buf);
3296
3297       /* Sanity check.  */
3298       if (! current_function_uses_pic_offset_table)
3299         internal_error ("emitting PIC operand, but PIC register isn't set up");
3300     }
3301   else
3302     assemble_name (file, buf);
3303 }
3304
3305 /* Worker function for OUTPUT_ADDR_CONST_EXTRA.  */
3306
3307 bool
3308 cris_output_addr_const_extra (FILE *file, rtx xconst)
3309 {
3310   switch (GET_CODE (xconst))
3311     {
3312       rtx x;
3313
3314     case UNSPEC:
3315       x = XVECEXP (xconst, 0, 0);
3316       CRIS_ASSERT (GET_CODE (x) == SYMBOL_REF
3317                    || GET_CODE (x) == LABEL_REF
3318                    || GET_CODE (x) == CONST);
3319       output_addr_const (file, x);
3320       switch (XINT (xconst, 1))
3321         {
3322         case CRIS_UNSPEC_PLT:
3323           fprintf (file, ":PLTG");
3324           break;
3325
3326         case CRIS_UNSPEC_GOTREL:
3327           fprintf (file, ":GOTOFF");
3328           break;
3329
3330         case CRIS_UNSPEC_GOTREAD:
3331           if (flag_pic == 1)
3332             fprintf (file, ":GOT16");
3333           else
3334             fprintf (file, ":GOT");
3335           break;
3336
3337         case CRIS_UNSPEC_PLTGOTREAD:
3338           if (flag_pic == 1)
3339             fprintf (file, CRIS_GOTPLT_SUFFIX "16");
3340           else
3341             fprintf (file, CRIS_GOTPLT_SUFFIX);
3342           break;
3343
3344         default:
3345           gcc_unreachable ();
3346         }
3347       return true;
3348
3349     default:
3350       return false;
3351     }
3352 }
3353
3354 /* Worker function for TARGET_STRUCT_VALUE_RTX.  */
3355
3356 static rtx
3357 cris_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
3358                        int incoming ATTRIBUTE_UNUSED)
3359 {
3360   return gen_rtx_REG (Pmode, CRIS_STRUCT_VALUE_REGNUM);
3361 }
3362
3363 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
3364
3365 static void
3366 cris_setup_incoming_varargs (CUMULATIVE_ARGS *ca,
3367                              enum machine_mode mode ATTRIBUTE_UNUSED,
3368                              tree type ATTRIBUTE_UNUSED,
3369                              int *pretend_arg_size,
3370                              int second_time)
3371 {
3372   if (ca->regs < CRIS_MAX_ARGS_IN_REGS)
3373     {
3374       int stdarg_regs = CRIS_MAX_ARGS_IN_REGS - ca->regs;
3375       cfun->machine->stdarg_regs = stdarg_regs;
3376       *pretend_arg_size = stdarg_regs * 4;
3377     }
3378
3379   if (TARGET_PDEBUG)
3380     fprintf (asm_out_file,
3381              "\n; VA:: ANSI: %d args before, anon @ #%d, %dtime\n",
3382              ca->regs, *pretend_arg_size, second_time);
3383 }
3384
3385 /* Return true if TYPE must be passed by invisible reference.
3386    For cris, we pass <= 8 bytes by value, others by reference.  */
3387
3388 static bool
3389 cris_pass_by_reference (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
3390                         enum machine_mode mode, tree type,
3391                         bool named ATTRIBUTE_UNUSED)
3392 {
3393   return (targetm.calls.must_pass_in_stack (mode, type)
3394           || CRIS_FUNCTION_ARG_SIZE (mode, type) > 8);
3395 }
3396
3397
3398 static int
3399 cris_arg_partial_bytes (CUMULATIVE_ARGS *ca, enum machine_mode mode,
3400                         tree type, bool named ATTRIBUTE_UNUSED)
3401 {
3402   if (ca->regs == CRIS_MAX_ARGS_IN_REGS - 1
3403       && !targetm.calls.must_pass_in_stack (mode, type)
3404       && CRIS_FUNCTION_ARG_SIZE (mode, type) > 4
3405       && CRIS_FUNCTION_ARG_SIZE (mode, type) <= 8)
3406     return UNITS_PER_WORD;
3407   else
3408     return 0;
3409 }
3410
3411 /* Worker function for TARGET_MD_ASM_CLOBBERS.  */
3412
3413 static tree
3414 cris_md_asm_clobbers (tree outputs, tree inputs, tree in_clobbers)
3415 {
3416   HARD_REG_SET mof_set;
3417   tree clobbers;
3418   tree t;
3419
3420   CLEAR_HARD_REG_SET (mof_set);
3421   SET_HARD_REG_BIT (mof_set, CRIS_MOF_REGNUM);
3422
3423   /* For the time being, all asms clobber condition codes.  Revisit when
3424      there's a reasonable use for inputs/outputs that mention condition
3425      codes.  */
3426   clobbers
3427     = tree_cons (NULL_TREE,
3428                  build_string (strlen (reg_names[CRIS_CC0_REGNUM]),
3429                                reg_names[CRIS_CC0_REGNUM]),
3430                  in_clobbers);
3431
3432   for (t = outputs; t != NULL; t = TREE_CHAIN (t))
3433     {
3434       tree val = TREE_VALUE (t);
3435
3436       /* The constraint letter for the singleton register class of MOF
3437          is 'h'.  If it's mentioned in the constraints, the asm is
3438          MOF-aware and adding it to the clobbers would cause it to have
3439          impossible constraints.  */
3440       if (strchr (TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))),
3441                   'h') != NULL
3442           || decl_overlaps_hard_reg_set_p (val, mof_set))
3443         return clobbers;
3444     }
3445
3446   for (t = inputs; t != NULL; t = TREE_CHAIN (t))
3447     {
3448       tree val = TREE_VALUE (t);
3449
3450       if (strchr (TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t))),
3451                   'h') != NULL
3452           || decl_overlaps_hard_reg_set_p (val, mof_set))
3453         return clobbers;
3454     }
3455
3456   return tree_cons (NULL_TREE,
3457                     build_string (strlen (reg_names[CRIS_MOF_REGNUM]),
3458                                   reg_names[CRIS_MOF_REGNUM]),
3459                     clobbers);
3460 }
3461
3462 #if 0
3463 /* Various small functions to replace macros.  Only called from a
3464    debugger.  They might collide with gcc functions or system functions,
3465    so only emit them when '#if 1' above.  */
3466
3467 enum rtx_code Get_code (rtx);
3468
3469 enum rtx_code
3470 Get_code (rtx x)
3471 {
3472   return GET_CODE (x);
3473 }
3474
3475 const char *Get_mode (rtx);
3476
3477 const char *
3478 Get_mode (rtx x)
3479 {
3480   return GET_MODE_NAME (GET_MODE (x));
3481 }
3482
3483 rtx Xexp (rtx, int);
3484
3485 rtx
3486 Xexp (rtx x, int n)
3487 {
3488   return XEXP (x, n);
3489 }
3490
3491 rtx Xvecexp (rtx, int, int);
3492
3493 rtx
3494 Xvecexp (rtx x, int n, int m)
3495 {
3496   return XVECEXP (x, n, m);
3497 }
3498
3499 int Get_rtx_len (rtx);
3500
3501 int
3502 Get_rtx_len (rtx x)
3503 {
3504   return GET_RTX_LENGTH (GET_CODE (x));
3505 }
3506
3507 /* Use upper-case to distinguish from local variables that are sometimes
3508    called next_insn and prev_insn.  */
3509
3510 rtx Next_insn (rtx);
3511
3512 rtx
3513 Next_insn (rtx insn)
3514 {
3515   return NEXT_INSN (insn);
3516 }
3517
3518 rtx Prev_insn (rtx);
3519
3520 rtx
3521 Prev_insn (rtx insn)
3522 {
3523   return PREV_INSN (insn);
3524 }
3525 #endif
3526
3527 #include "gt-cris.h"
3528
3529 /*
3530  * Local variables:
3531  * eval: (c-set-style "gnu")
3532  * indent-tabs-mode: t
3533  * End:
3534  */