OSDN Git Service

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