OSDN Git Service

* Makefile.in (INSN_ATTR_H): New macro. Replace all dependencies
[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, 1997, 1998, 1999, 2000
3    Free Software Foundation, Inc. 
4    Contributed by Steve Chamberlain (sac@cygnus.com),
5    Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "real.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-flags.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "obstack.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43
44 /* Forward declarations.  */
45 static int h8300_interrupt_function_p PARAMS ((tree));
46 static int h8300_monitor_function_p PARAMS ((tree));
47 static int h8300_os_task_function_p PARAMS ((tree));
48 static void dosize PARAMS ((FILE *, const char *, unsigned int));
49 static const char *cond_string PARAMS ((enum rtx_code));
50
51 /* CPU_TYPE, says what cpu we're compiling for.  */
52 int cpu_type;
53
54 /* True if the current function is an interrupt handler
55    (either via #pragma or an attribute specification).  */
56 int interrupt_handler;
57
58 /* True if the current function is an OS Task
59    (via an attribute specification).  */
60 int os_task;
61
62 /* True if the current function is a monitor
63    (via an attribute specification).  */
64 int monitor;
65
66 /* True if a #pragma saveall has been seen for the current function.  */
67 int pragma_saveall;
68
69 static const char *const names_big[] =
70 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
71
72 static const char *const names_extended[] =
73 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
74
75 static const char *const names_upper_extended[] =
76 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
77
78 /* Points to one of the above.  */
79 /* ??? The above could be put in an array indexed by CPU_TYPE.  */
80 const char * const *h8_reg_names;
81
82 /* Various operations needed by the following, indexed by CPU_TYPE.  */
83
84 static const char *const h8_push_ops[2] = { "push", "push.l" };
85 static const char *const h8_pop_ops[2] = { "pop", "pop.l" };
86 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
87
88 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
89
90 /* Initialize various cpu specific globals at start up.  */
91
92 void
93 h8300_init_once ()
94 {
95   if (TARGET_H8300)
96     {
97       cpu_type = (int) CPU_H8300;
98       h8_reg_names = names_big;
99     }
100   else
101     {
102       /* For this we treat the H8/300 and H8/S the same.  */
103       cpu_type = (int) CPU_H8300H;
104       h8_reg_names = names_extended;
105     }
106   h8_push_op = h8_push_ops[cpu_type];
107   h8_pop_op = h8_pop_ops[cpu_type];
108   h8_mov_op = h8_mov_ops[cpu_type];
109 }
110
111 const char *
112 byte_reg (x, b)
113      rtx x;
114      int b;
115 {
116   static const char *const names_small[] =
117   {"r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
118    "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"};
119
120   return names_small[REGNO (x) * 2 + b];
121 }
122
123 /* REGNO must be saved/restored across calls if this macro is true.  */
124
125 #define WORD_REG_USED(regno)                                    \
126   (regno < 7                                                    \
127    /* No need to save registers if this function will not return.  */\
128    && ! TREE_THIS_VOLATILE (current_function_decl)              \
129    && (pragma_saveall                                           \
130        /* Save any call saved register that was used.  */       \
131        || (regs_ever_live[regno] && !call_used_regs[regno])     \
132        /* Save the frame pointer if it was used.  */            \
133        || (regno == FRAME_POINTER_REGNUM && regs_ever_live[regno])\
134        /* Save any register used in an interrupt handler.  */   \
135        || (interrupt_handler && regs_ever_live[regno])          \
136        /* Save call clobbered registers in non-leaf interrupt   \
137           handlers.  */                                         \
138        || (interrupt_handler                                    \
139            && call_used_regs[regno]                             \
140            && !current_function_is_leaf)))
141
142 /* Output assembly language to FILE for the operation OP with operand size
143    SIZE to adjust the stack pointer.  */
144
145 static void
146 dosize (file, op, size)
147      FILE *file;
148      const char *op;
149      unsigned int size;
150 {
151   /* On the h8300h and h8300s, for sizes <= 8 bytes it is as good or
152      better to use adds/subs insns rather than add.l/sub.l
153      with an immediate value.   */
154   if (size > 4 && size <= 8 && (TARGET_H8300H || TARGET_H8300S))
155     {
156       /* Crank the size down to <= 4.  */
157       fprintf (file, "\t%ss\t#%d,sp\n", op, 4);
158       size -= 4;
159     }
160
161   switch (size)
162     {
163     case 4:
164       if (TARGET_H8300H || TARGET_H8300S)
165         {
166           fprintf (file, "\t%ss\t#%d,sp\n", op, 4);
167           size = 0;
168           break;
169         }
170     case 3:
171       fprintf (file, "\t%ss\t#%d,sp\n", op, 2);
172       size -= 2;
173       /* Fall through...  */
174     case 2:
175     case 1:
176       fprintf (file, "\t%ss\t#%d,sp\n", op, size);
177       size = 0;
178       break;
179     case 0:
180       break;
181     default:
182       if (TARGET_H8300)
183         {
184           if (current_function_needs_context
185               && strcmp (op, "sub") == 0)
186             {
187               /* Egad.  We don't have a temporary to hold the
188                  size of the frame in the prologue!  Just inline
189                  the bastard since this shouldn't happen often.  */
190               while (size >= 2)
191                 {
192                   fprintf (file, "\tsubs\t#2,sp\n");
193                   size -= 2;
194                 }
195
196               if (size)
197                 fprintf (file, "\tsubs\t#1,sp\n");
198
199               size = 0;
200             }
201           else
202             fprintf (file, "\tmov.w\t#%d,r3\n\t%s.w\tr3,sp\n", size, op);
203         }
204       else
205         fprintf (file, "\t%s\t#%d,sp\n", op, size);
206       size = 0;
207       break;
208     }
209 }
210
211 /* Output assembly language code for the function prologue.  */
212 static int push_order[FIRST_PSEUDO_REGISTER] =
213 { 0, 1, 2, 3, 4, 5, 6, -1, -1, -1 };
214 static int pop_order[FIRST_PSEUDO_REGISTER] =
215 { 6, 5, 4, 3, 2, 1, 0, -1, -1, -1 };
216
217 /* This is what the stack looks like after the prolog of 
218    a function with a frame has been set up:
219
220    <args>
221    PC
222    FP                   <- fp
223    <locals>
224    <saved registers>    <- sp
225
226    This is what the stack looks like after the prolog of
227    a function which doesn't have a frame:
228
229    <args>
230    PC
231    <locals>
232    <saved registers>    <- sp
233 */
234
235 void
236 function_prologue (file, size)
237      FILE *file;
238      int size;
239 {
240   int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
241   int idx;
242
243   /* Note a function with the interrupt attribute and set interrupt_handler
244      accordingly.  */
245   if (h8300_interrupt_function_p (current_function_decl))
246     interrupt_handler = 1;
247
248   /* If the current function has the OS_Task attribute set, then
249      we have a naked prologue.  */
250   if (h8300_os_task_function_p (current_function_decl))
251     {
252       fprintf (file, ";OS_Task prologue\n");
253       os_task = 1;
254       return;
255     }
256
257   if (h8300_monitor_function_p (current_function_decl))
258     {
259       /* My understanding of monitor functions is they act just
260          like interrupt functions, except the prologue must
261          mask interrupts.  */
262       fprintf (file, ";monitor prologue\n");
263       interrupt_handler = 1;
264       monitor = 1;
265       if (TARGET_H8300)
266         {
267           fprintf (file, "\tsubs\t#2,sp\n");
268           fprintf (file, "\tpush\tr0\n");
269           fprintf (file, "\tstc\tccr,r0l\n");
270           fprintf (file, "\torc\t#128,ccr\n");
271           fprintf (file, "\tmov.b\tr0l,@(4,sp)\n");
272         }
273       else
274         {
275           fprintf (file, "\tpush\ter0\n");
276           fprintf (file, "\tstc\tccr,r0l\n");
277           fprintf (file, "\torc\t#128,ccr\n");
278           fprintf (file, "\tmov.b\tr0l,@(4,sp)\n");
279         }
280     }
281
282   if (frame_pointer_needed)
283     {
284       /* Push fp.  */
285       fprintf (file, "\t%s\t%s\n", h8_push_op,
286                h8_reg_names[FRAME_POINTER_REGNUM]);
287       fprintf (file, "\t%s\t%s,%s\n", h8_mov_op,
288                h8_reg_names[STACK_POINTER_REGNUM],
289                h8_reg_names[FRAME_POINTER_REGNUM]);
290     }
291
292   /* Leave room for locals.  */
293   dosize (file, "sub", fsize);
294
295   /* Push the rest of the registers.  */
296   for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
297     {
298       int regno = push_order[idx];
299
300       if (regno >= 0
301           && WORD_REG_USED (regno)
302           && (!frame_pointer_needed || regno != FRAME_POINTER_REGNUM))
303         {
304           if (TARGET_H8300S)
305             {
306               /* Try to push multiple registers.  */
307               if (regno == 0 || regno == 4)
308                 {
309                   int second_regno = push_order[idx + 1];
310                   int third_regno = push_order[idx + 2];
311                   int fourth_regno = push_order[idx + 3];
312
313                   if (fourth_regno >= 0
314                       && WORD_REG_USED (fourth_regno)
315                       && (!frame_pointer_needed
316                           || fourth_regno != FRAME_POINTER_REGNUM)
317                       && third_regno >= 0
318                       && WORD_REG_USED (third_regno)
319                       && (!frame_pointer_needed
320                           || third_regno != FRAME_POINTER_REGNUM)
321                       && second_regno >= 0
322                       && WORD_REG_USED (second_regno)
323                       && (!frame_pointer_needed
324                           || second_regno != FRAME_POINTER_REGNUM))
325                     {
326                       fprintf (file, "\tstm.l %s-%s,@-sp\n",
327                                h8_reg_names[regno],
328                                h8_reg_names[fourth_regno]);
329                       idx += 3;
330                       continue;
331                     }
332                 }
333               if (regno == 0 || regno == 4)
334                 {
335                   int second_regno = push_order[idx + 1];
336                   int third_regno = push_order[idx + 2];
337
338                   if (third_regno >= 0
339                       && WORD_REG_USED (third_regno)
340                       && (!frame_pointer_needed
341                           || third_regno != FRAME_POINTER_REGNUM)
342                       && second_regno >= 0
343                       && WORD_REG_USED (second_regno)
344                       && (!frame_pointer_needed
345                           || second_regno != FRAME_POINTER_REGNUM))
346                     {
347                       fprintf (file, "\tstm.l %s-%s,@-sp\n",
348                                h8_reg_names[regno],
349                                h8_reg_names[third_regno]);
350                       idx += 2;
351                       continue;
352                     }
353                 }
354               if (regno == 0 || regno == 2 || regno == 4 || regno == 6)
355                 {
356                   int second_regno = push_order[idx + 1];
357
358                   if (second_regno >= 0
359                       && WORD_REG_USED (second_regno)
360                       && (!frame_pointer_needed
361                           || second_regno != FRAME_POINTER_REGNUM))
362                     {
363                       fprintf (file, "\tstm.l %s-%s,@-sp\n",
364                                h8_reg_names[regno],
365                                h8_reg_names[second_regno]);
366                       idx += 1;
367                       continue;
368                     }
369                 }
370             }
371           fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[regno]);
372         }
373     }
374 }
375
376 /* Output assembly language code for the function epilogue.  */
377
378 void
379 function_epilogue (file, size)
380      FILE *file;
381      int size;
382 {
383   int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
384   int idx;
385   rtx insn = get_last_insn ();
386
387   if (os_task)
388     {
389       /* OS_Task epilogues are nearly naked -- they just have an
390          rts instruction.  */
391       fprintf (file, ";OS_task epilogue\n");
392       fprintf (file, "\trts\n");
393       goto out;
394     }
395
396   /* Monitor epilogues are the same as interrupt function epilogues.
397      Just make a note that we're in an monitor epilogue.  */
398   if (monitor)
399     fprintf (file, ";monitor epilogue\n");
400
401   /* If the last insn was a BARRIER, we don't have to write any code.  */
402   if (GET_CODE (insn) == NOTE)
403     insn = prev_nonnote_insn (insn);
404   if (insn && GET_CODE (insn) == BARRIER)
405     goto out;
406
407   /* Pop the saved registers.  */
408   for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
409     {
410       int regno = pop_order[idx];
411
412       if (regno >= 0
413           && WORD_REG_USED (regno)
414           && (!frame_pointer_needed || regno != FRAME_POINTER_REGNUM))
415         {
416           if (TARGET_H8300S)
417             {
418               /* Try to pop multiple registers.  */
419               if (regno == 7 || regno == 3)
420                 {
421                   int second_regno = pop_order[idx + 1];
422                   int third_regno = pop_order[idx + 2];
423                   int fourth_regno = pop_order[idx + 3];
424
425                   if (fourth_regno >= 0
426                       && WORD_REG_USED (fourth_regno)
427                       && (!frame_pointer_needed
428                           || fourth_regno != FRAME_POINTER_REGNUM)
429                       && third_regno >= 0
430                       && WORD_REG_USED (third_regno)
431                       && (!frame_pointer_needed
432                           || third_regno != FRAME_POINTER_REGNUM)
433                       && second_regno >= 0
434                       && WORD_REG_USED (second_regno)
435                       && (!frame_pointer_needed
436                           || second_regno != FRAME_POINTER_REGNUM))
437                     {
438                       fprintf (file, "\tldm.l @sp+,%s-%s\n",
439                                h8_reg_names[fourth_regno],
440                                h8_reg_names[regno]);
441                       idx += 3;
442                       continue;
443                     }
444                 }
445               if (regno == 6 || regno == 2)
446                 {
447                   int second_regno = pop_order[idx + 1];
448                   int third_regno = pop_order[idx + 2];
449
450                   if (third_regno >= 0
451                       && WORD_REG_USED (third_regno)
452                       && (!frame_pointer_needed
453                           || third_regno != FRAME_POINTER_REGNUM)
454                       && second_regno >= 0
455                       && WORD_REG_USED (second_regno)
456                       && (!frame_pointer_needed
457                           || second_regno != FRAME_POINTER_REGNUM))
458                     {
459                       fprintf (file, "\tldm.l @sp+,%s-%s\n",
460                                h8_reg_names[third_regno],
461                                h8_reg_names[regno]);
462                       idx += 2;
463                       continue;
464                     }
465                 }
466               if (regno == 7 || regno == 5 || regno == 3 || regno == 1)
467                 {
468                   int second_regno = pop_order[idx + 1];
469
470                   if (second_regno >= 0
471                       && WORD_REG_USED (second_regno)
472                       && (!frame_pointer_needed
473                           || second_regno != FRAME_POINTER_REGNUM))
474                     {
475                       fprintf (file, "\tldm.l @sp+,%s-%s\n",
476                                h8_reg_names[second_regno],
477                                h8_reg_names[regno]);
478                       idx += 1;
479                       continue;
480                     }
481                 }
482             }
483           fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[regno]);
484         }
485     }
486
487   /* Deallocate locals.  */
488   dosize (file, "add", fsize);
489
490   /* Pop frame pointer if we had one.  */
491   if (frame_pointer_needed)
492     fprintf (file, "\t%s\t%s\n",
493              h8_pop_op, h8_reg_names[FRAME_POINTER_REGNUM]);
494
495   /* If this is a monitor function, there is one register still left on
496      the stack.  */
497   if (monitor)
498     fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[0]);
499
500   if (interrupt_handler)
501     fprintf (file, "\trte\n");
502   else
503     fprintf (file, "\trts\n");
504
505  out:
506   interrupt_handler = 0;
507   os_task = 0;
508   monitor = 0;
509   pragma_saveall = 0;
510 }
511
512 /* Output assembly code for the start of the file.  */
513
514 void
515 asm_file_start (file)
516      FILE *file;
517 {
518   fprintf (file, ";\tGCC For the Hitachi H8/300\n");
519   fprintf (file, ";\tBy Hitachi America Ltd and Cygnus Support\n");
520   fprintf (file, ";\trelease F-1\n");
521   if (optimize)
522     fprintf (file, "; -O%d\n", optimize);
523   if (TARGET_H8300H)
524     fprintf (file, "\n\t.h8300h\n");
525   else if (TARGET_H8300S)
526     fprintf (file, "\n\t.h8300s\n");
527   else
528     fprintf (file, "\n\n");
529   output_file_directive (file, main_input_filename);
530 }
531
532 /* Output assembly language code for the end of file.  */
533
534 void
535 asm_file_end (file)
536      FILE *file;
537 {
538   fprintf (file, "\t.end\n");
539 }
540 \f
541 /* Return true if VALUE is a valid constant for constraint 'P'.
542    IE: VALUE is a power of two <= 2**15.  */
543
544 int
545 small_power_of_two (value)
546      HOST_WIDE_INT value;
547 {
548   int power = exact_log2 (value);
549   return power >= 0 && power <= 15;
550 }
551
552 /* Return true if VALUE is a valid constant for constraint 'O', which
553    means that the constant would be ok to use as a bit for a bclr
554    instruction.  */
555
556 int
557 ok_for_bclr (value)
558      HOST_WIDE_INT value;
559 {
560   return small_power_of_two ((~value) & 0xff);
561 }
562
563 /* Return true is OP is a valid source operand for an integer move
564    instruction.  */
565
566 int
567 general_operand_src (op, mode)
568      rtx op;
569      enum machine_mode mode;
570 {
571   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
572     return 1;
573   return general_operand (op, mode);
574 }
575
576 /* Return true if OP is a valid destination operand for an integer move
577    instruction.  */
578
579 int
580 general_operand_dst (op, mode)
581      rtx op;
582      enum machine_mode mode;
583 {
584   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == PRE_DEC)
585     return 1;
586   return general_operand (op, mode);
587 }
588
589 /* Return true if OP is a const valid for a bit clear instruction.  */
590
591 int
592 o_operand (operand, mode)
593      rtx operand;
594      enum machine_mode mode ATTRIBUTE_UNUSED;
595 {
596   return (GET_CODE (operand) == CONST_INT
597           && CONST_OK_FOR_O (INTVAL (operand)));
598 }
599
600 /* Return true if OP is a const valid for a bit set or bit xor instruction.  */
601
602 int
603 p_operand (operand, mode)
604      rtx operand;
605      enum machine_mode mode ATTRIBUTE_UNUSED;
606 {
607   return (GET_CODE (operand) == CONST_INT
608           && CONST_OK_FOR_P (INTVAL (operand)));
609 }
610
611 /* Return true if OP is a valid call operand.  */
612
613 int
614 call_insn_operand (op, mode)
615      rtx op;
616      enum machine_mode mode ATTRIBUTE_UNUSED;
617 {
618   if (GET_CODE (op) == MEM)
619     {
620       rtx inside = XEXP (op, 0);
621       if (register_operand (inside, Pmode))
622         return 1;
623       if (CONSTANT_ADDRESS_P (inside))
624         return 1;
625     }
626   return 0;
627 }
628
629 /* Return 1 if an addition/subtraction of a constant integer can be
630    transformed into two consecutive adds/subs that are faster than the
631    straightforward way.  Otherwise, return 0.  */
632
633 int
634 two_insn_adds_subs_operand (op, mode)
635      rtx op;
636      enum machine_mode mode;
637 {
638   if (GET_CODE (op) == CONST_INT)
639     {
640       HOST_WIDE_INT value = INTVAL (op);
641
642       /* Force VALUE to be positive so that we do not have to consider
643          the negative case.  */
644       if (value < 0)
645         value = -value;
646       if (TARGET_H8300H || TARGET_H8300S)
647         {
648           /* A constant addition/subtraction takes 2 states in QImode,
649              4 states in HImode, and 6 states in SImode.  Thus, the
650              only case we can win is when SImode is used, in which
651              case, two adds/subs are used, taking 4 states.  */
652           if (mode == SImode
653               && (value == 2 + 1
654                   || value == 4 + 1
655                   || value == 4 + 2
656                   || value == 4 + 4))
657             return 1;
658         }
659       else
660         {
661           /* A constant addition/subtraction takes 2 states in
662              QImode. It takes 6 states in HImode, requiring the
663              constant to be loaded to a register first, and a lot more
664              in SImode.  Thus the only case we can win is when either
665              HImode or SImode is used.  */
666           if (mode != QImode
667               && (value == 2 + 1
668                   || value == 2 + 2))
669             return 1;
670         }
671     }
672
673   return 0;
674 }
675
676 /* Split an add of a small constant into two adds/subs insns.  */
677
678 void
679 split_adds_subs (mode, operands)
680      enum machine_mode mode;
681      rtx *operands;
682 {
683   HOST_WIDE_INT val = INTVAL (operands[1]);
684   rtx reg = operands[0];
685   rtx tmp;
686
687   /* Take care of +/- 4 for H8300H and H8300S.  */
688   if (TARGET_H8300H || TARGET_H8300S)
689     {
690       /* Get the value in range of +/- 4.  */
691       if (val > 4)
692         {
693           tmp = gen_rtx_PLUS (mode, reg, GEN_INT (4));
694           emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
695           val -= 4;
696         }
697       else if (val < -4)
698         {
699           tmp = gen_rtx_PLUS (mode, reg, GEN_INT (-4));
700           emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
701           val += 4;
702         }
703
704       if (val == 4 || val == -4)
705         {
706           tmp = gen_rtx_PLUS (mode, reg, GEN_INT (val));
707           emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
708           return;
709         }
710     }
711
712   /* Get the value in range of +/- 2.  */
713   if (val > 2)
714     {
715       tmp = gen_rtx_PLUS (mode, reg, GEN_INT (2));
716       emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
717       val -= 2;
718     }
719   else if (val < -2)
720     {
721       tmp = gen_rtx_PLUS (mode, reg, GEN_INT (-2));
722       emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
723       val += 2;
724     }
725
726   /* If not optimizing, we might be asked to add 0.  */
727   if (val == 0)
728     return;
729
730   /* We should have one or two now.  */
731   if (val >= -2 && val <= 2)
732     {
733       tmp = gen_rtx_PLUS (mode, reg, GEN_INT (val));
734       emit_insn (gen_rtx_SET (VOIDmode, reg, tmp));
735       return;
736     }
737
738   /* In theory, this can't happen.  */
739   abort ();
740 }
741
742 /* Return true if OP is a valid call operand, and OP represents
743    an operand for a small call (4 bytes instead of 6 bytes).  */
744
745 int
746 small_call_insn_operand (op, mode)
747      rtx op;
748      enum machine_mode mode ATTRIBUTE_UNUSED;
749 {
750   if (GET_CODE (op) == MEM)
751     {
752       rtx inside = XEXP (op, 0);
753
754       /* Register indirect is a small call.  */
755       if (register_operand (inside, Pmode))
756         return 1;
757
758       /* A call through the function vector is a small
759          call too.  */
760       if (GET_CODE (inside) == SYMBOL_REF
761           && SYMBOL_REF_FLAG (inside))
762         return 1;
763     }
764   /* Otherwise it's a large call.  */
765   return 0;
766 }
767
768 /* Return true if OP is a valid jump operand.  */
769
770 int
771 jump_address_operand (op, mode)
772      rtx op;
773      enum machine_mode mode;
774 {
775   if (GET_CODE (op) == REG)
776     return mode == Pmode;
777
778   if (GET_CODE (op) == MEM)
779     {
780       rtx inside = XEXP (op, 0);
781       if (register_operand (inside, Pmode))
782         return 1;
783       if (CONSTANT_ADDRESS_P (inside))
784         return 1;
785     }
786   return 0;
787 }
788
789 /* Recognize valid operands for bitfield instructions.  */
790
791 extern int rtx_equal_function_value_matters;
792
793 int
794 bit_operand (op, mode)
795      rtx op;
796      enum machine_mode mode;
797 {
798   /* We can except any general operand, expept that MEM operands must
799      be limited to those that use addresses valid for the 'U' constraint.  */
800   if (!general_operand (op, mode))
801     return 0;
802
803   /* Accept any mem during RTL generation.  Otherwise, the code that does
804      insv and extzv will think that we can not handle memory.  However,
805      to avoid reload problems, we only accept 'U' MEM operands after RTL
806      generation.  This means that any named pattern which uses this predicate
807      must force its operands to match 'U' before emitting RTL.  */
808
809   if (GET_CODE (op) == REG)
810     return 1;
811   if (GET_CODE (op) == SUBREG)
812     return 1;
813   if (!rtx_equal_function_value_matters)
814     /* We're building rtl.  */
815     return GET_CODE (op) == MEM;
816   else
817     return (GET_CODE (op) == MEM
818             && EXTRA_CONSTRAINT (op, 'U'));
819 }
820
821 int
822 bit_memory_operand (op, mode)
823      rtx op;
824      enum machine_mode mode ATTRIBUTE_UNUSED;
825 {
826   return (GET_CODE (op) == MEM
827           && EXTRA_CONSTRAINT (op, 'U'));
828 }
829
830 /* Recognize valid operators for bit test.  */
831
832 int
833 eq_operator (x, mode)
834      rtx x;
835      enum machine_mode mode ATTRIBUTE_UNUSED;
836 {
837   return (GET_CODE (x) == EQ || GET_CODE (x) == NE);
838 }
839
840 /* Handle machine specific pragmas for compatibility with existing
841    compilers for the H8/300.
842
843    pragma saveall generates prolog/epilog code which saves and
844    restores all the registers on function entry.
845
846    pragma interrupt saves and restores all registers, and exits with
847    an rte instruction rather than an rts.  A pointer to a function
848    with this attribute may be safely used in an interrupt vector.  */
849
850 int
851 handle_pragma (p_getc, p_ungetc, pname)
852      int (* ATTRIBUTE_UNUSED p_getc) PARAMS ((void));
853      void (* ATTRIBUTE_UNUSED p_ungetc) PARAMS ((int));
854      const char *pname;
855 {
856   int retval = 0;
857
858   if (strcmp (pname, "interrupt") == 0)
859     interrupt_handler = retval = 1;
860   else if (strcmp (pname, "saveall") == 0)
861     pragma_saveall = retval = 1;
862
863   return retval;
864 }
865 \f
866 /* If the next arg with MODE and TYPE is to be passed in a register, return
867    the rtx to represent where it is passed.  CUM represents the state after
868    the last argument.  NAMED is not used.  */
869
870 static const char *const hand_list[] =
871 {
872   "__main",
873   "__cmpsi2",
874   "__divhi3",
875   "__modhi3",
876   "__udivhi3",
877   "__umodhi3",
878   "__divsi3",
879   "__modsi3",
880   "__udivsi3",
881   "__umodsi3",
882   "__mulhi3",
883   "__mulsi3",
884   "__reg_memcpy",
885   "__reg_memset",
886   "__ucmpsi2",
887   0,
888 };
889
890 /* Return an RTX to represent where a value with mode MODE will be returned
891    from a function.  If the result is 0, the argument is pushed.  */
892
893 rtx
894 function_arg (cum, mode, type, named)
895      CUMULATIVE_ARGS *cum;
896      enum machine_mode mode;
897      tree type;
898      int named;
899 {
900   rtx result = 0;
901   const char *fname;
902   int regpass = 0;
903
904   /* Never pass unnamed arguments in registers.  */
905   if (!named)
906     return 0;
907
908   /* Pass 3 regs worth of data in regs when user asked on the command line.  */
909   if (TARGET_QUICKCALL)
910     regpass = 3;
911
912   /* If calling hand written assembler, use 4 regs of args.  */
913
914   if (cum->libcall)
915     {
916       const char * const *p;
917
918       fname = XSTR (cum->libcall, 0);
919
920       /* See if this libcall is one of the hand coded ones.  */
921
922       for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
923         ;
924
925       if (*p)
926         regpass = 4;
927     }
928
929   if (regpass)
930     {
931       int size;
932
933       if (mode == BLKmode)
934         size = int_size_in_bytes (type);
935       else
936         size = GET_MODE_SIZE (mode);
937
938       if (size + cum->nbytes > regpass * UNITS_PER_WORD)
939         {
940           result = 0;
941         }
942       else
943         {
944           switch (cum->nbytes / UNITS_PER_WORD)
945             {
946             case 0:
947               result = gen_rtx_REG (mode, 0);
948               break;
949             case 1:
950               result = gen_rtx_REG (mode, 1);
951               break;
952             case 2:
953               result = gen_rtx_REG (mode, 2);
954               break;
955             case 3:
956               result = gen_rtx_REG (mode, 3);
957               break;
958             default:
959               result = 0;
960             }
961         }
962     }
963
964   return result;
965 }
966 \f
967 /* Return the cost of the rtx R with code CODE.  */
968
969 int
970 const_costs (r, c)
971      rtx r;
972      enum rtx_code c;
973 {
974   switch (c)
975     {
976     case CONST_INT:
977       switch (INTVAL (r))
978         {
979         case 0:
980         case 1:
981         case 2:
982         case -1:
983         case -2:
984           return 0;
985         case 4:
986         case -4:
987           if (TARGET_H8300H || TARGET_H8300S)
988             return 0;
989           else
990             return 1;
991         default:
992           return 1;
993         }
994
995     case CONST:
996     case LABEL_REF:
997     case SYMBOL_REF:
998       return 3;
999
1000     case CONST_DOUBLE:
1001       return 20;
1002
1003     default:
1004       return 4;
1005     }
1006 }
1007 \f
1008 /* Documentation for the machine specific operand escapes:
1009
1010    'A' print rn in h8/300 mode, erN in H8/300H mode
1011    'C' print (operand - 2).
1012    'E' like s but negative.
1013    'F' like t but negative.
1014    'G' constant just the negative
1015    'M' turn a 'M' constant into its negative mod 2.
1016    'P' if operand is incing/decing sp, print .w, otherwise .b.
1017    'R' print operand as a byte:8 address if appropriate, else fall back to
1018        'X' handling.
1019    'S' print operand as a long word
1020    'T' print operand as a word
1021    'U' if operand is incing/decing sp, print l, otherwise nothing.
1022    'V' find the set bit, and print its number.
1023    'W' find the clear bit, and print its number.
1024    'X' print operand as a byte
1025    'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1026        If this operand isn't a register, fall back to 'R' handling.
1027    'Z' print int & 7.
1028    'b' print the bit opcode
1029    'c' print the ibit opcode
1030    'd' bcc if EQ, bcs if NE
1031    'e' first word of 32 bit value - if reg, then least reg. if mem
1032        then least. if const then most sig word
1033    'f' second word of 32 bit value - if reg, then biggest reg. if mem
1034        then +2. if const then least sig word
1035    'g' bcs if EQ, bcc if NE
1036    'j' print operand as condition code.
1037    'k' print operand as reverse condition code.
1038    's' print as low byte of 16 bit value
1039    't' print as high byte of 16 bit value
1040    'w' print as low byte of 32 bit value
1041    'x' print as 2nd byte of 32 bit value
1042    'y' print as 3rd byte of 32 bit value
1043    'z' print as msb of 32 bit value
1044 */
1045
1046 /* Return assembly language string which identifies a comparison type.  */
1047
1048 static const char *
1049 cond_string (code)
1050      enum rtx_code code;
1051 {
1052   switch (code)
1053     {
1054     case NE:
1055       return "ne";
1056     case EQ:
1057       return "eq";
1058     case GE:
1059       return "ge";
1060     case GT:
1061       return "gt";
1062     case LE:
1063       return "le";
1064     case LT:
1065       return "lt";
1066     case GEU:
1067       return "hs";
1068     case GTU:
1069       return "hi";
1070     case LEU:
1071       return "ls";
1072     case LTU:
1073       return "lo";
1074     default:
1075       abort ();
1076     }
1077 }
1078
1079 /* Print operand X using operand code CODE to assembly language output file
1080    FILE.  */
1081
1082 void
1083 print_operand (file, x, code)
1084      FILE *file;
1085      rtx x;
1086      int code;
1087 {
1088   /* This is used for communication between the 'P' and 'U' codes.  */
1089   static const char *last_p;
1090
1091   /* This is used for communication between codes V,W,Z and Y.  */
1092   static int bitint;
1093
1094   switch (code)
1095     {
1096     case 'A':
1097       if (GET_CODE (x) == REG)
1098         fprintf (file, "%s", h8_reg_names[REGNO (x)]);
1099       else
1100         goto def;
1101       break;
1102     case 'C':
1103       fprintf (file, "#%d", INTVAL (x) - 2);
1104       break;
1105     case 'E':
1106       switch (GET_CODE (x))
1107         {
1108         case REG:
1109           fprintf (file, "%sl", names_big[REGNO (x)]);
1110           break;
1111         case CONST_INT:
1112           fprintf (file, "#%d", (-INTVAL (x)) & 0xff);
1113           break;
1114         default:
1115           abort ();
1116         }
1117       break;
1118     case 'F':
1119       switch (GET_CODE (x))
1120         {
1121         case REG:
1122           fprintf (file, "%sh", names_big[REGNO (x)]);
1123           break;
1124         case CONST_INT:
1125           fprintf (file, "#%d", ((-INTVAL (x)) & 0xff00) >> 8);
1126           break;
1127         default:
1128           abort ();
1129         }
1130       break;
1131     case 'G':
1132       if (GET_CODE (x) != CONST_INT)
1133         abort ();
1134       fprintf (file, "#%d", 0xff & (-INTVAL (x)));
1135       break;
1136     case 'M':
1137       /* For 3/-3 and 4/-4, the other 2 is handled separately.  */
1138       switch (INTVAL (x))
1139         {
1140         case 2:
1141         case 4:
1142         case -2:
1143         case -4:
1144           fprintf (file, "#2");
1145           break;
1146         case 1:
1147         case 3:
1148         case -1:
1149         case -3:
1150           fprintf (file, "#1");
1151           break;
1152         default:
1153           abort ();
1154         }
1155       break;
1156     case 'P':
1157       if (REGNO (XEXP (XEXP (x, 0), 0)) == STACK_POINTER_REGNUM)
1158         {
1159           last_p = "";
1160           fprintf (file, ".w");
1161         }
1162       else
1163         {
1164           last_p = "l";
1165           fprintf (file, ".b");
1166         }
1167       break;
1168     case 'S':
1169       if (GET_CODE (x) == REG)
1170         fprintf (file, "%s", names_extended[REGNO (x)]);
1171       else
1172         goto def;
1173       break;
1174     case 'T':
1175       if (GET_CODE (x) == REG)
1176         fprintf (file, "%s", names_big[REGNO (x)]);
1177       else
1178         goto def;
1179       break;
1180     case 'U':
1181       fprintf (file, "%s%s", names_big[REGNO (x)], last_p);
1182       break;
1183     case 'V':
1184       bitint = exact_log2 (INTVAL (x));
1185       if (bitint == -1)
1186         abort ();
1187       fprintf (file, "#%d", bitint & 7);
1188       break;
1189     case 'W':
1190       bitint = exact_log2 ((~INTVAL (x)) & 0xff);
1191       if (bitint == -1)
1192         abort ();
1193       fprintf (file, "#%d", bitint & 7);
1194       break;
1195     case 'R':
1196     case 'X':
1197       if (GET_CODE (x) == REG)
1198         fprintf (file, "%s", byte_reg (x, 0));
1199       else
1200         goto def;
1201       break;
1202     case 'Y':
1203       if (bitint == -1)
1204         abort ();
1205       if (GET_CODE (x) == REG)
1206         fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1207       else
1208         print_operand (file, x, 'R');
1209       bitint = -1;
1210       break;
1211     case 'Z':
1212       bitint = INTVAL (x);
1213       fprintf (file, "#%d", bitint & 7);
1214       break;
1215     case 'b':
1216       switch (GET_CODE (x))
1217         {
1218         case IOR:
1219           fprintf (file, "bor");
1220           break;
1221         case XOR:
1222           fprintf (file, "bxor");
1223           break;
1224         case AND:
1225           fprintf (file, "band");
1226           break;
1227         default:
1228           break;
1229         }
1230       break;
1231     case 'c':
1232       switch (GET_CODE (x))
1233         {
1234         case IOR:
1235           fprintf (file, "bior");
1236           break;
1237         case XOR:
1238           fprintf (file, "bixor");
1239           break;
1240         case AND:
1241           fprintf (file, "biand");
1242           break;
1243         default:
1244           break;
1245         }
1246       break;
1247     case 'd':
1248       switch (GET_CODE (x))
1249         {
1250         case EQ:
1251           fprintf (file, "bcc");
1252           break;
1253         case NE:
1254           fprintf (file, "bcs");
1255           break;
1256         default:
1257           abort ();
1258         }
1259       break;
1260     case 'e':
1261       switch (GET_CODE (x))
1262         {
1263         case REG:
1264           if (TARGET_H8300)
1265             fprintf (file, "%s", names_big[REGNO (x)]);
1266           else
1267             fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1268           break;
1269         case MEM:
1270           x = adj_offsettable_operand (x, 0);
1271           print_operand (file, x, 0);
1272           break;
1273         case CONST_INT:
1274           fprintf (file, "#%d", ((INTVAL (x) >> 16) & 0xffff));
1275           break;
1276         case CONST_DOUBLE:
1277           {
1278             long val;
1279             REAL_VALUE_TYPE rv;
1280             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1281             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1282             fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1283             break;
1284           }
1285         default:
1286           abort ();
1287           break;
1288         }
1289       break;
1290     case 'f':
1291       switch (GET_CODE (x))
1292         {
1293         case REG:
1294           if (TARGET_H8300)
1295             fprintf (file, "%s", names_big[REGNO (x) + 1]);
1296           else
1297             fprintf (file, "%s", names_big[REGNO (x)]);
1298           break;
1299         case MEM:
1300           x = adj_offsettable_operand (x, 2);
1301           print_operand (file, x, 0);
1302           break;
1303         case CONST_INT:
1304           fprintf (file, "#%d", INTVAL (x) & 0xffff);
1305           break;
1306         case CONST_DOUBLE:
1307           {
1308             long val;
1309             REAL_VALUE_TYPE rv;
1310             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1311             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1312             fprintf (file, "#%ld", (val & 0xffff));
1313             break;
1314           }
1315         default:
1316           abort ();
1317         }
1318       break;
1319     case 'g':
1320       switch (GET_CODE (x))
1321         {
1322         case NE:
1323           fprintf (file, "bcc");
1324           break;
1325         case EQ:
1326           fprintf (file, "bcs");
1327           break;
1328         default:
1329           abort ();
1330         }
1331       break;
1332     case 'j':
1333       asm_fprintf (file, cond_string (GET_CODE (x)));
1334       break;
1335     case 'k':
1336       asm_fprintf (file, cond_string (reverse_condition (GET_CODE (x))));
1337       break;
1338     case 's':
1339       if (GET_CODE (x) == CONST_INT)
1340         fprintf (file, "#%d", (INTVAL (x)) & 0xff);
1341       else
1342         fprintf (file, "%s", byte_reg (x, 0));
1343       break;
1344     case 't':
1345       if (GET_CODE (x) == CONST_INT)
1346         fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1347       else
1348         fprintf (file, "%s", byte_reg (x, 1));
1349       break;
1350     case 'u':
1351       if (GET_CODE (x) != CONST_INT)
1352         abort ();
1353       fprintf (file, "%d", INTVAL (x));
1354       break;
1355     case 'w':
1356       if (GET_CODE (x) == CONST_INT)
1357         fprintf (file, "#%d", INTVAL (x) & 0xff);
1358       else
1359         fprintf (file, "%s",
1360                  byte_reg (x, TARGET_H8300 ? 2 : 0));
1361       break;
1362     case 'x':
1363       if (GET_CODE (x) == CONST_INT)
1364         fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1365       else
1366         fprintf (file, "%s",
1367                  byte_reg (x, TARGET_H8300 ? 3 : 1));
1368       break;
1369     case 'y':
1370       if (GET_CODE (x) == CONST_INT)
1371         fprintf (file, "#%d", (INTVAL (x) >> 16) & 0xff);
1372       else
1373         fprintf (file, "%s", byte_reg (x, 0));
1374       break;
1375     case 'z':
1376       if (GET_CODE (x) == CONST_INT)
1377         fprintf (file, "#%d", (INTVAL (x) >> 24) & 0xff);
1378       else
1379         fprintf (file, "%s", byte_reg (x, 1));
1380       break;
1381
1382     default:
1383     def:
1384       switch (GET_CODE (x))
1385         {
1386         case REG:
1387           switch (GET_MODE (x))
1388             {
1389             case QImode:
1390 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1391               fprintf (file, "%s", byte_reg (x, 0));
1392 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1393               fprintf (file, "%s", names_big[REGNO (x)]);
1394 #endif
1395               break;
1396             case HImode:
1397               fprintf (file, "%s", names_big[REGNO (x)]);
1398               break;
1399             case SImode:
1400             case SFmode:
1401               fprintf (file, "%s", names_extended[REGNO (x)]);
1402               break;
1403             default:
1404               abort ();
1405             }
1406           break;
1407
1408         case MEM:
1409           fprintf (file, "@");
1410           output_address (XEXP (x, 0));
1411
1412           /* If this is an 'R' operand (reference into the 8-bit
1413              area), then specify a symbolic address as "foo:8",
1414              otherwise if operand is still in eight bit section, use
1415              "foo:16".  */
1416           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1417               && SYMBOL_REF_FLAG (XEXP (x, 0)))
1418             fprintf (file, (code == 'R' ? ":8" : ":16"));
1419           else if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1420                    && TINY_DATA_NAME_P (XSTR (XEXP (x, 0), 0)))
1421             fprintf (file, ":16");
1422           else if ((code == 'R')
1423                    && EIGHTBIT_CONSTANT_ADDRESS_P (XEXP (x, 0)))
1424             fprintf (file, ":8");
1425           break;
1426
1427         case CONST_INT:
1428         case SYMBOL_REF:
1429         case CONST:
1430         case LABEL_REF:
1431           fprintf (file, "#");
1432           print_operand_address (file, x);
1433           break;
1434         case CONST_DOUBLE:
1435           {
1436             long val;
1437             REAL_VALUE_TYPE rv;
1438             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1439             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1440             fprintf (file, "#%ld", val);
1441             break;
1442           }
1443         default:
1444           break;
1445         }
1446     }
1447 }
1448
1449 /* Output assembly language output for the address ADDR to FILE.  */
1450
1451 void
1452 print_operand_address (file, addr)
1453      FILE *file;
1454      rtx addr;
1455 {
1456   switch (GET_CODE (addr))
1457     {
1458     case REG:
1459       fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1460       break;
1461
1462     case PRE_DEC:
1463       fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1464       break;
1465
1466     case POST_INC:
1467       fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1468       break;
1469
1470     case PLUS:
1471       fprintf (file, "(");
1472       if (GET_CODE (XEXP (addr, 0)) == REG)
1473         {
1474           /* reg,foo */
1475           print_operand_address (file, XEXP (addr, 1));
1476           fprintf (file, ",");
1477           print_operand_address (file, XEXP (addr, 0));
1478         }
1479       else
1480         {
1481           /* foo+k */
1482           print_operand_address (file, XEXP (addr, 0));
1483           fprintf (file, "+");
1484           print_operand_address (file, XEXP (addr, 1));
1485         }
1486       fprintf (file, ")");
1487       break;
1488
1489     case CONST_INT:
1490       {
1491         /* Since the h8/300 only has 16 bit pointers, negative values are also
1492            those >= 32768.  This happens for example with pointer minus a
1493            constant.  We don't want to turn (char *p - 2) into
1494            (char *p + 65534) because loop unrolling can build upon this
1495            (IE: char *p + 131068).  */
1496         int n = INTVAL (addr);
1497         if (TARGET_H8300)
1498           n = (int) (short) n;
1499         if (n < 0)
1500           /* ??? Why the special case for -ve values?  */
1501           fprintf (file, "-%d", -n);
1502         else
1503           fprintf (file, "%d", n);
1504         break;
1505       }
1506
1507     default:
1508       output_addr_const (file, addr);
1509       break;
1510     }
1511 }
1512 \f
1513 /* Output all insn addresses and their sizes into the assembly language
1514    output file.  This is helpful for debugging whether the length attributes
1515    in the md file are correct.  This is not meant to be a user selectable
1516    option.  */
1517
1518 void
1519 final_prescan_insn (insn, operand, num_operands)
1520      rtx insn, *operand ATTRIBUTE_UNUSED;
1521      int num_operands ATTRIBUTE_UNUSED;
1522 {
1523   /* This holds the last insn address.  */
1524   static int last_insn_address = 0;
1525
1526   int uid = INSN_UID (insn);
1527
1528   if (TARGET_RTL_DUMP)
1529     {
1530       fprintf (asm_out_file, "\n****************");
1531       print_rtl (asm_out_file, PATTERN (insn));
1532       fprintf (asm_out_file, "\n");
1533     }
1534
1535   if (TARGET_ADDRESSES)
1536     {
1537       fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1538                INSN_ADDRESSES (uid) - last_insn_address);
1539       last_insn_address = INSN_ADDRESSES (uid);
1540     }
1541 }
1542
1543 /* Prepare for an SI sized move.  */
1544
1545 int
1546 do_movsi (operands)
1547      rtx operands[];
1548 {
1549   rtx src = operands[1];
1550   rtx dst = operands[0];
1551   if (!reload_in_progress && !reload_completed)
1552     {
1553       if (!register_operand (dst, GET_MODE (dst)))
1554         {
1555           rtx tmp = gen_reg_rtx (GET_MODE (dst));
1556           emit_move_insn (tmp, src);
1557           operands[1] = tmp;
1558         }
1559     }
1560   return 0;
1561 }
1562
1563 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1564    Define the offset between two registers, one to be eliminated, and
1565    the other its replacement, at the start of a routine.  */
1566
1567 int
1568 initial_offset (from, to)
1569      int from, to;
1570 {
1571   int offset = 0;
1572
1573   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1574     offset = UNITS_PER_WORD + frame_pointer_needed * UNITS_PER_WORD;
1575   else
1576     {
1577       int regno;
1578
1579       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1580         if (WORD_REG_USED (regno))
1581           offset += UNITS_PER_WORD;
1582
1583       /* See the comments for get_frame_size.  We need to round it up to
1584          STACK_BOUNDARY.  */
1585
1586       offset += ((get_frame_size () + STACK_BOUNDARY / BITS_PER_UNIT - 1)
1587                  & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
1588
1589       if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1590         offset += UNITS_PER_WORD;       /* Skip saved PC */
1591     }
1592   return offset;
1593 }
1594
1595 /* Update the condition code from the insn.  */
1596
1597 void
1598 notice_update_cc (body, insn)
1599      rtx body;
1600      rtx insn;
1601 {
1602   switch (get_attr_cc (insn))
1603     {
1604     case CC_NONE:
1605       /* Insn does not affect CC at all.  */
1606       break;
1607
1608     case CC_NONE_0HIT:
1609       /* Insn does not change CC, but the 0'th operand has been changed.  */
1610       if (cc_status.value1 != 0
1611           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1612         cc_status.value1 = 0;
1613       break;
1614
1615     case CC_SET_ZN:
1616       /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1617          The V flag is unusable.  The C flag may or may not be known but
1618          that's ok because alter_cond will change tests to use EQ/NE.  */
1619       CC_STATUS_INIT;
1620       cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1621       cc_status.value1 = recog_data.operand[0];
1622       break;
1623
1624     case CC_SET_ZNV:
1625       /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1626          The C flag may or may not be known but that's ok because
1627          alter_cond will change tests to use EQ/NE.  */
1628       CC_STATUS_INIT;
1629       cc_status.flags |= CC_NO_CARRY;
1630       cc_status.value1 = recog_data.operand[0];
1631       break;
1632
1633     case CC_COMPARE:
1634       /* The insn is a compare instruction.  */
1635       CC_STATUS_INIT;
1636       cc_status.value1 = SET_SRC (body);
1637       break;
1638
1639     case CC_CLOBBER:
1640       /* Insn doesn't leave CC in a usable state.  */
1641       CC_STATUS_INIT;
1642       break;
1643     }
1644 }
1645
1646 /* Recognize valid operators for bit instructions.  */
1647
1648 int
1649 bit_operator (x, mode)
1650      rtx x;
1651      enum machine_mode mode ATTRIBUTE_UNUSED;
1652 {
1653   enum rtx_code code = GET_CODE (x);
1654
1655   return (code == XOR
1656           || code == AND
1657           || code == IOR);
1658 }
1659 \f
1660 /* Shifts.
1661
1662    We devote a fair bit of code to getting efficient shifts since we can only
1663    shift one bit at a time on the H8/300 and H8/300H and only one or two
1664    bits at a time on the H8/S.
1665
1666    The basic shift methods:
1667
1668      * loop shifts -- emit a loop using one (or two on H8/S) bit shifts;
1669      this is the default.  SHIFT_LOOP
1670
1671      * inlined shifts -- emit straight line code for the shift; this is
1672      used when a straight line shift is about the same size or smaller
1673      than a loop.  We allow the inline version to be slightly longer in
1674      some cases as it saves a register.  SHIFT_INLINE
1675
1676      * rotate + and -- rotate the value the opposite direction, then
1677      mask off the values we don't need.  This is used when only a few
1678      of the bits in the original value will survive in the shifted value.
1679      Again, this is used when it's about the same size or smaller than
1680      a loop.  We allow this version to be slightly longer as it is usually
1681      much faster than a loop.  SHIFT_ROT_AND
1682
1683      * swap (+ shifts) -- often it's possible to swap bytes/words to
1684      simulate a shift by 8/16.  Once swapped a few inline shifts can be
1685      added if the shift count is slightly more than 8 or 16.  This is used
1686      when it's about the same size or smaller than a loop.  We allow this
1687      version to be slightly longer as it is usually much faster than a loop.
1688      SHIFT_SPECIAL
1689
1690      * There other oddballs.  Not worth explaining.  SHIFT_SPECIAL
1691
1692    Here are some thoughts on what the absolutely positively best code is.
1693    "Best" here means some rational trade-off between code size and speed,
1694    where speed is more preferred but not at the expense of generating 20 insns.
1695
1696    A trailing '*' after the shift count indicates the "best" mode isn't
1697    implemented.
1698    
1699    H8/300 QImode shifts
1700    1-4    - do them inline
1701    5-6    - ASHIFT | LSHIFTRT: rotate, mask off other bits
1702             ASHIFTRT: loop
1703    7      - ASHIFT | LSHIFTRT: rotate, mask off other bits
1704             ASHIFTRT: shll, subx (propagate carry bit to all bits)
1705
1706    H8/300 HImode shifts
1707    1-4    - do them inline
1708    5-6    - loop
1709    7      - shift 2nd half other way into carry.
1710             copy 1st half into 2nd half
1711             rotate 2nd half other way with carry
1712             rotate 1st half other way (no carry)
1713             mask off bits in 1st half (ASHIFT | LSHIFTRT).
1714             sign extend 1st half (ASHIFTRT)
1715    8      - move byte, zero (ASHIFT | LSHIFTRT) or sign extend other (ASHIFTRT)
1716    9-12   - do shift by 8, inline remaining shifts
1717    13-14* - ASHIFT | LSHIFTRT: rotate 3/2, mask, move byte, set other byte to 0
1718           - ASHIFTRT: loop
1719    15     - ASHIFT | LSHIFTRT: rotate 1, mask, move byte, set other byte to 0
1720           - ASHIFTRT: shll, subx, set other byte
1721
1722    H8/300 SImode shifts
1723    1-2    - do them inline
1724    3-6    - loop
1725    7*     - shift other way once, move bytes into place,
1726             move carry into place (possibly with sign extension)
1727    8      - move bytes into place, zero or sign extend other
1728    9-14   - loop
1729    15*    - shift other way once, move word into place, move carry into place
1730    16     - move word, zero or sign extend other
1731    17-23  - loop
1732    24*    - move bytes into place, zero or sign extend other
1733    25-27  - loop
1734    28-30* - ASHIFT | LSHIFTRT: rotate top byte, mask, move byte into place,
1735                                zero others
1736             ASHIFTRT: loop
1737    31     - ASHIFT | LSHIFTRT: rotate top byte, mask, move byte into place,
1738                                zero others
1739             ASHIFTRT: shll top byte, subx, copy to other bytes
1740
1741    H8/300H QImode shifts (same as H8/300 QImode shifts)
1742    1-4    - do them inline
1743    5-6    - ASHIFT | LSHIFTRT: rotate, mask off other bits
1744             ASHIFTRT: loop
1745    7      - ASHIFT | LSHIFTRT: rotate, mask off other bits
1746             ASHIFTRT: shll, subx (propagate carry bit to all bits)
1747
1748    H8/300H HImode shifts
1749    1-4    - do them inline
1750    5-6    - loop
1751    7      - shift 2nd half other way into carry.
1752             copy 1st half into 2nd half
1753             rotate entire word other way using carry
1754             mask off remaining bits  (ASHIFT | LSHIFTRT)
1755             sign extend remaining bits (ASHIFTRT)
1756    8      - move byte, zero (ASHIFT | LSHIFTRT) or sign extend other (ASHIFTRT)
1757    9-12   - do shift by 8, inline remaining shifts
1758    13-14  - ASHIFT | LSHIFTRT: rotate 3/2, mask, move byte, set other byte to 0
1759           - ASHIFTRT: loop
1760    15     - ASHIFT | LSHIFTRT: rotate 1, mask, move byte, set other byte to 0
1761           - ASHIFTRT: shll, subx, set other byte
1762
1763    H8/300H SImode shifts
1764    (These are complicated by the fact that we don't have byte level access to
1765    the top word.)
1766    A word is: bytes 3,2,1,0 (msb -> lsb), word 1,0 (msw -> lsw)
1767    1-4    - do them inline
1768    5-14   - loop
1769    15*    - shift other way once, move word into place, move carry into place
1770             (with sign extension for ASHIFTRT)
1771    16     - move word into place, zero or sign extend other
1772    17-20  - do 16bit shift, then inline remaining shifts
1773    20-23  - loop
1774    24*    - ASHIFT: move byte 0(msb) to byte 1, zero byte 0,
1775                     move word 0 to word 1, zero word 0
1776             LSHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1777                       zero word 1, zero byte 1
1778             ASHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1779                       sign extend byte 0, sign extend word 0
1780    25-27* - either loop, or
1781             do 24 bit shift, inline rest
1782    28-30  - ASHIFT: rotate 4/3/2, mask
1783             LSHIFTRT: rotate 4/3/2, mask
1784             ASHIFTRT: loop
1785    31     - shll, subx byte 0, sign extend byte 0, sign extend word 0
1786
1787    H8/S QImode shifts
1788    1-6    - do them inline
1789    7      - ASHIFT | LSHIFTRT: rotate, mask off other bits
1790             ASHIFTRT: shll, subx (propagate carry bit to all bits)
1791
1792    H8/S HImode shifts
1793    1-7    - do them inline
1794    8      - move byte, zero (ASHIFT | LSHIFTRT) or sign extend other (ASHIFTRT)
1795    9-12   - do shift by 8, inline remaining shifts
1796    13-14  - ASHIFT | LSHIFTRT: rotate 3/2, mask, move byte, set other byte to 0
1797           - ASHIFTRT: loop
1798    15     - ASHIFT | LSHIFTRT: rotate 1, mask, move byte, set other byte to 0
1799           - ASHIFTRT: shll, subx, set other byte
1800
1801    H8/S SImode shifts
1802    (These are complicated by the fact that we don't have byte level access to
1803    the top word.)
1804    A word is: bytes 3,2,1,0 (msb -> lsb), word 1,0 (msw -> lsw)
1805    1-10   - do them inline
1806    11-14  - loop
1807    15*    - shift other way once, move word into place, move carry into place
1808             (with sign extension for ASHIFTRT)
1809    16     - move word into place, zero or sign extend other
1810    17-20  - do 16bit shift, then inline remaining shifts
1811    20-23  - loop
1812    24*    - ASHIFT: move byte 0(msb) to byte 1, zero byte 0,
1813                     move word 0 to word 1, zero word 0
1814             LSHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1815                       zero word 1, zero byte 1
1816             ASHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1817                       sign extend byte 0, sign extend word 0
1818    25-27* - either loop, or
1819             do 24 bit shift, inline rest
1820    28-30  - ASHIFT: rotate 4/3/2, mask
1821             LSHIFTRT: rotate 4/3/2, mask
1822             ASHIFTRT: loop
1823    31     - shll, subx byte 0, sign extend byte 0, sign extend word 0
1824
1825    Panic!!!  */
1826
1827 int
1828 nshift_operator (x, mode)
1829      rtx x;
1830      enum machine_mode mode ATTRIBUTE_UNUSED;
1831 {
1832   switch (GET_CODE (x))
1833     {
1834     case ASHIFTRT:
1835     case LSHIFTRT:
1836     case ASHIFT:
1837       return 1;
1838
1839     default:
1840       return 0;
1841     }
1842 }
1843
1844 /* Called from the .md file to emit code to do shifts.
1845    Returns a boolean indicating success
1846    (currently this is always TRUE).  */
1847
1848 int
1849 expand_a_shift (mode, code, operands)
1850      enum machine_mode mode;
1851      int code;
1852      rtx operands[];
1853 {
1854   emit_move_insn (operands[0], operands[1]);
1855
1856   /* Need a loop to get all the bits we want  - we generate the
1857      code at emit time, but need to allocate a scratch reg now.  */
1858
1859   emit_insn (gen_rtx_PARALLEL
1860              (VOIDmode,
1861               gen_rtvec (2,
1862                          gen_rtx_SET (VOIDmode, operands[0],
1863                                       gen_rtx (code, mode, operands[0],
1864                                                operands[2])),
1865                          gen_rtx_CLOBBER (VOIDmode,
1866                                           gen_rtx_SCRATCH (QImode)))));
1867
1868   return 1;
1869 }
1870
1871 /* Shift algorithm determination.
1872
1873    There are various ways of doing a shift:
1874    SHIFT_INLINE: If the amount is small enough, just generate as many one-bit
1875                  shifts as we need.
1876    SHIFT_ROT_AND: If the amount is large but close to either end, rotate the
1877                   necessary bits into position and then set the rest to zero.
1878    SHIFT_SPECIAL: Hand crafted assembler.
1879    SHIFT_LOOP:    If the above methods fail, just loop.  */
1880
1881 enum shift_alg
1882 {
1883   SHIFT_INLINE,
1884   SHIFT_ROT_AND,
1885   SHIFT_SPECIAL,
1886   SHIFT_LOOP,
1887   SHIFT_MAX
1888 };
1889
1890 /* Symbols of the various shifts which can be used as indices.  */
1891
1892 enum shift_type
1893   {
1894     SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
1895   };
1896
1897 /* Symbols of the various modes which can be used as indices.  */
1898
1899 enum shift_mode
1900   {
1901     QIshift, HIshift, SIshift
1902   };
1903
1904 /* For single bit shift insns, record assembler and what bits of the
1905    condition code are valid afterwards (represented as various CC_FOO
1906    bits, 0 means CC isn't left in a usable state).  */
1907
1908 struct shift_insn
1909 {
1910   const char *assembler;
1911   int cc_valid;
1912 };
1913
1914 /* Assembler instruction shift table.
1915
1916    These tables are used to look up the basic shifts.
1917    They are indexed by cpu, shift_type, and mode.  */
1918
1919 static const struct shift_insn shift_one[2][3][3] =
1920 {
1921 /* H8/300 */
1922   {
1923 /* SHIFT_ASHIFT */
1924     {
1925       { "shll\t%X0", CC_NO_CARRY },
1926       { "add.w\t%T0,%T0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1927       { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", 0 }
1928     },
1929 /* SHIFT_LSHIFTRT */
1930     {
1931       { "shlr\t%X0", CC_NO_CARRY },
1932       { "shlr\t%t0\n\trotxr\t%s0", 0 },
1933       { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", 0 }
1934     },
1935 /* SHIFT_ASHIFTRT */
1936     {
1937       { "shar\t%X0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1938       { "shar\t%t0\n\trotxr\t%s0", 0 },
1939       { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", 0 }
1940     }
1941   },
1942 /* H8/300H */
1943   {
1944 /* SHIFT_ASHIFT */
1945     {
1946       { "shll.b\t%X0", CC_NO_CARRY },
1947       { "shll.w\t%T0", CC_NO_CARRY },
1948       { "shll.l\t%S0", CC_NO_CARRY }
1949     },
1950 /* SHIFT_LSHIFTRT */
1951     {
1952       { "shlr.b\t%X0", CC_NO_CARRY },
1953       { "shlr.w\t%T0", CC_NO_CARRY },
1954       { "shlr.l\t%S0", CC_NO_CARRY }
1955     },
1956 /* SHIFT_ASHIFTRT */
1957     {
1958       { "shar.b\t%X0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1959       { "shar.w\t%T0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1960       { "shar.l\t%S0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY }
1961     }
1962   }
1963 };
1964
1965 static const struct shift_insn shift_two[3][3] =
1966 {
1967 /* SHIFT_ASHIFT */
1968     {
1969       { "shll.b\t#2,%X0", CC_NO_CARRY },
1970       { "shll.w\t#2,%T0", CC_NO_CARRY },
1971       { "shll.l\t#2,%S0", CC_NO_CARRY }
1972     },
1973 /* SHIFT_LSHIFTRT */
1974     {
1975       { "shlr.b\t#2,%X0", CC_NO_CARRY },
1976       { "shlr.w\t#2,%T0", CC_NO_CARRY },
1977       { "shlr.l\t#2,%S0", CC_NO_CARRY }
1978     },
1979 /* SHIFT_ASHIFTRT */
1980     {
1981       { "shar.b\t#2,%X0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1982       { "shar.w\t#2,%T0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1983       { "shar.l\t#2,%S0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY }
1984     }
1985 };
1986
1987 /* Rotates are organized by which shift they'll be used in implementing.
1988    There's no need to record whether the cc is valid afterwards because
1989    it is the AND insn that will decide this.  */
1990
1991 static const char *const rotate_one[2][3][3] =
1992 {
1993 /* H8/300 */
1994   {
1995 /* SHIFT_ASHIFT */
1996     {
1997       "rotr\t%X0",
1998       "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
1999       0
2000     },
2001 /* SHIFT_LSHIFTRT */
2002     {
2003       "rotl\t%X0",
2004       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
2005       0
2006     },
2007 /* SHIFT_ASHIFTRT */
2008     {
2009       "rotl\t%X0",
2010       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
2011       0
2012     }
2013   },
2014 /* H8/300H */
2015   {
2016 /* SHIFT_ASHIFT */
2017     {
2018       "rotr.b\t%X0",
2019       "rotr.w\t%T0",
2020       "rotr.l\t%S0"
2021     },
2022 /* SHIFT_LSHIFTRT */
2023     {
2024       "rotl.b\t%X0",
2025       "rotl.w\t%T0",
2026       "rotl.l\t%S0"
2027     },
2028 /* SHIFT_ASHIFTRT */
2029     {
2030       "rotl.b\t%X0",
2031       "rotl.w\t%T0",
2032       "rotl.l\t%S0"
2033     }
2034   }
2035 };
2036
2037 static const char *const rotate_two[3][3] =
2038 {
2039 /* SHIFT_ASHIFT */
2040     {
2041       "rotr.b\t#2,%X0",
2042       "rotr.w\t#2,%T0",
2043       "rotr.l\t#2,%S0"
2044     },
2045 /* SHIFT_LSHIFTRT */
2046     {
2047       "rotl.b\t#2,%X0",
2048       "rotl.w\t#2,%T0",
2049       "rotl.l\t#2,%S0"
2050     },
2051 /* SHIFT_ASHIFTRT */
2052     {
2053       "rotl.b\t#2,%X0",
2054       "rotl.w\t#2,%T0",
2055       "rotl.l\t#2,%S0"
2056     }
2057 };
2058
2059 static enum shift_alg get_shift_alg PARAMS ((enum attr_cpu, enum shift_type,
2060                                              enum machine_mode, int,
2061                                              const char **, const char **,
2062                                              int *));
2063
2064 /* Given CPU, MODE, SHIFT_TYPE, and shift count COUNT, determine the best
2065    algorithm for doing the shift.  The assembler code is stored in ASSEMBLER.
2066    We don't achieve maximum efficiency in all cases, but the hooks are here
2067    to do so.
2068
2069    For now we just use lots of switch statements.  Since we don't even come
2070    close to supporting all the cases, this is simplest.  If this function ever
2071    gets too big, perhaps resort to a more table based lookup.  Of course,
2072    at this point you may just wish to do it all in rtl.
2073
2074    WARNING: The constraints on insns shiftbyn_QI/HI/SI assume shifts of
2075    1,2,3,4 will be inlined (1,2 for SI).  */
2076
2077 static enum shift_alg
2078 get_shift_alg (cpu, shift_type, mode, count, assembler_p,
2079                assembler2_p, cc_valid_p)
2080      enum attr_cpu cpu;
2081      enum shift_type shift_type;
2082      enum machine_mode mode;
2083      int count;
2084      const char **assembler_p;
2085      const char **assembler2_p;
2086      int *cc_valid_p;
2087 {
2088   /* The default is to loop.  */
2089   enum shift_alg alg = SHIFT_LOOP;
2090   enum shift_mode shift_mode;
2091
2092   /* We don't handle negative shifts or shifts greater than the word size,
2093      they should have been handled already.  */
2094
2095   if (count < 0 || count > GET_MODE_BITSIZE (mode))
2096     abort ();
2097
2098   switch (mode)
2099     {
2100     case QImode:
2101       shift_mode = QIshift;
2102       break;
2103     case HImode:
2104       shift_mode = HIshift;
2105       break;
2106     case SImode:
2107       shift_mode = SIshift;
2108       break;
2109     default:
2110       abort ();
2111     }
2112
2113   /* Assume either SHIFT_LOOP or SHIFT_INLINE.
2114      It is up to the caller to know that looping clobbers cc.  */
2115   *assembler_p = shift_one[cpu][shift_type][shift_mode].assembler;
2116   if (TARGET_H8300S)
2117     *assembler2_p = shift_two[shift_type][shift_mode].assembler;
2118   else
2119     *assembler2_p = NULL;
2120   *cc_valid_p = shift_one[cpu][shift_type][shift_mode].cc_valid;
2121
2122   /* Now look for cases we want to optimize.  */
2123
2124   switch (shift_mode)
2125     {
2126     case QIshift:
2127       if (count <= 4)
2128         return SHIFT_INLINE;
2129       else
2130         {
2131           /* Shift by 5/6 are only 3 insns on the H8/S, so it's just as
2132              fast as SHIFT_ROT_AND, plus CC is valid.  */
2133           if (TARGET_H8300S && count <= 6)
2134             return SHIFT_INLINE;
2135
2136           /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
2137              through the entire value.  */
2138           if (shift_type == SHIFT_ASHIFTRT && count == 7)
2139             {
2140               *assembler_p = "shll\t%X0\n\tsubx\t%X0,%X0";
2141               *cc_valid_p = 0;
2142               return SHIFT_SPECIAL;
2143             }
2144
2145           /* Other ASHIFTRTs are too much of a pain.  */
2146           if (shift_type == SHIFT_ASHIFTRT)
2147             return SHIFT_LOOP;
2148
2149           /* Other shifts by 5, 6, or 7 bits use SHIFT_ROT_AND.  */
2150           *assembler_p = rotate_one[cpu][shift_type][shift_mode];
2151           if (TARGET_H8300S)
2152             *assembler2_p = rotate_two[shift_type][shift_mode];
2153           *cc_valid_p = 0;
2154           return SHIFT_ROT_AND;
2155         }
2156
2157     case HIshift:
2158       if (count <= 4)
2159         return SHIFT_INLINE;
2160       else if (TARGET_H8300S && count <= 7)
2161         return SHIFT_INLINE;
2162       else if (count == 7)
2163         {
2164           if (shift_type == SHIFT_ASHIFT && TARGET_H8300)
2165             {
2166               *assembler_p = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.b\t%t0\n\trotr.b\t%s0\n\tand.b\t#0x80,%s0";
2167               *cc_valid_p = 0;
2168               return SHIFT_SPECIAL;
2169             }
2170
2171           if (shift_type == SHIFT_ASHIFT && TARGET_H8300H)
2172             {
2173               *assembler_p = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
2174               *cc_valid_p = 0;
2175               return SHIFT_SPECIAL;
2176             }
2177
2178           if (shift_type == SHIFT_LSHIFTRT && TARGET_H8300)
2179             {
2180               *assembler_p = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\trotl.b\t%t0\n\tand.b\t#0x01,%t0";
2181               *cc_valid_p = 0;
2182               return SHIFT_SPECIAL;
2183             }
2184
2185           if (shift_type == SHIFT_LSHIFTRT && TARGET_H8300H)
2186             {
2187               *assembler_p = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
2188               *cc_valid_p = 0;
2189               return SHIFT_SPECIAL;
2190             }
2191
2192           if (shift_type == SHIFT_ASHIFTRT)
2193             {
2194               *assembler_p = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
2195               *cc_valid_p = 0;
2196               return SHIFT_SPECIAL;
2197             }
2198         }
2199       else if (count == 8)
2200         {
2201           switch (shift_type)
2202             {
2203             case SHIFT_ASHIFT:
2204               *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
2205               *cc_valid_p = 0;
2206               return SHIFT_SPECIAL;
2207             case SHIFT_LSHIFTRT:
2208               *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
2209               *cc_valid_p = 0;
2210               return SHIFT_SPECIAL;
2211             case SHIFT_ASHIFTRT:
2212               if (TARGET_H8300)
2213                 *assembler_p = "mov.b\t%t0,%s0\n\tshll\t%t0\n\tsubx\t%t0,%t0\t";
2214               else
2215                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0";
2216               *cc_valid_p = 0;
2217               return SHIFT_SPECIAL;
2218             }
2219         }
2220       else if (count == 9)
2221         {
2222           switch (shift_type)
2223             {
2224             case SHIFT_ASHIFT:
2225               *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t%t0";
2226               *cc_valid_p = 0;
2227               return SHIFT_SPECIAL;
2228             case SHIFT_LSHIFTRT:
2229               *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t%s0";
2230               *cc_valid_p = 0;
2231               return SHIFT_SPECIAL;
2232             case SHIFT_ASHIFTRT:
2233               if (TARGET_H8300)
2234                 *assembler_p = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0\n\tshar.b\t%s0";
2235               else
2236                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0\n\tshar.b\t%s0";
2237               *cc_valid_p = 0;
2238               return SHIFT_SPECIAL;
2239             }
2240         }
2241       else if (count == 10)
2242         {
2243           switch (shift_type)
2244             {
2245             case SHIFT_ASHIFT:
2246               if (TARGET_H8300S)
2247                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t#2,%t0\n\t";
2248               else
2249                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t%t0\n\tshal.b\t%t0";
2250               *cc_valid_p = 0;
2251               return SHIFT_SPECIAL;
2252             case SHIFT_LSHIFTRT:
2253               if (TARGET_H8300S)
2254                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t#2,%s0";
2255               else
2256                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t%s0\n\tshlr.b\t%s0";
2257               *cc_valid_p = 0;
2258               return SHIFT_SPECIAL;
2259             case SHIFT_ASHIFTRT:
2260               if (TARGET_H8300)
2261                 *assembler_p = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2262               else if (TARGET_H8300H)
2263                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2264               else if (TARGET_H8300S)
2265                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0\n\tshar.b\t#2,%s0";
2266               *cc_valid_p = 0;
2267               return SHIFT_SPECIAL;
2268             }
2269         }
2270       else if (count == 11)
2271         {
2272           switch (shift_type)
2273             {
2274             case SHIFT_ASHIFT:
2275               if (TARGET_H8300S)
2276                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t#2,%t0\n\tshal.b\t%t0";
2277               else
2278                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t%t0\n\tshal.b\t%t0\n\tshal.b\t%t0";
2279               *cc_valid_p = 0;
2280               return SHIFT_SPECIAL;
2281             case SHIFT_LSHIFTRT:
2282               if (TARGET_H8300S)
2283                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t#2,%s0\n\tshlr.b\t%s0";
2284               else
2285                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t%s0\n\tshlr.b\t%s0\n\tshlr.b\t%s0";
2286               *cc_valid_p = 0;
2287               return SHIFT_SPECIAL;
2288             case SHIFT_ASHIFTRT:
2289               if (TARGET_H8300)
2290                 *assembler_p = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2291               else if (TARGET_H8300H)
2292                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2293               else if (TARGET_H8300S)
2294                 *assembler_p = "mov.b\t%t0,%s0\n\texts.w\t%T0\n\tshar.b\t#2,%s0\n\tshar.b\t%s0";
2295               *cc_valid_p = 0;
2296               return SHIFT_SPECIAL;
2297             }
2298         }
2299       else if (count == 12)
2300         {
2301           switch (shift_type)
2302             {
2303             case SHIFT_ASHIFT:
2304               if (TARGET_H8300S)
2305                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t#2,%t0\n\tshal.b\t#2,%t0";
2306               else
2307                 *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tshal.b\t%t0\n\tshal.b\t%t0\n\tshal.b\t%t0\n\tshal.b\t%t0";
2308               *cc_valid_p = 0;
2309               return SHIFT_SPECIAL;
2310             case SHIFT_LSHIFTRT:
2311               if (TARGET_H8300S)
2312                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t#2,%s0\n\tshlr.b\t#2,%s0";
2313               else
2314                 *assembler_p = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0\n\tshlr.b\t%s0\n\tshlr.b\t%s0\n\tshlr.b\t%s0\n\tshlr.b\t%s0";
2315               *cc_valid_p = 0;
2316               return SHIFT_SPECIAL;
2317             case SHIFT_ASHIFTRT:
2318               if (TARGET_H8300)
2319                 *assembler_p = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2320               else if (TARGET_H8300H)
2321                 *assembler_p = "mov.b\t%t0,%s0\n\textw.w\t%T0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0\n\tshar.b\t%s0";
2322               else if (TARGET_H8300S)
2323                 *assembler_p = "mov.b\t%t0,%s0\n\textw.w\t%T0\n\tshar.b\t#2,%s0\n\tshar.b\t#2,%s0";
2324               *cc_valid_p = 0;
2325               return SHIFT_SPECIAL;
2326             }
2327         }
2328       else if ((!TARGET_H8300 && (count == 13 || count == 14))
2329                || count == 15)
2330         {
2331           if (count == 15 && shift_type == SHIFT_ASHIFTRT)
2332             {
2333               *assembler_p = "shll\t%t0,%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
2334               *cc_valid_p = 0;
2335               return SHIFT_SPECIAL;
2336             }
2337           else if (shift_type != SHIFT_ASHIFTRT)
2338             {
2339               *assembler_p = rotate_one[cpu][shift_type][shift_mode];
2340               if (TARGET_H8300S)
2341                 *assembler2_p = rotate_two[shift_type][shift_mode];
2342               else
2343                 *assembler2_p = NULL;
2344               *cc_valid_p = 0;
2345               return SHIFT_ROT_AND;
2346             }
2347         }
2348       break;
2349
2350     case SIshift:
2351       if (count <= (TARGET_H8300 ? 2 : 4))
2352         return SHIFT_INLINE;
2353       else if (TARGET_H8300S && count <= 10)
2354         return SHIFT_INLINE;
2355       else if (count == 8 && TARGET_H8300)
2356         {
2357           switch (shift_type)
2358             {
2359             case SHIFT_ASHIFT:
2360               *assembler_p = "mov.b\t%y0,%z0\n\tmov.b\t%x0,%y0\n\tmov.b\t%w0,%x0\n\tsub.b\t%w0,%w0";
2361               *cc_valid_p = 0;
2362               return SHIFT_SPECIAL;
2363             case SHIFT_LSHIFTRT:
2364               *assembler_p = "mov.b\t%x0,%w0\n\tmov.b\t%y0,%x0\n\tmov.b\t%z0,%y0\n\tsub.b\t%z0,%z0";
2365               *cc_valid_p = 0;
2366               return SHIFT_SPECIAL;
2367             case SHIFT_ASHIFTRT:
2368               *assembler_p = "mov.b\t%x0,%w0\n\tmov.b\t%y0,%x0\n\tmov.b\t%z0,%y0\n\tshll\t%z0\n\tsubx\t%z0,%z0";
2369               *cc_valid_p = 0;
2370               return SHIFT_SPECIAL;
2371             }
2372         }
2373       else if (count == 8 && !TARGET_H8300)
2374         {
2375           switch (shift_type)
2376             {
2377             case SHIFT_ASHIFT:
2378               *assembler_p = "mov.w\t%e0,%f4\n\tmov.b\t%s4,%t4\n\tmov.b\t%t0,%s4\n\tmov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tmov.w\t%f4,%e0";
2379               *cc_valid_p = 0;
2380               return SHIFT_SPECIAL;
2381             case SHIFT_LSHIFTRT:
2382               *assembler_p = "mov.w\t%e0,%f4\n\tmov.b\t%t0,%s0\n\tmov.b\t%s4,%t0\n\tmov.b\t%t4,%s4\n\textu.w\t%f4\n\tmov.w\t%f4,%e0";
2383               *cc_valid_p = 0;
2384               return SHIFT_SPECIAL;
2385             case SHIFT_ASHIFTRT:
2386               *assembler_p = "mov.w\t%e0,%f4\n\tmov.b\t%t0,%s0\n\tmov.b\t%s4,%t0\n\tmov.b\t%t4,%s4\n\texts.w\t%f4\n\tmov.w\t%f4,%e0";
2387               *cc_valid_p = 0;
2388               return SHIFT_SPECIAL;
2389             }
2390         }
2391       else if (count == 16)
2392         {
2393           switch (shift_type)
2394             {
2395             case SHIFT_ASHIFT:
2396               *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
2397               *cc_valid_p = 0;
2398               return SHIFT_SPECIAL;
2399             case SHIFT_LSHIFTRT:
2400               *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
2401               *cc_valid_p = 0;
2402               return SHIFT_SPECIAL;
2403             case SHIFT_ASHIFTRT:
2404               if (TARGET_H8300)
2405                 *assembler_p = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
2406               else
2407                 *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0";
2408               *cc_valid_p = 0;
2409               return SHIFT_SPECIAL;
2410             }
2411         }
2412       else if (count == 17 && !TARGET_H8300)
2413         {
2414           switch (shift_type)
2415             {
2416             case SHIFT_ASHIFT:
2417               *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t%S0";
2418               *cc_valid_p = 0;
2419               return SHIFT_SPECIAL;
2420             case SHIFT_LSHIFTRT:
2421               *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t%S0";
2422               *cc_valid_p = 0;
2423               return SHIFT_SPECIAL;
2424             case SHIFT_ASHIFTRT:
2425               *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t%S0";
2426               *cc_valid_p = 0;
2427               return SHIFT_SPECIAL;
2428             }
2429         }
2430       else if (count == 18 && !TARGET_H8300)
2431         {
2432           switch (shift_type)
2433             {
2434             case SHIFT_ASHIFT:
2435               if (TARGET_H8300S)
2436                 *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t#2,%S0";
2437               else
2438                 *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t%S0\n\tshll.l\t%S0";
2439               *cc_valid_p = 0;
2440               return SHIFT_SPECIAL;
2441             case SHIFT_LSHIFTRT:
2442               if (TARGET_H8300S)
2443                 *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t#2,%S0";
2444               else
2445                 *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t%S0\n\tshlr.l\t%S0";
2446               *cc_valid_p = 0;
2447               return SHIFT_SPECIAL;
2448             case SHIFT_ASHIFTRT:
2449               if (TARGET_H8300S)
2450                 *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t#2,%S0";
2451               else
2452                 *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t%S0\n\tshar.l\t%S0";
2453               *cc_valid_p = 0;
2454               return SHIFT_SPECIAL;
2455             }
2456         }
2457       else if (count == 19 && !TARGET_H8300)
2458         {
2459           switch (shift_type)
2460             {
2461             case SHIFT_ASHIFT:
2462               if (TARGET_H8300S)
2463                 *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t#2,%S0\n\tshll.l\t%S0";
2464               else
2465                 *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t%S0\n\tshll.l\t%S0\n\tshll.l\t%S0";
2466               *cc_valid_p = 0;
2467               return SHIFT_SPECIAL;
2468             case SHIFT_LSHIFTRT:
2469               if (TARGET_H8300S)
2470                 *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t#2,%S0\n\tshlr.l\t%S0";
2471               else
2472                 *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t%S0\n\tshlr.l\t%S0\n\tshlr.l\t%S0";
2473               *cc_valid_p = 0;
2474               return SHIFT_SPECIAL;
2475             case SHIFT_ASHIFTRT:
2476               if (TARGET_H8300S)
2477                 *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t#2,%S0\n\tshar.l\t%S0";
2478               else
2479                 *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t%S0\n\tshar.l\t%S0\n\tshar.l\t%S0";
2480               *cc_valid_p = 0;
2481               return SHIFT_SPECIAL;
2482             }
2483         }
2484       else if (count == 20 && TARGET_H8300S)
2485         {
2486           switch (shift_type)
2487             {
2488             case SHIFT_ASHIFT:
2489               *assembler_p = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0\n\tshll.l\t#2,%S0\n\tshll.l\t#2,%S0";
2490               *cc_valid_p = 0;
2491               return SHIFT_SPECIAL;
2492             case SHIFT_LSHIFTRT:
2493               *assembler_p = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0\n\tshlr.l\t#2,%S0\n\tshlr.l\t#2,%S0";
2494               *cc_valid_p = 0;
2495               return SHIFT_SPECIAL;
2496             case SHIFT_ASHIFTRT:
2497               *assembler_p = "mov.w\t%e0,%f0\n\texts.l\t%S0\n\tshar.l\t#2,%S0\n\tshar.l\t#2,%S0";
2498               *cc_valid_p = 0;
2499               return SHIFT_SPECIAL;
2500             }
2501         }
2502       else if (count == 24 && !TARGET_H8300)
2503         {
2504           switch (shift_type)
2505             {
2506             case SHIFT_ASHIFT:
2507               *assembler_p = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0\n\tmov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
2508               *cc_valid_p = 0;
2509               return SHIFT_SPECIAL;
2510             case SHIFT_LSHIFTRT:
2511               *assembler_p = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
2512               *cc_valid_p = 0;
2513               return SHIFT_SPECIAL;
2514             case SHIFT_ASHIFTRT:
2515               *assembler_p = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
2516               *cc_valid_p = 0;
2517               return SHIFT_SPECIAL;
2518             }
2519         }
2520       else if (count >= 28 && count <= 30 && !TARGET_H8300)
2521         {
2522           if (shift_type == SHIFT_ASHIFTRT)
2523             {
2524               return SHIFT_LOOP;
2525             }
2526           else
2527             {
2528               *assembler_p = rotate_one[cpu][shift_type][shift_mode];
2529               if (TARGET_H8300S)
2530                 *assembler2_p = rotate_two[shift_type][shift_mode];
2531               else
2532                 *assembler2_p = NULL;
2533               *cc_valid_p = 0;
2534               return SHIFT_ROT_AND;
2535             }
2536         }
2537       else if (count == 31)
2538         {
2539           if (shift_type == SHIFT_ASHIFTRT)
2540             {
2541               if (TARGET_H8300)
2542                 *assembler_p = "shll\t%z0\n\tsubx %w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
2543               else
2544                 *assembler_p = "shll\t%e0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
2545               *cc_valid_p = 0;
2546               return SHIFT_SPECIAL;
2547             }
2548           else
2549             {
2550               if (TARGET_H8300)
2551                 {
2552                   if (shift_type == SHIFT_ASHIFT)
2553                     *assembler_p = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
2554                   else
2555                     *assembler_p = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
2556                   *cc_valid_p = 0;
2557                   return SHIFT_SPECIAL;
2558                 }
2559               else
2560                 {
2561                   *assembler_p = rotate_one[cpu][shift_type][shift_mode];
2562                   if (TARGET_H8300S)
2563                     *assembler2_p = rotate_two[shift_type][shift_mode];
2564                   else
2565                     *assembler2_p = NULL;
2566                   *cc_valid_p = 0;
2567                   return SHIFT_ROT_AND;
2568                 }
2569             }
2570         }
2571       break;
2572
2573     default:
2574       abort ();
2575     }
2576
2577   return alg;
2578 }
2579
2580 /* Emit the assembler code for doing shifts.  */
2581
2582 const char *
2583 emit_a_shift (insn, operands)
2584      rtx insn ATTRIBUTE_UNUSED;
2585      rtx *operands;
2586 {
2587   static int loopend_lab;
2588   const char *assembler;
2589   const char *assembler2;
2590   int cc_valid;
2591   rtx shift = operands[3];
2592   enum machine_mode mode = GET_MODE (shift);
2593   enum rtx_code code = GET_CODE (shift);
2594   enum shift_type shift_type;
2595   enum shift_mode shift_mode;
2596
2597   loopend_lab++;
2598
2599   switch (mode)
2600     {
2601     case QImode:
2602       shift_mode = QIshift;
2603       break;
2604     case HImode:
2605       shift_mode = HIshift;
2606       break;
2607     case SImode:
2608       shift_mode = SIshift;
2609       break;
2610     default:
2611       abort ();
2612     }
2613
2614   switch (code)
2615     {
2616     case ASHIFTRT:
2617       shift_type = SHIFT_ASHIFTRT;
2618       break;
2619     case LSHIFTRT:
2620       shift_type = SHIFT_LSHIFTRT;
2621       break;
2622     case ASHIFT:
2623       shift_type = SHIFT_ASHIFT;
2624       break;
2625     default:
2626       abort ();
2627     }
2628
2629   if (GET_CODE (operands[2]) != CONST_INT)
2630     {
2631       /* Indexing by reg, so have to loop and test at top.  */
2632       output_asm_insn ("mov.b   %X2,%X4", operands);
2633       fprintf (asm_out_file, "\tble     .Lle%d\n", loopend_lab);
2634
2635       /* Get the assembler code to do one shift.  */
2636       get_shift_alg (cpu_type, shift_type, mode, 1, &assembler,
2637                      &assembler2, &cc_valid);
2638     }
2639   else
2640     {
2641       int n = INTVAL (operands[2]);
2642       enum shift_alg alg;
2643
2644       /* If the count is negative, make it 0.  */
2645       if (n < 0)
2646         n = 0;
2647       /* If the count is too big, truncate it.
2648          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
2649          do the intuitive thing.  */
2650       else if (n > GET_MODE_BITSIZE (mode))
2651         n = GET_MODE_BITSIZE (mode);
2652
2653       alg = get_shift_alg (cpu_type, shift_type, mode, n, &assembler,
2654                            &assembler2, &cc_valid);
2655
2656       switch (alg)
2657         {
2658         case SHIFT_INLINE:
2659           /* Emit two bit shifts first.  */
2660           while (n > 1 && assembler2 != NULL)
2661             {
2662               output_asm_insn (assembler2, operands);
2663               n -= 2;
2664             }
2665
2666           /* Now emit one bit shifts for any residual.  */
2667           while (n > 0)
2668             {
2669               output_asm_insn (assembler, operands);
2670               n -= 1;
2671             }
2672
2673           /* Keep track of CC.  */
2674           if (cc_valid)
2675             {
2676               cc_status.value1 = operands[0];
2677               cc_status.flags |= cc_valid;
2678             }
2679           return "";
2680
2681         case SHIFT_ROT_AND:
2682           {
2683             int m = GET_MODE_BITSIZE (mode) - n;
2684             int mask = (shift_type == SHIFT_ASHIFT
2685                         ? ((1 << (GET_MODE_BITSIZE (mode) - n)) - 1) << n
2686                         : (1 << (GET_MODE_BITSIZE (mode) - n)) - 1);
2687             char insn_buf[200];
2688             /* Not all possibilities of rotate are supported.  They shouldn't
2689                be generated, but let's watch for 'em.  */
2690             if (assembler == 0)
2691               abort ();
2692
2693             /* Emit two bit rotates first.  */
2694             while (m > 1 && assembler2 != NULL)
2695               {
2696                 output_asm_insn (assembler2, operands);
2697                 m -= 2;
2698               }
2699
2700             /* Now single bit rotates for any residual.  */
2701             while (m > 0)
2702               {
2703                 output_asm_insn (assembler, operands);
2704                 m -= 1;
2705               }
2706
2707             /* Now mask off the high bits.  */
2708             if (TARGET_H8300)
2709               {
2710                 switch (mode)
2711                   {
2712                   case QImode:
2713                     sprintf (insn_buf, "and #%d,%%X0", mask);
2714                     cc_status.value1 = operands[0];
2715                     cc_status.flags |= CC_NO_CARRY;
2716                     break;
2717                   case HImode:
2718                     sprintf (insn_buf, "and #%d,%%s0\n\tand #%d,%%t0",
2719                              mask & 255, mask >> 8);
2720                     break;
2721                   case SImode:
2722                     abort ();
2723                   default:
2724                     break;
2725                   }
2726               }
2727             else
2728               {
2729                 sprintf (insn_buf, "and.%c #%d,%%%c0",
2730                          "bwl"[shift_mode], mask,
2731                          mode == QImode ? 'X' : mode == HImode ? 'T' : 'S');
2732                 cc_status.value1 = operands[0];
2733                 cc_status.flags |= CC_NO_CARRY;
2734               }
2735             output_asm_insn (insn_buf, operands);
2736             return "";
2737           }
2738         case SHIFT_SPECIAL:
2739           output_asm_insn (assembler, operands);
2740           return "";
2741         }
2742
2743       /* A loop to shift by a "large" constant value.
2744          If we have shift-by-2 insns, use them.  */
2745       if (assembler2 != NULL)
2746         {
2747           fprintf (asm_out_file, "\tmov.b       #%d,%sl\n", n / 2,
2748                    names_big[REGNO (operands[4])]);
2749           fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
2750           output_asm_insn (assembler2, operands);
2751           output_asm_insn ("add #0xff,%X4", operands);
2752           fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
2753           if (n % 2)
2754             output_asm_insn (assembler, operands);
2755           return "";
2756         }
2757       else
2758         {
2759           fprintf (asm_out_file, "\tmov.b       #%d,%sl\n", n,
2760                    names_big[REGNO (operands[4])]);
2761           fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
2762           output_asm_insn (assembler, operands);
2763           output_asm_insn ("add #0xff,%X4", operands);
2764           fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
2765           return "";
2766         }
2767     }
2768
2769   fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
2770   output_asm_insn (assembler, operands);
2771   output_asm_insn ("add #0xff,%X4", operands);
2772   fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
2773   fprintf (asm_out_file, ".Lle%d:\n", loopend_lab);
2774
2775   return "";
2776 }
2777 \f
2778 /* Fix the operands of a gen_xxx so that it could become a bit
2779   operating insn.  */
2780
2781 int
2782 fix_bit_operand (operands, what, type)
2783      rtx *operands;
2784      int what;
2785      enum rtx_code type;
2786 {
2787   /* The bit_operand predicate accepts any memory during RTL generation, but
2788      only 'U' memory afterwards, so if this is a MEM operand, we must force
2789      it to be valid for 'U' by reloading the address.  */
2790
2791   if (GET_CODE (operands[2]) == CONST_INT)
2792     {
2793       if (CONST_OK_FOR_LETTER_P (INTVAL (operands[2]), what))
2794         {
2795           /* Ok to have a memory dest.  */
2796           if (GET_CODE (operands[0]) == MEM && !EXTRA_CONSTRAINT (operands[0], 'U'))
2797             {
2798               rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
2799                                      copy_to_mode_reg (Pmode,
2800                                                        XEXP (operands[0], 0)));
2801               MEM_COPY_ATTRIBUTES (mem, operands[0]);
2802               operands[0] = mem;
2803             }
2804
2805           if (GET_CODE (operands[1]) == MEM && !EXTRA_CONSTRAINT (operands[1], 'U'))
2806             {
2807               rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
2808                                      copy_to_mode_reg (Pmode,
2809                                                        XEXP (operands[1], 0)));
2810               MEM_COPY_ATTRIBUTES (mem, operands[0]);
2811               operands[1] = mem;
2812             }
2813           return 0;
2814         }
2815     }
2816
2817   /* Dest and src op must be register.  */
2818
2819   operands[1] = force_reg (QImode, operands[1]);
2820   {
2821     rtx res = gen_reg_rtx (QImode);
2822     emit_insn (gen_rtx_SET (VOIDmode, res,
2823                             gen_rtx (type, QImode, operands[1], operands[2])));
2824     emit_insn (gen_rtx_SET (VOIDmode, operands[0], res));
2825   }
2826   return 1;
2827 }
2828
2829 /* Return nonzero if FUNC is an interrupt function as specified
2830    by the "interrupt" attribute.  */
2831
2832 static int
2833 h8300_interrupt_function_p (func)
2834      tree func;
2835 {
2836   tree a;
2837
2838   if (TREE_CODE (func) != FUNCTION_DECL)
2839     return 0;
2840
2841   a = lookup_attribute ("interrupt_handler", DECL_MACHINE_ATTRIBUTES (func));
2842   return a != NULL_TREE;
2843 }
2844
2845 /* Return nonzero if FUNC is an OS_Task function as specified
2846    by the "OS_Task" attribute.  */
2847
2848 static int
2849 h8300_os_task_function_p (func)
2850      tree func;
2851 {
2852   tree a;
2853
2854   if (TREE_CODE (func) != FUNCTION_DECL)
2855     return 0;
2856
2857   a = lookup_attribute ("OS_Task", DECL_MACHINE_ATTRIBUTES (func));
2858   return a != NULL_TREE;
2859 }
2860
2861 /* Return nonzero if FUNC is a monitor function as specified
2862    by the "monitor" attribute.  */
2863
2864 static int
2865 h8300_monitor_function_p (func)
2866      tree func;
2867 {
2868   tree a;
2869
2870   if (TREE_CODE (func) != FUNCTION_DECL)
2871     return 0;
2872
2873   a = lookup_attribute ("monitor", DECL_MACHINE_ATTRIBUTES (func));
2874   return a != NULL_TREE;
2875 }
2876
2877 /* Return nonzero if FUNC is a function that should be called
2878    through the function vector.  */
2879
2880 int
2881 h8300_funcvec_function_p (func)
2882      tree func;
2883 {
2884   tree a;
2885
2886   if (TREE_CODE (func) != FUNCTION_DECL)
2887     return 0;
2888
2889   a = lookup_attribute ("function_vector", DECL_MACHINE_ATTRIBUTES (func));
2890   return a != NULL_TREE;
2891 }
2892
2893 /* Return nonzero if DECL is a variable that's in the eight bit
2894    data area.  */
2895
2896 int
2897 h8300_eightbit_data_p (decl)
2898      tree decl;
2899 {
2900   tree a;
2901
2902   if (TREE_CODE (decl) != VAR_DECL)
2903     return 0;
2904
2905   a = lookup_attribute ("eightbit_data", DECL_MACHINE_ATTRIBUTES (decl));
2906   return a != NULL_TREE;
2907 }
2908
2909 /* Return nonzero if DECL is a variable that's in the tiny
2910    data area.  */
2911
2912 int
2913 h8300_tiny_data_p (decl)
2914      tree decl;
2915 {
2916   tree a;
2917
2918   if (TREE_CODE (decl) != VAR_DECL)
2919     return 0;
2920
2921   a = lookup_attribute ("tiny_data", DECL_MACHINE_ATTRIBUTES (decl));
2922   return a != NULL_TREE;
2923 }
2924
2925 /* Return nonzero if ATTR is a valid attribute for DECL.
2926    ATTRIBUTES are any existing attributes and ARGS are the arguments
2927    supplied with ATTR.
2928
2929    Supported attributes:
2930
2931    interrupt_handler: output a prologue and epilogue suitable for an
2932    interrupt handler.
2933
2934    function_vector: This function should be called through the
2935    function vector.
2936
2937    eightbit_data: This variable lives in the 8-bit data area and can
2938    be referenced with 8-bit absolute memory addresses.
2939
2940    tiny_data: This variable lives in the tiny data area and can be
2941    referenced with 16-bit absolute memory references.  */
2942
2943 int
2944 h8300_valid_machine_decl_attribute (decl, attributes, attr, args)
2945      tree decl;
2946      tree attributes ATTRIBUTE_UNUSED;
2947      tree attr;
2948      tree args;
2949 {
2950   if (args != NULL_TREE)
2951     return 0;
2952
2953   if (is_attribute_p ("interrupt_handler", attr)
2954       || is_attribute_p ("OS_Task", attr)
2955       || is_attribute_p ("monitor", attr)
2956       || is_attribute_p ("function_vector", attr))
2957     return TREE_CODE (decl) == FUNCTION_DECL;
2958
2959   if (is_attribute_p ("eightbit_data", attr)
2960       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2961     {
2962       if (DECL_INITIAL (decl) == NULL_TREE)
2963         {
2964           warning ("Only initialized variables can be placed into the 8-bit area.");
2965           return 0;
2966         }
2967       DECL_SECTION_NAME (decl) = build_string (7, ".eight");
2968       return 1;
2969     }
2970
2971   if (is_attribute_p ("tiny_data", attr)
2972       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
2973     {
2974       if (DECL_INITIAL (decl) == NULL_TREE)
2975         {
2976           warning ("Only initialized variables can be placed into the 8-bit area.");
2977           return 0;
2978         }
2979       DECL_SECTION_NAME (decl) = build_string (6, ".tiny");
2980       return 1;
2981     }
2982
2983   return 0;
2984 }
2985
2986 extern struct obstack *saveable_obstack;
2987
2988 void
2989 h8300_encode_label (decl)
2990      tree decl;
2991 {
2992   const char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2993   int len = strlen (str);
2994   char *newstr;
2995
2996   newstr = obstack_alloc (saveable_obstack, len + 2);
2997
2998   strcpy (newstr + 1, str);
2999   *newstr = '&';
3000   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3001 }
3002
3003 const char *
3004 output_simode_bld (bild, log2, operands)
3005      int bild;
3006      int log2;
3007      rtx operands[];
3008 {
3009   /* Clear the destination register.  */
3010   if (TARGET_H8300H || TARGET_H8300S)
3011     output_asm_insn ("sub.l\t%S0,%S0", operands);
3012   else
3013     output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
3014
3015   /* Get the bit number we want to load.  */
3016   if (log2)
3017     operands[2] = GEN_INT (exact_log2 (INTVAL (operands[2])));
3018
3019   /* Now output the bit load or bit inverse load, and store it in
3020      the destination.  */
3021   if (bild)
3022     output_asm_insn ("bild\t%Z2,%Y1\n\tbst\t#0,%w0", operands);
3023   else
3024     output_asm_insn ("bld\t%Z2,%Y1\n\tbst\t#0,%w0", operands);
3025
3026   /* All done.  */
3027   return "";
3028 }
3029
3030 /* Given INSN and its current length LENGTH, return the adjustment
3031    (in bytes) to correctly compute INSN's length.
3032
3033    We use this to get the lengths of various memory references correct.  */
3034
3035 int
3036 h8300_adjust_insn_length (insn, length)
3037      rtx insn;
3038      int length ATTRIBUTE_UNUSED;
3039 {
3040   rtx pat;
3041
3042   /* We must filter these ou before calling get_attr_adjust_length.  */
3043   if (GET_CODE (PATTERN (insn)) == USE
3044       || GET_CODE (PATTERN (insn)) == CLOBBER
3045       || GET_CODE (PATTERN (insn)) == SEQUENCE
3046       || GET_CODE (PATTERN (insn)) == ADDR_VEC
3047       || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
3048     return 0;
3049
3050   if (get_attr_adjust_length (insn) == ADJUST_LENGTH_NO)
3051     return 0;
3052
3053   pat = PATTERN (insn);
3054
3055   /* Adjust length for reg->mem and mem->reg copies.  */
3056   if (GET_CODE (pat) == SET
3057       && (GET_CODE (SET_SRC (pat)) == MEM
3058           || GET_CODE (SET_DEST (pat)) == MEM))
3059     {
3060       /* This insn might need a length adjustment.  */
3061       rtx addr;
3062
3063       if (GET_CODE (SET_SRC (pat)) == MEM)
3064         addr = XEXP (SET_SRC (pat), 0);
3065       else
3066         addr = XEXP (SET_DEST (pat), 0);
3067
3068       /* On the H8/300, only one adjustment is necessary; if the
3069          address mode is register indirect, then this insn is two
3070          bytes shorter than indicated in the machine description.  */
3071       if (TARGET_H8300 && GET_CODE (addr) == REG)
3072         return -2;
3073
3074       /* On the H8/300H and H8/S, register indirect is 6 bytes shorter than
3075          indicated in the machine description.  */
3076       if ((TARGET_H8300H || TARGET_H8300S)
3077           && GET_CODE (addr) == REG)
3078         return -6;
3079
3080       /* On the H8/300H and H8/300S, reg + d, for small displacements is 4
3081          bytes shorter than indicated in the machine description.  */
3082       if ((TARGET_H8300H || TARGET_H8300S)
3083           && GET_CODE (addr) == PLUS
3084           && GET_CODE (XEXP (addr, 0)) == REG
3085           && GET_CODE (XEXP (addr, 1)) == CONST_INT
3086           && INTVAL (XEXP (addr, 1)) > -32768
3087           && INTVAL (XEXP (addr, 1)) < 32767)
3088         return -4;
3089
3090       /* On the H8/300H and H8/300S, abs:16 is two bytes shorter than the
3091          more general abs:24.  */
3092       if ((TARGET_H8300H || TARGET_H8300S)
3093           && GET_CODE (addr) == SYMBOL_REF
3094           && TINY_DATA_NAME_P (XSTR (addr, 0)))
3095         return -2;
3096     }
3097
3098   /* Loading some constants needs adjustment.  */
3099   if (GET_CODE (pat) == SET
3100       && GET_CODE (SET_SRC (pat)) == CONST_INT
3101       && GET_MODE (SET_DEST (pat)) == SImode
3102       && INTVAL (SET_SRC (pat)) != 0)
3103     {
3104       if (TARGET_H8300
3105           && ((INTVAL (SET_SRC (pat)) & 0xffff) == 0
3106               || ((INTVAL (SET_SRC (pat)) >> 16) & 0xffff) == 0))
3107         return -2;
3108
3109       if (TARGET_H8300H || TARGET_H8300S)
3110         {
3111           int val = INTVAL (SET_SRC (pat));
3112
3113           if (val == (val & 0xff)
3114               || val == (val & 0xff00))
3115             return -6;
3116
3117           if (val == -4 || val == -2 || val == -1)
3118             return -6;
3119         }
3120     }
3121
3122   /* Shifts need various adjustments.  */
3123   if (GET_CODE (pat) == PARALLEL
3124       && GET_CODE (XVECEXP (pat, 0, 0)) == SET
3125       && (GET_CODE (SET_SRC (XVECEXP (pat, 0, 0))) == ASHIFTRT
3126           || GET_CODE (SET_SRC (XVECEXP (pat, 0, 0))) == LSHIFTRT
3127           || GET_CODE (SET_SRC (XVECEXP (pat, 0, 0))) == ASHIFT))
3128     {
3129       rtx src = SET_SRC (XVECEXP (pat, 0, 0));
3130       enum machine_mode mode = GET_MODE (src);
3131       int shift;
3132
3133       if (GET_CODE (XEXP (src, 1)) != CONST_INT)
3134         return 0;
3135
3136       shift = INTVAL (XEXP (src, 1));
3137       /* According to ANSI, negative shift is undefined.  It is
3138          considered to be zero in this case (see function
3139          emit_a_shift above).  */
3140       if (shift < 0)
3141         shift = 0;
3142
3143       /* QImode shifts by small constants take one insn
3144          per shift.  So the adjustment is 20 (md length) -
3145          # shifts * 2.  */
3146       if (mode == QImode && shift <= 4)
3147         return -(20 - shift * 2);
3148
3149       /* Similarly for HImode and SImode shifts by
3150          small constants on the H8/300H and H8/300S.  */
3151       if ((TARGET_H8300H || TARGET_H8300S)
3152           && (mode == HImode || mode == SImode) && shift <= 4)
3153         return -(20 - shift * 2);
3154
3155       /* HImode shifts by small constants for the H8/300.  */
3156       if (mode == HImode && shift <= 4)
3157         return -(20 - (shift * (GET_CODE (src) == ASHIFT ? 2 : 4)));
3158
3159       /* SImode shifts by small constants for the H8/300.  */
3160       if (mode == SImode && shift <= 2)
3161         return -(20 - (shift * (GET_CODE (src) == ASHIFT ? 6 : 8)));
3162
3163       /* XXX ??? Could check for more shift/rotate cases here.  */
3164     }
3165
3166   return 0;
3167 }