OSDN Git Service

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