OSDN Git Service

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