OSDN Git Service

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