OSDN Git Service

5d2f2b422f9f73c734e432aa73398f181cf095c2
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.c
1 /* Subroutines for insn-output.c for Hitachi H8/300.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3    Contributed by Steve Chamberlain (sac@cygnus.com),
4    Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include <stdio.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
39 /* Forward declarations.  */
40 void print_operand_address ();
41 char *index ();
42
43 /* CPU_TYPE, says what cpu we're compiling for.  */
44 int cpu_type;
45
46 /* True if a #pragma interrupt has been seen for the current function.  */
47 int pragma_interrupt;
48
49 /* True if a #pragma saveall has been seen for the current function.  */
50 int pragma_saveall;
51
52 static char *names_big[] =
53 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7"};
54
55 static char *names_extended[] =
56 {"er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"};
57
58 static char *names_upper_extended[] =
59 {"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"};
60
61 /* Points to one of the above.  */
62 /* ??? The above could be put in an array indexed by CPU_TYPE.  */
63 char **h8_reg_names;
64
65 /* Various operations needed by the following, indexed by CPU_TYPE.  */
66 /* ??? The h8/300 assembler doesn't understand pop.w (yet).  */
67
68 static char *h8_push_ops[2] =
69 {"push", "push.l"};
70 static char *h8_pop_ops[2] =
71 {"pop", "pop.l"};
72 static char *h8_mov_ops[2] =
73 {"mov.w", "mov.l"};
74
75 char *h8_push_op, *h8_pop_op, *h8_mov_op;
76
77 /* Initialize various cpu specific globals at start up.  */
78
79 void
80 h8300_init_once ()
81 {
82   if (TARGET_H8300)
83     {
84       cpu_type = (int) CPU_H8300;
85       h8_reg_names = names_big;
86     }
87   else
88     {
89       cpu_type = (int) CPU_H8300H;
90       h8_reg_names = names_extended;
91     }
92   h8_push_op = h8_push_ops[cpu_type];
93   h8_pop_op = h8_pop_ops[cpu_type];
94   h8_mov_op = h8_mov_ops[cpu_type];
95 }
96
97 char *
98 byte_reg (x, b)
99      rtx x;
100      int b;
101 {
102   static char *names_small[] =
103   {"r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
104    "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7lBAD", "r7hBAD"};
105
106   return names_small[REGNO (x) * 2 + b];
107 }
108
109 /* REGNO must be saved/restored across calls if this macro is true.  */
110
111 #define WORD_REG_USED(regno)                                    \
112   (regno < 7 &&                                                 \
113    (pragma_interrupt                                            \
114     || pragma_saveall                                           \
115     || (regno == FRAME_POINTER_REGNUM && regs_ever_live[regno]) \
116     || (regs_ever_live[regno] & !call_used_regs[regno])))
117
118 /* Output assembly language to FILE for the operation OP with operand size
119    SIZE to adjust the stack pointer.  */
120 /* ??? FPED is currently unused.  */
121
122 static void
123 dosize (file, op, size, fped)
124      FILE *file;
125      char *op;
126      unsigned int size;
127      int fped;
128 {
129   switch (size)
130     {
131     case 4:
132       /* ??? TARGET_H8300H can do this in one insn.  */
133     case 3:
134       fprintf (file, "\t%ss\t#%d,sp\n", op, 2);
135       size -= 2;
136       /* Fall through...  */
137     case 2:
138     case 1:
139       fprintf (file, "\t%ss\t#%d,sp\n", op, size);
140       size = 0;
141       break;
142     case 0:
143       break;
144     default:
145       if (TARGET_H8300)
146         fprintf (file, "\tmov.w\t#%d,r3\n\t%s.w\tr3,sp\n", size, op);
147       else
148         fprintf (file, "\t%s\t#%d,sp\n", op, size);
149       size = 0;
150       break;
151     }
152 }
153
154 /* Output assembly language code for the function prologue.  */
155 static int push_order[FIRST_PSEUDO_REGISTER] =
156 {6, 5, 4, 3, 2, 1, 0, -1, -1};
157 static int pop_order[FIRST_PSEUDO_REGISTER] =
158 {0, 1, 2, 3, 4, 5, 6, -1, -1};
159
160 /* This is what the stack looks like after the prolog of 
161    a function with a frame has been set up:
162
163    <args>
164    PC
165    FP                   <- fp
166    <locals>
167    <saved registers>    <- sp
168
169    This is what the stack looks like after the prolog of
170    a function which doesn't have a frame:
171
172    <args>
173    PC
174    <locals>
175    <saved registers>    <- sp
176 */
177
178 int current_function_anonymous_args;
179
180 /* Extra arguments to pop, in words (IE: 2 bytes for 300, 4 for 300h */
181 static int extra_pop;
182
183 void
184 function_prologue (file, size)
185      FILE *file;
186      int size;
187 {
188   register int mask = 0;
189   int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
190   int idx;
191   extra_pop = 0;
192
193   if (current_function_anonymous_args && TARGET_QUICKCALL)
194     {
195       /* Push regs as if done by caller, and move around return address.  */
196
197       switch (current_function_args_info.nbytes / UNITS_PER_WORD)
198         {
199         case 0:
200           /* get ret addr */
201           fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
202           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
203           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[1]);
204           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[0]);
205           /* push it again */
206           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
207           extra_pop = 3;
208           break;
209         case 1:
210           /* get ret addr */
211           fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
212           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
213           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[1]);
214           /* push it again */
215           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
216           extra_pop = 2;
217           break;
218         case 2:
219           /* get ret addr */
220           fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
221           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
222           /* push it again */
223           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
224           extra_pop = 1;
225           break;
226         default:
227           fprintf (file, "; varargs\n");
228           break;
229         }
230     }
231
232   if (frame_pointer_needed)
233     {
234       /* Push fp */
235       fprintf (file, "\t%s\t%s\n", h8_push_op,
236                h8_reg_names[FRAME_POINTER_REGNUM]);
237       fprintf (file, "\t%s\t%s,%s\n", h8_mov_op,
238                h8_reg_names[STACK_POINTER_REGNUM],
239                h8_reg_names[FRAME_POINTER_REGNUM]);
240
241       /* leave room for locals */
242       dosize (file, "sub", fsize, 1);
243
244       /* Push the rest of the registers */
245       for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
246         {
247           int regno = push_order[idx];
248
249           if (regno >= 0 && WORD_REG_USED (regno) && regno != FRAME_POINTER_REGNUM)
250             fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[regno]);
251         }
252     }
253   else
254     {
255       dosize (file, "sub", fsize, 0);
256       for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
257         {
258           int regno = push_order[idx];
259
260           if (regno >= 0 && WORD_REG_USED (regno))
261             fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[regno]);
262         }
263     }
264 }
265
266 /* Output assembly language code for the function epilogue.  */
267
268 void
269 function_epilogue (file, size)
270      FILE *file;
271      int size;
272 {
273   register int regno;
274   register int mask = 0;
275   int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
276   int nregs;
277   int offset;
278   int idx;
279   rtx insn = get_last_insn ();
280
281   /* If the last insn was a BARRIER, we don't have to write any code.  */
282   if (GET_CODE (insn) == NOTE)
283     insn = prev_nonnote_insn (insn);
284   if (insn && GET_CODE (insn) == BARRIER)
285     return;
286
287   nregs = 0;
288
289   if (frame_pointer_needed)
290     {
291       /* Pop saved registers */
292       for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
293         {
294           regno = pop_order[idx];
295           if (regno >= 0 && regno != FRAME_POINTER_REGNUM && WORD_REG_USED (regno))
296             fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[regno]);
297         }
298       /* deallocate locals */
299       dosize (file, "add", fsize, 1);
300       /* pop frame pointer */
301       fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[FRAME_POINTER_REGNUM]);
302     }
303   else
304     {
305       /* pop saved registers */
306       for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
307         {
308           regno = pop_order[idx];
309           if (regno >= 0 && WORD_REG_USED (regno))
310             fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[regno]);
311         }
312       /* deallocate locals */
313       dosize (file, "add", fsize, 0);
314     }
315
316   if (extra_pop)
317     {
318       fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
319       while (extra_pop)
320         {
321           fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[2]);
322           extra_pop--;
323         }
324       fprintf (file, "\tjmp     @%s\n", h8_reg_names[3]);
325     }
326   else
327     {
328       if (pragma_interrupt)
329         fprintf (file, "\trte\n");
330       else
331         fprintf (file, "\trts\n");
332     }
333
334   pragma_interrupt = 0;
335   pragma_saveall = 0;
336
337   current_function_anonymous_args = 0;
338 }
339
340 /* Output assembly code for the start of the file.  */
341
342 asm_file_start (file)
343      FILE *file;
344 {
345   fprintf (file, ";\tGCC For the Hitachi H8/300\n");
346   fprintf (file, ";\tBy Hitachi America Ltd and Cygnus Support\n");
347   fprintf (file, ";\trelease F-1\n");
348   if (optimize)
349     fprintf (file, "; -O%d\n", optimize);
350   if (TARGET_H8300H)
351     fprintf (file, "\n\t.h8300h\n");
352   else
353     fprintf (file, "\n\n");
354   output_file_directive (file, main_input_filename);
355 }
356
357 /* Output assembly language code for the end of file.  */
358
359 void
360 asm_file_end (file)
361      FILE *file;
362 {
363   fprintf (file, "\t.end\n");
364 }
365 \f
366 /* Return true if VALUE is a valid constant for constraint 'P'.
367    IE: VALUE is a power of two <= 2**15.  */
368
369 int
370 small_power_of_two (value)
371      int value;
372 {
373   switch (value)
374     {
375     case 1:
376     case 2:
377     case 4:
378     case 8:
379     case 16:
380     case 32:
381     case 64:
382     case 128:
383     case 256:
384     case 512:
385     case 1024:
386     case 2048:
387     case 4096:
388     case 8192:
389     case 16384:
390     case 32768:
391       return 1;
392     }
393   return 0;
394 }
395
396 /* Return true if VALUE is a valid constant for constraint 'O', which
397    means that the constant would be ok to use as a bit for a bclr
398    instruction.  */
399
400 int
401 ok_for_bclr (value)
402      int value;
403 {
404   return small_power_of_two ((~value) & 0xff);
405 }
406
407 /* Return true is OP is a valid source operand for an integer move
408    instruction.  */
409
410 int
411 general_operand_src (op, mode)
412      rtx op;
413      enum machine_mode mode;
414 {
415   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
416     return 1;
417   return general_operand (op, mode);
418 }
419
420 /* Return true if OP is a valid destination operand for an integer move
421    instruction.  */
422
423 int
424 general_operand_dst (op, mode)
425      rtx op;
426      enum machine_mode mode;
427 {
428   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == PRE_DEC)
429     return 1;
430   return general_operand (op, mode);
431 }
432
433 /* Return true if OP is a const valid for a bit clear instruction.  */
434
435 int
436 o_operand (operand, mode)
437      rtx operand;
438      enum machine_mode mode;
439 {
440   return (GET_CODE (operand) == CONST_INT
441           && CONST_OK_FOR_O (INTVAL (operand)));
442 }
443
444 /* Return true if OP is a const valid for a bit set or bit xor instruction.  */
445
446 int
447 p_operand (operand, mode)
448      rtx operand;
449      enum machine_mode mode;
450 {
451   return (GET_CODE (operand) == CONST_INT
452           && CONST_OK_FOR_P (INTVAL (operand)));
453 }
454
455 /* Return true if OP is a valid call operand.  */
456
457 int
458 call_insn_operand (op, mode)
459      rtx op;
460      enum machine_mode mode;
461 {
462   if (GET_CODE (op) == MEM)
463     {
464       rtx inside = XEXP (op, 0);
465       if (register_operand (inside, Pmode))
466         return 1;
467       if (CONSTANT_ADDRESS_P (inside))
468         return 1;
469     }
470   return 0;
471 }
472
473 /* Return true if OP is a valid jump operand.  */
474
475 int
476 jump_address_operand (op, mode)
477      rtx op;
478      enum machine_mode mode;
479 {
480   if (GET_CODE (op) == REG)
481     return mode == Pmode;
482
483   if (GET_CODE (op) == MEM)
484     {
485       rtx inside = XEXP (op, 0);
486       if (register_operand (inside, Pmode))
487         return 1;
488       if (CONSTANT_ADDRESS_P (inside))
489         return 1;
490     }
491   return 0;
492 }
493
494 /* Recognize valid operands for bitfield instructions.  */
495
496 extern int rtx_equal_function_value_matters;
497
498 int
499 bit_operand (op, mode)
500      rtx op;
501      enum machine_mode mode;
502 {
503   /* We can except any general operand, expept that MEM operands must
504      be limited to those that use addresses valid for the 'U' constraint.  */
505   if (!general_operand (op, mode))
506     return 0;
507
508   /* Accept any mem during RTL generation.  Otherwise, the code that does
509      insv and extzv will think that we can not handle memory.  However,
510      to avoid reload problems, we only accept 'U' MEM operands after RTL
511      generation.  This means that any named pattern which uses this predicate
512      must force its operands to match 'U' before emitting RTL.  */
513
514   if (GET_CODE (op) == REG)
515     return 1;
516   if (GET_CODE (op) == SUBREG)
517     return 1;
518   if (!rtx_equal_function_value_matters)
519     {
520       /* We're building rtl */
521       return GET_CODE (op) == MEM;
522     }
523   else
524     {
525       return (GET_CODE (op) == MEM
526               && EXTRA_CONSTRAINT (op, 'U'));
527     }
528 }
529
530 /* Recognize valid operators for bit test.  */
531
532 int
533 eq_operator (x, mode)
534      rtx x;
535      enum machine_mode mode;
536 {
537   return (GET_CODE (x) == EQ || GET_CODE (x) == NE);
538 }
539
540 /* Handle machine specific pragmas for compatibility with existing
541    compilers for the H8/300.
542
543    pragma saveall generates prolog/epilog code which saves and
544    restores all the registers on function entry.
545
546    pragma interrupt saves and restores all registers, and exits with
547    an rte instruction rather than an rts.  A pointer to a function
548    with this attribute may be safely used in an interrupt vector.  */
549
550 int
551 handle_pragma (file, c)
552      FILE *file;
553      int c;
554 {
555   char pbuf[20];
556   int psize = 0;
557
558   while (c == ' ' || c == '\t')
559     c = getc (file);
560
561   if (c != '\n' & c != EOF)
562     {
563       while (psize < sizeof (pbuf) - 1
564              && isalpha (c))
565         {
566           pbuf[psize++] = c;
567           c = getc (file);
568         }
569       pbuf[psize] = 0;
570
571       if (strcmp (pbuf, "interrupt") == 0)
572         pragma_interrupt = 1;
573       else if (strcmp (pbuf, "saveall") == 0)
574         pragma_saveall = 1;
575
576       while (c != '\n' && c != EOF)
577         c = getc (file);
578     }
579
580   return c;
581 }
582 \f
583 /* If the next arg with MODE and TYPE is to be passed in a register, return
584    the rtx to represent where it is passed.  CUM represents the state after
585    the last argument.  NAMED is not used.  */
586
587 static char *hand_list[] =
588 {
589   "__main",
590   "__cmpsi2",
591   "__divhi3",
592   "__modhi3",
593   "__udivhi3",
594   "__umodhi3",
595   "__divsi3",
596   "__modsi3",
597   "__udivsi3",
598   "__umodsi3",
599   "__mulhi3",
600   "__mulsi3",
601   "__reg_memcpy",
602   "__reg_memset",
603   "__ucmpsi2",
604   0,
605 };
606
607 /* Return an RTX to represent where a value with mode MODE will be returned
608    from a function.  If the result is 0, the argument is pushed.  */
609
610 rtx
611 function_arg (cum, mode, type, named)
612      CUMULATIVE_ARGS *cum;
613      enum machine_mode mode;
614      tree type;
615      int named;
616 {
617   rtx result = 0;
618   char *fname;
619   int regpass = 0;
620
621   /* Pass 3 regs worth of data in regs when user asked on the command line.  */
622   if (TARGET_QUICKCALL)
623     regpass = 3;
624
625   /* If calling hand written assembler, use 4 regs of args.  */
626
627   if (cum->libcall)
628     {
629       char **p;
630
631       fname = XSTR (cum->libcall, 0);
632
633       /* See if this libcall is one of the hand coded ones.  */
634
635       for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
636         ;
637
638       if (*p)
639         regpass = 4;
640     }
641
642   if (regpass)
643     {
644       int size;
645
646       if (mode == BLKmode)
647         size = int_size_in_bytes (type);
648       else
649         size = GET_MODE_SIZE (mode);
650
651       if (size + cum->nbytes > regpass * UNITS_PER_WORD)
652         {
653           result = 0;
654         }
655       else
656         {
657           switch (cum->nbytes / UNITS_PER_WORD)
658             {
659             case 0:
660               result = gen_rtx (REG, mode, 0);
661               break;
662             case 1:
663               result = gen_rtx (REG, mode, 1);
664               break;
665             case 2:
666               result = gen_rtx (REG, mode, 2);
667               break;
668             case 3:
669               result = gen_rtx (REG, mode, 3);
670               break;
671             default:
672               result = 0;
673             }
674         }
675     }
676
677   return result;
678 }
679 \f
680 /* Return the cost of the rtx R with code CODE.  */
681
682 int
683 const_costs (r, c)
684      rtx r;
685      enum rtx_code c;
686 {
687   switch (c)
688     {
689     case CONST_INT:
690       switch (INTVAL (r))
691         {
692         case 0:
693         case 1:
694         case 2:
695         case -1:
696         case -2:
697           return 0;
698         default:
699           return 1;
700         }
701
702     case CONST:
703     case LABEL_REF:
704     case SYMBOL_REF:
705       return 3;
706
707     case CONST_DOUBLE:
708       return 20;
709
710     default:
711       return 4;
712     }
713 }
714 \f
715 /* Documentation for the machine specific operand escapes:
716
717    'A' print rn in h8/300 mode, erN in H8/300H mode
718    'C' print (operand - 2).
719    'E' like s but negative.
720    'F' like t but negative.
721    'G' constant just the negative
722    'L' fake label, changed after used twice.
723    'M' turn a 'M' constant into its negative mod 2.
724    'P' if operand is incing/decing sp, print .w, otherwise .b.
725    'S' print operand as a long word
726    'T' print operand as a word
727    'U' if operand is incing/decing sp, print l, otherwise nothing.
728    'V' find the set bit, and print its number.
729    'W' find the clear bit, and print its number.
730    'X' print operand as a byte
731    'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
732    'Z' print int & 7.
733    'b' print the bit opcode
734    'c' print the ibit opcode
735    'd' bcc if EQ, bcs if NE
736    'e' first word of 32 bit value - if reg, then least reg. if mem
737        then least. if const then most sig word
738    'f' second word of 32 bit value - if reg, then biggest reg. if mem
739        then +2. if const then least sig word
740    'g' bcs if EQ, bcc if NE
741    'j' print operand as condition code.
742    'k' print operand as reverse condition code.
743    's' print as low byte of 16 bit value
744    't' print as high byte of 16 bit value
745    'w' print as low byte of 32 bit value
746    'x' print as 2nd byte of 32 bit value
747    'y' print as 3rd byte of 32 bit value
748    'z' print as msb of 32 bit value
749 */
750
751 /* Return assembly language string which identifies a comparison type.  */
752
753 static char *
754 cond_string (code)
755      enum rtx_code code;
756 {
757   switch (code)
758     {
759     case NE:
760       if (cc_prev_status.flags & CC_DONE_CBIT)
761         return "cs";
762       return "ne";
763     case EQ:
764       if (cc_prev_status.flags & CC_DONE_CBIT)
765         return "cc";
766       return "eq";
767     case GE:
768       return "ge";
769     case GT:
770       return "gt";
771     case LE:
772       return "le";
773     case LT:
774       return "lt";
775     case GEU:
776       return "hs";
777     case GTU:
778       return "hi";
779     case LEU:
780       return "ls";
781     case LTU:
782       return "lo";
783     default:
784       abort ();
785     }
786 }
787
788 /* Print operand X using operand code CODE to assembly language output file
789    FILE.  */
790
791 void
792 print_operand (file, x, code)
793      FILE *file;
794      rtx x;
795      int code;
796 {
797   /* This is used to general unique labels for the 'L' code.  */
798   static int lab = 1000;
799
800   /* This is used for communication between the 'P' and 'U' codes.  */
801   static char *last_p;
802
803   /* This is used for communication between the 'Z' and 'Y' codes.  */
804   /* ??? 'V' and 'W' use it too.  */
805   static int bitint;
806
807   switch (code)
808     {
809     case 'A':
810       if (GET_CODE (x) == REG)
811         fprintf (file, "%s", h8_reg_names[REGNO (x)]);
812       else
813         goto def;
814       break;
815     case 'C':
816       fprintf (file, "#%d", INTVAL (x) - 2);
817       break;
818     case 'E':
819       switch (GET_CODE (x))
820         {
821         case REG:
822           fprintf (file, "%sl", names_big[REGNO (x)]);
823           break;
824         case CONST_INT:
825           fprintf (file, "#%d", (-INTVAL (x)) & 0xff);
826           break;
827         default:
828           abort ();
829         }
830       break;
831     case 'F':
832       switch (GET_CODE (x))
833         {
834         case REG:
835           fprintf (file, "%sh", names_big[REGNO (x)]);
836           break;
837         case CONST_INT:
838           fprintf (file, "#%d", ((-INTVAL (x)) & 0xff00) >> 8);
839           break;
840         default:
841           abort ();
842         }
843       break;
844     case 'G':
845       if (GET_CODE (x) != CONST_INT)
846         abort ();
847       fprintf (file, "#%d", 0xff & (-INTVAL (x)));
848       break;
849     case 'L':
850       /* 'L' must always be used twice in a single pattern.  It generates
851          the same label twice, and then will generate a unique label the
852          next time it is used.  */
853       asm_fprintf (file, "tl%d", (lab++) / 2);
854       break;
855     case 'M':
856       /* For 3/-3 and 4/-4, the other 2 is handled separately.  */
857       switch (INTVAL (x))
858         {
859         case 2:
860         case 4:
861         case -2:
862         case -4:
863           fprintf (file, "#2");
864           break;
865         case 1:
866         case 3:
867         case -1:
868         case -3:
869           fprintf (file, "#1");
870           break;
871         default:
872           abort ();
873         }
874       break;
875     case 'P':
876       if (REGNO (XEXP (XEXP (x, 0), 0)) == STACK_POINTER_REGNUM)
877         {
878           last_p = "";
879           fprintf (file, ".w");
880         }
881       else
882         {
883           last_p = "l";
884           fprintf (file, ".b");
885         }
886       break;
887     case 'S':
888       if (GET_CODE (x) == REG)
889         fprintf (file, "%s", names_extended[REGNO (x)]);
890       else
891         goto def;
892       break;
893     case 'T':
894       if (GET_CODE (x) == REG)
895         fprintf (file, "%s", names_big[REGNO (x)]);
896       else
897         goto def;
898       break;
899     case 'U':
900       fprintf (file, "%s%s", names_big[REGNO (x)], last_p);
901       break;
902     case 'V':
903       bitint = exact_log2 (INTVAL (x));
904       if (bitint == -1)
905         abort ();
906       fprintf (file, "#%d", bitint & 7);
907       break;
908     case 'W':
909       bitint = exact_log2 ((~INTVAL (x)) & 0xff);
910       if (bitint == -1)
911         abort ();
912       fprintf (file, "#%d", bitint & 7);
913       break;
914     case 'X':
915       if (GET_CODE (x) == REG)
916         fprintf (file, "%s", byte_reg (x, 0));
917       else
918         goto def;
919       break;
920     case 'Y':
921       if (bitint == -1)
922         abort ();
923       if (GET_CODE (x) == REG)
924         fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
925       else
926         print_operand (file, x, 0);
927       bitint = -1;
928       break;
929     case 'Z':
930       bitint = INTVAL (x);
931       fprintf (file, "#%d", bitint & 7);
932       break;
933     case 'b':
934       switch (GET_CODE (x))
935         {
936         case IOR:
937           fprintf (file, "bor");
938           break;
939         case XOR:
940           fprintf (file, "bxor");
941           break;
942         case AND:
943           fprintf (file, "band");
944           break;
945         }
946       break;
947     case 'c':
948       switch (GET_CODE (x))
949         {
950         case IOR:
951           fprintf (file, "bior");
952           break;
953         case XOR:
954           fprintf (file, "bixor");
955           break;
956         case AND:
957           fprintf (file, "biand");
958           break;
959         }
960       break;
961     case 'd':
962       switch (GET_CODE (x))
963         {
964         case EQ:
965           fprintf (file, "bcc");
966           break;
967         case NE:
968           fprintf (file, "bcs");
969           break;
970         default:
971           abort ();
972         }
973       break;
974     case 'e':
975       switch (GET_CODE (x))
976         {
977         case REG:
978           if (TARGET_H8300)
979             fprintf (file, "%s", names_big[REGNO (x)]);
980           else
981             fprintf (file, "%s", names_upper_extended[REGNO (x)]);
982           break;
983         case MEM:
984           x = adj_offsettable_operand (x, 0);
985           print_operand (file, x, 0);
986           break;
987         case CONST_INT:
988           fprintf (file, "#%d", ((INTVAL (x) >> 16) & 0xffff));
989           break;
990         default:
991           abort ();
992           break;
993         }
994       break;
995     case 'f':
996       switch (GET_CODE (x))
997         {
998         case REG:
999           if (TARGET_H8300)
1000             fprintf (file, "%s", names_big[REGNO (x) + 1]);
1001           else
1002             fprintf (file, "%s", names_big[REGNO (x)]);
1003           break;
1004         case MEM:
1005           x = adj_offsettable_operand (x, 2);
1006           print_operand (file, x, 0);
1007           break;
1008         case CONST_INT:
1009           fprintf (file, "#%d", INTVAL (x) & 0xffff);
1010           break;
1011         default:
1012           abort ();
1013         }
1014       break;
1015     case 'g':
1016       switch (GET_CODE (x))
1017         {
1018         case NE:
1019           fprintf (file, "bcc");
1020           break;
1021         case EQ:
1022           fprintf (file, "bcs");
1023           break;
1024         default:
1025           abort ();
1026         }
1027       break;
1028     case 'j':
1029       asm_fprintf (file, cond_string (GET_CODE (x)));
1030       break;
1031     case 'k':
1032       asm_fprintf (file, cond_string (reverse_condition (GET_CODE (x))));
1033       break;
1034     case 's':
1035       if (GET_CODE (x) == CONST_INT)
1036         fprintf (file, "#%d", (INTVAL (x)) & 0xff);
1037       else
1038         fprintf (file, "%s", byte_reg (x, 0));
1039       break;
1040     case 't':
1041       if (GET_CODE (x) == CONST_INT)
1042         fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1043       else
1044         fprintf (file, "%s", byte_reg (x, 1));
1045       break;
1046     case 'u':
1047       if (GET_CODE (x) != CONST_INT)
1048         abort ();
1049       fprintf (file, "%d", INTVAL (x));
1050       break;
1051     case 'w':
1052       if (GET_CODE (x) == CONST_INT)
1053         fprintf (file, "#%d", INTVAL (x) & 0xff);
1054       else
1055         fprintf (file, "%s", byte_reg (x, TARGET_H8300 ? 2 : 0));
1056       break;
1057     case 'x':
1058       if (GET_CODE (x) == CONST_INT)
1059         fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1060       else
1061         fprintf (file, "%s", byte_reg (x, TARGET_H8300 ? 3 : 1));
1062       break;
1063     case 'y':
1064       if (GET_CODE (x) == CONST_INT)
1065         fprintf (file, "#%d", (INTVAL (x) >> 16) & 0xff);
1066       else
1067         fprintf (file, "%s", byte_reg (x, 0));
1068       break;
1069     case 'z':
1070       if (GET_CODE (x) == CONST_INT)
1071         fprintf (file, "#%d", (INTVAL (x) >> 24) & 0xff);
1072       else
1073         fprintf (file, "%s", byte_reg (x, 1));
1074       break;
1075
1076     default:
1077     def:
1078       switch (GET_CODE (x))
1079         {
1080         case REG:
1081           switch (GET_MODE (x))
1082             {
1083             case QImode:
1084 #if 0                           /* Is it asm ("mov.b %0,r2l", ...) */
1085               fprintf (file, "%s", byte_reg (x, 0));
1086 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1087               fprintf (file, "%s", names_big[REGNO (x)]);
1088 #endif
1089               break;
1090             case HImode:
1091               fprintf (file, "%s", names_big[REGNO (x)]);
1092               break;
1093             case SImode:
1094             case SFmode:
1095               fprintf (file, "%s", names_extended[REGNO (x)]);
1096               break;
1097             default:
1098               abort ();
1099             }
1100           break;
1101
1102         case MEM:
1103           fprintf (file, "@");
1104           output_address (XEXP (x, 0));
1105           break;
1106
1107         case CONST_INT:
1108         case SYMBOL_REF:
1109         case CONST:
1110         case LABEL_REF:
1111           fprintf (file, "#");
1112           print_operand_address (file, x);
1113           break;
1114         }
1115     }
1116 }
1117
1118 /* Output assembly language output for the address ADDR to FILE.  */
1119
1120 void
1121 print_operand_address (file, addr)
1122      FILE *file;
1123      rtx addr;
1124 {
1125   switch (GET_CODE (addr))
1126     {
1127     case REG:
1128       fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1129       break;
1130
1131     case PRE_DEC:
1132       fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1133       break;
1134
1135     case POST_INC:
1136       fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1137       break;
1138
1139     case PLUS:
1140       fprintf (file, "(");
1141       if (GET_CODE (XEXP (addr, 0)) == REG)
1142         {
1143           /* reg,foo */
1144           print_operand_address (file, XEXP (addr, 1));
1145           fprintf (file, ",");
1146           print_operand_address (file, XEXP (addr, 0));
1147         }
1148       else
1149         {
1150           /* foo+k */
1151           print_operand_address (file, XEXP (addr, 0));
1152           fprintf (file, "+");
1153           print_operand_address (file, XEXP (addr, 1));
1154         }
1155       fprintf (file, ")");
1156       break;
1157
1158     case CONST_INT:
1159       {
1160         /* Since the h8/300 only has 16 bit pointers, negative values are also
1161            those >= 32768.  This happens for example with pointer minus a
1162            constant.  We don't want to turn (char *p - 2) into
1163            (char *p + 65534) because loop unrolling can build upon this
1164            (IE: char *p + 131068).  */
1165         int n = INTVAL (addr);
1166         if (TARGET_H8300)
1167           n = (int) (short) n;
1168         if (n < 0)
1169           /* ??? Why the special case for -ve values? */
1170           fprintf (file, "-%d", -n);
1171         else
1172           fprintf (file, "%d", n);
1173         break;
1174       }
1175
1176     default:
1177       output_addr_const (file, addr);
1178       break;
1179     }
1180 }
1181 \f
1182 /* Output all insn addresses and their sizes into the assembly language
1183    output file.  This is helpful for debugging whether the length attributes
1184    in the md file are correct.  This is not meant to be a user selectable
1185    option.  */
1186
1187 void
1188 final_prescan_insn (insn, operand, num_operands)
1189      rtx insn, *operand;
1190      int num_operands;
1191 {
1192   /* This holds the last insn address.  */
1193   static int last_insn_address = 0;
1194
1195   int uid = INSN_UID (insn);
1196
1197   if (TARGET_RTL_DUMP)
1198     {
1199       fprintf (asm_out_file, "\n****************");
1200       print_rtl (asm_out_file, PATTERN (insn));
1201       fprintf (asm_out_file, "\n");
1202     }
1203
1204   if (TARGET_ADDRESSES)
1205     {
1206       fprintf (asm_out_file, "; 0x%x %d\n", insn_addresses[uid],
1207                insn_addresses[uid] - last_insn_address);
1208       last_insn_address = insn_addresses[uid];
1209     }
1210 }
1211
1212 /* Prepare for an SI sized move.  */
1213
1214 int
1215 do_movsi (operands)
1216      rtx operands[];
1217 {
1218   rtx src = operands[1];
1219   rtx dst = operands[0];
1220   if (!reload_in_progress && !reload_completed)
1221     {
1222       if (!register_operand (dst, GET_MODE (dst)))
1223         {
1224           rtx tmp = gen_reg_rtx (GET_MODE (dst));
1225           emit_move_insn (tmp, src);
1226           operands[1] = tmp;
1227         }
1228     }
1229   return 0;
1230 }
1231
1232 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1233    Define the offset between two registers, one to be eliminated, and the other
1234    its replacement, at the start of a routine.  */
1235
1236 int
1237 initial_offset (from, to)
1238 {
1239   int offset = 0;
1240
1241   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1242     offset = UNITS_PER_WORD + frame_pointer_needed * UNITS_PER_WORD;
1243   else
1244     {
1245       int regno;
1246
1247       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1248         if ((regs_ever_live[regno]
1249              && (!call_used_regs[regno] || regno == FRAME_POINTER_REGNUM)))
1250           offset += UNITS_PER_WORD;
1251
1252       /* See the comments for get_frame_size.  We need to round it up to
1253          STACK_BOUNDARY.  */
1254
1255       offset += ((get_frame_size () + STACK_BOUNDARY / BITS_PER_UNIT - 1)
1256                  & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
1257
1258       if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1259         offset += UNITS_PER_WORD;       /* Skip saved PC */
1260     }
1261   return offset;
1262 }
1263
1264 /* Update the condition code from the insn.  */
1265
1266 int
1267 notice_update_cc (body, insn)
1268      rtx body;
1269      rtx insn;
1270 {
1271   switch (get_attr_cc (insn))
1272     {
1273     case CC_NONE:
1274       /* Insn does not affect the CC at all */
1275       break;
1276
1277     case CC_NONE_0HIT:
1278       /* Insn does not change the CC, but the 0't operand has been changed.  */
1279
1280       if (cc_status.value1 != 0
1281           && reg_overlap_mentioned_p (recog_operand[0], cc_status.value1))
1282         cc_status.value1 = 0;
1283
1284       if (cc_status.value2 != 0
1285           && reg_overlap_mentioned_p (recog_operand[0], cc_status.value2))
1286         cc_status.value2 = 0;
1287
1288       break;
1289
1290     case CC_SET:
1291       /* Insn sets CC to recog_operand[0], but overflow is impossible.  */
1292       CC_STATUS_INIT;
1293       cc_status.flags |= CC_NO_OVERFLOW;
1294       cc_status.value1 = recog_operand[0];
1295       break;
1296
1297     case CC_COMPARE:
1298       /* The insn is a compare instruction */
1299       CC_STATUS_INIT;
1300       cc_status.value1 = SET_SRC (body);
1301       break;
1302
1303     case CC_CBIT:
1304       CC_STATUS_INIT;
1305       cc_status.flags |= CC_DONE_CBIT;
1306       cc_status.value1 = 0;
1307       break;
1308
1309     case CC_WHOOPS:
1310     case CC_CLOBBER:
1311       /* Insn clobbers CC. */
1312       CC_STATUS_INIT;
1313       break;
1314     }
1315 }
1316
1317 /* Recognize valid operators for bit instructions */
1318
1319 int
1320 bit_operator (x, mode)
1321      rtx x;
1322      enum machine_mode mode;
1323 {
1324   enum rtx_code code = GET_CODE (x);
1325
1326   return (code == XOR
1327           || code == AND
1328           || code == IOR);
1329 }
1330 \f
1331 /* Shifts.
1332
1333    We devote a fair bit of code to getting efficient shifts since we can only
1334    shift one bit at a time.  See the .md file for more comments.
1335
1336    Here are some thoughts on what the absolutely positively best code is.
1337    "Best" here means some rational trade-off between code size and speed,
1338    where speed is more preferred but not at the expense of generating 20 insns.
1339
1340    H8/300 QImode shifts
1341    1-4   - do them inline
1342    5-6   - ASHIFT | LSHIFTRT: rotate, mask off other bits
1343            ASHIFTRT: loop
1344    7     - ASHIFT | LSHIFTRT: rotate, mask off other bits
1345            ASHIFTRT: shll, subx (propagate carry bit to all bits)
1346
1347    H8/300 HImode shifts
1348    1-4   - do them inline
1349    5-6   - loop
1350    7     - shift other way once, move byte into place, move carry bit into place
1351    8     - move byte, zero (ASHIFT | LSHIFTRT) or sign extend other (ASHIFTRT)
1352    9     - inline shift 1-4, move byte, set other byte
1353    13-14 - ASHIFT | LSHIFTRT: rotate 3/2, mask, move byte, set other byte to 0
1354          - ASHIFTRT: loop
1355    15    - ASHIFT | LSHIFTRT: rotate 1, mask, move byte, set other byte to 0
1356          - ASHIFTRT: shll, subx, set other byte
1357
1358    H8/300 SImode shifts
1359    1-2   - do them inline
1360    3-6   - loop
1361    7     - shift other way once, move bytes into place,
1362            move carry into place (possibly with sign extension)
1363    8     - move bytes into place, zero or sign extend other
1364    9-14  - loop
1365    15    - shift other way once, move word into place, move carry into place
1366    16    - move word, zero or sign extend other
1367    17-23 - loop
1368    24    - move bytes into place, zero or sign extend other
1369    25-27 - loop
1370    28-30 - ASHIFT | LSHIFTRT: rotate top byte, mask, move byte into place,
1371                               zero others
1372            ASHIFTRT: loop
1373    31    - ASHIFT | LSHIFTRT: rotate top byte, mask, byte byte into place,
1374                               zero others
1375            ASHIFTRT: shll top byte, subx, copy to other bytes
1376
1377    H8/300H QImode shifts
1378    - same as H8/300
1379
1380    H8/300H HImode shifts
1381    - same as H8/300
1382
1383    H8/300H SImode shifts
1384    (These are complicated by the fact that we don't have byte level access to
1385    the top word.)
1386    A word is: bytes 3,2,1,0 (msb -> lsb), word 1,0 (msw -> lsw)
1387    1-4   - do them inline
1388    5-14  - loop
1389    15    - shift other way once, move word into place, move carry into place
1390            (with sign extension for ASHIFTRT)
1391    16    - move word into place, zero or sign extend other
1392    17-23 - loop
1393    24    - ASHIFT: move byte 0(msb) to byte 1, zero byte 0,
1394                    move word 0 to word 1, zero word 0
1395            LSHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1396                      zero word 1, zero byte 1
1397            ASHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1398                      sign extend byte 0, sign extend word 0
1399    25-27 - either loop, or
1400            do 24 bit shift, inline rest
1401    28-30 - ASHIFT: rotate 4/3/2, mask
1402            LSHIFTRT: rotate 4/3/2, mask
1403            ASHIFTRT: loop
1404    31    - shll, subx byte 0, sign extend byte 0, sign extend word 0
1405
1406    Don't Panic!!!
1407
1408    All of these haven't been implemented.  I've just documented them and
1409    provided hooks so they can be.
1410 */
1411
1412 int
1413 nshift_operator (x, mode)
1414      rtx x;
1415      enum machine_mode mode;
1416 {
1417   switch (GET_CODE (x))
1418     {
1419     case ASHIFTRT:
1420     case LSHIFTRT:
1421     case ASHIFT:
1422       return 1;
1423
1424     default:
1425       return 0;
1426     }
1427 }
1428
1429 /* Called from the .md file to emit code to do shifts.
1430    Returns a boolean indicating success
1431    (currently this is always TRUE).  */
1432
1433 int
1434 expand_a_shift (mode, code, operands)
1435      enum machine_mode mode;
1436      int code;
1437      rtx operands[];
1438 {
1439   extern int rtx_equal_function_value_matters;
1440
1441   emit_move_insn (operands[0], operands[1]);
1442
1443   /* need a loop to get all the bits we want  - we generate the
1444      code at emit time, but need to allocate a scratch reg now  */
1445
1446   emit_insn (gen_rtx
1447              (PARALLEL, VOIDmode,
1448               gen_rtvec (2,
1449                          gen_rtx (SET, VOIDmode, operands[0],
1450                                   gen_rtx (code, mode, operands[0], operands[2])),
1451                          gen_rtx (CLOBBER, VOIDmode, gen_rtx (SCRATCH, QImode, 0)))));
1452
1453   return 1;
1454 }
1455
1456 /* Shift algorithm determination.
1457
1458    There are various ways of doing a shift:
1459    SHIFT_INLINE: If the amount is small enough, just generate as many one-bit
1460                  shifts as we need.
1461    SHIFT_ROT_AND: If the amount is large but close to either end, rotate the
1462                   necessary bits into position and then set the rest to zero.
1463    SHIFT_SPECIAL: Hand crafted assembler.
1464    SHIFT_LOOP:    If the above methods fail, just loop.  */
1465
1466 enum shift_alg
1467 {
1468   SHIFT_INLINE,
1469   SHIFT_ROT_AND,
1470   SHIFT_SPECIAL,
1471   SHIFT_LOOP,
1472   SHIFT_MAX
1473 };
1474
1475 /* Symbols of the various shifts which can be used as indices.  */
1476
1477 enum shift_type
1478   {
1479     SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
1480   };
1481
1482 /* Symbols of the various modes which can be used as indices.  */
1483
1484 enum shift_mode
1485   {
1486     QIshift, HIshift, SIshift
1487   };
1488
1489 /* For single bit shift insns, record assembler and whether the condition code
1490    is valid afterwards.  */
1491
1492 struct shift_insn
1493 {
1494   char *assembler;
1495   int cc_valid;
1496 };
1497
1498 /* Assembler instruction shift table.
1499
1500    These tables are used to look up the basic shifts.
1501    They are indexed by cpu, shift_type, and mode.
1502 */
1503
1504 static const struct shift_insn shift_one[2][3][3] =
1505 {
1506 /* H8/300 */
1507   {
1508 /* SHIFT_ASHIFT */
1509     {
1510       { "shal %X0", 1 },
1511       { "add.w %T0,%T0\t; shal.w", 1 },
1512       { "add.w %f0,%f0\t; shal.l\n\taddx %y0,%y0\n\taddx %z0,%z0\t; end shal.l", 0 }
1513     },
1514 /* SHIFT_LSHIFTRT */
1515     {
1516       { "shlr %X0", 1 },
1517       { "shlr %t0\t; shlr.w\n\trotxr %s0\t; end shlr.w", 0 },
1518       { "shlr %z0\t; shlr.l\n\trotxr %y0\n\trotxr %x0\n\trotxr %w0\t; end shlr.l", 0 }
1519     },
1520 /* SHIFT_ASHIFTRT */
1521     {
1522       { "shar %X0", 1 },
1523       { "shar %t0\t; shar.w\n\trotxr %s0\t; end shar.w", 0 },
1524       { "shar %z0\t; shar.l\n\trotxr %y0\n\trotxr %x0\n\trotxr %w0\t; end shar.l", 0 }
1525     }
1526   },
1527 /* H8/300H */
1528   {
1529 /* SHIFT_ASHIFT */
1530     {
1531       { "shal.b %X0", 1 },
1532       { "shal.w %T0", 1 },
1533       { "shal.l %S0", 1 }
1534     },
1535 /* SHIFT_LSHIFTRT */
1536     {
1537       { "shlr.b %X0", 1 },
1538       { "shlr.w %T0", 1 },
1539       { "shlr.l %S0", 1 }
1540     },
1541 /* SHIFT_ASHIFTRT */
1542     {
1543       { "shar.b %X0", 1 },
1544       { "shar.w %T0", 1 },
1545       { "shar.l %S0", 1 }
1546     }
1547   }
1548 };
1549
1550 /* Rotates are organized by which shift they'll be used in implementing.
1551    There's no need to record whether the cc is valid afterwards because
1552    it is the AND insn that will decide this.  */
1553
1554 static const char *const rotate_one[2][3][3] =
1555 {
1556 /* H8/300 */
1557   {
1558 /* SHIFT_ASHIFT */
1559     {
1560       "rotr %X0",
1561       "shlr %t0\t; rotr.w\n\trotxr %s0\n\tbst #7,%t0\t; end rotr.w",
1562       0
1563     },
1564 /* SHIFT_LSHIFTRT */
1565     {
1566       "rotl %X0",
1567       "shll %s0\t; rotl.w\n\trotxl %t0\n\tbst #0,%s0\t; end rotl.w",
1568       0
1569     },
1570 /* SHIFT_ASHIFTRT */
1571     {
1572       "rotl %X0",
1573       "shll %s0\t; rotl.w\n\trotxl %t0\n\tbst #0,%s0\t; end rotl.w",
1574       0
1575     }
1576   },
1577 /* H8/300H */
1578   {
1579 /* SHIFT_ASHIFT */
1580     {
1581       "rotr.b %X0",
1582       "rotr.w %T0",
1583       "rotr.l %S0"
1584     },
1585 /* SHIFT_LSHIFTRT */
1586     {
1587       "rotl.b %X0",
1588       "rotl.w %T0",
1589       "rotl.l %S0"
1590     },
1591 /* SHIFT_ASHIFTRT */
1592     {
1593       "rotl.b %X0",
1594       "rotl.w %T0",
1595       "rotl.l %S0"
1596     }
1597   }
1598 };
1599
1600 /* Given CPU, MODE, SHIFT_TYPE, and shift count COUNT, determine the best
1601    algorithm for doing the shift.  The assembler code is stored in ASSEMBLER.
1602    We don't achieve maximum efficiency in all cases, but the hooks are here
1603    to do so.
1604
1605    For now we just use lots of switch statements.  Since we don't even come
1606    close to supporting all the cases, this is simplest.  If this function ever
1607    gets too big, perhaps resort to a more table based lookup.  Of course,
1608    at this point you may just wish to do it all in rtl.
1609
1610    WARNING: The constraints on insns shiftbyn_QI/HI/SI assume shifts of
1611    1,2,3,4 will be inlined (1,2 for SI).  */
1612
1613 static enum shift_alg
1614 get_shift_alg (cpu, shift_type, mode, count, assembler_p, cc_valid_p)
1615      enum attr_cpu cpu;
1616      enum shift_type shift_type;
1617      enum machine_mode mode;
1618      int count;
1619      const char **assembler_p;
1620      int *cc_valid_p;
1621 {
1622   /* The default is to loop.  */
1623   enum shift_alg alg = SHIFT_LOOP;
1624   enum shift_mode shift_mode;
1625
1626   /* We don't handle negative shifts or shifts greater than the word size,
1627      they should have been handled already.  */
1628
1629   if (count < 0 || count > GET_MODE_BITSIZE (mode))
1630     abort ();
1631
1632   switch (mode)
1633     {
1634     case QImode:
1635       shift_mode = QIshift;
1636       break;
1637     case HImode:
1638       shift_mode = HIshift;
1639       break;
1640     case SImode:
1641       shift_mode = SIshift;
1642       break;
1643     default:
1644       abort ();
1645     }
1646
1647   /* Assume either SHIFT_LOOP or SHIFT_INLINE.
1648      It is up to the caller to know that looping clobbers cc.  */
1649   *assembler_p = shift_one[cpu][shift_type][shift_mode].assembler;
1650   *cc_valid_p = shift_one[cpu][shift_type][shift_mode].cc_valid;
1651
1652   /* Now look for cases we want to optimize.  */
1653
1654   switch (shift_mode)
1655     {
1656     case QIshift:
1657       if (count <= 4)
1658         return SHIFT_INLINE;
1659       else if (count <= 6)
1660         {
1661           if (shift_type == SHIFT_ASHIFTRT)
1662             {
1663               return SHIFT_LOOP;
1664             }
1665           else
1666             {
1667               *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1668               *cc_valid_p = 0;
1669               return SHIFT_ROT_AND;
1670             }
1671         }
1672       else if (count == 7)
1673         {
1674           if (shift_type == SHIFT_ASHIFTRT)
1675             {
1676               *assembler_p = "shll %X0\t; shar.b(7)\n\tsubx %X0,%X0\t; end shar.b(7)";
1677               *cc_valid_p = 0;
1678               return SHIFT_SPECIAL;
1679             }
1680           else
1681             {
1682               *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1683               *cc_valid_p = 0;
1684               return SHIFT_ROT_AND;
1685             }
1686         }
1687       break;
1688     case HIshift:
1689       if (count <= 4)
1690         return SHIFT_INLINE;
1691       else if (count == 8)
1692         {
1693           switch (shift_type)
1694             {
1695             case SHIFT_ASHIFT:
1696               *assembler_p = "mov.b %s0,%t0\t; shal.w(8)\n\tsub.b %s0,%s0\t; end shal.w(8)";
1697               *cc_valid_p = 0;
1698               return SHIFT_SPECIAL;
1699             case SHIFT_LSHIFTRT:
1700               *assembler_p = "mov.b %t0,%s0\t; shlr.w(8)\n\tsub.b %t0,%t0\t; end shlr.w(8)";
1701               *cc_valid_p = 0;
1702               return SHIFT_SPECIAL;
1703             case SHIFT_ASHIFTRT:
1704               if (cpu == CPU_H8300)
1705                 *assembler_p = "mov.b %t0,%s0\t; shar.w(8)\n\tshll %t0\n\tsubx %t0,%t0\t; end shar.w(8)";
1706               else
1707                 *assembler_p = "mov.b %t0,%s0\t; shar.w(8)\n\texts.w %T0\t; end shar.w(8)";
1708               *cc_valid_p = 0;
1709               return SHIFT_SPECIAL;
1710             }
1711           abort ();
1712         }
1713       else if (count == 15)
1714         {
1715           if (shift_type == SHIFT_ASHIFTRT)
1716             {
1717               *assembler_p = "shll %t0,%t0\t; shar.w(15)\n\tsubx %t0,%t0\n\tmov.b %t0,%s0\t; end shar.w(15)";
1718               *cc_valid_p = 0;
1719               return SHIFT_SPECIAL;
1720             }
1721           else
1722             {
1723               *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1724               *cc_valid_p = 0;
1725               return SHIFT_ROT_AND;
1726             }
1727         }
1728       break;
1729     case SIshift:
1730       if (count <= (cpu == CPU_H8300 ? 2 : 4))
1731         return SHIFT_INLINE;
1732       else if (count == 8)
1733         {
1734           if (cpu == CPU_H8300)
1735             {
1736               switch (shift_type)
1737                 {
1738                 case SHIFT_ASHIFT:
1739                   *assembler_p = "mov.b %y0,%z0\t; shal.l(8)\n\tmov.b %x0,%y0\n\tmov.b %w0,%x0\n\tsub.b %w0,%w0\t; end shal.l(8)";
1740                   *cc_valid_p = 0;
1741                   return SHIFT_SPECIAL;
1742                 case SHIFT_LSHIFTRT:
1743                   *assembler_p = "mov.b %x0,%w0\t; shlr.l(8)\n\tmov.b %y0,%x0\n\tmov.b %z0,%y0\n\tsub.b %z0,%z0\t; end shlr.l(8)";
1744                   *cc_valid_p = 0;
1745                   return SHIFT_SPECIAL;
1746                 case SHIFT_ASHIFTRT:
1747                   *assembler_p = "mov.b %x0,%w0\t; shar.l(8)\n\tmov.b %y0,%x0\n\tmov.b %z0,%y0\n\tshll %z0\n\tsubx %z0,%z0; end shar.l(8)";
1748                   *cc_valid_p = 0;
1749                   return SHIFT_SPECIAL;
1750                 }
1751             }
1752           else                  /* CPU_H8300H */
1753             /* We don't have byte level access to the high word so this isn't
1754                easy to do.  For now, just loop.  */
1755             ;
1756         }
1757       else if (count == 16)
1758         {
1759           switch (shift_type)
1760             {
1761             case SHIFT_ASHIFT:
1762               *assembler_p = "mov.w %f0,%e0\t; shal.l(16)\n\tsub.w %f0,%f0\t; end shal.l(16)";
1763               *cc_valid_p = 0;
1764               return SHIFT_SPECIAL;
1765             case SHIFT_LSHIFTRT:
1766               *assembler_p = "mov.w %e0,%f0\t; shlr.l(16)\n\tsub.w %e0,%e0\t; end shlr.l(16)";
1767               *cc_valid_p = 0;
1768               return SHIFT_SPECIAL;
1769             case SHIFT_ASHIFTRT:
1770               if (cpu == CPU_H8300)
1771                 *assembler_p = "mov.w %e0,%f0\t; shar.l(16)\n\tshll %z0\n\tsubx %z0,%z0\n\tmov.b %z0,%y0\t; end shar.l(16)";
1772               else
1773                 *assembler_p = "mov.w %e0,%f0\t; shar.l(16)\n\texts.l %S0\t; end shar.l(16)";
1774               *cc_valid_p = 0;
1775               return SHIFT_SPECIAL;
1776             }
1777         }
1778       else if (count >= 28 && count <= 30)
1779         {
1780           if (shift_type == SHIFT_ASHIFTRT)
1781             {
1782               return SHIFT_LOOP;
1783             }
1784           else
1785             {
1786               if (cpu == CPU_H8300)
1787                 return SHIFT_LOOP;
1788               else
1789                 {
1790                   *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1791                   *cc_valid_p = 0;
1792                   return SHIFT_ROT_AND;
1793                 }
1794             }
1795         }
1796       else if (count == 31)
1797         {
1798           if (shift_type == SHIFT_ASHIFTRT)
1799             {
1800               if (cpu == CPU_H8300)
1801                 *assembler_p = "shll %z0\t; shar.l(31)\n\tsubx %w0,%w0\n\tmov.b %w0,%x0\n\tmov.w %f0,%e0\t; end shar.l(31)";
1802               else
1803                 *assembler_p = "shll %e0\t; shar.l(31)\n\tsubx %w0,%w0\n\tmov.b %w0,%x0\n\tmov.w %f0,%e0\t; end shar.l(31)";
1804               *cc_valid_p = 0;
1805               return SHIFT_SPECIAL;
1806             }
1807           else
1808             {
1809               if (cpu == CPU_H8300)
1810                 {
1811                   if (shift_type == SHIFT_ASHIFT)
1812                     *assembler_p = "sub.w %e0,%e0\t; shal.l(31)\n\tshlr %w0\n\tmov.w %e0,%f0\n\trotxr %z0\t; end shal.l(31)";
1813                   else
1814                     *assembler_p = "sub.w %f0,%f0\t; shlr.l(31)\n\tshll %z0\n\tmov.w %f0,%e0\n\trotxl %w0\t; end shlr.l(31)";
1815                   *cc_valid_p = 0;
1816                   return SHIFT_SPECIAL;
1817                 }
1818               else
1819                 {
1820                   *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1821                   *cc_valid_p = 0;
1822                   return SHIFT_ROT_AND;
1823                 }
1824             }
1825         }
1826       break;
1827     default:
1828       abort ();
1829     }
1830
1831   return alg;
1832 }
1833
1834 /* Emit the assembler code for doing shifts.  */
1835
1836 char *
1837 emit_a_shift (insn, operands)
1838      rtx insn;
1839      rtx *operands;
1840 {
1841   static int loopend_lab;
1842   char *assembler;
1843   int cc_valid;
1844   rtx inside = PATTERN (insn);
1845   rtx shift = operands[3];
1846   enum machine_mode mode = GET_MODE (shift);
1847   enum rtx_code code = GET_CODE (shift);
1848   enum shift_type shift_type;
1849   enum shift_mode shift_mode;
1850
1851   loopend_lab++;
1852
1853   switch (mode)
1854     {
1855     case QImode:
1856       shift_mode = QIshift;
1857       break;
1858     case HImode:
1859       shift_mode = HIshift;
1860       break;
1861     case SImode:
1862       shift_mode = SIshift;
1863       break;
1864     default:
1865       abort ();
1866     }
1867
1868   switch (code)
1869     {
1870     case ASHIFTRT:
1871       shift_type = SHIFT_ASHIFTRT;
1872       break;
1873     case LSHIFTRT:
1874       shift_type = SHIFT_LSHIFTRT;
1875       break;
1876     case ASHIFT:
1877       shift_type = SHIFT_ASHIFT;
1878       break;
1879     default:
1880       abort ();
1881     }
1882
1883   if (GET_CODE (operands[2]) != CONST_INT)
1884     {
1885       /* Indexing by reg, so have to loop and test at top */
1886       output_asm_insn ("mov.b   %X2,%X4", operands);
1887       fprintf (asm_out_file, "\tble     .Lle%d\n", loopend_lab);
1888
1889       /* Get the assembler code to do one shift.  */
1890       get_shift_alg (cpu_type, shift_type, mode, 1, &assembler, &cc_valid);
1891     }
1892   else
1893     {
1894       int n = INTVAL (operands[2]);
1895       enum shift_alg alg;
1896
1897       /* If the count is negative, make it 0.  */
1898       if (n < 0)
1899         n = 0;
1900       /* If the count is too big, truncate it.
1901          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
1902          do the intuitive thing.  */
1903       else if (n > GET_MODE_BITSIZE (mode))
1904         n = GET_MODE_BITSIZE (mode);
1905
1906       alg = get_shift_alg (cpu_type, shift_type, mode, n, &assembler, &cc_valid);
1907
1908       switch (alg)
1909         {
1910         case SHIFT_INLINE:
1911           while (--n >= 0)
1912             output_asm_insn (assembler, operands);
1913           if (cc_valid)
1914             cc_status.value1 = operands[0];
1915           return "";
1916         case SHIFT_ROT_AND:
1917           {
1918             int m = GET_MODE_BITSIZE (mode) - n;
1919             int mask = (shift_type == SHIFT_ASHIFT
1920                         ? ((1 << GET_MODE_BITSIZE (mode) - n) - 1) << n
1921                         : (1 << GET_MODE_BITSIZE (mode) - n) - 1);
1922             char insn_buf[200];
1923             /* Not all possibilities of rotate are supported.  They shouldn't
1924                be generated, but let's watch for 'em.  */
1925             if (assembler == 0)
1926               abort ();
1927             while (--m >= 0)
1928               output_asm_insn (assembler, operands);
1929             if (TARGET_H8300)
1930               {
1931                 switch (mode)
1932                   {
1933                   case QImode:
1934                     sprintf (insn_buf, "and #%d,%%X0\t; end shift %d via rotate+and",
1935                              mask, n);
1936                     cc_status.value1 = operands[0];
1937                     break;
1938                   case HImode:
1939                     sprintf (insn_buf, "and #%d,%%s0\n\tand #%d,%%t0\t; end shift %d via rotate+and",
1940                              mask & 255, mask >> 8, n);
1941                     break;
1942                   case SImode:
1943                     abort ();
1944                   }
1945               }
1946             else
1947               {
1948                 sprintf (insn_buf, "and.%c #%d,%%%c0",
1949                          "bwl"[shift_mode], mask,
1950                          mode == QImode ? 'X' : mode == HImode ? 'T' : 'S');
1951                 cc_status.value1 = operands[0];
1952               }
1953             output_asm_insn (insn_buf, operands);
1954             return "";
1955           }
1956         case SHIFT_SPECIAL:
1957           output_asm_insn (assembler, operands);
1958           return "";
1959         }
1960
1961       /* Need a loop, move limit to tmp reg */
1962       fprintf (asm_out_file, "\tmov.b   #%d,%sl\n", n, names_big[REGNO (operands[4])]);
1963     }
1964
1965   fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
1966   output_asm_insn (assembler, operands);
1967   output_asm_insn ("add #0xff,%X4", operands);
1968   fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
1969   fprintf (asm_out_file, ".Lle%d:\n", loopend_lab);
1970
1971   return "";
1972 }
1973 \f
1974 /* Fix the operands of a gen_xxx so that it could become a bit
1975   operating insn.  */
1976
1977 int
1978 fix_bit_operand (operands, what, type)
1979      rtx *operands;
1980      char what;
1981      enum rtx_code type;
1982 {
1983   /* The bit_operand predicate accepts any memory during RTL generation, but
1984      only 'U' memory afterwards, so if this is a MEM operand, we must force
1985      it to be valid for 'U' by reloading the address.  */
1986
1987   if (GET_CODE (operands[2]) == CONST_INT)
1988     {
1989       if (CONST_OK_FOR_LETTER_P (INTVAL (operands[2]), what))
1990         {
1991           /* Ok to have a memory dest.  */
1992           if (GET_CODE (operands[0]) == MEM && !EXTRA_CONSTRAINT (operands[0], 'U'))
1993             {
1994               rtx mem;
1995               mem = gen_rtx (MEM, GET_MODE (operands[0]),
1996                            copy_to_mode_reg (Pmode, XEXP (operands[0], 0)));
1997               RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (operands[0]);
1998               MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (operands[0]);
1999               MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (operands[0]);
2000               operands[0] = mem;
2001             }
2002
2003           if (GET_CODE (operands[1]) == MEM && !EXTRA_CONSTRAINT (operands[1], 'U'))
2004             {
2005               rtx mem;
2006               mem = gen_rtx (MEM, GET_MODE (operands[1]),
2007                            copy_to_mode_reg (Pmode, XEXP (operands[1], 0)));
2008               RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (operands[1]);
2009               MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (operands[1]);
2010               MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (operands[1]);
2011               operands[1] = mem;
2012             }
2013           return 0;
2014         }
2015     }
2016
2017   /* Dest and src op must be register.  */
2018
2019   operands[1] = force_reg (QImode, operands[1]);
2020   {
2021     rtx res = gen_reg_rtx (QImode);
2022     emit_insn (gen_rtx (SET, VOIDmode, res, gen_rtx (type, QImode, operands[1], operands[2])));
2023     emit_insn (gen_rtx (SET, VOIDmode, operands[0], res));
2024   }
2025   return 1;
2026 }