OSDN Git Service

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