OSDN Git Service

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