OSDN Git Service

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