OSDN Git Service

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