OSDN Git Service

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