OSDN Git Service

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