OSDN Git Service

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