OSDN Git Service

(shiftcosts, genshifty_op): Add SH3 support.
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Hitachi Super-H.
2    Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
3
4    This file is part of GNU CC.
5
6    GNU CC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU CC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU CC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* Contributed by Steve Chamberlain (sac@cygnus.com).
21    Improved by Jim Wilson (wilson@cygnus.com).  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "insn-flags.h"
31 #include "expr.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "output.h"
35
36 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
37 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
38
39 /* ??? The pragma interrupt support will not work for SH3.  */
40 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
41    output code for the next function appropriate for an interrupt handler.  */
42 int pragma_interrupt;
43
44 /* This is set by #pragma trapa, and is similar to the above, except that
45    the compiler doesn't emit code to preserve all registers.  */
46 static int pragma_trapa;
47
48 /* This is used for communication between SETUP_INCOMING_VARARGS and
49    sh_expand_prologue.  */
50 int current_function_anonymous_args;
51
52 /* Global variables from toplev.c and final.c that are used within, but
53    not declared in any header file.  */
54 extern char *version_string;
55 extern int *insn_addresses;
56
57 /* Global variables for machine-dependent things. */
58
59 /* Which cpu are we scheduling for.  */
60 enum processor_type sh_cpu;
61
62 /* Saved operands from the last compare to use when we generate an scc
63    or bcc insn.  */
64
65 rtx sh_compare_op0;
66 rtx sh_compare_op1;
67
68 /* Provides the class number of the smallest class containing
69    reg number.  */
70
71 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
72 {
73   R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
74   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
75   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
76   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
77   GENERAL_REGS, PR_REGS, T_REGS, NO_REGS,
78   MAC_REGS, MAC_REGS,
79 };
80
81 /* Provide reg_class from a letter such as appears in the machine
82    description.  */
83
84 enum reg_class reg_class_from_letter[] =
85 {
86   /* a */ NO_REGS, /* b */ NO_REGS, /* c */ NO_REGS, /* d */ NO_REGS,
87   /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
88   /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ PR_REGS,
89   /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
90   /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
91   /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ MAC_REGS,
92   /* y */ NO_REGS, /* z */ R0_REGS
93 };
94 \f
95 /* Print the operand address in x to the stream.  */
96
97 void
98 print_operand_address (stream, x)
99      FILE *stream;
100      rtx x;
101 {
102   switch (GET_CODE (x))
103     {
104     case REG:
105       fprintf (stream, "@%s", reg_names[REGNO (x)]);
106       break;
107
108     case PLUS:
109       {
110         rtx base = XEXP (x, 0);
111         rtx index = XEXP (x, 1);
112
113         switch (GET_CODE (index))
114           {
115           case CONST_INT:
116             fprintf (stream, "@(%d,%s)", INTVAL (index),
117                      reg_names[REGNO (base)]);
118             break;
119
120           case REG:
121             fprintf (stream, "@(r0,%s)",
122                      reg_names[MAX (REGNO (base), REGNO (index))]);
123             break;
124
125           default:
126             debug_rtx (x);
127             abort ();
128           }
129       }
130       break;
131
132     case PRE_DEC:
133       fprintf (stream, "@-%s", reg_names[REGNO (XEXP (x, 0))]);
134       break;
135
136     case POST_INC:
137       fprintf (stream, "@%s+", reg_names[REGNO (XEXP (x, 0))]);
138       break;
139
140     default:
141       output_addr_const (stream, x);
142       break;
143     }
144 }
145
146 /* Print operand x (an rtx) in assembler syntax to file stream
147    according to modifier code.
148
149    '.'  print a .s if insn needs delay slot
150    '@'  print rte or rts depending upon pragma interruptness
151    '#'  output a nop if there is nothing to put in the delay slot
152    'O'  print a constant without the #
153    'R'  print the LSW of a dp value - changes if in little endian
154    'S'  print the MSW of a dp value - changes if in little endian
155    'T'  print the next word of a dp value - same as 'R' in big endian mode.  */
156
157 void
158 print_operand (stream, x, code)
159      FILE *stream;
160      rtx x;
161      int code;
162 {
163   switch (code)
164     {
165     case '.':
166       if (final_sequence)
167         fprintf (stream, ".s");
168       break;
169     case '@':
170       if (pragma_interrupt)
171         fprintf (stream, "rte");
172       else
173         fprintf (stream, "rts");
174       break;
175     case '#':
176       /* Output a nop if there's nothing in the delay slot.  */
177       if (dbr_sequence_length () == 0)
178         fprintf (stream, "\n\tnop");
179       break;
180     case 'O':
181       output_addr_const (stream, x);
182       break;
183     case 'R':
184       fputs (reg_names[REGNO (x) + LSW], (stream));
185       break;
186     case 'S':
187       fputs (reg_names[REGNO (x) + MSW], (stream));
188       break;
189     case 'T':
190       /* Next word of a double.  */
191       switch (GET_CODE (x))
192         {
193         case REG:
194           fputs (reg_names[REGNO (x) + 1], (stream));
195           break;
196         case MEM:
197           print_operand_address (stream,
198                                  XEXP (adj_offsettable_operand (x, 4), 0));
199           break;
200         }
201       break;
202     default:
203       switch (GET_CODE (x))
204         {
205         case REG:
206           fputs (reg_names[REGNO (x)], (stream));
207           break;
208         case MEM:
209           output_address (XEXP (x, 0));
210           break;
211         default:
212           fputc ('#', stream);
213           output_addr_const (stream, x);
214           break;
215         }
216       break;
217     }
218 }
219 \f
220 /* Emit code to perform a block move.  Choose the best method.
221
222    OPERANDS[0] is the destination.
223    OPERANDS[1] is the source.
224    OPERANDS[2] is the size.
225    OPERANDS[3] is the alignment safe to use.  */
226
227 int
228 expand_block_move (operands)
229      rtx *operands;
230 {
231   int align = INTVAL (operands[3]);
232   int constp = (GET_CODE (operands[2]) == CONST_INT);
233   int bytes = (constp ? INTVAL (operands[2]) : 0);
234
235   /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
236      alignment, or if it isn't a multiple of 4 bytes, then fail.  */
237   if (! constp || align < 4 || (bytes % 4 != 0))
238     return 0;
239
240   if (bytes < 64)
241     {
242       char entry[30];
243       tree entry_name;
244       rtx func_addr_rtx;
245       rtx r4 = gen_rtx (REG, SImode, 4);
246       rtx r5 = gen_rtx (REG, SImode, 5);
247
248       sprintf (entry, "__movstrSI%d", bytes);
249       entry_name = get_identifier (entry);
250
251       func_addr_rtx
252         = copy_to_mode_reg (Pmode,
253                             gen_rtx (SYMBOL_REF, Pmode,
254                                      IDENTIFIER_POINTER (entry_name)));
255       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
256       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
257       emit_insn (gen_block_move_real (func_addr_rtx));
258       return 1;
259     }
260
261   /* This is the same number of bytes as a memcpy call, but to a different
262      less common function name, so this will occasionally use more space.  */
263   if (! TARGET_SMALLCODE)
264     {
265       tree entry_name;
266       rtx func_addr_rtx;
267       int final_switch, while_loop;
268       rtx r4 = gen_rtx (REG, SImode, 4);
269       rtx r5 = gen_rtx (REG, SImode, 5);
270       rtx r6 = gen_rtx (REG, SImode, 6);
271
272       entry_name = get_identifier ("__movstr");
273       func_addr_rtx
274         = copy_to_mode_reg (Pmode,
275                             gen_rtx (SYMBOL_REF, Pmode,
276                                      IDENTIFIER_POINTER (entry_name)));
277       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
278       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
279
280       /* r6 controls the size of the move.  16 is decremented from it
281          for each 64 bytes moved.  Then the negative bit left over is used
282          as an index into a list of move instructions.  e.g., a 72 byte move
283          would be set up with size(r6) = 14, for one iteration through the
284          big while loop, and a switch of -2 for the last part.  */
285
286       final_switch = 16 - ((bytes / 4) % 16);
287       while_loop = ((bytes / 4) / 16 - 1) * 16;
288       emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
289       emit_insn (gen_block_lump_real (func_addr_rtx));
290       return 1;
291     }
292
293   return 0;
294 }
295
296 /* Prepare operands for a move define_expand; specifically, one of the
297    operands must be in a register.  */
298
299 int
300 prepare_move_operands (operands, mode)
301      rtx operands[];
302      enum machine_mode mode;
303 {
304   /* Copy the source to a register if both operands aren't registers.  */
305   if (! reload_in_progress && ! reload_completed
306       && ! register_operand (operands[0], mode)
307       && ! register_operand (operands[1], mode))
308     operands[1] = copy_to_mode_reg (mode, operands[1]);
309
310   return 0;
311 }
312
313 /* Prepare the operands for an scc instruction; make sure that the
314    compare has been done.  */
315 rtx
316 prepare_scc_operands (code)
317      enum rtx_code code;
318 {
319   rtx t_reg = gen_rtx (REG, SImode, T_REG);
320   enum rtx_code oldcode = code;
321
322   /* First need a compare insn.  */
323   switch (code)
324     {
325     case NE:
326       /* It isn't possible to handle this case.  */
327       abort ();
328     case LT:
329       code = GT;
330       break;
331     case LE:
332       code = GE;
333       break;
334     case LTU:
335       code = GTU;
336       break;
337     case LEU:
338       code = GEU;
339       break;
340     }
341   if (code != oldcode)
342     {
343       rtx tmp = sh_compare_op0;
344       sh_compare_op0 = sh_compare_op1;
345       sh_compare_op1 = tmp;
346     }
347
348   sh_compare_op0 = force_reg (SImode, sh_compare_op0);
349   if (code != EQ && code != NE
350       && (sh_compare_op1 != const0_rtx
351           || code == GTU  || code == GEU || code == LTU || code == LEU))
352     sh_compare_op1 = force_reg (SImode, sh_compare_op1);
353
354   emit_insn (gen_rtx (SET, VOIDmode, t_reg,
355                       gen_rtx (code, SImode, sh_compare_op0,
356                                sh_compare_op1)));
357
358   return t_reg;
359 }
360
361 /* Called from the md file, set up the operands of a compare instruction.  */
362
363 void
364 from_compare (operands, code)
365      rtx *operands;
366      int code;
367 {
368   if (code != EQ && code != NE)
369     {
370       /* Force args into regs, since we can't use constants here.  */
371       sh_compare_op0 = force_reg (SImode, sh_compare_op0);
372       if (sh_compare_op1 != const0_rtx
373           || code == GTU  || code == GEU || code == LTU || code == LEU)
374         sh_compare_op1 = force_reg (SImode, sh_compare_op1);
375     }
376   operands[1] = sh_compare_op0;
377   operands[2] = sh_compare_op1;
378 }
379 \f
380 /* Functions to output assembly code.  */
381
382 /* Return a sequence of instructions to perform DI or DF move.
383
384    Since the SH cannot move a DI or DF in one instruction, we have
385    to take care when we see overlapping source and dest registers.  */
386
387 char *
388 output_movedouble (insn, operands, mode)
389      rtx insn;
390      rtx operands[];
391      enum machine_mode mode;
392 {
393   rtx dst = operands[0];
394   rtx src = operands[1];
395
396   if (GET_CODE (dst) == MEM
397       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
398     return "mov.l       %T1,%0\n\tmov.l %1,%0";
399
400   if (register_operand (dst, mode)
401       && register_operand (src, mode))
402     {
403       if (REGNO (src) == MACH_REG)
404         return "sts     mach,%S0\n\tsts macl,%R0";
405
406       /* When mov.d r1,r2 do r2->r3 then r1->r2;
407          when mov.d r1,r0 do r1->r0 then r2->r1.  */
408
409       if (REGNO (src) + 1 == REGNO (dst))
410         return "mov     %T1,%T0\n\tmov  %1,%0";
411       else
412         return "mov     %1,%0\n\tmov    %T1,%T0";
413     }
414   else if (GET_CODE (src) == CONST_INT)
415     {
416       if (INTVAL (src) < 0)
417         output_asm_insn ("mov   #-1,%S0", operands);
418       else
419         output_asm_insn ("mov   #0,%S0", operands);
420
421       return "mov       %1,%R0";
422     }
423   else if (GET_CODE (src) == MEM)
424     {
425       int ptrreg = -1;
426       int dreg = REGNO (dst);
427       rtx inside = XEXP (src, 0);
428
429       if (GET_CODE (inside) == REG)
430         ptrreg = REGNO (inside);
431       else if (GET_CODE (inside) == PLUS)
432         {
433           ptrreg = REGNO (XEXP (inside, 0));
434           /* ??? A r0+REG address shouldn't be possible here, because it isn't
435              an offsettable address.  Unfortunately, offsettable addresses use
436              QImode to check the offset, and a QImode offsettable address
437              requires r0 for the other operand, which is not currently
438              supported, so we can't use the 'o' constraint.
439              Thus we must check for and handle r0+REG addresses here.
440              We punt for now, since this is likely very rare.  */
441           if (GET_CODE (XEXP (inside, 1)) == REG)
442             abort ();
443         }
444       else if (GET_CODE (inside) == LABEL_REF)
445         return "mov.l   %1,%0\n\tmov.l  %1+4,%T0";
446       else if (GET_CODE (inside) == POST_INC)
447         return "mov.l   %1,%0\n\tmov.l  %1,%T0";
448       else
449         abort ();
450
451       /* Work out the safe way to copy.  Copy into the second half first.  */
452       if (dreg == ptrreg)
453         return "mov.l   %T1,%T0\n\tmov.l        %1,%0";
454     }
455
456   return "mov.l %1,%0\n\tmov.l  %T1,%T0";
457 }
458
459 /* Print an instruction which would have gone into a delay slot after
460    another instruction, but couldn't because the other instruction expanded
461    into a sequence where putting the slot insn at the end wouldn't work.  */
462
463 static void
464 print_slot (insn)
465      rtx insn;
466 {
467   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
468
469   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
470 }
471
472 /* We can't tell if we need a register as a scratch for the jump
473    until after branch shortening, and then it's too late to allocate a
474    register the 'proper' way.  These instruction sequences are rare
475    anyway, so to avoid always using a reg up from our limited set, we'll
476    grab one when we need one on output.  */
477
478 /* ??? Should fix compiler so that using a clobber scratch in jump
479    instructions works, and then this will be unnecessary.  */
480
481 char *
482 output_far_jump (insn, op)
483      rtx insn;
484      rtx op;
485 {
486   rtx thislab = gen_label_rtx ();
487
488   /* Output the delay slot insn first if any.  */
489   if (dbr_sequence_length ())
490     print_slot (final_sequence);
491
492   output_asm_insn ("mov.l       r13,@-r15", 0);
493   output_asm_insn ("mov.l       %O0,r13", &thislab);
494   output_asm_insn ("jmp @r13", 0);
495   output_asm_insn ("mov.l       @r15+,r13", 0);
496   output_asm_insn (".align      2", 0);
497   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (thislab));
498   output_asm_insn (".long       %O0", &op);
499   return "";
500 }
501
502 /* Local label counter, used for constants in the pool and inside
503    pattern branches.  */
504
505 static int lf = 100;
506
507 /* Output code for ordinary branches.  */
508
509 char *
510 output_branch (logic, insn, operands)
511      int logic;
512      rtx insn;
513      rtx *operands;
514 {
515   int label = lf++;
516
517   switch (get_attr_length (insn))
518     {
519     case 2:
520       /* A branch with an unfilled delay slot.  */
521     case 4:
522       /* Simple branch in range -252..+258 bytes */
523       return logic ? "bt%.      %l0" : "bf%.    %l0";
524
525     case 6:
526       /* A branch with an unfilled delay slot.  */
527     case 8:
528       /* Branch in range -4092..+4098 bytes.  */
529       {
530         /* The call to print_slot will clobber the operands.  */
531         rtx op0 = operands[0];
532
533         if (final_sequence)
534           {
535             fprintf (asm_out_file, "\tb%c.s\tLF%d\n", logic ? 'f' : 't',
536                      label);
537             print_slot (final_sequence);
538           }
539         else
540           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
541
542         output_asm_insn ("bra   %l0", &op0);
543         fprintf (asm_out_file, "\tnop\n");
544         fprintf (asm_out_file, "LF%d:\n", label);
545       }
546       return "";
547
548     case 16:
549       /* A branch with an unfilled delay slot.  */
550     case 18:
551       /* Branches a long way away.  */
552       {
553         /* The call to print_slot will clobber the operands.  */
554         rtx op0 = operands[0];
555
556         if (final_sequence)
557           {
558             fprintf (asm_out_file, "\tb%c.s\tLF%d\n", logic ? 'f' : 't',
559                      label);
560             print_slot (final_sequence);
561           }
562         else
563           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
564
565         output_far_jump (insn, op0);
566         fprintf (asm_out_file, "LF%d:\n", label);
567         return "";
568       }
569     }
570   return "bad";
571 }
572 \f
573 /* A copy of the option structure defined in toplev.c.  */
574
575 struct option
576 {
577   char *string;
578   int *variable;
579   int on_value;
580 };
581
582 /* Output a single output option string NAME to FILE, without generating
583    lines longer than MAX.  */
584
585 static int
586 output_option (file, sep, type, name, indent, pos, max)
587      FILE *file;
588      char *sep;
589      char *type;
590      char *name;
591      char *indent;
592      int pos;
593      int max;
594 {
595   if (strlen (sep) + strlen (type) + strlen (name) + pos > max)
596     {
597       fprintf (file, indent);
598       return fprintf (file, "%s%s", type, name);
599     }
600   return pos + fprintf (file, "%s%s%s", sep, type, name);
601 }
602
603 /* A copy of the target_switches variable in toplev.c.  */
604
605 static struct
606 {
607   char *name;
608   int value;
609 } m_options[] = TARGET_SWITCHES;
610
611 /* Output all options to the assembly language file.  */
612
613 static void
614 output_options (file, f_options, f_len, W_options, W_len,
615                 pos, max, sep, indent, term)
616      FILE *file;
617      struct option *f_options;
618      struct option *W_options;
619      int f_len, W_len;
620      int pos;
621      int max;
622      char *sep;
623      char *indent;
624      char *term;
625 {
626   register int j;
627
628   if (optimize)
629     pos = output_option (file, sep, "-O", "", indent, pos, max);
630   if (write_symbols != NO_DEBUG)
631     pos = output_option (file, sep, "-g", "", indent, pos, max);
632   if (profile_flag)
633     pos = output_option (file, sep, "-p", "", indent, pos, max);
634   if (profile_block_flag)
635     pos = output_option (file, sep, "-a", "", indent, pos, max);
636
637   for (j = 0; j < f_len; j++)
638     if (*f_options[j].variable == f_options[j].on_value)
639       pos = output_option (file, sep, "-f", f_options[j].string,
640                            indent, pos, max);
641
642   for (j = 0; j < W_len; j++)
643     if (*W_options[j].variable == W_options[j].on_value)
644       pos = output_option (file, sep, "-W", W_options[j].string,
645                            indent, pos, max);
646
647   for (j = 0; j < sizeof m_options / sizeof m_options[0]; j++)
648     if (m_options[j].name[0] != '\0'
649         && m_options[j].value > 0
650         && ((m_options[j].value & target_flags)
651             == m_options[j].value))
652       pos = output_option (file, sep, "-m", m_options[j].name,
653                            indent, pos, max);
654
655   fprintf (file, term);
656 }
657
658 /* Output to FILE the start of the assembler file.  */
659
660 void
661 output_file_start (file, f_options, f_len, W_options, W_len)
662      FILE *file;
663      struct option *f_options;
664      struct option *W_options;
665      int f_len, W_len;
666 {
667   register int pos;
668
669   output_file_directive (file, main_input_filename);
670
671   /* Switch to the data section so that the coffsem symbol and the
672      gcc2_compiled. symbol aren't in the text section.  */
673   data_section ();
674
675   pos = fprintf (file, "\n! Hitachi SH cc1 (%s) arguments:", version_string);
676   output_options (file, f_options, f_len, W_options, W_len,
677                   pos, 75, " ", "\n! ", "\n\n");
678
679   if (TARGET_LITTLE_ENDIAN)
680     fprintf (file, "\t.little\n");
681 }
682 \f
683 /* Actual number of instructions used to make a shift by N.  */
684 static char ashiftrt_insns[] =
685   { 0,1,2,3,4,5,8,8,8,8,8,8,8,8,8,8,2,3,4,5,8,8,8,8,8,8,8,8,8,8,8,2};
686
687 /* Left shift and logical right shift are the same.  */
688 static char shift_insns[]    =
689   { 0,1,1,2,2,3,3,4,1,2,2,3,3,4,3,3,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
690
691 /* Individual shift amounts needed to get the above length sequences.
692    One bit right shifts clobber the T bit, so when possible, put one bit
693    shifts in the middle of the sequence, so the ends are eligible for
694    branch delay slots.  */
695 static short shift_amounts[32][5] = {
696   {0}, {1}, {2}, {2, 1},
697   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
698   {8}, {8, 1}, {8, 2}, {8, 1, 2},
699   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
700   {16}, {16, 1}, {16, 2}, {16, 1, 2},
701   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
702   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
703   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
704
705 /* This is used in length attributes in sh.md to help compute the length
706    of arbitrary constant shift instructions.  */
707
708 int
709 shift_insns_rtx (insn)
710      rtx insn;
711 {
712   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
713   int shift_count = INTVAL (XEXP (set_src, 1));
714   enum rtx_code shift_code = GET_CODE (set_src);
715
716   switch (shift_code)
717     {
718     case ASHIFTRT:
719       return ashiftrt_insns[shift_count];
720     case LSHIFTRT:
721     case ASHIFT:
722       return shift_insns[shift_count];
723     default:
724       abort();
725     }
726 }
727
728 /* Return the cost of a shift.  */
729
730 int
731 shiftcosts (x)
732      rtx x;
733 {
734   int value = INTVAL (XEXP (x, 1));
735
736   /* If shift by a non constant, then this will be expensive.  */
737   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
738     {
739       if (TARGET_SH3)
740         return 2;
741       /* If not an sh3 then we don't even have an instruction for it.  */
742       return 20;
743     }
744
745   /* Otherwise, return the true cost in instructions.  */
746   if (GET_CODE (x) == ASHIFTRT)
747     return ashiftrt_insns[value];
748   else
749     return shift_insns[value];
750 }
751
752 /* Return the cost of an AND operation.  */
753
754 int
755 andcosts (x)
756      rtx x;
757 {
758   int i;
759
760   /* Anding with a register is a single cycle and instruction.  */
761   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
762     return 1;
763
764   i = INTVAL (XEXP (x, 1));
765   /* These constants are single cycle extu.[bw] instructions.  */
766   if (i == 0xff || i == 0xffff)
767     return 1;
768   /* Constants that can be used in an and immediate instruction is a single
769      cycle, but this requires r0, so make it a little more expensive.  */
770   if (CONST_OK_FOR_L (i))
771     return 2;
772   /* Constants that can be loaded with a mov immediate and an and.
773      This case is probably unnecessary.  */
774   if (CONST_OK_FOR_I (i))
775     return 2;
776   /* Any other constants requires a 2 cycle pc-relative load plus an and.
777      This case is probably unnecessary.  */
778   return 3;
779 }
780
781 /* Return the cost of a multiply.  */
782 int
783 multcosts (x)
784      rtx x;
785 {
786   if (TARGET_SH2)
787     {
788       /* We have a mul insn, so we can never take more than the mul and the
789          read of the mac reg, but count more because of the latency and extra
790          reg usage.  */
791       if (TARGET_SMALLCODE)
792         return 2;
793       return 3;
794     }
795
796   /* If we're aiming at small code, then just count the number of
797      insns in a multiply call sequence.  */
798   if (TARGET_SMALLCODE)
799     return 5;
800
801   /* Otherwise count all the insns in the routine we'd be calling too.  */
802   return 20;
803 }
804
805 /* Code to expand a shift.  */
806
807 void
808 gen_ashift (type, n, reg)
809      int type;
810      int n;
811      rtx reg;
812 {
813   /* Negative values here come from the shift_amounts array.  */
814   if (n < 0)
815     {
816       if (type == ASHIFT)
817         type = LSHIFTRT;
818       else
819         type = ASHIFT;
820       n = -n;
821     }
822
823   switch (type)
824     {
825     case ASHIFTRT:
826       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
827       break;
828     case LSHIFTRT:
829       if (n == 1)
830         emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
831       else
832         emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
833       break;
834     case ASHIFT:
835       emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
836       break;
837     }
838 }
839
840 /* Output RTL to split a constant shift into its component SH constant
841    shift instructions.  */
842    
843 /* ??? For SH3, should reject constant shifts when slower than loading the
844    shift count into a register?  */
845
846 int
847 gen_shifty_op (code, operands)
848      int code;
849      rtx *operands;
850 {
851   int value = INTVAL (operands[2]);
852   int max, i;
853
854   if (value == 31)
855     {
856       if (code == LSHIFTRT)
857         {
858           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
859           emit_insn (gen_movt (operands[0]));
860           return;
861         }
862       else if (code == ASHIFT)
863         {
864           /* There is a two instruction sequence for 31 bit left shifts,
865              but it requires r0.  */
866           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
867             {
868               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
869               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
870               return;
871             }
872         }
873     }
874
875   max = shift_insns[value];
876   for (i = 0; i < max; i++)
877     gen_ashift (code, shift_amounts[value][i], operands[0]);
878 }
879
880 /* Output RTL for an arithmetic right shift.  */
881
882 /* ??? Rewrite to use super-optimizer sequences.  */
883
884 int
885 expand_ashiftrt (operands)
886      rtx *operands;
887 {
888   rtx wrk;
889   char func[18];
890   tree func_name;
891   int value;
892
893   if (TARGET_SH3 && GET_CODE (operands[2]) != CONST_INT)
894     {
895       rtx count = copy_to_mode_reg (SImode, operands[2]);
896       emit_insn (gen_negsi2 (count, count));
897       emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
898       return 1;
899     }
900   if (GET_CODE (operands[2]) != CONST_INT)
901     return 0;
902
903   value = INTVAL (operands[2]);
904
905   if (value == 31)
906     {
907       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
908       return 1;
909     }
910   else if (value >= 16 && value <= 19)
911     {
912       wrk = gen_reg_rtx (SImode);
913       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
914       value -= 16;
915       while (value--)
916         gen_ashift (ASHIFTRT, 1, wrk);
917       emit_move_insn (operands[0], wrk);
918       return 1;
919     }
920   /* Expand a short sequence inline, longer call a magic routine.  */
921   else if (value <= 5)
922     {
923       wrk = gen_reg_rtx (SImode);
924       emit_move_insn (wrk, operands[1]);
925       while (value--)
926         gen_ashift (ASHIFTRT, 1, wrk);
927       emit_move_insn (operands[0], wrk);
928       return 1;
929     }
930
931   wrk = gen_reg_rtx (Pmode);
932
933   /* Load the value into an arg reg and call a helper.  */
934   emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
935   sprintf (func, "__ashiftrt_r4_%d", value);
936   func_name = get_identifier (func);
937   emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
938                                 IDENTIFIER_POINTER (func_name)));
939   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
940   emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
941   return 1;
942 }
943 \f
944 /* The SH cannot load a large constant into a register, constants have to
945    come from a pc relative load.  The reference of a pc relative load
946    instruction must be less than 1k infront of the instruction.  This
947    means that we often have to dump a constant inside a function, and
948    generate code to branch around it.
949
950    It is important to minimize this, since the branches will slow things
951    down and make things bigger.
952
953    Worst case code looks like:
954
955    mov.l L1,rn
956    bra   L2
957    nop
958    align
959    L1:   .long value
960    L2:
961    ..
962
963    mov.l L3,rn
964    bra   L4
965    nop
966    align
967    L3:   .long value
968    L4:
969    ..
970
971    We fix this by performing a scan before scheduling, which notices which
972    instructions need to have their operands fetched from the constant table
973    and builds the table.
974
975    The algorithm is:
976
977    scan, find an instruction which needs a pcrel move.  Look forward, find the
978    last barrier which is within MAX_COUNT bytes of the requirement.
979    If there isn't one, make one.  Process all the instructions between
980    the find and the barrier.
981
982    In the above example, we can tell that L3 is within 1k of L1, so
983    the first move can be shrunk from the 3 insn+constant sequence into
984    just 1 insn, and the constant moved to L3 to make:
985
986    mov.l        L1,rn
987    ..
988    mov.l        L3,rn
989    bra          L4
990    nop
991    align
992    L3:.long value
993    L4:.long value
994
995    Then the second move becomes the target for the shortening process.  */
996
997 typedef struct
998 {
999   rtx value;                    /* Value in table.  */
1000   rtx label;                    /* Label of value.  */
1001   enum machine_mode mode;       /* Mode of value.  */
1002 } pool_node;
1003
1004 /* The maximum number of constants that can fit into one pool, since
1005    the pc relative range is 0...1020 bytes and constants are at least 4
1006    bytes long.  */
1007
1008 #define MAX_POOL_SIZE (1020/4)
1009 static pool_node pool_vector[MAX_POOL_SIZE];
1010 static int pool_size;
1011
1012 /* ??? If we need a constant in HImode which is the truncated value of a
1013    constant we need in SImode, we could combine the two entries thus saving
1014    two bytes.  Is this common enough to be worth the effort of implementing
1015    it?  */
1016
1017 /* ??? This stuff should be done at the same time that we shorten branches.
1018    As it is now, we must assume that all branches are the maximum size, and
1019    this causes us to almost always output constant pools sooner than
1020    necessary.  */
1021
1022 /* Add a constant to the pool and return its label.  */
1023
1024 static rtx
1025 add_constant (x, mode)
1026      rtx x;
1027      enum machine_mode mode;
1028 {
1029   int i;
1030   rtx lab;
1031
1032   /* First see if we've already got it.  */
1033   for (i = 0; i < pool_size; i++)
1034     {
1035       if (x->code == pool_vector[i].value->code
1036           && mode == pool_vector[i].mode)
1037         {
1038           if (x->code == CODE_LABEL)
1039             {
1040               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1041                 continue;
1042             }
1043           if (rtx_equal_p (x, pool_vector[i].value))
1044             return pool_vector[i].label;
1045         }
1046     }
1047
1048   /* Need a new one.  */
1049   pool_vector[pool_size].value = x;
1050   lab = gen_label_rtx ();
1051   pool_vector[pool_size].mode = mode;
1052   pool_vector[pool_size].label = lab;
1053   pool_size++;
1054   return lab;
1055 }
1056
1057 /* Output the literal table.  */
1058
1059 static void
1060 dump_table (scan)
1061      rtx scan;
1062 {
1063   int i;
1064   int need_align = 1;
1065
1066   /* Do two passes, first time dump out the HI sized constants.  */
1067
1068   for (i = 0; i < pool_size; i++)
1069     {
1070       pool_node *p = &pool_vector[i];
1071
1072       if (p->mode == HImode)
1073         {
1074           if (need_align)
1075             {
1076               scan = emit_insn_after (gen_align_2 (), scan);
1077               need_align = 0;
1078             }
1079           scan = emit_label_after (p->label, scan);
1080           scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1081         }
1082     }
1083
1084   need_align = 1;
1085
1086   for (i = 0; i < pool_size; i++)
1087     {
1088       pool_node *p = &pool_vector[i];
1089
1090       switch (p->mode)
1091         {
1092         case HImode:
1093           break;
1094         case SImode:
1095           if (need_align)
1096             {
1097               need_align = 0;
1098               scan = emit_label_after (gen_label_rtx (), scan);
1099               scan = emit_insn_after (gen_align_4 (), scan);
1100             }
1101           scan = emit_label_after (p->label, scan);
1102           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1103           break;
1104         case DImode:
1105           if (need_align)
1106             {
1107               need_align = 0;
1108               scan = emit_label_after (gen_label_rtx (), scan);
1109               scan = emit_insn_after (gen_align_4 (), scan);
1110             }
1111           scan = emit_label_after (p->label, scan);
1112           scan = emit_insn_after (gen_consttable_8 (p->value), scan);
1113           break;
1114         default:
1115           abort ();
1116           break;
1117         }
1118     }
1119
1120   scan = emit_insn_after (gen_consttable_end (), scan);
1121   scan = emit_barrier_after (scan);
1122   pool_size = 0;
1123 }
1124
1125 /* Return non-zero if constant would be an ok source for a
1126    mov.w instead of a mov.l.  */
1127
1128 static int
1129 hi_const (src)
1130      rtx src;
1131 {
1132   return (GET_CODE (src) == CONST_INT
1133           && INTVAL (src) >= -32768
1134           && INTVAL (src) <= 32767);
1135 }
1136
1137 /* Non-zero if the insn is a move instruction which needs to be fixed.  */
1138
1139 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1140    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
1141    need to fix it if the input value is CONST_OK_FOR_I.  */
1142
1143 static int
1144 broken_move (insn)
1145      rtx insn;
1146 {
1147   if (GET_CODE (insn) == INSN
1148       && GET_CODE (PATTERN (insn)) == SET
1149       /* We can load any 8 bit value if we don't care what the high
1150          order bits end up as.  */
1151       && GET_MODE (SET_DEST (PATTERN (insn))) != QImode
1152       && CONSTANT_P (SET_SRC (PATTERN (insn)))
1153       && (GET_CODE (SET_SRC (PATTERN (insn))) != CONST_INT
1154           || ! CONST_OK_FOR_I (INTVAL (SET_SRC (PATTERN (insn))))))
1155     return 1;
1156
1157   return 0;
1158 }
1159
1160 /* Find the last barrier from insn FROM which is close enough to hold the
1161    constant pool.  If we can't find one, then create one near the end of
1162    the range.  */
1163
1164 /* ??? It would be good to put constant pool tables between a case jump and
1165    the jump table.  This fails for two reasons.  First, there is no
1166    barrier after the case jump.  This is a bug in the casesi pattern.
1167    Second, inserting the table here may break the mova instruction that
1168    loads the jump table address, by moving the jump table too far away.
1169    We fix that problem by never outputting the constant pool between a mova
1170    and its label.  */
1171
1172 static rtx
1173 find_barrier (from)
1174      rtx from;
1175 {
1176   int count_si = 0;
1177   int count_hi = 0;
1178   int found_hi = 0;
1179   int found_si = 0;
1180   rtx found_barrier = 0;
1181   rtx found_mova = 0;
1182
1183   /* For HImode: range is 510, add 4 because pc counts from address of
1184      second instruction after this one, subtract 2 for the jump instruction
1185      that we may need to emit before the table.  This gives 512.
1186      For SImode: range is 1020, add 4 because pc counts from address of
1187      second instruction after this one, subtract 2 in case pc is 2 byte
1188      aligned, subtract 2 for the jump instruction that we may need to emit
1189      before the table.  This gives 1020.  */
1190   while (from && count_si < 1020 && count_hi < 512)
1191     {
1192       int inc;
1193
1194       if (GET_CODE (from) == BARRIER)
1195         found_barrier = from;
1196
1197       /* Count the length of this insn - we assume that all moves will
1198          be 2 bytes long, except the DImode/DFmode movess.  */
1199
1200       if (broken_move (from))
1201         {
1202           rtx src = SET_SRC (PATTERN (from));
1203
1204           if (hi_const (src))
1205             found_hi = 1;
1206           else
1207             found_si = 1;
1208           inc = (GET_MODE_SIZE (GET_MODE (src)) > 4) ? 4 : 2;
1209         }
1210       else
1211         inc = get_attr_length (from);
1212
1213       if (GET_CODE (from) == INSN
1214           && GET_CODE (PATTERN (from)) == SET
1215           && GET_CODE (SET_SRC (PATTERN (from))) == UNSPEC
1216           && XINT (SET_SRC (PATTERN (from)), 1) == 1)
1217         found_mova = from;
1218       else if (GET_CODE (from) == JUMP_INSN
1219                && (GET_CODE (PATTERN (from)) == ADDR_VEC
1220                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1221         found_mova = 0;
1222
1223       if (found_si)
1224         count_si += inc;
1225       if (found_hi)
1226         count_hi += inc;
1227       from = NEXT_INSN (from);
1228     }
1229
1230   /* Insert the constant pool table before the mova instruction, to prevent
1231      the mova label reference from going out of range.  */
1232   if (found_mova)
1233     from = found_mova;
1234
1235   if (! found_barrier)
1236     {
1237       /* We didn't find a barrier in time to dump our stuff,
1238          so we'll make one.  */
1239       rtx label = gen_label_rtx ();
1240
1241       /* We went one instruction too far above.  */
1242       from = PREV_INSN (from);
1243       /* Walk back to be just before any jump or label.
1244          Putting it before a label reduces the number of times the branch
1245          around the constant pool table will be hit.  Putting it before
1246          a jump makes it more likely that the bra delay slot will be
1247          filled.  */
1248       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
1249              || GET_CODE (from) == CODE_LABEL)
1250         from = PREV_INSN (from);
1251
1252       from = emit_jump_insn_after (gen_jump (label), from);
1253       JUMP_LABEL (from) = label;
1254       found_barrier = emit_barrier_after (from);
1255       emit_label_after (label, found_barrier);
1256     }
1257
1258   return found_barrier;
1259 }
1260
1261 /* Exported to toplev.c.
1262
1263    Scan the function looking for move instructions which have to be changed to
1264    pc-relative loads and insert the literal tables.  */
1265
1266 void
1267 machine_dependent_reorg (first)
1268      rtx first;
1269 {
1270   rtx insn;
1271
1272   for (insn = first; insn; insn = NEXT_INSN (insn))
1273     {
1274       if (broken_move (insn))
1275         {
1276           rtx scan;
1277           /* Scan ahead looking for a barrier to stick the constant table
1278              behind.  */
1279           rtx barrier = find_barrier (insn);
1280
1281           /* Now find all the moves between the points and modify them.  */
1282           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
1283             {
1284               if (broken_move (scan))
1285                 {
1286                   rtx pat = PATTERN (scan);
1287                   rtx src = SET_SRC (pat);
1288                   rtx dst = SET_DEST (pat);
1289                   enum machine_mode mode = GET_MODE (dst);
1290                   rtx lab;
1291                   rtx newinsn;
1292                   rtx newsrc;
1293
1294                   if (mode == SImode && hi_const (src))
1295                     {
1296                       int offset = 0;
1297
1298                       mode = HImode;
1299                       while (GET_CODE (dst) == SUBREG)
1300                         {
1301                           offset += SUBREG_WORD (dst);
1302                           dst = SUBREG_REG (dst);
1303                         }
1304                       dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
1305                     }
1306
1307                   lab = add_constant (src, mode);
1308                   newsrc = gen_rtx (MEM, mode,
1309                                     gen_rtx (LABEL_REF, VOIDmode, lab));
1310                   RTX_UNCHANGING_P (newsrc) = 1;
1311                   newinsn = emit_insn_after (gen_rtx (SET, VOIDmode,
1312                                                       dst, newsrc), scan);
1313
1314                   delete_insn (scan);
1315                   scan = newinsn;
1316                 }
1317             }
1318           dump_table (barrier);
1319         }
1320     }
1321 }
1322
1323 /* Dump out instruction addresses, which is useful for debugging the
1324    constant pool table stuff.  */
1325
1326 /* ??? This is unnecessary, and probably should be deleted.  This makes
1327    the insn_addresses declaration above unnecessary.  */
1328
1329 /* ??? The addresses printed by this routine for insns are nonsense for
1330    insns which are inside of a sequence where none of the inner insns have
1331    variable length.  This is because the second pass of shorten_branches
1332    does not bother to update them.  */
1333
1334 void
1335 final_prescan_insn (insn, opvec, noperands)
1336      rtx insn;
1337      rtx *opvec;
1338      int noperands;
1339 {
1340   if (TARGET_DUMPISIZE)
1341     fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
1342 }
1343
1344 /* Dump out any constants accumulated in the final pass.  These will
1345    will only be labels.  */
1346
1347 char *
1348 output_jump_label_table ()
1349 {
1350   int i;
1351
1352   if (pool_size)
1353     {
1354       fprintf (asm_out_file, "\t.align 2\n");
1355       for (i = 0; i < pool_size; i++)
1356         {
1357           pool_node *p = &pool_vector[i];
1358
1359           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1360                                      CODE_LABEL_NUMBER (p->label));
1361           output_asm_insn (".long       %O0", &p->value);
1362         }
1363       pool_size = 0;
1364     }
1365
1366   return "";
1367 }
1368 \f
1369 /* A full frame looks like:
1370
1371    arg-5
1372    arg-4
1373    [ if current_function_anonymous_args
1374    arg-3
1375    arg-2
1376    arg-1
1377    arg-0 ]
1378    saved-fp
1379    saved-r10
1380    saved-r11
1381    saved-r12
1382    saved-pr
1383    local-n
1384    ..
1385    local-1
1386    local-0        <- fp points here.  */
1387
1388 /* Number of bytes pushed for anonymous args, used to pass information
1389    between expand_prologue and expand_epilogue.  */
1390
1391 static int extra_push;
1392
1393 /* Adjust the stack and return the number of bytes taken to do it.  */
1394
1395 static void
1396 output_stack_adjust (size)
1397      int size;
1398 {
1399   if (size)
1400     {
1401       rtx val = GEN_INT (size);
1402       rtx insn;
1403
1404       if (! CONST_OK_FOR_I (size))
1405         {
1406           rtx reg = gen_rtx (REG, SImode, 3);
1407           emit_insn (gen_movsi (reg, val));
1408           val = reg;
1409         }
1410
1411       insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
1412       emit_insn (insn);
1413     }
1414 }
1415
1416 /* Output RTL to push register RN onto the stack.  */
1417
1418 static void
1419 push (rn)
1420      int rn;
1421 {
1422   rtx x;
1423   x = emit_insn (gen_push (gen_rtx (REG, SImode, rn)));
1424   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1425                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1426 }
1427
1428 /* Output RTL to pop register RN from the stack.  */
1429
1430 static void
1431 pop (rn)
1432      int rn;
1433 {
1434   rtx x;
1435   x = emit_insn (gen_pop (gen_rtx (REG, SImode, rn)));
1436   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1437                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1438 }
1439
1440 /* Generate code to push the regs specified in the mask, and return
1441    the number of bytes the insns take.  */
1442
1443 static void
1444 push_regs (mask)
1445      int mask;
1446 {
1447   int i;
1448
1449   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1450     if (mask & (1 << i))
1451       push (i);
1452 }
1453
1454 /* Work out the registers which need to be saved, both as a mask and a
1455    count.
1456
1457    If doing a pragma interrupt function, then push all regs used by the
1458    function, and if we call another function (we can tell by looking at PR),
1459    make sure that all the regs it clobbers are safe too.  */
1460
1461 static int
1462 calc_live_regs (count_ptr)
1463      int *count_ptr;
1464 {
1465   int reg;
1466   int live_regs_mask = 0;
1467   int count = 0;
1468
1469   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1470     {
1471       if (pragma_interrupt && ! pragma_trapa)
1472         {
1473           /* Need to save all the regs ever live.  */
1474           if ((regs_ever_live[reg]
1475                || (call_used_regs[reg] && regs_ever_live[PR_REG]))
1476               && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
1477               && reg != T_REG && reg != GBR_REG)
1478             {
1479               live_regs_mask |= 1 << reg;
1480               count++;
1481             }
1482         }
1483       else
1484         {
1485           /* Only push those regs which are used and need to be saved.  */
1486           if (regs_ever_live[reg] && ! call_used_regs[reg])
1487             {
1488               live_regs_mask |= (1 << reg);
1489               count++;
1490             }
1491         }
1492     }
1493
1494   *count_ptr = count;
1495   return live_regs_mask;
1496 }
1497
1498 /* Code to generate prologue and epilogue sequences */
1499
1500 void
1501 sh_expand_prologue ()
1502 {
1503   int live_regs_mask;
1504   int d, i;
1505   live_regs_mask = calc_live_regs (&d);
1506
1507   /* We have pretend args if we had an object sent partially in registers
1508      and partially on the stack, e.g. a large structure.  */
1509   output_stack_adjust (-current_function_pretend_args_size);
1510
1511   extra_push = 0;
1512
1513   /* This is set by SETUP_VARARGS to indicate that this is a varargs
1514      routine.  Clear it here so that the next function isn't affected.  */
1515   if (current_function_anonymous_args)
1516     {
1517       current_function_anonymous_args = 0;
1518
1519       /* Push arg regs as if they'd been provided by caller in stack.  */
1520       for (i = 0; i < NPARM_REGS; i++)
1521         {
1522           int rn = NPARM_REGS + FIRST_PARM_REG - i - 1;
1523           if (i > NPARM_REGS - current_function_args_info)
1524             break;
1525           push (rn);
1526           extra_push += 4;
1527         }
1528     }
1529   push_regs (live_regs_mask);
1530   output_stack_adjust (-get_frame_size ());
1531
1532   if (frame_pointer_needed)
1533     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
1534 }
1535
1536 void
1537 sh_expand_epilogue ()
1538 {
1539   int live_regs_mask;
1540   int d, i;
1541
1542   live_regs_mask = calc_live_regs (&d);
1543
1544   if (frame_pointer_needed)
1545     emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
1546
1547   output_stack_adjust (get_frame_size ());
1548
1549   /* Pop all the registers.  */
1550
1551   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1552     {
1553       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
1554       if (live_regs_mask & (1 << j))
1555         pop (j);
1556     }
1557
1558   output_stack_adjust (extra_push + current_function_pretend_args_size);
1559 }
1560
1561 /* Clear variables at function end.  */
1562
1563 void
1564 function_epilogue (stream, size)
1565      FILE *stream;
1566      int size;
1567 {
1568   pragma_interrupt = pragma_trapa = 0;
1569 }
1570
1571 /* Define the offset between two registers, one to be eliminated, and
1572    the other its replacement, at the start of a routine.  */
1573
1574 int
1575 initial_elimination_offset (from, to)
1576      int from;
1577      int to;
1578 {
1579   int regs_saved;
1580   int total_saved_regs_space;
1581   int total_auto_space = get_frame_size ();
1582
1583   calc_live_regs (&regs_saved);
1584   total_saved_regs_space = (regs_saved) * 4;
1585
1586   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1587     return total_saved_regs_space + total_auto_space;
1588
1589   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1590     return total_saved_regs_space + total_auto_space;
1591
1592   /* Initial gap between fp and sp is 0.  */
1593   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1594     return 0;
1595
1596   abort ();
1597 }
1598 \f
1599 /* Handle machine specific pragmas to be semi-compatible with Hitachi
1600    compiler.  */
1601
1602 int
1603 handle_pragma (file)
1604      FILE *file;
1605 {
1606   int c;
1607   char pbuf[200];
1608   int psize = 0;
1609
1610   c = getc (file);
1611   while (c == ' ' || c == '\t')
1612     c = getc (file);
1613
1614   if (c == '\n' || c == EOF)
1615     return c;
1616
1617   while (psize < sizeof (pbuf) - 1 && c != '\n')
1618     {
1619       pbuf[psize++] = c;
1620       if (psize == 9 && strncmp (pbuf, "interrupt", 9) == 0)
1621         {
1622           pragma_interrupt = 1;
1623           return ' ';
1624         }
1625       if (psize == 5 && strncmp (pbuf, "trapa", 5) == 0)
1626         {
1627           pragma_interrupt = pragma_trapa = 1;
1628           return ' ';
1629         }
1630       c = getc (file);
1631     }
1632   return c;
1633 }
1634 \f
1635 /* Predicates used by the templates.  */
1636
1637 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
1638    Used only in general_movsrc_operand.  */
1639
1640 int
1641 system_reg_operand (op, mode)
1642      rtx op;
1643      enum machine_mode mode;
1644 {
1645   switch (REGNO (op))
1646     {
1647     case PR_REG:
1648     case MACL_REG:
1649     case MACH_REG:
1650       return 1;
1651     }
1652   return 0;
1653 }
1654
1655 /* Returns 1 if OP can be source of a simple move operation.
1656    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
1657    invalid as are subregs of system registers.  */
1658
1659 int
1660 general_movsrc_operand (op, mode)
1661      rtx op;
1662      enum machine_mode mode;
1663 {
1664   if (GET_CODE (op) == MEM)
1665     {
1666       rtx inside = XEXP (op, 0);
1667       if (GET_CODE (inside) == CONST)
1668         inside = XEXP (inside, 0);
1669
1670       if (GET_CODE (inside) == LABEL_REF)
1671         return 1;
1672
1673       if (GET_CODE (inside) == PLUS
1674           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
1675           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
1676         return 1;
1677
1678       /* Only post inc allowed.  */
1679       if (GET_CODE (inside) == PRE_DEC)
1680         return 0;
1681     }
1682
1683   if ((mode == QImode || mode == HImode)
1684       && (GET_CODE (op) == SUBREG
1685           && GET_CODE (XEXP (op, 0)) == REG
1686           && system_reg_operand (XEXP (op, 0), mode)))
1687     return 0;
1688
1689   return general_operand (op, mode);
1690 }
1691
1692 /* Returns 1 if OP can be a destination of a move.
1693    Same as general_operand, but no preinc allowed.  */
1694
1695 int
1696 general_movdst_operand (op, mode)
1697      rtx op;
1698      enum machine_mode mode;
1699 {
1700   /* Only pre dec allowed.  */
1701   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
1702     return 0;
1703
1704   return general_operand (op, mode);
1705 }
1706
1707 /* Returns 1 if OP is a normal arithmetic register.  */
1708
1709 int
1710 arith_reg_operand (op, mode)
1711      rtx op;
1712      enum machine_mode mode;
1713 {
1714   if (register_operand (op, mode))
1715     {
1716       if (GET_CODE (op) == REG)
1717         return (REGNO (op) != T_REG
1718                 && REGNO (op) != PR_REG
1719                 && REGNO (op) != MACH_REG
1720                 && REGNO (op) != MACL_REG);
1721       return 1;
1722     }
1723   return 0;
1724 }
1725
1726 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
1727
1728 int
1729 arith_operand (op, mode)
1730      rtx op;
1731      enum machine_mode mode;
1732 {
1733   if (arith_reg_operand (op, mode))
1734     return 1;
1735
1736   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
1737     return 1;
1738
1739   return 0;
1740 }
1741
1742 /* Returns 1 if OP is a valid source operand for a compare insn.  */
1743
1744 int
1745 arith_reg_or_0_operand (op, mode)
1746      rtx op;
1747      enum machine_mode mode;
1748 {
1749   if (arith_reg_operand (op, mode))
1750     return 1;
1751
1752   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
1753     return 1;
1754
1755   return 0;
1756 }
1757
1758 /* Returns 1 if OP is a valid source operand for a logical operation.  */
1759
1760 int
1761 logical_operand (op, mode)
1762      rtx op;
1763      enum machine_mode mode;
1764 {
1765   if (arith_reg_operand (op, mode))
1766     return 1;
1767
1768   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
1769     return 1;
1770
1771   return 0;
1772 }
1773 \f
1774 /* Determine where to put an argument to a function.
1775    Value is zero to push the argument on the stack,
1776    or a hard register in which to store the argument.
1777
1778    MODE is the argument's machine mode.
1779    TYPE is the data type of the argument (as a tree).
1780     This is null for libcalls where that information may
1781     not be available.
1782    CUM is a variable of type CUMULATIVE_ARGS which gives info about
1783     the preceding args and about the function being called.
1784    NAMED is nonzero if this argument is a named parameter
1785     (otherwise it is an extra parameter matching an ellipsis).  */
1786
1787 rtx
1788 sh_function_arg (cum, mode, type, named)
1789      CUMULATIVE_ARGS cum;
1790      enum machine_mode mode;
1791      tree type;
1792      int named;
1793 {
1794   if (named)
1795     {
1796       int rr = (ROUND_REG (cum, mode));
1797
1798       if (rr < NPARM_REGS)
1799         return ((type == 0 || ! TREE_ADDRESSABLE (type))
1800                 ? gen_rtx (REG, mode, FIRST_PARM_REG + rr) : 0);
1801     }
1802   return 0;
1803 }
1804
1805 /* For an arg passed partly in registers and partly in memory,
1806    this is the number of registers used.
1807    For args passed entirely in registers or entirely in memory, zero.
1808    Any arg that starts in the first 4 regs but won't entirely fit in them
1809    needs partial registers on the SH.  */
1810
1811 int
1812 sh_function_arg_partial_nregs (cum, mode, type, named)
1813      CUMULATIVE_ARGS cum;
1814      enum machine_mode mode;
1815      tree type;
1816      int named;
1817 {
1818   if (cum < NPARM_REGS)
1819     {
1820       if ((type == 0 || ! TREE_ADDRESSABLE (type))
1821           && (cum + (mode == BLKmode
1822                      ? ROUND_ADVANCE (int_size_in_bytes (type))
1823                      : ROUND_ADVANCE (GET_MODE_SIZE (mode))) - NPARM_REGS > 0))
1824         return NPARM_REGS - cum;
1825     }
1826   return 0;
1827 }