OSDN Git Service

Merge from gcc-2.8
[pf3gnuchains/gcc-fork.git] / gcc / config / v850 / v850.c
1 /* Subroutines for insn-output.c for NEC V850 series
2    Copyright (C) 1996, 1997 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 <stdio.h>
23 #include <ctype.h>
24 #include "config.h"
25 #include "rtl.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 "tree.h"
38 #include "obstack.h"
39
40 /* True if the current function has anonymous arguments.  */
41 int current_function_anonymous_args;
42
43 /* Information about the various small memory areas.  */
44 struct small_memory_info small_memory[ (int)SMALL_MEMORY_max ] =
45 {
46   /* name       value           max             physical max */
47   { "tda",      (char *)0,      0,              256 },
48   { "sda",      (char *)0,      0,              65536 },
49   { "zda",      (char *)0,      0,              32768 },
50 };
51
52 /* True if we don't need to check any more if the current
53    function is an interrupt handler */
54 static int v850_interrupt_cache_p = FALSE;
55
56 /* Whether current function is an interrupt handler.  */
57 static int v850_interrupt_p = FALSE;
58
59 \f
60 /* Sometimes certain combinations of command options do not make
61    sense on a particular target machine.  You can define a macro
62    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
63    defined, is executed once just after all the command options have
64    been parsed.
65
66    Don't use this macro to turn on various extra optimizations for
67    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
68
69 void
70 override_options ()
71 {
72   int i;
73   extern int atoi ();
74
75   /* Parse -m{s,t,z}da=nnn switches */
76   for (i = 0; i < (int)SMALL_MEMORY_max; i++)
77     {
78       if (small_memory[i].value)
79         {
80           if (!isdigit (*small_memory[i].value))
81             error ("%s=%s is not numeric.",
82                    small_memory[i].name,
83                    small_memory[i].value);
84           else
85             {
86               small_memory[i].max = atoi (small_memory[i].value);
87               if (small_memory[i].max > small_memory[i].physical_max)
88                 error ("%s=%s is too large.",
89                    small_memory[i].name,
90                    small_memory[i].value);
91             }
92         }
93     }
94 }
95
96 \f
97 /* Output assembly code for the start of the file.  */
98
99 void
100 asm_file_start (file)
101      FILE *file;
102 {
103   output_file_directive (file, main_input_filename);
104 }
105 \f
106
107 /* Return an RTX to represent where a value with mode MODE will be returned
108    from a function.  If the result is 0, the argument is pushed.  */
109
110 rtx
111 function_arg (cum, mode, type, named)
112      CUMULATIVE_ARGS *cum;
113      enum machine_mode mode;
114      tree type;
115      int named;
116 {
117   rtx result = 0;
118   int size, align;
119
120   if (TARGET_GHS && !named)
121     return NULL_RTX;
122
123   if (mode == BLKmode)
124     size = int_size_in_bytes (type);
125   else
126     size = GET_MODE_SIZE (mode);
127
128   if (type)
129     align = TYPE_ALIGN (type) / BITS_PER_UNIT;
130   else
131     align = size;
132
133   cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
134
135   if (cum->nbytes > 4 * UNITS_PER_WORD)
136     return 0;
137
138   if (type == NULL_TREE
139       && cum->nbytes + size > 4 * UNITS_PER_WORD)
140     return 0;
141
142   switch (cum->nbytes / UNITS_PER_WORD)
143     {
144     case 0:
145       result = gen_rtx (REG, mode, 6);
146       break;
147     case 1:
148       result = gen_rtx (REG, mode, 7);
149       break;
150     case 2:
151       result = gen_rtx (REG, mode, 8);
152       break;
153     case 3:
154       result = gen_rtx (REG, mode, 9);
155       break;
156     default:
157       result = 0;
158     }
159
160   return result;
161 }
162
163 \f
164 /* Return the number of words which must be put into registers
165    for values which are part in registers and part in memory.  */
166
167 int
168 function_arg_partial_nregs (cum, mode, type, named)
169      CUMULATIVE_ARGS *cum;
170      enum machine_mode mode;
171      tree type;
172      int named;
173 {
174   int size, align;
175
176   if (TARGET_GHS && !named)
177     return 0;
178
179   if (mode == BLKmode)
180     size = int_size_in_bytes (type);
181   else
182     size = GET_MODE_SIZE (mode);
183
184   if (type)
185     align = TYPE_ALIGN (type) / BITS_PER_UNIT;
186   else
187     align = size;
188
189   cum->nbytes = (cum->nbytes + align - 1) &~(align - 1);
190
191   if (cum->nbytes > 4 * UNITS_PER_WORD)
192     return 0;
193
194   if (cum->nbytes + size <= 4 * UNITS_PER_WORD)
195     return 0;
196
197   if (type == NULL_TREE
198       && cum->nbytes + size > 4 * UNITS_PER_WORD)
199     return 0;
200
201   return (4 * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
202 }
203
204 \f
205 /* Return the high and low words of a CONST_DOUBLE */
206
207 static void
208 const_double_split (x, p_high, p_low)
209      rtx x;
210      HOST_WIDE_INT *p_high;
211      HOST_WIDE_INT *p_low;
212 {
213   if (GET_CODE (x) == CONST_DOUBLE)
214     {
215       long t[2];
216       REAL_VALUE_TYPE rv;
217
218       switch (GET_MODE (x))
219         {
220         case DFmode:
221           REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
222           REAL_VALUE_TO_TARGET_DOUBLE (rv, t);
223           *p_high = t[1];       /* since v850 is little endian */
224           *p_low = t[0];        /* high is second word */
225           return;
226
227         case SFmode:
228           REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
229           REAL_VALUE_TO_TARGET_SINGLE (rv, *p_high);
230           *p_low = 0;
231           return;
232
233         case VOIDmode:
234         case DImode:
235           *p_high = CONST_DOUBLE_HIGH (x);
236           *p_low  = CONST_DOUBLE_LOW (x);
237           return;
238         }
239     }
240
241   fatal_insn ("const_double_split got a bad insn:", x);
242 }
243
244 \f
245 /* Return the cost of the rtx R with code CODE.  */
246
247 static int
248 const_costs_int (value, zero_cost)
249      HOST_WIDE_INT value;
250      int zero_cost;
251 {
252   if (CONST_OK_FOR_I (value))
253       return zero_cost;
254   else if (CONST_OK_FOR_J (value))
255     return 1;
256   else if (CONST_OK_FOR_K (value))
257     return 2;
258   else
259     return 4;
260 }
261
262 int
263 const_costs (r, c)
264      rtx r;
265      enum rtx_code c;
266 {
267   HOST_WIDE_INT high, low;
268
269   switch (c)
270     {
271     case CONST_INT:
272       return const_costs_int (INTVAL (r), 0);
273
274     case CONST_DOUBLE:
275       const_double_split (r, &high, &low);
276       if (GET_MODE (r) == SFmode)
277         return const_costs_int (high, 1);
278       else
279         return const_costs_int (high, 1) + const_costs_int (low, 1);
280
281     case SYMBOL_REF:
282     case LABEL_REF:
283     case CONST:
284       return 2;
285
286     case HIGH:
287       return 1;
288
289     default:
290       return 4;
291     }
292 }
293
294 \f
295 /* Print operand X using operand code CODE to assembly language output file
296    FILE.  */
297
298 void
299 print_operand (file, x, code)
300      FILE *file;
301      rtx x;
302      int code;
303 {
304   HOST_WIDE_INT high, low;
305
306   switch (code)
307     {
308     case 'b':
309     case 'B':
310     case 'c':
311     case 'C':
312       switch ((code == 'B' || code == 'C')
313               ? reverse_condition (GET_CODE (x)) : GET_CODE (x))
314         {
315           case NE:
316             if (code == 'c' || code == 'C')
317               fprintf (file, "nz");
318             else
319               fprintf (file, "ne");
320             break;
321           case EQ:
322             if (code == 'c' || code == 'C')
323               fprintf (file, "z");
324             else
325               fprintf (file, "e");
326             break;
327           case GE:
328             fprintf (file, "ge");
329             break;
330           case GT:
331             fprintf (file, "gt");
332             break;
333           case LE:
334             fprintf (file, "le");
335             break;
336           case LT:
337             fprintf (file, "lt");
338             break;
339           case GEU:
340             fprintf (file, "nl");
341             break;
342           case GTU:
343             fprintf (file, "h");
344             break;
345           case LEU:
346             fprintf (file, "nh");
347             break;
348           case LTU:
349             fprintf (file, "l");
350             break;
351           default:
352             abort ();
353         }
354       break;
355     case 'F':                   /* high word of CONST_DOUBLE */
356       if (GET_CODE (x) == CONST_INT)
357         fprintf (file, "%d", (INTVAL (x) >= 0) ? 0 : -1);
358       else if (GET_CODE (x) == CONST_DOUBLE)
359         {
360           const_double_split (x, &high, &low);
361           fprintf (file, "%ld", (long) high);
362         }
363       else
364         abort ();
365       break;
366     case 'G':                   /* low word of CONST_DOUBLE */
367       if (GET_CODE (x) == CONST_INT)
368         fprintf (file, "%ld", (long) INTVAL (x));
369       else if (GET_CODE (x) == CONST_DOUBLE)
370         {
371           const_double_split (x, &high, &low);
372           fprintf (file, "%ld", (long) low);
373         }
374       else
375         abort ();
376       break;
377     case 'L':
378       fprintf (file, "%d\n", INTVAL (x) & 0xffff);
379       break;
380     case 'M':
381       fprintf (file, "%d", exact_log2 (INTVAL (x)));
382       break;
383     case 'O':
384       if (special_symbolref_operand (x, VOIDmode))
385         {
386           char* name;
387
388           if (GET_CODE (x) == SYMBOL_REF)
389             name = XSTR (x, 0);
390           else if (GET_CODE (x) == CONST)
391             name = XSTR (XEXP (XEXP (x, 0), 0), 0);
392           else
393             abort ();
394
395           if (ZDA_NAME_P (name))
396             fprintf (file, "zdaoff");
397           else if (SDA_NAME_P (name))
398             fprintf (file, "sdaoff");
399           else if (TDA_NAME_P (name))
400             fprintf (file, "tdaoff");
401           else
402             abort ();
403         }
404       else
405         abort ();
406       break;
407     case 'P':
408       if (special_symbolref_operand (x, VOIDmode))
409         output_addr_const (file, x);
410       else
411         abort ();
412       break;
413     case 'Q':
414       if (special_symbolref_operand (x, VOIDmode))
415         {
416           char* name;
417
418           if (GET_CODE (x) == SYMBOL_REF)
419             name = XSTR (x, 0);
420           else if (GET_CODE (x) == CONST)
421             name = XSTR (XEXP (XEXP (x, 0), 0), 0);
422           else
423             abort ();
424
425           if (ZDA_NAME_P (name))
426             fprintf (file, "r0");
427           else if (SDA_NAME_P (name))
428             fprintf (file, "gp");
429           else if (TDA_NAME_P (name))
430             fprintf (file, "ep");
431           else
432             abort ();
433         }
434       else
435         abort ();
436       break;
437     case 'R':           /* 2nd word of a double.  */
438       switch (GET_CODE (x))
439         {
440           case REG:
441             fprintf (file, reg_names[REGNO (x) + 1]);
442             break;
443           case MEM:
444             print_operand_address (file,
445                                    XEXP (adj_offsettable_operand (x, 4), 0));
446             break;
447         }
448       break;
449     case 'S':
450       {
451         /* if it's a reference to a TDA variable, use sst/sld vs. st/ld */
452         if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), FALSE))
453           fputs ("s", file);
454
455         break;
456       }
457     case 'T':
458       {
459         /* Like an 'S' operand above, but for unsigned loads only.  */
460         if (GET_CODE (x) == MEM && ep_memory_operand (x, GET_MODE (x), TRUE))
461           fputs ("s", file);
462
463         break;
464       }
465     case 'W':                   /* print the instruction suffix */
466       switch (GET_MODE (x))
467         {
468         default:
469           abort ();
470
471         case QImode: fputs (".b", file); break;
472         case HImode: fputs (".h", file); break;
473         case SImode: fputs (".w", file); break;
474         case SFmode: fputs (".w", file); break;
475         }
476       break;
477     case '.':                   /* register r0 */
478       fputs (reg_names[0], file);
479       break;
480     case 'z':                   /* reg or zero */
481       if (x == const0_rtx)
482         fputs (reg_names[0], file);
483       else if (GET_CODE (x) == REG)
484         fputs (reg_names[REGNO (x)], file);
485       else
486         abort ();
487       break;
488     default:
489       switch (GET_CODE (x))
490         {
491         case MEM:
492           if (GET_CODE (XEXP (x, 0)) == CONST_INT)
493             output_address (gen_rtx (PLUS, SImode,
494                                      gen_rtx (REG, SImode, 0),
495                                      XEXP (x, 0)));
496           else
497             output_address (XEXP (x, 0));
498           break;
499
500         case REG:
501           fputs (reg_names[REGNO (x)], file);
502           break;
503         case SUBREG:
504           fputs (reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)], file);
505           break;
506         case CONST_INT:
507         case SYMBOL_REF:
508         case CONST:
509         case LABEL_REF:
510         case CODE_LABEL:
511           print_operand_address (file, x);
512           break;
513         default:
514           abort ();
515         }
516       break;
517
518     }
519 }
520
521 \f
522 /* Output assembly language output for the address ADDR to FILE.  */
523
524 void
525 print_operand_address (file, addr)
526      FILE *file;
527      rtx addr;
528 {
529   switch (GET_CODE (addr))
530     {
531     case REG:
532       fprintf (file, "0[");
533       print_operand (file, addr, 0);
534       fprintf (file, "]");
535       break;
536     case LO_SUM:
537       if (GET_CODE (XEXP (addr, 0)) == REG)
538         {
539           /* reg,foo */
540           fprintf (file, "lo(");
541           print_operand (file, XEXP (addr, 1), 0);
542           fprintf (file, ")[");
543           print_operand (file, XEXP (addr, 0), 0);
544           fprintf (file, "]");
545         }
546       break;
547     case PLUS:
548       if (GET_CODE (XEXP (addr, 0)) == REG
549           || GET_CODE (XEXP (addr, 0)) == SUBREG)
550         {
551           /* reg,foo */
552           print_operand (file, XEXP (addr, 1), 0);
553           fprintf (file, "[");
554           print_operand (file, XEXP (addr, 0), 0);
555           fprintf (file, "]");
556         }
557       else
558         {
559           print_operand (file, XEXP (addr, 0), 0);
560           fprintf (file, "+");
561           print_operand (file, XEXP (addr, 1), 0);
562         }
563       break;
564     case SYMBOL_REF:
565       if (ENCODED_NAME_P (XSTR (addr, 0)))
566         {
567           char* name = XSTR (addr, 0);
568           char* off_name;
569           char* reg_name;
570
571           if (ZDA_NAME_P (name))
572             {
573               off_name = "zdaoff";
574               reg_name = "r0";
575             }
576           else if (SDA_NAME_P (name))
577             {
578               off_name = "sdaoff";
579               reg_name = "gp";
580             }
581           else if (TDA_NAME_P (name))
582             {
583               off_name = "tdaoff";
584               reg_name = "ep";
585             }
586           else
587             abort ();
588
589           fprintf (file, "%s(", off_name);
590           output_addr_const (file, addr);
591           fprintf (file, ")[%s]", reg_name);
592         }
593       else
594         output_addr_const (file, addr);
595       break;
596     case CONST:
597       if (special_symbolref_operand (addr, VOIDmode))
598         {
599           char* name = XSTR (XEXP (XEXP (addr, 0), 0), 0);
600           char* off_name;
601           char* reg_name;
602
603           if (ZDA_NAME_P (name))
604             {
605               off_name = "zdaoff";
606               reg_name = "r0";
607             }
608           else if (SDA_NAME_P (name))
609             {
610               off_name = "sdaoff";
611               reg_name = "gp";
612             }
613           else if (TDA_NAME_P (name))
614             {
615               off_name = "tdaoff";
616               reg_name = "ep";
617             }
618           else
619             abort ();
620
621           fprintf (file, "%s(", off_name);
622           output_addr_const (file, addr);
623           fprintf (file, ")[%s]", reg_name);
624         }
625       else
626         output_addr_const (file, addr);
627       break;
628     default:
629       output_addr_const (file, addr);
630       break;
631     }
632 }
633
634 \f
635 /* Return appropriate code to load up a 1, 2, or 4 integer/floating
636    point value.  */
637
638 char *
639 output_move_single (operands)
640      rtx *operands;
641 {
642   rtx dst = operands[0];
643   rtx src = operands[1];
644
645   if (REG_P (dst))
646     {
647       if (REG_P (src))
648         return "mov %1,%0";
649
650       else if (GET_CODE (src) == CONST_INT)
651         {
652           HOST_WIDE_INT value = INTVAL (src);
653
654           if (CONST_OK_FOR_J (value))           /* signed 5 bit immediate */
655             return "mov %1,%0";
656
657           else if (CONST_OK_FOR_K (value))      /* signed 16 bit immediate */
658             return "movea lo(%1),%.,%0";
659
660           else if (CONST_OK_FOR_L (value))      /* upper 16 bits were set */
661             return "movhi hi(%1),%.,%0";
662
663           else                                  /* random constant */
664             return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
665         }
666
667       else if (GET_CODE (src) == CONST_DOUBLE && GET_MODE (src) == SFmode)
668         {
669           HOST_WIDE_INT high, low;
670
671           const_double_split (src, &high, &low);
672           if (CONST_OK_FOR_J (high))            /* signed 5 bit immediate */
673             return "mov %F1,%0";
674
675           else if (CONST_OK_FOR_K (high))       /* signed 16 bit immediate */
676             return "movea lo(%F1),%.,%0";
677
678           else if (CONST_OK_FOR_L (high))       /* upper 16 bits were set */
679             return "movhi hi(%F1),%.,%0";
680
681           else                                  /* random constant */
682             return "movhi hi(%F1),%.,%0\n\tmovea lo(%F1),%0,%0";
683         }
684
685       else if (GET_CODE (src) == MEM)
686         return "%S1ld%W1 %1,%0";
687
688       else if (special_symbolref_operand (src, VOIDmode))
689         return "movea %O1(%P1),%Q1,%0";
690
691       else if (GET_CODE (src) == LABEL_REF
692                || GET_CODE (src) == SYMBOL_REF
693                || GET_CODE (src) == CONST)
694         {
695           return "movhi hi(%1),%.,%0\n\tmovea lo(%1),%0,%0";
696         }
697
698       else if (GET_CODE (src) == HIGH)
699         return "movhi hi(%1),%.,%0";
700
701       else if (GET_CODE (src) == LO_SUM)
702         {
703           operands[2] = XEXP (src, 0);
704           operands[3] = XEXP (src, 1);
705           return "movea lo(%3),%2,%0";
706         }
707     }
708
709   else if (GET_CODE (dst) == MEM)
710     {
711       if (REG_P (src))
712         return "%S0st%W0 %1,%0";
713
714       else if (GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
715         return "%S0st%W0 %.,%0";
716
717       else if (GET_CODE (src) == CONST_DOUBLE
718                && CONST0_RTX (GET_MODE (dst)) == src)
719         return "%S0st%W0 %.,%0";
720     }
721
722   fatal_insn ("output_move_single:", gen_rtx (SET, VOIDmode, dst, src));
723   return "";
724 }
725
726 \f
727 /* Return appropriate code to load up an 8 byte integer or floating point value */
728
729 char *
730 output_move_double (operands)
731     rtx *operands;
732 {
733   enum machine_mode mode = GET_MODE (operands[0]);
734   rtx dst = operands[0];
735   rtx src = operands[1];
736
737   if (register_operand (dst, mode)
738       && register_operand (src, mode))
739     {
740       if (REGNO (src) + 1 == REGNO (dst))
741         return "mov %R1,%R0\n\tmov %1,%0";
742       else
743         return "mov %1,%0\n\tmov %R1,%R0";
744     }
745
746   /* Storing 0 */
747   if (GET_CODE (dst) == MEM
748       && ((GET_CODE (src) == CONST_INT && INTVAL (src) == 0)
749           || (GET_CODE (src) == CONST_DOUBLE && CONST_DOUBLE_OK_FOR_G (src))))
750     return "st.w %.,%0\n\tst.w %.,%R0";
751
752   if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE)
753     {
754       HOST_WIDE_INT high_low[2];
755       int i;
756       rtx xop[10];
757
758       if (GET_CODE (src) == CONST_DOUBLE)
759         const_double_split (src, &high_low[1], &high_low[0]);
760       else
761         {
762           high_low[0] = INTVAL (src);
763           high_low[1] = (INTVAL (src) >= 0) ? 0 : -1;
764         }
765
766       for (i = 0; i < 2; i++)
767         {
768           xop[0] = gen_rtx (REG, SImode, REGNO (dst)+i);
769           xop[1] = GEN_INT (high_low[i]);
770           output_asm_insn (output_move_single (xop), xop);
771         }
772
773       return "";
774     }
775
776   if (GET_CODE (src) == MEM)
777     {
778       int ptrreg = -1;
779       int dreg = REGNO (dst);
780       rtx inside = XEXP (src, 0);
781
782       if (GET_CODE (inside) == REG)
783         ptrreg = REGNO (inside);
784       else if (GET_CODE (inside) == SUBREG)
785         ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
786       else if (GET_CODE (inside) == PLUS)
787         ptrreg = REGNO (XEXP (inside, 0));
788       else if (GET_CODE (inside) == LO_SUM)
789         ptrreg = REGNO (XEXP (inside, 0));
790
791       if (dreg == ptrreg)
792         return "ld.w %R1,%R0\n\tld.w %1,%0";
793     }
794
795   if (GET_CODE (src) == MEM)
796     return "ld.w %1,%0\n\tld.w %R1,%R0";
797   
798   if (GET_CODE (dst) == MEM)
799     return "st.w %1,%0\n\tst.w %R1,%R0";
800
801   return "mov %1,%0\n\tmov %R1,%R0";
802 }
803
804 \f
805 /* Return maximum offset supported for a short EP memory reference of mode
806    MODE and signedness UNSIGNEDP.  */
807
808 int
809 ep_memory_offset (mode, unsignedp)
810      enum machine_mode mode;
811      int unsignedp;
812 {
813   int max_offset = 0;
814
815   switch (mode)
816     {
817     case QImode:
818       max_offset = (1 << 7);
819       break;
820
821     case HImode:
822       max_offset = (1 << 8);
823       break;
824
825     case SImode:
826     case SFmode:
827       max_offset = (1 << 8);
828       break;
829     }
830
831   return max_offset;
832 }
833
834 /* Return true if OP is a valid short EP memory reference */
835
836 int
837 ep_memory_operand (op, mode, unsigned_load)
838      rtx op;
839      enum machine_mode mode;
840      int unsigned_load;
841 {
842   rtx addr, op0, op1;
843   int max_offset;
844   int mask;
845
846   if (GET_CODE (op) != MEM)
847     return FALSE;
848
849   max_offset = ep_memory_offset (mode, unsigned_load);
850
851   mask = GET_MODE_SIZE (mode) - 1;
852
853   addr = XEXP (op, 0);
854   if (GET_CODE (addr) == CONST)
855     addr = XEXP (addr, 0);
856
857   switch (GET_CODE (addr))
858     {
859     default:
860       break;
861
862     case SYMBOL_REF:
863       return TDA_NAME_P (XSTR (addr, 0));
864
865     case REG:
866       return REGNO (addr) == EP_REGNUM;
867
868     case PLUS:
869       op0 = XEXP (addr, 0);
870       op1 = XEXP (addr, 1);
871       if (GET_CODE (op1) == CONST_INT
872           && INTVAL (op1) < max_offset
873           && (INTVAL (op1) & mask) == 0)
874         {
875           if (GET_CODE (op0) == REG && REGNO (op0) == EP_REGNUM)
876             return TRUE;
877
878           if (GET_CODE (op0) == SYMBOL_REF && TDA_NAME_P (XSTR (op0, 0)))
879             return TRUE;
880         }
881       break;
882     }
883
884   return FALSE;
885 }
886
887 /* Return true if OP is either a register or 0 */
888
889 int
890 reg_or_0_operand (op, mode)
891      rtx op;
892      enum machine_mode mode;
893 {
894   if (GET_CODE (op) == CONST_INT)
895     return INTVAL (op) == 0;
896
897   else if (GET_CODE (op) == CONST_DOUBLE)
898     return CONST_DOUBLE_OK_FOR_G (op);
899
900   else
901     return register_operand (op, mode);
902 }
903
904 /* Return true if OP is either a register or a signed five bit integer */
905
906 int
907 reg_or_int5_operand (op, mode)
908      rtx op;
909      enum machine_mode mode;
910 {
911   if (GET_CODE (op) == CONST_INT)
912     return CONST_OK_FOR_J (INTVAL (op));
913
914   else
915     return register_operand (op, mode);
916 }
917
918 /* Return true if OP is a valid call operand.  */
919
920 int
921 call_address_operand (op, mode)
922      rtx op;
923      enum machine_mode mode;
924 {
925   /* Only registers are valid call operands if TARGET_LONG_CALLS.  */
926   if (TARGET_LONG_CALLS)
927     return GET_CODE (op) == REG;
928   return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
929 }
930
931 int
932 special_symbolref_operand (op, mode)
933      rtx op;
934      enum machine_mode mode;
935 {
936   if (GET_CODE (op) == SYMBOL_REF)
937     return ENCODED_NAME_P (XSTR (op, 0));
938
939   else if (GET_CODE (op) == CONST)
940     return (GET_CODE (XEXP (op, 0)) == PLUS
941             && GET_CODE (XEXP (XEXP (op, 0), 0)) == SYMBOL_REF
942             && ENCODED_NAME_P (XSTR (XEXP (XEXP (op, 0), 0), 0))
943             && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST_INT
944             && CONST_OK_FOR_K (INTVAL (XEXP (XEXP (op, 0), 1))));
945
946   return FALSE;
947 }
948
949 int
950 movsi_source_operand (op, mode)
951      rtx op;
952      enum machine_mode mode;
953 {
954   /* Some constants, as well as symbolic operands
955      must be done with HIGH & LO_SUM patterns.  */
956   if (CONSTANT_P (op)
957       && GET_CODE (op) != HIGH
958       && !(GET_CODE (op) == CONST_INT
959            && (CONST_OK_FOR_J (INTVAL (op))
960                || CONST_OK_FOR_K (INTVAL (op))
961                || CONST_OK_FOR_L (INTVAL (op)))))
962     return special_symbolref_operand (op, mode);
963   else
964     return general_operand (op, mode);
965 }
966
967 int
968 power_of_two_operand (op, mode)
969      rtx op;
970      enum machine_mode mode;
971 {
972   if (GET_CODE (op) != CONST_INT)
973     return 0;
974
975   if (exact_log2 (INTVAL (op)) == -1)
976     return 0;
977   return 1;
978 }
979
980 int
981 not_power_of_two_operand (op, mode)
982      rtx op;
983      enum machine_mode mode;
984 {
985   unsigned int mask;
986
987   if (mode == QImode)
988     mask = 0xff;
989   else if (mode == HImode)
990     mask = 0xffff;
991   else if (mode == SImode)
992     mask = 0xffffffff; 
993   else
994     return 0;
995
996   if (GET_CODE (op) != CONST_INT)
997     return 0;
998
999   if (exact_log2 (~INTVAL (op) & mask) == -1)
1000     return 0;
1001   return 1;
1002 }
1003
1004 \f
1005 /* Substitute memory references involving a pointer, to use the ep pointer,
1006    taking care to save and preserve the ep.  */
1007
1008 static void
1009 substitute_ep_register (first_insn, last_insn, uses, regno, p_r1, p_ep)
1010      rtx first_insn;
1011      rtx last_insn;
1012      int uses;
1013      int regno;
1014      rtx *p_r1;
1015      rtx *p_ep;
1016 {
1017   rtx reg = gen_rtx (REG, Pmode, regno);
1018   rtx insn;
1019   int i;
1020
1021   if (!*p_r1)
1022     {
1023       regs_ever_live[1] = 1;
1024       *p_r1 = gen_rtx (REG, Pmode, 1);
1025       *p_ep = gen_rtx (REG, Pmode, 30);
1026     }
1027
1028   if (TARGET_DEBUG)
1029     fprintf (stderr, "Saved %d bytes (%d uses of register %s) in function %s, starting as insn %d, ending at %d\n",
1030              2 * (uses - 3), uses, reg_names[regno],
1031              IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
1032              INSN_UID (first_insn), INSN_UID (last_insn));
1033
1034   if (GET_CODE (first_insn) == NOTE)
1035     first_insn = next_nonnote_insn (first_insn);
1036
1037   last_insn = next_nonnote_insn (last_insn);
1038   for (insn = first_insn; insn && insn != last_insn; insn = NEXT_INSN (insn))
1039     {
1040       if (GET_CODE (insn) == INSN)
1041         {
1042           rtx pattern = single_set (insn);
1043
1044           /* Replace the memory references.  */
1045           if (pattern)
1046             {
1047               rtx *p_mem;
1048               /* Memory operands are signed by default.  */
1049               int unsignedp = FALSE;
1050
1051               if (GET_CODE (SET_DEST (pattern)) == MEM
1052                   && GET_CODE (SET_SRC (pattern)) == MEM)
1053                 p_mem = (rtx *)0;
1054
1055               else if (GET_CODE (SET_DEST (pattern)) == MEM)
1056                 p_mem = &SET_DEST (pattern);
1057
1058               else if (GET_CODE (SET_SRC (pattern)) == MEM)
1059                 p_mem = &SET_SRC (pattern);
1060
1061               else
1062                 p_mem = (rtx *)0;
1063
1064               if (p_mem)
1065                 {
1066                   rtx addr = XEXP (*p_mem, 0);
1067
1068                   if (GET_CODE (addr) == REG && REGNO (addr) == regno)
1069                     *p_mem = change_address (*p_mem, VOIDmode, *p_ep);
1070
1071                   else if (GET_CODE (addr) == PLUS
1072                            && GET_CODE (XEXP (addr, 0)) == REG
1073                            && REGNO (XEXP (addr, 0)) == regno
1074                            && GET_CODE (XEXP (addr, 1)) == CONST_INT
1075                            && (((unsigned)INTVAL (XEXP (addr, 1)))
1076                                < ep_memory_offset (GET_MODE (*p_mem),
1077                                                    unsignedp)))
1078                     *p_mem = change_address (*p_mem, VOIDmode,
1079                                              gen_rtx (PLUS, Pmode,
1080                                                       *p_ep, XEXP (addr, 1)));
1081                 }
1082             }
1083         }
1084     }
1085
1086   /* Optimize back to back cases of ep <- r1 & r1 <- ep.  */
1087   insn = prev_nonnote_insn (first_insn);
1088   if (insn && GET_CODE (insn) == INSN
1089       && GET_CODE (PATTERN (insn)) == SET
1090       && SET_DEST (PATTERN (insn)) == *p_ep
1091       && SET_SRC (PATTERN (insn)) == *p_r1)
1092     delete_insn (insn);
1093   else
1094     emit_insn_before (gen_rtx (SET, Pmode, *p_r1, *p_ep), first_insn);
1095
1096   emit_insn_before (gen_rtx (SET, Pmode, *p_ep, reg), first_insn);
1097   emit_insn_before (gen_rtx (SET, Pmode, *p_ep, *p_r1), last_insn);
1098 }
1099
1100 \f
1101 /* In rare cases, correct code generation requires extra machine
1102    dependent processing between the second jump optimization pass and
1103    delayed branch scheduling.  On those machines, define this macro
1104    as a C statement to act on the code starting at INSN.
1105
1106    On the 850, we use it to implement the -mep mode to copy heavily used
1107    pointers to ep to use the implicit addressing */
1108
1109 void v850_reorg (start_insn)
1110      rtx start_insn;
1111 {
1112   struct {
1113     int uses;
1114     rtx first_insn;
1115     rtx last_insn;
1116   } regs[FIRST_PSEUDO_REGISTER];
1117
1118   int i;
1119   int use_ep = FALSE;
1120   rtx r1 = NULL_RTX;
1121   rtx ep = NULL_RTX;
1122   rtx insn;
1123   rtx pattern;
1124
1125   /* If not ep mode, just return now */
1126   if (!TARGET_EP)
1127     return;
1128
1129   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1130     {
1131       regs[i].uses = 0;
1132       regs[i].first_insn = NULL_RTX;
1133       regs[i].last_insn = NULL_RTX;
1134     }
1135
1136   for (insn = start_insn; insn != NULL_RTX; insn = NEXT_INSN (insn))
1137     {
1138       switch (GET_CODE (insn))
1139         {
1140           /* End of basic block */
1141         default:
1142           if (!use_ep)
1143             {
1144               int max_uses = -1;
1145               int max_regno = -1;
1146
1147               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1148                 {
1149                   if (max_uses < regs[i].uses)
1150                     {
1151                       max_uses = regs[i].uses;
1152                       max_regno = i;
1153                     }
1154                 }
1155
1156               if (max_uses > 3)
1157                 substitute_ep_register (regs[max_regno].first_insn,
1158                                         regs[max_regno].last_insn,
1159                                         max_uses, max_regno, &r1, &ep);
1160             }
1161
1162           use_ep = FALSE;
1163           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1164             {
1165               regs[i].uses = 0;
1166               regs[i].first_insn = NULL_RTX;
1167               regs[i].last_insn = NULL_RTX;
1168             }
1169           break;
1170
1171         case NOTE:
1172           break;
1173
1174         case INSN:
1175           pattern = single_set (insn);
1176
1177           /* See if there are any memory references we can shorten */
1178           if (pattern)
1179             {
1180               rtx src = SET_SRC (pattern);
1181               rtx dest = SET_DEST (pattern);
1182               rtx mem;
1183               /* Memory operands are signed by default.  */
1184               int unsignedp = FALSE;
1185
1186               if (GET_CODE (dest) == MEM && GET_CODE (src) == MEM)
1187                 mem = NULL_RTX;
1188
1189               else if (GET_CODE (dest) == MEM)
1190                 mem = dest;
1191
1192               else if (GET_CODE (src) == MEM)
1193                 mem = src;
1194
1195               else
1196                 mem = NULL_RTX;
1197
1198               if (mem && ep_memory_operand (mem, GET_MODE (mem), unsignedp))
1199                 use_ep = TRUE;
1200
1201               else if (!use_ep && mem
1202                        && GET_MODE_SIZE (GET_MODE (mem)) <= UNITS_PER_WORD)
1203                 {
1204                   rtx addr = XEXP (mem, 0);
1205                   int regno = -1;
1206                   int short_p;
1207
1208                   if (GET_CODE (addr) == REG)
1209                     {
1210                       short_p = TRUE;
1211                       regno = REGNO (addr);
1212                     }
1213
1214                   else if (GET_CODE (addr) == PLUS
1215                            && GET_CODE (XEXP (addr, 0)) == REG
1216                            && GET_CODE (XEXP (addr, 1)) == CONST_INT
1217                            && (((unsigned)INTVAL (XEXP (addr, 1)))
1218                                < ep_memory_offset (GET_MODE (mem), unsignedp)))
1219                     {
1220                       short_p = TRUE;
1221                       regno = REGNO (XEXP (addr, 0));
1222                     }
1223
1224                   else
1225                     short_p = FALSE;
1226
1227                   if (short_p)
1228                     {
1229                       regs[regno].uses++;
1230                       regs[regno].last_insn = insn;
1231                       if (!regs[regno].first_insn)
1232                         regs[regno].first_insn = insn;
1233                     }
1234                 }
1235
1236               /* Loading up a register in the basic block zaps any savings
1237                  for the register */
1238               if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
1239                 {
1240                   enum machine_mode mode = GET_MODE (dest);
1241                   int word = 0;
1242                   int regno;
1243                   int endregno;
1244
1245                   while (GET_CODE (dest) == SUBREG)
1246                     {
1247                       word = SUBREG_WORD (dest);
1248                       dest = SUBREG_REG (dest);
1249                     }
1250
1251                   regno = REGNO (dest) + word;
1252                   endregno = regno + HARD_REGNO_NREGS (regno, mode);
1253
1254                   if (!use_ep)
1255                     {
1256                       /* See if we can use the pointer before this
1257                          modification.  */
1258                       int max_uses = -1;
1259                       int max_regno = -1;
1260
1261                       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1262                         {
1263                           if (max_uses < regs[i].uses)
1264                             {
1265                               max_uses = regs[i].uses;
1266                               max_regno = i;
1267                             }
1268                         }
1269
1270                       if (max_uses > 3
1271                           && max_regno >= regno
1272                           && max_regno < endregno)
1273                         {
1274                           substitute_ep_register (regs[max_regno].first_insn,
1275                                                   regs[max_regno].last_insn,
1276                                                   max_uses, max_regno, &r1, &ep);
1277
1278                           /* Since we made a substitution, zap all remembered
1279                              registers.  */
1280                           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1281                             {
1282                               regs[i].uses = 0;
1283                               regs[i].first_insn = NULL_RTX;
1284                               regs[i].last_insn = NULL_RTX;
1285                             }
1286                         }
1287                     }
1288
1289                   for (i = regno; i < endregno; i++)
1290                     {
1291                       regs[i].uses = 0;
1292                       regs[i].first_insn = NULL_RTX;
1293                       regs[i].last_insn = NULL_RTX;
1294                     }
1295                 }
1296             }
1297         }
1298     }
1299 }
1300
1301 \f
1302 /* # of registers saved by the interrupt handler.  */
1303 #define INTERRUPT_FIXED_NUM 4
1304
1305 /* # of bytes for registers saved by the interrupt handler.  */
1306 #define INTERRUPT_FIXED_SAVE_SIZE (4 * INTERRUPT_FIXED_NUM)
1307
1308 /* # of registers saved in register parameter area.  */
1309 #define INTERRUPT_REGPARM_NUM 4
1310 /* # of words saved for other registers.  */
1311 #define INTERRUPT_ALL_SAVE_NUM \
1312   (30 - INTERRUPT_FIXED_NUM + INTERRUPT_REGPARM_NUM)
1313
1314 #define INTERRUPT_ALL_SAVE_SIZE (4 * INTERRUPT_ALL_SAVE_NUM)
1315
1316 int
1317 compute_register_save_size (p_reg_saved)
1318      long *p_reg_saved;
1319 {
1320   int size = 0;
1321   int i;
1322   int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1323   int call_p = regs_ever_live[31];
1324   long reg_saved = 0;
1325
1326   /* Count the return pointer if we need to save it.  */
1327   if (profile_flag && !call_p)
1328     regs_ever_live[31] = call_p = 1;
1329  
1330   /* Count space for the register saves.  */
1331   if (interrupt_handler)
1332     {
1333       for (i = 0; i <= 31; i++)
1334         switch (i)
1335           {
1336           default:
1337             if (regs_ever_live[i] || call_p)
1338               {
1339                 size += 4;
1340                 reg_saved |= 1L << i;
1341               }
1342             break;
1343
1344             /* We don't save/restore r0 or the stack pointer */
1345           case 0:
1346           case STACK_POINTER_REGNUM:
1347             break;
1348
1349             /* For registers with fixed use, we save them, set them to the
1350                appropriate value, and then restore them.
1351                These registers are handled specially, so don't list them
1352                on the list of registers to save in the prologue.  */
1353           case 1:               /* temp used to hold ep */
1354           case 4:               /* gp */
1355           case 10:              /* temp used to call interrupt save/restore */
1356           case EP_REGNUM:       /* ep */
1357             size += 4;
1358             break;
1359           }
1360     }
1361
1362   else
1363     for (i = 0; i <= 31; i++)
1364       if (regs_ever_live[i] && ((! call_used_regs[i]) || i == 31))
1365         {
1366           size += 4;
1367           reg_saved |= 1L << i;
1368         }
1369
1370   if (p_reg_saved)
1371     *p_reg_saved = reg_saved;
1372
1373   return size;
1374 }
1375
1376 int
1377 compute_frame_size (size, p_reg_saved)
1378      int size;
1379      long *p_reg_saved;
1380 {
1381   extern int current_function_outgoing_args_size;
1382
1383   return (size
1384           + compute_register_save_size (p_reg_saved)
1385           + current_function_outgoing_args_size);
1386 }
1387
1388 \f
1389 void
1390 expand_prologue ()
1391 {
1392   unsigned int i;
1393   int offset;
1394   unsigned int size = get_frame_size ();
1395   unsigned int actual_fsize;
1396   unsigned int init_stack_alloc = 0;
1397   rtx save_regs[32];
1398   rtx save_all;
1399   int num_save;
1400   int default_stack;
1401   int code;
1402   int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1403   long reg_saved = 0;
1404
1405   actual_fsize = compute_frame_size (size, &reg_saved);
1406
1407   /* Save/setup global registers for interrupt functions right now */
1408   if (interrupt_handler)
1409     {
1410       emit_insn (gen_save_interrupt ());
1411       actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1412       if (((1L << 31) & reg_saved) != 0)
1413         actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1414     }
1415
1416   /* Save arg registers to the stack if necessary.  */
1417   else if (current_function_anonymous_args)
1418     {
1419       if (TARGET_PROLOG_FUNCTION)
1420         emit_insn (gen_save_r6_r9 ());
1421       else
1422         {
1423           offset = 0;
1424           for (i = 6; i < 10; i++)
1425             {
1426               emit_move_insn (gen_rtx (MEM, SImode,
1427                                        plus_constant (stack_pointer_rtx,
1428                                                       offset)),
1429                               gen_rtx (REG, SImode, i));
1430               offset += 4;
1431             }
1432         }
1433     }
1434
1435   /* Identify all of the saved registers */
1436   num_save = 0;
1437   default_stack = 0;
1438   for (i = 1; i < 31; i++)
1439     {
1440       if (((1L << i) & reg_saved) != 0)
1441         save_regs[num_save++] = gen_rtx (REG, Pmode, i);
1442     }
1443
1444   /* If the return pointer is saved, the helper functions also allocate
1445      16 bytes of stack for arguments to be saved in.  */
1446   if (((1L << 31) & reg_saved) != 0)
1447     {
1448       save_regs[num_save++] = gen_rtx (REG, Pmode, 31);
1449       default_stack = 16;
1450     }
1451
1452   /* See if we have an insn that allocates stack space and saves the particular
1453      registers we want to.  */
1454   save_all = NULL_RTX;
1455   if (TARGET_PROLOG_FUNCTION && num_save > 0 && actual_fsize >= default_stack)
1456     {
1457       int alloc_stack = (4 * num_save) + default_stack;
1458       int unalloc_stack = actual_fsize - alloc_stack;
1459       int save_func_len = 4;
1460       int save_normal_len;
1461
1462       if (unalloc_stack)
1463         save_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1464
1465       /* see if we would have used ep to save the stack */
1466       if (TARGET_EP && num_save > 3 && (unsigned)actual_fsize < 255)
1467         save_normal_len = (3 * 2) + (2 * num_save);
1468       else
1469         save_normal_len = 4 * num_save;
1470
1471       save_normal_len += CONST_OK_FOR_J (actual_fsize) ? 2 : 4;
1472
1473       /* Don't bother checking if we don't actually save any space.
1474          This happens for instance if one register is saved and additional
1475          stack space is allocated.  */
1476       if (save_func_len < save_normal_len)
1477         {
1478           save_all = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (num_save + (TARGET_V850 ? 2 : 1)));
1479           XVECEXP (save_all, 0, 0) = gen_rtx (SET, VOIDmode,
1480                                               stack_pointer_rtx,
1481                                               gen_rtx (PLUS, Pmode,
1482                                                        stack_pointer_rtx,
1483                                                        GEN_INT (-alloc_stack)));
1484
1485           if (TARGET_V850)
1486             {
1487               XVECEXP (save_all, 0, num_save+1)
1488                 = gen_rtx (CLOBBER, VOIDmode, gen_rtx (REG, Pmode, 10));
1489             }
1490
1491           offset = - default_stack;
1492           for (i = 0; i < num_save; i++)
1493             {
1494               XVECEXP (save_all, 0, i+1)
1495                 = gen_rtx (SET, VOIDmode,
1496                            gen_rtx (MEM, Pmode,
1497                                     plus_constant (stack_pointer_rtx, offset)),
1498                                     save_regs[i]);
1499               offset -= 4;
1500             }
1501
1502           code = recog (save_all, NULL_RTX, NULL_PTR);
1503           if (code >= 0)
1504             {
1505               rtx insn = emit_insn (save_all);
1506               INSN_CODE (insn) = code;
1507               actual_fsize -= alloc_stack;
1508
1509               if (TARGET_DEBUG)
1510                 fprintf (stderr, "Saved %d bytes via prologue function (%d vs. %d) for function %s\n",
1511                          save_normal_len - save_func_len,
1512                          save_normal_len, save_func_len,
1513                          IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1514             }
1515           else
1516             save_all = NULL_RTX;
1517         }
1518     }
1519
1520   /* If no prolog save function is available, store the registers the old fashioned
1521      way (one by one). */
1522   if (!save_all)
1523     {
1524       /* Special case interrupt functions that save all registers for a call.  */
1525       if (interrupt_handler && ((1L << 31) & reg_saved) != 0)
1526         emit_insn (gen_save_all_interrupt ());
1527
1528       else
1529         {
1530           /* If the stack is too big, allocate it in chunks so we can do the
1531              register saves.  We use the register save size so we use the ep
1532              register.  */
1533           if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1534             init_stack_alloc = compute_register_save_size (NULL);
1535           else
1536             init_stack_alloc = actual_fsize;
1537               
1538           /* Save registers at the beginning of the stack frame */
1539           offset = init_stack_alloc - 4;
1540           
1541           if (init_stack_alloc)
1542             emit_insn (gen_addsi3 (stack_pointer_rtx,
1543                                    stack_pointer_rtx,
1544                                    GEN_INT (-init_stack_alloc)));
1545           
1546           /* Save the return pointer first.  */
1547           if (num_save > 0 && REGNO (save_regs[num_save-1]) == 31)
1548             {
1549               emit_move_insn (gen_rtx (MEM, SImode,
1550                                        plus_constant (stack_pointer_rtx,
1551                                                       offset)),
1552                               save_regs[--num_save]);
1553               offset -= 4;
1554             }
1555           
1556           for (i = 0; i < num_save; i++)
1557             {
1558               emit_move_insn (gen_rtx (MEM, SImode,
1559                                        plus_constant (stack_pointer_rtx,
1560                                                       offset)),
1561                               save_regs[i]);
1562               offset -= 4;
1563             }
1564         }
1565     }
1566
1567   /* Allocate the rest of the stack that was not allocated above (either it is
1568      > 32K or we just called a function to save the registers and needed more
1569      stack.  */
1570   if (actual_fsize > init_stack_alloc)
1571     {
1572       int diff = actual_fsize - init_stack_alloc;
1573       if (CONST_OK_FOR_K (diff))
1574         emit_insn (gen_addsi3 (stack_pointer_rtx,
1575                                stack_pointer_rtx,
1576                                GEN_INT (-diff)));
1577       else
1578         {
1579           rtx reg = gen_rtx (REG, Pmode, 12);
1580           emit_move_insn (reg, GEN_INT (-diff));
1581           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
1582         }
1583     }
1584
1585   /* If we need a frame pointer, set it up now.  */
1586   if (frame_pointer_needed)
1587     emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
1588 }
1589 \f
1590
1591 void
1592 expand_epilogue ()
1593 {
1594   unsigned int i;
1595   int offset;
1596   unsigned int size = get_frame_size ();
1597   long reg_saved = 0;
1598   unsigned int actual_fsize = compute_frame_size (size, &reg_saved);
1599   unsigned int init_stack_free = 0;
1600   rtx restore_regs[32];
1601   rtx restore_all;
1602   int num_restore;
1603   int default_stack;
1604   int code;
1605   int interrupt_handler = v850_interrupt_function_p (current_function_decl);
1606
1607   /* Eliminate the initial stack stored by interrupt functions.  */
1608   if (interrupt_handler)
1609     {
1610       actual_fsize -= INTERRUPT_FIXED_SAVE_SIZE;
1611       if (((1L << 31) & reg_saved) != 0)
1612         actual_fsize -= INTERRUPT_ALL_SAVE_SIZE;
1613     }
1614
1615   /* Cut off any dynamic stack created.  */
1616   if (frame_pointer_needed)
1617     emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
1618
1619   /* Identify all of the saved registers */
1620   num_restore = 0;
1621   default_stack = 0;
1622   for (i = 1; i < 31; i++)
1623     {
1624       if (((1L << i) & reg_saved) != 0)
1625         restore_regs[num_restore++] = gen_rtx (REG, Pmode, i);
1626     }
1627
1628   /* If the return pointer is saved, the helper functions also allocate
1629      16 bytes of stack for arguments to be saved in.  */
1630   if (((1L << 31) & reg_saved) != 0)
1631     {
1632       restore_regs[num_restore++] = gen_rtx (REG, Pmode, 31);
1633       default_stack = 16;
1634     }
1635
1636   /* See if we have an insn that restores the particular registers we
1637      want to.  */
1638   restore_all = NULL_RTX;
1639   if (TARGET_PROLOG_FUNCTION && num_restore > 0 && actual_fsize >= default_stack
1640       && !interrupt_handler)
1641     {
1642       int alloc_stack = (4 * num_restore) + default_stack;
1643       int unalloc_stack = actual_fsize - alloc_stack;
1644       int restore_func_len = 4;
1645       int restore_normal_len;
1646
1647       if (unalloc_stack)
1648         restore_func_len += CONST_OK_FOR_J (unalloc_stack) ? 2 : 4;
1649
1650       /* see if we would have used ep to restore the registers */
1651       if (TARGET_EP && num_restore > 3 && (unsigned)actual_fsize < 255)
1652         restore_normal_len = (3 * 2) + (2 * num_restore);
1653       else
1654         restore_normal_len = 4 * num_restore;
1655
1656       restore_normal_len += (CONST_OK_FOR_J (actual_fsize) ? 2 : 4) + 2;
1657
1658       /* Don't bother checking if we don't actually save any space.  */
1659       if (restore_func_len < restore_normal_len)
1660         {
1661           restore_all = gen_rtx (PARALLEL, VOIDmode,
1662                                  rtvec_alloc (num_restore + 2));
1663           XVECEXP (restore_all, 0, 0) = gen_rtx (RETURN, VOIDmode);
1664           XVECEXP (restore_all, 0, 1)
1665             = gen_rtx (SET, VOIDmode, stack_pointer_rtx,
1666                        gen_rtx (PLUS, Pmode,
1667                                 stack_pointer_rtx,
1668                                 GEN_INT (alloc_stack)));
1669
1670           offset = alloc_stack - 4;
1671           for (i = 0; i < num_restore; i++)
1672             {
1673               XVECEXP (restore_all, 0, i+2)
1674                 = gen_rtx (SET, VOIDmode,
1675                            restore_regs[i],
1676                            gen_rtx (MEM, Pmode,
1677                                     plus_constant (stack_pointer_rtx, offset)));
1678               offset -= 4;
1679             }
1680
1681           code = recog (restore_all, NULL_RTX, NULL_PTR);
1682           if (code >= 0)
1683             {
1684               rtx insn;
1685
1686               actual_fsize -= alloc_stack;
1687               if (actual_fsize)
1688                 {
1689                   if (CONST_OK_FOR_K (actual_fsize))
1690                     emit_insn (gen_addsi3 (stack_pointer_rtx,
1691                                            stack_pointer_rtx,
1692                                            GEN_INT (actual_fsize)));
1693                   else
1694                     {
1695                       rtx reg = gen_rtx (REG, Pmode, 12);
1696                       emit_move_insn (reg, GEN_INT (actual_fsize));
1697                       emit_insn (gen_addsi3 (stack_pointer_rtx,
1698                                              stack_pointer_rtx,
1699                                              reg));
1700                     }
1701                 }
1702
1703               insn = emit_jump_insn (restore_all);
1704               INSN_CODE (insn) = code;
1705
1706               if (TARGET_DEBUG)
1707                 fprintf (stderr, "Saved %d bytes via epilogue function (%d vs. %d) in function %s\n",
1708                          restore_normal_len - restore_func_len,
1709                          restore_normal_len, restore_func_len,
1710                          IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1711             }
1712           else
1713             restore_all = NULL_RTX;
1714         }
1715     }
1716
1717   /* If no epilog save function is available, restore the registers the
1718      old fashioned way (one by one). */
1719   if (!restore_all)
1720     {
1721       /* If the stack is large, we need to cut it down in 2 pieces.  */
1722       if (actual_fsize && !CONST_OK_FOR_K (-actual_fsize))
1723         init_stack_free = 4 * num_restore;
1724       else
1725         init_stack_free = actual_fsize;
1726
1727       /* Deallocate the rest of the stack if it is > 32K or if extra stack
1728          was allocated for an interrupt handler that makes a call.  */
1729       if (actual_fsize > init_stack_free || (interrupt_handler && actual_fsize))
1730         {
1731           int diff = actual_fsize - ((interrupt_handler) ? 0 : init_stack_free);
1732           if (CONST_OK_FOR_K (diff))
1733             emit_insn (gen_addsi3 (stack_pointer_rtx,
1734                                    stack_pointer_rtx,
1735                                    GEN_INT (diff)));
1736           else
1737             {
1738               rtx reg = gen_rtx (REG, Pmode, 12);
1739               emit_move_insn (reg, GEN_INT (diff));
1740               emit_insn (gen_addsi3 (stack_pointer_rtx,
1741                                      stack_pointer_rtx,
1742                                      reg));
1743             }
1744         }
1745
1746       /* Special case interrupt functions that save all registers
1747          for a call.  */
1748       if (interrupt_handler && ((1L << 31) & reg_saved) != 0)
1749         emit_insn (gen_restore_all_interrupt ());
1750       else
1751         {
1752           /* Restore registers from the beginning of the stack frame */
1753           offset = init_stack_free - 4;
1754
1755           /* Restore the return pointer first.  */
1756           if (num_restore > 0 && REGNO (restore_regs[num_restore-1]) == 31)
1757             {
1758               emit_move_insn (restore_regs[--num_restore],
1759                               gen_rtx (MEM, SImode,
1760                                        plus_constant (stack_pointer_rtx,
1761                                                       offset)));
1762               offset -= 4;
1763             }
1764
1765           for (i = 0; i < num_restore; i++)
1766             {
1767               emit_move_insn (restore_regs[i],
1768                               gen_rtx (MEM, SImode,
1769                                        plus_constant (stack_pointer_rtx,
1770                                                       offset)));
1771
1772               offset -= 4;
1773             }
1774
1775           /* Cut back the remainder of the stack.  */
1776           if (init_stack_free)
1777             emit_insn (gen_addsi3 (stack_pointer_rtx,
1778                                    stack_pointer_rtx,
1779                                    GEN_INT (init_stack_free)));
1780         }
1781
1782       /* And return or use reti for interrupt handlers.  */
1783       if (interrupt_handler)
1784         emit_jump_insn (gen_restore_interrupt ());
1785       else if (actual_fsize)
1786         emit_jump_insn (gen_return_internal ());
1787       else
1788         emit_jump_insn (gen_return ());
1789     }
1790
1791   current_function_anonymous_args = 0;
1792   v850_interrupt_cache_p = FALSE;
1793   v850_interrupt_p = FALSE;
1794 }
1795
1796 \f
1797 /* Update the condition code from the insn.  */
1798
1799 void
1800 notice_update_cc (body, insn)
1801      rtx body;
1802      rtx insn;
1803 {
1804   switch (get_attr_cc (insn))
1805     {
1806     case CC_NONE:
1807       /* Insn does not affect CC at all.  */
1808       break;
1809
1810     case CC_NONE_0HIT:
1811       /* Insn does not change CC, but the 0'th operand has been changed.  */
1812       if (cc_status.value1 != 0
1813           && reg_overlap_mentioned_p (recog_operand[0], cc_status.value1))
1814         cc_status.value1 = 0;
1815       break;
1816
1817     case CC_SET_ZN:
1818       /* Insn sets the Z,N flags of CC to recog_operand[0].
1819          V,C is in an unusable state.  */
1820       CC_STATUS_INIT;
1821       cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1822       cc_status.value1 = recog_operand[0];
1823       break;
1824
1825     case CC_SET_ZNV:
1826       /* Insn sets the Z,N,V flags of CC to recog_operand[0].
1827          C is in an unusable state.  */
1828       CC_STATUS_INIT;
1829       cc_status.flags |= CC_NO_CARRY;
1830       cc_status.value1 = recog_operand[0];
1831       break;
1832
1833     case CC_COMPARE:
1834       /* The insn is a compare instruction.  */
1835       CC_STATUS_INIT;
1836       cc_status.value1 = SET_SRC (body);
1837       break;
1838
1839     case CC_CLOBBER:
1840       /* Insn doesn't leave CC in a usable state.  */
1841       CC_STATUS_INIT;
1842       break;
1843     }
1844 }
1845
1846 \f
1847 /* Return nonzero if ATTR is a valid attribute for DECL.
1848    ATTRIBUTES are any existing attributes and ARGS are the arguments
1849    supplied with ATTR.
1850
1851    Supported attributes:
1852
1853    interrupt_handler or interrupt: output a prologue and epilogue suitable
1854    for an interrupt handler.  */
1855
1856 int
1857 v850_valid_machine_decl_attribute (decl, attributes, attr, args)
1858      tree decl;
1859      tree attributes;
1860      tree attr;
1861      tree args;
1862 {
1863   if (args != NULL_TREE)
1864     return 0;
1865
1866   if (is_attribute_p ("interrupt_handler", attr)
1867       || is_attribute_p ("interrupt", attr))
1868     return TREE_CODE (decl) == FUNCTION_DECL;
1869
1870   return 0;
1871 }
1872
1873 \f
1874 /* Return nonzero if FUNC is an interrupt function as specified
1875    by the "interrupt" attribute.  */
1876
1877 int
1878 v850_interrupt_function_p (func)
1879      tree func;
1880 {
1881   tree a;
1882   int ret = 0;
1883
1884   if (v850_interrupt_cache_p)
1885     return v850_interrupt_p;
1886
1887   if (TREE_CODE (func) != FUNCTION_DECL)
1888     return 0;
1889
1890   a = lookup_attribute ("interrupt_handler", DECL_MACHINE_ATTRIBUTES (func));
1891   if (a != NULL_TREE)
1892     ret = 1;
1893
1894   else
1895     {
1896       a = lookup_attribute ("interrupt", DECL_MACHINE_ATTRIBUTES (func));
1897       ret = a != NULL_TREE;
1898     }
1899
1900   /* Its not safe to trust global variables until after function inlining has
1901      been done.  */
1902   if (reload_completed | reload_in_progress)
1903     v850_interrupt_p = ret;
1904
1905   return ret;
1906 }
1907
1908 \f
1909 extern struct obstack *saveable_obstack;
1910
1911 v850_encode_data_area (decl)
1912      tree decl;
1913 {
1914   char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1915   int len = strlen (str);
1916   char *newstr;
1917
1918   /* In the Cygnus sources we actually do something; this is just
1919      here to make merges easier.  */
1920   return;
1921 }
1922
1923 /* Return true if the given RTX is a register which can be restored
1924    by a function epilogue.  */
1925 int
1926 register_is_ok_for_epilogue (op, mode)
1927      rtx op;
1928      enum machine_mode mode;
1929 {
1930   /* The save/restore routines can only cope with registers 2, and 20 - 31 */
1931   return (GET_CODE (op) == REG)
1932           && (((REGNO (op) >= 20) && REGNO (op) <= 31)
1933               || REGNO (op) == 2);
1934 }
1935
1936 /* Return non-zero if the given RTX is suitable for collapsing into
1937    jump to a function epilogue.  */
1938 int
1939 pattern_is_ok_for_epilogue (op, mode)
1940      rtx op;
1941      enum machine_mode mode;
1942 {
1943   int count = XVECLEN (op, 0);
1944   int i;
1945   
1946   /* If there are no registers to restore then the function epilogue
1947      is not suitable.  */
1948   if (count <= 2)
1949     return 0;
1950
1951   /* The pattern matching has already established that we are performing a
1952      function epilogue and that we are popping at least one register.  We must
1953      now check the remaining entries in the vector to make sure that they are
1954      also register pops.  There is no good reason why there should ever be
1955      anything else in this vector, but being paranoid always helps...
1956
1957      The test below performs the C equivalent of this machine description
1958      pattern match:
1959
1960         (set (match_operand:SI n "register_is_ok_for_epilogue" "r")
1961           (mem:SI (plus:SI (reg:SI 3) (match_operand:SI n "immediate_operand" "i"))))
1962      */
1963
1964   for (i = 3; i < count; i++)
1965     {
1966       rtx vector_element = XVECEXP (op, 0, i);
1967       rtx dest;
1968       rtx src;
1969       rtx plus;
1970       
1971       if (GET_CODE (vector_element) != SET)
1972         return 0;
1973       
1974       dest = SET_DEST (vector_element);
1975       src = SET_SRC (vector_element);
1976
1977       if (GET_CODE (dest) != REG
1978           || GET_MODE (dest) != SImode
1979           || ! register_is_ok_for_epilogue (dest, SImode)
1980           || GET_CODE (src) != MEM
1981           || GET_MODE (src) != SImode)
1982         return 0;
1983
1984       plus = XEXP (src, 0);
1985
1986       if (GET_CODE (plus) != PLUS
1987           || GET_CODE (XEXP (plus, 0)) != REG
1988           || GET_MODE (XEXP (plus, 0)) != SImode
1989           || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
1990           || GET_CODE (XEXP (plus, 1)) != CONST_INT)
1991         return 0;
1992     }
1993
1994   return 1;
1995 }
1996
1997 /* Construct a JR instruction to a routine that will perform the equivalent of
1998    the RTL passed in as an argument.  This RTL is a function epilogue that
1999    pops registers off the stack and possibly releases some extra stack space
2000    as well.  The code has already verified that the RTL matches these
2001    requirements.  */
2002 char *
2003 construct_restore_jr (op)
2004      rtx op;
2005 {
2006   int count = XVECLEN (op, 0);
2007   int stack_bytes;
2008   unsigned long int mask;
2009   unsigned long int first;
2010   unsigned long int last;
2011   int i;
2012   static char buff [100]; /* XXX */
2013   
2014   if (count <= 2)
2015     {
2016       error ("Bogus JR construction: %d\n", count);
2017       return NULL;
2018     }
2019
2020   /* Work out how many bytes to pop off the stack before retrieving
2021      registers.  */
2022   if (GET_CODE (XVECEXP (op, 0, 1)) != SET)
2023     abort ();
2024   if (GET_CODE (SET_SRC (XVECEXP (op, 0, 1))) != PLUS)
2025     abort ();
2026   if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1)) != CONST_INT)
2027     abort ();
2028     
2029   stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 1)), 1));
2030
2031   /* Each pop will remove 4 bytes from the stack... */
2032   stack_bytes -= (count - 2) * 4;
2033
2034   /* Make sure that the amount we are popping either 0 or 16 bytes.  */
2035   if (stack_bytes != 0 && stack_bytes != 16)
2036     {
2037       error ("Bad amount of stack space removal: %d", stack_bytes);
2038       return NULL;
2039     }
2040
2041   /* Now compute the bit mask of registers to push.  */
2042   mask = 0;
2043   for (i = 2; i < count; i++)
2044     {
2045       rtx vector_element = XVECEXP (op, 0, i);
2046       
2047       if (GET_CODE (vector_element) != SET)
2048         abort ();
2049       if (GET_CODE (SET_DEST (vector_element)) != REG)
2050         abort ();
2051       if (! register_is_ok_for_epilogue (SET_DEST (vector_element), SImode))
2052         abort ();
2053       
2054       mask |= 1 << REGNO (SET_DEST (vector_element));
2055     }
2056
2057   /* Scan for the first register to pop.  */
2058   for (first = 0; first < 32; first++)
2059     {
2060       if (mask & (1 << first))
2061         break;
2062     }
2063
2064   if (first >= 32)
2065     abort ();
2066
2067   /* Discover the last register to pop.  */
2068   if (mask & (1 << 31))
2069     {
2070       if (stack_bytes != 16)
2071         abort ();
2072       
2073       last = 31;
2074     }
2075   else
2076     {
2077       if (stack_bytes != 0)
2078         abort ();
2079       if ((mask & (1 << 29)) == 0)
2080         abort ();
2081       
2082       last = 29;
2083     }
2084
2085   /* Note, it is possible to have gaps in the register mask.
2086      We ignore this here, and generate a JR anyway.  We will
2087      be popping more registers thatn is strictly necessary, but
2088      it does save code space.  */
2089   
2090   if (first == last)
2091     sprintf (buff, "jr __return_%s", reg_names [first]);
2092   else
2093     sprintf (buff, "jr __return_%s_%s", reg_names [first], reg_names [last]);
2094
2095   return buff;
2096 }
2097
2098
2099 /* Return non-zero if the given RTX is suitable for collapsing into
2100    a jump to a function prologue.  */
2101 int
2102 pattern_is_ok_for_prologue (op, mode)
2103      rtx op;
2104      enum machine_mode mode;
2105 {
2106   int count = XVECLEN (op, 0);
2107   int i; 
2108   rtx vector_element;
2109  
2110   /* If there are no registers to save then the function prologue
2111      is not suitable.  */
2112   if (count <= 2)
2113     return 0;
2114
2115   /* The pattern matching has already established that we are adjusting the
2116      stack and pushing at least one register.  We must now check that the
2117      remaining entries in the vector to make sure that they are also register
2118      pushes, except for the last entry which should be a CLOBBER of r10.
2119
2120      The test below performs the C equivalent of this machine description
2121      pattern match:
2122
2123      (set (mem:SI (plus:SI (reg:SI 3)
2124       (match_operand:SI 2 "immediate_operand" "i")))
2125       (match_operand:SI 3 "register_is_ok_for_epilogue" "r"))
2126
2127      */
2128
2129   for (i = 2; i < count - 1; i++)
2130     {
2131       rtx dest;
2132       rtx src;
2133       rtx plus;
2134       
2135       vector_element = XVECEXP (op, 0, i);
2136       
2137       if (GET_CODE (vector_element) != SET)
2138         return 0;
2139       
2140       dest = SET_DEST (vector_element);
2141       src = SET_SRC (vector_element);
2142
2143       if (GET_CODE (dest) != MEM
2144           || GET_MODE (dest) != SImode
2145           || GET_CODE (src) != REG
2146           || GET_MODE (src) != SImode
2147           || ! register_is_ok_for_epilogue (src, SImode))
2148         return 0;
2149
2150       plus = XEXP (dest, 0);
2151
2152       if ( GET_CODE (plus) != PLUS
2153           || GET_CODE (XEXP (plus, 0)) != REG
2154           || GET_MODE (XEXP (plus, 0)) != SImode
2155           || REGNO (XEXP (plus, 0)) != STACK_POINTER_REGNUM
2156           || GET_CODE (XEXP (plus, 1)) != CONST_INT)
2157         return 0;
2158
2159       /* If the register is being pushed somewhere other than the stack
2160          space just acquired by the first operand then abandon this quest.
2161          Note: the test is <= because both values are negative.  */
2162       if (INTVAL (XEXP (plus, 1))
2163           <= INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)))
2164         {
2165           return 0;
2166         }
2167     }
2168
2169   /* Make sure that the last entry in the vector is a clobber.  */
2170   vector_element = XVECEXP (op, 0, i);
2171   
2172   if (GET_CODE (vector_element) != CLOBBER
2173       || GET_CODE (XEXP (vector_element, 0)) != REG
2174       || REGNO (XEXP (vector_element, 0)) != 10)
2175     return 0;
2176   
2177   return 1;
2178 }
2179
2180 /* Construct a JARL instruction to a routine that will perform the equivalent
2181    of the RTL passed as a parameter.  This RTL is a function prologue that
2182    saves some of the registers r20 - r31 onto the stack, and possibly acquires
2183    some stack space as well.  The code has already verified that the RTL
2184    matches these requirements.  */
2185 char *
2186 construct_save_jarl (op)
2187      rtx op;
2188 {
2189   int count = XVECLEN (op, 0);
2190   int stack_bytes;
2191   unsigned long int mask;
2192   unsigned long int first;
2193   unsigned long int last;
2194   int i;
2195   static char buff [100]; /* XXX */
2196   
2197   if (count <= 2)
2198     {
2199       error ("Bogus JARL construction: %d\n", count);
2200       return NULL;
2201     }
2202
2203   /* Paranoia.  */
2204   if (GET_CODE (XVECEXP (op, 0, 0)) != SET)
2205     abort ();
2206   if (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != PLUS)
2207     abort ();
2208   if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0)) != REG)
2209     abort ();
2210   if (GET_CODE (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1)) != CONST_INT)
2211     abort ();
2212     
2213   /* Work out how many bytes to push onto the stack after storing the
2214      registers.  */
2215   stack_bytes = INTVAL (XEXP (SET_SRC (XVECEXP (op, 0, 0)), 1));
2216
2217   /* Each push will put 4 bytes from the stack... */
2218   stack_bytes += (count - 2) * 4;
2219
2220   /* Make sure that the amount we are popping either 0 or 16 bytes.  */
2221   if (stack_bytes != 0 && stack_bytes != -16)
2222     {
2223       error ("Bad amount of stack space removal: %d", stack_bytes);
2224       return NULL;
2225     }
2226
2227   /* Now compute the bit mask of registers to push.  */
2228   mask = 0;
2229   for (i = 1; i < count - 1; i++)
2230     {
2231       rtx vector_element = XVECEXP (op, 0, i);
2232       
2233       if (GET_CODE (vector_element) != SET)
2234         abort ();
2235       if (GET_CODE (SET_SRC (vector_element)) != REG)
2236         abort ();
2237       if (! register_is_ok_for_epilogue (SET_SRC (vector_element), SImode))
2238         abort ();
2239       
2240       mask |= 1 << REGNO (SET_SRC (vector_element));
2241     }
2242
2243   /* Scan for the first register to push.  */  
2244   for (first = 0; first < 32; first++)
2245     {
2246       if (mask & (1 << first))
2247         break;
2248     }
2249
2250   if (first >= 32)
2251     abort ();
2252
2253   /* Discover the last register to push.  */
2254   if (mask & (1 << 31))
2255     {
2256       if (stack_bytes != -16)
2257         abort ();
2258       
2259       last = 31;
2260     }
2261   else
2262     {
2263       if (stack_bytes != 0)
2264         abort ();
2265       if ((mask & (1 << 29)) == 0)
2266         abort ();
2267       
2268       last = 29;
2269     }
2270
2271   /* Note, it is possible to have gaps in the register mask.
2272      We ignore this here, and generate a JARL anyway.  We will
2273      be pushing more registers thatn is strictly necessary, but
2274      it does save code space.  */
2275   
2276   if (first == last)
2277     sprintf (buff, "jarl __save_%s, r10", reg_names [first]);
2278   else
2279     sprintf (buff, "jarl __save_%s_%s, r10", reg_names [first],
2280              reg_names [last]);
2281
2282   return buff;
2283 }
2284