OSDN Git Service

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