OSDN Git Service

* target.h (enum opt_levels, struct default_options): New.
[pf3gnuchains/gcc-fork.git] / gcc / config / mn10300 / mn10300.c
1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4    Contributed by Jeff Law (law@cygnus.com).
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING3.  If not see
20    <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "conditions.h"
32 #include "output.h"
33 #include "insn-attr.h"
34 #include "flags.h"
35 #include "recog.h"
36 #include "reload.h"
37 #include "expr.h"
38 #include "optabs.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "diagnostic-core.h"
42 #include "toplev.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "target-def.h"
46 #include "df.h"
47
48 /* This is used by GOTaddr2picreg to uniquely identify
49    UNSPEC_INT_LABELs.  */
50 int mn10300_unspec_int_label_counter;
51
52 /* This is used in the am33_2.0-linux-gnu port, in which global symbol
53    names are not prefixed by underscores, to tell whether to prefix a
54    label with a plus sign or not, so that the assembler can tell
55    symbol names from register names.  */
56 int mn10300_protect_label;
57
58 /* The selected processor.  */
59 enum processor_type mn10300_processor = PROCESSOR_DEFAULT;
60
61 /* The size of the callee register save area.  Right now we save everything
62    on entry since it costs us nothing in code size.  It does cost us from a
63    speed standpoint, so we want to optimize this sooner or later.  */
64 #define REG_SAVE_BYTES (4 * df_regs_ever_live_p (2)             \
65                         + 4 * df_regs_ever_live_p (3)           \
66                         + 4 * df_regs_ever_live_p (6)           \
67                         + 4 * df_regs_ever_live_p (7)           \
68                         + 16 * (df_regs_ever_live_p (14)        \
69                                 || df_regs_ever_live_p (15)     \
70                                 || df_regs_ever_live_p (16)     \
71                                 || df_regs_ever_live_p (17)))
72
73 static int mn10300_address_cost (rtx, bool);
74
75 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE.  */
76 static const struct default_options mn10300_option_optimization_table[] =
77   {
78     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
79     { OPT_LEVELS_NONE, 0, NULL, 0 }
80   };
81 \f
82 /* Implement TARGET_HANDLE_OPTION.  */
83
84 static bool
85 mn10300_handle_option (size_t code,
86                        const char *arg ATTRIBUTE_UNUSED,
87                        int value)
88 {
89   switch (code)
90     {
91     case OPT_mam33:
92       mn10300_processor = value ? PROCESSOR_AM33 : PROCESSOR_MN10300;
93       return true;
94     case OPT_mam33_2:
95       mn10300_processor = (value
96                            ? PROCESSOR_AM33_2
97                            : MIN (PROCESSOR_AM33, PROCESSOR_DEFAULT));
98       return true;
99     default:
100       return true;
101     }
102 }
103
104 /* Implement TARGET_OPTION_OVERRIDE.  */
105
106 static void
107 mn10300_option_override (void)
108 {
109   if (TARGET_AM33)
110     target_flags &= ~MASK_MULT_BUG;
111 }
112
113 static void
114 mn10300_file_start (void)
115 {
116   default_file_start ();
117
118   if (TARGET_AM33_2)
119     fprintf (asm_out_file, "\t.am33_2\n");
120   else if (TARGET_AM33)
121     fprintf (asm_out_file, "\t.am33\n");
122 }
123 \f
124 /* Print operand X using operand code CODE to assembly language output file
125    FILE.  */
126
127 void
128 mn10300_print_operand (FILE *file, rtx x, int code)
129 {
130   switch (code)
131     {
132       case 'b':
133       case 'B':
134         if (GET_MODE (XEXP (x, 0)) == CC_FLOATmode)
135           {
136             switch (code == 'b' ? GET_CODE (x)
137                     : reverse_condition_maybe_unordered (GET_CODE (x)))
138               {
139               case NE:
140                 fprintf (file, "ne");
141                 break;
142               case EQ:
143                 fprintf (file, "eq");
144                 break;
145               case GE:
146                 fprintf (file, "ge");
147                 break;
148               case GT:
149                 fprintf (file, "gt");
150                 break;
151               case LE:
152                 fprintf (file, "le");
153                 break;
154               case LT:
155                 fprintf (file, "lt");
156                 break;
157               case ORDERED:
158                 fprintf (file, "lge");
159                 break;
160               case UNORDERED:
161                 fprintf (file, "uo");
162                 break;
163               case LTGT:
164                 fprintf (file, "lg");
165                 break;
166               case UNEQ:
167                 fprintf (file, "ue");
168                 break;
169               case UNGE:
170                 fprintf (file, "uge");
171                 break;
172               case UNGT:
173                 fprintf (file, "ug");
174                 break;
175               case UNLE:
176                 fprintf (file, "ule");
177                 break;
178               case UNLT:
179                 fprintf (file, "ul");
180                 break;
181               default:
182                 gcc_unreachable ();
183               }
184             break;
185           }
186         /* These are normal and reversed branches.  */
187         switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
188           {
189           case NE:
190             fprintf (file, "ne");
191             break;
192           case EQ:
193             fprintf (file, "eq");
194             break;
195           case GE:
196             fprintf (file, "ge");
197             break;
198           case GT:
199             fprintf (file, "gt");
200             break;
201           case LE:
202             fprintf (file, "le");
203             break;
204           case LT:
205             fprintf (file, "lt");
206             break;
207           case GEU:
208             fprintf (file, "cc");
209             break;
210           case GTU:
211             fprintf (file, "hi");
212             break;
213           case LEU:
214             fprintf (file, "ls");
215             break;
216           case LTU:
217             fprintf (file, "cs");
218             break;
219           default:
220             gcc_unreachable ();
221           }
222         break;
223       case 'C':
224         /* This is used for the operand to a call instruction;
225            if it's a REG, enclose it in parens, else output
226            the operand normally.  */
227         if (REG_P (x))
228           {
229             fputc ('(', file);
230             mn10300_print_operand (file, x, 0);
231             fputc (')', file);
232           }
233         else
234           mn10300_print_operand (file, x, 0);
235         break;
236
237       case 'D':
238         switch (GET_CODE (x))
239           {
240           case MEM:
241             fputc ('(', file);
242             output_address (XEXP (x, 0));
243             fputc (')', file);
244             break;
245
246           case REG:
247             fprintf (file, "fd%d", REGNO (x) - 18);
248             break;
249
250           default:
251             gcc_unreachable ();
252           }
253         break;
254
255       /* These are the least significant word in a 64bit value.  */
256       case 'L':
257         switch (GET_CODE (x))
258           {
259           case MEM:
260             fputc ('(', file);
261             output_address (XEXP (x, 0));
262             fputc (')', file);
263             break;
264
265           case REG:
266             fprintf (file, "%s", reg_names[REGNO (x)]);
267             break;
268
269           case SUBREG:
270             fprintf (file, "%s", reg_names[subreg_regno (x)]);
271             break;
272
273           case CONST_DOUBLE:
274               {
275                 long val[2];
276                 REAL_VALUE_TYPE rv;
277
278                 switch (GET_MODE (x))
279                   {
280                     case DFmode:
281                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
282                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
283                       fprintf (file, "0x%lx", val[0]);
284                       break;;
285                     case SFmode:
286                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
287                       REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
288                       fprintf (file, "0x%lx", val[0]);
289                       break;;
290                     case VOIDmode:
291                     case DImode:
292                       mn10300_print_operand_address (file,
293                                                      GEN_INT (CONST_DOUBLE_LOW (x)));
294                       break;
295                     default:
296                       break;
297                   }
298                 break;
299               }
300
301           case CONST_INT:
302             {
303               rtx low, high;
304               split_double (x, &low, &high);
305               fprintf (file, "%ld", (long)INTVAL (low));
306               break;
307             }
308
309           default:
310             gcc_unreachable ();
311           }
312         break;
313
314       /* Similarly, but for the most significant word.  */
315       case 'H':
316         switch (GET_CODE (x))
317           {
318           case MEM:
319             fputc ('(', file);
320             x = adjust_address (x, SImode, 4);
321             output_address (XEXP (x, 0));
322             fputc (')', file);
323             break;
324
325           case REG:
326             fprintf (file, "%s", reg_names[REGNO (x) + 1]);
327             break;
328
329           case SUBREG:
330             fprintf (file, "%s", reg_names[subreg_regno (x) + 1]);
331             break;
332
333           case CONST_DOUBLE:
334               {
335                 long val[2];
336                 REAL_VALUE_TYPE rv;
337
338                 switch (GET_MODE (x))
339                   {
340                     case DFmode:
341                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
342                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
343                       fprintf (file, "0x%lx", val[1]);
344                       break;;
345                     case SFmode:
346                       gcc_unreachable ();
347                     case VOIDmode:
348                     case DImode:
349                       mn10300_print_operand_address (file,
350                                                      GEN_INT (CONST_DOUBLE_HIGH (x)));
351                       break;
352                     default:
353                       break;
354                   }
355                 break;
356               }
357
358           case CONST_INT:
359             {
360               rtx low, high;
361               split_double (x, &low, &high);
362               fprintf (file, "%ld", (long)INTVAL (high));
363               break;
364             }
365
366           default:
367             gcc_unreachable ();
368           }
369         break;
370
371       case 'A':
372         fputc ('(', file);
373         if (REG_P ((XEXP (x, 0))))
374           output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), const0_rtx));
375         else
376           output_address (XEXP (x, 0));
377         fputc (')', file);
378         break;
379
380       case 'N':
381         gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
382         fprintf (file, "%d", (int)((~INTVAL (x)) & 0xff));
383         break;
384
385       case 'U':
386         gcc_assert (INTVAL (x) >= -128 && INTVAL (x) <= 255);
387         fprintf (file, "%d", (int)(INTVAL (x) & 0xff));
388         break;
389
390       /* For shift counts.  The hardware ignores the upper bits of
391          any immediate, but the assembler will flag an out of range
392          shift count as an error.  So we mask off the high bits
393          of the immediate here.  */
394       case 'S':
395         if (CONST_INT_P ((x)))
396           {
397             fprintf (file, "%d", (int)(INTVAL (x) & 0x1f));
398             break;
399           }
400         /* FALL THROUGH */
401
402       default:
403         switch (GET_CODE (x))
404           {
405           case MEM:
406             fputc ('(', file);
407             output_address (XEXP (x, 0));
408             fputc (')', file);
409             break;
410
411           case PLUS:
412             output_address (x);
413             break;
414
415           case REG:
416             fprintf (file, "%s", reg_names[REGNO (x)]);
417             break;
418
419           case SUBREG:
420             fprintf (file, "%s", reg_names[subreg_regno (x)]);
421             break;
422
423           /* This will only be single precision....  */
424           case CONST_DOUBLE:
425             {
426               unsigned long val;
427               REAL_VALUE_TYPE rv;
428
429               REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
430               REAL_VALUE_TO_TARGET_SINGLE (rv, val);
431               fprintf (file, "0x%lx", val);
432               break;
433             }
434
435           case CONST_INT:
436           case SYMBOL_REF:
437           case CONST:
438           case LABEL_REF:
439           case CODE_LABEL:
440           case UNSPEC:
441             mn10300_print_operand_address (file, x);
442             break;
443           default:
444             gcc_unreachable ();
445           }
446         break;
447    }
448 }
449
450 /* Output assembly language output for the address ADDR to FILE.  */
451
452 void
453 mn10300_print_operand_address (FILE *file, rtx addr)
454 {
455   switch (GET_CODE (addr))
456     {
457     case POST_INC:
458       mn10300_print_operand_address (file, XEXP (addr, 0));
459       fputc ('+', file);
460       break;
461     case REG:
462       mn10300_print_operand (file, addr, 0);
463       break;
464     case PLUS:
465       {
466         rtx base, index;
467         if (REG_P (XEXP (addr, 0))
468             && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
469           base = XEXP (addr, 0), index = XEXP (addr, 1);
470         else if (REG_P (XEXP (addr, 1))
471             && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
472           base = XEXP (addr, 1), index = XEXP (addr, 0);
473         else
474           gcc_unreachable ();
475         mn10300_print_operand (file, index, 0);
476         fputc (',', file);
477         mn10300_print_operand (file, base, 0);;
478         break;
479       }
480     case SYMBOL_REF:
481       output_addr_const (file, addr);
482       break;
483     default:
484       output_addr_const (file, addr);
485       break;
486     }
487 }
488
489 /* Count the number of FP registers that have to be saved.  */
490 static int
491 fp_regs_to_save (void)
492 {
493   int i, n = 0;
494
495   if (! TARGET_AM33_2)
496     return 0;
497
498   for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
499     if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
500       ++n;
501
502   return n;
503 }
504
505 /* Print a set of registers in the format required by "movm" and "ret".
506    Register K is saved if bit K of MASK is set.  The data and address
507    registers can be stored individually, but the extended registers cannot.
508    We assume that the mask already takes that into account.  For instance,
509    bits 14 to 17 must have the same value.  */
510
511 void
512 mn10300_print_reg_list (FILE *file, int mask)
513 {
514   int need_comma;
515   int i;
516
517   need_comma = 0;
518   fputc ('[', file);
519
520   for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
521     if ((mask & (1 << i)) != 0)
522       {
523         if (need_comma)
524           fputc (',', file);
525         fputs (reg_names [i], file);
526         need_comma = 1;
527       }
528
529   if ((mask & 0x3c000) != 0)
530     {
531       gcc_assert ((mask & 0x3c000) == 0x3c000);
532       if (need_comma)
533         fputc (',', file);
534       fputs ("exreg1", file);
535       need_comma = 1;
536     }
537
538   fputc (']', file);
539 }
540
541 int
542 mn10300_can_use_return_insn (void)
543 {
544   /* size includes the fixed stack space needed for function calls.  */
545   int size = get_frame_size () + crtl->outgoing_args_size;
546
547   /* And space for the return pointer.  */
548   size += crtl->outgoing_args_size ? 4 : 0;
549
550   return (reload_completed
551           && size == 0
552           && !df_regs_ever_live_p (2)
553           && !df_regs_ever_live_p (3)
554           && !df_regs_ever_live_p (6)
555           && !df_regs_ever_live_p (7)
556           && !df_regs_ever_live_p (14)
557           && !df_regs_ever_live_p (15)
558           && !df_regs_ever_live_p (16)
559           && !df_regs_ever_live_p (17)
560           && fp_regs_to_save () == 0
561           && !frame_pointer_needed);
562 }
563
564 /* Returns the set of live, callee-saved registers as a bitmask.  The
565    callee-saved extended registers cannot be stored individually, so
566    all of them will be included in the mask if any one of them is used.  */
567
568 int
569 mn10300_get_live_callee_saved_regs (void)
570 {
571   int mask;
572   int i;
573
574   mask = 0;
575   for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
576     if (df_regs_ever_live_p (i) && ! call_really_used_regs[i])
577       mask |= (1 << i);
578   if ((mask & 0x3c000) != 0)
579     mask |= 0x3c000;
580
581   return mask;
582 }
583
584 static rtx
585 F (rtx r)
586 {
587   RTX_FRAME_RELATED_P (r) = 1;
588   return r;
589 }
590
591 /* Generate an instruction that pushes several registers onto the stack.
592    Register K will be saved if bit K in MASK is set.  The function does
593    nothing if MASK is zero.
594
595    To be compatible with the "movm" instruction, the lowest-numbered
596    register must be stored in the lowest slot.  If MASK is the set
597    { R1,...,RN }, where R1...RN are ordered least first, the generated
598    instruction will have the form:
599
600        (parallel
601          (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
602          (set (mem:SI (plus:SI (reg:SI 9)
603                                (const_int -1*4)))
604               (reg:SI RN))
605          ...
606          (set (mem:SI (plus:SI (reg:SI 9)
607                                (const_int -N*4)))
608               (reg:SI R1))) */
609
610 void
611 mn10300_gen_multiple_store (int mask)
612 {
613   if (mask != 0)
614     {
615       int i;
616       int count;
617       rtx par;
618       int pari;
619
620       /* Count how many registers need to be saved.  */
621       count = 0;
622       for (i = 0; i <= LAST_EXTENDED_REGNUM; i++)
623         if ((mask & (1 << i)) != 0)
624           count += 1;
625
626       /* We need one PARALLEL element to update the stack pointer and
627          an additional element for each register that is stored.  */
628       par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count + 1));
629
630       /* Create the instruction that updates the stack pointer.  */
631       XVECEXP (par, 0, 0)
632         = F (gen_rtx_SET (SImode,
633                           stack_pointer_rtx,
634                           gen_rtx_PLUS (SImode,
635                                         stack_pointer_rtx,
636                                         GEN_INT (-count * 4))));
637
638       /* Create each store.  */
639       pari = 1;
640       for (i = LAST_EXTENDED_REGNUM; i >= 0; i--)
641         if ((mask & (1 << i)) != 0)
642           {
643             rtx address = gen_rtx_PLUS (SImode,
644                                         stack_pointer_rtx,
645                                         GEN_INT (-pari * 4));
646             XVECEXP(par, 0, pari)
647               = F (gen_rtx_SET (VOIDmode,
648                                 gen_rtx_MEM (SImode, address),
649                                 gen_rtx_REG (SImode, i)));
650             pari += 1;
651           }
652
653       F (emit_insn (par));
654     }
655 }
656
657 void
658 mn10300_expand_prologue (void)
659 {
660   HOST_WIDE_INT size;
661
662   /* SIZE includes the fixed stack space needed for function calls.  */
663   size = get_frame_size () + crtl->outgoing_args_size;
664   size += (crtl->outgoing_args_size ? 4 : 0);
665
666   /* If we use any of the callee-saved registers, save them now.  */
667   mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs ());
668
669   if (TARGET_AM33_2 && fp_regs_to_save ())
670     {
671       int num_regs_to_save = fp_regs_to_save (), i;
672       HOST_WIDE_INT xsize;
673       enum
674       {
675         save_sp_merge,
676         save_sp_no_merge,
677         save_sp_partial_merge,
678         save_a0_merge,
679         save_a0_no_merge
680       } strategy;
681       unsigned int strategy_size = (unsigned)-1, this_strategy_size;
682       rtx reg;
683
684       /* We have several different strategies to save FP registers.
685          We can store them using SP offsets, which is beneficial if
686          there are just a few registers to save, or we can use `a0' in
687          post-increment mode (`a0' is the only call-clobbered address
688          register that is never used to pass information to a
689          function).  Furthermore, if we don't need a frame pointer, we
690          can merge the two SP adds into a single one, but this isn't
691          always beneficial; sometimes we can just split the two adds
692          so that we don't exceed a 16-bit constant size.  The code
693          below will select which strategy to use, so as to generate
694          smallest code.  Ties are broken in favor or shorter sequences
695          (in terms of number of instructions).  */
696
697 #define SIZE_ADD_AX(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
698                         : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 2)
699 #define SIZE_ADD_SP(S) ((((S) >= (1 << 15)) || ((S) < -(1 << 15))) ? 6 \
700                         : (((S) >= (1 << 7)) || ((S) < -(1 << 7))) ? 4 : 3)
701 #define SIZE_FMOV_LIMIT(S,N,L,SIZE1,SIZE2,ELSE) \
702   (((S) >= (L)) ? (SIZE1) * (N) \
703    : ((S) + 4 * (N) >= (L)) ? (((L) - (S)) / 4 * (SIZE2) \
704                                + ((S) + 4 * (N) - (L)) / 4 * (SIZE1)) \
705    : (ELSE))
706 #define SIZE_FMOV_SP_(S,N) \
707   (SIZE_FMOV_LIMIT ((S), (N), (1 << 24), 7, 6, \
708                    SIZE_FMOV_LIMIT ((S), (N), (1 << 8), 6, 4, \
709                                     (S) ? 4 * (N) : 3 + 4 * ((N) - 1))))
710 #define SIZE_FMOV_SP(S,N) (SIZE_FMOV_SP_ ((unsigned HOST_WIDE_INT)(S), (N)))
711
712       /* Consider alternative save_sp_merge only if we don't need the
713          frame pointer and size is nonzero.  */
714       if (! frame_pointer_needed && size)
715         {
716           /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
717           this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
718           /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
719           this_strategy_size += SIZE_FMOV_SP (size, num_regs_to_save);
720
721           if (this_strategy_size < strategy_size)
722             {
723               strategy = save_sp_merge;
724               strategy_size = this_strategy_size;
725             }
726         }
727
728       /* Consider alternative save_sp_no_merge unconditionally.  */
729       /* Insn: add -4 * num_regs_to_save, sp.  */
730       this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
731       /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
732       this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
733       if (size)
734         {
735           /* Insn: add -size, sp.  */
736           this_strategy_size += SIZE_ADD_SP (-size);
737         }
738
739       if (this_strategy_size < strategy_size)
740         {
741           strategy = save_sp_no_merge;
742           strategy_size = this_strategy_size;
743         }
744
745       /* Consider alternative save_sp_partial_merge only if we don't
746          need a frame pointer and size is reasonably large.  */
747       if (! frame_pointer_needed && size + 4 * num_regs_to_save > 128)
748         {
749           /* Insn: add -128, sp.  */
750           this_strategy_size = SIZE_ADD_SP (-128);
751           /* Insn: fmov fs#, (##, sp), for each fs# to be saved.  */
752           this_strategy_size += SIZE_FMOV_SP (128 - 4 * num_regs_to_save,
753                                               num_regs_to_save);
754           if (size)
755             {
756               /* Insn: add 128-size, sp.  */
757               this_strategy_size += SIZE_ADD_SP (128 - size);
758             }
759
760           if (this_strategy_size < strategy_size)
761             {
762               strategy = save_sp_partial_merge;
763               strategy_size = this_strategy_size;
764             }
765         }
766
767       /* Consider alternative save_a0_merge only if we don't need a
768          frame pointer, size is nonzero and the user hasn't
769          changed the calling conventions of a0.  */
770       if (! frame_pointer_needed && size
771           && call_really_used_regs [FIRST_ADDRESS_REGNUM]
772           && ! fixed_regs[FIRST_ADDRESS_REGNUM])
773         {
774           /* Insn: add -(size + 4 * num_regs_to_save), sp.  */
775           this_strategy_size = SIZE_ADD_SP (-(size + 4 * num_regs_to_save));
776           /* Insn: mov sp, a0.  */
777           this_strategy_size++;
778           if (size)
779             {
780               /* Insn: add size, a0.  */
781               this_strategy_size += SIZE_ADD_AX (size);
782             }
783           /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
784           this_strategy_size += 3 * num_regs_to_save;
785
786           if (this_strategy_size < strategy_size)
787             {
788               strategy = save_a0_merge;
789               strategy_size = this_strategy_size;
790             }
791         }
792
793       /* Consider alternative save_a0_no_merge if the user hasn't
794          changed the calling conventions of a0.  */
795       if (call_really_used_regs [FIRST_ADDRESS_REGNUM]
796           && ! fixed_regs[FIRST_ADDRESS_REGNUM])
797         {
798           /* Insn: add -4 * num_regs_to_save, sp.  */
799           this_strategy_size = SIZE_ADD_SP (-4 * num_regs_to_save);
800           /* Insn: mov sp, a0.  */
801           this_strategy_size++;
802           /* Insn: fmov fs#, (a0+), for each fs# to be saved.  */
803           this_strategy_size += 3 * num_regs_to_save;
804           if (size)
805             {
806               /* Insn: add -size, sp.  */
807               this_strategy_size += SIZE_ADD_SP (-size);
808             }
809
810           if (this_strategy_size < strategy_size)
811             {
812               strategy = save_a0_no_merge;
813               strategy_size = this_strategy_size;
814             }
815         }
816
817       /* Emit the initial SP add, common to all strategies.  */
818       switch (strategy)
819         {
820         case save_sp_no_merge:
821         case save_a0_no_merge:
822           F (emit_insn (gen_addsi3 (stack_pointer_rtx,
823                                     stack_pointer_rtx,
824                                     GEN_INT (-4 * num_regs_to_save))));
825           xsize = 0;
826           break;
827
828         case save_sp_partial_merge:
829           F (emit_insn (gen_addsi3 (stack_pointer_rtx,
830                                     stack_pointer_rtx,
831                                     GEN_INT (-128))));
832           xsize = 128 - 4 * num_regs_to_save;
833           size -= xsize;
834           break;
835
836         case save_sp_merge:
837         case save_a0_merge:
838           F (emit_insn (gen_addsi3 (stack_pointer_rtx,
839                                     stack_pointer_rtx,
840                                     GEN_INT (-(size + 4 * num_regs_to_save)))));
841           /* We'll have to adjust FP register saves according to the
842              frame size.  */
843           xsize = size;
844           /* Since we've already created the stack frame, don't do it
845              again at the end of the function.  */
846           size = 0;
847           break;
848
849         default:
850           gcc_unreachable ();
851         }
852
853       /* Now prepare register a0, if we have decided to use it.  */
854       switch (strategy)
855         {
856         case save_sp_merge:
857         case save_sp_no_merge:
858         case save_sp_partial_merge:
859           reg = 0;
860           break;
861
862         case save_a0_merge:
863         case save_a0_no_merge:
864           reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM);
865           F (emit_insn (gen_movsi (reg, stack_pointer_rtx)));
866           if (xsize)
867             F (emit_insn (gen_addsi3 (reg, reg, GEN_INT (xsize))));
868           reg = gen_rtx_POST_INC (SImode, reg);
869           break;
870
871         default:
872           gcc_unreachable ();
873         }
874
875       /* Now actually save the FP registers.  */
876       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
877         if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
878           {
879             rtx addr;
880
881             if (reg)
882               addr = reg;
883             else
884               {
885                 /* If we aren't using `a0', use an SP offset.  */
886                 if (xsize)
887                   {
888                     addr = gen_rtx_PLUS (SImode,
889                                          stack_pointer_rtx,
890                                          GEN_INT (xsize));
891                   }
892                 else
893                   addr = stack_pointer_rtx;
894
895                 xsize += 4;
896               }
897
898             F (emit_insn (gen_movsf (gen_rtx_MEM (SFmode, addr),
899                                      gen_rtx_REG (SFmode, i))));
900           }
901     }
902
903   /* Now put the frame pointer into the frame pointer register.  */
904   if (frame_pointer_needed)
905     F (emit_move_insn (frame_pointer_rtx, stack_pointer_rtx));
906
907   /* Allocate stack for this frame.  */
908   if (size)
909     F (emit_insn (gen_addsi3 (stack_pointer_rtx,
910                               stack_pointer_rtx,
911                               GEN_INT (-size))));
912
913   if (flag_pic && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
914     emit_insn (gen_GOTaddr2picreg ());
915 }
916
917 void
918 mn10300_expand_epilogue (void)
919 {
920   HOST_WIDE_INT size;
921
922   /* SIZE includes the fixed stack space needed for function calls.  */
923   size = get_frame_size () + crtl->outgoing_args_size;
924   size += (crtl->outgoing_args_size ? 4 : 0);
925   
926   if (TARGET_AM33_2 && fp_regs_to_save ())
927     {
928       int num_regs_to_save = fp_regs_to_save (), i;
929       rtx reg = 0;
930
931       /* We have several options to restore FP registers.  We could
932          load them from SP offsets, but, if there are enough FP
933          registers to restore, we win if we use a post-increment
934          addressing mode.  */
935
936       /* If we have a frame pointer, it's the best option, because we
937          already know it has the value we want.  */
938       if (frame_pointer_needed)
939         reg = gen_rtx_REG (SImode, FRAME_POINTER_REGNUM);
940       /* Otherwise, we may use `a1', since it's call-clobbered and
941          it's never used for return values.  But only do so if it's
942          smaller than using SP offsets.  */
943       else
944         {
945           enum { restore_sp_post_adjust,
946                  restore_sp_pre_adjust,
947                  restore_sp_partial_adjust,
948                  restore_a1 } strategy;
949           unsigned int this_strategy_size, strategy_size = (unsigned)-1;
950
951           /* Consider using sp offsets before adjusting sp.  */
952           /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
953           this_strategy_size = SIZE_FMOV_SP (size, num_regs_to_save);
954           /* If size is too large, we'll have to adjust SP with an
955                  add.  */
956           if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
957             {
958               /* Insn: add size + 4 * num_regs_to_save, sp.  */
959               this_strategy_size += SIZE_ADD_SP (size + 4 * num_regs_to_save);
960             }
961           /* If we don't have to restore any non-FP registers,
962                  we'll be able to save one byte by using rets.  */
963           if (! REG_SAVE_BYTES)
964             this_strategy_size--;
965
966           if (this_strategy_size < strategy_size)
967             {
968               strategy = restore_sp_post_adjust;
969               strategy_size = this_strategy_size;
970             }
971
972           /* Consider using sp offsets after adjusting sp.  */
973           /* Insn: add size, sp.  */
974           this_strategy_size = SIZE_ADD_SP (size);
975           /* Insn: fmov (##,sp),fs#, for each fs# to be restored.  */
976           this_strategy_size += SIZE_FMOV_SP (0, num_regs_to_save);
977           /* We're going to use ret to release the FP registers
978                  save area, so, no savings.  */
979
980           if (this_strategy_size < strategy_size)
981             {
982               strategy = restore_sp_pre_adjust;
983               strategy_size = this_strategy_size;
984             }
985
986           /* Consider using sp offsets after partially adjusting sp.
987              When size is close to 32Kb, we may be able to adjust SP
988              with an imm16 add instruction while still using fmov
989              (d8,sp).  */
990           if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
991             {
992               /* Insn: add size + 4 * num_regs_to_save
993                                 + REG_SAVE_BYTES - 252,sp.  */
994               this_strategy_size = SIZE_ADD_SP (size + 4 * num_regs_to_save
995                                                 + REG_SAVE_BYTES - 252);
996               /* Insn: fmov (##,sp),fs#, fo each fs# to be restored.  */
997               this_strategy_size += SIZE_FMOV_SP (252 - REG_SAVE_BYTES
998                                                   - 4 * num_regs_to_save,
999                                                   num_regs_to_save);
1000               /* We're going to use ret to release the FP registers
1001                  save area, so, no savings.  */
1002
1003               if (this_strategy_size < strategy_size)
1004                 {
1005                   strategy = restore_sp_partial_adjust;
1006                   strategy_size = this_strategy_size;
1007                 }
1008             }
1009
1010           /* Consider using a1 in post-increment mode, as long as the
1011              user hasn't changed the calling conventions of a1.  */
1012           if (call_really_used_regs [FIRST_ADDRESS_REGNUM + 1]
1013               && ! fixed_regs[FIRST_ADDRESS_REGNUM+1])
1014             {
1015               /* Insn: mov sp,a1.  */
1016               this_strategy_size = 1;
1017               if (size)
1018                 {
1019                   /* Insn: add size,a1.  */
1020                   this_strategy_size += SIZE_ADD_AX (size);
1021                 }
1022               /* Insn: fmov (a1+),fs#, for each fs# to be restored.  */
1023               this_strategy_size += 3 * num_regs_to_save;
1024               /* If size is large enough, we may be able to save a
1025                  couple of bytes.  */
1026               if (size + 4 * num_regs_to_save + REG_SAVE_BYTES > 255)
1027                 {
1028                   /* Insn: mov a1,sp.  */
1029                   this_strategy_size += 2;
1030                 }
1031               /* If we don't have to restore any non-FP registers,
1032                  we'll be able to save one byte by using rets.  */
1033               if (! REG_SAVE_BYTES)
1034                 this_strategy_size--;
1035
1036               if (this_strategy_size < strategy_size)
1037                 {
1038                   strategy = restore_a1;
1039                   strategy_size = this_strategy_size;
1040                 }
1041             }
1042
1043           switch (strategy)
1044             {
1045             case restore_sp_post_adjust:
1046               break;
1047
1048             case restore_sp_pre_adjust:
1049               emit_insn (gen_addsi3 (stack_pointer_rtx,
1050                                      stack_pointer_rtx,
1051                                      GEN_INT (size)));
1052               size = 0;
1053               break;
1054
1055             case restore_sp_partial_adjust:
1056               emit_insn (gen_addsi3 (stack_pointer_rtx,
1057                                      stack_pointer_rtx,
1058                                      GEN_INT (size + 4 * num_regs_to_save
1059                                               + REG_SAVE_BYTES - 252)));
1060               size = 252 - REG_SAVE_BYTES - 4 * num_regs_to_save;
1061               break;
1062
1063             case restore_a1:
1064               reg = gen_rtx_REG (SImode, FIRST_ADDRESS_REGNUM + 1);
1065               emit_insn (gen_movsi (reg, stack_pointer_rtx));
1066               if (size)
1067                 emit_insn (gen_addsi3 (reg, reg, GEN_INT (size)));
1068               break;
1069
1070             default:
1071               gcc_unreachable ();
1072             }
1073         }
1074
1075       /* Adjust the selected register, if any, for post-increment.  */
1076       if (reg)
1077         reg = gen_rtx_POST_INC (SImode, reg);
1078
1079       for (i = FIRST_FP_REGNUM; i <= LAST_FP_REGNUM; ++i)
1080         if (df_regs_ever_live_p (i) && ! call_really_used_regs [i])
1081           {
1082             rtx addr;
1083
1084             if (reg)
1085               addr = reg;
1086             else if (size)
1087               {
1088                 /* If we aren't using a post-increment register, use an
1089                    SP offset.  */
1090                 addr = gen_rtx_PLUS (SImode,
1091                                      stack_pointer_rtx,
1092                                      GEN_INT (size));
1093               }
1094             else
1095               addr = stack_pointer_rtx;
1096
1097             size += 4;
1098
1099             emit_insn (gen_movsf (gen_rtx_REG (SFmode, i),
1100                                   gen_rtx_MEM (SFmode, addr)));
1101           }
1102
1103       /* If we were using the restore_a1 strategy and the number of
1104          bytes to be released won't fit in the `ret' byte, copy `a1'
1105          to `sp', to avoid having to use `add' to adjust it.  */
1106       if (! frame_pointer_needed && reg && size + REG_SAVE_BYTES > 255)
1107         {
1108           emit_move_insn (stack_pointer_rtx, XEXP (reg, 0));
1109           size = 0;
1110         }
1111     }
1112
1113   /* Maybe cut back the stack, except for the register save area.
1114
1115      If the frame pointer exists, then use the frame pointer to
1116      cut back the stack.
1117
1118      If the stack size + register save area is more than 255 bytes,
1119      then the stack must be cut back here since the size + register
1120      save size is too big for a ret/retf instruction.
1121
1122      Else leave it alone, it will be cut back as part of the
1123      ret/retf instruction, or there wasn't any stack to begin with.
1124
1125      Under no circumstances should the register save area be
1126      deallocated here, that would leave a window where an interrupt
1127      could occur and trash the register save area.  */
1128   if (frame_pointer_needed)
1129     {
1130       emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
1131       size = 0;
1132     }
1133   else if (size + REG_SAVE_BYTES > 255)
1134     {
1135       emit_insn (gen_addsi3 (stack_pointer_rtx,
1136                              stack_pointer_rtx,
1137                              GEN_INT (size)));
1138       size = 0;
1139     }
1140
1141   /* Adjust the stack and restore callee-saved registers, if any.  */
1142   if (size || df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1143       || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1144       || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1145       || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1146       || frame_pointer_needed)
1147     emit_jump_insn (gen_return_internal_regs
1148                     (GEN_INT (size + REG_SAVE_BYTES)));
1149   else
1150     emit_jump_insn (gen_return_internal ());
1151 }
1152
1153 /* Recognize the PARALLEL rtx generated by mn10300_gen_multiple_store().
1154    This function is for MATCH_PARALLEL and so assumes OP is known to be
1155    parallel.  If OP is a multiple store, return a mask indicating which
1156    registers it saves.  Return 0 otherwise.  */
1157
1158 int
1159 mn10300_store_multiple_operation (rtx op,
1160                                   enum machine_mode mode ATTRIBUTE_UNUSED)
1161 {
1162   int count;
1163   int mask;
1164   int i;
1165   unsigned int last;
1166   rtx elt;
1167
1168   count = XVECLEN (op, 0);
1169   if (count < 2)
1170     return 0;
1171
1172   /* Check that first instruction has the form (set (sp) (plus A B)) */
1173   elt = XVECEXP (op, 0, 0);
1174   if (GET_CODE (elt) != SET
1175       || (! REG_P (SET_DEST (elt)))
1176       || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
1177       || GET_CODE (SET_SRC (elt)) != PLUS)
1178     return 0;
1179
1180   /* Check that A is the stack pointer and B is the expected stack size.
1181      For OP to match, each subsequent instruction should push a word onto
1182      the stack.  We therefore expect the first instruction to create
1183      COUNT-1 stack slots.  */
1184   elt = SET_SRC (elt);
1185   if ((! REG_P (XEXP (elt, 0)))
1186       || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1187       || (! CONST_INT_P (XEXP (elt, 1)))
1188       || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
1189     return 0;
1190
1191   /* Now go through the rest of the vector elements.  They must be
1192      ordered so that the first instruction stores the highest-numbered
1193      register to the highest stack slot and that subsequent instructions
1194      store a lower-numbered register to the slot below.
1195
1196      LAST keeps track of the smallest-numbered register stored so far.
1197      MASK is the set of stored registers.  */
1198   last = LAST_EXTENDED_REGNUM + 1;
1199   mask = 0;
1200   for (i = 1; i < count; i++)
1201     {
1202       /* Check that element i is a (set (mem M) R) and that R is valid.  */
1203       elt = XVECEXP (op, 0, i);
1204       if (GET_CODE (elt) != SET
1205           || (! MEM_P (SET_DEST (elt)))
1206           || (! REG_P (SET_SRC (elt)))
1207           || REGNO (SET_SRC (elt)) >= last)
1208         return 0;
1209
1210       /* R was OK, so provisionally add it to MASK.  We return 0 in any
1211          case if the rest of the instruction has a flaw.  */
1212       last = REGNO (SET_SRC (elt));
1213       mask |= (1 << last);
1214
1215       /* Check that M has the form (plus (sp) (const_int -I*4)) */
1216       elt = XEXP (SET_DEST (elt), 0);
1217       if (GET_CODE (elt) != PLUS
1218           || (! REG_P (XEXP (elt, 0)))
1219           || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
1220           || (! CONST_INT_P (XEXP (elt, 1)))
1221           || INTVAL (XEXP (elt, 1)) != -i * 4)
1222         return 0;
1223     }
1224
1225   /* All or none of the callee-saved extended registers must be in the set.  */
1226   if ((mask & 0x3c000) != 0
1227       && (mask & 0x3c000) != 0x3c000)
1228     return 0;
1229
1230   return mask;
1231 }
1232
1233 /* What (if any) secondary registers are needed to move IN with mode
1234    MODE into a register in register class RCLASS.
1235
1236    We might be able to simplify this.  */
1237
1238 enum reg_class
1239 mn10300_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
1240                                 rtx in)
1241 {
1242   rtx inner = in;
1243
1244   /* Strip off any SUBREG expressions from IN.  Basically we want
1245      to know if IN is a pseudo or (subreg (pseudo)) as those can
1246      turn into MEMs during reload.  */
1247   while (GET_CODE (inner) == SUBREG)
1248     inner = SUBREG_REG (inner);
1249
1250   /* Memory loads less than a full word wide can't have an
1251      address or stack pointer destination.  They must use
1252      a data register as an intermediate register.  */
1253   if ((MEM_P ((in))
1254        || (REG_P ((inner))
1255            && REGNO (inner) >= FIRST_PSEUDO_REGISTER))
1256       && (mode == QImode || mode == HImode)
1257       && (rclass == ADDRESS_REGS || rclass == SP_REGS
1258           || rclass == SP_OR_ADDRESS_REGS))
1259     {
1260       if (TARGET_AM33)
1261         return DATA_OR_EXTENDED_REGS;
1262       return DATA_REGS;
1263     }
1264
1265   /* We can't directly load sp + const_int into a data register;
1266      we must use an address register as an intermediate.  */
1267   if (rclass != SP_REGS
1268       && rclass != ADDRESS_REGS
1269       && rclass != SP_OR_ADDRESS_REGS
1270       && rclass != SP_OR_EXTENDED_REGS
1271       && rclass != ADDRESS_OR_EXTENDED_REGS
1272       && rclass != SP_OR_ADDRESS_OR_EXTENDED_REGS
1273       && (in == stack_pointer_rtx
1274           || (GET_CODE (in) == PLUS
1275               && (XEXP (in, 0) == stack_pointer_rtx
1276                   || XEXP (in, 1) == stack_pointer_rtx))))
1277     return ADDRESS_REGS;
1278
1279   if (TARGET_AM33_2
1280       && rclass == FP_REGS)
1281     {
1282       /* We can't load directly into an FP register from a      
1283          constant address.  */
1284       if (MEM_P ((in))
1285           && CONSTANT_ADDRESS_P (XEXP (in, 0)))
1286         return DATA_OR_EXTENDED_REGS;
1287
1288       /* Handle case were a pseudo may not get a hard register
1289          but has an equivalent memory location defined.  */
1290       if (REG_P ((inner))
1291           && REGNO (inner) >= FIRST_PSEUDO_REGISTER
1292           && reg_equiv_mem [REGNO (inner)]
1293           && CONSTANT_ADDRESS_P (XEXP (reg_equiv_mem [REGNO (inner)], 0)))
1294         return DATA_OR_EXTENDED_REGS;
1295     }
1296
1297   /* Otherwise assume no secondary reloads are needed.  */
1298   return NO_REGS;
1299 }
1300
1301 int
1302 mn10300_initial_offset (int from, int to)
1303 {
1304   /* The difference between the argument pointer and the frame pointer
1305      is the size of the callee register save area.  */
1306   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1307     {
1308       if (df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1309           || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1310           || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1311           || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1312           || fp_regs_to_save ()
1313           || frame_pointer_needed)
1314         return REG_SAVE_BYTES
1315           + 4 * fp_regs_to_save ();
1316       else
1317         return 0;
1318     }
1319
1320   /* The difference between the argument pointer and the stack pointer is
1321      the sum of the size of this function's frame, the callee register save
1322      area, and the fixed stack space needed for function calls (if any).  */
1323   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1324     {
1325       if (df_regs_ever_live_p (2) || df_regs_ever_live_p (3)
1326           || df_regs_ever_live_p (6) || df_regs_ever_live_p (7)
1327           || df_regs_ever_live_p (14) || df_regs_ever_live_p (15)
1328           || df_regs_ever_live_p (16) || df_regs_ever_live_p (17)
1329           || fp_regs_to_save ()
1330           || frame_pointer_needed)
1331         return (get_frame_size () + REG_SAVE_BYTES
1332                 + 4 * fp_regs_to_save ()
1333                 + (crtl->outgoing_args_size
1334                    ? crtl->outgoing_args_size + 4 : 0));
1335       else
1336         return (get_frame_size ()
1337                 + (crtl->outgoing_args_size
1338                    ? crtl->outgoing_args_size + 4 : 0));
1339     }
1340
1341   /* The difference between the frame pointer and stack pointer is the sum
1342      of the size of this function's frame and the fixed stack space needed
1343      for function calls (if any).  */
1344   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1345     return (get_frame_size ()
1346             + (crtl->outgoing_args_size
1347                ? crtl->outgoing_args_size + 4 : 0));
1348
1349   gcc_unreachable ();
1350 }
1351
1352 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
1353
1354 static bool
1355 mn10300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1356 {
1357   /* Return values > 8 bytes in length in memory.  */
1358   return (int_size_in_bytes (type) > 8
1359           || int_size_in_bytes (type) == 0
1360           || TYPE_MODE (type) == BLKmode);
1361 }
1362
1363 /* Flush the argument registers to the stack for a stdarg function;
1364    return the new argument pointer.  */
1365 static rtx
1366 mn10300_builtin_saveregs (void)
1367 {
1368   rtx offset, mem;
1369   tree fntype = TREE_TYPE (current_function_decl);
1370   int argadj = ((!stdarg_p (fntype))
1371                 ? UNITS_PER_WORD : 0);
1372   alias_set_type set = get_varargs_alias_set ();
1373
1374   if (argadj)
1375     offset = plus_constant (crtl->args.arg_offset_rtx, argadj);
1376   else
1377     offset = crtl->args.arg_offset_rtx;
1378
1379   mem = gen_rtx_MEM (SImode, crtl->args.internal_arg_pointer);
1380   set_mem_alias_set (mem, set);
1381   emit_move_insn (mem, gen_rtx_REG (SImode, 0));
1382
1383   mem = gen_rtx_MEM (SImode,
1384                      plus_constant (crtl->args.internal_arg_pointer, 4));
1385   set_mem_alias_set (mem, set);
1386   emit_move_insn (mem, gen_rtx_REG (SImode, 1));
1387
1388   return copy_to_reg (expand_binop (Pmode, add_optab,
1389                                     crtl->args.internal_arg_pointer,
1390                                     offset, 0, 0, OPTAB_LIB_WIDEN));
1391 }
1392
1393 static void
1394 mn10300_va_start (tree valist, rtx nextarg)
1395 {
1396   nextarg = expand_builtin_saveregs ();
1397   std_expand_builtin_va_start (valist, nextarg);
1398 }
1399
1400 /* Return true when a parameter should be passed by reference.  */
1401
1402 static bool
1403 mn10300_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
1404                            enum machine_mode mode, const_tree type,
1405                            bool named ATTRIBUTE_UNUSED)
1406 {
1407   unsigned HOST_WIDE_INT size;
1408
1409   if (type)
1410     size = int_size_in_bytes (type);
1411   else
1412     size = GET_MODE_SIZE (mode);
1413
1414   return (size > 8 || size == 0);
1415 }
1416
1417 /* Return an RTX to represent where a value with mode MODE will be returned
1418    from a function.  If the result is NULL_RTX, the argument is pushed.  */
1419
1420 rtx
1421 mn10300_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1422                       tree type, int named ATTRIBUTE_UNUSED)
1423 {
1424   rtx result = NULL_RTX;
1425   int size, align;
1426
1427   /* We only support using 2 data registers as argument registers.  */
1428   int nregs = 2;
1429
1430   /* Figure out the size of the object to be passed.  */
1431   if (mode == BLKmode)
1432     size = int_size_in_bytes (type);
1433   else
1434     size = GET_MODE_SIZE (mode);
1435
1436   /* Figure out the alignment of the object to be passed.  */
1437   align = size;
1438
1439   cum->nbytes = (cum->nbytes + 3) & ~3;
1440
1441   /* Don't pass this arg via a register if all the argument registers
1442      are used up.  */
1443   if (cum->nbytes > nregs * UNITS_PER_WORD)
1444     return result;
1445
1446   /* Don't pass this arg via a register if it would be split between
1447      registers and memory.  */
1448   if (type == NULL_TREE
1449       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1450     return result;
1451
1452   switch (cum->nbytes / UNITS_PER_WORD)
1453     {
1454     case 0:
1455       result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM);
1456       break;
1457     case 1:
1458       result = gen_rtx_REG (mode, FIRST_ARGUMENT_REGNUM + 1);
1459       break;
1460     default:
1461       break;
1462     }
1463
1464   return result;
1465 }
1466
1467 /* Return the number of bytes of registers to use for an argument passed
1468    partially in registers and partially in memory.  */
1469
1470 static int
1471 mn10300_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1472                            tree type, bool named ATTRIBUTE_UNUSED)
1473 {
1474   int size, align;
1475
1476   /* We only support using 2 data registers as argument registers.  */
1477   int nregs = 2;
1478
1479   /* Figure out the size of the object to be passed.  */
1480   if (mode == BLKmode)
1481     size = int_size_in_bytes (type);
1482   else
1483     size = GET_MODE_SIZE (mode);
1484
1485   /* Figure out the alignment of the object to be passed.  */
1486   align = size;
1487
1488   cum->nbytes = (cum->nbytes + 3) & ~3;
1489
1490   /* Don't pass this arg via a register if all the argument registers
1491      are used up.  */
1492   if (cum->nbytes > nregs * UNITS_PER_WORD)
1493     return 0;
1494
1495   if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1496     return 0;
1497
1498   /* Don't pass this arg via a register if it would be split between
1499      registers and memory.  */
1500   if (type == NULL_TREE
1501       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1502     return 0;
1503
1504   return nregs * UNITS_PER_WORD - cum->nbytes;
1505 }
1506
1507 /* Return the location of the function's value.  This will be either
1508    $d0 for integer functions, $a0 for pointers, or a PARALLEL of both
1509    $d0 and $a0 if the -mreturn-pointer-on-do flag is set.  Note that
1510    we only return the PARALLEL for outgoing values; we do not want
1511    callers relying on this extra copy.  */
1512
1513 static rtx
1514 mn10300_function_value (const_tree valtype,
1515                         const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1516                         bool outgoing)
1517 {
1518   rtx rv;
1519   enum machine_mode mode = TYPE_MODE (valtype);
1520
1521   if (! POINTER_TYPE_P (valtype))
1522     return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1523   else if (! TARGET_PTR_A0D0 || ! outgoing
1524            || cfun->returns_struct)
1525     return gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM);
1526
1527   rv = gen_rtx_PARALLEL (mode, rtvec_alloc (2));
1528   XVECEXP (rv, 0, 0)
1529     = gen_rtx_EXPR_LIST (VOIDmode,
1530                          gen_rtx_REG (mode, FIRST_ADDRESS_REGNUM),
1531                          GEN_INT (0));
1532
1533   XVECEXP (rv, 0, 1)
1534     = gen_rtx_EXPR_LIST (VOIDmode,
1535                          gen_rtx_REG (mode, FIRST_DATA_REGNUM),
1536                          GEN_INT (0));
1537   return rv;
1538 }
1539
1540 /* Implements TARGET_LIBCALL_VALUE.  */
1541
1542 static rtx
1543 mn10300_libcall_value (enum machine_mode mode,
1544                        const_rtx fun ATTRIBUTE_UNUSED)
1545 {
1546   return gen_rtx_REG (mode, FIRST_DATA_REGNUM);
1547 }
1548
1549 /* Implements FUNCTION_VALUE_REGNO_P.  */
1550
1551 bool
1552 mn10300_function_value_regno_p (const unsigned int regno)
1553 {
1554  return (regno == FIRST_DATA_REGNUM || regno == FIRST_ADDRESS_REGNUM);
1555 }
1556
1557 /* Output a compare insn.  */
1558
1559 const char *
1560 mn10300_output_cmp (rtx operand, rtx insn)
1561 {
1562   rtx temp;
1563   int past_call = 0;
1564
1565   /* We can save a byte if we can find a register which has the value
1566      zero in it.  */
1567   temp = PREV_INSN (insn);
1568   while (optimize && temp)
1569     {
1570       rtx set;
1571
1572       /* We allow the search to go through call insns.  We record
1573          the fact that we've past a CALL_INSN and reject matches which
1574          use call clobbered registers.  */
1575       if (LABEL_P (temp)
1576           || JUMP_P (temp)
1577           || GET_CODE (temp) == BARRIER)
1578         break;
1579
1580       if (CALL_P (temp))
1581         past_call = 1;
1582
1583       if (GET_CODE (temp) == NOTE)
1584         {
1585           temp = PREV_INSN (temp);
1586           continue;
1587         }
1588
1589       /* It must be an insn, see if it is a simple set.  */
1590       set = single_set (temp);
1591       if (!set)
1592         {
1593           temp = PREV_INSN (temp);
1594           continue;
1595         }
1596
1597       /* Are we setting a data register to zero (this does not win for
1598          address registers)?
1599
1600          If it's a call clobbered register, have we past a call?
1601
1602          Make sure the register we find isn't the same as ourself;
1603          the mn10300 can't encode that.
1604
1605          ??? reg_set_between_p return nonzero anytime we pass a CALL_INSN
1606          so the code to detect calls here isn't doing anything useful.  */
1607       if (REG_P (SET_DEST (set))
1608           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1609           && !reg_set_between_p (SET_DEST (set), temp, insn)
1610           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1611               == REGNO_REG_CLASS (REGNO (operand)))
1612           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) != EXTENDED_REGS
1613           && REGNO (SET_DEST (set)) != REGNO (operand)
1614           && (!past_call
1615               || ! call_really_used_regs [REGNO (SET_DEST (set))]))
1616         {
1617           rtx xoperands[2];
1618           xoperands[0] = operand;
1619           xoperands[1] = SET_DEST (set);
1620
1621           output_asm_insn ("cmp %1,%0", xoperands);
1622           return "";
1623         }
1624
1625       if (REGNO_REG_CLASS (REGNO (operand)) == EXTENDED_REGS
1626           && REG_P (SET_DEST (set))
1627           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1628           && !reg_set_between_p (SET_DEST (set), temp, insn)
1629           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1630               != REGNO_REG_CLASS (REGNO (operand)))
1631           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) == EXTENDED_REGS
1632           && REGNO (SET_DEST (set)) != REGNO (operand)
1633           && (!past_call
1634               || ! call_really_used_regs [REGNO (SET_DEST (set))]))
1635         {
1636           rtx xoperands[2];
1637           xoperands[0] = operand;
1638           xoperands[1] = SET_DEST (set);
1639
1640           output_asm_insn ("cmp %1,%0", xoperands);
1641           return "";
1642         }
1643       temp = PREV_INSN (temp);
1644     }
1645   return "cmp 0,%0";
1646 }
1647
1648 /* Similarly, but when using a zero_extract pattern for a btst where
1649    the source operand might end up in memory.  */
1650 int
1651 mn10300_mask_ok_for_mem_btst (int len, int bit)
1652 {
1653   unsigned int mask = 0;
1654
1655   while (len > 0)
1656     {
1657       mask |= (1 << bit);
1658       bit++;
1659       len--;
1660     }
1661
1662   /* MASK must bit into an 8bit value.  */
1663   return (((mask & 0xff) == mask)
1664           || ((mask & 0xff00) == mask)
1665           || ((mask & 0xff0000) == mask)
1666           || ((mask & 0xff000000) == mask));
1667 }
1668
1669 /* Return 1 if X contains a symbolic expression.  We know these
1670    expressions will have one of a few well defined forms, so
1671    we need only check those forms.  */
1672
1673 int
1674 mn10300_symbolic_operand (rtx op,
1675                           enum machine_mode mode ATTRIBUTE_UNUSED)
1676 {
1677   switch (GET_CODE (op))
1678     {
1679     case SYMBOL_REF:
1680     case LABEL_REF:
1681       return 1;
1682     case CONST:
1683       op = XEXP (op, 0);
1684       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1685                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1686               && CONST_INT_P ((XEXP (op, 1))));
1687     default:
1688       return 0;
1689     }
1690 }
1691
1692 /* Try machine dependent ways of modifying an illegitimate address
1693    to be legitimate.  If we find one, return the new valid address.
1694    This macro is used in only one place: `memory_address' in explow.c.
1695
1696    OLDX is the address as it was before break_out_memory_refs was called.
1697    In some cases it is useful to look at this to decide what needs to be done.
1698
1699    Normally it is always safe for this macro to do nothing.  It exists to
1700    recognize opportunities to optimize the output.
1701
1702    But on a few ports with segmented architectures and indexed addressing
1703    (mn10300, hppa) it is used to rewrite certain problematical addresses.  */
1704
1705 static rtx
1706 mn10300_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1707                             enum machine_mode mode ATTRIBUTE_UNUSED)
1708 {
1709   if (flag_pic && ! mn10300_legitimate_pic_operand_p (x))
1710     x = mn10300_legitimize_pic_address (oldx, NULL_RTX);
1711
1712   /* Uh-oh.  We might have an address for x[n-100000].  This needs
1713      special handling to avoid creating an indexed memory address
1714      with x-100000 as the base.  */
1715   if (GET_CODE (x) == PLUS
1716       && mn10300_symbolic_operand (XEXP (x, 1), VOIDmode))
1717     {
1718       /* Ugly.  We modify things here so that the address offset specified
1719          by the index expression is computed first, then added to x to form
1720          the entire address.  */
1721
1722       rtx regx1, regy1, regy2, y;
1723
1724       /* Strip off any CONST.  */
1725       y = XEXP (x, 1);
1726       if (GET_CODE (y) == CONST)
1727         y = XEXP (y, 0);
1728
1729       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1730         {
1731           regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1732           regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1733           regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1734           regx1 = force_reg (Pmode,
1735                              gen_rtx_fmt_ee (GET_CODE (y), Pmode, regx1,
1736                                              regy2));
1737           return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1738         }
1739     }
1740   return x;
1741 }
1742
1743 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
1744    @GOTOFF in `reg'.  */
1745
1746 rtx
1747 mn10300_legitimize_pic_address (rtx orig, rtx reg)
1748 {
1749   if (GET_CODE (orig) == LABEL_REF
1750       || (GET_CODE (orig) == SYMBOL_REF
1751           && (CONSTANT_POOL_ADDRESS_P (orig)
1752               || ! MN10300_GLOBAL_P (orig))))
1753     {
1754       if (reg == 0)
1755         reg = gen_reg_rtx (Pmode);
1756
1757       emit_insn (gen_symGOTOFF2reg (reg, orig));
1758       return reg;
1759     }
1760   else if (GET_CODE (orig) == SYMBOL_REF)
1761     {
1762       if (reg == 0)
1763         reg = gen_reg_rtx (Pmode);
1764
1765       emit_insn (gen_symGOT2reg (reg, orig));
1766       return reg;
1767     }
1768   return orig;
1769 }
1770
1771 /* Return zero if X references a SYMBOL_REF or LABEL_REF whose symbol
1772    isn't protected by a PIC unspec; nonzero otherwise.  */
1773
1774 int
1775 mn10300_legitimate_pic_operand_p (rtx x)
1776 {
1777   const char *fmt;
1778   int i;
1779
1780   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1781     return 0;
1782
1783   if (GET_CODE (x) == UNSPEC
1784       && (XINT (x, 1) == UNSPEC_PIC
1785           || XINT (x, 1) == UNSPEC_GOT
1786           || XINT (x, 1) == UNSPEC_GOTOFF
1787           || XINT (x, 1) == UNSPEC_PLT
1788           || XINT (x, 1) == UNSPEC_GOTSYM_OFF))
1789       return 1;
1790
1791   fmt = GET_RTX_FORMAT (GET_CODE (x));
1792   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1793     {
1794       if (fmt[i] == 'E')
1795         {
1796           int j;
1797
1798           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1799             if (! mn10300_legitimate_pic_operand_p (XVECEXP (x, i, j)))
1800               return 0;
1801         }
1802       else if (fmt[i] == 'e'
1803                && ! mn10300_legitimate_pic_operand_p (XEXP (x, i)))
1804         return 0;
1805     }
1806
1807   return 1;
1808 }
1809
1810 /* Return TRUE if the address X, taken from a (MEM:MODE X) rtx, is
1811    legitimate, and FALSE otherwise.
1812
1813    On the mn10300, the value in the address register must be
1814    in the same memory space/segment as the effective address.
1815
1816    This is problematical for reload since it does not understand
1817    that base+index != index+base in a memory reference.
1818
1819    Note it is still possible to use reg+reg addressing modes,
1820    it's just much more difficult.  For a discussion of a possible
1821    workaround and solution, see the comments in pa.c before the
1822    function record_unscaled_index_insn_codes.  */
1823
1824 static bool
1825 mn10300_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
1826 {
1827   if (CONSTANT_ADDRESS_P (x)
1828       && (! flag_pic || mn10300_legitimate_pic_operand_p (x)))
1829     return TRUE;
1830
1831   if (RTX_OK_FOR_BASE_P (x, strict))
1832     return TRUE;
1833
1834   if (TARGET_AM33
1835       && GET_CODE (x) == POST_INC
1836       && RTX_OK_FOR_BASE_P (XEXP (x, 0), strict)
1837       && (mode == SImode || mode == SFmode || mode == HImode))
1838     return TRUE;
1839
1840   if (GET_CODE (x) == PLUS)
1841     {
1842       rtx base = 0, index = 0;
1843
1844       if (REG_P (XEXP (x, 0))
1845           && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 0)), strict))
1846         {
1847           base = XEXP (x, 0);
1848           index = XEXP (x, 1);
1849         }
1850
1851       if (REG_P (XEXP (x, 1))
1852           && REGNO_STRICT_OK_FOR_BASE_P (REGNO (XEXP (x, 1)), strict))
1853         {
1854           base = XEXP (x, 1);
1855           index = XEXP (x, 0);
1856         }
1857
1858       if (base != 0 && index != 0)
1859         {
1860           if (CONST_INT_P ((index)))
1861             return TRUE;
1862           if (GET_CODE (index) == CONST
1863               && GET_CODE (XEXP (index, 0)) != PLUS
1864               && (! flag_pic
1865                   || (mn10300_legitimate_pic_operand_p (index)
1866                       && GET_MODE_SIZE (mode) == 4)))
1867             return TRUE;
1868         }
1869     }
1870
1871   return FALSE;
1872 }
1873
1874 /* Used by LEGITIMATE_CONSTANT_P().  Returns TRUE if X is a valid
1875    constant.  Note that some "constants" aren't valid, such as TLS
1876    symbols and unconverted GOT-based references, so we eliminate
1877    those here.  */
1878
1879 bool
1880 mn10300_legitimate_constant_p (rtx x)
1881 {
1882   switch (GET_CODE (x))
1883     {
1884     case CONST:
1885       x = XEXP (x, 0);
1886
1887       if (GET_CODE (x) == PLUS)
1888         {
1889           if (! CONST_INT_P (XEXP (x, 1)))
1890             return false;
1891           x = XEXP (x, 0);
1892         }
1893
1894       /* Only some unspecs are valid as "constants".  */
1895       if (GET_CODE (x) == UNSPEC)
1896         {
1897           rtx sym = XVECEXP (x, 0, 0);
1898           switch (XINT (x, 1))
1899             {
1900             case UNSPEC_INT_LABEL:
1901             case UNSPEC_PIC:
1902             case UNSPEC_GOT:
1903             case UNSPEC_GOTOFF:
1904             case UNSPEC_PLT:
1905               return true;
1906             default:
1907               return false;
1908             }
1909         }
1910
1911       /* We must have drilled down to a symbol.  */
1912       if (! mn10300_symbolic_operand (x, Pmode))
1913         return false;
1914       break;
1915
1916     default:
1917       break;
1918     }
1919
1920   return true;
1921 }
1922
1923 static int
1924 mn10300_address_cost_1 (rtx x, int *unsig)
1925 {
1926   switch (GET_CODE (x))
1927     {
1928     case REG:
1929       switch (REGNO_REG_CLASS (REGNO (x)))
1930         {
1931         case SP_REGS:
1932           *unsig = 1;
1933           return 0;
1934
1935         case ADDRESS_REGS:
1936           return 1;
1937
1938         case DATA_REGS:
1939         case EXTENDED_REGS:
1940         case FP_REGS:
1941           return 3;
1942
1943         case NO_REGS:
1944           return 5;
1945
1946         default:
1947           gcc_unreachable ();
1948         }
1949
1950     case PLUS:
1951     case MINUS:
1952     case ASHIFT:
1953     case AND:
1954     case IOR:
1955       return (mn10300_address_cost_1 (XEXP (x, 0), unsig)
1956               + mn10300_address_cost_1 (XEXP (x, 1), unsig));
1957
1958     case EXPR_LIST:
1959     case SUBREG:
1960     case MEM:
1961       return mn10300_address_cost (XEXP (x, 0), !optimize_size);
1962
1963     case ZERO_EXTEND:
1964       *unsig = 1;
1965       return mn10300_address_cost_1 (XEXP (x, 0), unsig);
1966
1967     case CONST_INT:
1968       if (INTVAL (x) == 0)
1969         return 0;
1970       if (INTVAL (x) + (*unsig ? 0 : 0x80) < 0x100)
1971         return 1;
1972       if (INTVAL (x) + (*unsig ? 0 : 0x8000) < 0x10000)
1973         return 3;
1974       if (INTVAL (x) + (*unsig ? 0 : 0x800000) < 0x1000000)
1975         return 5;
1976       return 7;
1977
1978     case CONST:
1979     case SYMBOL_REF:
1980     case LABEL_REF:
1981       return 8;
1982
1983     default:
1984       gcc_unreachable ();
1985
1986     }
1987 }
1988
1989 static int
1990 mn10300_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
1991 {
1992   int s = 0;
1993   return mn10300_address_cost_1 (x, &s);
1994 }
1995
1996 static bool
1997 mn10300_rtx_costs (rtx x, int code, int outer_code, int *total,
1998                    bool speed ATTRIBUTE_UNUSED)
1999 {
2000   switch (code)
2001     {
2002     case CONST_INT:
2003       /* Zeros are extremely cheap.  */
2004       if (INTVAL (x) == 0 && (outer_code == SET || outer_code == COMPARE))
2005         *total = 0;
2006       /* If it fits in 8 bits, then it's still relatively cheap.  */
2007       else if (INT_8_BITS (INTVAL (x)))
2008         *total = 1;
2009       /* This is the "base" cost, includes constants where either the
2010          upper or lower 16bits are all zeros.  */
2011       else if (INT_16_BITS (INTVAL (x))
2012                || (INTVAL (x) & 0xffff) == 0
2013                || (INTVAL (x) & 0xffff0000) == 0)
2014         *total = 2;
2015       else
2016         *total = 4;
2017       return true;
2018
2019     case CONST:
2020     case LABEL_REF:
2021     case SYMBOL_REF:
2022       /* These are more costly than a CONST_INT, but we can relax them,
2023          so they're less costly than a CONST_DOUBLE.  */
2024       *total = 6;
2025       return true;
2026
2027     case CONST_DOUBLE:
2028       /* We don't optimize CONST_DOUBLEs well nor do we relax them well,
2029          so their cost is very high.  */
2030       *total = 8;
2031       return true;
2032
2033     case ZERO_EXTRACT:
2034       /* This is cheap, we can use btst.  */
2035       if (outer_code == COMPARE)
2036         *total = 0;
2037       return false;
2038
2039    /* ??? This probably needs more work.  */
2040     case MOD:
2041     case DIV:
2042     case MULT:
2043       *total = 8;
2044       return true;
2045
2046     default:
2047       return false;
2048     }
2049 }
2050
2051 /* Check whether a constant used to initialize a DImode or DFmode can
2052    use a clr instruction.  The code here must be kept in sync with
2053    movdf and movdi.  */
2054
2055 bool
2056 mn10300_wide_const_load_uses_clr (rtx operands[2])
2057 {
2058   long val[2] = {0, 0};
2059
2060   if (! REG_P (operands[0])
2061       || REGNO_REG_CLASS (REGNO (operands[0])) != DATA_REGS)
2062     return false;
2063
2064   switch (GET_CODE (operands[1]))
2065     {
2066     case CONST_INT:
2067       {
2068         rtx low, high;
2069         split_double (operands[1], &low, &high);
2070         val[0] = INTVAL (low);
2071         val[1] = INTVAL (high);
2072       }
2073       break;
2074
2075     case CONST_DOUBLE:
2076       if (GET_MODE (operands[1]) == DFmode)
2077         {
2078           REAL_VALUE_TYPE rv;
2079
2080           REAL_VALUE_FROM_CONST_DOUBLE (rv, operands[1]);
2081           REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
2082         }
2083       else if (GET_MODE (operands[1]) == VOIDmode
2084                || GET_MODE (operands[1]) == DImode)
2085         {
2086           val[0] = CONST_DOUBLE_LOW (operands[1]);
2087           val[1] = CONST_DOUBLE_HIGH (operands[1]);
2088         }
2089       break;
2090
2091     default:
2092       return false;
2093     }
2094
2095   return val[0] == 0 || val[1] == 0;
2096 }
2097 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
2098    may access it using GOTOFF instead of GOT.  */
2099
2100 static void
2101 mn10300_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
2102 {
2103   rtx symbol;
2104
2105   if (! MEM_P (rtl))
2106     return;
2107   symbol = XEXP (rtl, 0);
2108   if (GET_CODE (symbol) != SYMBOL_REF)
2109     return;
2110
2111   if (flag_pic)
2112     SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
2113 }
2114
2115 /* Dispatch tables on the mn10300 are extremely expensive in terms of code
2116    and readonly data size.  So we crank up the case threshold value to
2117    encourage a series of if/else comparisons to implement many small switch
2118    statements.  In theory, this value could be increased much more if we
2119    were solely optimizing for space, but we keep it "reasonable" to avoid
2120    serious code efficiency lossage.  */
2121
2122 static unsigned int
2123 mn10300_case_values_threshold (void)
2124 {
2125   return 6;
2126 }
2127
2128 /* Worker function for TARGET_ASM_TRAMPOLINE_TEMPLATE.  */
2129
2130 static void
2131 mn10300_asm_trampoline_template (FILE *f)
2132 {
2133   fprintf (f, "\tadd -4,sp\n");
2134   fprintf (f, "\t.long 0x0004fffa\n");
2135   fprintf (f, "\tmov (0,sp),a0\n");
2136   fprintf (f, "\tadd 4,sp\n");
2137   fprintf (f, "\tmov (13,a0),a1\n");    
2138   fprintf (f, "\tmov (17,a0),a0\n");
2139   fprintf (f, "\tjmp (a0)\n");
2140   fprintf (f, "\t.long 0\n");
2141   fprintf (f, "\t.long 0\n");
2142 }
2143
2144 /* Worker function for TARGET_TRAMPOLINE_INIT.  */
2145
2146 static void
2147 mn10300_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
2148 {
2149   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
2150   rtx mem;
2151
2152   emit_block_move (m_tramp, assemble_trampoline_template (),
2153                    GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
2154
2155   mem = adjust_address (m_tramp, SImode, 0x14);
2156   emit_move_insn (mem, chain_value);
2157   mem = adjust_address (m_tramp, SImode, 0x18);
2158   emit_move_insn (mem, fnaddr);
2159 }
2160
2161 /* Output the assembler code for a C++ thunk function.
2162    THUNK_DECL is the declaration for the thunk function itself, FUNCTION
2163    is the decl for the target function.  DELTA is an immediate constant
2164    offset to be added to the THIS parameter.  If VCALL_OFFSET is nonzero
2165    the word at the adjusted address *(*THIS' + VCALL_OFFSET) should be
2166    additionally added to THIS.  Finally jump to the entry point of
2167    FUNCTION.  */
2168
2169 static void
2170 mn10300_asm_output_mi_thunk (FILE *        file,
2171                              tree          thunk_fndecl ATTRIBUTE_UNUSED,
2172                              HOST_WIDE_INT delta,
2173                              HOST_WIDE_INT vcall_offset,
2174                              tree          function)
2175 {
2176   const char * _this;
2177
2178   /* Get the register holding the THIS parameter.  Handle the case
2179      where there is a hidden first argument for a returned structure.  */
2180   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
2181     _this = reg_names [FIRST_ARGUMENT_REGNUM + 1];
2182   else
2183     _this = reg_names [FIRST_ARGUMENT_REGNUM];
2184
2185   fprintf (file, "\t%s Thunk Entry Point:\n", ASM_COMMENT_START);
2186
2187   if (delta)
2188     fprintf (file, "\tadd %d, %s\n", (int) delta, _this);
2189
2190   if (vcall_offset)
2191     {
2192       const char * scratch = reg_names [FIRST_ADDRESS_REGNUM + 1];
2193
2194       fprintf (file, "\tmov %s, %s\n", _this, scratch);
2195       fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2196       fprintf (file, "\tadd %d, %s\n", (int) vcall_offset, scratch);
2197       fprintf (file, "\tmov (%s), %s\n", scratch, scratch);
2198       fprintf (file, "\tadd %s, %s\n", scratch, _this);
2199     }
2200
2201   fputs ("\tjmp ", file);
2202   assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
2203   putc ('\n', file);
2204 }
2205
2206 /* Return true if mn10300_output_mi_thunk would be able to output the
2207    assembler code for the thunk function specified by the arguments
2208    it is passed, and false otherwise.  */
2209
2210 static bool
2211 mn10300_can_output_mi_thunk (const_tree    thunk_fndecl ATTRIBUTE_UNUSED,
2212                              HOST_WIDE_INT delta        ATTRIBUTE_UNUSED,
2213                              HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
2214                              const_tree    function     ATTRIBUTE_UNUSED)
2215 {
2216   return true;
2217 }
2218
2219 bool
2220 mn10300_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
2221 {
2222   if (REGNO_REG_CLASS (regno) == FP_REGS
2223       || REGNO_REG_CLASS (regno) == FP_ACC_REGS)
2224     /* Do not store integer values in FP registers.  */
2225     return GET_MODE_CLASS (mode) == MODE_FLOAT && ((regno & 1) == 0);
2226   
2227   if (((regno) & 1) == 0 || GET_MODE_SIZE (mode) == 4)
2228     return true;
2229
2230   if (REGNO_REG_CLASS (regno) == DATA_REGS
2231       || (TARGET_AM33 && REGNO_REG_CLASS (regno) == ADDRESS_REGS)
2232       || REGNO_REG_CLASS (regno) == EXTENDED_REGS)
2233     return GET_MODE_SIZE (mode) <= 4;
2234   
2235   return false;
2236 }
2237
2238 bool
2239 mn10300_modes_tieable (enum machine_mode mode1, enum machine_mode mode2)
2240 {
2241   if (GET_MODE_CLASS (mode1) == MODE_FLOAT
2242       && GET_MODE_CLASS (mode2) != MODE_FLOAT)
2243     return false;
2244
2245   if (GET_MODE_CLASS (mode2) == MODE_FLOAT
2246       && GET_MODE_CLASS (mode1) != MODE_FLOAT)
2247     return false;
2248
2249   if (TARGET_AM33
2250       || mode1 == mode2
2251       || (GET_MODE_SIZE (mode1) <= 4 && GET_MODE_SIZE (mode2) <= 4))
2252     return true;
2253
2254   return false;
2255 }
2256
2257 enum machine_mode
2258 mn10300_select_cc_mode (rtx x)
2259 {
2260   return (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT) ? CC_FLOATmode : CCmode;
2261 }
2262 \f
2263 /* Initialize the GCC target structure.  */
2264
2265 #undef  TARGET_EXCEPT_UNWIND_INFO
2266 #define TARGET_EXCEPT_UNWIND_INFO sjlj_except_unwind_info
2267
2268 #undef  TARGET_ASM_ALIGNED_HI_OP
2269 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
2270
2271 #undef  TARGET_LEGITIMIZE_ADDRESS
2272 #define TARGET_LEGITIMIZE_ADDRESS mn10300_legitimize_address
2273
2274 #undef  TARGET_RTX_COSTS
2275 #define TARGET_RTX_COSTS mn10300_rtx_costs
2276 #undef  TARGET_ADDRESS_COST
2277 #define TARGET_ADDRESS_COST mn10300_address_cost
2278
2279 #undef  TARGET_ASM_FILE_START
2280 #define TARGET_ASM_FILE_START mn10300_file_start
2281 #undef  TARGET_ASM_FILE_START_FILE_DIRECTIVE
2282 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
2283
2284 #undef  TARGET_DEFAULT_TARGET_FLAGS
2285 #define TARGET_DEFAULT_TARGET_FLAGS MASK_MULT_BUG | MASK_PTR_A0D0
2286 #undef  TARGET_HANDLE_OPTION
2287 #define TARGET_HANDLE_OPTION mn10300_handle_option
2288 #undef  TARGET_OPTION_OVERRIDE
2289 #define TARGET_OPTION_OVERRIDE mn10300_option_override
2290 #undef  TARGET_OPTION_OPTIMIZATION_TABLE
2291 #define TARGET_OPTION_OPTIMIZATION_TABLE mn10300_option_optimization_table
2292
2293 #undef  TARGET_ENCODE_SECTION_INFO
2294 #define TARGET_ENCODE_SECTION_INFO mn10300_encode_section_info
2295
2296 #undef  TARGET_PROMOTE_PROTOTYPES
2297 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
2298 #undef  TARGET_RETURN_IN_MEMORY
2299 #define TARGET_RETURN_IN_MEMORY mn10300_return_in_memory
2300 #undef  TARGET_PASS_BY_REFERENCE
2301 #define TARGET_PASS_BY_REFERENCE mn10300_pass_by_reference
2302 #undef  TARGET_CALLEE_COPIES
2303 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true
2304 #undef  TARGET_ARG_PARTIAL_BYTES
2305 #define TARGET_ARG_PARTIAL_BYTES mn10300_arg_partial_bytes
2306
2307 #undef  TARGET_EXPAND_BUILTIN_SAVEREGS
2308 #define TARGET_EXPAND_BUILTIN_SAVEREGS mn10300_builtin_saveregs
2309 #undef  TARGET_EXPAND_BUILTIN_VA_START
2310 #define TARGET_EXPAND_BUILTIN_VA_START mn10300_va_start
2311
2312 #undef  TARGET_CASE_VALUES_THRESHOLD
2313 #define TARGET_CASE_VALUES_THRESHOLD mn10300_case_values_threshold
2314
2315 #undef  TARGET_LEGITIMATE_ADDRESS_P
2316 #define TARGET_LEGITIMATE_ADDRESS_P     mn10300_legitimate_address_p
2317
2318 #undef  TARGET_ASM_TRAMPOLINE_TEMPLATE
2319 #define TARGET_ASM_TRAMPOLINE_TEMPLATE mn10300_asm_trampoline_template
2320 #undef  TARGET_TRAMPOLINE_INIT
2321 #define TARGET_TRAMPOLINE_INIT mn10300_trampoline_init
2322
2323 #undef  TARGET_FUNCTION_VALUE
2324 #define TARGET_FUNCTION_VALUE mn10300_function_value
2325 #undef  TARGET_LIBCALL_VALUE
2326 #define TARGET_LIBCALL_VALUE mn10300_libcall_value
2327
2328 #undef  TARGET_ASM_OUTPUT_MI_THUNK
2329 #define TARGET_ASM_OUTPUT_MI_THUNK      mn10300_asm_output_mi_thunk
2330 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
2331 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK  mn10300_can_output_mi_thunk
2332
2333 struct gcc_target targetm = TARGET_INITIALIZER;