OSDN Git Service

PR target/13721
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.c
1 /* Subroutines for insn-output.c for Renesas H8/300.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004 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 GCC.
8
9 GCC 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 GCC 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 GCC; 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 "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "recog.h"
39 #include "expr.h"
40 #include "function.h"
41 #include "optabs.h"
42 #include "toplev.h"
43 #include "c-pragma.h"
44 #include "tm_p.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48
49 /* Forward declarations.  */
50 static const char *byte_reg (rtx, int);
51 static int h8300_interrupt_function_p (tree);
52 static int h8300_saveall_function_p (tree);
53 static int h8300_monitor_function_p (tree);
54 static int h8300_os_task_function_p (tree);
55 static void h8300_emit_stack_adjustment (int, unsigned int);
56 static int round_frame_size (int);
57 static unsigned int compute_saved_regs (void);
58 static void push (int);
59 static void pop (int);
60 static const char *cond_string (enum rtx_code);
61 static unsigned int h8300_asm_insn_count (const char *);
62 static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
63 static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
64 static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
65 #ifndef OBJECT_FORMAT_ELF
66 static void h8300_asm_named_section (const char *, unsigned int);
67 #endif
68 static int h8300_and_costs (rtx);
69 static int h8300_shift_costs (rtx);
70
71 /* CPU_TYPE, says what cpu we're compiling for.  */
72 int cpu_type;
73
74 /* True if a #pragma interrupt has been seen for the current function.  */
75 static int pragma_interrupt;
76
77 /* True if a #pragma saveall has been seen for the current function.  */
78 static int pragma_saveall;
79
80 static const char *const names_big[] =
81 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
82
83 static const char *const names_extended[] =
84 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
85
86 static const char *const names_upper_extended[] =
87 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
88
89 /* Points to one of the above.  */
90 /* ??? The above could be put in an array indexed by CPU_TYPE.  */
91 const char * const *h8_reg_names;
92
93 /* Various operations needed by the following, indexed by CPU_TYPE.  */
94
95 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
96
97 /* Machine-specific symbol_ref flags.  */
98 #define SYMBOL_FLAG_FUNCVEC_FUNCTION    (SYMBOL_FLAG_MACH_DEP << 0)
99 #define SYMBOL_FLAG_EIGHTBIT_DATA       (SYMBOL_FLAG_MACH_DEP << 1)
100 #define SYMBOL_FLAG_TINY_DATA           (SYMBOL_FLAG_MACH_DEP << 2)
101 \f
102 /* See below where shifts are handled for explanation of this enum.  */
103
104 enum shift_alg
105 {
106   SHIFT_INLINE,
107   SHIFT_ROT_AND,
108   SHIFT_SPECIAL,
109   SHIFT_LOOP
110 };
111
112 /* Symbols of the various shifts which can be used as indices.  */
113
114 enum shift_type
115 {
116   SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
117 };
118
119 /* Macros to keep the shift algorithm tables small.  */
120 #define INL SHIFT_INLINE
121 #define ROT SHIFT_ROT_AND
122 #define LOP SHIFT_LOOP
123 #define SPC SHIFT_SPECIAL
124
125 /* The shift algorithms for each machine, mode, shift type, and shift
126    count are defined below.  The three tables below correspond to
127    QImode, HImode, and SImode, respectively.  Each table is organized
128    by, in the order of indices, machine, shift type, and shift count.  */
129
130 static enum shift_alg shift_alg_qi[3][3][8] = {
131   {
132     /* TARGET_H8300  */
133     /* 0    1    2    3    4    5    6    7  */
134     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
135     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
136     { INL, INL, INL, INL, INL, LOP, LOP, SPC }  /* SHIFT_ASHIFTRT */
137   },
138   {
139     /* TARGET_H8300H  */
140     /* 0    1    2    3    4    5    6    7  */
141     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
142     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
143     { INL, INL, INL, INL, INL, LOP, LOP, SPC }  /* SHIFT_ASHIFTRT */
144   },
145   {
146     /* TARGET_H8300S  */
147     /*  0    1    2    3    4    5    6    7  */
148     { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT   */
149     { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
150     { INL, INL, INL, INL, INL, INL, INL, SPC }  /* SHIFT_ASHIFTRT */
151   }
152 };
153
154 static enum shift_alg shift_alg_hi[3][3][16] = {
155   {
156     /* TARGET_H8300  */
157     /*  0    1    2    3    4    5    6    7  */
158     /*  8    9   10   11   12   13   14   15  */
159     { INL, INL, INL, INL, INL, INL, INL, SPC,
160       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
161     { INL, INL, INL, INL, INL, LOP, LOP, SPC,
162       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
163     { INL, INL, INL, INL, INL, LOP, LOP, SPC,
164       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
165   },
166   {
167     /* TARGET_H8300H  */
168     /*  0    1    2    3    4    5    6    7  */
169     /*  8    9   10   11   12   13   14   15  */
170     { INL, INL, INL, INL, INL, INL, INL, SPC,
171       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
172     { INL, INL, INL, INL, INL, INL, INL, SPC,
173       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
174     { INL, INL, INL, INL, INL, INL, INL, SPC,
175       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
176   },
177   {
178     /* TARGET_H8300S  */
179     /*  0    1    2    3    4    5    6    7  */
180     /*  8    9   10   11   12   13   14   15  */
181     { INL, INL, INL, INL, INL, INL, INL, INL,
182       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
183     { INL, INL, INL, INL, INL, INL, INL, INL,
184       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
185     { INL, INL, INL, INL, INL, INL, INL, INL,
186       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
187   }
188 };
189
190 static enum shift_alg shift_alg_si[3][3][32] = {
191   {
192     /* TARGET_H8300  */
193     /*  0    1    2    3    4    5    6    7  */
194     /*  8    9   10   11   12   13   14   15  */
195     /* 16   17   18   19   20   21   22   23  */
196     /* 24   25   26   27   28   29   30   31  */
197     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
198       SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
199       SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
200       SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT   */
201     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
202       SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
203       SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
204       SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
205     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
206       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
207       SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
208       SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
209   },
210   {
211     /* TARGET_H8300H  */
212     /*  0    1    2    3    4    5    6    7  */
213     /*  8    9   10   11   12   13   14   15  */
214     /* 16   17   18   19   20   21   22   23  */
215     /* 24   25   26   27   28   29   30   31  */
216     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
217       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
218       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
219       SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
220     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
221       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
222       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
223       SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
224     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
225       SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
226       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
227       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
228   },
229   {
230     /* TARGET_H8300S  */
231     /*  0    1    2    3    4    5    6    7  */
232     /*  8    9   10   11   12   13   14   15  */
233     /* 16   17   18   19   20   21   22   23  */
234     /* 24   25   26   27   28   29   30   31  */
235     { INL, INL, INL, INL, INL, INL, INL, INL,
236       INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
237       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
238       SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
239     { INL, INL, INL, INL, INL, INL, INL, INL,
240       INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
241       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
242       SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
243     { INL, INL, INL, INL, INL, INL, INL, INL,
244       INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
245       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
246       SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
247   }
248 };
249
250 #undef INL
251 #undef ROT
252 #undef LOP
253 #undef SPC
254
255 enum h8_cpu
256 {
257   H8_300,
258   H8_300H,
259   H8_S
260 };
261
262 /* Initialize various cpu specific globals at start up.  */
263
264 void
265 h8300_init_once (void)
266 {
267   static const char *const h8_push_ops[2] = { "push" , "push.l" };
268   static const char *const h8_pop_ops[2]  = { "pop"  , "pop.l"  };
269   static const char *const h8_mov_ops[2]  = { "mov.w", "mov.l"  };
270
271   if (TARGET_H8300)
272     {
273       cpu_type = (int) CPU_H8300;
274       h8_reg_names = names_big;
275     }
276   else
277     {
278       /* For this we treat the H8/300H and H8S the same.  */
279       cpu_type = (int) CPU_H8300H;
280       h8_reg_names = names_extended;
281     }
282   h8_push_op = h8_push_ops[cpu_type];
283   h8_pop_op = h8_pop_ops[cpu_type];
284   h8_mov_op = h8_mov_ops[cpu_type];
285
286   if (!TARGET_H8300S && TARGET_MAC)
287     {
288       error ("-ms2600 is used without -ms");
289       target_flags |= MASK_H8300S;
290     }
291
292   if (TARGET_H8300 && TARGET_NORMAL_MODE)
293     {
294       error ("-mn is used without -mh or -ms");
295       target_flags ^= MASK_NORMAL_MODE;
296     }
297
298   /* Some of the shifts are optimized for speed by default.
299      See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
300      If optimizing for size, change shift_alg for those shift to
301      SHIFT_LOOP.  */
302   if (optimize_size)
303     {
304       /* H8/300 */
305       shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
306       shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
307       shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
308       shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
309
310       shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
311       shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
312
313       shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
314       shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
315
316       /* H8/300H */
317       shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
318       shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
319
320       shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
321       shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
322
323       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
324       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
325       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
326       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
327
328       /* H8S */
329       shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
330     }
331 }
332
333 static const char *
334 byte_reg (rtx x, int b)
335 {
336   static const char *const names_small[] = {
337     "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
338     "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
339   };
340
341   if (!REG_P (x))
342     abort ();
343
344   return names_small[REGNO (x) * 2 + b];
345 }
346
347 /* REGNO must be saved/restored across calls if this macro is true.  */
348
349 #define WORD_REG_USED(regno)                                            \
350   (regno < SP_REG                                                       \
351    /* No need to save registers if this function will not return.  */   \
352    && ! TREE_THIS_VOLATILE (current_function_decl)                      \
353    && (h8300_saveall_function_p (current_function_decl)                 \
354        /* Save any call saved register that was used.  */               \
355        || (regs_ever_live[regno] && !call_used_regs[regno])             \
356        /* Save the frame pointer if it was used.  */                    \
357        || (regno == HARD_FRAME_POINTER_REGNUM && regs_ever_live[regno]) \
358        /* Save any register used in an interrupt handler.  */           \
359        || (h8300_current_function_interrupt_function_p ()               \
360            && regs_ever_live[regno])                                    \
361        /* Save call clobbered registers in non-leaf interrupt           \
362           handlers.  */                                                 \
363        || (h8300_current_function_interrupt_function_p ()               \
364            && call_used_regs[regno]                                     \
365            && !current_function_is_leaf)))
366
367 /* Output assembly language to FILE for the operation OP with operand size
368    SIZE to adjust the stack pointer.  */
369
370 static void
371 h8300_emit_stack_adjustment (int sign, unsigned int size)
372 {
373   /* H8/300 cannot add/subtract a large constant with a single
374      instruction.  If a temporary register is available, load the
375      constant to it and then do the addition.  */
376   if (TARGET_H8300
377       && size > 4
378       && !h8300_current_function_interrupt_function_p ()
379       && !(current_function_needs_context && sign < 0))
380     {
381       rtx new_sp;
382       rtx r3 = gen_rtx_REG (Pmode, 3);
383       emit_insn (gen_rtx_SET (Pmode, r3, GEN_INT (sign * size)));
384       new_sp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, r3);
385       emit_insn (gen_rtx_SET (Pmode, stack_pointer_rtx, new_sp));
386     }
387   else
388     {
389       /* The stack adjustment made here is further optimized by the
390          splitter.  In case of H8/300, the splitter always splits the
391          addition emitted here to make the adjustment
392          interrupt-safe.  */
393       rtx new_sp = plus_constant (stack_pointer_rtx, sign * size);
394       emit_insn (gen_rtx_SET (Pmode, stack_pointer_rtx, new_sp));
395     }
396 }
397
398 /* Round up frame size SIZE.  */
399
400 static int
401 round_frame_size (int size)
402 {
403   return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
404           & -STACK_BOUNDARY / BITS_PER_UNIT);
405 }
406
407 /* Compute which registers to push/pop.
408    Return a bit vector of registers.  */
409
410 static unsigned int
411 compute_saved_regs (void)
412 {
413   unsigned int saved_regs = 0;
414   int regno;
415
416   /* Construct a bit vector of registers to be pushed/popped.  */
417   for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
418     {
419       if (WORD_REG_USED (regno))
420         saved_regs |= 1 << regno;
421     }
422
423   /* Don't push/pop the frame pointer as it is treated separately.  */
424   if (frame_pointer_needed)
425     saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
426
427   return saved_regs;
428 }
429
430 /* Emit an insn to push register RN.  */
431
432 static void
433 push (int rn)
434 {
435   rtx reg = gen_rtx_REG (word_mode, rn);
436   rtx x;
437
438   if (TARGET_H8300)
439     x = gen_push_h8300 (reg);
440   else if (!TARGET_NORMAL_MODE)
441     x = gen_push_h8300hs_advanced (reg);
442   else
443     x = gen_push_h8300hs_normal (reg);
444   x = emit_insn (x);
445   REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
446 }
447
448 /* Emit an insn to pop register RN.  */
449
450 static void
451 pop (int rn)
452 {
453   rtx reg = gen_rtx_REG (word_mode, rn);
454   rtx x;
455
456   if (TARGET_H8300)
457     x = gen_pop_h8300 (reg);
458   else if (!TARGET_NORMAL_MODE)
459     x = gen_pop_h8300hs_advanced (reg);
460   else
461     x = gen_pop_h8300hs_normal (reg);
462   x = emit_insn (x);
463   REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
464 }
465
466 /* This is what the stack looks like after the prolog of
467    a function with a frame has been set up:
468
469    <args>
470    PC
471    FP                   <- fp
472    <locals>
473    <saved registers>    <- sp
474
475    This is what the stack looks like after the prolog of
476    a function which doesn't have a frame:
477
478    <args>
479    PC
480    <locals>
481    <saved registers>    <- sp
482 */
483
484 /* Generate RTL code for the function prologue.  */
485
486 void
487 h8300_expand_prologue (void)
488 {
489   int regno;
490   int saved_regs;
491   int n_regs;
492
493   /* If the current function has the OS_Task attribute set, then
494      we have a naked prologue.  */
495   if (h8300_os_task_function_p (current_function_decl))
496     return;
497
498   if (h8300_monitor_function_p (current_function_decl))
499     /* My understanding of monitor functions is they act just like
500        interrupt functions, except the prologue must mask
501        interrupts.  */
502     emit_insn (gen_monitor_prologue ());
503
504   if (frame_pointer_needed)
505     {
506       /* Push fp.  */
507       push (HARD_FRAME_POINTER_REGNUM);
508       emit_insn (gen_rtx_SET (Pmode, hard_frame_pointer_rtx,
509                               stack_pointer_rtx));
510     }
511
512   /* Push the rest of the registers in ascending order.  */
513   saved_regs = compute_saved_regs ();
514   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
515     {
516       n_regs = 1;
517       if (saved_regs & (1 << regno))
518         {
519           if (TARGET_H8300S)
520             {
521               /* See how many registers we can push at the same time.  */
522               if ((regno == 0 || regno == 4)
523                   && ((saved_regs >> regno) & 0x0f) == 0x0f)
524                 n_regs = 4;
525
526               else if ((regno == 0 || regno == 4)
527                        && ((saved_regs >> regno) & 0x07) == 0x07)
528                 n_regs = 3;
529
530               else if ((regno == 0 || regno == 2 || regno == 4 || regno == 6)
531                        && ((saved_regs >> regno) & 0x03) == 0x03)
532                 n_regs = 2;
533             }
534
535           switch (n_regs)
536             {
537             case 1:
538               push (regno);
539               break;
540             case 2:
541               emit_insn (gen_stm_h8300s_2 (gen_rtx_REG (SImode, regno),
542                                            gen_rtx_REG (SImode, regno + 1)));
543               break;
544             case 3:
545               emit_insn (gen_stm_h8300s_3 (gen_rtx_REG (SImode, regno),
546                                            gen_rtx_REG (SImode, regno + 1),
547                                            gen_rtx_REG (SImode, regno + 2)));
548               break;
549             case 4:
550               emit_insn (gen_stm_h8300s_4 (gen_rtx_REG (SImode, regno),
551                                            gen_rtx_REG (SImode, regno + 1),
552                                            gen_rtx_REG (SImode, regno + 2),
553                                            gen_rtx_REG (SImode, regno + 3)));
554               break;
555             default:
556               abort ();
557             }
558         }
559     }
560
561   /* Leave room for locals.  */
562   h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()));
563 }
564
565 int
566 h8300_can_use_return_insn_p (void)
567 {
568   return (reload_completed
569           && !frame_pointer_needed
570           && get_frame_size () == 0
571           && compute_saved_regs () == 0);
572 }
573
574 /* Generate RTL code for the function epilogue.  */
575
576 void
577 h8300_expand_epilogue (void)
578 {
579   int regno;
580   int saved_regs;
581   int n_regs;
582
583   if (h8300_os_task_function_p (current_function_decl))
584     /* OS_Task epilogues are nearly naked -- they just have an
585        rts instruction.  */
586     return;
587
588   /* Deallocate locals.  */
589   h8300_emit_stack_adjustment (1, round_frame_size (get_frame_size ()));
590
591   /* Pop the saved registers in descending order.  */
592   saved_regs = compute_saved_regs ();
593   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
594     {
595       n_regs = 1;
596       if (saved_regs & (1 << regno))
597         {
598           if (TARGET_H8300S)
599             {
600               /* See how many registers we can pop at the same time.  */
601               if ((regno == 7 || regno == 3)
602                   && ((saved_regs >> (regno - 3)) & 0x0f) == 0x0f)
603                 n_regs = 4;
604
605               else if ((regno == 6 || regno == 2)
606                        && ((saved_regs >> (regno - 2)) & 0x07) == 0x07)
607                 n_regs = 3;
608
609               else if ((regno == 7 || regno == 5 || regno == 3 || regno == 1)
610                        && ((saved_regs >> (regno - 1)) & 0x03) == 0x03)
611                 n_regs = 2;
612             }
613
614           switch (n_regs)
615             {
616             case 1:
617               pop (regno);
618               break;
619             case 2:
620               emit_insn (gen_ldm_h8300s_2 (gen_rtx_REG (SImode, regno - 1),
621                                            gen_rtx_REG (SImode, regno)));
622               break;
623             case 3:
624               emit_insn (gen_ldm_h8300s_3 (gen_rtx_REG (SImode, regno - 2),
625                                            gen_rtx_REG (SImode, regno - 1),
626                                            gen_rtx_REG (SImode, regno)));
627               break;
628             case 4:
629               emit_insn (gen_ldm_h8300s_4 (gen_rtx_REG (SImode, regno - 3),
630                                            gen_rtx_REG (SImode, regno - 2),
631                                            gen_rtx_REG (SImode, regno - 1),
632                                            gen_rtx_REG (SImode, regno)));
633               break;
634             default:
635               abort ();
636             }
637         }
638     }
639
640   /* Pop frame pointer if we had one.  */
641   if (frame_pointer_needed)
642     pop (HARD_FRAME_POINTER_REGNUM);
643 }
644
645 /* Return nonzero if the current function is an interrupt
646    function.  */
647
648 int
649 h8300_current_function_interrupt_function_p (void)
650 {
651   return (h8300_interrupt_function_p (current_function_decl)
652           || h8300_monitor_function_p (current_function_decl));
653 }
654
655 /* Output assembly code for the start of the file.  */
656
657 static void
658 h8300_file_start (void)
659 {
660   default_file_start ();
661
662   if (TARGET_H8300H)
663     fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
664   else if (TARGET_H8300S)
665     fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
666 }
667
668 /* Output assembly language code for the end of file.  */
669
670 static void
671 h8300_file_end (void)
672 {
673   fputs ("\t.end\n", asm_out_file);
674 }
675 \f
676 /* Return true if OP is a valid source operand for an integer move
677    instruction.  */
678
679 int
680 general_operand_src (rtx op, enum machine_mode mode)
681 {
682   if (GET_MODE (op) == mode
683       && GET_CODE (op) == MEM
684       && GET_CODE (XEXP (op, 0)) == POST_INC)
685     return 1;
686   return general_operand (op, mode);
687 }
688
689 /* Return true if OP is a valid destination operand for an integer move
690    instruction.  */
691
692 int
693 general_operand_dst (rtx op, enum machine_mode mode)
694 {
695   if (GET_MODE (op) == mode
696       && GET_CODE (op) == MEM
697       && GET_CODE (XEXP (op, 0)) == PRE_DEC)
698     return 1;
699   return general_operand (op, mode);
700 }
701
702 /* Return true if OP is a constant that contains only one 1 in its
703    binary representation.  */
704
705 int
706 single_one_operand (rtx operand, enum machine_mode mode ATTRIBUTE_UNUSED)
707 {
708   if (GET_CODE (operand) == CONST_INT)
709     {
710       /* We really need to do this masking because 0x80 in QImode is
711          represented as -128 for example.  */
712       if (exact_log2 (INTVAL (operand) & GET_MODE_MASK (mode)) >= 0)
713         return 1;
714     }
715
716   return 0;
717 }
718
719 /* Return true if OP is a constant that contains only one 0 in its
720    binary representation.  */
721
722 int
723 single_zero_operand (rtx operand, enum machine_mode mode ATTRIBUTE_UNUSED)
724 {
725   if (GET_CODE (operand) == CONST_INT)
726     {
727       /* We really need to do this masking because 0x80 in QImode is
728          represented as -128 for example.  */
729       if (exact_log2 (~INTVAL (operand) & GET_MODE_MASK (mode)) >= 0)
730         return 1;
731     }
732
733   return 0;
734 }
735
736 /* Return true if OP is a valid call operand.  */
737
738 int
739 call_insn_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
740 {
741   if (GET_CODE (op) == MEM)
742     {
743       rtx inside = XEXP (op, 0);
744       if (register_operand (inside, Pmode))
745         return 1;
746       if (CONSTANT_ADDRESS_P (inside))
747         return 1;
748     }
749   return 0;
750 }
751
752 /* Return 1 if an addition/subtraction of a constant integer can be
753    transformed into two consecutive adds/subs that are faster than the
754    straightforward way.  Otherwise, return 0.  */
755
756 int
757 two_insn_adds_subs_operand (rtx op, enum machine_mode mode)
758 {
759   if (GET_CODE (op) == CONST_INT)
760     {
761       HOST_WIDE_INT value = INTVAL (op);
762
763       /* Force VALUE to be positive so that we do not have to consider
764          the negative case.  */
765       if (value < 0)
766         value = -value;
767       if (TARGET_H8300H || TARGET_H8300S)
768         {
769           /* A constant addition/subtraction takes 2 states in QImode,
770              4 states in HImode, and 6 states in SImode.  Thus, the
771              only case we can win is when SImode is used, in which
772              case, two adds/subs are used, taking 4 states.  */
773           if (mode == SImode
774               && (value == 2 + 1
775                   || value == 4 + 1
776                   || value == 4 + 2
777                   || value == 4 + 4))
778             return 1;
779         }
780       else
781         {
782           /* We do not profit directly by splitting addition or
783              subtraction of 3 and 4.  However, since these are
784              implemented as a sequence of adds or subs, they do not
785              clobber (cc0) unlike a sequence of add.b and add.x.  */
786           if (mode == HImode
787               && (value == 2 + 1
788                   || value == 2 + 2))
789             return 1;
790         }
791     }
792
793   return 0;
794 }
795
796 /* Split an add of a small constant into two adds/subs insns.
797
798    If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
799    instead of adds/subs.  */
800
801 void
802 split_adds_subs (enum machine_mode mode, rtx *operands)
803 {
804   HOST_WIDE_INT val = INTVAL (operands[1]);
805   rtx reg = operands[0];
806   HOST_WIDE_INT sign = 1;
807   HOST_WIDE_INT amount;
808   rtx (*gen_add) (rtx, rtx, rtx);
809
810   /* Force VAL to be positive so that we do not have to consider the
811      sign.  */
812   if (val < 0)
813     {
814       val = -val;
815       sign = -1;
816     }
817
818   switch (mode)
819     {
820     case HImode:
821       gen_add = gen_addhi3;
822       break;
823
824     case SImode:
825       gen_add = gen_addsi3;
826       break;
827
828     default:
829       abort ();
830     }
831
832   /* Try different amounts in descending order.  */
833   for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
834        amount > 0;
835        amount /= 2)
836     {
837       for (; val >= amount; val -= amount)
838         emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
839     }
840
841   return;
842 }
843
844 /* Return true if OP is a valid call operand, and OP represents
845    an operand for a small call (4 bytes instead of 6 bytes).  */
846
847 int
848 small_call_insn_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
849 {
850   if (GET_CODE (op) == MEM)
851     {
852       rtx inside = XEXP (op, 0);
853
854       /* Register indirect is a small call.  */
855       if (register_operand (inside, Pmode))
856         return 1;
857
858       /* A call through the function vector is a small call too.  */
859       if (GET_CODE (inside) == SYMBOL_REF
860           && (SYMBOL_REF_FLAGS (inside) & SYMBOL_FLAG_FUNCVEC_FUNCTION))
861         return 1;
862     }
863   /* Otherwise it's a large call.  */
864   return 0;
865 }
866
867 /* Return true if OP is a valid jump operand.  */
868
869 int
870 jump_address_operand (rtx op, enum machine_mode mode)
871 {
872   if (GET_CODE (op) == REG)
873     return mode == Pmode;
874
875   if (GET_CODE (op) == MEM)
876     {
877       rtx inside = XEXP (op, 0);
878       if (register_operand (inside, Pmode))
879         return 1;
880       if (CONSTANT_ADDRESS_P (inside))
881         return 1;
882     }
883   return 0;
884 }
885
886 /* Recognize valid operands for bit-field instructions.  */
887
888 extern int rtx_equal_function_value_matters;
889
890 int
891 bit_operand (rtx op, enum machine_mode mode)
892 {
893   /* We can accept any general operand, except that MEM operands must
894      be limited to those that use addresses valid for the 'U' constraint.  */
895   if (!general_operand (op, mode))
896     return 0;
897
898   /* Accept any mem during RTL generation.  Otherwise, the code that does
899      insv and extzv will think that we can not handle memory.  However,
900      to avoid reload problems, we only accept 'U' MEM operands after RTL
901      generation.  This means that any named pattern which uses this predicate
902      must force its operands to match 'U' before emitting RTL.  */
903
904   if (GET_CODE (op) == REG)
905     return 1;
906   if (GET_CODE (op) == SUBREG)
907     return 1;
908   return (GET_CODE (op) == MEM
909           && EXTRA_CONSTRAINT (op, 'U'));
910 }
911
912 int
913 bit_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
914 {
915   return (GET_CODE (op) == MEM
916           && EXTRA_CONSTRAINT (op, 'U'));
917 }
918
919 /* Handle machine specific pragmas for compatibility with existing
920    compilers for the H8/300.
921
922    pragma saveall generates prologue/epilogue code which saves and
923    restores all the registers on function entry.
924
925    pragma interrupt saves and restores all registers, and exits with
926    an rte instruction rather than an rts.  A pointer to a function
927    with this attribute may be safely used in an interrupt vector.  */
928
929 void
930 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
931 {
932   pragma_interrupt = 1;
933 }
934
935 void
936 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
937 {
938   pragma_saveall = 1;
939 }
940
941 /* If the next function argument with MODE and TYPE is to be passed in
942    a register, return a reg RTX for the hard register in which to pass
943    the argument.  CUM represents the state after the last argument.
944    If the argument is to be pushed, NULL_RTX is returned.  */
945
946 rtx
947 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
948               tree type, int named)
949 {
950   static const char *const hand_list[] = {
951     "__main",
952     "__cmpsi2",
953     "__divhi3",
954     "__modhi3",
955     "__udivhi3",
956     "__umodhi3",
957     "__divsi3",
958     "__modsi3",
959     "__udivsi3",
960     "__umodsi3",
961     "__mulhi3",
962     "__mulsi3",
963     "__reg_memcpy",
964     "__reg_memset",
965     "__ucmpsi2",
966     0,
967   };
968
969   rtx result = NULL_RTX;
970   const char *fname;
971   int regpass = 0;
972
973   /* Never pass unnamed arguments in registers.  */
974   if (!named)
975     return NULL_RTX;
976
977   /* Pass 3 regs worth of data in regs when user asked on the command line.  */
978   if (TARGET_QUICKCALL)
979     regpass = 3;
980
981   /* If calling hand written assembler, use 4 regs of args.  */
982   if (cum->libcall)
983     {
984       const char * const *p;
985
986       fname = XSTR (cum->libcall, 0);
987
988       /* See if this libcall is one of the hand coded ones.  */
989       for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
990         ;
991
992       if (*p)
993         regpass = 4;
994     }
995
996   if (regpass)
997     {
998       int size;
999
1000       if (mode == BLKmode)
1001         size = int_size_in_bytes (type);
1002       else
1003         size = GET_MODE_SIZE (mode);
1004
1005       if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1006           && cum->nbytes / UNITS_PER_WORD <= 3)
1007         result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1008     }
1009
1010   return result;
1011 }
1012 \f
1013 static int
1014 h8300_and_costs (rtx x)
1015 {
1016   rtx operands[4];
1017
1018   if (GET_MODE (x) == QImode)
1019     return 1;
1020
1021   if (GET_MODE (x) != HImode
1022       && GET_MODE (x) != SImode)
1023     return 100;
1024
1025   operands[0] = NULL;
1026   operands[1] = NULL;
1027   operands[2] = XEXP (x, 1);
1028   operands[3] = x;
1029   return compute_logical_op_length (GET_MODE (x), operands) / 2;
1030 }
1031
1032 static int
1033 h8300_shift_costs (rtx x)
1034 {
1035   rtx operands[4];
1036
1037   if (GET_MODE (x) != QImode
1038       && GET_MODE (x) != HImode
1039       && GET_MODE (x) != SImode)
1040     return 100;
1041
1042   operands[0] = NULL;
1043   operands[1] = NULL;
1044   operands[2] = XEXP (x, 1);
1045   operands[3] = x;
1046   return compute_a_shift_length (NULL, operands) / 2;
1047 }
1048
1049 static bool
1050 h8300_rtx_costs (rtx x, int code, int outer_code, int *total)
1051 {
1052   switch (code)
1053     {
1054     case CONST_INT:
1055       {
1056         HOST_WIDE_INT n = INTVAL (x);
1057
1058         if (-4 <= n || n <= 4)
1059           {
1060             switch ((int) n)
1061               {
1062               case 0:
1063                 *total = 0;
1064                 return true;
1065               case 1:
1066               case 2:
1067               case -1:
1068               case -2:
1069                 *total = 0 + (outer_code == SET);
1070                 return true;
1071               case 4:
1072               case -4:
1073                 if (TARGET_H8300H || TARGET_H8300S)
1074                   *total = 0 + (outer_code == SET);
1075                 else
1076                   *total = 1;
1077                 return true;
1078               }
1079           }
1080         *total = 1;
1081         return true;
1082       }
1083
1084     case CONST:
1085     case LABEL_REF:
1086     case SYMBOL_REF:
1087       *total = 3;
1088       return true;
1089
1090     case CONST_DOUBLE:
1091       *total = 20;
1092       return true;
1093
1094     case AND:
1095       *total = COSTS_N_INSNS (h8300_and_costs (x));
1096       return true;
1097
1098     /* We say that MOD and DIV are so expensive because otherwise we'll
1099        generate some really horrible code for division of a power of two.  */
1100     case MOD:
1101     case DIV:
1102       *total = 60;
1103       return true;
1104
1105     case MULT:
1106       *total = 20;
1107       return true;
1108
1109     case ASHIFT:
1110     case ASHIFTRT:
1111     case LSHIFTRT:
1112       *total = COSTS_N_INSNS (h8300_shift_costs (x));
1113       return true;
1114
1115     case ROTATE:
1116     case ROTATERT:
1117       if (GET_MODE (x) == HImode)
1118         *total = 2;
1119       else
1120         *total = 8;
1121       return true;
1122
1123     default:
1124       *total = 4;
1125       return true;
1126     }
1127 }
1128 \f
1129 /* Documentation for the machine specific operand escapes:
1130
1131    'E' like s but negative.
1132    'F' like t but negative.
1133    'G' constant just the negative
1134    'R' print operand as a byte:8 address if appropriate, else fall back to
1135        'X' handling.
1136    'S' print operand as a long word
1137    'T' print operand as a word
1138    'V' find the set bit, and print its number.
1139    'W' find the clear bit, and print its number.
1140    'X' print operand as a byte
1141    'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1142        If this operand isn't a register, fall back to 'R' handling.
1143    'Z' print int & 7.
1144    'c' print the opcode corresponding to rtl
1145    'e' first word of 32 bit value - if reg, then least reg. if mem
1146        then least. if const then most sig word
1147    'f' second word of 32 bit value - if reg, then biggest reg. if mem
1148        then +2. if const then least sig word
1149    'j' print operand as condition code.
1150    'k' print operand as reverse condition code.
1151    's' print as low byte of 16 bit value
1152    't' print as high byte of 16 bit value
1153    'w' print as low byte of 32 bit value
1154    'x' print as 2nd byte of 32 bit value
1155    'y' print as 3rd byte of 32 bit value
1156    'z' print as msb of 32 bit value
1157 */
1158
1159 /* Return assembly language string which identifies a comparison type.  */
1160
1161 static const char *
1162 cond_string (enum rtx_code code)
1163 {
1164   switch (code)
1165     {
1166     case NE:
1167       return "ne";
1168     case EQ:
1169       return "eq";
1170     case GE:
1171       return "ge";
1172     case GT:
1173       return "gt";
1174     case LE:
1175       return "le";
1176     case LT:
1177       return "lt";
1178     case GEU:
1179       return "hs";
1180     case GTU:
1181       return "hi";
1182     case LEU:
1183       return "ls";
1184     case LTU:
1185       return "lo";
1186     default:
1187       abort ();
1188     }
1189 }
1190
1191 /* Print operand X using operand code CODE to assembly language output file
1192    FILE.  */
1193
1194 void
1195 print_operand (FILE *file, rtx x, int code)
1196 {
1197   /* This is used for communication between codes V,W,Z and Y.  */
1198   static int bitint;
1199
1200   switch (code)
1201     {
1202     case 'E':
1203       switch (GET_CODE (x))
1204         {
1205         case REG:
1206           fprintf (file, "%sl", names_big[REGNO (x)]);
1207           break;
1208         case CONST_INT:
1209           fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1210           break;
1211         default:
1212           abort ();
1213         }
1214       break;
1215     case 'F':
1216       switch (GET_CODE (x))
1217         {
1218         case REG:
1219           fprintf (file, "%sh", names_big[REGNO (x)]);
1220           break;
1221         case CONST_INT:
1222           fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1223           break;
1224         default:
1225           abort ();
1226         }
1227       break;
1228     case 'G':
1229       if (GET_CODE (x) != CONST_INT)
1230         abort ();
1231       fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1232       break;
1233     case 'S':
1234       if (GET_CODE (x) == REG)
1235         fprintf (file, "%s", names_extended[REGNO (x)]);
1236       else
1237         goto def;
1238       break;
1239     case 'T':
1240       if (GET_CODE (x) == REG)
1241         fprintf (file, "%s", names_big[REGNO (x)]);
1242       else
1243         goto def;
1244       break;
1245     case 'V':
1246       bitint = exact_log2 (INTVAL (x) & 0xff);
1247       if (bitint == -1)
1248         abort ();
1249       fprintf (file, "#%d", bitint);
1250       break;
1251     case 'W':
1252       bitint = exact_log2 ((~INTVAL (x)) & 0xff);
1253       if (bitint == -1)
1254         abort ();
1255       fprintf (file, "#%d", bitint);
1256       break;
1257     case 'R':
1258     case 'X':
1259       if (GET_CODE (x) == REG)
1260         fprintf (file, "%s", byte_reg (x, 0));
1261       else
1262         goto def;
1263       break;
1264     case 'Y':
1265       if (bitint == -1)
1266         abort ();
1267       if (GET_CODE (x) == REG)
1268         fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1269       else
1270         print_operand (file, x, 'R');
1271       bitint = -1;
1272       break;
1273     case 'Z':
1274       bitint = INTVAL (x);
1275       fprintf (file, "#%d", bitint & 7);
1276       break;
1277     case 'c':
1278       switch (GET_CODE (x))
1279         {
1280         case IOR:
1281           fprintf (file, "or");
1282           break;
1283         case XOR:
1284           fprintf (file, "xor");
1285           break;
1286         case AND:
1287           fprintf (file, "and");
1288           break;
1289         default:
1290           break;
1291         }
1292       break;
1293     case 'e':
1294       switch (GET_CODE (x))
1295         {
1296         case REG:
1297           if (TARGET_H8300)
1298             fprintf (file, "%s", names_big[REGNO (x)]);
1299           else
1300             fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1301           break;
1302         case MEM:
1303           print_operand (file, x, 0);
1304           break;
1305         case CONST_INT:
1306           fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1307           break;
1308         case CONST_DOUBLE:
1309           {
1310             long val;
1311             REAL_VALUE_TYPE rv;
1312             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1313             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1314             fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1315             break;
1316           }
1317         default:
1318           abort ();
1319           break;
1320         }
1321       break;
1322     case 'f':
1323       switch (GET_CODE (x))
1324         {
1325         case REG:
1326           if (TARGET_H8300)
1327             fprintf (file, "%s", names_big[REGNO (x) + 1]);
1328           else
1329             fprintf (file, "%s", names_big[REGNO (x)]);
1330           break;
1331         case MEM:
1332           x = adjust_address (x, HImode, 2);
1333           print_operand (file, x, 0);
1334           break;
1335         case CONST_INT:
1336           fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1337           break;
1338         case CONST_DOUBLE:
1339           {
1340             long val;
1341             REAL_VALUE_TYPE rv;
1342             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1343             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1344             fprintf (file, "#%ld", (val & 0xffff));
1345             break;
1346           }
1347         default:
1348           abort ();
1349         }
1350       break;
1351     case 'j':
1352       fputs (cond_string (GET_CODE (x)), file);
1353       break;
1354     case 'k':
1355       fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1356       break;
1357     case 's':
1358       if (GET_CODE (x) == CONST_INT)
1359         fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1360       else
1361         fprintf (file, "%s", byte_reg (x, 0));
1362       break;
1363     case 't':
1364       if (GET_CODE (x) == CONST_INT)
1365         fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1366       else
1367         fprintf (file, "%s", byte_reg (x, 1));
1368       break;
1369     case 'w':
1370       if (GET_CODE (x) == CONST_INT)
1371         fprintf (file, "#%ld", INTVAL (x) & 0xff);
1372       else
1373         fprintf (file, "%s",
1374                  byte_reg (x, TARGET_H8300 ? 2 : 0));
1375       break;
1376     case 'x':
1377       if (GET_CODE (x) == CONST_INT)
1378         fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1379       else
1380         fprintf (file, "%s",
1381                  byte_reg (x, TARGET_H8300 ? 3 : 1));
1382       break;
1383     case 'y':
1384       if (GET_CODE (x) == CONST_INT)
1385         fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1386       else
1387         fprintf (file, "%s", byte_reg (x, 0));
1388       break;
1389     case 'z':
1390       if (GET_CODE (x) == CONST_INT)
1391         fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1392       else
1393         fprintf (file, "%s", byte_reg (x, 1));
1394       break;
1395
1396     default:
1397     def:
1398       switch (GET_CODE (x))
1399         {
1400         case REG:
1401           switch (GET_MODE (x))
1402             {
1403             case QImode:
1404 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1405               fprintf (file, "%s", byte_reg (x, 0));
1406 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1407               fprintf (file, "%s", names_big[REGNO (x)]);
1408 #endif
1409               break;
1410             case HImode:
1411               fprintf (file, "%s", names_big[REGNO (x)]);
1412               break;
1413             case SImode:
1414             case SFmode:
1415               fprintf (file, "%s", names_extended[REGNO (x)]);
1416               break;
1417             default:
1418               abort ();
1419             }
1420           break;
1421
1422         case MEM:
1423           {
1424             rtx addr = XEXP (x, 0);
1425
1426             fprintf (file, "@");
1427             output_address (addr);
1428
1429             /* We fall back from smaller addressing to larger
1430                addressing in various ways depending on CODE.  */
1431             switch (code)
1432               {
1433               case 'R':
1434                 /* Used for mov.b and bit operations.  */
1435                 if (h8300_eightbit_constant_address_p (addr))
1436                   {
1437                     fprintf (file, ":8");
1438                     break;
1439                   }
1440
1441                 /* Fall through.  We should not get here if we are
1442                    processing bit operations on H8/300 or H8/300H
1443                    because 'U' constraint does not allow bit
1444                    operations on the tiny area on these machines.  */
1445
1446               case 'T':
1447               case 'S':
1448                 /* Used for mov.w and mov.l.  */
1449                 if (h8300_tiny_constant_address_p (addr))
1450                   fprintf (file, ":16");
1451                 break;
1452               default:
1453                 break;
1454               }
1455           }
1456           break;
1457
1458         case CONST_INT:
1459         case SYMBOL_REF:
1460         case CONST:
1461         case LABEL_REF:
1462           fprintf (file, "#");
1463           print_operand_address (file, x);
1464           break;
1465         case CONST_DOUBLE:
1466           {
1467             long val;
1468             REAL_VALUE_TYPE rv;
1469             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1470             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1471             fprintf (file, "#%ld", val);
1472             break;
1473           }
1474         default:
1475           break;
1476         }
1477     }
1478 }
1479
1480 /* Output assembly language output for the address ADDR to FILE.  */
1481
1482 void
1483 print_operand_address (FILE *file, rtx addr)
1484 {
1485   switch (GET_CODE (addr))
1486     {
1487     case REG:
1488       fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1489       break;
1490
1491     case PRE_DEC:
1492       fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1493       break;
1494
1495     case POST_INC:
1496       fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1497       break;
1498
1499     case PLUS:
1500       fprintf (file, "(");
1501       if (GET_CODE (XEXP (addr, 0)) == REG)
1502         {
1503           /* reg,foo */
1504           print_operand_address (file, XEXP (addr, 1));
1505           fprintf (file, ",");
1506           print_operand_address (file, XEXP (addr, 0));
1507         }
1508       else
1509         {
1510           /* foo+k */
1511           print_operand_address (file, XEXP (addr, 0));
1512           fprintf (file, "+");
1513           print_operand_address (file, XEXP (addr, 1));
1514         }
1515       fprintf (file, ")");
1516       break;
1517
1518     case CONST_INT:
1519       {
1520         /* Since the H8/300 only has 16 bit pointers, negative values are also
1521            those >= 32768.  This happens for example with pointer minus a
1522            constant.  We don't want to turn (char *p - 2) into
1523            (char *p + 65534) because loop unrolling can build upon this
1524            (IE: char *p + 131068).  */
1525         int n = INTVAL (addr);
1526         if (TARGET_H8300)
1527           n = (int) (short) n;
1528         fprintf (file, "%d", n);
1529         break;
1530       }
1531
1532     default:
1533       output_addr_const (file, addr);
1534       break;
1535     }
1536 }
1537 \f
1538 /* Output all insn addresses and their sizes into the assembly language
1539    output file.  This is helpful for debugging whether the length attributes
1540    in the md file are correct.  This is not meant to be a user selectable
1541    option.  */
1542
1543 void
1544 final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
1545                     int num_operands ATTRIBUTE_UNUSED)
1546 {
1547   /* This holds the last insn address.  */
1548   static int last_insn_address = 0;
1549
1550   const int uid = INSN_UID (insn);
1551
1552   if (TARGET_ADDRESSES)
1553     {
1554       fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1555                INSN_ADDRESSES (uid) - last_insn_address);
1556       last_insn_address = INSN_ADDRESSES (uid);
1557     }
1558 }
1559
1560 /* Prepare for an SI sized move.  */
1561
1562 int
1563 h8300_expand_movsi (rtx operands[])
1564 {
1565   rtx src = operands[1];
1566   rtx dst = operands[0];
1567   if (!reload_in_progress && !reload_completed)
1568     {
1569       if (!register_operand (dst, GET_MODE (dst)))
1570         {
1571           rtx tmp = gen_reg_rtx (GET_MODE (dst));
1572           emit_move_insn (tmp, src);
1573           operands[1] = tmp;
1574         }
1575     }
1576   return 0;
1577 }
1578
1579 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1580    Define the offset between two registers, one to be eliminated, and
1581    the other its replacement, at the start of a routine.  */
1582
1583 int
1584 h8300_initial_elimination_offset (int from, int to)
1585 {
1586   /* The number of bytes that the return address takes on the stack.  */
1587   int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1588
1589   /* The number of bytes that the saved frame pointer takes on the stack.  */
1590   int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1591
1592   /* The number of bytes that the saved registers, excluding the frame
1593      pointer, take on the stack.  */
1594   int saved_regs_size = 0;
1595
1596   /* The number of bytes that the locals takes on the stack.  */
1597   int frame_size = round_frame_size (get_frame_size ());
1598
1599   int regno;
1600
1601   for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1602     if (WORD_REG_USED (regno))
1603       saved_regs_size += UNITS_PER_WORD;
1604
1605   /* Adjust saved_regs_size because the above loop took the frame
1606      pointer int account.  */
1607   saved_regs_size -= fp_size;
1608
1609   if (to == HARD_FRAME_POINTER_REGNUM)
1610     {
1611       switch (from)
1612         {
1613         case ARG_POINTER_REGNUM:
1614           return pc_size + fp_size;
1615         case RETURN_ADDRESS_POINTER_REGNUM:
1616           return fp_size;
1617         case FRAME_POINTER_REGNUM:
1618           return -saved_regs_size;
1619         default:
1620           abort ();
1621         }
1622     }
1623   else if (to == STACK_POINTER_REGNUM)
1624     {
1625       switch (from)
1626         {
1627         case ARG_POINTER_REGNUM:
1628           return pc_size + saved_regs_size + frame_size;
1629         case RETURN_ADDRESS_POINTER_REGNUM:
1630           return saved_regs_size + frame_size;
1631         case FRAME_POINTER_REGNUM:
1632           return frame_size;
1633         default:
1634           abort ();
1635         }
1636     }
1637   else
1638     abort ();
1639 }
1640
1641 rtx
1642 h8300_return_addr_rtx (int count, rtx frame)
1643 {
1644   rtx ret;
1645
1646   if (count == 0)
1647     ret = gen_rtx_MEM (Pmode,
1648                        gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
1649   else if (flag_omit_frame_pointer)
1650     return (rtx) 0;
1651   else
1652     ret = gen_rtx_MEM (Pmode,
1653                        memory_address (Pmode,
1654                                        plus_constant (frame, UNITS_PER_WORD)));
1655   set_mem_alias_set (ret, get_frame_alias_set ());
1656   return ret;
1657 }
1658
1659 /* Update the condition code from the insn.  */
1660
1661 void
1662 notice_update_cc (rtx body, rtx insn)
1663 {
1664   rtx set;
1665
1666   switch (get_attr_cc (insn))
1667     {
1668     case CC_NONE:
1669       /* Insn does not affect CC at all.  */
1670       break;
1671
1672     case CC_NONE_0HIT:
1673       /* Insn does not change CC, but the 0'th operand has been changed.  */
1674       if (cc_status.value1 != 0
1675           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1676         cc_status.value1 = 0;
1677       if (cc_status.value2 != 0
1678           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
1679         cc_status.value2 = 0;
1680       break;
1681
1682     case CC_SET_ZN:
1683       /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1684          The V flag is unusable.  The C flag may or may not be known but
1685          that's ok because alter_cond will change tests to use EQ/NE.  */
1686       CC_STATUS_INIT;
1687       cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1688       set = single_set (insn);
1689       cc_status.value1 = SET_SRC (set);
1690       if (SET_DEST (set) != cc0_rtx)
1691         cc_status.value2 = SET_DEST (set);
1692       break;
1693
1694     case CC_SET_ZNV:
1695       /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1696          The C flag may or may not be known but that's ok because
1697          alter_cond will change tests to use EQ/NE.  */
1698       CC_STATUS_INIT;
1699       cc_status.flags |= CC_NO_CARRY;
1700       set = single_set (insn);
1701       cc_status.value1 = SET_SRC (set);
1702       if (SET_DEST (set) != cc0_rtx)
1703         {
1704           /* If the destination is STRICT_LOW_PART, strip off
1705              STRICT_LOW_PART.  */
1706           if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1707             cc_status.value2 = XEXP (SET_DEST (set), 0);
1708           else
1709             cc_status.value2 = SET_DEST (set);
1710         }
1711       break;
1712
1713     case CC_COMPARE:
1714       /* The insn is a compare instruction.  */
1715       CC_STATUS_INIT;
1716       cc_status.value1 = SET_SRC (body);
1717       break;
1718
1719     case CC_CLOBBER:
1720       /* Insn doesn't leave CC in a usable state.  */
1721       CC_STATUS_INIT;
1722       break;
1723     }
1724 }
1725
1726 /* Return nonzero if X is a stack pointer.  */
1727
1728 int
1729 stack_pointer_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1730 {
1731   return x == stack_pointer_rtx;
1732 }
1733
1734 /* Return nonzero if X is a constant whose absolute value is greater
1735    than 2.  */
1736
1737 int
1738 const_int_gt_2_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1739 {
1740   return (GET_CODE (x) == CONST_INT
1741           && abs (INTVAL (x)) > 2);
1742 }
1743
1744 /* Return nonzero if X is a constant whose absolute value is no
1745    smaller than 8.  */
1746
1747 int
1748 const_int_ge_8_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1749 {
1750   return (GET_CODE (x) == CONST_INT
1751           && abs (INTVAL (x)) >= 8);
1752 }
1753
1754 /* Return nonzero if X is a constant expressible in QImode.  */
1755
1756 int
1757 const_int_qi_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1758 {
1759   return (GET_CODE (x) == CONST_INT
1760           && (INTVAL (x) & 0xff) == INTVAL (x));
1761 }
1762
1763 /* Return nonzero if X is a constant expressible in HImode.  */
1764
1765 int
1766 const_int_hi_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1767 {
1768   return (GET_CODE (x) == CONST_INT
1769           && (INTVAL (x) & 0xffff) == INTVAL (x));
1770 }
1771
1772 /* Return nonzero if X is a constant suitable for inc/dec.  */
1773
1774 int
1775 incdec_operand (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1776 {
1777   return (GET_CODE (x) == CONST_INT
1778           && (CONST_OK_FOR_M (INTVAL (x))
1779               || CONST_OK_FOR_O (INTVAL (x))));
1780 }
1781
1782 /* Return nonzero if X is either EQ or NE.  */
1783
1784 int
1785 eqne_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1786 {
1787   enum rtx_code code = GET_CODE (x);
1788
1789   return (code == EQ || code == NE);
1790 }
1791
1792 /* Return nonzero if X is GT, LE, GTU, or LEU.  */
1793
1794 int
1795 gtle_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1796 {
1797   enum rtx_code code = GET_CODE (x);
1798
1799   return (code == GT || code == LE || code == GTU || code == LEU);
1800 }
1801
1802 /* Return nonzero if X is either GTU or LEU.  */
1803
1804 int
1805 gtuleu_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1806 {
1807   enum rtx_code code = GET_CODE (x);
1808
1809   return (code == GTU || code == LEU);
1810 }
1811
1812 /* Return nonzero if X is either IOR or XOR.  */
1813
1814 int
1815 iorxor_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1816 {
1817   enum rtx_code code = GET_CODE (x);
1818
1819   return (code == IOR || code == XOR);
1820 }
1821
1822 /* Recognize valid operators for bit instructions.  */
1823
1824 int
1825 bit_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
1826 {
1827   enum rtx_code code = GET_CODE (x);
1828
1829   return (code == XOR
1830           || code == AND
1831           || code == IOR);
1832 }
1833 \f
1834 /* Return the length of mov instruction.  */
1835
1836 unsigned int
1837 compute_mov_length (rtx *operands)
1838 {
1839   /* If the mov instruction involves a memory operand, we compute the
1840      length, assuming the largest addressing mode is used, and then
1841      adjust later in the function.  Otherwise, we compute and return
1842      the exact length in one step.  */
1843   enum machine_mode mode = GET_MODE (operands[0]);
1844   rtx dest = operands[0];
1845   rtx src = operands[1];
1846   rtx addr;
1847
1848   if (GET_CODE (src) == MEM)
1849     addr = XEXP (src, 0);
1850   else if (GET_CODE (dest) == MEM)
1851     addr = XEXP (dest, 0);
1852   else
1853     addr = NULL_RTX;
1854
1855   if (TARGET_H8300)
1856     {
1857       unsigned int base_length;
1858
1859       switch (mode)
1860         {
1861         case QImode:
1862           if (addr == NULL_RTX)
1863             return 2;
1864
1865           /* The eightbit addressing is available only in QImode, so
1866              go ahead and take care of it.  */
1867           if (h8300_eightbit_constant_address_p (addr))
1868             return 2;
1869
1870           base_length = 4;
1871           break;
1872
1873         case HImode:
1874           if (addr == NULL_RTX)
1875             {
1876               if (REG_P (src))
1877                 return 2;
1878
1879               if (src == const0_rtx)
1880                 return 2;
1881
1882               return 4;
1883             }
1884
1885           base_length = 4;
1886           break;
1887
1888         case SImode:
1889           if (addr == NULL_RTX)
1890             {
1891               if (REG_P (src))
1892                 return 4;
1893
1894               if (GET_CODE (src) == CONST_INT)
1895                 {
1896                   if (src == const0_rtx)
1897                     return 4;
1898
1899                   if ((INTVAL (src) & 0xffff) == 0)
1900                     return 6;
1901
1902                   if ((INTVAL (src) & 0xffff) == 0)
1903                     return 6;
1904
1905                   if ((INTVAL (src) & 0xffff)
1906                       == ((INTVAL (src) >> 16) & 0xffff))
1907                     return 6;
1908                 }
1909               return 8;
1910             }
1911
1912           base_length = 8;
1913           break;
1914
1915         case SFmode:
1916           if (addr == NULL_RTX)
1917             {
1918               if (REG_P (src))
1919                 return 4;
1920
1921               if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
1922                 return 4;
1923
1924               return 8;
1925             }
1926
1927           base_length = 8;
1928           break;
1929
1930         default:
1931           abort ();
1932         }
1933
1934       /* Adjust the length based on the addressing mode used.
1935          Specifically, we subtract the difference between the actual
1936          length and the longest one, which is @(d:16,Rs).  For SImode
1937          and SFmode, we double the adjustment because two mov.w are
1938          used to do the job.  */
1939
1940       /* @Rs+ and @-Rd are 2 bytes shorter than the longest.  */
1941       if (GET_CODE (addr) == PRE_DEC
1942           || GET_CODE (addr) == POST_INC)
1943         {
1944           if (mode == QImode || mode == HImode)
1945             return base_length - 2;
1946           else
1947             /* In SImode and SFmode, we use two mov.w instructions, so
1948                double the adjustment.  */
1949             return base_length - 4;
1950         }
1951
1952       /* @Rs and @Rd are 2 bytes shorter than the longest.  Note that
1953          in SImode and SFmode, the second mov.w involves an address
1954          with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
1955          only 2 bytes.  */
1956       if (GET_CODE (addr) == REG)
1957         return base_length - 2;
1958
1959       return base_length;
1960     }
1961   else
1962     {
1963       unsigned int base_length;
1964
1965       switch (mode)
1966         {
1967         case QImode:
1968           if (addr == NULL_RTX)
1969             return 2;
1970
1971           /* The eightbit addressing is available only in QImode, so
1972              go ahead and take care of it.  */
1973           if (h8300_eightbit_constant_address_p (addr))
1974             return 2;
1975
1976           base_length = 8;
1977           break;
1978
1979         case HImode:
1980           if (addr == NULL_RTX)
1981             {
1982               if (REG_P (src))
1983                 return 2;
1984
1985               if (src == const0_rtx)
1986                 return 2;
1987
1988               return 4;
1989             }
1990
1991           base_length = 8;
1992           break;
1993
1994         case SImode:
1995           if (addr == NULL_RTX)
1996             {
1997               if (REG_P (src))
1998                 {
1999                   if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2000                     return 4;
2001                   else
2002                     return 2;
2003                 }
2004
2005               if (GET_CODE (src) == CONST_INT)
2006                 {
2007                   int val = INTVAL (src);
2008
2009                   if (val == 0)
2010                     return 2;
2011
2012                   if (val == (val & 0x00ff) || val == (val & 0xff00))
2013                     return 4;
2014
2015                   switch (val & 0xffffffff)
2016                     {
2017                     case 0xffffffff:
2018                     case 0xfffffffe:
2019                     case 0xfffffffc:
2020                     case 0x0000ffff:
2021                     case 0x0000fffe:
2022                     case 0xffff0000:
2023                     case 0xfffe0000:
2024                     case 0x00010000:
2025                     case 0x00020000:
2026                       return 4;
2027                     }
2028                 }
2029               return 6;
2030             }
2031
2032           base_length = 10;
2033           break;
2034
2035         case SFmode:
2036           if (addr == NULL_RTX)
2037             {
2038               if (REG_P (src))
2039                 return 2;
2040
2041               if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2042                 return 2;
2043
2044               return 6;
2045             }
2046
2047           base_length = 10;
2048           break;
2049
2050         default:
2051           abort ();
2052         }
2053
2054       /* Adjust the length based on the addressing mode used.
2055          Specifically, we subtract the difference between the actual
2056          length and the longest one, which is @(d:24,ERs).  */
2057
2058       /* @ERs+ and @-ERd are 6 bytes shorter than the longest.  */
2059       if (GET_CODE (addr) == PRE_DEC
2060           || GET_CODE (addr) == POST_INC)
2061         return base_length - 6;
2062
2063       /* @ERs and @ERd are 6 bytes shorter than the longest.  */
2064       if (GET_CODE (addr) == REG)
2065         return base_length - 6;
2066
2067       /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2068          longest.  */
2069       if (GET_CODE (addr) == PLUS
2070           && GET_CODE (XEXP (addr, 0)) == REG
2071           && GET_CODE (XEXP (addr, 1)) == CONST_INT
2072           && INTVAL (XEXP (addr, 1)) > -32768
2073           && INTVAL (XEXP (addr, 1)) < 32767)
2074         return base_length - 4;
2075
2076       /* @aa:16 is 4 bytes shorter than the longest.  */
2077       if (h8300_tiny_constant_address_p (addr))
2078         return base_length - 4;
2079
2080       /* @aa:24 is 2 bytes shorter than the longest.  */
2081       if (CONSTANT_P (addr))
2082         return base_length - 2;
2083
2084       return base_length;
2085     }
2086 }
2087 \f
2088 const char *
2089 output_plussi (rtx *operands)
2090 {
2091   enum machine_mode mode = GET_MODE (operands[0]);
2092
2093   if (mode != SImode)
2094     abort ();
2095
2096   if (TARGET_H8300)
2097     {
2098       if (GET_CODE (operands[2]) == REG)
2099         return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2100
2101       if (GET_CODE (operands[2]) == CONST_INT)
2102         {
2103           HOST_WIDE_INT n = INTVAL (operands[2]);
2104
2105           if ((n & 0xffffff) == 0)
2106             return "add\t%z2,%z0";
2107           if ((n & 0xffff) == 0)
2108             return "add\t%y2,%y0\n\taddx\t%z2,%z0";
2109           if ((n & 0xff) == 0)
2110             return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2111         }
2112
2113       return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2114     }
2115   else
2116     {
2117       if (GET_CODE (operands[2]) == REG)
2118         return "add.l\t%S2,%S0";
2119
2120       if (GET_CODE (operands[2]) == CONST_INT)
2121         {
2122           HOST_WIDE_INT intval = INTVAL (operands[2]);
2123
2124           /* See if we can finish with 2 bytes.  */
2125
2126           switch ((unsigned int) intval & 0xffffffff)
2127             {
2128             case 0x00000001:
2129             case 0x00000002:
2130             case 0x00000004:
2131               return "adds\t%2,%S0";
2132
2133             case 0xffffffff:
2134             case 0xfffffffe:
2135             case 0xfffffffc:
2136               return "subs\t%G2,%S0";
2137
2138             case 0x00010000:
2139             case 0x00020000:
2140               operands[2] = GEN_INT (intval >> 16);
2141               return "inc.w\t%2,%e0";
2142
2143             case 0xffff0000:
2144             case 0xfffe0000:
2145               operands[2] = GEN_INT (intval >> 16);
2146               return "dec.w\t%G2,%e0";
2147             }
2148
2149           /* See if we can finish with 4 bytes.  */
2150           if ((intval & 0xffff) == 0)
2151             {
2152               operands[2] = GEN_INT (intval >> 16);
2153               return "add.w\t%2,%e0";
2154             }
2155         }
2156
2157       return "add.l\t%S2,%S0";
2158     }
2159 }
2160
2161 unsigned int
2162 compute_plussi_length (rtx *operands)
2163 {
2164   enum machine_mode mode = GET_MODE (operands[0]);
2165
2166   if (mode != SImode)
2167     abort ();
2168
2169   if (TARGET_H8300)
2170     {
2171       if (GET_CODE (operands[2]) == REG)
2172         return 6;
2173
2174       if (GET_CODE (operands[2]) == CONST_INT)
2175         {
2176           HOST_WIDE_INT n = INTVAL (operands[2]);
2177
2178           if ((n & 0xffffff) == 0)
2179             return 2;
2180           if ((n & 0xffff) == 0)
2181             return 4;
2182           if ((n & 0xff) == 0)
2183             return 6;
2184         }
2185
2186       return 8;
2187     }
2188   else
2189     {
2190       if (GET_CODE (operands[2]) == REG)
2191         return 2;
2192
2193       if (GET_CODE (operands[2]) == CONST_INT)
2194         {
2195           HOST_WIDE_INT intval = INTVAL (operands[2]);
2196
2197           /* See if we can finish with 2 bytes.  */
2198
2199           switch ((unsigned int) intval & 0xffffffff)
2200             {
2201             case 0x00000001:
2202             case 0x00000002:
2203             case 0x00000004:
2204               return 2;
2205
2206             case 0xffffffff:
2207             case 0xfffffffe:
2208             case 0xfffffffc:
2209               return 2;
2210
2211             case 0x00010000:
2212             case 0x00020000:
2213               return 2;
2214
2215             case 0xffff0000:
2216             case 0xfffe0000:
2217               return 2;
2218             }
2219
2220           /* See if we can finish with 4 bytes.  */
2221           if ((intval & 0xffff) == 0)
2222             return 4;
2223         }
2224
2225       return 6;
2226     }
2227 }
2228
2229 int
2230 compute_plussi_cc (rtx *operands)
2231 {
2232   enum machine_mode mode = GET_MODE (operands[0]);
2233
2234   if (mode != SImode)
2235     abort ();
2236
2237   if (TARGET_H8300)
2238     {
2239       return CC_CLOBBER;
2240     }
2241   else
2242     {
2243       if (GET_CODE (operands[2]) == REG)
2244         return CC_SET_ZN;
2245
2246       if (GET_CODE (operands[2]) == CONST_INT)
2247         {
2248           HOST_WIDE_INT intval = INTVAL (operands[2]);
2249
2250           /* See if we can finish with 2 bytes.  */
2251
2252           switch ((unsigned int) intval & 0xffffffff)
2253             {
2254             case 0x00000001:
2255             case 0x00000002:
2256             case 0x00000004:
2257               return CC_NONE_0HIT;
2258
2259             case 0xffffffff:
2260             case 0xfffffffe:
2261             case 0xfffffffc:
2262               return CC_NONE_0HIT;
2263
2264             case 0x00010000:
2265             case 0x00020000:
2266               return CC_CLOBBER;
2267
2268             case 0xffff0000:
2269             case 0xfffe0000:
2270               return CC_CLOBBER;
2271             }
2272
2273           /* See if we can finish with 4 bytes.  */
2274           if ((intval & 0xffff) == 0)
2275             return CC_CLOBBER;
2276         }
2277
2278       return CC_SET_ZN;
2279     }
2280 }
2281 \f
2282 const char *
2283 output_logical_op (enum machine_mode mode, rtx *operands)
2284 {
2285   /* Figure out the logical op that we need to perform.  */
2286   enum rtx_code code = GET_CODE (operands[3]);
2287   /* Pretend that every byte is affected if both operands are registers.  */
2288   const unsigned HOST_WIDE_INT intval =
2289     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
2290                               ? INTVAL (operands[2]) : 0x55555555);
2291   /* The determinant of the algorithm.  If we perform an AND, 0
2292      affects a bit.  Otherwise, 1 affects a bit.  */
2293   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
2294   /* Break up DET into pieces.  */
2295   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
2296   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
2297   const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
2298   const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
2299   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
2300   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
2301   int lower_half_easy_p = 0;
2302   int upper_half_easy_p = 0;
2303   /* The name of an insn.  */
2304   const char *opname;
2305   char insn_buf[100];
2306
2307   switch (code)
2308     {
2309     case AND:
2310       opname = "and";
2311       break;
2312     case IOR:
2313       opname = "or";
2314       break;
2315     case XOR:
2316       opname = "xor";
2317       break;
2318     default:
2319       abort ();
2320     }
2321
2322   switch (mode)
2323     {
2324     case HImode:
2325       /* First, see if we can finish with one insn.  */
2326       if ((TARGET_H8300H || TARGET_H8300S)
2327           && b0 != 0
2328           && b1 != 0)
2329         {
2330           sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
2331           output_asm_insn (insn_buf, operands);
2332         }
2333       else
2334         {
2335           /* Take care of the lower byte.  */
2336           if (b0 != 0)
2337             {
2338               sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
2339               output_asm_insn (insn_buf, operands);
2340             }
2341           /* Take care of the upper byte.  */
2342           if (b1 != 0)
2343             {
2344               sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
2345               output_asm_insn (insn_buf, operands);
2346             }
2347         }
2348       break;
2349     case SImode:
2350       if (TARGET_H8300H || TARGET_H8300S)
2351         {
2352           /* Determine if the lower half can be taken care of in no more
2353              than two bytes.  */
2354           lower_half_easy_p = (b0 == 0
2355                                || b1 == 0
2356                                || (code != IOR && w0 == 0xffff));
2357
2358           /* Determine if the upper half can be taken care of in no more
2359              than two bytes.  */
2360           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
2361                                || (code == AND && w1 == 0xff00));
2362         }
2363
2364       /* Check if doing everything with one insn is no worse than
2365          using multiple insns.  */
2366       if ((TARGET_H8300H || TARGET_H8300S)
2367           && w0 != 0 && w1 != 0
2368           && !(lower_half_easy_p && upper_half_easy_p)
2369           && !(code == IOR && w1 == 0xffff
2370                && (w0 & 0x8000) != 0 && lower_half_easy_p))
2371         {
2372           sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
2373           output_asm_insn (insn_buf, operands);
2374         }
2375       else
2376         {
2377           /* Take care of the lower and upper words individually.  For
2378              each word, we try different methods in the order of
2379
2380              1) the special insn (in case of AND or XOR),
2381              2) the word-wise insn, and
2382              3) The byte-wise insn.  */
2383           if (w0 == 0xffff
2384               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
2385             output_asm_insn ((code == AND)
2386                              ? "sub.w\t%f0,%f0" : "not.w\t%f0",
2387                              operands);
2388           else if ((TARGET_H8300H || TARGET_H8300S)
2389                    && (b0 != 0)
2390                    && (b1 != 0))
2391             {
2392               sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
2393               output_asm_insn (insn_buf, operands);
2394             }
2395           else
2396             {
2397               if (b0 != 0)
2398                 {
2399                   sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
2400                   output_asm_insn (insn_buf, operands);
2401                 }
2402               if (b1 != 0)
2403                 {
2404                   sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
2405                   output_asm_insn (insn_buf, operands);
2406                 }
2407             }
2408
2409           if ((w1 == 0xffff)
2410               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
2411             output_asm_insn ((code == AND)
2412                              ? "sub.w\t%e0,%e0" : "not.w\t%e0",
2413                              operands);
2414           else if ((TARGET_H8300H || TARGET_H8300S)
2415                    && code == IOR
2416                    && w1 == 0xffff
2417                    && (w0 & 0x8000) != 0)
2418             {
2419               output_asm_insn ("exts.l\t%S0", operands);
2420             }
2421           else if ((TARGET_H8300H || TARGET_H8300S)
2422                    && code == AND
2423                    && w1 == 0xff00)
2424             {
2425               output_asm_insn ("extu.w\t%e0", operands);
2426             }
2427           else if (TARGET_H8300H || TARGET_H8300S)
2428             {
2429               if (w1 != 0)
2430                 {
2431                   sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
2432                   output_asm_insn (insn_buf, operands);
2433                 }
2434             }
2435           else
2436             {
2437               if (b2 != 0)
2438                 {
2439                   sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
2440                   output_asm_insn (insn_buf, operands);
2441                 }
2442               if (b3 != 0)
2443                 {
2444                   sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
2445                   output_asm_insn (insn_buf, operands);
2446                 }
2447             }
2448         }
2449       break;
2450     default:
2451       abort ();
2452     }
2453   return "";
2454 }
2455
2456 unsigned int
2457 compute_logical_op_length (enum machine_mode mode, rtx *operands)
2458 {
2459   /* Figure out the logical op that we need to perform.  */
2460   enum rtx_code code = GET_CODE (operands[3]);
2461   /* Pretend that every byte is affected if both operands are registers.  */
2462   const unsigned HOST_WIDE_INT intval =
2463     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
2464                               ? INTVAL (operands[2]) : 0x55555555);
2465   /* The determinant of the algorithm.  If we perform an AND, 0
2466      affects a bit.  Otherwise, 1 affects a bit.  */
2467   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
2468   /* Break up DET into pieces.  */
2469   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
2470   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
2471   const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
2472   const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
2473   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
2474   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
2475   int lower_half_easy_p = 0;
2476   int upper_half_easy_p = 0;
2477   /* Insn length.  */
2478   unsigned int length = 0;
2479
2480   switch (mode)
2481     {
2482     case HImode:
2483       /* First, see if we can finish with one insn.  */
2484       if ((TARGET_H8300H || TARGET_H8300S)
2485           && b0 != 0
2486           && b1 != 0)
2487         {
2488           if (REG_P (operands[2]))
2489             length += 2;
2490           else
2491             length += 4;
2492         }
2493       else
2494         {
2495           /* Take care of the lower byte.  */
2496           if (b0 != 0)
2497             length += 2;
2498
2499           /* Take care of the upper byte.  */
2500           if (b1 != 0)
2501             length += 2;
2502         }
2503       break;
2504     case SImode:
2505       if (TARGET_H8300H || TARGET_H8300S)
2506         {
2507           /* Determine if the lower half can be taken care of in no more
2508              than two bytes.  */
2509           lower_half_easy_p = (b0 == 0
2510                                || b1 == 0
2511                                || (code != IOR && w0 == 0xffff));
2512
2513           /* Determine if the upper half can be taken care of in no more
2514              than two bytes.  */
2515           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
2516                                || (code == AND && w1 == 0xff00));
2517         }
2518
2519       /* Check if doing everything with one insn is no worse than
2520          using multiple insns.  */
2521       if ((TARGET_H8300H || TARGET_H8300S)
2522           && w0 != 0 && w1 != 0
2523           && !(lower_half_easy_p && upper_half_easy_p)
2524           && !(code == IOR && w1 == 0xffff
2525                && (w0 & 0x8000) != 0 && lower_half_easy_p))
2526         {
2527           if (REG_P (operands[2]))
2528             length += 4;
2529           else
2530             length += 6;
2531         }
2532       else
2533         {
2534           /* Take care of the lower and upper words individually.  For
2535              each word, we try different methods in the order of
2536
2537              1) the special insn (in case of AND or XOR),
2538              2) the word-wise insn, and
2539              3) The byte-wise insn.  */
2540           if (w0 == 0xffff
2541               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
2542             {
2543               length += 2;
2544             }
2545           else if ((TARGET_H8300H || TARGET_H8300S)
2546                    && (b0 != 0)
2547                    && (b1 != 0))
2548             {
2549               length += 4;
2550             }
2551           else
2552             {
2553               if (b0 != 0)
2554                 length += 2;
2555
2556               if (b1 != 0)
2557                 length += 2;
2558             }
2559
2560           if (w1 == 0xffff
2561               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
2562             {
2563               length += 2;
2564             }
2565           else if ((TARGET_H8300H || TARGET_H8300S)
2566                    && code == IOR
2567                    && w1 == 0xffff
2568                    && (w0 & 0x8000) != 0)
2569             {
2570               length += 2;
2571             }
2572           else if ((TARGET_H8300H || TARGET_H8300S)
2573                    && code == AND
2574                    && w1 == 0xff00)
2575             {
2576               length += 2;
2577             }
2578           else if (TARGET_H8300H || TARGET_H8300S)
2579             {
2580               if (w1 != 0)
2581                 length += 4;
2582             }
2583           else
2584             {
2585               if (b2 != 0)
2586                 length += 2;
2587
2588               if (b3 != 0)
2589                 length += 2;
2590             }
2591         }
2592       break;
2593     default:
2594       abort ();
2595     }
2596   return length;
2597 }
2598
2599 int
2600 compute_logical_op_cc (enum machine_mode mode, rtx *operands)
2601 {
2602   /* Figure out the logical op that we need to perform.  */
2603   enum rtx_code code = GET_CODE (operands[3]);
2604   /* Pretend that every byte is affected if both operands are registers.  */
2605   const unsigned HOST_WIDE_INT intval =
2606     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
2607                               ? INTVAL (operands[2]) : 0x55555555);
2608   /* The determinant of the algorithm.  If we perform an AND, 0
2609      affects a bit.  Otherwise, 1 affects a bit.  */
2610   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
2611   /* Break up DET into pieces.  */
2612   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
2613   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
2614   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
2615   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
2616   int lower_half_easy_p = 0;
2617   int upper_half_easy_p = 0;
2618   /* Condition code.  */
2619   enum attr_cc cc = CC_CLOBBER;
2620
2621   switch (mode)
2622     {
2623     case HImode:
2624       /* First, see if we can finish with one insn.  */
2625       if ((TARGET_H8300H || TARGET_H8300S)
2626           && b0 != 0
2627           && b1 != 0)
2628         {
2629           cc = CC_SET_ZNV;
2630         }
2631       break;
2632     case SImode:
2633       if (TARGET_H8300H || TARGET_H8300S)
2634         {
2635           /* Determine if the lower half can be taken care of in no more
2636              than two bytes.  */
2637           lower_half_easy_p = (b0 == 0
2638                                || b1 == 0
2639                                || (code != IOR && w0 == 0xffff));
2640
2641           /* Determine if the upper half can be taken care of in no more
2642              than two bytes.  */
2643           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
2644                                || (code == AND && w1 == 0xff00));
2645         }
2646
2647       /* Check if doing everything with one insn is no worse than
2648          using multiple insns.  */
2649       if ((TARGET_H8300H || TARGET_H8300S)
2650           && w0 != 0 && w1 != 0
2651           && !(lower_half_easy_p && upper_half_easy_p)
2652           && !(code == IOR && w1 == 0xffff
2653                && (w0 & 0x8000) != 0 && lower_half_easy_p))
2654         {
2655           cc = CC_SET_ZNV;
2656         }
2657       else
2658         {
2659           if ((TARGET_H8300H || TARGET_H8300S)
2660               && code == IOR
2661               && w1 == 0xffff
2662               && (w0 & 0x8000) != 0)
2663             {
2664               cc = CC_SET_ZNV;
2665             }
2666         }
2667       break;
2668     default:
2669       abort ();
2670     }
2671   return cc;
2672 }
2673 \f
2674 /* Shifts.
2675
2676    We devote a fair bit of code to getting efficient shifts since we
2677    can only shift one bit at a time on the H8/300 and H8/300H and only
2678    one or two bits at a time on the H8S.
2679
2680    All shift code falls into one of the following ways of
2681    implementation:
2682
2683    o SHIFT_INLINE: Emit straight line code for the shift; this is used
2684      when a straight line shift is about the same size or smaller than
2685      a loop.
2686
2687    o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
2688      off the bits we don't need.  This is used when only a few of the
2689      bits in the original value will survive in the shifted value.
2690
2691    o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
2692      simulate a shift by 8, 16, or 24 bits.  Once moved, a few inline
2693      shifts can be added if the shift count is slightly more than 8 or
2694      16.  This case also includes other oddballs that are not worth
2695      explaining here.
2696
2697    o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
2698
2699    For each shift count, we try to use code that has no trade-off
2700    between code size and speed whenever possible.
2701
2702    If the trade-off is unavoidable, we try to be reasonable.
2703    Specifically, the fastest version is one instruction longer than
2704    the shortest version, we take the fastest version.  We also provide
2705    the use a way to switch back to the shortest version with -Os.
2706
2707    For the details of the shift algorithms for various shift counts,
2708    refer to shift_alg_[qhs]i.  */
2709
2710 int
2711 nshift_operator (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
2712 {
2713   switch (GET_CODE (x))
2714     {
2715     case ASHIFTRT:
2716     case LSHIFTRT:
2717     case ASHIFT:
2718       return 1;
2719
2720     default:
2721       return 0;
2722     }
2723 }
2724
2725 /* Emit code to do shifts.  */
2726
2727 void
2728 expand_a_shift (enum machine_mode mode, int code, rtx operands[])
2729 {
2730   emit_move_insn (operands[0], operands[1]);
2731
2732   /* Need a loop to get all the bits we want  - we generate the
2733      code at emit time, but need to allocate a scratch reg now.  */
2734
2735   emit_insn (gen_rtx_PARALLEL
2736              (VOIDmode,
2737               gen_rtvec (2,
2738                          gen_rtx_SET (VOIDmode, operands[0],
2739                                       gen_rtx_fmt_ee (code, mode,
2740                                                       operands[0], operands[2])),
2741                          gen_rtx_CLOBBER (VOIDmode,
2742                                           gen_rtx_SCRATCH (QImode)))));
2743 }
2744
2745 /* Symbols of the various modes which can be used as indices.  */
2746
2747 enum shift_mode
2748 {
2749   QIshift, HIshift, SIshift
2750 };
2751
2752 /* For single bit shift insns, record assembler and what bits of the
2753    condition code are valid afterwards (represented as various CC_FOO
2754    bits, 0 means CC isn't left in a usable state).  */
2755
2756 struct shift_insn
2757 {
2758   const char *const assembler;
2759   const int cc_valid;
2760 };
2761
2762 /* Assembler instruction shift table.
2763
2764    These tables are used to look up the basic shifts.
2765    They are indexed by cpu, shift_type, and mode.  */
2766
2767 static const struct shift_insn shift_one[2][3][3] =
2768 {
2769 /* H8/300 */
2770   {
2771 /* SHIFT_ASHIFT */
2772     {
2773       { "shll\t%X0", CC_SET_ZNV },
2774       { "add.w\t%T0,%T0", CC_SET_ZN },
2775       { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", CC_CLOBBER }
2776     },
2777 /* SHIFT_LSHIFTRT */
2778     {
2779       { "shlr\t%X0", CC_SET_ZNV },
2780       { "shlr\t%t0\n\trotxr\t%s0", CC_CLOBBER },
2781       { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
2782     },
2783 /* SHIFT_ASHIFTRT */
2784     {
2785       { "shar\t%X0", CC_SET_ZNV },
2786       { "shar\t%t0\n\trotxr\t%s0", CC_CLOBBER },
2787       { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
2788     }
2789   },
2790 /* H8/300H */
2791   {
2792 /* SHIFT_ASHIFT */
2793     {
2794       { "shll.b\t%X0", CC_SET_ZNV },
2795       { "shll.w\t%T0", CC_SET_ZNV },
2796       { "shll.l\t%S0", CC_SET_ZNV }
2797     },
2798 /* SHIFT_LSHIFTRT */
2799     {
2800       { "shlr.b\t%X0", CC_SET_ZNV },
2801       { "shlr.w\t%T0", CC_SET_ZNV },
2802       { "shlr.l\t%S0", CC_SET_ZNV }
2803     },
2804 /* SHIFT_ASHIFTRT */
2805     {
2806       { "shar.b\t%X0", CC_SET_ZNV },
2807       { "shar.w\t%T0", CC_SET_ZNV },
2808       { "shar.l\t%S0", CC_SET_ZNV }
2809     }
2810   }
2811 };
2812
2813 static const struct shift_insn shift_two[3][3] =
2814 {
2815 /* SHIFT_ASHIFT */
2816     {
2817       { "shll.b\t#2,%X0", CC_SET_ZNV },
2818       { "shll.w\t#2,%T0", CC_SET_ZNV },
2819       { "shll.l\t#2,%S0", CC_SET_ZNV }
2820     },
2821 /* SHIFT_LSHIFTRT */
2822     {
2823       { "shlr.b\t#2,%X0", CC_SET_ZNV },
2824       { "shlr.w\t#2,%T0", CC_SET_ZNV },
2825       { "shlr.l\t#2,%S0", CC_SET_ZNV }
2826     },
2827 /* SHIFT_ASHIFTRT */
2828     {
2829       { "shar.b\t#2,%X0", CC_SET_ZNV },
2830       { "shar.w\t#2,%T0", CC_SET_ZNV },
2831       { "shar.l\t#2,%S0", CC_SET_ZNV }
2832     }
2833 };
2834
2835 /* Rotates are organized by which shift they'll be used in implementing.
2836    There's no need to record whether the cc is valid afterwards because
2837    it is the AND insn that will decide this.  */
2838
2839 static const char *const rotate_one[2][3][3] =
2840 {
2841 /* H8/300 */
2842   {
2843 /* SHIFT_ASHIFT */
2844     {
2845       "rotr\t%X0",
2846       "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
2847       0
2848     },
2849 /* SHIFT_LSHIFTRT */
2850     {
2851       "rotl\t%X0",
2852       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
2853       0
2854     },
2855 /* SHIFT_ASHIFTRT */
2856     {
2857       "rotl\t%X0",
2858       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
2859       0
2860     }
2861   },
2862 /* H8/300H */
2863   {
2864 /* SHIFT_ASHIFT */
2865     {
2866       "rotr.b\t%X0",
2867       "rotr.w\t%T0",
2868       "rotr.l\t%S0"
2869     },
2870 /* SHIFT_LSHIFTRT */
2871     {
2872       "rotl.b\t%X0",
2873       "rotl.w\t%T0",
2874       "rotl.l\t%S0"
2875     },
2876 /* SHIFT_ASHIFTRT */
2877     {
2878       "rotl.b\t%X0",
2879       "rotl.w\t%T0",
2880       "rotl.l\t%S0"
2881     }
2882   }
2883 };
2884
2885 static const char *const rotate_two[3][3] =
2886 {
2887 /* SHIFT_ASHIFT */
2888     {
2889       "rotr.b\t#2,%X0",
2890       "rotr.w\t#2,%T0",
2891       "rotr.l\t#2,%S0"
2892     },
2893 /* SHIFT_LSHIFTRT */
2894     {
2895       "rotl.b\t#2,%X0",
2896       "rotl.w\t#2,%T0",
2897       "rotl.l\t#2,%S0"
2898     },
2899 /* SHIFT_ASHIFTRT */
2900     {
2901       "rotl.b\t#2,%X0",
2902       "rotl.w\t#2,%T0",
2903       "rotl.l\t#2,%S0"
2904     }
2905 };
2906
2907 struct shift_info {
2908   /* Shift algorithm.  */
2909   enum shift_alg alg;
2910
2911   /* The number of bits to be shifted by shift1 and shift2.  Valid
2912      when ALG is SHIFT_SPECIAL.  */
2913   unsigned int remainder;
2914
2915   /* Special insn for a shift.  Valid when ALG is SHIFT_SPECIAL.  */
2916   const char *special;
2917
2918   /* Insn for a one-bit shift.  Valid when ALG is either SHIFT_INLINE
2919      or SHIFT_SPECIAL, and REMAINDER is nonzero.  */
2920   const char *shift1;
2921
2922   /* Insn for a two-bit shift.  Valid when ALG is either SHIFT_INLINE
2923      or SHIFT_SPECIAL, and REMAINDER is nonzero.  */
2924   const char *shift2;
2925
2926   /* CC status for SHIFT_INLINE.  */
2927   int cc_inline;
2928
2929   /* CC status  for SHIFT_SPECIAL.  */
2930   int cc_special;
2931 };
2932
2933 static void get_shift_alg (enum shift_type,
2934                            enum shift_mode, unsigned int,
2935                            struct shift_info *);
2936
2937 /* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
2938    best algorithm for doing the shift.  The assembler code is stored
2939    in the pointers in INFO.  We achieve the maximum efficiency in most
2940    cases when !TARGET_H8300.  In case of TARGET_H8300, shifts in
2941    SImode in particular have a lot of room to optimize.
2942
2943    We first determine the strategy of the shift algorithm by a table
2944    lookup.  If that tells us to use a hand crafted assembly code, we
2945    go into the big switch statement to find what that is.  Otherwise,
2946    we resort to a generic way, such as inlining.  In either case, the
2947    result is returned through INFO.  */
2948
2949 static void
2950 get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
2951                unsigned int count, struct shift_info *info)
2952 {
2953   enum h8_cpu cpu;
2954
2955   /* Find the target CPU.  */
2956   if (TARGET_H8300)
2957     cpu = H8_300;
2958   else if (TARGET_H8300H)
2959     cpu = H8_300H;
2960   else
2961     cpu = H8_S;
2962
2963   /* Find the shift algorithm.  */
2964   info->alg = SHIFT_LOOP;
2965   switch (shift_mode)
2966     {
2967     case QIshift:
2968       if (count < GET_MODE_BITSIZE (QImode))
2969         info->alg = shift_alg_qi[cpu][shift_type][count];
2970       break;
2971
2972     case HIshift:
2973       if (count < GET_MODE_BITSIZE (HImode))
2974         info->alg = shift_alg_hi[cpu][shift_type][count];
2975       break;
2976
2977     case SIshift:
2978       if (count < GET_MODE_BITSIZE (SImode))
2979         info->alg = shift_alg_si[cpu][shift_type][count];
2980       break;
2981
2982     default:
2983       abort ();
2984     }
2985
2986   /* Fill in INFO.  Return unless we have SHIFT_SPECIAL.  */
2987   switch (info->alg)
2988     {
2989     case SHIFT_INLINE:
2990       info->remainder = count;
2991       /* Fall through.  */
2992
2993     case SHIFT_LOOP:
2994       /* It is up to the caller to know that looping clobbers cc.  */
2995       info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
2996       info->shift2 = shift_two[shift_type][shift_mode].assembler;
2997       info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
2998       goto end;
2999
3000     case SHIFT_ROT_AND:
3001       info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
3002       info->shift2 = rotate_two[shift_type][shift_mode];
3003       info->cc_inline = CC_CLOBBER;
3004       goto end;
3005
3006     case SHIFT_SPECIAL:
3007       /* REMAINDER is 0 for most cases, so initialize it to 0.  */
3008       info->remainder = 0;
3009       info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3010       info->shift2 = shift_two[shift_type][shift_mode].assembler;
3011       info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
3012       info->cc_special = CC_CLOBBER;
3013       break;
3014     }
3015
3016   /* Here we only deal with SHIFT_SPECIAL.  */
3017   switch (shift_mode)
3018     {
3019     case QIshift:
3020       /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
3021          through the entire value.  */
3022       if (shift_type == SHIFT_ASHIFTRT && count == 7)
3023         {
3024           info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
3025           goto end;
3026         }
3027       abort ();
3028
3029     case HIshift:
3030       if (count == 7)
3031         {
3032           switch (shift_type)
3033             {
3034             case SHIFT_ASHIFT:
3035               if (TARGET_H8300)
3036                 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";
3037               else
3038                 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
3039               goto end;
3040             case SHIFT_LSHIFTRT:
3041               if (TARGET_H8300)
3042                 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";
3043               else
3044                 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
3045               goto end;
3046             case SHIFT_ASHIFTRT:
3047               info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
3048               goto end;
3049             }
3050         }
3051       else if ((8 <= count && count <= 13)
3052                || (TARGET_H8300S && count == 14))
3053         {
3054           info->remainder = count - 8;
3055
3056           switch (shift_type)
3057             {
3058             case SHIFT_ASHIFT:
3059               info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
3060               goto end;
3061             case SHIFT_LSHIFTRT:
3062               if (TARGET_H8300)
3063                 {
3064                   info->special = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
3065                   info->shift1  = "shlr.b\t%s0";
3066                   info->cc_inline = CC_SET_ZNV;
3067                 }
3068               else
3069                 {
3070                   info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
3071                   info->cc_special = CC_SET_ZNV;
3072                 }
3073               goto end;
3074             case SHIFT_ASHIFTRT:
3075               if (TARGET_H8300)
3076                 {
3077                   info->special = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0";
3078                   info->shift1  = "shar.b\t%s0";
3079                 }
3080               else
3081                 {
3082                   info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
3083                   info->cc_special = CC_SET_ZNV;
3084                 }
3085               goto end;
3086             }
3087         }
3088       else if (count == 14)
3089         {
3090           switch (shift_type)
3091             {
3092             case SHIFT_ASHIFT:
3093               if (TARGET_H8300)
3094                 info->special = "mov.b\t%s0,%t0\n\trotr.b\t%t0\n\trotr.b\t%t0\n\tand.b\t#0xC0,%t0\n\tsub.b\t%s0,%s0";
3095               goto end;
3096             case SHIFT_LSHIFTRT:
3097               if (TARGET_H8300)
3098                 info->special = "mov.b\t%t0,%s0\n\trotl.b\t%s0\n\trotl.b\t%s0\n\tand.b\t#3,%s0\n\tsub.b\t%t0,%t0";
3099               goto end;
3100             case SHIFT_ASHIFTRT:
3101               if (TARGET_H8300)
3102                 info->special = "mov.b\t%t0,%s0\n\tshll.b\t%s0\n\tsubx.b\t%t0,%t0\n\tshll.b\t%s0\n\tmov.b\t%t0,%s0\n\tbst.b\t#0,%s0";
3103               else if (TARGET_H8300H)
3104                 {
3105                   info->special = "shll.b\t%t0\n\tsubx.b\t%s0,%s0\n\tshll.b\t%t0\n\trotxl.b\t%s0\n\texts.w\t%T0";
3106                   info->cc_special = CC_SET_ZNV;
3107                 }
3108               else /* TARGET_H8300S */
3109                 abort ();
3110               goto end;
3111             }
3112         }
3113       else if (count == 15)
3114         {
3115           switch (shift_type)
3116             {
3117             case SHIFT_ASHIFT:
3118               info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
3119               goto end;
3120             case SHIFT_LSHIFTRT:
3121               info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
3122               goto end;
3123             case SHIFT_ASHIFTRT:
3124               info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
3125               goto end;
3126             }
3127         }
3128       abort ();
3129
3130     case SIshift:
3131       if (TARGET_H8300 && 8 <= count && count <= 9)
3132         {
3133           info->remainder = count - 8;
3134
3135           switch (shift_type)
3136             {
3137             case SHIFT_ASHIFT:
3138               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";
3139               goto end;
3140             case SHIFT_LSHIFTRT:
3141               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";
3142               info->shift1  = "shlr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0";
3143               goto end;
3144             case SHIFT_ASHIFTRT:
3145               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";
3146               goto end;
3147             }
3148         }
3149       else if (count == 8 && !TARGET_H8300)
3150         {
3151           switch (shift_type)
3152             {
3153             case SHIFT_ASHIFT:
3154               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";
3155               goto end;
3156             case SHIFT_LSHIFTRT:
3157               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";
3158               goto end;
3159             case SHIFT_ASHIFTRT:
3160               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";
3161               goto end;
3162             }
3163         }
3164       else if (count == 15 && TARGET_H8300)
3165         {
3166           switch (shift_type)
3167             {
3168             case SHIFT_ASHIFT:
3169               abort ();
3170             case SHIFT_LSHIFTRT:
3171               info->special = "bld\t#7,%z0\n\tmov.w\t%e0,%f0\n\txor\t%y0,%y0\n\txor\t%z0,%z0\n\trotxl\t%w0\n\trotxl\t%x0\n\trotxl\t%y0";
3172               goto end;
3173             case SHIFT_ASHIFTRT:
3174               info->special = "bld\t#7,%z0\n\tmov.w\t%e0,%f0\n\trotxl\t%w0\n\trotxl\t%x0\n\tsubx\t%y0,%y0\n\tsubx\t%z0,%z0";
3175               goto end;
3176             }
3177         }
3178       else if (count == 15 && !TARGET_H8300)
3179         {
3180           switch (shift_type)
3181             {
3182             case SHIFT_ASHIFT:
3183               info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
3184               info->cc_special = CC_SET_ZNV;
3185               goto end;
3186             case SHIFT_LSHIFTRT:
3187               info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
3188               info->cc_special = CC_SET_ZNV;
3189               goto end;
3190             case SHIFT_ASHIFTRT:
3191               abort ();
3192             }
3193         }
3194       else if ((TARGET_H8300 && 16 <= count && count <= 20)
3195                || (TARGET_H8300H && 16 <= count && count <= 19)
3196                || (TARGET_H8300S && 16 <= count && count <= 21))
3197         {
3198           info->remainder = count - 16;
3199
3200           switch (shift_type)
3201             {
3202             case SHIFT_ASHIFT:
3203               info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
3204               if (TARGET_H8300)
3205                 info->shift1 = "add.w\t%e0,%e0";
3206               goto end;
3207             case SHIFT_LSHIFTRT:
3208               if (TARGET_H8300)
3209                 {
3210                   info->special = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
3211                   info->shift1  = "shlr\t%x0\n\trotxr\t%w0";
3212                 }
3213               else
3214                 {
3215                   info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
3216                   info->cc_special = CC_SET_ZNV;
3217                 }
3218               goto end;
3219             case SHIFT_ASHIFTRT:
3220               if (TARGET_H8300)
3221                 {
3222                   info->special = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
3223                   info->shift1  = "shar\t%x0\n\trotxr\t%w0";
3224                 }
3225               else
3226                 {
3227                   info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
3228                   info->cc_special = CC_SET_ZNV;
3229                 }
3230               goto end;
3231             }
3232         }
3233       else if (TARGET_H8300 && 24 <= count && count <= 28)
3234         {
3235           info->remainder = count - 24;
3236
3237           switch (shift_type)
3238             {
3239             case SHIFT_ASHIFT:
3240               info->special = "mov.b\t%w0,%z0\n\tsub.b\t%y0,%y0\n\tsub.w\t%f0,%f0";
3241               info->shift1  = "shll.b\t%z0";
3242               info->cc_inline = CC_SET_ZNV;
3243               goto end;
3244             case SHIFT_LSHIFTRT:
3245               info->special = "mov.b\t%z0,%w0\n\tsub.b\t%x0,%x0\n\tsub.w\t%e0,%e0";
3246               info->shift1  = "shlr.b\t%w0";
3247               info->cc_inline = CC_SET_ZNV;
3248               goto end;
3249             case SHIFT_ASHIFTRT:
3250               info->special = "mov.b\t%z0,%w0\n\tbld\t#7,%w0\n\tsubx\t%x0,%x0\n\tsubx\t%x0,%x0\n\tsubx\t%x0,%x0";
3251               info->shift1  = "shar.b\t%w0";
3252               info->cc_inline = CC_SET_ZNV;
3253               goto end;
3254             }
3255         }
3256       else if ((TARGET_H8300H && count == 24)
3257                || (TARGET_H8300S && 24 <= count && count <= 25))
3258         {
3259           info->remainder = count - 24;
3260
3261           switch (shift_type)
3262             {
3263             case SHIFT_ASHIFT:
3264               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";
3265               goto end;
3266             case SHIFT_LSHIFTRT:
3267               info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
3268               info->cc_special = CC_SET_ZNV;
3269               goto end;
3270             case SHIFT_ASHIFTRT:
3271               info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
3272               info->cc_special = CC_SET_ZNV;
3273               goto end;
3274             }
3275         }
3276       else if (!TARGET_H8300 && count == 28)
3277         {
3278           switch (shift_type)
3279             {
3280             case SHIFT_ASHIFT:
3281               if (TARGET_H8300H)
3282                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3283               else
3284                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
3285               goto end;
3286             case SHIFT_LSHIFTRT:
3287               if (TARGET_H8300H)
3288                 {
3289                   info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
3290                   info->cc_special = CC_SET_ZNV;
3291                 }
3292               else
3293                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
3294               goto end;
3295             case SHIFT_ASHIFTRT:
3296               abort ();
3297             }
3298         }
3299       else if (!TARGET_H8300 && count == 29)
3300         {
3301           switch (shift_type)
3302             {
3303             case SHIFT_ASHIFT:
3304               if (TARGET_H8300H)
3305                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3306               else
3307                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3308               goto end;
3309             case SHIFT_LSHIFTRT:
3310               if (TARGET_H8300H)
3311                 {
3312                   info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
3313                   info->cc_special = CC_SET_ZNV;
3314                 }
3315               else
3316                 {
3317                   info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
3318                   info->cc_special = CC_SET_ZNV;
3319                 }
3320               goto end;
3321             case SHIFT_ASHIFTRT:
3322               abort ();
3323             }
3324         }
3325       else if (!TARGET_H8300 && count == 30)
3326         {
3327           switch (shift_type)
3328             {
3329             case SHIFT_ASHIFT:
3330               if (TARGET_H8300H)
3331                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3332               else
3333                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
3334               goto end;
3335             case SHIFT_LSHIFTRT:
3336               if (TARGET_H8300H)
3337                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
3338               else
3339                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
3340               goto end;
3341             case SHIFT_ASHIFTRT:
3342               abort ();
3343             }
3344         }
3345       else if (count == 31)
3346         {
3347           if (TARGET_H8300)
3348             {
3349               switch (shift_type)
3350                 {
3351                 case SHIFT_ASHIFT:
3352                   info->special = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
3353                   goto end;
3354                 case SHIFT_LSHIFTRT:
3355                   info->special = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
3356                   goto end;
3357                 case SHIFT_ASHIFTRT:
3358                   info->special = "shll\t%z0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
3359                   goto end;
3360                 }
3361             }
3362           else
3363             {
3364               switch (shift_type)
3365                 {
3366                 case SHIFT_ASHIFT:
3367                   info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
3368                   info->cc_special = CC_SET_ZNV;
3369                   goto end;
3370                 case SHIFT_LSHIFTRT:
3371                   info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
3372                   info->cc_special = CC_SET_ZNV;
3373                   goto end;
3374                 case SHIFT_ASHIFTRT:
3375                   info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
3376                   info->cc_special = CC_SET_ZNV;
3377                   goto end;
3378                 }
3379             }
3380         }
3381       abort ();
3382
3383     default:
3384       abort ();
3385     }
3386
3387  end:
3388   if (!TARGET_H8300S)
3389     info->shift2 = NULL;
3390 }
3391
3392 /* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
3393    needed for some shift with COUNT and MODE.  Return 0 otherwise.  */
3394
3395 int
3396 h8300_shift_needs_scratch_p (int count, enum machine_mode mode)
3397 {
3398   enum h8_cpu cpu;
3399   int a, lr, ar;
3400
3401   if (GET_MODE_BITSIZE (mode) <= count)
3402     return 1;
3403
3404   /* Find out the target CPU.  */
3405   if (TARGET_H8300)
3406     cpu = H8_300;
3407   else if (TARGET_H8300H)
3408     cpu = H8_300H;
3409   else
3410     cpu = H8_S;
3411
3412   /* Find the shift algorithm.  */
3413   switch (mode)
3414     {
3415     case QImode:
3416       a  = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
3417       lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
3418       ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
3419       break;
3420
3421     case HImode:
3422       a  = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
3423       lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
3424       ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
3425       break;
3426
3427     case SImode:
3428       a  = shift_alg_si[cpu][SHIFT_ASHIFT][count];
3429       lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
3430       ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
3431       break;
3432
3433     default:
3434       abort ();
3435     }
3436
3437   /* On H8/300H, count == 8 uses a scratch register.  */
3438   return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
3439           || (TARGET_H8300H && mode == SImode && count == 8));
3440 }
3441
3442 /* Emit the assembler code for doing shifts.  */
3443
3444 const char *
3445 output_a_shift (rtx *operands)
3446 {
3447   static int loopend_lab;
3448   rtx shift = operands[3];
3449   enum machine_mode mode = GET_MODE (shift);
3450   enum rtx_code code = GET_CODE (shift);
3451   enum shift_type shift_type;
3452   enum shift_mode shift_mode;
3453   struct shift_info info;
3454
3455   loopend_lab++;
3456
3457   switch (mode)
3458     {
3459     case QImode:
3460       shift_mode = QIshift;
3461       break;
3462     case HImode:
3463       shift_mode = HIshift;
3464       break;
3465     case SImode:
3466       shift_mode = SIshift;
3467       break;
3468     default:
3469       abort ();
3470     }
3471
3472   switch (code)
3473     {
3474     case ASHIFTRT:
3475       shift_type = SHIFT_ASHIFTRT;
3476       break;
3477     case LSHIFTRT:
3478       shift_type = SHIFT_LSHIFTRT;
3479       break;
3480     case ASHIFT:
3481       shift_type = SHIFT_ASHIFT;
3482       break;
3483     default:
3484       abort ();
3485     }
3486
3487   if (GET_CODE (operands[2]) != CONST_INT)
3488     {
3489       /* This case must be taken care of by one of the two splitters
3490          that convert a variable shift into a loop.  */
3491       abort ();
3492     }
3493   else
3494     {
3495       int n = INTVAL (operands[2]);
3496
3497       /* If the count is negative, make it 0.  */
3498       if (n < 0)
3499         n = 0;
3500       /* If the count is too big, truncate it.
3501          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
3502          do the intuitive thing.  */
3503       else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
3504         n = GET_MODE_BITSIZE (mode);
3505
3506       get_shift_alg (shift_type, shift_mode, n, &info);
3507
3508       switch (info.alg)
3509         {
3510         case SHIFT_SPECIAL:
3511           output_asm_insn (info.special, operands);
3512           /* Fall through.  */
3513
3514         case SHIFT_INLINE:
3515           n = info.remainder;
3516
3517           /* Emit two bit shifts first.  */
3518           if (info.shift2 != NULL)
3519             {
3520               for (; n > 1; n -= 2)
3521                 output_asm_insn (info.shift2, operands);
3522             }
3523
3524           /* Now emit one bit shifts for any residual.  */
3525           for (; n > 0; n--)
3526             output_asm_insn (info.shift1, operands);
3527           return "";
3528
3529         case SHIFT_ROT_AND:
3530           {
3531             int m = GET_MODE_BITSIZE (mode) - n;
3532             const int mask = (shift_type == SHIFT_ASHIFT
3533                               ? ((1 << m) - 1) << n
3534                               : (1 << m) - 1);
3535             char insn_buf[200];
3536
3537             /* Not all possibilities of rotate are supported.  They shouldn't
3538                be generated, but let's watch for 'em.  */
3539             if (info.shift1 == 0)
3540               abort ();
3541
3542             /* Emit two bit rotates first.  */
3543             if (info.shift2 != NULL)
3544               {
3545                 for (; m > 1; m -= 2)
3546                   output_asm_insn (info.shift2, operands);
3547               }
3548
3549             /* Now single bit rotates for any residual.  */
3550             for (; m > 0; m--)
3551               output_asm_insn (info.shift1, operands);
3552
3553             /* Now mask off the high bits.  */
3554             if (mode == QImode)
3555               sprintf (insn_buf, "and\t#%d,%%X0", mask);
3556             else if (mode == HImode && (TARGET_H8300H || TARGET_H8300S))
3557               sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
3558             else
3559               abort ();
3560
3561             output_asm_insn (insn_buf, operands);
3562             return "";
3563           }
3564
3565         case SHIFT_LOOP:
3566           /* A loop to shift by a "large" constant value.
3567              If we have shift-by-2 insns, use them.  */
3568           if (info.shift2 != NULL)
3569             {
3570               fprintf (asm_out_file, "\tmov.b   #%d,%sl\n", n / 2,
3571                        names_big[REGNO (operands[4])]);
3572               fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
3573               output_asm_insn (info.shift2, operands);
3574               output_asm_insn ("add     #0xff,%X4", operands);
3575               fprintf (asm_out_file, "\tbne     .Llt%d\n", loopend_lab);
3576               if (n % 2)
3577                 output_asm_insn (info.shift1, operands);
3578             }
3579           else
3580             {
3581               fprintf (asm_out_file, "\tmov.b   #%d,%sl\n", n,
3582                        names_big[REGNO (operands[4])]);
3583               fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
3584               output_asm_insn (info.shift1, operands);
3585               output_asm_insn ("add     #0xff,%X4", operands);
3586               fprintf (asm_out_file, "\tbne     .Llt%d\n", loopend_lab);
3587             }
3588           return "";
3589
3590         default:
3591           abort ();
3592         }
3593     }
3594 }
3595
3596 static unsigned int
3597 h8300_asm_insn_count (const char *template)
3598 {
3599   unsigned int count = 1;
3600
3601   for (; *template; template++)
3602     if (*template == '\n')
3603       count++;
3604
3605   return count;
3606 }
3607
3608 unsigned int
3609 compute_a_shift_length (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
3610 {
3611   rtx shift = operands[3];
3612   enum machine_mode mode = GET_MODE (shift);
3613   enum rtx_code code = GET_CODE (shift);
3614   enum shift_type shift_type;
3615   enum shift_mode shift_mode;
3616   struct shift_info info;
3617   unsigned int wlength = 0;
3618
3619   switch (mode)
3620     {
3621     case QImode:
3622       shift_mode = QIshift;
3623       break;
3624     case HImode:
3625       shift_mode = HIshift;
3626       break;
3627     case SImode:
3628       shift_mode = SIshift;
3629       break;
3630     default:
3631       abort ();
3632     }
3633
3634   switch (code)
3635     {
3636     case ASHIFTRT:
3637       shift_type = SHIFT_ASHIFTRT;
3638       break;
3639     case LSHIFTRT:
3640       shift_type = SHIFT_LSHIFTRT;
3641       break;
3642     case ASHIFT:
3643       shift_type = SHIFT_ASHIFT;
3644       break;
3645     default:
3646       abort ();
3647     }
3648
3649   if (GET_CODE (operands[2]) != CONST_INT)
3650     {
3651       /* Get the assembler code to do one shift.  */
3652       get_shift_alg (shift_type, shift_mode, 1, &info);
3653
3654       return (4 + h8300_asm_insn_count (info.shift1)) * 2;
3655     }
3656   else
3657     {
3658       int n = INTVAL (operands[2]);
3659
3660       /* If the count is negative, make it 0.  */
3661       if (n < 0)
3662         n = 0;
3663       /* If the count is too big, truncate it.
3664          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
3665          do the intuitive thing.  */
3666       else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
3667         n = GET_MODE_BITSIZE (mode);
3668
3669       get_shift_alg (shift_type, shift_mode, n, &info);
3670
3671       switch (info.alg)
3672         {
3673         case SHIFT_SPECIAL:
3674           wlength += h8300_asm_insn_count (info.special);
3675
3676           /* Every assembly instruction used in SHIFT_SPECIAL case
3677              takes 2 bytes except xor.l, which takes 4 bytes, so if we
3678              see xor.l, we just pretend that xor.l counts as two insns
3679              so that the insn length will be computed correctly.  */
3680           if (strstr (info.special, "xor.l") != NULL)
3681             wlength++;
3682
3683           /* Fall through.  */
3684
3685         case SHIFT_INLINE:
3686           n = info.remainder;
3687
3688           if (info.shift2 != NULL)
3689             {
3690               wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
3691               n = n % 2;
3692             }
3693
3694           wlength += h8300_asm_insn_count (info.shift1) * n;
3695
3696           return 2 * wlength;
3697
3698         case SHIFT_ROT_AND:
3699           {
3700             int m = GET_MODE_BITSIZE (mode) - n;
3701
3702             /* Not all possibilities of rotate are supported.  They shouldn't
3703                be generated, but let's watch for 'em.  */
3704             if (info.shift1 == 0)
3705               abort ();
3706
3707             if (info.shift2 != NULL)
3708               {
3709                 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
3710                 m = m % 2;
3711               }
3712
3713             wlength += h8300_asm_insn_count (info.shift1) * m;
3714
3715             /* Now mask off the high bits.  */
3716             switch (mode)
3717               {
3718               case QImode:
3719                 wlength += 1;
3720                 break;
3721               case HImode:
3722                 wlength += 2;
3723                 break;
3724               case SImode:
3725                 if (TARGET_H8300)
3726                   abort ();
3727                 wlength += 3;
3728                 break;
3729               default:
3730                 abort ();
3731               }
3732             return 2 * wlength;
3733           }
3734
3735         case SHIFT_LOOP:
3736           /* A loop to shift by a "large" constant value.
3737              If we have shift-by-2 insns, use them.  */
3738           if (info.shift2 != NULL)
3739             {
3740               wlength += 3 + h8300_asm_insn_count (info.shift2);
3741               if (n % 2)
3742                 wlength += h8300_asm_insn_count (info.shift1);
3743             }
3744           else
3745             {
3746               wlength += 3 + h8300_asm_insn_count (info.shift1);
3747             }
3748           return 2 * wlength;
3749
3750         default:
3751           abort ();
3752         }
3753     }
3754 }
3755
3756 int
3757 compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
3758 {
3759   rtx shift = operands[3];
3760   enum machine_mode mode = GET_MODE (shift);
3761   enum rtx_code code = GET_CODE (shift);
3762   enum shift_type shift_type;
3763   enum shift_mode shift_mode;
3764   struct shift_info info;
3765
3766   switch (mode)
3767     {
3768     case QImode:
3769       shift_mode = QIshift;
3770       break;
3771     case HImode:
3772       shift_mode = HIshift;
3773       break;
3774     case SImode:
3775       shift_mode = SIshift;
3776       break;
3777     default:
3778       abort ();
3779     }
3780
3781   switch (code)
3782     {
3783     case ASHIFTRT:
3784       shift_type = SHIFT_ASHIFTRT;
3785       break;
3786     case LSHIFTRT:
3787       shift_type = SHIFT_LSHIFTRT;
3788       break;
3789     case ASHIFT:
3790       shift_type = SHIFT_ASHIFT;
3791       break;
3792     default:
3793       abort ();
3794     }
3795
3796   if (GET_CODE (operands[2]) != CONST_INT)
3797     {
3798       /* This case must be taken care of by one of the two splitters
3799          that convert a variable shift into a loop.  */
3800       abort ();
3801     }
3802   else
3803     {
3804       int n = INTVAL (operands[2]);
3805
3806       /* If the count is negative, make it 0.  */
3807       if (n < 0)
3808         n = 0;
3809       /* If the count is too big, truncate it.
3810          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
3811          do the intuitive thing.  */
3812       else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
3813         n = GET_MODE_BITSIZE (mode);
3814
3815       get_shift_alg (shift_type, shift_mode, n, &info);
3816
3817       switch (info.alg)
3818         {
3819         case SHIFT_SPECIAL:
3820           if (info.remainder == 0)
3821             return info.cc_special;
3822
3823           /* Fall through.  */
3824
3825         case SHIFT_INLINE:
3826           return info.cc_inline;
3827
3828         case SHIFT_ROT_AND:
3829           /* This case always ends with an and instruction.  */
3830           return CC_SET_ZNV;
3831
3832         case SHIFT_LOOP:
3833           /* A loop to shift by a "large" constant value.
3834              If we have shift-by-2 insns, use them.  */
3835           if (info.shift2 != NULL)
3836             {
3837               if (n % 2)
3838                 return info.cc_inline;
3839             }
3840           return CC_CLOBBER;
3841
3842         default:
3843           abort ();
3844         }
3845     }
3846 }
3847 \f
3848 /* A rotation by a non-constant will cause a loop to be generated, in
3849    which a rotation by one bit is used.  A rotation by a constant,
3850    including the one in the loop, will be taken care of by
3851    output_a_rotate () at the insn emit time.  */
3852
3853 int
3854 expand_a_rotate (enum rtx_code code, rtx operands[])
3855 {
3856   rtx dst = operands[0];
3857   rtx src = operands[1];
3858   rtx rotate_amount = operands[2];
3859   enum machine_mode mode = GET_MODE (dst);
3860   rtx tmp;
3861
3862   /* We rotate in place.  */
3863   emit_move_insn (dst, src);
3864
3865   if (GET_CODE (rotate_amount) != CONST_INT)
3866     {
3867       rtx counter = gen_reg_rtx (QImode);
3868       rtx start_label = gen_label_rtx ();
3869       rtx end_label = gen_label_rtx ();
3870
3871       /* If the rotate amount is less than or equal to 0,
3872          we go out of the loop.  */
3873       emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
3874                                QImode, 0, end_label);
3875
3876       /* Initialize the loop counter.  */
3877       emit_move_insn (counter, rotate_amount);
3878
3879       emit_label (start_label);
3880
3881       /* Rotate by one bit.  */
3882       tmp = gen_rtx_fmt_ee (code, mode, dst, const1_rtx);
3883       emit_insn (gen_rtx_SET (mode, dst, tmp));
3884
3885       /* Decrement the counter by 1.  */
3886       tmp = gen_rtx_PLUS (QImode, counter, constm1_rtx);
3887       emit_insn (gen_rtx_SET (VOIDmode, counter, tmp));
3888
3889       /* If the loop counter is nonzero, we go back to the beginning
3890          of the loop.  */
3891       emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
3892                                start_label);
3893
3894       emit_label (end_label);
3895     }
3896   else
3897     {
3898       /* Rotate by AMOUNT bits.  */
3899       tmp = gen_rtx_fmt_ee (code, mode, dst, rotate_amount);
3900       emit_insn (gen_rtx_SET (mode, dst, tmp));
3901     }
3902
3903   return 1;
3904 }
3905
3906 /* Output rotate insns.  */
3907
3908 const char *
3909 output_a_rotate (enum rtx_code code, rtx *operands)
3910 {
3911   rtx dst = operands[0];
3912   rtx rotate_amount = operands[2];
3913   enum shift_mode rotate_mode;
3914   enum shift_type rotate_type;
3915   const char *insn_buf;
3916   int bits;
3917   int amount;
3918   enum machine_mode mode = GET_MODE (dst);
3919
3920   if (GET_CODE (rotate_amount) != CONST_INT)
3921     abort ();
3922
3923   switch (mode)
3924     {
3925     case QImode:
3926       rotate_mode = QIshift;
3927       break;
3928     case HImode:
3929       rotate_mode = HIshift;
3930       break;
3931     case SImode:
3932       rotate_mode = SIshift;
3933       break;
3934     default:
3935       abort ();
3936     }
3937
3938   switch (code)
3939     {
3940     case ROTATERT:
3941       rotate_type = SHIFT_ASHIFT;
3942       break;
3943     case ROTATE:
3944       rotate_type = SHIFT_LSHIFTRT;
3945       break;
3946     default:
3947       abort ();
3948     }
3949
3950   amount = INTVAL (rotate_amount);
3951
3952   /* Clean up AMOUNT.  */
3953   if (amount < 0)
3954     amount = 0;
3955   if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
3956     amount = GET_MODE_BITSIZE (mode);
3957
3958   /* Determine the faster direction.  After this phase, amount will be
3959      at most a half of GET_MODE_BITSIZE (mode).  */
3960   if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
3961     {
3962       /* Flip the direction.  */
3963       amount = GET_MODE_BITSIZE (mode) - amount;
3964       rotate_type =
3965         (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
3966     }
3967
3968   /* See if a byte swap (in HImode) or a word swap (in SImode) can
3969      boost up the rotation.  */
3970   if ((mode == HImode && TARGET_H8300 && amount >= 5)
3971       || (mode == HImode && TARGET_H8300H && amount >= 6)
3972       || (mode == HImode && TARGET_H8300S && amount == 8)
3973       || (mode == SImode && TARGET_H8300H && amount >= 10)
3974       || (mode == SImode && TARGET_H8300S && amount >= 13))
3975     {
3976       switch (mode)
3977         {
3978         case HImode:
3979           /* This code works on any family.  */
3980           insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
3981           output_asm_insn (insn_buf, operands);
3982           break;
3983
3984         case SImode:
3985           /* This code works on the H8/300H and H8S.  */
3986           insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
3987           output_asm_insn (insn_buf, operands);
3988           break;
3989
3990         default:
3991           abort ();
3992         }
3993
3994       /* Adjust AMOUNT and flip the direction.  */
3995       amount = GET_MODE_BITSIZE (mode) / 2 - amount;
3996       rotate_type =
3997         (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
3998     }
3999
4000   /* Emit rotate insns.  */
4001   for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
4002     {
4003       if (bits == 2)
4004         insn_buf = rotate_two[rotate_type][rotate_mode];
4005       else
4006         insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
4007
4008       for (; amount >= bits; amount -= bits)
4009         output_asm_insn (insn_buf, operands);
4010     }
4011
4012   return "";
4013 }
4014
4015 unsigned int
4016 compute_a_rotate_length (rtx *operands)
4017 {
4018   rtx src = operands[1];
4019   rtx amount_rtx = operands[2];
4020   enum machine_mode mode = GET_MODE (src);
4021   int amount;
4022   unsigned int length = 0;
4023
4024   if (GET_CODE (amount_rtx) != CONST_INT)
4025     abort ();
4026
4027   amount = INTVAL (amount_rtx);
4028
4029   /* Clean up AMOUNT.  */
4030   if (amount < 0)
4031     amount = 0;
4032   if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4033     amount = GET_MODE_BITSIZE (mode);
4034
4035   /* Determine the faster direction.  After this phase, amount
4036      will be at most a half of GET_MODE_BITSIZE (mode).  */
4037   if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
4038     /* Flip the direction.  */
4039     amount = GET_MODE_BITSIZE (mode) - amount;
4040
4041   /* See if a byte swap (in HImode) or a word swap (in SImode) can
4042      boost up the rotation.  */
4043   if ((mode == HImode && TARGET_H8300 && amount >= 5)
4044       || (mode == HImode && TARGET_H8300H && amount >= 6)
4045       || (mode == HImode && TARGET_H8300S && amount == 8)
4046       || (mode == SImode && TARGET_H8300H && amount >= 10)
4047       || (mode == SImode && TARGET_H8300S && amount >= 13))
4048     {
4049       /* Adjust AMOUNT and flip the direction.  */
4050       amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4051       length += 6;
4052     }
4053
4054   /* We use 2-bit rotations on the H8S.  */
4055   if (TARGET_H8300S)
4056     amount = amount / 2 + amount % 2;
4057
4058   /* The H8/300 uses three insns to rotate one bit, taking 6
4059      length.  */
4060   length += amount * ((TARGET_H8300 && mode == HImode) ? 6 : 2);
4061
4062   return length;
4063 }
4064 \f
4065 /* Fix the operands of a gen_xxx so that it could become a bit
4066    operating insn.  */
4067
4068 int
4069 fix_bit_operand (rtx *operands, int what, enum rtx_code type)
4070 {
4071   /* The bit_operand predicate accepts any memory during RTL generation, but
4072      only 'U' memory afterwards, so if this is a MEM operand, we must force
4073      it to be valid for 'U' by reloading the address.  */
4074
4075   if ((what == 0 && single_zero_operand (operands[2], QImode))
4076       || (what == 1 && single_one_operand (operands[2], QImode)))
4077     {
4078       /* OK to have a memory dest.  */
4079       if (GET_CODE (operands[0]) == MEM
4080           && !EXTRA_CONSTRAINT (operands[0], 'U'))
4081         {
4082           rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
4083                                  copy_to_mode_reg (Pmode,
4084                                                    XEXP (operands[0], 0)));
4085           MEM_COPY_ATTRIBUTES (mem, operands[0]);
4086           operands[0] = mem;
4087         }
4088
4089       if (GET_CODE (operands[1]) == MEM
4090           && !EXTRA_CONSTRAINT (operands[1], 'U'))
4091         {
4092           rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
4093                                  copy_to_mode_reg (Pmode,
4094                                                    XEXP (operands[1], 0)));
4095           MEM_COPY_ATTRIBUTES (mem, operands[0]);
4096           operands[1] = mem;
4097         }
4098       return 0;
4099     }
4100
4101   /* Dest and src op must be register.  */
4102
4103   operands[1] = force_reg (QImode, operands[1]);
4104   {
4105     rtx res = gen_reg_rtx (QImode);
4106     emit_insn (gen_rtx_SET (VOIDmode, res,
4107                             gen_rtx_fmt_ee (type, QImode,
4108                                             operands[1], operands[2])));
4109     emit_insn (gen_rtx_SET (VOIDmode, operands[0], res));
4110   }
4111   return 1;
4112 }
4113
4114 /* Return nonzero if FUNC is an interrupt function as specified
4115    by the "interrupt" attribute.  */
4116
4117 static int
4118 h8300_interrupt_function_p (tree func)
4119 {
4120   tree a;
4121
4122   if (TREE_CODE (func) != FUNCTION_DECL)
4123     return 0;
4124
4125   a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
4126   return a != NULL_TREE;
4127 }
4128
4129 /* Return nonzero if FUNC is a saveall function as specified by the
4130    "saveall" attribute.  */
4131
4132 static int
4133 h8300_saveall_function_p (tree func)
4134 {
4135   tree a;
4136
4137   if (TREE_CODE (func) != FUNCTION_DECL)
4138     return 0;
4139
4140   a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
4141   return a != NULL_TREE;
4142 }
4143
4144 /* Return nonzero if FUNC is an OS_Task function as specified
4145    by the "OS_Task" attribute.  */
4146
4147 static int
4148 h8300_os_task_function_p (tree func)
4149 {
4150   tree a;
4151
4152   if (TREE_CODE (func) != FUNCTION_DECL)
4153     return 0;
4154
4155   a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
4156   return a != NULL_TREE;
4157 }
4158
4159 /* Return nonzero if FUNC is a monitor function as specified
4160    by the "monitor" attribute.  */
4161
4162 static int
4163 h8300_monitor_function_p (tree func)
4164 {
4165   tree a;
4166
4167   if (TREE_CODE (func) != FUNCTION_DECL)
4168     return 0;
4169
4170   a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
4171   return a != NULL_TREE;
4172 }
4173
4174 /* Return nonzero if FUNC is a function that should be called
4175    through the function vector.  */
4176
4177 int
4178 h8300_funcvec_function_p (tree func)
4179 {
4180   tree a;
4181
4182   if (TREE_CODE (func) != FUNCTION_DECL)
4183     return 0;
4184
4185   a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
4186   return a != NULL_TREE;
4187 }
4188
4189 /* Return nonzero if DECL is a variable that's in the eight bit
4190    data area.  */
4191
4192 int
4193 h8300_eightbit_data_p (tree decl)
4194 {
4195   tree a;
4196
4197   if (TREE_CODE (decl) != VAR_DECL)
4198     return 0;
4199
4200   a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
4201   return a != NULL_TREE;
4202 }
4203
4204 /* Return nonzero if DECL is a variable that's in the tiny
4205    data area.  */
4206
4207 int
4208 h8300_tiny_data_p (tree decl)
4209 {
4210   tree a;
4211
4212   if (TREE_CODE (decl) != VAR_DECL)
4213     return 0;
4214
4215   a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
4216   return a != NULL_TREE;
4217 }
4218
4219 /* Generate an 'interrupt_handler' attribute for decls.  We convert
4220    all the pragmas to corresponding attributes.  */
4221
4222 static void
4223 h8300_insert_attributes (tree node, tree *attributes)
4224 {
4225   if (TREE_CODE (node) == FUNCTION_DECL)
4226     {
4227       if (pragma_interrupt)
4228         {
4229           pragma_interrupt = 0;
4230
4231           /* Add an 'interrupt_handler' attribute.  */
4232           *attributes = tree_cons (get_identifier ("interrupt_handler"),
4233                                    NULL, *attributes);
4234         }
4235
4236       if (pragma_saveall)
4237         {
4238           pragma_saveall = 0;
4239
4240           /* Add an 'saveall' attribute.  */
4241           *attributes = tree_cons (get_identifier ("saveall"),
4242                                    NULL, *attributes);
4243         }
4244     }
4245 }
4246
4247 /* Supported attributes:
4248
4249    interrupt_handler: output a prologue and epilogue suitable for an
4250    interrupt handler.
4251
4252    saveall: output a prologue and epilogue that saves and restores
4253    all registers except the stack pointer.
4254
4255    function_vector: This function should be called through the
4256    function vector.
4257
4258    eightbit_data: This variable lives in the 8-bit data area and can
4259    be referenced with 8-bit absolute memory addresses.
4260
4261    tiny_data: This variable lives in the tiny data area and can be
4262    referenced with 16-bit absolute memory references.  */
4263
4264 const struct attribute_spec h8300_attribute_table[] =
4265 {
4266   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
4267   { "interrupt_handler", 0, 0, true,  false, false, h8300_handle_fndecl_attribute },
4268   { "saveall",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
4269   { "OS_Task",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
4270   { "monitor",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
4271   { "function_vector",   0, 0, true,  false, false, h8300_handle_fndecl_attribute },
4272   { "eightbit_data",     0, 0, true,  false, false, h8300_handle_eightbit_data_attribute },
4273   { "tiny_data",         0, 0, true,  false, false, h8300_handle_tiny_data_attribute },
4274   { NULL,                0, 0, false, false, false, NULL }
4275 };
4276
4277
4278 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
4279    struct attribute_spec.handler.  */
4280 static tree
4281 h8300_handle_fndecl_attribute (tree *node, tree name,
4282                                tree args ATTRIBUTE_UNUSED,
4283                                int flags ATTRIBUTE_UNUSED,
4284                                bool *no_add_attrs)
4285 {
4286   if (TREE_CODE (*node) != FUNCTION_DECL)
4287     {
4288       warning ("`%s' attribute only applies to functions",
4289                IDENTIFIER_POINTER (name));
4290       *no_add_attrs = true;
4291     }
4292
4293   return NULL_TREE;
4294 }
4295
4296 /* Handle an "eightbit_data" attribute; arguments as in
4297    struct attribute_spec.handler.  */
4298 static tree
4299 h8300_handle_eightbit_data_attribute (tree *node, tree name,
4300                                       tree args ATTRIBUTE_UNUSED,
4301                                       int flags ATTRIBUTE_UNUSED,
4302                                       bool *no_add_attrs)
4303 {
4304   tree decl = *node;
4305
4306   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
4307     {
4308       DECL_SECTION_NAME (decl) = build_string (7, ".eight");
4309     }
4310   else
4311     {
4312       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4313       *no_add_attrs = true;
4314     }
4315
4316   return NULL_TREE;
4317 }
4318
4319 /* Handle an "tiny_data" attribute; arguments as in
4320    struct attribute_spec.handler.  */
4321 static tree
4322 h8300_handle_tiny_data_attribute (tree *node, tree name,
4323                                   tree args ATTRIBUTE_UNUSED,
4324                                   int flags ATTRIBUTE_UNUSED,
4325                                   bool *no_add_attrs)
4326 {
4327   tree decl = *node;
4328
4329   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
4330     {
4331       DECL_SECTION_NAME (decl) = build_string (6, ".tiny");
4332     }
4333   else
4334     {
4335       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4336       *no_add_attrs = true;
4337     }
4338
4339   return NULL_TREE;
4340 }
4341
4342 /* Mark function vectors, and various small data objects.  */
4343
4344 static void
4345 h8300_encode_section_info (tree decl, rtx rtl, int first)
4346 {
4347   int extra_flags = 0;
4348
4349   default_encode_section_info (decl, rtl, first);
4350
4351   if (TREE_CODE (decl) == FUNCTION_DECL
4352       && h8300_funcvec_function_p (decl))
4353     extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
4354   else if (TREE_CODE (decl) == VAR_DECL
4355            && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
4356     {
4357       if (h8300_eightbit_data_p (decl))
4358         extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
4359       else if (first && h8300_tiny_data_p (decl))
4360         extra_flags = SYMBOL_FLAG_TINY_DATA;
4361     }
4362
4363   if (extra_flags)
4364     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
4365 }
4366
4367 const char *
4368 output_simode_bld (int bild, rtx operands[])
4369 {
4370   if (TARGET_H8300)
4371     {
4372       /* Clear the destination register.  */
4373       output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
4374
4375       /* Now output the bit load or bit inverse load, and store it in
4376          the destination.  */
4377       if (bild)
4378         output_asm_insn ("bild\t%Z2,%Y1", operands);
4379       else
4380         output_asm_insn ("bld\t%Z2,%Y1", operands);
4381
4382       output_asm_insn ("bst\t#0,%w0", operands);
4383     }
4384   else
4385     {
4386       /* Determine if we can clear the destination first.  */
4387       int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
4388                          && REGNO (operands[0]) != REGNO (operands[1]));
4389
4390       if (clear_first)
4391         output_asm_insn ("sub.l\t%S0,%S0", operands);
4392
4393       /* Output the bit load or bit inverse load.  */
4394       if (bild)
4395         output_asm_insn ("bild\t%Z2,%Y1", operands);
4396       else
4397         output_asm_insn ("bld\t%Z2,%Y1", operands);
4398
4399       if (!clear_first)
4400         output_asm_insn ("xor.l\t%S0,%S0", operands);
4401
4402       /* Perform the bit store.  */
4403       output_asm_insn ("rotxl.l\t%S0", operands);
4404     }
4405
4406   /* All done.  */
4407   return "";
4408 }
4409
4410 #ifndef OBJECT_FORMAT_ELF
4411 static void
4412 h8300_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED)
4413 {
4414   /* ??? Perhaps we should be using default_coff_asm_named_section.  */
4415   fprintf (asm_out_file, "\t.section %s\n", name);
4416 }
4417 #endif /* ! OBJECT_FORMAT_ELF */
4418
4419 /* Nonzero if X is a constant address suitable as an 8-bit absolute,
4420    which is a special case of the 'R' operand.  */
4421
4422 int
4423 h8300_eightbit_constant_address_p (rtx x)
4424 {
4425   /* The ranges of the 8-bit area.  */
4426   const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
4427   const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
4428   const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
4429   const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
4430   const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
4431   const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
4432
4433   unsigned HOST_WIDE_INT addr;
4434
4435   /* We accept symbols declared with eightbit_data.  */
4436   if (GET_CODE (x) == SYMBOL_REF)
4437     return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
4438
4439   if (GET_CODE (x) != CONST_INT)
4440     return 0;
4441
4442   addr = INTVAL (x);
4443
4444   return (0
4445           || ((TARGET_H8300 || TARGET_NORMAL_MODE) && IN_RANGE (addr, n1, n2))
4446           || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
4447           || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
4448 }
4449
4450 /* Nonzero if X is a constant address suitable as an 16-bit absolute
4451    on H8/300H and H8S.  */
4452
4453 int
4454 h8300_tiny_constant_address_p (rtx x)
4455 {
4456   /* The ranges of the 16-bit area.  */
4457   const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
4458   const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
4459   const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
4460   const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
4461   const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
4462   const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
4463   const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
4464   const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
4465
4466   unsigned HOST_WIDE_INT addr;
4467
4468   switch (GET_CODE (x))
4469     {
4470     case SYMBOL_REF:
4471       /* In the normal mode, any symbol fits in the 16-bit absolute
4472          address range.  We also accept symbols declared with
4473          tiny_data.  */
4474       return (TARGET_NORMAL_MODE
4475               || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
4476
4477     case CONST_INT:
4478       addr = INTVAL (x);
4479       return (TARGET_NORMAL_MODE
4480               || (TARGET_H8300H
4481                   && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
4482               || (TARGET_H8300S
4483                   && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
4484
4485     case CONST:
4486       return TARGET_NORMAL_MODE;
4487
4488     default:
4489       return 0;
4490     }
4491
4492 }
4493
4494 int
4495 byte_accesses_mergeable_p (rtx addr1, rtx addr2)
4496 {
4497   HOST_WIDE_INT offset1, offset2;
4498   rtx reg1, reg2;
4499
4500   if (REG_P (addr1))
4501     {
4502       reg1 = addr1;
4503       offset1 = 0;
4504     }
4505   else if (GET_CODE (addr1) == PLUS
4506            && REG_P (XEXP (addr1, 0))
4507            && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
4508     {
4509       reg1 = XEXP (addr1, 0);
4510       offset1 = INTVAL (XEXP (addr1, 1));
4511     }
4512   else
4513     return 0;
4514
4515   if (REG_P (addr2))
4516     {
4517       reg2 = addr2;
4518       offset2 = 0;
4519     }
4520   else if (GET_CODE (addr2) == PLUS
4521            && REG_P (XEXP (addr2, 0))
4522            && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
4523     {
4524       reg2 = XEXP (addr2, 0);
4525       offset2 = INTVAL (XEXP (addr2, 1));
4526     }
4527   else
4528     return 0;
4529
4530   if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
4531        || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
4532       && offset1 % 2 == 0
4533       && offset1 + 1 == offset2)
4534     return 1;
4535
4536   return 0;
4537 }
4538
4539 /* Return nonzero if we have the same comparison insn as I3 two insns
4540    before I3.  I3 is assumed to be a comparison insn.  */
4541
4542 int
4543 same_cmp_preceding_p (rtx i3)
4544 {
4545   rtx i1, i2;
4546
4547   /* Make sure we have a sequence of three insns.  */
4548   i2 = prev_nonnote_insn (i3);
4549   if (i2 == NULL_RTX)
4550     return 0;
4551   i1 = prev_nonnote_insn (i2);
4552   if (i1 == NULL_RTX)
4553     return 0;
4554
4555   return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
4556           && any_condjump_p (i2) && onlyjump_p (i2));
4557 }
4558
4559 /* Return nonzero if we have the same comparison insn as I1 two insns
4560    after I1.  I1 is assumed to be a comparison insn.  */
4561
4562 int
4563 same_cmp_following_p (rtx i1)
4564 {
4565   rtx i2, i3;
4566
4567   /* Make sure we have a sequence of three insns.  */
4568   i2 = next_nonnote_insn (i1);
4569   if (i2 == NULL_RTX)
4570     return 0;
4571   i3 = next_nonnote_insn (i2);
4572   if (i3 == NULL_RTX)
4573     return 0;
4574
4575   return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
4576           && any_condjump_p (i2) && onlyjump_p (i2));
4577 }
4578
4579 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG.  */
4580
4581 int
4582 h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
4583                             unsigned int new_reg)
4584 {
4585   /* Interrupt functions can only use registers that have already been
4586      saved by the prologue, even if they would normally be
4587      call-clobbered.  */
4588
4589   if (h8300_current_function_interrupt_function_p ()
4590       && !regs_ever_live[new_reg])
4591     return 0;
4592
4593   return 1;
4594 }
4595
4596 /* Return nonzero if X is a legitimate constant.  */
4597
4598 int
4599 h8300_legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
4600 {
4601   return 1;
4602 }
4603
4604 /* Return nonzero if X is a REG or SUBREG suitable as a base register.  */
4605
4606 static int
4607 h8300_rtx_ok_for_base_p (rtx x, int strict)
4608 {
4609   /* Strip off SUBREG if any.  */
4610   if (GET_CODE (x) == SUBREG)
4611     x = SUBREG_REG (x);
4612
4613   return (REG_P (x)
4614           && (strict
4615               ? REG_OK_FOR_BASE_STRICT_P (x)
4616               : REG_OK_FOR_BASE_NONSTRICT_P (x)));
4617 }
4618
4619 /* Return nozero if X is a legitimate address.  On the H8/300, a
4620    legitimate address has the form REG, REG+CONSTANT_ADDRESS or
4621    CONSTANT_ADDRESS.  */
4622
4623 int
4624 h8300_legitimate_address_p (rtx x, int strict)
4625 {
4626   /* The register indirect addresses like @er0 is always valid.  */
4627   if (h8300_rtx_ok_for_base_p (x, strict))
4628     return 1;
4629
4630   if (CONSTANT_ADDRESS_P (x))
4631     return 1;
4632
4633   if (GET_CODE (x) == PLUS
4634       && CONSTANT_ADDRESS_P (XEXP (x, 1))
4635       && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
4636     return 1;
4637
4638   return 0;
4639 }
4640 \f
4641 /* Perform target dependent optabs initialization.  */
4642 static void
4643 h8300_init_libfuncs (void)
4644 {
4645   set_optab_libfunc (smul_optab, HImode, "__mulhi3");
4646   set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
4647   set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
4648   set_optab_libfunc (smod_optab, HImode, "__modhi3");
4649   set_optab_libfunc (umod_optab, HImode, "__umodhi3");
4650 }
4651 \f
4652 static bool
4653 h8300_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
4654 {
4655   return (TYPE_MODE (type) == BLKmode
4656           || GET_MODE_SIZE (TYPE_MODE (type)) > (TARGET_H8300 ? 4 : 8));
4657 }
4658 \f
4659 /* Initialize the GCC target structure.  */
4660 #undef TARGET_ATTRIBUTE_TABLE
4661 #define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
4662
4663 #undef TARGET_ASM_ALIGNED_HI_OP
4664 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
4665
4666 #undef TARGET_ASM_FILE_START
4667 #define TARGET_ASM_FILE_START h8300_file_start
4668 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
4669 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
4670
4671 #undef TARGET_ASM_FILE_END
4672 #define TARGET_ASM_FILE_END h8300_file_end
4673
4674 #undef TARGET_ENCODE_SECTION_INFO
4675 #define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
4676
4677 #undef TARGET_INSERT_ATTRIBUTES
4678 #define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
4679
4680 #undef TARGET_RTX_COSTS
4681 #define TARGET_RTX_COSTS h8300_rtx_costs
4682
4683 #undef TARGET_INIT_LIBFUNCS
4684 #define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
4685
4686 #undef TARGET_RETURN_IN_MEMORY
4687 #define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
4688
4689 struct gcc_target targetm = TARGET_INITIALIZER;