OSDN Git Service

* config/mcore/mcore.c (TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE):
[pf3gnuchains/gcc-fork.git] / gcc / config / mcore / mcore.c
1 /* Output routines for Motorola MCore processor
2    Copyright (C) 1993, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 2, or (at your
10    option) any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    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 "tree.h"
28 #include "tm_p.h"
29 #include "assert.h"
30 #include "mcore.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "obstack.h"
40 #include "expr.h"
41 #include "reload.h"
42 #include "recog.h"
43 #include "function.h"
44 #include "ggc.h"
45 #include "toplev.h"
46 #include "target.h"
47 #include "target-def.h"
48
49 /* Maximum size we are allowed to grow the stack in a single operation.
50    If we want more, we must do it in increments of at most this size.
51    If this value is 0, we don't check at all.  */
52 const char * mcore_stack_increment_string = 0;
53 int          mcore_stack_increment = STACK_UNITS_MAXSTEP;
54
55 /* For dumping information about frame sizes.  */
56 char * mcore_current_function_name = 0;
57 long   mcore_current_compilation_timestamp = 0;
58
59 /* Global variables for machine-dependent things.  */
60
61 /* Saved operands from the last compare to use when we generate an scc
62   or bcc insn.  */
63 rtx arch_compare_op0;
64 rtx arch_compare_op1;
65
66 /* Provides the class number of the smallest class containing
67    reg number.  */
68 const int regno_reg_class[FIRST_PSEUDO_REGISTER] =
69 {
70   GENERAL_REGS, ONLYR1_REGS,  LRW_REGS,     LRW_REGS,
71   LRW_REGS,     LRW_REGS,     LRW_REGS,     LRW_REGS,
72   LRW_REGS,     LRW_REGS,     LRW_REGS,     LRW_REGS,
73   LRW_REGS,     LRW_REGS,     LRW_REGS,     GENERAL_REGS,
74   GENERAL_REGS, C_REGS,       NO_REGS,      NO_REGS,
75 };
76
77 /* Provide reg_class from a letter such as appears in the machine
78    description.  */
79 const enum reg_class reg_class_from_letter[] =
80 {
81   /* a */ LRW_REGS, /* b */ ONLYR1_REGS, /* c */ C_REGS,  /* d */ NO_REGS,
82   /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
83   /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ NO_REGS,
84   /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
85   /* q */ NO_REGS, /* r */ GENERAL_REGS, /* s */ NO_REGS, /* t */ NO_REGS,
86   /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ ALL_REGS,
87   /* y */ NO_REGS, /* z */ NO_REGS
88 };
89
90 struct mcore_frame
91 {
92   int arg_size;                 /* Stdarg spills (bytes).  */
93   int reg_size;                 /* Non-volatile reg saves (bytes).  */
94   int reg_mask;                 /* Non-volatile reg saves.  */
95   int local_size;               /* Locals.  */
96   int outbound_size;            /* Arg overflow on calls out.  */
97   int pad_outbound;
98   int pad_local;
99   int pad_reg;
100   /* Describe the steps we'll use to grow it.  */
101 #define MAX_STACK_GROWS 4       /* Gives us some spare space.  */
102   int growth[MAX_STACK_GROWS];
103   int arg_offset;
104   int reg_offset;
105   int reg_growth;
106   int local_growth;
107 };
108
109 typedef enum
110 {
111   COND_NO,
112   COND_MOV_INSN,
113   COND_CLR_INSN,
114   COND_INC_INSN,
115   COND_DEC_INSN,
116   COND_BRANCH_INSN
117 }
118 cond_type;
119
120 static void       output_stack_adjust           (int, int);
121 static int        calc_live_regs                (int *);
122 static int        const_ok_for_mcore            (int);
123 static int        try_constant_tricks           (long, int *, int *);
124 static const char *     output_inline_const     (enum machine_mode, rtx *);
125 static void       block_move_sequence           (rtx, rtx, rtx, rtx, int, int, int);
126 static void       layout_mcore_frame            (struct mcore_frame *);
127 static void       mcore_setup_incoming_varargs  (CUMULATIVE_ARGS *, enum machine_mode, tree, int *, int);
128 static cond_type  is_cond_candidate             (rtx);
129 static rtx        emit_new_cond_insn            (rtx, int);
130 static rtx        conditionalize_block          (rtx);
131 static void       conditionalize_optimization   (void);
132 static void       mcore_reorg                   (void);
133 static rtx        handle_structs_in_regs        (enum machine_mode, tree, int);
134 static void       mcore_mark_dllexport          (tree);
135 static void       mcore_mark_dllimport          (tree);
136 static int        mcore_dllexport_p             (tree);
137 static int        mcore_dllimport_p             (tree);
138 const struct attribute_spec mcore_attribute_table[];
139 static tree       mcore_handle_naked_attribute  (tree *, tree, tree, int, bool *);
140 #ifdef OBJECT_FORMAT_ELF
141 static void       mcore_asm_named_section       (const char *,
142                                                         unsigned int);
143 #endif
144 static void       mcore_unique_section          (tree, int);
145 static void mcore_encode_section_info           (tree, rtx, int);
146 static const char *mcore_strip_name_encoding    (const char *);
147 static int        mcore_const_costs             (rtx, RTX_CODE);
148 static int        mcore_and_cost                (rtx);
149 static int        mcore_ior_cost                (rtx);
150 static bool       mcore_rtx_costs               (rtx, int, int, int *);
151 static void       mcore_external_libcall        (rtx);
152 static bool       mcore_return_in_memory        (tree, tree);
153
154 \f
155 /* Initialize the GCC target structure.  */
156 #undef  TARGET_ASM_EXTERNAL_LIBCALL
157 #define TARGET_ASM_EXTERNAL_LIBCALL     mcore_external_libcall
158
159 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
160 #undef  TARGET_MERGE_DECL_ATTRIBUTES
161 #define TARGET_MERGE_DECL_ATTRIBUTES    merge_dllimport_decl_attributes
162 #endif
163
164 #ifdef OBJECT_FORMAT_ELF
165 #undef  TARGET_ASM_UNALIGNED_HI_OP
166 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
167 #undef  TARGET_ASM_UNALIGNED_SI_OP
168 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
169 #endif
170
171 #undef  TARGET_ATTRIBUTE_TABLE
172 #define TARGET_ATTRIBUTE_TABLE          mcore_attribute_table
173 #undef  TARGET_ASM_UNIQUE_SECTION
174 #define TARGET_ASM_UNIQUE_SECTION       mcore_unique_section
175 #undef  TARGET_ENCODE_SECTION_INFO
176 #define TARGET_ENCODE_SECTION_INFO      mcore_encode_section_info
177 #undef  TARGET_STRIP_NAME_ENCODING
178 #define TARGET_STRIP_NAME_ENCODING      mcore_strip_name_encoding
179 #undef  TARGET_RTX_COSTS
180 #define TARGET_RTX_COSTS                mcore_rtx_costs
181 #undef  TARGET_ADDRESS_COST
182 #define TARGET_ADDRESS_COST             hook_int_rtx_0
183 #undef  TARGET_MACHINE_DEPENDENT_REORG
184 #define TARGET_MACHINE_DEPENDENT_REORG  mcore_reorg
185
186 #undef  TARGET_PROMOTE_FUNCTION_ARGS
187 #define TARGET_PROMOTE_FUNCTION_ARGS    hook_bool_tree_true
188 #undef  TARGET_PROMOTE_FUNCTION_RETURN
189 #define TARGET_PROMOTE_FUNCTION_RETURN  hook_bool_tree_true
190 #undef  TARGET_PROMOTE_PROTOTYPES
191 #define TARGET_PROMOTE_PROTOTYPES       hook_bool_tree_true
192
193 #undef  TARGET_RETURN_IN_MEMORY
194 #define TARGET_RETURN_IN_MEMORY         mcore_return_in_memory
195
196 #undef  TARGET_SETUP_INCOMING_VARARGS
197 #define TARGET_SETUP_INCOMING_VARARGS   mcore_setup_incoming_varargs
198
199 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
200 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE hook_int_void_1
201
202 struct gcc_target targetm = TARGET_INITIALIZER;
203 \f
204 /* Adjust the stack and return the number of bytes taken to do it.  */
205 static void
206 output_stack_adjust (int direction, int size)
207 {
208   /* If extending stack a lot, we do it incrementally.  */
209   if (direction < 0 && size > mcore_stack_increment && mcore_stack_increment > 0)
210     {
211       rtx tmp = gen_rtx_REG (SImode, 1);
212       rtx memref;
213
214       emit_insn (gen_movsi (tmp, GEN_INT (mcore_stack_increment)));
215       do
216         {
217           emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, tmp));
218           memref = gen_rtx_MEM (SImode, stack_pointer_rtx);
219           MEM_VOLATILE_P (memref) = 1;
220           emit_insn (gen_movsi (memref, stack_pointer_rtx));
221           size -= mcore_stack_increment;
222         }
223       while (size > mcore_stack_increment);
224
225       /* SIZE is now the residual for the last adjustment,
226          which doesn't require a probe.  */
227     }
228
229   if (size)
230     {
231       rtx insn;
232       rtx val = GEN_INT (size);
233
234       if (size > 32)
235         {
236           rtx nval = gen_rtx_REG (SImode, 1);
237           emit_insn (gen_movsi (nval, val));
238           val = nval;
239         }
240       
241       if (direction > 0)
242         insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
243       else
244         insn = gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
245       
246       emit_insn (insn);
247     }
248 }
249
250 /* Work out the registers which need to be saved,
251    both as a mask and a count.  */
252
253 static int
254 calc_live_regs (int * count)
255 {
256   int reg;
257   int live_regs_mask = 0;
258   
259   * count = 0;
260
261   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
262     {
263       if (regs_ever_live[reg] && !call_used_regs[reg])
264         {
265           (*count)++;
266           live_regs_mask |= (1 << reg);
267         }
268     }
269
270   return live_regs_mask;
271 }
272
273 /* Print the operand address in x to the stream.  */
274
275 void
276 mcore_print_operand_address (FILE * stream, rtx x)
277 {
278   switch (GET_CODE (x))
279     {
280     case REG:
281       fprintf (stream, "(%s)", reg_names[REGNO (x)]);
282       break;
283       
284     case PLUS:
285       {
286         rtx base = XEXP (x, 0);
287         rtx index = XEXP (x, 1);
288
289         if (GET_CODE (base) != REG)
290           {
291             /* Ensure that BASE is a register (one of them must be).  */
292             rtx temp = base;
293             base = index;
294             index = temp;
295           }
296
297         switch (GET_CODE (index))
298           {
299           case CONST_INT:
300             fprintf (stream, "(%s," HOST_WIDE_INT_PRINT_DEC ")",
301                      reg_names[REGNO(base)], INTVAL (index));
302             break;
303
304           default:
305             debug_rtx (x);
306
307             abort ();
308           }
309       }
310
311       break;
312
313     default:
314       output_addr_const (stream, x);
315       break;
316     }
317 }
318
319 /* Print operand x (an rtx) in assembler syntax to file stream
320    according to modifier code.
321
322    'R'  print the next register or memory location along, ie the lsw in
323         a double word value
324    'O'  print a constant without the #
325    'M'  print a constant as its negative
326    'P'  print log2 of a power of two
327    'Q'  print log2 of an inverse of a power of two
328    'U'  print register for ldm/stm instruction
329    'X'  print byte number for xtrbN instruction.  */
330
331 void
332 mcore_print_operand (FILE * stream, rtx x, int code)
333 {
334   switch (code)
335     {
336     case 'N':
337       if (INTVAL(x) == -1)
338         fprintf (asm_out_file, "32");
339       else
340         fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x) + 1));
341       break;
342     case 'P':
343       fprintf (asm_out_file, "%d", exact_log2 (INTVAL (x)));
344       break;
345     case 'Q':
346       fprintf (asm_out_file, "%d", exact_log2 (~INTVAL (x)));
347       break;
348     case 'O':
349       fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
350       break;
351     case 'M':
352       fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, - INTVAL (x));
353       break;
354     case 'R':
355       /* Next location along in memory or register.  */
356       switch (GET_CODE (x))
357         {
358         case REG:
359           fputs (reg_names[REGNO (x) + 1], (stream));
360           break;
361         case MEM:
362           mcore_print_operand_address
363             (stream, XEXP (adjust_address (x, SImode, 4), 0));
364           break;
365         default:
366           abort ();
367         }
368       break;
369     case 'U':
370       fprintf (asm_out_file, "%s-%s", reg_names[REGNO (x)],
371                reg_names[REGNO (x) + 3]);
372       break;
373     case 'x':
374       fprintf (asm_out_file, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
375       break;
376     case 'X':
377       fprintf (asm_out_file, HOST_WIDE_INT_PRINT_DEC, 3 - INTVAL (x) / 8);
378       break;
379
380     default:
381       switch (GET_CODE (x))
382         {
383         case REG:
384           fputs (reg_names[REGNO (x)], (stream));
385           break;
386         case MEM:
387           output_address (XEXP (x, 0));
388           break;
389         default:
390           output_addr_const (stream, x);
391           break;
392         }
393       break;
394     }
395 }
396
397 /* What does a constant cost ?  */
398
399 static int
400 mcore_const_costs (rtx exp, enum rtx_code code)
401 {
402   int val = INTVAL (exp);
403
404   /* Easy constants.  */
405   if (   CONST_OK_FOR_I (val)   
406       || CONST_OK_FOR_M (val)   
407       || CONST_OK_FOR_N (val)   
408       || (code == PLUS && CONST_OK_FOR_L (val)))
409     return 1;                                   
410   else if (code == AND
411            && (   CONST_OK_FOR_M (~val)
412                || CONST_OK_FOR_N (~val)))
413     return 2;
414   else if (code == PLUS                 
415            && (   CONST_OK_FOR_I (-val) 
416                || CONST_OK_FOR_M (-val) 
417                || CONST_OK_FOR_N (-val)))       
418     return 2;                                           
419
420   return 5;                                     
421 }
422
423 /* What does an and instruction cost - we do this b/c immediates may 
424    have been relaxed.   We want to ensure that cse will cse relaxed immeds
425    out.  Otherwise we'll get bad code (multiple reloads of the same const).  */
426
427 static int
428 mcore_and_cost (rtx x)
429 {
430   int val;
431
432   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
433     return 2;
434
435   val = INTVAL (XEXP (x, 1));
436    
437   /* Do it directly.  */
438   if (CONST_OK_FOR_K (val) || CONST_OK_FOR_M (~val))
439     return 2;
440   /* Takes one instruction to load.  */
441   else if (const_ok_for_mcore (val))
442     return 3;
443   /* Takes two instructions to load.  */
444   else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
445     return 4;
446
447   /* Takes a lrw to load.  */
448   return 5;
449 }
450
451 /* What does an or cost - see and_cost().  */
452
453 static int
454 mcore_ior_cost (rtx x)
455 {
456   int val;
457
458   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
459     return 2;
460
461   val = INTVAL (XEXP (x, 1));
462
463   /* Do it directly with bclri.  */
464   if (CONST_OK_FOR_M (val))
465     return 2;
466   /* Takes one instruction to load.  */
467   else if (const_ok_for_mcore (val))
468     return 3;
469   /* Takes two instructions to load.  */
470   else if (TARGET_HARDLIT && mcore_const_ok_for_inline (val))
471     return 4;
472   
473   /* Takes a lrw to load.  */
474   return 5;
475 }
476
477 static bool
478 mcore_rtx_costs (rtx x, int code, int outer_code, int * total)
479 {
480   switch (code)
481     {
482     case CONST_INT:
483       *total = mcore_const_costs (x, outer_code);
484       return true;
485     case CONST:
486     case LABEL_REF:
487     case SYMBOL_REF:
488       *total = 5;
489       return true;
490     case CONST_DOUBLE:
491       *total = 10;
492       return true;
493
494     case AND:
495       *total = COSTS_N_INSNS (mcore_and_cost (x));
496       return true;
497
498     case IOR:
499       *total = COSTS_N_INSNS (mcore_ior_cost (x));
500       return true;
501
502     case DIV:
503     case UDIV:
504     case MOD:
505     case UMOD:
506     case FLOAT:
507     case FIX:
508       *total = COSTS_N_INSNS (100);
509       return true;
510   
511     default:
512       return false;
513     }
514 }
515
516 /* Check to see if a comparison against a constant can be made more efficient
517    by incrementing/decrementing the constant to get one that is more efficient
518    to load.  */
519
520 int
521 mcore_modify_comparison (enum rtx_code code)
522 {
523   rtx op1 = arch_compare_op1;
524   
525   if (GET_CODE (op1) == CONST_INT)
526     {
527       int val = INTVAL (op1);
528       
529       switch (code)
530         {
531         case LE:
532           if (CONST_OK_FOR_J (val + 1))
533             {
534               arch_compare_op1 = GEN_INT (val + 1);
535               return 1;
536             }
537           break;
538           
539         default:
540           break;
541         }
542     }
543   
544   return 0;
545 }
546
547 /* Prepare the operands for a comparison.  */
548
549 rtx
550 mcore_gen_compare_reg (enum rtx_code code)
551 {
552   rtx op0 = arch_compare_op0;
553   rtx op1 = arch_compare_op1;
554   rtx cc_reg = gen_rtx_REG (CCmode, CC_REG);
555
556   if (CONSTANT_P (op1) && GET_CODE (op1) != CONST_INT)
557     op1 = force_reg (SImode, op1);
558
559   /* cmpnei: 0-31 (K immediate)
560      cmplti: 1-32 (J immediate, 0 using btsti x,31).  */
561   switch (code)
562     {
563     case EQ:    /* Use inverted condition, cmpne.  */
564       code = NE;
565       /* Drop through.  */
566       
567     case NE:    /* Use normal condition, cmpne.  */
568       if (GET_CODE (op1) == CONST_INT && ! CONST_OK_FOR_K (INTVAL (op1)))
569         op1 = force_reg (SImode, op1);
570       break;
571
572     case LE:    /* Use inverted condition, reversed cmplt.  */
573       code = GT;
574       /* Drop through.  */
575       
576     case GT:    /* Use normal condition, reversed cmplt.  */
577       if (GET_CODE (op1) == CONST_INT)
578         op1 = force_reg (SImode, op1);
579       break;
580
581     case GE:    /* Use inverted condition, cmplt.  */
582       code = LT;
583       /* Drop through.  */
584       
585     case LT:    /* Use normal condition, cmplt.  */
586       if (GET_CODE (op1) == CONST_INT && 
587           /* covered by btsti x,31.  */
588           INTVAL (op1) != 0 &&
589           ! CONST_OK_FOR_J (INTVAL (op1)))
590         op1 = force_reg (SImode, op1);
591       break;
592
593     case GTU:   /* Use inverted condition, cmple.  */
594       if (GET_CODE (op1) == CONST_INT && INTVAL (op1) == 0)
595         {
596           /* Unsigned > 0 is the same as != 0, but we need
597              to invert the condition, so we want to set
598              code = EQ.  This cannot be done however, as the
599              mcore does not support such a test.  Instead we
600              cope with this case in the "bgtu" pattern itself
601              so we should never reach this point.  */
602           /* code = EQ; */
603           abort ();
604           break;
605         }
606       code = LEU;
607       /* Drop through.  */
608       
609     case LEU:   /* Use normal condition, reversed cmphs.  */
610       if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
611         op1 = force_reg (SImode, op1);
612       break;
613
614     case LTU:   /* Use inverted condition, cmphs.  */
615       code = GEU;
616       /* Drop through.  */
617       
618     case GEU:   /* Use normal condition, cmphs.  */
619       if (GET_CODE (op1) == CONST_INT && INTVAL (op1) != 0)
620         op1 = force_reg (SImode, op1);
621       break;
622
623     default:
624       break;
625     }
626
627   emit_insn (gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_fmt_ee (code, CCmode, op0, op1)));
628   
629   return cc_reg;
630 }
631
632 int
633 mcore_symbolic_address_p (rtx x)
634 {
635   switch (GET_CODE (x))
636     {
637     case SYMBOL_REF:
638     case LABEL_REF:
639       return 1;
640     case CONST:
641       x = XEXP (x, 0);
642       return (   (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
643                || GET_CODE (XEXP (x, 0)) == LABEL_REF)
644               && GET_CODE (XEXP (x, 1)) == CONST_INT);
645     default:
646       return 0;
647     }
648 }
649
650 int
651 mcore_call_address_operand (rtx x, enum machine_mode mode)
652 {
653   return register_operand (x, mode) || CONSTANT_P (x);
654 }
655
656 /* Functions to output assembly code for a function call.  */
657
658 char *
659 mcore_output_call (rtx operands[], int index)
660 {
661   static char buffer[20];
662   rtx addr = operands [index];
663   
664   if (REG_P (addr))
665     {
666       if (TARGET_CG_DATA)
667         {
668           if (mcore_current_function_name == 0)
669             abort ();
670           
671           ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name,
672                               "unknown", 1);
673         }
674
675       sprintf (buffer, "jsr\t%%%d", index);
676     }
677   else
678     {
679       if (TARGET_CG_DATA)
680         {
681           if (mcore_current_function_name == 0)
682             abort ();
683           
684           if (GET_CODE (addr) != SYMBOL_REF)
685             abort ();
686           
687           ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, XSTR (addr, 0), 0);
688         }
689       
690       sprintf (buffer, "jbsr\t%%%d", index);
691     }
692
693   return buffer;
694 }
695
696 /* Can we load a constant with a single instruction ?  */
697
698 static int
699 const_ok_for_mcore (int value)
700 {
701   if (value >= 0 && value <= 127)
702     return 1;
703   
704   /* Try exact power of two.  */
705   if ((value & (value - 1)) == 0)
706     return 1;
707   
708   /* Try exact power of two - 1.  */
709   if ((value & (value + 1)) == 0)
710     return 1;
711   
712   return 0;
713 }
714
715 /* Can we load a constant inline with up to 2 instructions ?  */
716
717 int
718 mcore_const_ok_for_inline (long value)
719 {
720   int x, y;
721    
722   return try_constant_tricks (value, & x, & y) > 0;
723 }
724
725 /* Are we loading the constant using a not ?  */
726
727 int
728 mcore_const_trick_uses_not (long value)
729 {
730   int x, y;
731
732   return try_constant_tricks (value, & x, & y) == 2; 
733 }       
734
735 /* Try tricks to load a constant inline and return the trick number if
736    success (0 is non-inlinable).
737   
738    0: not inlinable
739    1: single instruction (do the usual thing)
740    2: single insn followed by a 'not'
741    3: single insn followed by a subi
742    4: single insn followed by an addi
743    5: single insn followed by rsubi
744    6: single insn followed by bseti
745    7: single insn followed by bclri
746    8: single insn followed by rotli
747    9: single insn followed by lsli
748    10: single insn followed by ixh
749    11: single insn followed by ixw.  */
750
751 static int
752 try_constant_tricks (long value, int * x, int * y)
753 {
754   int i;
755   unsigned bit, shf, rot;
756
757   if (const_ok_for_mcore (value))
758     return 1;   /* Do the usual thing.  */
759   
760   if (TARGET_HARDLIT) 
761     {
762       if (const_ok_for_mcore (~value))
763         {
764           *x = ~value;
765           return 2;
766         }
767       
768       for (i = 1; i <= 32; i++)
769         {
770           if (const_ok_for_mcore (value - i))
771             {
772               *x = value - i;
773               *y = i;
774               
775               return 3;
776             }
777           
778           if (const_ok_for_mcore (value + i))
779             {
780               *x = value + i;
781               *y = i;
782               
783               return 4;
784             }
785         }
786       
787       bit = 0x80000000L;
788       
789       for (i = 0; i <= 31; i++)
790         {
791           if (const_ok_for_mcore (i - value))
792             {
793               *x = i - value;
794               *y = i;
795               
796               return 5;
797             }
798           
799           if (const_ok_for_mcore (value & ~bit))
800             {
801               *y = bit;
802               *x = value & ~bit;
803               
804               return 6;
805             }
806           
807           if (const_ok_for_mcore (value | bit))
808             {
809               *y = ~bit;
810               *x = value | bit;
811               
812               return 7;
813             }
814           
815           bit >>= 1;
816         }
817       
818       shf = value;
819       rot = value;
820       
821       for (i = 1; i < 31; i++)
822         {
823           int c;
824           
825           /* MCore has rotate left.  */
826           c = rot << 31;
827           rot >>= 1;
828           rot &= 0x7FFFFFFF;
829           rot |= c;   /* Simulate rotate.  */
830           
831           if (const_ok_for_mcore (rot))
832             {
833               *y = i;
834               *x = rot;
835               
836               return 8;
837             }
838           
839           if (shf & 1)
840             shf = 0;    /* Can't use logical shift, low order bit is one.  */
841           
842           shf >>= 1;
843           
844           if (shf != 0 && const_ok_for_mcore (shf))
845             {
846               *y = i;
847               *x = shf;
848               
849               return 9;
850             }
851         }
852       
853       if ((value % 3) == 0 && const_ok_for_mcore (value / 3))
854         {
855           *x = value / 3;
856           
857           return 10;
858         }
859       
860       if ((value % 5) == 0 && const_ok_for_mcore (value / 5))
861         {
862           *x = value / 5;
863           
864           return 11;
865         }
866     }
867   
868   return 0;
869 }
870
871 /* Check whether reg is dead at first.  This is done by searching ahead
872    for either the next use (i.e., reg is live), a death note, or a set of
873    reg.  Don't just use dead_or_set_p() since reload does not always mark 
874    deaths (especially if PRESERVE_DEATH_NOTES_REGNO_P is not defined). We
875    can ignore subregs by extracting the actual register.  BRC  */
876
877 int
878 mcore_is_dead (rtx first, rtx reg)
879 {
880   rtx insn;
881
882   /* For mcore, subregs can't live independently of their parent regs.  */
883   if (GET_CODE (reg) == SUBREG)
884     reg = SUBREG_REG (reg);
885
886   /* Dies immediately.  */
887   if (dead_or_set_p (first, reg))
888     return 1;
889
890   /* Look for conclusive evidence of live/death, otherwise we have
891      to assume that it is live.  */
892   for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
893     {
894       if (GET_CODE (insn) == JUMP_INSN)
895         return 0;       /* We lose track, assume it is alive.  */
896
897       else if (GET_CODE(insn) == CALL_INSN)
898         {
899           /* Call's might use it for target or register parms.  */
900           if (reg_referenced_p (reg, PATTERN (insn))
901               || find_reg_fusage (insn, USE, reg))
902             return 0;
903           else if (dead_or_set_p (insn, reg))
904             return 1;
905         }
906       else if (GET_CODE (insn) == INSN)
907         {
908           if (reg_referenced_p (reg, PATTERN (insn)))
909             return 0;
910           else if (dead_or_set_p (insn, reg))
911             return 1;
912         }
913     }
914
915   /* No conclusive evidence either way, we can not take the chance
916      that control flow hid the use from us -- "I'm not dead yet".  */
917   return 0;
918 }
919
920 /* Count the number of ones in mask.  */
921
922 int
923 mcore_num_ones (int mask)
924 {
925   /* A trick to count set bits recently posted on comp.compilers.  */
926   mask =  (mask >> 1  & 0x55555555) + (mask & 0x55555555);
927   mask = ((mask >> 2) & 0x33333333) + (mask & 0x33333333);
928   mask = ((mask >> 4) + mask) & 0x0f0f0f0f;
929   mask = ((mask >> 8) + mask);
930
931   return (mask + (mask >> 16)) & 0xff;
932 }
933
934 /* Count the number of zeros in mask.  */
935
936 int
937 mcore_num_zeros (int mask)
938 {
939   return 32 - mcore_num_ones (mask);
940 }
941
942 /* Determine byte being masked.  */
943
944 int
945 mcore_byte_offset (unsigned int mask)
946 {
947   if (mask == 0x00ffffffL)
948     return 0;
949   else if (mask == 0xff00ffffL)
950     return 1;
951   else if (mask == 0xffff00ffL)
952     return 2;
953   else if (mask == 0xffffff00L)
954     return 3;
955
956   return -1;
957 }
958
959 /* Determine halfword being masked.  */
960
961 int
962 mcore_halfword_offset (unsigned int mask)
963 {
964   if (mask == 0x0000ffffL)
965     return 0;
966   else if (mask == 0xffff0000L)
967     return 1;
968
969   return -1;
970 }
971
972 /* Output a series of bseti's corresponding to mask.  */
973
974 const char *
975 mcore_output_bseti (rtx dst, int mask)
976 {
977   rtx out_operands[2];
978   int bit;
979
980   out_operands[0] = dst;
981
982   for (bit = 0; bit < 32; bit++)
983     {
984       if ((mask & 0x1) == 0x1)
985         {
986           out_operands[1] = GEN_INT (bit);
987           
988           output_asm_insn ("bseti\t%0,%1", out_operands);
989         }
990       mask >>= 1;
991     }  
992
993   return "";
994 }
995
996 /* Output a series of bclri's corresponding to mask.  */
997
998 const char *
999 mcore_output_bclri (rtx dst, int mask)
1000 {
1001   rtx out_operands[2];
1002   int bit;
1003
1004   out_operands[0] = dst;
1005
1006   for (bit = 0; bit < 32; bit++)
1007     {
1008       if ((mask & 0x1) == 0x0)
1009         {
1010           out_operands[1] = GEN_INT (bit);
1011           
1012           output_asm_insn ("bclri\t%0,%1", out_operands);
1013         }
1014       
1015       mask >>= 1;
1016     }  
1017
1018   return "";
1019 }
1020
1021 /* Output a conditional move of two constants that are +/- 1 within each
1022    other.  See the "movtK" patterns in mcore.md.   I'm not sure this is
1023    really worth the effort.  */
1024
1025 const char *
1026 mcore_output_cmov (rtx operands[], int cmp_t, const char * test)
1027 {
1028   int load_value;
1029   int adjust_value;
1030   rtx out_operands[4];
1031
1032   out_operands[0] = operands[0];
1033
1034   /* Check to see which constant is loadable.  */
1035   if (const_ok_for_mcore (INTVAL (operands[1])))
1036     {
1037       out_operands[1] = operands[1];
1038       out_operands[2] = operands[2];
1039     }
1040   else if (const_ok_for_mcore (INTVAL (operands[2])))
1041     {
1042       out_operands[1] = operands[2];
1043       out_operands[2] = operands[1];
1044
1045       /* Complement test since constants are swapped.  */
1046       cmp_t = (cmp_t == 0);
1047     }
1048   load_value   = INTVAL (out_operands[1]);
1049   adjust_value = INTVAL (out_operands[2]);
1050
1051   /* First output the test if folded into the pattern.  */
1052
1053   if (test) 
1054     output_asm_insn (test, operands);
1055
1056   /* Load the constant - for now, only support constants that can be
1057      generated with a single instruction.  maybe add general inlinable
1058      constants later (this will increase the # of patterns since the
1059      instruction sequence has a different length attribute).  */
1060   if (load_value >= 0 && load_value <= 127)
1061     output_asm_insn ("movi\t%0,%1", out_operands);
1062   else if ((load_value & (load_value - 1)) == 0)
1063     output_asm_insn ("bgeni\t%0,%P1", out_operands);
1064   else if ((load_value & (load_value + 1)) == 0)
1065     output_asm_insn ("bmaski\t%0,%N1", out_operands);
1066    
1067   /* Output the constant adjustment.  */
1068   if (load_value > adjust_value)
1069     {
1070       if (cmp_t)
1071         output_asm_insn ("decf\t%0", out_operands);
1072       else
1073         output_asm_insn ("dect\t%0", out_operands);
1074     }
1075   else
1076     {
1077       if (cmp_t)
1078         output_asm_insn ("incf\t%0", out_operands);
1079       else
1080         output_asm_insn ("inct\t%0", out_operands);
1081     }
1082
1083   return "";
1084 }
1085
1086 /* Outputs the peephole for moving a constant that gets not'ed followed 
1087    by an and (i.e. combine the not and the and into andn). BRC  */
1088
1089 const char *
1090 mcore_output_andn (rtx insn ATTRIBUTE_UNUSED, rtx operands[])
1091 {
1092   int x, y;
1093   rtx out_operands[3];
1094   const char * load_op;
1095   char buf[256];
1096
1097   if (try_constant_tricks (INTVAL (operands[1]), &x, &y) != 2)
1098     abort ();
1099
1100   out_operands[0] = operands[0];
1101   out_operands[1] = GEN_INT(x);
1102   out_operands[2] = operands[2];
1103
1104   if (x >= 0 && x <= 127)
1105     load_op = "movi\t%0,%1";
1106   
1107   /* Try exact power of two.  */
1108   else if ((x & (x - 1)) == 0)
1109     load_op = "bgeni\t%0,%P1";
1110   
1111   /* Try exact power of two - 1.  */
1112   else if ((x & (x + 1)) == 0)
1113     load_op = "bmaski\t%0,%N1";
1114   
1115   else 
1116     load_op = "BADMOVI\t%0,%1";
1117
1118   sprintf (buf, "%s\n\tandn\t%%2,%%0", load_op);
1119   output_asm_insn (buf, out_operands);
1120
1121   return "";
1122 }
1123
1124 /* Output an inline constant.  */
1125
1126 static const char *
1127 output_inline_const (enum machine_mode mode, rtx operands[])
1128 {
1129   int x = 0, y = 0;
1130   int trick_no;
1131   rtx out_operands[3];
1132   char buf[256];
1133   char load_op[256];
1134   const char *dst_fmt;
1135   int value;
1136
1137   value = INTVAL (operands[1]);
1138    
1139   if ((trick_no = try_constant_tricks (value, &x, &y)) == 0)
1140     {
1141       /* lrw's are handled separately:  Large inlinable constants
1142          never get turned into lrw's.  Our caller uses try_constant_tricks
1143          to back off to an lrw rather than calling this routine.  */
1144       abort ();
1145     }
1146
1147   if (trick_no == 1)
1148     x = value;
1149
1150   /* operands: 0 = dst, 1 = load immed., 2 = immed. adjustment.  */
1151   out_operands[0] = operands[0];
1152   out_operands[1] = GEN_INT (x);
1153   
1154   if (trick_no > 2)
1155     out_operands[2] = GEN_INT (y);
1156
1157   /* Select dst format based on mode.  */
1158   if (mode == DImode && (! TARGET_LITTLE_END))
1159     dst_fmt = "%R0";
1160   else
1161     dst_fmt = "%0";
1162
1163   if (x >= 0 && x <= 127)
1164     sprintf (load_op, "movi\t%s,%%1", dst_fmt);
1165   
1166   /* Try exact power of two.  */
1167   else if ((x & (x - 1)) == 0)
1168     sprintf (load_op, "bgeni\t%s,%%P1", dst_fmt);
1169   
1170   /* Try exact power of two - 1.  */
1171   else if ((x & (x + 1)) == 0)
1172     sprintf (load_op, "bmaski\t%s,%%N1", dst_fmt);
1173   
1174   else 
1175     sprintf (load_op, "BADMOVI\t%s,%%1", dst_fmt);
1176
1177   switch (trick_no)
1178     {
1179     case 1:
1180       strcpy (buf, load_op);
1181       break;
1182     case 2:   /* not */
1183       sprintf (buf, "%s\n\tnot\t%s\t// %d 0x%x", load_op, dst_fmt, value, value);
1184       break;
1185     case 3:   /* add */
1186       sprintf (buf, "%s\n\taddi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1187       break;
1188     case 4:   /* sub */
1189       sprintf (buf, "%s\n\tsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1190       break;
1191     case 5:   /* rsub */
1192       /* Never happens unless -mrsubi, see try_constant_tricks().  */
1193       sprintf (buf, "%s\n\trsubi\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1194       break;
1195     case 6:   /* bset */
1196       sprintf (buf, "%s\n\tbseti\t%s,%%P2\t// %d 0x%x", load_op, dst_fmt, value, value);
1197       break;
1198     case 7:   /* bclr */
1199       sprintf (buf, "%s\n\tbclri\t%s,%%Q2\t// %d 0x%x", load_op, dst_fmt, value, value);
1200       break;
1201     case 8:   /* rotl */
1202       sprintf (buf, "%s\n\trotli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1203       break;
1204     case 9:   /* lsl */
1205       sprintf (buf, "%s\n\tlsli\t%s,%%2\t// %d 0x%x", load_op, dst_fmt, value, value);
1206       break;
1207     case 10:  /* ixh */
1208       sprintf (buf, "%s\n\tixh\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1209       break;
1210     case 11:  /* ixw */
1211       sprintf (buf, "%s\n\tixw\t%s,%s\t// %d 0x%x", load_op, dst_fmt, dst_fmt, value, value);
1212       break;
1213     default:
1214       return "";
1215     }
1216   
1217   output_asm_insn (buf, out_operands);
1218
1219   return "";
1220 }
1221
1222 /* Output a move of a word or less value.  */
1223
1224 const char *
1225 mcore_output_move (rtx insn ATTRIBUTE_UNUSED, rtx operands[],
1226                    enum machine_mode mode ATTRIBUTE_UNUSED)
1227 {
1228   rtx dst = operands[0];
1229   rtx src = operands[1];
1230
1231   if (GET_CODE (dst) == REG)
1232     {
1233       if (GET_CODE (src) == REG)
1234         {               
1235           if (REGNO (src) == CC_REG)            /* r-c */
1236             return "mvc\t%0"; 
1237           else 
1238             return "mov\t%0,%1";                /* r-r*/
1239         }
1240       else if (GET_CODE (src) == MEM)
1241         {
1242           if (GET_CODE (XEXP (src, 0)) == LABEL_REF) 
1243             return "lrw\t%0,[%1]";              /* a-R */
1244           else
1245             switch (GET_MODE (src))             /* r-m */
1246               {
1247               case SImode:
1248                 return "ldw\t%0,%1";
1249               case HImode:
1250                 return "ld.h\t%0,%1";
1251               case QImode:
1252                 return "ld.b\t%0,%1";
1253               default:
1254                 abort ();
1255               }
1256         }
1257       else if (GET_CODE (src) == CONST_INT)
1258         {
1259           int x, y;
1260           
1261           if (CONST_OK_FOR_I (INTVAL (src)))       /* r-I */
1262             return "movi\t%0,%1";
1263           else if (CONST_OK_FOR_M (INTVAL (src)))  /* r-M */
1264             return "bgeni\t%0,%P1\t// %1 %x1";
1265           else if (CONST_OK_FOR_N (INTVAL (src)))  /* r-N */
1266             return "bmaski\t%0,%N1\t// %1 %x1";
1267           else if (try_constant_tricks (INTVAL (src), &x, &y))     /* R-P */
1268             return output_inline_const (SImode, operands);  /* 1-2 insns */
1269           else 
1270             return "lrw\t%0,%x1\t// %1";        /* Get it from literal pool.  */
1271         }
1272       else
1273         return "lrw\t%0, %1";                /* Into the literal pool.  */
1274     }
1275   else if (GET_CODE (dst) == MEM)               /* m-r */
1276     switch (GET_MODE (dst))
1277       {
1278       case SImode:
1279         return "stw\t%1,%0";
1280       case HImode:
1281         return "st.h\t%1,%0";
1282       case QImode:
1283         return "st.b\t%1,%0";
1284       default:
1285         abort ();
1286       }
1287
1288   abort ();
1289 }
1290
1291 /* Return a sequence of instructions to perform DI or DF move.
1292    Since the MCORE cannot move a DI or DF in one instruction, we have
1293    to take care when we see overlapping source and dest registers.  */
1294
1295 const char *
1296 mcore_output_movedouble (rtx operands[], enum machine_mode mode ATTRIBUTE_UNUSED)
1297 {
1298   rtx dst = operands[0];
1299   rtx src = operands[1];
1300
1301   if (GET_CODE (dst) == REG)
1302     {
1303       if (GET_CODE (src) == REG)
1304         {
1305           int dstreg = REGNO (dst);
1306           int srcreg = REGNO (src);
1307           
1308           /* Ensure the second source not overwritten.  */
1309           if (srcreg + 1 == dstreg)
1310             return "mov %R0,%R1\n\tmov  %0,%1";
1311           else
1312             return "mov %0,%1\n\tmov    %R0,%R1";
1313         }
1314       else if (GET_CODE (src) == MEM)
1315         {
1316           rtx memexp = memexp = XEXP (src, 0);
1317           int dstreg = REGNO (dst);
1318           int basereg = -1;
1319           
1320           if (GET_CODE (memexp) == LABEL_REF)
1321             return "lrw\t%0,[%1]\n\tlrw\t%R0,[%R1]";
1322           else if (GET_CODE (memexp) == REG) 
1323             basereg = REGNO (memexp);
1324           else if (GET_CODE (memexp) == PLUS)
1325             {
1326               if (GET_CODE (XEXP (memexp, 0)) == REG)
1327                 basereg = REGNO (XEXP (memexp, 0));
1328               else if (GET_CODE (XEXP (memexp, 1)) == REG)
1329                 basereg = REGNO (XEXP (memexp, 1));
1330               else
1331                 abort ();
1332             }
1333           else
1334             abort ();
1335
1336           /* ??? length attribute is wrong here.  */
1337           if (dstreg == basereg)
1338             {
1339               /* Just load them in reverse order.  */
1340               return "ldw\t%R0,%R1\n\tldw\t%0,%1";
1341               
1342               /* XXX: alternative: move basereg to basereg+1
1343                  and then fall through.  */
1344             }
1345           else
1346             return "ldw\t%0,%1\n\tldw\t%R0,%R1";
1347         }
1348       else if (GET_CODE (src) == CONST_INT)
1349         {
1350           if (TARGET_LITTLE_END)
1351             {
1352               if (CONST_OK_FOR_I (INTVAL (src)))
1353                 output_asm_insn ("movi  %0,%1", operands);
1354               else if (CONST_OK_FOR_M (INTVAL (src)))
1355                 output_asm_insn ("bgeni %0,%P1", operands);
1356               else if (INTVAL (src) == -1)
1357                 output_asm_insn ("bmaski        %0,32", operands);
1358               else if (CONST_OK_FOR_N (INTVAL (src)))
1359                 output_asm_insn ("bmaski        %0,%N1", operands);
1360               else
1361                 abort ();
1362
1363               if (INTVAL (src) < 0)
1364                 return "bmaski  %R0,32";
1365               else
1366                 return "movi    %R0,0";
1367             }
1368           else
1369             {
1370               if (CONST_OK_FOR_I (INTVAL (src)))
1371                 output_asm_insn ("movi  %R0,%1", operands);
1372               else if (CONST_OK_FOR_M (INTVAL (src)))
1373                 output_asm_insn ("bgeni %R0,%P1", operands);
1374               else if (INTVAL (src) == -1)
1375                 output_asm_insn ("bmaski        %R0,32", operands);
1376               else if (CONST_OK_FOR_N (INTVAL (src)))
1377                 output_asm_insn ("bmaski        %R0,%N1", operands);
1378               else
1379                 abort ();
1380               
1381               if (INTVAL (src) < 0)
1382                 return "bmaski  %0,32";
1383               else
1384                 return "movi    %0,0";
1385             }
1386         }
1387       else
1388         abort ();
1389     }
1390   else if (GET_CODE (dst) == MEM && GET_CODE (src) == REG)
1391     return "stw\t%1,%0\n\tstw\t%R1,%R0";
1392   else
1393     abort ();
1394 }
1395
1396 /* Predicates used by the templates.  */
1397
1398 /* Nonzero if OP can be source of a simple move operation.  */
1399
1400 int
1401 mcore_general_movsrc_operand (rtx op, enum machine_mode mode)
1402 {
1403   /* Any (MEM LABEL_REF) is OK.  That is a pc-relative load.  */
1404   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == LABEL_REF)
1405     return 1;
1406
1407   return general_operand (op, mode);
1408 }
1409
1410 /* Nonzero if OP can be destination of a simple move operation.  */
1411
1412 int
1413 mcore_general_movdst_operand (rtx op, enum machine_mode mode)
1414 {
1415   if (GET_CODE (op) == REG && REGNO (op) == CC_REG)
1416     return 0;
1417   
1418   return general_operand (op, mode);
1419 }
1420
1421 /* Nonzero if OP is a normal arithmetic register.  */
1422
1423 int
1424 mcore_arith_reg_operand (rtx op, enum machine_mode mode)
1425 {
1426   if (! register_operand (op, mode))
1427     return 0;
1428
1429   if (GET_CODE (op) == SUBREG)
1430     op = SUBREG_REG (op);
1431
1432   if (GET_CODE (op) == REG)
1433     return REGNO (op) != CC_REG;
1434
1435   return 1;
1436 }
1437
1438 /* Nonzero if OP should be recognized during reload for an ixh/ixw
1439    operand.  See the ixh/ixw patterns.  */
1440
1441 int
1442 mcore_reload_operand (rtx op, enum machine_mode mode)
1443 {
1444   if (mcore_arith_reg_operand (op, mode))
1445     return 1;
1446
1447   if (! reload_in_progress)
1448     return 0;
1449
1450   return GET_CODE (op) == MEM;
1451 }
1452
1453 /* Nonzero if OP is a valid source operand for an arithmetic insn.  */
1454
1455 int
1456 mcore_arith_J_operand (rtx op, enum machine_mode mode)
1457 {
1458   if (register_operand (op, mode))
1459     return 1;
1460
1461   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_J (INTVAL (op)))
1462     return 1;
1463   
1464   return 0;
1465 }
1466
1467 /* Nonzero if OP is a valid source operand for an arithmetic insn.  */
1468
1469 int
1470 mcore_arith_K_operand (rtx op, enum machine_mode mode)
1471 {
1472   if (register_operand (op, mode))
1473     return 1;
1474
1475   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1476     return 1;
1477
1478   return 0;
1479 }
1480
1481 /* Nonzero if OP is a valid source operand for a shift or rotate insn.  */
1482
1483 int
1484 mcore_arith_K_operand_not_0 (rtx op, enum machine_mode mode)
1485 {
1486   if (register_operand (op, mode))
1487     return 1;
1488
1489   if (   GET_CODE (op) == CONST_INT
1490       && CONST_OK_FOR_K (INTVAL (op))
1491       && INTVAL (op) != 0)
1492     return 1;
1493
1494   return 0;
1495 }
1496
1497 int
1498 mcore_arith_K_S_operand (rtx op, enum machine_mode mode)
1499 {
1500   if (register_operand (op, mode))
1501     return 1;
1502
1503   if (GET_CODE (op) == CONST_INT)
1504     {
1505       if (CONST_OK_FOR_K (INTVAL (op)) || CONST_OK_FOR_M (~INTVAL (op)))
1506         return 1;
1507     }
1508   
1509   return 0;
1510 }
1511
1512 int
1513 mcore_arith_S_operand (rtx op)
1514 {
1515   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (~INTVAL (op)))
1516     return 1;
1517   
1518   return 0;
1519 }
1520
1521 int
1522 mcore_arith_M_operand (rtx op, enum machine_mode mode)
1523 {
1524   if (register_operand (op, mode))
1525     return 1;
1526
1527   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_M (INTVAL (op)))
1528     return 1;
1529
1530   return 0;
1531 }
1532
1533 /* Nonzero if OP is a valid source operand for loading.  */
1534
1535 int
1536 mcore_arith_imm_operand (rtx op, enum machine_mode mode)
1537 {
1538   if (register_operand (op, mode))
1539     return 1;
1540
1541   if (GET_CODE (op) == CONST_INT && const_ok_for_mcore (INTVAL (op)))
1542     return 1;
1543
1544   return 0;
1545 }
1546
1547 int
1548 mcore_arith_any_imm_operand (rtx op, enum machine_mode mode)
1549 {
1550   if (register_operand (op, mode))
1551     return 1;
1552
1553   if (GET_CODE (op) == CONST_INT)
1554     return 1;
1555
1556   return 0;
1557 }
1558
1559 /* Nonzero if OP is a valid source operand for a cmov with two consts +/- 1.  */
1560
1561 int
1562 mcore_arith_O_operand (rtx op, enum machine_mode mode)
1563 {
1564   if (register_operand (op, mode))
1565     return 1;
1566
1567   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_O (INTVAL (op)))
1568     return 1;
1569   
1570   return 0;
1571 }
1572
1573 /* Nonzero if OP is a valid source operand for a btsti.  */
1574
1575 int
1576 mcore_literal_K_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1577 {
1578   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_K (INTVAL (op)))
1579     return 1;
1580
1581   return 0;
1582 }
1583
1584 /* Nonzero if OP is a valid source operand for an add/sub insn.  */
1585
1586 int
1587 mcore_addsub_operand (rtx op, enum machine_mode mode)
1588 {
1589   if (register_operand (op, mode))
1590     return 1;
1591
1592   if (GET_CODE (op) == CONST_INT)
1593     {
1594       return 1;
1595       
1596       /* The following is removed because it precludes large constants from being
1597          returned as valid source operands for and add/sub insn.  While large 
1598          constants may not directly be used in an add/sub, they may if first loaded
1599          into a register.  Thus, this predicate should indicate that they are valid,
1600          and the constraint in mcore.md should control whether an additional load to
1601          register is needed. (see mcore.md, addsi). -- DAC 4/2/1998  */
1602       /*
1603         if (CONST_OK_FOR_J(INTVAL(op)) || CONST_OK_FOR_L(INTVAL(op)))
1604           return 1;
1605       */
1606     }
1607   
1608   return 0;
1609 }
1610
1611 /* Nonzero if OP is a valid source operand for a compare operation.  */
1612
1613 int
1614 mcore_compare_operand (rtx op, enum machine_mode mode)
1615 {
1616   if (register_operand (op, mode))
1617     return 1;
1618
1619   if (GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
1620     return 1;
1621   
1622   return 0;
1623 }
1624
1625 /* Expand insert bit field.  BRC  */
1626
1627 int
1628 mcore_expand_insv (rtx operands[])
1629 {
1630   int width = INTVAL (operands[1]);
1631   int posn = INTVAL (operands[2]);
1632   int mask;
1633   rtx mreg, sreg, ereg;
1634
1635   /* To get width 1 insv, the test in store_bit_field() (expmed.c, line 191)
1636      for width==1 must be removed.  Look around line 368.  This is something
1637      we really want the md part to do.  */
1638   if (width == 1 && GET_CODE (operands[3]) == CONST_INT)
1639     {
1640       /* Do directly with bseti or bclri.  */
1641       /* RBE: 2/97 consider only low bit of constant.  */
1642       if ((INTVAL(operands[3])&1) == 0)
1643         {
1644           mask = ~(1 << posn);
1645           emit_insn (gen_rtx_SET (SImode, operands[0],
1646                               gen_rtx_AND (SImode, operands[0], GEN_INT (mask))));
1647         }
1648       else
1649         {
1650           mask = 1 << posn;
1651           emit_insn (gen_rtx_SET (SImode, operands[0],
1652                             gen_rtx_IOR (SImode, operands[0], GEN_INT (mask))));
1653         }
1654       
1655       return 1;
1656     }
1657
1658   /* Look at some bit-field placements that we aren't interested
1659      in handling ourselves, unless specifically directed to do so.  */
1660   if (! TARGET_W_FIELD)
1661     return 0;           /* Generally, give up about now.  */
1662
1663   if (width == 8 && posn % 8 == 0)
1664     /* Byte sized and aligned; let caller break it up.  */
1665     return 0;
1666   
1667   if (width == 16 && posn % 16 == 0)
1668     /* Short sized and aligned; let caller break it up.  */
1669     return 0;
1670
1671   /* The general case - we can do this a little bit better than what the
1672      machine independent part tries.  This will get rid of all the subregs
1673      that mess up constant folding in combine when working with relaxed
1674      immediates.  */
1675
1676   /* If setting the entire field, do it directly.  */
1677   if (GET_CODE (operands[3]) == CONST_INT && 
1678       INTVAL (operands[3]) == ((1 << width) - 1))
1679     {
1680       mreg = force_reg (SImode, GEN_INT (INTVAL (operands[3]) << posn));
1681       emit_insn (gen_rtx_SET (SImode, operands[0],
1682                          gen_rtx_IOR (SImode, operands[0], mreg)));
1683       return 1;
1684     }
1685
1686   /* Generate the clear mask.  */
1687   mreg = force_reg (SImode, GEN_INT (~(((1 << width) - 1) << posn)));
1688
1689   /* Clear the field, to overlay it later with the source.  */
1690   emit_insn (gen_rtx_SET (SImode, operands[0], 
1691                       gen_rtx_AND (SImode, operands[0], mreg)));
1692
1693   /* If the source is constant 0, we've nothing to add back.  */
1694   if (GET_CODE (operands[3]) == CONST_INT && INTVAL (operands[3]) == 0)
1695     return 1;
1696
1697   /* XXX: Should we worry about more games with constant values?
1698      We've covered the high profile: set/clear single-bit and many-bit
1699      fields. How often do we see "arbitrary bit pattern" constants?  */
1700   sreg = copy_to_mode_reg (SImode, operands[3]);
1701
1702   /* Extract src as same width as dst (needed for signed values).  We
1703      always have to do this since we widen everything to SImode.
1704      We don't have to mask if we're shifting this up against the
1705      MSB of the register (e.g., the shift will push out any hi-order
1706      bits.  */
1707   if (width + posn != (int) GET_MODE_SIZE (SImode))
1708     {
1709       ereg = force_reg (SImode, GEN_INT ((1 << width) - 1));      
1710       emit_insn (gen_rtx_SET (SImode, sreg,
1711                           gen_rtx_AND (SImode, sreg, ereg)));
1712     }
1713
1714   /* Insert source value in dest.  */
1715   if (posn != 0)
1716     emit_insn (gen_rtx_SET (SImode, sreg,
1717                         gen_rtx_ASHIFT (SImode, sreg, GEN_INT (posn))));
1718   
1719   emit_insn (gen_rtx_SET (SImode, operands[0],
1720                       gen_rtx_IOR (SImode, operands[0], sreg)));
1721
1722   return 1;
1723 }
1724
1725 /* Return 1 if OP is a load multiple operation.  It is known to be a
1726    PARALLEL and the first section will be tested.  */
1727
1728 int
1729 mcore_load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1730 {
1731   int count = XVECLEN (op, 0);
1732   int dest_regno;
1733   rtx src_addr;
1734   int i;
1735
1736   /* Perform a quick check so we don't blow up below.  */
1737   if (count <= 1
1738       || GET_CODE (XVECEXP (op, 0, 0)) != SET
1739       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
1740       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
1741     return 0;
1742
1743   dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
1744   src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
1745
1746   for (i = 1; i < count; i++)
1747     {
1748       rtx elt = XVECEXP (op, 0, i);
1749
1750       if (GET_CODE (elt) != SET
1751           || GET_CODE (SET_DEST (elt)) != REG
1752           || GET_MODE (SET_DEST (elt)) != SImode
1753           || REGNO (SET_DEST (elt))    != (unsigned) (dest_regno + i)
1754           || GET_CODE (SET_SRC (elt))  != MEM
1755           || GET_MODE (SET_SRC (elt))  != SImode
1756           || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
1757           || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
1758           || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
1759           || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
1760         return 0;
1761     }
1762
1763   return 1;
1764 }
1765
1766 /* Similar, but tests for store multiple.  */
1767
1768 int
1769 mcore_store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1770 {
1771   int count = XVECLEN (op, 0);
1772   int src_regno;
1773   rtx dest_addr;
1774   int i;
1775
1776   /* Perform a quick check so we don't blow up below.  */
1777   if (count <= 1
1778       || GET_CODE (XVECEXP (op, 0, 0)) != SET
1779       || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
1780       || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
1781     return 0;
1782
1783   src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
1784   dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
1785
1786   for (i = 1; i < count; i++)
1787     {
1788       rtx elt = XVECEXP (op, 0, i);
1789
1790       if (GET_CODE (elt) != SET
1791           || GET_CODE (SET_SRC (elt)) != REG
1792           || GET_MODE (SET_SRC (elt)) != SImode
1793           || REGNO (SET_SRC (elt)) != (unsigned) (src_regno + i)
1794           || GET_CODE (SET_DEST (elt)) != MEM
1795           || GET_MODE (SET_DEST (elt)) != SImode
1796           || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
1797           || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
1798           || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
1799           || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
1800         return 0;
1801     }
1802
1803   return 1;
1804 }
1805 \f
1806 /* ??? Block move stuff stolen from m88k.  This code has not been
1807    verified for correctness.  */
1808
1809 /* Emit code to perform a block move.  Choose the best method.
1810
1811    OPERANDS[0] is the destination.
1812    OPERANDS[1] is the source.
1813    OPERANDS[2] is the size.
1814    OPERANDS[3] is the alignment safe to use.  */
1815
1816 /* Emit code to perform a block move with an offset sequence of ldw/st
1817    instructions (..., ldw 0, stw 1, ldw 1, stw 0, ...).  SIZE and ALIGN are
1818    known constants.  DEST and SRC are registers.  OFFSET is the known
1819    starting point for the output pattern.  */
1820
1821 static const enum machine_mode mode_from_align[] =
1822 {
1823   VOIDmode, QImode, HImode, VOIDmode, SImode,
1824   VOIDmode, VOIDmode, VOIDmode, DImode
1825 };
1826
1827 static void
1828 block_move_sequence (rtx dest, rtx dst_mem, rtx src, rtx src_mem,
1829                      int size, int align, int offset)
1830 {
1831   rtx temp[2];
1832   enum machine_mode mode[2];
1833   int amount[2];
1834   int active[2];
1835   int phase = 0;
1836   int next;
1837   int offset_ld = offset;
1838   int offset_st = offset;
1839
1840   active[0] = active[1] = FALSE;
1841
1842   /* Establish parameters for the first load and for the second load if
1843      it is known to be the same mode as the first.  */
1844   amount[0] = amount[1] = align;
1845
1846   mode[0] = mode_from_align[align];
1847
1848   temp[0] = gen_reg_rtx (mode[0]);
1849   
1850   if (size >= 2 * align)
1851     {
1852       mode[1] = mode[0];
1853       temp[1] = gen_reg_rtx (mode[1]);
1854     }
1855
1856   do
1857     {
1858       rtx srcp, dstp;
1859       
1860       next = phase;
1861       phase = !phase;
1862
1863       if (size > 0)
1864         {
1865           /* Change modes as the sequence tails off.  */
1866           if (size < amount[next])
1867             {
1868               amount[next] = (size >= 4 ? 4 : (size >= 2 ? 2 : 1));
1869               mode[next] = mode_from_align[amount[next]];
1870               temp[next] = gen_reg_rtx (mode[next]);
1871             }
1872           
1873           size -= amount[next];
1874           srcp = gen_rtx_MEM (
1875 #if 0
1876                           MEM_IN_STRUCT_P (src_mem) ? mode[next] : BLKmode,
1877 #else
1878                           mode[next],
1879 #endif
1880                           gen_rtx_PLUS (Pmode, src, GEN_INT (offset_ld)));
1881           
1882           RTX_UNCHANGING_P (srcp) = RTX_UNCHANGING_P (src_mem);
1883           MEM_VOLATILE_P (srcp) = MEM_VOLATILE_P (src_mem);
1884           MEM_IN_STRUCT_P (srcp) = 1;
1885           emit_insn (gen_rtx_SET (VOIDmode, temp[next], srcp));
1886           offset_ld += amount[next];
1887           active[next] = TRUE;
1888         }
1889
1890       if (active[phase])
1891         {
1892           active[phase] = FALSE;
1893           
1894           dstp = gen_rtx_MEM (
1895 #if 0
1896                           MEM_IN_STRUCT_P (dst_mem) ? mode[phase] : BLKmode,
1897 #else
1898                           mode[phase],
1899 #endif
1900                           gen_rtx_PLUS (Pmode, dest, GEN_INT (offset_st)));
1901           
1902           RTX_UNCHANGING_P (dstp) = RTX_UNCHANGING_P (dst_mem);
1903           MEM_VOLATILE_P (dstp) = MEM_VOLATILE_P (dst_mem);
1904           MEM_IN_STRUCT_P (dstp) = 1;
1905           emit_insn (gen_rtx_SET (VOIDmode, dstp, temp[phase]));
1906           offset_st += amount[phase];
1907         }
1908     }
1909   while (active[next]);
1910 }
1911
1912 void
1913 mcore_expand_block_move (rtx dst_mem, rtx src_mem, rtx * operands)
1914 {
1915   int align = INTVAL (operands[3]);
1916   int bytes;
1917
1918   if (GET_CODE (operands[2]) == CONST_INT)
1919     {
1920       bytes = INTVAL (operands[2]);
1921       
1922       if (bytes <= 0)
1923         return;
1924       if (align > 4)
1925         align = 4;
1926       
1927       /* RBE: bumped 1 and 2 byte align from 1 and 2 to 4 and 8 bytes before
1928          we give up and go to memcpy.  */
1929       if ((align == 4 && (bytes <= 4*4
1930                           || ((bytes & 01) == 0 && bytes <= 8*4)
1931                           || ((bytes & 03) == 0 && bytes <= 16*4)))
1932           || (align == 2 && bytes <= 4*2)
1933           || (align == 1 && bytes <= 4*1))
1934         {
1935           block_move_sequence (operands[0], dst_mem, operands[1], src_mem,
1936                                bytes, align, 0);
1937           return;
1938         }
1939     }
1940
1941   /* If we get here, just use the library routine.  */
1942   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "memcpy"), 0, VOIDmode, 3,
1943                      operands[0], Pmode, operands[1], Pmode, operands[2],
1944                      SImode);
1945 }
1946 \f
1947
1948 /* Code to generate prologue and epilogue sequences.  */
1949 static int number_of_regs_before_varargs;
1950
1951 /* Set by TARGET_SETUP_INCOMING_VARARGS to indicate to prolog that this is
1952    for a varargs function.  */
1953 static int current_function_anonymous_args;
1954
1955 #define STACK_BYTES (STACK_BOUNDARY/BITS_PER_UNIT)
1956 #define STORE_REACH (64)        /* Maximum displace of word store + 4.  */
1957 #define ADDI_REACH (32)         /* Maximum addi operand.  */
1958
1959 static void
1960 layout_mcore_frame (struct mcore_frame * infp)
1961 {
1962   int n;
1963   unsigned int i;
1964   int nbytes;
1965   int regarg;
1966   int localregarg;
1967   int localreg;
1968   int outbounds;
1969   unsigned int growths;
1970   int step;
1971
1972   /* Might have to spill bytes to re-assemble a big argument that
1973      was passed partially in registers and partially on the stack.  */
1974   nbytes = current_function_pretend_args_size;
1975   
1976   /* Determine how much space for spilled anonymous args (e.g., stdarg).  */
1977   if (current_function_anonymous_args)
1978     nbytes += (NPARM_REGS - number_of_regs_before_varargs) * UNITS_PER_WORD;
1979   
1980   infp->arg_size = nbytes;
1981
1982   /* How much space to save non-volatile registers we stomp.  */
1983   infp->reg_mask = calc_live_regs (& n);
1984   infp->reg_size = n * 4;
1985
1986   /* And the rest of it... locals and space for overflowed outbounds.  */
1987   infp->local_size = get_frame_size ();
1988   infp->outbound_size = current_function_outgoing_args_size;
1989
1990   /* Make sure we have a whole number of words for the locals.  */
1991   if (infp->local_size % STACK_BYTES)
1992     infp->local_size = (infp->local_size + STACK_BYTES - 1) & ~ (STACK_BYTES -1);
1993   
1994   /* Only thing we know we have to pad is the outbound space, since
1995      we've aligned our locals assuming that base of locals is aligned.  */
1996   infp->pad_local = 0;
1997   infp->pad_reg = 0;
1998   infp->pad_outbound = 0;
1999   if (infp->outbound_size % STACK_BYTES)
2000     infp->pad_outbound = STACK_BYTES - (infp->outbound_size % STACK_BYTES);
2001
2002   /* Now we see how we want to stage the prologue so that it does
2003      the most appropriate stack growth and register saves to either:
2004      (1) run fast,
2005      (2) reduce instruction space, or
2006      (3) reduce stack space.  */
2007   for (i = 0; i < ARRAY_SIZE (infp->growth); i++)
2008     infp->growth[i] = 0;
2009
2010   regarg      = infp->reg_size + infp->arg_size;
2011   localregarg = infp->local_size + regarg;
2012   localreg    = infp->local_size + infp->reg_size;
2013   outbounds   = infp->outbound_size + infp->pad_outbound;
2014   growths     = 0;
2015
2016   /* XXX: Consider one where we consider localregarg + outbound too! */
2017
2018   /* Frame of <= 32 bytes and using stm would get <= 2 registers.
2019      use stw's with offsets and buy the frame in one shot.  */
2020   if (localregarg <= ADDI_REACH
2021       && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2022     {
2023       /* Make sure we'll be aligned.  */
2024       if (localregarg % STACK_BYTES)
2025         infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2026
2027       step = localregarg + infp->pad_reg;
2028       infp->reg_offset = infp->local_size;
2029       
2030       if (outbounds + step <= ADDI_REACH && !frame_pointer_needed)
2031         {
2032           step += outbounds;
2033           infp->reg_offset += outbounds;
2034           outbounds = 0;
2035         }
2036       
2037       infp->arg_offset = step - 4;
2038       infp->growth[growths++] = step;
2039       infp->reg_growth = growths;
2040       infp->local_growth = growths;
2041       
2042       /* If we haven't already folded it in.  */
2043       if (outbounds)
2044         infp->growth[growths++] = outbounds;
2045       
2046       goto finish;
2047     }
2048
2049   /* Frame can't be done with a single subi, but can be done with 2
2050      insns.  If the 'stm' is getting <= 2 registers, we use stw's and
2051      shift some of the stack purchase into the first subi, so both are
2052      single instructions.  */
2053   if (localregarg <= STORE_REACH
2054       && (infp->local_size > ADDI_REACH)
2055       && (infp->reg_size <= 8 || (infp->reg_mask & 0xc000) != 0xc000))
2056     {
2057       int all;
2058
2059       /* Make sure we'll be aligned; use either pad_reg or pad_local.  */
2060       if (localregarg % STACK_BYTES)
2061         infp->pad_reg = STACK_BYTES - (localregarg % STACK_BYTES);
2062
2063       all = localregarg + infp->pad_reg + infp->pad_local;
2064       step = ADDI_REACH;        /* As much up front as we can.  */
2065       if (step > all)
2066         step = all;
2067       
2068       /* XXX: Consider whether step will still be aligned; we believe so.  */
2069       infp->arg_offset = step - 4;
2070       infp->growth[growths++] = step;
2071       infp->reg_growth = growths;
2072       infp->reg_offset = step - infp->pad_reg - infp->reg_size;
2073       all -= step;
2074
2075       /* Can we fold in any space required for outbounds?  */
2076       if (outbounds + all <= ADDI_REACH && !frame_pointer_needed)
2077         {
2078           all += outbounds;
2079           outbounds = 0;
2080         }
2081
2082       /* Get the rest of the locals in place.  */
2083       step = all;
2084       infp->growth[growths++] = step;
2085       infp->local_growth = growths;
2086       all -= step;
2087
2088       assert (all == 0);
2089
2090       /* Finish off if we need to do so.  */
2091       if (outbounds)
2092         infp->growth[growths++] = outbounds;
2093       
2094       goto finish;
2095     }
2096
2097   /* Registers + args is nicely aligned, so we'll buy that in one shot.
2098      Then we buy the rest of the frame in 1 or 2 steps depending on
2099      whether we need a frame pointer.  */
2100   if ((regarg % STACK_BYTES) == 0)
2101     {
2102       infp->growth[growths++] = regarg;
2103       infp->reg_growth = growths;
2104       infp->arg_offset = regarg - 4;
2105       infp->reg_offset = 0;
2106
2107       if (infp->local_size % STACK_BYTES)
2108         infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2109       
2110       step = infp->local_size + infp->pad_local;
2111       
2112       if (!frame_pointer_needed)
2113         {
2114           step += outbounds;
2115           outbounds = 0;
2116         }
2117       
2118       infp->growth[growths++] = step;
2119       infp->local_growth = growths;
2120
2121       /* If there's any left to be done.  */
2122       if (outbounds)
2123         infp->growth[growths++] = outbounds;
2124       
2125       goto finish;
2126     }
2127
2128   /* XXX: optimizations that we'll want to play with....
2129      -- regarg is not aligned, but it's a small number of registers;
2130         use some of localsize so that regarg is aligned and then 
2131         save the registers.  */
2132
2133   /* Simple encoding; plods down the stack buying the pieces as it goes.
2134      -- does not optimize space consumption.
2135      -- does not attempt to optimize instruction counts.
2136      -- but it is safe for all alignments.  */
2137   if (regarg % STACK_BYTES != 0)
2138     infp->pad_reg = STACK_BYTES - (regarg % STACK_BYTES);
2139   
2140   infp->growth[growths++] = infp->arg_size + infp->reg_size + infp->pad_reg;
2141   infp->reg_growth = growths;
2142   infp->arg_offset = infp->growth[0] - 4;
2143   infp->reg_offset = 0;
2144   
2145   if (frame_pointer_needed)
2146     {
2147       if (infp->local_size % STACK_BYTES != 0)
2148         infp->pad_local = STACK_BYTES - (infp->local_size % STACK_BYTES);
2149       
2150       infp->growth[growths++] = infp->local_size + infp->pad_local;
2151       infp->local_growth = growths;
2152       
2153       infp->growth[growths++] = outbounds;
2154     }
2155   else
2156     {
2157       if ((infp->local_size + outbounds) % STACK_BYTES != 0)
2158         infp->pad_local = STACK_BYTES - ((infp->local_size + outbounds) % STACK_BYTES);
2159       
2160       infp->growth[growths++] = infp->local_size + infp->pad_local + outbounds;
2161       infp->local_growth = growths;
2162     }
2163
2164   /* Anything else that we've forgotten?, plus a few consistency checks.  */
2165  finish:
2166   assert (infp->reg_offset >= 0);
2167   assert (growths <= MAX_STACK_GROWS);
2168   
2169   for (i = 0; i < growths; i++)
2170     {
2171       if (infp->growth[i] % STACK_BYTES)
2172         {
2173           fprintf (stderr,"stack growth of %d is not %d aligned\n",
2174                    infp->growth[i], STACK_BYTES);
2175           abort ();
2176         }
2177     }
2178 }
2179
2180 /* Define the offset between two registers, one to be eliminated, and
2181    the other its replacement, at the start of a routine.  */
2182
2183 int
2184 mcore_initial_elimination_offset (int from, int to)
2185 {
2186   int above_frame;
2187   int below_frame;
2188   struct mcore_frame fi;
2189
2190   layout_mcore_frame (& fi);
2191
2192   /* fp to ap */
2193   above_frame = fi.local_size + fi.pad_local + fi.reg_size + fi.pad_reg;
2194   /* sp to fp */
2195   below_frame = fi.outbound_size + fi.pad_outbound;
2196
2197   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
2198     return above_frame;
2199
2200   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2201     return above_frame + below_frame;
2202
2203   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2204     return below_frame;
2205
2206   abort ();
2207
2208   return 0;
2209 }
2210
2211 /* Keep track of some information about varargs for the prolog.  */
2212
2213 static void
2214 mcore_setup_incoming_varargs (CUMULATIVE_ARGS *args_so_far,
2215                               enum machine_mode mode, tree type,
2216                               int * ptr_pretend_size ATTRIBUTE_UNUSED,
2217                               int second_time ATTRIBUTE_UNUSED)
2218 {
2219   current_function_anonymous_args = 1;
2220
2221   /* We need to know how many argument registers are used before
2222      the varargs start, so that we can push the remaining argument
2223      registers during the prologue.  */
2224   number_of_regs_before_varargs = *args_so_far + mcore_num_arg_regs (mode, type);
2225   
2226   /* There is a bug somewhere in the arg handling code.
2227      Until I can find it this workaround always pushes the
2228      last named argument onto the stack.  */
2229   number_of_regs_before_varargs = *args_so_far;
2230   
2231   /* The last named argument may be split between argument registers
2232      and the stack.  Allow for this here.  */
2233   if (number_of_regs_before_varargs > NPARM_REGS)
2234     number_of_regs_before_varargs = NPARM_REGS;
2235 }
2236
2237 void
2238 mcore_expand_prolog (void)
2239 {
2240   struct mcore_frame fi;
2241   int space_allocated = 0;
2242   int growth = 0;
2243
2244   /* Find out what we're doing.  */
2245   layout_mcore_frame (&fi);
2246   
2247   space_allocated = fi.arg_size + fi.reg_size + fi.local_size +
2248     fi.outbound_size + fi.pad_outbound + fi.pad_local + fi.pad_reg;
2249
2250   if (TARGET_CG_DATA)
2251     {
2252       /* Emit a symbol for this routine's frame size.  */
2253       rtx x;
2254
2255       x = DECL_RTL (current_function_decl);
2256       
2257       if (GET_CODE (x) != MEM)
2258         abort ();
2259       
2260       x = XEXP (x, 0);
2261       
2262       if (GET_CODE (x) != SYMBOL_REF)
2263         abort ();
2264       
2265       if (mcore_current_function_name)
2266         free (mcore_current_function_name);
2267       
2268       mcore_current_function_name = xstrdup (XSTR (x, 0));
2269       
2270       ASM_OUTPUT_CG_NODE (asm_out_file, mcore_current_function_name, space_allocated);
2271
2272       if (current_function_calls_alloca)
2273         ASM_OUTPUT_CG_EDGE (asm_out_file, mcore_current_function_name, "alloca", 1);
2274
2275       /* 970425: RBE:
2276          We're looking at how the 8byte alignment affects stack layout
2277          and where we had to pad things. This emits information we can
2278          extract which tells us about frame sizes and the like.  */
2279       fprintf (asm_out_file,
2280                "\t.equ\t__$frame$info$_%s_$_%d_%d_x%x_%d_%d_%d,0\n",
2281                mcore_current_function_name,
2282                fi.arg_size, fi.reg_size, fi.reg_mask,
2283                fi.local_size, fi.outbound_size,
2284                frame_pointer_needed);
2285     }
2286
2287   if (mcore_naked_function_p ())
2288     return;
2289   
2290   /* Handle stdarg+regsaves in one shot: can't be more than 64 bytes.  */
2291   output_stack_adjust (-1, fi.growth[growth++]);        /* Grows it.  */
2292
2293   /* If we have a parameter passed partially in regs and partially in memory,
2294      the registers will have been stored to memory already in function.c.  So
2295      we only need to do something here for varargs functions.  */
2296   if (fi.arg_size != 0 && current_function_pretend_args_size == 0)
2297     {
2298       int offset;
2299       int rn = FIRST_PARM_REG + NPARM_REGS - 1;
2300       int remaining = fi.arg_size;
2301
2302       for (offset = fi.arg_offset; remaining >= 4; offset -= 4, rn--, remaining -= 4)
2303         {
2304           emit_insn (gen_movsi
2305                      (gen_rtx_MEM (SImode,
2306                                plus_constant (stack_pointer_rtx, offset)),
2307                       gen_rtx_REG (SImode, rn)));
2308         }
2309     }
2310
2311   /* Do we need another stack adjustment before we do the register saves?  */
2312   if (growth < fi.reg_growth)
2313     output_stack_adjust (-1, fi.growth[growth++]);              /* Grows it.  */
2314
2315   if (fi.reg_size != 0)
2316     {
2317       int i;
2318       int offs = fi.reg_offset;
2319       
2320       for (i = 15; i >= 0; i--)
2321         {
2322           if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2323             {
2324               int first_reg = 15;
2325
2326               while (fi.reg_mask & (1 << first_reg))
2327                 first_reg--;
2328               first_reg++;
2329
2330               emit_insn (gen_store_multiple (gen_rtx_MEM (SImode, stack_pointer_rtx),
2331                                              gen_rtx_REG (SImode, first_reg),
2332                                              GEN_INT (16 - first_reg)));
2333
2334               i -= (15 - first_reg);
2335               offs += (16 - first_reg) * 4;
2336             }
2337           else if (fi.reg_mask & (1 << i))
2338             {
2339               emit_insn (gen_movsi
2340                          (gen_rtx_MEM (SImode,
2341                                    plus_constant (stack_pointer_rtx, offs)),
2342                           gen_rtx_REG (SImode, i)));
2343               offs += 4;
2344             }
2345         }
2346     }
2347
2348   /* Figure the locals + outbounds.  */
2349   if (frame_pointer_needed)
2350     {
2351       /* If we haven't already purchased to 'fp'.  */
2352       if (growth < fi.local_growth)
2353         output_stack_adjust (-1, fi.growth[growth++]);          /* Grows it.  */
2354       
2355       emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
2356
2357       /* ... and then go any remaining distance for outbounds, etc.  */
2358       if (fi.growth[growth])
2359         output_stack_adjust (-1, fi.growth[growth++]);
2360     }
2361   else
2362     {
2363       if (growth < fi.local_growth)
2364         output_stack_adjust (-1, fi.growth[growth++]);          /* Grows it.  */
2365       if (fi.growth[growth])
2366         output_stack_adjust (-1, fi.growth[growth++]);
2367     }
2368 }
2369
2370 void
2371 mcore_expand_epilog (void)
2372 {
2373   struct mcore_frame fi;
2374   int i;
2375   int offs;
2376   int growth = MAX_STACK_GROWS - 1 ;
2377
2378     
2379   /* Find out what we're doing.  */
2380   layout_mcore_frame(&fi);
2381
2382   if (mcore_naked_function_p ())
2383     return;
2384
2385   /* If we had a frame pointer, restore the sp from that.  */
2386   if (frame_pointer_needed)
2387     {
2388       emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
2389       growth = fi.local_growth - 1;
2390     }
2391   else
2392     {
2393       /* XXX: while loop should accumulate and do a single sell.  */
2394       while (growth >= fi.local_growth)
2395         {
2396           if (fi.growth[growth] != 0)
2397             output_stack_adjust (1, fi.growth[growth]);
2398           growth--;
2399         }
2400     }
2401
2402   /* Make sure we've shrunk stack back to the point where the registers
2403      were laid down. This is typically 0/1 iterations.  Then pull the
2404      register save information back off the stack.  */
2405   while (growth >= fi.reg_growth)
2406     output_stack_adjust ( 1, fi.growth[growth--]);
2407   
2408   offs = fi.reg_offset;
2409   
2410   for (i = 15; i >= 0; i--)
2411     {
2412       if (offs == 0 && i == 15 && ((fi.reg_mask & 0xc000) == 0xc000))
2413         {
2414           int first_reg;
2415
2416           /* Find the starting register.  */
2417           first_reg = 15;
2418           
2419           while (fi.reg_mask & (1 << first_reg))
2420             first_reg--;
2421           
2422           first_reg++;
2423
2424           emit_insn (gen_load_multiple (gen_rtx_REG (SImode, first_reg),
2425                                         gen_rtx_MEM (SImode, stack_pointer_rtx),
2426                                         GEN_INT (16 - first_reg)));
2427
2428           i -= (15 - first_reg);
2429           offs += (16 - first_reg) * 4;
2430         }
2431       else if (fi.reg_mask & (1 << i))
2432         {
2433           emit_insn (gen_movsi
2434                      (gen_rtx_REG (SImode, i),
2435                       gen_rtx_MEM (SImode,
2436                                plus_constant (stack_pointer_rtx, offs))));
2437           offs += 4;
2438         }
2439     }
2440
2441   /* Give back anything else.  */
2442   /* XXX: Should accumulate total and then give it back.  */
2443   while (growth >= 0)
2444     output_stack_adjust ( 1, fi.growth[growth--]);
2445 }
2446 \f
2447 /* This code is borrowed from the SH port.  */
2448
2449 /* The MCORE cannot load a large constant into a register, constants have to
2450    come from a pc relative load.  The reference of a pc relative load
2451    instruction must be less than 1k infront of the instruction.  This
2452    means that we often have to dump a constant inside a function, and
2453    generate code to branch around it.
2454
2455    It is important to minimize this, since the branches will slow things
2456    down and make things bigger.
2457
2458    Worst case code looks like:
2459
2460    lrw   L1,r0
2461    br    L2
2462    align
2463    L1:   .long value
2464    L2:
2465    ..
2466
2467    lrw   L3,r0
2468    br    L4
2469    align
2470    L3:   .long value
2471    L4:
2472    ..
2473
2474    We fix this by performing a scan before scheduling, which notices which
2475    instructions need to have their operands fetched from the constant table
2476    and builds the table.
2477
2478    The algorithm is:
2479
2480    scan, find an instruction which needs a pcrel move.  Look forward, find the
2481    last barrier which is within MAX_COUNT bytes of the requirement.
2482    If there isn't one, make one.  Process all the instructions between
2483    the find and the barrier.
2484
2485    In the above example, we can tell that L3 is within 1k of L1, so
2486    the first move can be shrunk from the 2 insn+constant sequence into
2487    just 1 insn, and the constant moved to L3 to make:
2488
2489    lrw          L1,r0
2490    ..
2491    lrw          L3,r0
2492    bra          L4
2493    align
2494    L3:.long value
2495    L4:.long value
2496
2497    Then the second move becomes the target for the shortening process.  */
2498
2499 typedef struct
2500 {
2501   rtx value;                    /* Value in table.  */
2502   rtx label;                    /* Label of value.  */
2503 } pool_node;
2504
2505 /* The maximum number of constants that can fit into one pool, since
2506    the pc relative range is 0...1020 bytes and constants are at least 4
2507    bytes long.  We subtract 4 from the range to allow for the case where
2508    we need to add a branch/align before the constant pool.  */
2509
2510 #define MAX_COUNT 1016
2511 #define MAX_POOL_SIZE (MAX_COUNT/4)
2512 static pool_node pool_vector[MAX_POOL_SIZE];
2513 static int pool_size;
2514
2515 /* Dump out any constants accumulated in the final pass.  These
2516    will only be labels.  */
2517
2518 const char *
2519 mcore_output_jump_label_table (void)
2520 {
2521   int i;
2522
2523   if (pool_size)
2524     {
2525       fprintf (asm_out_file, "\t.align 2\n");
2526       
2527       for (i = 0; i < pool_size; i++)
2528         {
2529           pool_node * p = pool_vector + i;
2530
2531           (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (p->label));
2532           
2533           output_asm_insn (".long       %0", &p->value);
2534         }
2535       
2536       pool_size = 0;
2537     }
2538
2539   return "";
2540 }
2541
2542 /* Check whether insn is a candidate for a conditional.  */
2543
2544 static cond_type
2545 is_cond_candidate (rtx insn)
2546 {
2547   /* The only things we conditionalize are those that can be directly
2548      changed into a conditional.  Only bother with SImode items.  If 
2549      we wanted to be a little more aggressive, we could also do other
2550      modes such as DImode with reg-reg move or load 0.  */
2551   if (GET_CODE (insn) == INSN)
2552     {
2553       rtx pat = PATTERN (insn);
2554       rtx src, dst;
2555
2556       if (GET_CODE (pat) != SET)
2557         return COND_NO;
2558
2559       dst = XEXP (pat, 0);
2560
2561       if ((GET_CODE (dst) != REG &&
2562            GET_CODE (dst) != SUBREG) ||
2563           GET_MODE (dst) != SImode)
2564         return COND_NO;
2565   
2566       src = XEXP (pat, 1);
2567
2568       if ((GET_CODE (src) == REG ||
2569            (GET_CODE (src) == SUBREG &&
2570             GET_CODE (SUBREG_REG (src)) == REG)) &&
2571           GET_MODE (src) == SImode)
2572         return COND_MOV_INSN;
2573       else if (GET_CODE (src) == CONST_INT && 
2574                INTVAL (src) == 0)
2575         return COND_CLR_INSN;
2576       else if (GET_CODE (src) == PLUS &&
2577                (GET_CODE (XEXP (src, 0)) == REG ||
2578                 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2579                  GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2580                GET_MODE (XEXP (src, 0)) == SImode &&
2581                GET_CODE (XEXP (src, 1)) == CONST_INT &&
2582                INTVAL (XEXP (src, 1)) == 1)
2583         return COND_INC_INSN;
2584       else if (((GET_CODE (src) == MINUS &&
2585                  GET_CODE (XEXP (src, 1)) == CONST_INT &&
2586                  INTVAL( XEXP (src, 1)) == 1) ||
2587                 (GET_CODE (src) == PLUS &&
2588                  GET_CODE (XEXP (src, 1)) == CONST_INT &&
2589                  INTVAL (XEXP (src, 1)) == -1)) &&
2590                (GET_CODE (XEXP (src, 0)) == REG ||
2591                 (GET_CODE (XEXP (src, 0)) == SUBREG &&
2592                  GET_CODE (SUBREG_REG (XEXP (src, 0))) == REG)) &&
2593                GET_MODE (XEXP (src, 0)) == SImode)
2594         return COND_DEC_INSN;
2595
2596       /* Some insns that we don't bother with:
2597          (set (rx:DI) (ry:DI))
2598          (set (rx:DI) (const_int 0))
2599       */            
2600
2601     }
2602   else if (GET_CODE (insn) == JUMP_INSN &&
2603            GET_CODE (PATTERN (insn)) == SET &&
2604            GET_CODE (XEXP (PATTERN (insn), 1)) == LABEL_REF)
2605     return COND_BRANCH_INSN;
2606
2607   return COND_NO;
2608 }
2609
2610 /* Emit a conditional version of insn and replace the old insn with the
2611    new one.  Return the new insn if emitted.  */
2612
2613 static rtx
2614 emit_new_cond_insn (rtx insn, int cond)
2615 {
2616   rtx c_insn = 0;
2617   rtx pat, dst, src;
2618   cond_type num;
2619
2620   if ((num = is_cond_candidate (insn)) == COND_NO)
2621     return NULL;
2622
2623   pat = PATTERN (insn);
2624
2625   if (GET_CODE (insn) == INSN)
2626     {
2627       dst = SET_DEST (pat);
2628       src = SET_SRC (pat);
2629     }
2630   else
2631     {
2632       dst = JUMP_LABEL (insn);
2633       src = NULL_RTX;
2634     }
2635
2636   switch (num)
2637     {
2638     case COND_MOV_INSN: 
2639     case COND_CLR_INSN:
2640       if (cond)
2641         c_insn = gen_movt0 (dst, src, dst);
2642       else
2643         c_insn = gen_movt0 (dst, dst, src);
2644       break;
2645
2646     case COND_INC_INSN:
2647       if (cond)
2648         c_insn = gen_incscc (dst, dst);
2649       else
2650         c_insn = gen_incscc_false (dst, dst);
2651       break;
2652   
2653     case COND_DEC_INSN:
2654       if (cond)
2655         c_insn = gen_decscc (dst, dst);
2656       else
2657         c_insn = gen_decscc_false (dst, dst);
2658       break;
2659
2660     case COND_BRANCH_INSN:
2661       if (cond)
2662         c_insn = gen_branch_true (dst);
2663       else
2664         c_insn = gen_branch_false (dst);
2665       break;
2666
2667     default:
2668       return NULL;
2669     }
2670
2671   /* Only copy the notes if they exist.  */
2672   if (rtx_length [GET_CODE (c_insn)] >= 7 && rtx_length [GET_CODE (insn)] >= 7)
2673     {
2674       /* We really don't need to bother with the notes and links at this
2675          point, but go ahead and save the notes.  This will help is_dead()
2676          when applying peepholes (links don't matter since they are not
2677          used any more beyond this point for the mcore).  */
2678       REG_NOTES (c_insn) = REG_NOTES (insn);
2679     }
2680   
2681   if (num == COND_BRANCH_INSN)
2682     {
2683       /* For jumps, we need to be a little bit careful and emit the new jump
2684          before the old one and to update the use count for the target label.
2685          This way, the barrier following the old (uncond) jump will get
2686          deleted, but the label won't.  */
2687       c_insn = emit_jump_insn_before (c_insn, insn);
2688       
2689       ++ LABEL_NUSES (dst);
2690       
2691       JUMP_LABEL (c_insn) = dst;
2692     }
2693   else
2694     c_insn = emit_insn_after (c_insn, insn);
2695
2696   delete_insn (insn);
2697   
2698   return c_insn;
2699 }
2700
2701 /* Attempt to change a basic block into a series of conditional insns.  This
2702    works by taking the branch at the end of the 1st block and scanning for the 
2703    end of the 2nd block.  If all instructions in the 2nd block have cond.
2704    versions and the label at the start of block 3 is the same as the target
2705    from the branch at block 1, then conditionalize all insn in block 2 using
2706    the inverse condition of the branch at block 1.  (Note I'm bending the
2707    definition of basic block here.)
2708
2709    e.g., change:   
2710
2711                 bt      L2             <-- end of block 1 (delete)
2712                 mov     r7,r8          
2713                 addu    r7,1           
2714                 br      L3             <-- end of block 2
2715
2716         L2:     ...                    <-- start of block 3 (NUSES==1)
2717         L3:     ...
2718
2719    to:
2720
2721                 movf    r7,r8
2722                 incf    r7
2723                 bf      L3
2724
2725         L3:     ...
2726
2727    we can delete the L2 label if NUSES==1 and re-apply the optimization
2728    starting at the last instruction of block 2.  This may allow an entire
2729    if-then-else statement to be conditionalized.  BRC  */
2730 static rtx
2731 conditionalize_block (rtx first)
2732 {
2733   rtx insn;
2734   rtx br_pat;
2735   rtx end_blk_1_br = 0;
2736   rtx end_blk_2_insn = 0;
2737   rtx start_blk_3_lab = 0;
2738   int cond;
2739   int br_lab_num;
2740   int blk_size = 0;
2741
2742     
2743   /* Check that the first insn is a candidate conditional jump.  This is
2744      the one that we'll eliminate.  If not, advance to the next insn to
2745      try.  */
2746   if (GET_CODE (first) != JUMP_INSN ||
2747       GET_CODE (PATTERN (first)) != SET ||
2748       GET_CODE (XEXP (PATTERN (first), 1)) != IF_THEN_ELSE)
2749     return NEXT_INSN (first);
2750
2751   /* Extract some information we need.  */
2752   end_blk_1_br = first;
2753   br_pat = PATTERN (end_blk_1_br);
2754
2755   /* Complement the condition since we use the reverse cond. for the insns.  */
2756   cond = (GET_CODE (XEXP (XEXP (br_pat, 1), 0)) == EQ);
2757
2758   /* Determine what kind of branch we have.  */
2759   if (GET_CODE (XEXP (XEXP (br_pat, 1), 1)) == LABEL_REF)
2760     {
2761       /* A normal branch, so extract label out of first arm.  */
2762       br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 1), 0));
2763     }
2764   else
2765     {
2766       /* An inverse branch, so extract the label out of the 2nd arm
2767          and complement the condition.  */
2768       cond = (cond == 0);
2769       br_lab_num = CODE_LABEL_NUMBER (XEXP (XEXP (XEXP (br_pat, 1), 2), 0));
2770     }
2771
2772   /* Scan forward for the start of block 2: it must start with a
2773      label and that label must be the same as the branch target
2774      label from block 1.  We don't care about whether block 2 actually
2775      ends with a branch or a label (an uncond. branch is 
2776      conditionalizable).  */
2777   for (insn = NEXT_INSN (first); insn; insn = NEXT_INSN (insn))
2778     {
2779       enum rtx_code code;
2780       
2781       code = GET_CODE (insn);
2782
2783       /* Look for the label at the start of block 3.  */
2784       if (code == CODE_LABEL && CODE_LABEL_NUMBER (insn) == br_lab_num)
2785         break;
2786
2787       /* Skip barriers, notes, and conditionalizable insns.  If the
2788          insn is not conditionalizable or makes this optimization fail,
2789          just return the next insn so we can start over from that point.  */
2790       if (code != BARRIER && code != NOTE && !is_cond_candidate (insn))
2791         return NEXT_INSN (insn);
2792      
2793       /* Remember the last real insn before the label (ie end of block 2).  */
2794       if (code == JUMP_INSN || code == INSN)
2795         {
2796           blk_size ++;
2797           end_blk_2_insn = insn;
2798         }
2799     }
2800
2801   if (!insn)
2802     return insn;
2803  
2804   /* It is possible for this optimization to slow performance if the blocks 
2805      are long.  This really depends upon whether the branch is likely taken 
2806      or not.  If the branch is taken, we slow performance in many cases.  But,
2807      if the branch is not taken, we always help performance (for a single 
2808      block, but for a double block (i.e. when the optimization is re-applied) 
2809      this is not true since the 'right thing' depends on the overall length of
2810      the collapsed block).  As a compromise, don't apply this optimization on 
2811      blocks larger than size 2 (unlikely for the mcore) when speed is important.
2812      the best threshold depends on the latencies of the instructions (i.e., 
2813      the branch penalty).  */
2814   if (optimize > 1 && blk_size > 2)
2815     return insn;
2816
2817   /* At this point, we've found the start of block 3 and we know that
2818      it is the destination of the branch from block 1.   Also, all
2819      instructions in the block 2 are conditionalizable.  So, apply the
2820      conditionalization and delete the branch.  */
2821   start_blk_3_lab = insn;   
2822    
2823   for (insn = NEXT_INSN (end_blk_1_br); insn != start_blk_3_lab; 
2824        insn = NEXT_INSN (insn))
2825     {
2826       rtx newinsn;
2827
2828       if (INSN_DELETED_P (insn))
2829         continue;
2830       
2831       /* Try to form a conditional variant of the instruction and emit it.  */
2832       if ((newinsn = emit_new_cond_insn (insn, cond)))
2833         {
2834           if (end_blk_2_insn == insn)
2835             end_blk_2_insn = newinsn;
2836
2837           insn = newinsn;
2838         }
2839     }
2840
2841   /* Note whether we will delete the label starting blk 3 when the jump
2842      gets deleted.  If so, we want to re-apply this optimization at the 
2843      last real instruction right before the label.  */
2844   if (LABEL_NUSES (start_blk_3_lab) == 1)
2845     {
2846       start_blk_3_lab = 0;
2847     }
2848
2849   /* ??? we probably should redistribute the death notes for this insn, esp.
2850      the death of cc, but it doesn't really matter this late in the game.
2851      The peepholes all use is_dead() which will find the correct death
2852      regardless of whether there is a note.  */
2853   delete_insn (end_blk_1_br);
2854
2855   if (! start_blk_3_lab)
2856     return end_blk_2_insn;
2857   
2858   /* Return the insn right after the label at the start of block 3.  */
2859   return NEXT_INSN (start_blk_3_lab);
2860 }
2861
2862 /* Apply the conditionalization of blocks optimization.  This is the
2863    outer loop that traverses through the insns scanning for a branch
2864    that signifies an opportunity to apply the optimization.  Note that
2865    this optimization is applied late.  If we could apply it earlier,
2866    say before cse 2, it may expose more optimization opportunities.  
2867    but, the pay back probably isn't really worth the effort (we'd have 
2868    to update all reg/flow/notes/links/etc to make it work - and stick it
2869    in before cse 2).  */
2870
2871 static void
2872 conditionalize_optimization (void)
2873 {
2874   rtx insn;
2875
2876   for (insn = get_insns (); insn; insn = conditionalize_block (insn))
2877     continue;
2878 }
2879
2880 static int saved_warn_return_type = -1;
2881 static int saved_warn_return_type_count = 0;
2882
2883 /* This is to handle loads from the constant pool.  */
2884
2885 static void
2886 mcore_reorg (void)
2887 {
2888   /* Reset this variable.  */
2889   current_function_anonymous_args = 0;
2890   
2891   /* Restore the warn_return_type if it has been altered.  */
2892   if (saved_warn_return_type != -1)
2893     {
2894       /* Only restore the value if we have reached another function.
2895          The test of warn_return_type occurs in final_function () in
2896          c-decl.c a long time after the code for the function is generated,
2897          so we need a counter to tell us when we have finished parsing that
2898          function and can restore the flag.  */
2899       if (--saved_warn_return_type_count == 0)
2900         {
2901           warn_return_type = saved_warn_return_type;
2902           saved_warn_return_type = -1;
2903         }
2904     }
2905   
2906   if (optimize == 0)
2907     return;
2908   
2909   /* Conditionalize blocks where we can.  */
2910   conditionalize_optimization ();
2911
2912   /* Literal pool generation is now pushed off until the assembler.  */
2913 }
2914
2915 \f
2916 /* Return true if X is something that can be moved directly into r15.  */
2917
2918 bool
2919 mcore_r15_operand_p (rtx x)
2920 {
2921   switch (GET_CODE (x))
2922     {
2923     case CONST_INT:
2924       return mcore_const_ok_for_inline (INTVAL (x));
2925
2926     case REG:
2927     case SUBREG:
2928     case MEM:
2929       return 1;
2930
2931     default:
2932       return 0;
2933     }
2934 }
2935
2936 /* Implement SECONDARY_RELOAD_CLASS.  If CLASS contains r15, and we can't
2937    directly move X into it, use r1-r14 as a temporary.  */
2938
2939 enum reg_class
2940 mcore_secondary_reload_class (enum reg_class class,
2941                               enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2942 {
2943   if (TEST_HARD_REG_BIT (reg_class_contents[class], 15)
2944       && !mcore_r15_operand_p (x))
2945     return LRW_REGS;
2946   return NO_REGS;
2947 }
2948
2949 /* Return the reg_class to use when reloading the rtx X into the class
2950    CLASS.  If X is too complex to move directly into r15, prefer to
2951    use LRW_REGS instead.  */
2952
2953 enum reg_class
2954 mcore_reload_class (rtx x, enum reg_class class)
2955 {
2956   if (reg_class_subset_p (LRW_REGS, class) && !mcore_r15_operand_p (x))
2957     return LRW_REGS;
2958
2959   return class;
2960 }
2961
2962 /* Tell me if a pair of reg/subreg rtx's actually refer to the same
2963    register.  Note that the current version doesn't worry about whether
2964    they are the same mode or note (e.g., a QImode in r2 matches an HImode
2965    in r2 matches an SImode in r2. Might think in the future about whether
2966    we want to be able to say something about modes.  */
2967
2968 int
2969 mcore_is_same_reg (rtx x, rtx y)
2970 {
2971   /* Strip any and all of the subreg wrappers.  */
2972   while (GET_CODE (x) == SUBREG)
2973     x = SUBREG_REG (x);
2974   
2975   while (GET_CODE (y) == SUBREG)
2976     y = SUBREG_REG (y);
2977
2978   if (GET_CODE(x) == REG && GET_CODE(y) == REG && REGNO(x) == REGNO(y))
2979     return 1;
2980
2981   return 0;
2982 }
2983
2984 void
2985 mcore_override_options (void)
2986 {
2987   if (mcore_stack_increment_string)
2988     {
2989       mcore_stack_increment = atoi (mcore_stack_increment_string);
2990       
2991       if (mcore_stack_increment < 0
2992           || (mcore_stack_increment == 0
2993               && (mcore_stack_increment_string[0] != '0'
2994                   || mcore_stack_increment_string[1] != 0)))
2995         error ("invalid option `-mstack-increment=%s'",
2996                mcore_stack_increment_string);   
2997     }
2998   
2999   /* Only the m340 supports little endian code.  */
3000   if (TARGET_LITTLE_END && ! TARGET_M340)
3001     target_flags |= M340_BIT;
3002 }
3003 \f
3004 int
3005 mcore_must_pass_on_stack (enum machine_mode mode ATTRIBUTE_UNUSED, tree type)
3006 {
3007   if (type == NULL)
3008     return 0;
3009
3010   /* If the argument can have its address taken, it must
3011      be placed on the stack.  */
3012   if (TREE_ADDRESSABLE (type))
3013     return 1;
3014
3015   return 0;
3016 }
3017
3018 /* Compute the number of word sized registers needed to 
3019    hold a function argument of mode MODE and type TYPE.  */
3020
3021 int
3022 mcore_num_arg_regs (enum machine_mode mode, tree type)
3023 {
3024   int size;
3025
3026   if (MUST_PASS_IN_STACK (mode, type))
3027     return 0;
3028
3029   if (type && mode == BLKmode)
3030     size = int_size_in_bytes (type);
3031   else
3032     size = GET_MODE_SIZE (mode);
3033
3034   return ROUND_ADVANCE (size);
3035 }
3036
3037 static rtx
3038 handle_structs_in_regs (enum machine_mode mode, tree type, int reg)
3039 {
3040   int size;
3041
3042   /* The MCore ABI defines that a structure whoes size is not a whole multiple
3043      of bytes is passed packed into registers (or spilled onto the stack if
3044      not enough registers are available) with the last few bytes of the
3045      structure being packed, left-justified, into the last register/stack slot.
3046      GCC handles this correctly if the last word is in a stack slot, but we
3047      have to generate a special, PARALLEL RTX if the last word is in an
3048      argument register.  */
3049   if (type
3050       && TYPE_MODE (type) == BLKmode
3051       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3052       && (size = int_size_in_bytes (type)) > UNITS_PER_WORD
3053       && (size % UNITS_PER_WORD != 0)
3054       && (reg + mcore_num_arg_regs (mode, type) <= (FIRST_PARM_REG + NPARM_REGS)))
3055     {
3056       rtx    arg_regs [NPARM_REGS]; 
3057       int    nregs;
3058       rtx    result;
3059       rtvec  rtvec;
3060                      
3061       for (nregs = 0; size > 0; size -= UNITS_PER_WORD)
3062         {
3063           arg_regs [nregs] =
3064             gen_rtx_EXPR_LIST (SImode, gen_rtx_REG (SImode, reg ++),
3065                                GEN_INT (nregs * UNITS_PER_WORD));
3066           nregs ++;
3067         }
3068
3069       /* We assume here that NPARM_REGS == 6.  The assert checks this.  */
3070       assert (ARRAY_SIZE (arg_regs) == 6);
3071       rtvec = gen_rtvec (nregs, arg_regs[0], arg_regs[1], arg_regs[2],
3072                           arg_regs[3], arg_regs[4], arg_regs[5]);
3073       
3074       result = gen_rtx_PARALLEL (mode, rtvec);
3075       return result;
3076     }
3077   
3078   return gen_rtx_REG (mode, reg);
3079 }
3080
3081 rtx
3082 mcore_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
3083 {
3084   enum machine_mode mode;
3085   int unsigned_p;
3086   
3087   mode = TYPE_MODE (valtype);
3088
3089   PROMOTE_MODE (mode, unsigned_p, NULL);
3090   
3091   return handle_structs_in_regs (mode, valtype, FIRST_RET_REG);
3092 }
3093
3094 /* Define where to put the arguments to a function.
3095    Value is zero to push the argument on the stack,
3096    or a hard register in which to store the argument.
3097
3098    MODE is the argument's machine mode.
3099    TYPE is the data type of the argument (as a tree).
3100     This is null for libcalls where that information may
3101     not be available.
3102    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3103     the preceding args and about the function being called.
3104    NAMED is nonzero if this argument is a named parameter
3105     (otherwise it is an extra parameter matching an ellipsis).
3106
3107    On MCore the first args are normally in registers
3108    and the rest are pushed.  Any arg that starts within the first
3109    NPARM_REGS words is at least partially passed in a register unless
3110    its data type forbids.  */
3111
3112 rtx
3113 mcore_function_arg (CUMULATIVE_ARGS cum, enum machine_mode mode,
3114                     tree type, int named)
3115 {
3116   int arg_reg;
3117   
3118   if (! named)
3119     return 0;
3120
3121   if (MUST_PASS_IN_STACK (mode, type))
3122     return 0;
3123
3124   arg_reg = ROUND_REG (cum, mode);
3125   
3126   if (arg_reg < NPARM_REGS)
3127     return handle_structs_in_regs (mode, type, FIRST_PARM_REG + arg_reg);
3128
3129   return 0;
3130 }
3131
3132 /* Implements the FUNCTION_ARG_PARTIAL_NREGS macro.
3133    Returns the number of argument registers required to hold *part* of
3134    a parameter of machine mode MODE and type TYPE (which may be NULL if
3135    the type is not known).  If the argument fits entirely in the argument
3136    registers, or entirely on the stack, then 0 is returned.  CUM is the
3137    number of argument registers already used by earlier parameters to
3138    the function.  */
3139
3140 int
3141 mcore_function_arg_partial_nregs (CUMULATIVE_ARGS cum, enum machine_mode mode,
3142                                   tree type, int named)
3143 {
3144   int reg = ROUND_REG (cum, mode);
3145
3146   if (named == 0)
3147     return 0;
3148
3149   if (MUST_PASS_IN_STACK (mode, type))
3150     return 0;
3151       
3152   /* REG is not the *hardware* register number of the register that holds
3153      the argument, it is the *argument* register number.  So for example,
3154      the first argument to a function goes in argument register 0, which
3155      translates (for the MCore) into hardware register 2.  The second
3156      argument goes into argument register 1, which translates into hardware
3157      register 3, and so on.  NPARM_REGS is the number of argument registers
3158      supported by the target, not the maximum hardware register number of
3159      the target.  */
3160   if (reg >= NPARM_REGS)
3161     return 0;
3162
3163   /* If the argument fits entirely in registers, return 0.  */
3164   if (reg + mcore_num_arg_regs (mode, type) <= NPARM_REGS)
3165     return 0;
3166
3167   /* The argument overflows the number of available argument registers.
3168      Compute how many argument registers have not yet been assigned to
3169      hold an argument.  */
3170   reg = NPARM_REGS - reg;
3171
3172   /* Return partially in registers and partially on the stack.  */
3173   return reg;
3174 }
3175 \f
3176 /* Return nonzero if SYMBOL is marked as being dllexport'd.  */
3177
3178 int
3179 mcore_dllexport_name_p (const char * symbol)
3180 {
3181   return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
3182 }
3183
3184 /* Return nonzero if SYMBOL is marked as being dllimport'd.  */
3185
3186 int
3187 mcore_dllimport_name_p (const char * symbol)
3188 {
3189   return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
3190 }
3191
3192 /* Mark a DECL as being dllexport'd.  */
3193
3194 static void
3195 mcore_mark_dllexport (tree decl)
3196 {
3197   const char * oldname;
3198   char * newname;
3199   rtx    rtlname;
3200   tree   idp;
3201
3202   rtlname = XEXP (DECL_RTL (decl), 0);
3203   
3204   if (GET_CODE (rtlname) == SYMBOL_REF)
3205     oldname = XSTR (rtlname, 0);
3206   else if (   GET_CODE (rtlname) == MEM
3207            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3208     oldname = XSTR (XEXP (rtlname, 0), 0);
3209   else
3210     abort ();
3211   
3212   if (mcore_dllexport_name_p (oldname))
3213     return;  /* Already done.  */
3214
3215   newname = alloca (strlen (oldname) + 4);
3216   sprintf (newname, "@e.%s", oldname);
3217
3218   /* We pass newname through get_identifier to ensure it has a unique
3219      address.  RTL processing can sometimes peek inside the symbol ref
3220      and compare the string's addresses to see if two symbols are
3221      identical.  */
3222   /* ??? At least I think that's why we do this.  */
3223   idp = get_identifier (newname);
3224
3225   XEXP (DECL_RTL (decl), 0) =
3226     gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
3227 }
3228
3229 /* Mark a DECL as being dllimport'd.  */
3230
3231 static void
3232 mcore_mark_dllimport (tree decl)
3233 {
3234   const char * oldname;
3235   char * newname;
3236   tree   idp;
3237   rtx    rtlname;
3238   rtx    newrtl;
3239
3240   rtlname = XEXP (DECL_RTL (decl), 0);
3241   
3242   if (GET_CODE (rtlname) == SYMBOL_REF)
3243     oldname = XSTR (rtlname, 0);
3244   else if (   GET_CODE (rtlname) == MEM
3245            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
3246     oldname = XSTR (XEXP (rtlname, 0), 0);
3247   else
3248     abort ();
3249   
3250   if (mcore_dllexport_name_p (oldname))
3251     abort (); /* This shouldn't happen.  */
3252   else if (mcore_dllimport_name_p (oldname))
3253     return; /* Already done.  */
3254
3255   /* ??? One can well ask why we're making these checks here,
3256      and that would be a good question.  */
3257
3258   /* Imported variables can't be initialized.  */
3259   if (TREE_CODE (decl) == VAR_DECL
3260       && !DECL_VIRTUAL_P (decl)
3261       && DECL_INITIAL (decl))
3262     {
3263       error ("%Jinitialized variable '%D' is marked dllimport", decl, decl);
3264       return;
3265     }
3266   
3267   /* `extern' needn't be specified with dllimport.
3268      Specify `extern' now and hope for the best.  Sigh.  */
3269   if (TREE_CODE (decl) == VAR_DECL
3270       /* ??? Is this test for vtables needed?  */
3271       && !DECL_VIRTUAL_P (decl))
3272     {
3273       DECL_EXTERNAL (decl) = 1;
3274       TREE_PUBLIC (decl) = 1;
3275     }
3276
3277   newname = alloca (strlen (oldname) + 11);
3278   sprintf (newname, "@i.__imp_%s", oldname);
3279
3280   /* We pass newname through get_identifier to ensure it has a unique
3281      address.  RTL processing can sometimes peek inside the symbol ref
3282      and compare the string's addresses to see if two symbols are
3283      identical.  */
3284   /* ??? At least I think that's why we do this.  */
3285   idp = get_identifier (newname);
3286
3287   newrtl = gen_rtx_MEM (Pmode,
3288                     gen_rtx_SYMBOL_REF (Pmode,
3289                              IDENTIFIER_POINTER (idp)));
3290   XEXP (DECL_RTL (decl), 0) = newrtl;
3291 }
3292
3293 static int
3294 mcore_dllexport_p (tree decl)
3295 {
3296   if (   TREE_CODE (decl) != VAR_DECL
3297       && TREE_CODE (decl) != FUNCTION_DECL)
3298     return 0;
3299
3300   return lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)) != 0;
3301 }
3302
3303 static int
3304 mcore_dllimport_p (tree decl)
3305 {
3306   if (   TREE_CODE (decl) != VAR_DECL
3307       && TREE_CODE (decl) != FUNCTION_DECL)
3308     return 0;
3309
3310   return lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) != 0;
3311 }
3312
3313 /* We must mark dll symbols specially.  Definitions of dllexport'd objects
3314    install some info in the .drective (PE) or .exports (ELF) sections.  */
3315
3316 static void
3317 mcore_encode_section_info (tree decl, rtx rtl ATTRIBUTE_UNUSED, int first ATTRIBUTE_UNUSED)
3318 {
3319   /* Mark the decl so we can tell from the rtl whether the object is
3320      dllexport'd or dllimport'd.  */
3321   if (mcore_dllexport_p (decl))
3322     mcore_mark_dllexport (decl);
3323   else if (mcore_dllimport_p (decl))
3324     mcore_mark_dllimport (decl);
3325   
3326   /* It might be that DECL has already been marked as dllimport, but
3327      a subsequent definition nullified that.  The attribute is gone
3328      but DECL_RTL still has @i.__imp_foo.  We need to remove that.  */
3329   else if ((TREE_CODE (decl) == FUNCTION_DECL
3330             || TREE_CODE (decl) == VAR_DECL)
3331            && DECL_RTL (decl) != NULL_RTX
3332            && GET_CODE (DECL_RTL (decl)) == MEM
3333            && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
3334            && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
3335            && mcore_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
3336     {
3337       const char * oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
3338       tree idp = get_identifier (oldname + 9);
3339       rtx newrtl = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
3340
3341       XEXP (DECL_RTL (decl), 0) = newrtl;
3342
3343       /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
3344          ??? We leave these alone for now.  */
3345     }
3346 }
3347
3348 /* Undo the effects of the above.  */
3349
3350 static const char *
3351 mcore_strip_name_encoding (const char * str)
3352 {
3353   return str + (str[0] == '@' ? 3 : 0);
3354 }
3355
3356 /* MCore specific attribute support.
3357    dllexport - for exporting a function/variable that will live in a dll
3358    dllimport - for importing a function/variable from a dll
3359    naked     - do not create a function prologue/epilogue.  */
3360
3361 const struct attribute_spec mcore_attribute_table[] =
3362 {
3363   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3364   { "dllexport", 0, 0, true,  false, false, NULL },
3365   { "dllimport", 0, 0, true,  false, false, NULL },
3366   { "naked",     0, 0, true,  false, false, mcore_handle_naked_attribute },
3367   { NULL,        0, 0, false, false, false, NULL }
3368 };
3369
3370 /* Handle a "naked" attribute; arguments as in
3371    struct attribute_spec.handler.  */
3372
3373 static tree
3374 mcore_handle_naked_attribute (tree * node, tree name, tree args ATTRIBUTE_UNUSED,
3375                               int flags ATTRIBUTE_UNUSED, bool * no_add_attrs)
3376 {
3377   if (TREE_CODE (*node) == FUNCTION_DECL)
3378     {
3379       /* PR14310 - don't complain about lack of return statement
3380          in naked functions.  The solution here is a gross hack
3381          but this is the only way to solve the problem without
3382          adding a new feature to GCC.  I did try submitting a patch
3383          that would add such a new feature, but it was (rightfully)
3384          rejected on the grounds that it was creeping featurism,
3385          so hence this code.  */
3386       if (warn_return_type)
3387         {
3388           saved_warn_return_type = warn_return_type;
3389           warn_return_type = 0;
3390           saved_warn_return_type_count = 2;
3391         }
3392       else if (saved_warn_return_type_count)
3393         saved_warn_return_type_count = 2;
3394     }
3395   else
3396     {
3397       warning ("`%s' attribute only applies to functions",
3398                IDENTIFIER_POINTER (name));
3399       *no_add_attrs = true;
3400     }
3401
3402   return NULL_TREE;
3403 }
3404
3405 /* ??? It looks like this is PE specific?  Oh well, this is what the
3406    old code did as well.  */
3407
3408 static void
3409 mcore_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
3410 {
3411   int len;
3412   const char * name;
3413   char * string;
3414   const char * prefix;
3415
3416   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3417   
3418   /* Strip off any encoding in name.  */
3419   name = (* targetm.strip_name_encoding) (name);
3420
3421   /* The object is put in, for example, section .text$foo.
3422      The linker will then ultimately place them in .text
3423      (everything from the $ on is stripped).  */
3424   if (TREE_CODE (decl) == FUNCTION_DECL)
3425     prefix = ".text$";
3426   /* For compatibility with EPOC, we ignore the fact that the
3427      section might have relocs against it.  */
3428   else if (decl_readonly_section (decl, 0))
3429     prefix = ".rdata$";
3430   else
3431     prefix = ".data$";
3432   
3433   len = strlen (name) + strlen (prefix);
3434   string = alloca (len + 1);
3435   
3436   sprintf (string, "%s%s", prefix, name);
3437
3438   DECL_SECTION_NAME (decl) = build_string (len, string);
3439 }
3440
3441 int
3442 mcore_naked_function_p (void)
3443 {
3444   return lookup_attribute ("naked", DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE;
3445 }
3446
3447 #ifdef OBJECT_FORMAT_ELF
3448 static void
3449 mcore_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED)
3450 {
3451   fprintf (asm_out_file, "\t.section %s\n", name);
3452 }
3453 #endif /* OBJECT_FORMAT_ELF */
3454
3455 /* Worker function for TARGET_ASM_EXTERNAL_LIBCALL.  */
3456
3457 static void
3458 mcore_external_libcall (rtx fun)
3459 {
3460   fprintf (asm_out_file, "\t.import\t");
3461   assemble_name (asm_out_file, XSTR (fun, 0));
3462   fprintf (asm_out_file, "\n");
3463 }
3464
3465 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
3466
3467 static bool
3468 mcore_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3469 {
3470   HOST_WIDE_INT size = int_size_in_bytes (type);
3471   return (size == -1 || size > 2 * UNITS_PER_WORD);
3472 }