OSDN Git Service

* mkconfig.sh: Include insn-flags.h.
[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 Free Software Foundation, Inc.
3    Contributed by Jeff Law (law@cygnus.com).
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "real.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "obstack.h"
38 #include "toplev.h"
39 #include "tm_p.h"
40
41 /* The size of the callee register save area.  Right now we save everything
42    on entry since it costs us nothing in code size.  It does cost us from a
43    speed standpoint, so we want to optimize this sooner or later.  */
44 #define REG_SAVE_BYTES (4 * regs_ever_live[2] \
45                         + 4 * regs_ever_live[3] \
46                         + 4 * regs_ever_live[6] \
47                         + 4 * regs_ever_live[7] \
48                         + 16 * (regs_ever_live[14] || regs_ever_live[15] \
49                                 || regs_ever_live[16] || regs_ever_live[17]))
50
51 void
52 asm_file_start (file)
53      FILE *file;
54 {
55   fprintf (file, "#\tGCC For the Matsushita MN10300\n");
56   if (optimize)
57     fprintf (file, "# -O%d\n", optimize);
58   else
59     fprintf (file, "\n\n");
60
61   if (TARGET_AM33)
62     fprintf (file, "\t.am33\n");
63   output_file_directive (file, main_input_filename);
64 }
65 \f
66
67 /* Print operand X using operand code CODE to assembly language output file
68    FILE.  */
69
70 void
71 print_operand (file, x, code)
72      FILE *file;
73      rtx x;
74      int code;
75 {
76   switch (code)
77     {
78       case 'b':
79       case 'B':
80         /* These are normal and reversed branches.  */
81         switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
82           {
83           case NE:
84             fprintf (file, "ne");
85             break;
86           case EQ:
87             fprintf (file, "eq");
88             break;
89           case GE:
90             fprintf (file, "ge");
91             break;
92           case GT:
93             fprintf (file, "gt");
94             break;
95           case LE:
96             fprintf (file, "le");
97             break;
98           case LT:
99             fprintf (file, "lt");
100             break;
101           case GEU:
102             fprintf (file, "cc");
103             break;
104           case GTU:
105             fprintf (file, "hi");
106             break;
107           case LEU:
108             fprintf (file, "ls");
109             break;
110           case LTU:
111             fprintf (file, "cs");
112             break;
113           default:
114             abort ();
115           }
116         break;
117       case 'C':
118         /* This is used for the operand to a call instruction;
119            if it's a REG, enclose it in parens, else output
120            the operand normally.  */
121         if (GET_CODE (x) == REG)
122           {
123             fputc ('(', file);
124             print_operand (file, x, 0);
125             fputc (')', file);
126           }
127         else
128           print_operand (file, x, 0);
129         break;
130      
131       /* These are the least significant word in a 64bit value.  */
132       case 'L':
133         switch (GET_CODE (x))
134           {
135           case MEM:
136             fputc ('(', file);
137             output_address (XEXP (x, 0));
138             fputc (')', file);
139             break;
140
141           case REG:
142             fprintf (file, "%s", reg_names[REGNO (x)]);
143             break;
144
145           case SUBREG:
146             fprintf (file, "%s",
147                      reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)]);
148             break;
149
150           case CONST_DOUBLE:
151               {
152                 long val[2];
153                 REAL_VALUE_TYPE rv;
154
155                 switch (GET_MODE (x))
156                   {
157                     case DFmode:
158                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
159                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
160                       fprintf (file, "0x%lx", val[0]);
161                       break;;
162                     case SFmode:
163                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
164                       REAL_VALUE_TO_TARGET_SINGLE (rv, val[0]);
165                       fprintf (file, "0x%lx", val[0]);
166                       break;;
167                     case VOIDmode:
168                     case DImode:
169                       print_operand_address (file,
170                                              GEN_INT (CONST_DOUBLE_LOW (x)));
171                       break;
172                     default:
173                       break;
174                   }
175                 break;
176               }
177
178           case CONST_INT:
179             {
180               rtx low, high;
181               split_double (x, &low, &high);
182               fprintf (file, "%ld", (long)INTVAL (low));
183               break;
184             }
185
186           default:
187             abort ();
188           }
189         break;
190
191       /* Similarly, but for the most significant word.  */
192       case 'H':
193         switch (GET_CODE (x))
194           {
195           case MEM:
196             fputc ('(', file);
197             x = adj_offsettable_operand (x, 4);
198             output_address (XEXP (x, 0));
199             fputc (')', file);
200             break;
201
202           case REG:
203             fprintf (file, "%s", reg_names[REGNO (x) + 1]);
204             break;
205
206           case SUBREG:
207             fprintf (file, "%s",
208                      reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)] + 1);
209             break;
210
211           case CONST_DOUBLE:
212               {
213                 long val[2];
214                 REAL_VALUE_TYPE rv;
215
216                 switch (GET_MODE (x))
217                   {
218                     case DFmode:
219                       REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
220                       REAL_VALUE_TO_TARGET_DOUBLE (rv, val);
221                       fprintf (file, "0x%lx", val[1]);
222                       break;;
223                     case SFmode:
224                       abort ();
225                     case VOIDmode:
226                     case DImode:
227                       print_operand_address (file, 
228                                              GEN_INT (CONST_DOUBLE_HIGH (x)));
229                       break;
230                     default:
231                       break;
232                   }
233                 break;
234               }
235
236           case CONST_INT:
237             {
238               rtx low, high;
239               split_double (x, &low, &high);
240               fprintf (file, "%ld", (long)INTVAL (high));
241               break;
242             }
243
244           default:
245             abort ();
246           }
247         break;
248
249       case 'A':
250         fputc ('(', file);
251         if (GET_CODE (XEXP (x, 0)) == REG)
252           output_address (gen_rtx_PLUS (SImode, XEXP (x, 0), GEN_INT (0)));
253         else
254           output_address (XEXP (x, 0));
255         fputc (')', file);
256         break;
257
258       case 'N':
259         output_address (GEN_INT ((~INTVAL (x)) & 0xff));
260         break;
261
262       /* For shift counts.  The hardware ignores the upper bits of
263          any immediate, but the assembler will flag an out of range
264          shift count as an error.  So we mask off the high bits
265          of the immediate here.  */
266       case 'S':
267         if (GET_CODE (x) == CONST_INT)
268           {
269             fprintf (file, "%d", INTVAL (x) & 0x1f);
270             break;
271           }
272         /* FALL THROUGH */
273
274       default:
275         switch (GET_CODE (x))
276           {
277           case MEM:
278             fputc ('(', file);
279             output_address (XEXP (x, 0));
280             fputc (')', file);
281             break;
282
283           case PLUS:
284             output_address (x);
285             break;
286
287           case REG:
288             fprintf (file, "%s", reg_names[REGNO (x)]);
289             break;
290
291           case SUBREG:
292             fprintf (file, "%s",
293                      reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)]);
294             break;
295
296           /* This will only be single precision....  */
297           case CONST_DOUBLE:
298             {
299               unsigned long val;
300               REAL_VALUE_TYPE rv;
301
302               REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
303               REAL_VALUE_TO_TARGET_SINGLE (rv, val);
304               fprintf (file, "0x%lx", val);
305               break;
306             }
307
308           case CONST_INT:
309           case SYMBOL_REF:
310           case CONST:
311           case LABEL_REF:
312           case CODE_LABEL:
313             print_operand_address (file, x);
314             break;
315           default:
316             abort ();
317           }
318         break;
319    }
320 }
321
322 /* Output assembly language output for the address ADDR to FILE.  */
323
324 void
325 print_operand_address (file, addr)
326      FILE *file;
327      rtx addr;
328 {
329   switch (GET_CODE (addr))
330     {
331     case POST_INC:
332       print_operand_address (file, XEXP (addr, 0));
333       fputc ('+', file);
334       break;
335     case REG:
336       print_operand (file, addr, 0);
337       break;
338     case PLUS:
339       {
340         rtx base, index;
341         if (REG_P (XEXP (addr, 0))
342             && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
343           base = XEXP (addr, 0), index = XEXP (addr, 1);
344         else if (REG_P (XEXP (addr, 1))
345             && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
346           base = XEXP (addr, 1), index = XEXP (addr, 0);
347         else
348           abort ();
349         print_operand (file, index, 0);
350         fputc (',', file);
351         print_operand (file, base, 0);;
352         break;
353       }
354     case SYMBOL_REF:
355       output_addr_const (file, addr);
356       break;
357     default:
358       output_addr_const (file, addr);
359       break;
360     }
361 }
362
363 /* Print a set of registers in the format required by "movm" and "ret".
364    Register K is saved if bit K of MASK is set.  The data and address
365    registers can be stored individually, but the extended registers cannot.
366    We assume that the mask alread takes that into account.  For instance,
367    bits 14 to 17 must have the same value. */
368
369 void
370 mn10300_print_reg_list (file, mask)
371      FILE *file;
372      int mask;
373 {
374   int need_comma;
375   int i;
376
377   need_comma = 0;
378   fputc ('[', file);
379
380   for (i = 0; i < FIRST_EXTENDED_REGNUM; i++)
381     if ((mask & (1 << i)) != 0)
382       {
383         if (need_comma)
384           fputc (',', file);
385         fputs (reg_names [i], file);
386         need_comma = 1;
387       }
388
389   if ((mask & 0x3c000) != 0)
390     {
391       if ((mask & 0x3c000) != 0x3c000)
392         abort();
393       if (need_comma)
394         fputc (',', file);
395       fputs ("exreg1", file);
396       need_comma = 1;
397     }
398
399   fputc (']', file);
400 }
401
402 int
403 can_use_return_insn ()
404 {
405   /* size includes the fixed stack space needed for function calls.  */
406   int size = get_frame_size () + current_function_outgoing_args_size;
407
408   /* And space for the return pointer.  */
409   size += current_function_outgoing_args_size ? 4 : 0;
410
411   return (reload_completed
412           && size == 0
413           && !regs_ever_live[2]
414           && !regs_ever_live[3]
415           && !regs_ever_live[6]
416           && !regs_ever_live[7]
417           && !regs_ever_live[14]
418           && !regs_ever_live[15]
419           && !regs_ever_live[16]
420           && !regs_ever_live[17]
421           && !frame_pointer_needed);
422 }
423
424 /* Returns the set of live, callee-saved registers as a bitmask.  The
425    callee-saved extended registers cannot be stored individually, so
426    all of them will be included in the mask if any one of them is used. */
427
428 int
429 mn10300_get_live_callee_saved_regs ()
430 {
431   int mask;
432   int i;
433
434   mask = 0;
435   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
436     if (regs_ever_live[i] && ! call_used_regs[i])
437       mask |= (1 << i);
438   if ((mask & 0x3c000) != 0)
439     mask |= 0x3c000;
440
441   return mask;
442 }
443
444 /* Generate an instruction that pushes several registers onto the stack.
445    Register K will be saved if bit K in MASK is set.  The function does
446    nothing if MASK is zero.
447
448    To be compatible with the "movm" instruction, the lowest-numbered
449    register must be stored in the lowest slot.  If MASK is the set
450    { R1,...,RN }, where R1...RN are ordered least first, the generated
451    instruction will have the form:
452
453        (parallel
454          (set (reg:SI 9) (plus:SI (reg:SI 9) (const_int -N*4)))
455          (set (mem:SI (plus:SI (reg:SI 9)
456                                (const_int -1*4)))
457               (reg:SI RN))
458          ...
459          (set (mem:SI (plus:SI (reg:SI 9)
460                                (const_int -N*4)))
461               (reg:SI R1))) */
462
463 void
464 mn10300_gen_multiple_store (mask)
465      int mask;
466 {
467   if (mask != 0)
468     {
469       int i;
470       int count;
471       rtx par;
472       int pari;
473
474       /* Count how many registers need to be saved. */
475       count = 0;
476       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477         if ((mask & (1 << i)) != 0)
478           count += 1;
479
480       /* We need one PARALLEL element to update the stack pointer and
481          an additional element for each register that is stored. */
482       par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count + 1));
483
484       /* Create the instruction that updates the stack pointer. */
485       XVECEXP (par, 0, 0)
486         = gen_rtx_SET (SImode,
487                        stack_pointer_rtx,
488                        gen_rtx_PLUS (SImode,
489                                      stack_pointer_rtx,
490                                      GEN_INT (-count * 4)));
491
492       /* Create each store. */
493       pari = 1;
494       for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
495         if ((mask & (1 << i)) != 0)
496           {
497             rtx address = gen_rtx_PLUS (SImode,
498                                         stack_pointer_rtx,
499                                         GEN_INT (-pari * 4));
500             XVECEXP(par, 0, pari)
501               = gen_rtx_SET (VOIDmode,
502                              gen_rtx_MEM (SImode, address),
503                              gen_rtx_REG (SImode, i));
504             pari += 1;
505           }
506
507       par = emit_insn (par);
508       RTX_FRAME_RELATED_P (par) = 1;
509     }
510 }
511
512 void
513 expand_prologue ()
514 {
515   HOST_WIDE_INT size;
516
517   /* SIZE includes the fixed stack space needed for function calls.  */
518   size = get_frame_size () + current_function_outgoing_args_size;
519   size += (current_function_outgoing_args_size ? 4 : 0);
520
521   /* If this is an old-style varargs function, then its arguments
522      need to be flushed back to the stack.  */
523   if (current_function_varargs)
524     {
525       emit_move_insn (gen_rtx_MEM (SImode,
526                                    plus_constant (stack_pointer_rtx, 4)),
527                       gen_rtx_REG (SImode, 0));
528       emit_move_insn (gen_rtx_MEM (SImode,
529                                    plus_constant (stack_pointer_rtx, 8)),
530                       gen_rtx_REG (SImode, 1));
531     }
532
533   /* If we use any of the callee-saved registers, save them now. */
534   mn10300_gen_multiple_store (mn10300_get_live_callee_saved_regs ());
535
536   /* Now put the frame pointer into the frame pointer register.  */
537   if (frame_pointer_needed)
538     emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
539
540   /* Allocate stack for this frame.  */
541   if (size)
542     emit_insn (gen_addsi3 (stack_pointer_rtx,
543                            stack_pointer_rtx,
544                            GEN_INT (-size)));
545 }
546
547 void
548 expand_epilogue ()
549 {
550   HOST_WIDE_INT size;
551
552   /* SIZE includes the fixed stack space needed for function calls.  */
553   size = get_frame_size () + current_function_outgoing_args_size;
554   size += (current_function_outgoing_args_size ? 4 : 0);
555
556   /* Maybe cut back the stack, except for the register save area.
557
558      If the frame pointer exists, then use the frame pointer to
559      cut back the stack.
560
561      If the stack size + register save area is more than 255 bytes,
562      then the stack must be cut back here since the size + register
563      save size is too big for a ret/retf instruction. 
564
565      Else leave it alone, it will be cut back as part of the
566      ret/retf instruction, or there wasn't any stack to begin with.
567
568      Under no circumstanes should the register save area be
569      deallocated here, that would leave a window where an interrupt
570      could occur and trash the register save area.  */
571   if (frame_pointer_needed)
572     {
573       emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
574       size = 0;
575     }
576   else if (size + REG_SAVE_BYTES > 255)
577     {
578       emit_insn (gen_addsi3 (stack_pointer_rtx,
579                              stack_pointer_rtx,
580                              GEN_INT (size)));
581       size = 0;
582     }
583
584   /* Adjust the stack and restore callee-saved registers, if any.  */
585   if (size || regs_ever_live[2] || regs_ever_live[3]
586       || regs_ever_live[6] || regs_ever_live[7]
587       || regs_ever_live[14] || regs_ever_live[15]
588       || regs_ever_live[16] || regs_ever_live[17]
589       || frame_pointer_needed)
590     emit_jump_insn (gen_return_internal_regs
591                     (GEN_INT (size + REG_SAVE_BYTES)));
592   else
593     emit_jump_insn (gen_return_internal ());
594 }
595
596 /* Update the condition code from the insn.  */
597
598 void
599 notice_update_cc (body, insn)
600      rtx body;
601      rtx insn;
602 {
603   switch (get_attr_cc (insn))
604     {
605     case CC_NONE:
606       /* Insn does not affect CC at all.  */
607       break;
608
609     case CC_NONE_0HIT:
610       /* Insn does not change CC, but the 0'th operand has been changed.  */
611       if (cc_status.value1 != 0
612           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
613         cc_status.value1 = 0;
614       break;
615
616     case CC_SET_ZN:
617       /* Insn sets the Z,N flags of CC to recog_data.operand[0].
618          V,C are unusable.  */
619       CC_STATUS_INIT;
620       cc_status.flags |= CC_NO_CARRY | CC_OVERFLOW_UNUSABLE;
621       cc_status.value1 = recog_data.operand[0];
622       break;
623
624     case CC_SET_ZNV:
625       /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
626          C is unusable.  */
627       CC_STATUS_INIT;
628       cc_status.flags |= CC_NO_CARRY;
629       cc_status.value1 = recog_data.operand[0];
630       break;
631
632     case CC_COMPARE:
633       /* The insn is a compare instruction.  */
634       CC_STATUS_INIT;
635       cc_status.value1 = SET_SRC (body);
636       break;
637
638     case CC_INVERT:
639       /* The insn is a compare instruction.  */
640       CC_STATUS_INIT;
641       cc_status.value1 = SET_SRC (body);
642       cc_status.flags |= CC_INVERTED;
643       break;
644
645     case CC_CLOBBER:
646       /* Insn doesn't leave CC in a usable state.  */
647       CC_STATUS_INIT;
648       break;
649
650     default:
651       abort ();
652     }
653 }
654
655 /* Recognise the PARALLEL rtx generated by mn10300_gen_multiple_store().
656    This function is for MATCH_PARALLEL and so assumes OP is known to be
657    parallel.  If OP is a multiple store, return a mask indicating which
658    registers it saves.  Return 0 otherwise.  */
659
660 int
661 store_multiple_operation (op, mode)
662      rtx op;
663      enum machine_mode mode ATTRIBUTE_UNUSED;
664 {
665   int count;
666   int mask;
667   int i;
668   unsigned int last;
669   rtx elt;
670
671   count = XVECLEN (op, 0);
672   if (count < 2)
673     return 0;
674
675   /* Check that first instruction has the form (set (sp) (plus A B)) */
676   elt = XVECEXP (op, 0, 0);
677   if (GET_CODE (elt) != SET
678       || GET_CODE (SET_DEST (elt)) != REG
679       || REGNO (SET_DEST (elt)) != STACK_POINTER_REGNUM
680       || GET_CODE (SET_SRC (elt)) != PLUS)
681     return 0;
682
683   /* Check that A is the stack pointer and B is the expected stack size.
684      For OP to match, each subsequent instruction should push a word onto
685      the stack.  We therefore expect the first instruction to create
686      COUNT-1 stack slots. */
687   elt = SET_SRC (elt);
688   if (GET_CODE (XEXP (elt, 0)) != REG
689       || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
690       || GET_CODE (XEXP (elt, 1)) != CONST_INT
691       || INTVAL (XEXP (elt, 1)) != -(count - 1) * 4)
692     return 0;
693
694   /* Now go through the rest of the vector elements.  They must be
695      ordered so that the first instruction stores the highest-numbered
696      register to the highest stack slot and that subsequent instructions
697      store a lower-numbered register to the slot below.
698
699      LAST keeps track of the smallest-numbered register stored so far.
700      MASK is the set of stored registers. */
701   last = FIRST_PSEUDO_REGISTER;
702   mask = 0;
703   for (i = 1; i < count; i++)
704     {
705       /* Check that element i is a (set (mem M) R) and that R is valid. */
706       elt = XVECEXP (op, 0, i);
707       if (GET_CODE (elt) != SET
708           || GET_CODE (SET_DEST (elt)) != MEM
709           || GET_CODE (SET_SRC (elt)) != REG
710           || REGNO (SET_SRC (elt)) >= last)
711         return 0;
712
713       /* R was OK, so provisionally add it to MASK.  We return 0 in any
714          case if the rest of the instruction has a flaw. */
715       last = REGNO (SET_SRC (elt));
716       mask |= (1 << last);
717
718       /* Check that M has the form (plus (sp) (const_int -I*4)) */
719       elt = XEXP (SET_DEST (elt), 0);
720       if (GET_CODE (elt) != PLUS
721           || GET_CODE (XEXP (elt, 0)) != REG
722           || REGNO (XEXP (elt, 0)) != STACK_POINTER_REGNUM
723           || GET_CODE (XEXP (elt, 1)) != CONST_INT
724           || INTVAL (XEXP (elt, 1)) != -i * 4)
725         return 0;
726     }
727
728   /* All or none of the callee-saved extended registers must be in the set. */
729   if ((mask & 0x3c000) != 0
730       && (mask & 0x3c000) != 0x3c000)
731     return 0;
732
733   return mask;
734 }
735
736 /* Return true if OP is a valid call operand.  */
737
738 int
739 call_address_operand (op, mode)
740      rtx op;
741      enum machine_mode mode ATTRIBUTE_UNUSED;
742 {
743   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
744 }
745
746 /* What (if any) secondary registers are needed to move IN with mode
747    MODE into a register in register class CLASS. 
748
749    We might be able to simplify this.  */
750 enum reg_class
751 secondary_reload_class (class, mode, in)
752      enum reg_class class;
753      enum machine_mode mode;
754      rtx in;
755 {
756   /* Memory loads less than a full word wide can't have an
757      address or stack pointer destination.  They must use
758      a data register as an intermediate register.  */
759   if ((GET_CODE (in) == MEM
760        || (GET_CODE (in) == REG
761            && REGNO (in) >= FIRST_PSEUDO_REGISTER)
762        || (GET_CODE (in) == SUBREG
763            && GET_CODE (SUBREG_REG (in)) == REG
764            && REGNO (SUBREG_REG (in)) >= FIRST_PSEUDO_REGISTER))
765       && (mode == QImode || mode == HImode)
766       && (class == ADDRESS_REGS || class == SP_REGS
767           || class == SP_OR_ADDRESS_REGS))
768     {
769       if (TARGET_AM33)
770         return DATA_OR_EXTENDED_REGS;
771       return DATA_REGS;
772     }
773
774   /* We can't directly load sp + const_int into a data register;
775      we must use an address register as an intermediate.  */
776   if (class != SP_REGS
777       && class != ADDRESS_REGS
778       && class != SP_OR_ADDRESS_REGS
779       && class != SP_OR_EXTENDED_REGS
780       && class != ADDRESS_OR_EXTENDED_REGS
781       && class != SP_OR_ADDRESS_OR_EXTENDED_REGS
782       && (in == stack_pointer_rtx
783           || (GET_CODE (in) == PLUS
784               && (XEXP (in, 0) == stack_pointer_rtx
785                   || XEXP (in, 1) == stack_pointer_rtx))))
786     return ADDRESS_REGS;
787
788   if (GET_CODE (in) == PLUS
789       && (XEXP (in, 0) == stack_pointer_rtx
790           || XEXP (in, 1) == stack_pointer_rtx))
791     {
792       if (TARGET_AM33)
793         return DATA_OR_EXTENDED_REGS;
794       return DATA_REGS;
795     }
796  
797   /* Otherwise assume no secondary reloads are needed.  */
798   return NO_REGS;
799 }
800
801 int
802 initial_offset (from, to)
803      int from, to;
804 {
805   /* The difference between the argument pointer and the frame pointer
806      is the size of the callee register save area.  */
807   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
808     {
809       if (regs_ever_live[2] || regs_ever_live[3]
810           || regs_ever_live[6] || regs_ever_live[7]
811           || regs_ever_live[14] || regs_ever_live[15]
812           || regs_ever_live[16] || regs_ever_live[17]
813           || frame_pointer_needed)
814         return REG_SAVE_BYTES;
815       else
816         return 0;
817     }
818
819   /* The difference between the argument pointer and the stack pointer is
820      the sum of the size of this function's frame, the callee register save
821      area, and the fixed stack space needed for function calls (if any).  */
822   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
823     {
824       if (regs_ever_live[2] || regs_ever_live[3]
825           || regs_ever_live[6] || regs_ever_live[7]
826           || regs_ever_live[14] || regs_ever_live[15]
827           || regs_ever_live[16] || regs_ever_live[17]
828           || frame_pointer_needed)
829         return (get_frame_size () + REG_SAVE_BYTES
830                 + (current_function_outgoing_args_size
831                    ? current_function_outgoing_args_size + 4 : 0)); 
832       else
833         return (get_frame_size ()
834                 + (current_function_outgoing_args_size
835                    ? current_function_outgoing_args_size + 4 : 0)); 
836     }
837
838   /* The difference between the frame pointer and stack pointer is the sum
839      of the size of this function's frame and the fixed stack space needed
840      for function calls (if any).  */
841   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
842     return (get_frame_size ()
843             + (current_function_outgoing_args_size
844                ? current_function_outgoing_args_size + 4 : 0)); 
845
846   abort ();
847 }
848
849 /* Flush the argument registers to the stack for a stdarg function;
850    return the new argument pointer.  */
851 rtx
852 mn10300_builtin_saveregs ()
853 {
854   rtx offset, mem;
855   tree fntype = TREE_TYPE (current_function_decl);
856   int argadj = ((!(TYPE_ARG_TYPES (fntype) != 0
857                    && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
858                        != void_type_node)))
859                 ? UNITS_PER_WORD : 0);
860   int set = get_varargs_alias_set ();
861
862   if (argadj)
863     offset = plus_constant (current_function_arg_offset_rtx, argadj);
864   else
865     offset = current_function_arg_offset_rtx;
866
867   mem = gen_rtx_MEM (SImode, current_function_internal_arg_pointer);
868   MEM_ALIAS_SET (mem) = set;
869   emit_move_insn (mem, gen_rtx_REG (SImode, 0));
870
871   mem = gen_rtx_MEM (SImode,
872                      plus_constant (current_function_internal_arg_pointer, 4));
873   MEM_ALIAS_SET (mem) = set;
874   emit_move_insn (mem, gen_rtx_REG (SImode, 1));
875
876   return copy_to_reg (expand_binop (Pmode, add_optab,
877                                     current_function_internal_arg_pointer,
878                                     offset, 0, 0, OPTAB_LIB_WIDEN));
879 }
880
881 void
882 mn10300_va_start (stdarg_p, valist, nextarg)
883      int stdarg_p;
884      tree valist;
885      rtx nextarg;
886 {
887   if (stdarg_p)
888     nextarg = expand_builtin_saveregs ();
889
890   std_expand_builtin_va_start (stdarg_p, valist, nextarg);
891 }
892
893 rtx
894 mn10300_va_arg (valist, type)
895      tree valist, type;
896 {
897   HOST_WIDE_INT align, rsize;
898   tree t, ptr, pptr;
899
900   /* Compute the rounded size of the type.  */
901   align = PARM_BOUNDARY / BITS_PER_UNIT;
902   rsize = (((int_size_in_bytes (type) + align - 1) / align) * align);
903
904   t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist, 
905              build_int_2 ((rsize > 8 ? 4 : rsize), 0));
906   TREE_SIDE_EFFECTS (t) = 1;
907
908   ptr = build_pointer_type (type);
909
910   /* "Large" types are passed by reference.  */
911   if (rsize > 8)
912     {
913       pptr = build_pointer_type (ptr);
914       t = build1 (NOP_EXPR, pptr, t);
915       TREE_SIDE_EFFECTS (t) = 1;
916
917       t = build1 (INDIRECT_REF, ptr, t);
918       TREE_SIDE_EFFECTS (t) = 1;
919     }
920   else
921     {
922       t = build1 (NOP_EXPR, ptr, t);
923       TREE_SIDE_EFFECTS (t) = 1;
924     }
925
926   /* Calculate!  */
927   return expand_expr (t, NULL_RTX, Pmode, EXPAND_NORMAL);
928 }
929
930 /* Return an RTX to represent where a value with mode MODE will be returned
931    from a function.  If the result is 0, the argument is pushed.  */
932
933 rtx
934 function_arg (cum, mode, type, named)
935      CUMULATIVE_ARGS *cum;
936      enum machine_mode mode;
937      tree type;
938      int named ATTRIBUTE_UNUSED;
939 {
940   rtx result = 0;
941   int size, align;
942
943   /* We only support using 2 data registers as argument registers.  */
944   int nregs = 2;
945
946   /* Figure out the size of the object to be passed.  */
947   if (mode == BLKmode)
948     size = int_size_in_bytes (type);
949   else
950     size = GET_MODE_SIZE (mode);
951
952   /* Figure out the alignment of the object to be passed.  */
953   align = size;
954
955   cum->nbytes = (cum->nbytes + 3) & ~3;
956
957   /* Don't pass this arg via a register if all the argument registers
958      are used up.  */
959   if (cum->nbytes > nregs * UNITS_PER_WORD)
960     return 0;
961
962   /* Don't pass this arg via a register if it would be split between
963      registers and memory.  */
964   if (type == NULL_TREE
965       && cum->nbytes + size > nregs * UNITS_PER_WORD)
966     return 0;
967
968   switch (cum->nbytes / UNITS_PER_WORD)
969     {
970     case 0:
971       result = gen_rtx_REG (mode, 0);
972       break;
973     case 1:
974       result = gen_rtx_REG (mode, 1);
975       break;
976     default:
977       result = 0;
978     }
979
980   return result;
981 }
982
983 /* Return the number of registers to use for an argument passed partially
984    in registers and partially in memory.  */
985
986 int
987 function_arg_partial_nregs (cum, mode, type, named)
988      CUMULATIVE_ARGS *cum;
989      enum machine_mode mode;
990      tree type;
991      int named ATTRIBUTE_UNUSED;
992 {
993   int size, align;
994
995   /* We only support using 2 data registers as argument registers.  */
996   int nregs = 2;
997
998   /* Figure out the size of the object to be passed.  */
999   if (mode == BLKmode)
1000     size = int_size_in_bytes (type);
1001   else
1002     size = GET_MODE_SIZE (mode);
1003
1004   /* Figure out the alignment of the object to be passed.  */
1005   align = size;
1006
1007   cum->nbytes = (cum->nbytes + 3) & ~3;
1008
1009   /* Don't pass this arg via a register if all the argument registers
1010      are used up.  */
1011   if (cum->nbytes > nregs * UNITS_PER_WORD)
1012     return 0;
1013
1014   if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1015     return 0;
1016
1017   /* Don't pass this arg via a register if it would be split between
1018      registers and memory.  */
1019   if (type == NULL_TREE
1020       && cum->nbytes + size > nregs * UNITS_PER_WORD)
1021     return 0;
1022
1023   return (nregs * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
1024 }
1025
1026 /* Output a tst insn.  */
1027 char *
1028 output_tst (operand, insn)
1029      rtx operand, insn;
1030 {
1031   rtx temp;
1032   int past_call = 0;
1033
1034   /* We can save a byte if we can find a register which has the value
1035      zero in it.  */
1036   temp = PREV_INSN (insn);
1037   while (optimize && temp)
1038     {
1039       rtx set;
1040
1041       /* We allow the search to go through call insns.  We record
1042          the fact that we've past a CALL_INSN and reject matches which
1043          use call clobbered registers.  */
1044       if (GET_CODE (temp) == CODE_LABEL
1045           || GET_CODE (temp) == JUMP_INSN
1046           || GET_CODE (temp) == BARRIER)
1047         break;
1048
1049       if (GET_CODE (temp) == CALL_INSN)
1050         past_call = 1;
1051
1052       if (GET_CODE (temp) == NOTE)
1053         {
1054           temp = PREV_INSN (temp);
1055           continue;
1056         }
1057
1058       /* It must be an insn, see if it is a simple set. */
1059       set = single_set (temp);
1060       if (!set)
1061         {
1062           temp = PREV_INSN (temp);
1063           continue;
1064         }
1065
1066       /* Are we setting a data register to zero (this does not win for
1067          address registers)? 
1068
1069          If it's a call clobbered register, have we past a call?
1070
1071          Make sure the register we find isn't the same as ourself;
1072          the mn10300 can't encode that.
1073
1074          ??? reg_set_between_p return nonzero anytime we pass a CALL_INSN
1075          so the code to detect calls here isn't doing anything useful.  */
1076       if (REG_P (SET_DEST (set))
1077           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1078           && !reg_set_between_p (SET_DEST (set), temp, insn)
1079           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1080               == REGNO_REG_CLASS (REGNO (operand)))
1081           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) != EXTENDED_REGS
1082           && REGNO (SET_DEST (set)) != REGNO (operand)
1083           && (!past_call 
1084               || !call_used_regs[REGNO (SET_DEST (set))]))
1085         {
1086           rtx xoperands[2];
1087           xoperands[0] = operand;
1088           xoperands[1] = SET_DEST (set);
1089
1090           output_asm_insn ("cmp %1,%0", xoperands);
1091           return "";
1092         }
1093
1094       if (REGNO_REG_CLASS (REGNO (operand)) == EXTENDED_REGS
1095           && REG_P (SET_DEST (set))
1096           && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1097           && !reg_set_between_p (SET_DEST (set), temp, insn)
1098           && (REGNO_REG_CLASS (REGNO (SET_DEST (set)))
1099               != REGNO_REG_CLASS (REGNO (operand)))
1100           && REGNO_REG_CLASS (REGNO (SET_DEST (set))) == EXTENDED_REGS
1101           && REGNO (SET_DEST (set)) != REGNO (operand)
1102           && (!past_call 
1103               || !call_used_regs[REGNO (SET_DEST (set))]))
1104         {
1105           rtx xoperands[2];
1106           xoperands[0] = operand;
1107           xoperands[1] = SET_DEST (set);
1108
1109           output_asm_insn ("cmp %1,%0", xoperands);
1110           return "";
1111         }
1112       temp = PREV_INSN (temp);
1113     }
1114   return "cmp 0,%0";
1115 }
1116
1117 int
1118 impossible_plus_operand (op, mode)
1119      rtx op;
1120      enum machine_mode mode ATTRIBUTE_UNUSED;
1121 {
1122   if (GET_CODE (op) != PLUS)
1123     return 0;
1124
1125   if (XEXP (op, 0) == stack_pointer_rtx
1126       || XEXP (op, 1) == stack_pointer_rtx)
1127     return 1;
1128
1129   return 0;
1130 }
1131
1132 /* Return 1 if X is a CONST_INT that is only 8 bits wide.  This is used
1133    for the btst insn which may examine memory or a register (the memory
1134    variant only allows an unsigned 8 bit integer).  */
1135 int
1136 const_8bit_operand (op, mode)
1137     register rtx op;
1138     enum machine_mode mode ATTRIBUTE_UNUSED;
1139 {
1140   return (GET_CODE (op) == CONST_INT
1141           && INTVAL (op) >= 0
1142           && INTVAL (op) < 256);
1143 }
1144
1145 /* Similarly, but when using a zero_extract pattern for a btst where
1146    the source operand might end up in memory.  */
1147 int
1148 mask_ok_for_mem_btst (len, bit)
1149      int len;
1150      int bit;
1151 {
1152   int mask = 0;
1153
1154   while (len > 0)
1155     {
1156       mask |= (1 << bit);
1157       bit++;
1158       len--;
1159     }
1160
1161   /* MASK must bit into an 8bit value.  */
1162   return (((mask & 0xff) == mask)
1163           || ((mask & 0xff00) == mask)
1164           || ((mask & 0xff0000) == mask)
1165           || ((mask & 0xff000000) == mask));
1166 }
1167
1168 /* Return 1 if X contains a symbolic expression.  We know these
1169    expressions will have one of a few well defined forms, so
1170    we need only check those forms.  */
1171 int
1172 symbolic_operand (op, mode)
1173      register rtx op;
1174      enum machine_mode mode ATTRIBUTE_UNUSED;
1175 {
1176   switch (GET_CODE (op))
1177     {
1178     case SYMBOL_REF:
1179     case LABEL_REF:
1180       return 1;
1181     case CONST:
1182       op = XEXP (op, 0);
1183       return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1184                || GET_CODE (XEXP (op, 0)) == LABEL_REF)
1185               && GET_CODE (XEXP (op, 1)) == CONST_INT);
1186     default:
1187       return 0;
1188     }
1189 }
1190
1191 /* Try machine dependent ways of modifying an illegitimate address
1192    to be legitimate.  If we find one, return the new valid address.
1193    This macro is used in only one place: `memory_address' in explow.c.
1194
1195    OLDX is the address as it was before break_out_memory_refs was called.
1196    In some cases it is useful to look at this to decide what needs to be done.
1197
1198    MODE and WIN are passed so that this macro can use
1199    GO_IF_LEGITIMATE_ADDRESS.
1200
1201    Normally it is always safe for this macro to do nothing.  It exists to
1202    recognize opportunities to optimize the output.
1203
1204    But on a few ports with segmented architectures and indexed addressing
1205    (mn10300, hppa) it is used to rewrite certain problematical addresses.  */
1206 rtx
1207 legitimize_address (x, oldx, mode)
1208      rtx x;
1209      rtx oldx ATTRIBUTE_UNUSED;
1210      enum machine_mode mode ATTRIBUTE_UNUSED;
1211 {
1212   /* Uh-oh.  We might have an address for x[n-100000].  This needs
1213      special handling to avoid creating an indexed memory address
1214      with x-100000 as the base.  */
1215   if (GET_CODE (x) == PLUS
1216       && symbolic_operand (XEXP (x, 1), VOIDmode))
1217     {
1218       /* Ugly.  We modify things here so that the address offset specified
1219          by the index expression is computed first, then added to x to form
1220          the entire address.  */
1221
1222       rtx regx1, regy1, regy2, y;
1223
1224       /* Strip off any CONST.  */
1225       y = XEXP (x, 1);
1226       if (GET_CODE (y) == CONST)
1227         y = XEXP (y, 0);
1228
1229       if (GET_CODE (y) == PLUS || GET_CODE (y) == MINUS)
1230         {
1231           regx1 = force_reg (Pmode, force_operand (XEXP (x, 0), 0));
1232           regy1 = force_reg (Pmode, force_operand (XEXP (y, 0), 0));
1233           regy2 = force_reg (Pmode, force_operand (XEXP (y, 1), 0));
1234           regx1 = force_reg (Pmode,
1235                              gen_rtx (GET_CODE (y), Pmode, regx1, regy2));
1236           return force_reg (Pmode, gen_rtx_PLUS (Pmode, regx1, regy1));
1237         }
1238     }
1239   return x;
1240 }
1241
1242 int
1243 mn10300_address_cost (x, unsig)
1244      rtx x;
1245      int *unsig;
1246 {
1247   int _s = 0;
1248   if (unsig == 0)
1249     unsig = &_s;
1250   
1251   switch (GET_CODE (x))
1252     {
1253     case REG:
1254       switch (REGNO_REG_CLASS (REGNO (x)))
1255         {
1256         case SP_REGS:
1257           *unsig = 1;
1258           return 0;
1259
1260         case ADDRESS_REGS:
1261           return 1;
1262
1263         case DATA_REGS:
1264         case EXTENDED_REGS:
1265           return 3;
1266
1267         case NO_REGS:
1268           return 5;
1269
1270         default:
1271           abort ();
1272         }
1273
1274     case PLUS:
1275     case MINUS:
1276     case ASHIFT:
1277     case AND:
1278     case IOR:
1279       return (mn10300_address_cost (XEXP (x, 0), unsig)
1280               + mn10300_address_cost (XEXP (x, 1), unsig));
1281
1282     case EXPR_LIST:
1283     case SUBREG:
1284     case MEM:
1285       return ADDRESS_COST (XEXP (x, 0));
1286
1287     case ZERO_EXTEND:
1288       *unsig = 1;
1289       return mn10300_address_cost (XEXP (x, 0), unsig);
1290
1291     case CONST_INT:
1292       if (INTVAL (x) == 0)
1293         return 0;
1294       if (INTVAL (x) + (*unsig ? 0 : 0x80) < 0x100)
1295         return 1;
1296       if (INTVAL (x) + (*unsig ? 0 : 0x8000) < 0x10000)
1297         return 3;
1298       if (INTVAL (x) + (*unsig ? 0 : 0x800000) < 0x1000000)
1299         return 5;
1300       return 7;
1301
1302     case CONST:
1303     case SYMBOL_REF:
1304     case LABEL_REF:
1305       return 8;
1306
1307     case ADDRESSOF:
1308       switch (GET_CODE (XEXP (x, 0)))
1309         {
1310         case MEM:
1311           return ADDRESS_COST (XEXP (x, 0));
1312
1313         case REG:
1314           return 1;
1315
1316         default:
1317           abort ();
1318         }
1319
1320     default:
1321       abort ();
1322
1323     }
1324 }