OSDN Git Service

546a6aee7d587f68aaf780266f42ee3f4e6b1bb8
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.c
1 /* Subroutines for insn-output.c for Renesas H8/300.
2    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Steve Chamberlain (sac@cygnus.com),
6    Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
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 "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "optabs.h"
41 #include "diagnostic-core.h"
42 #include "toplev.h"
43 #include "c-family/c-pragma.h"  /* ??? */
44 #include "tm_p.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48
49 /* Classifies a h8300_src_operand or h8300_dst_operand.
50
51    H8OP_IMMEDIATE
52         A constant operand of some sort.
53
54    H8OP_REGISTER
55         An ordinary register.
56
57    H8OP_MEM_ABSOLUTE
58         A memory reference with a constant address.
59
60    H8OP_MEM_BASE
61         A memory reference with a register as its address.
62
63    H8OP_MEM_COMPLEX
64         Some other kind of memory reference.  */
65 enum h8300_operand_class
66 {
67   H8OP_IMMEDIATE,
68   H8OP_REGISTER,
69   H8OP_MEM_ABSOLUTE,
70   H8OP_MEM_BASE,
71   H8OP_MEM_COMPLEX,
72   NUM_H8OPS
73 };
74
75 /* For a general two-operand instruction, element [X][Y] gives
76    the length of the opcode fields when the first operand has class
77    (X + 1) and the second has class Y.  */
78 typedef unsigned char h8300_length_table[NUM_H8OPS - 1][NUM_H8OPS];
79
80 /* Forward declarations.  */
81 static const char *byte_reg (rtx, int);
82 static int h8300_interrupt_function_p (tree);
83 static int h8300_saveall_function_p (tree);
84 static int h8300_monitor_function_p (tree);
85 static int h8300_os_task_function_p (tree);
86 static void h8300_emit_stack_adjustment (int, HOST_WIDE_INT, bool);
87 static HOST_WIDE_INT round_frame_size (HOST_WIDE_INT);
88 static unsigned int compute_saved_regs (void);
89 static void push (int);
90 static void pop (int);
91 static const char *cond_string (enum rtx_code);
92 static unsigned int h8300_asm_insn_count (const char *);
93 static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
94 static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
95 static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
96 #ifndef OBJECT_FORMAT_ELF
97 static void h8300_asm_named_section (const char *, unsigned int, tree);
98 #endif
99 static int h8300_and_costs (rtx);
100 static int h8300_shift_costs (rtx);
101 static void          h8300_push_pop               (int, int, bool, bool);
102 static int           h8300_stack_offset_p         (rtx, int);
103 static int           h8300_ldm_stm_regno          (rtx, int, int, int);
104 static void          h8300_reorg                  (void);
105 static unsigned int  h8300_constant_length        (rtx);
106 static unsigned int  h8300_displacement_length    (rtx, int);
107 static unsigned int  h8300_classify_operand       (rtx, int, enum h8300_operand_class *);
108 static unsigned int  h8300_length_from_table      (rtx, rtx, const h8300_length_table *);
109 static unsigned int  h8300_unary_length           (rtx);
110 static unsigned int  h8300_short_immediate_length (rtx);
111 static unsigned int  h8300_bitfield_length        (rtx, rtx);
112 static unsigned int  h8300_binary_length          (rtx, const h8300_length_table *);
113 static bool          h8300_short_move_mem_p       (rtx, enum rtx_code);
114 static unsigned int  h8300_move_length            (rtx *, const h8300_length_table *);
115 static bool          h8300_hard_regno_scratch_ok  (unsigned int);
116
117 /* CPU_TYPE, says what cpu we're compiling for.  */
118 int cpu_type;
119
120 /* True if a #pragma interrupt has been seen for the current function.  */
121 static int pragma_interrupt;
122
123 /* True if a #pragma saveall has been seen for the current function.  */
124 static int pragma_saveall;
125
126 static const char *const names_big[] =
127 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
128
129 static const char *const names_extended[] =
130 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
131
132 static const char *const names_upper_extended[] =
133 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
134
135 /* Points to one of the above.  */
136 /* ??? The above could be put in an array indexed by CPU_TYPE.  */
137 const char * const *h8_reg_names;
138
139 /* Various operations needed by the following, indexed by CPU_TYPE.  */
140
141 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
142
143 /* Value of MOVE_RATIO.  */
144 int h8300_move_ratio;
145 \f
146 /* See below where shifts are handled for explanation of this enum.  */
147
148 enum shift_alg
149 {
150   SHIFT_INLINE,
151   SHIFT_ROT_AND,
152   SHIFT_SPECIAL,
153   SHIFT_LOOP
154 };
155
156 /* Symbols of the various shifts which can be used as indices.  */
157
158 enum shift_type
159 {
160   SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
161 };
162
163 /* Macros to keep the shift algorithm tables small.  */
164 #define INL SHIFT_INLINE
165 #define ROT SHIFT_ROT_AND
166 #define LOP SHIFT_LOOP
167 #define SPC SHIFT_SPECIAL
168
169 /* The shift algorithms for each machine, mode, shift type, and shift
170    count are defined below.  The three tables below correspond to
171    QImode, HImode, and SImode, respectively.  Each table is organized
172    by, in the order of indices, machine, shift type, and shift count.  */
173
174 static enum shift_alg shift_alg_qi[3][3][8] = {
175   {
176     /* TARGET_H8300  */
177     /* 0    1    2    3    4    5    6    7  */
178     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
179     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
180     { INL, INL, INL, INL, INL, LOP, LOP, SPC }  /* SHIFT_ASHIFTRT */
181   },
182   {
183     /* TARGET_H8300H  */
184     /* 0    1    2    3    4    5    6    7  */
185     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
186     { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
187     { INL, INL, INL, INL, INL, LOP, LOP, SPC }  /* SHIFT_ASHIFTRT */
188   },
189   {
190     /* TARGET_H8300S  */
191     /*  0    1    2    3    4    5    6    7  */
192     { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT   */
193     { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
194     { INL, INL, INL, INL, INL, INL, INL, SPC }  /* SHIFT_ASHIFTRT */
195   }
196 };
197
198 static enum shift_alg shift_alg_hi[3][3][16] = {
199   {
200     /* TARGET_H8300  */
201     /*  0    1    2    3    4    5    6    7  */
202     /*  8    9   10   11   12   13   14   15  */
203     { INL, INL, INL, INL, INL, INL, INL, SPC,
204       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
205     { INL, INL, INL, INL, INL, LOP, LOP, SPC,
206       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
207     { INL, INL, INL, INL, INL, LOP, LOP, SPC,
208       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
209   },
210   {
211     /* TARGET_H8300H  */
212     /*  0    1    2    3    4    5    6    7  */
213     /*  8    9   10   11   12   13   14   15  */
214     { INL, INL, INL, INL, INL, INL, INL, SPC,
215       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
216     { INL, INL, INL, INL, INL, INL, INL, SPC,
217       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
218     { INL, INL, INL, INL, INL, INL, INL, SPC,
219       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
220   },
221   {
222     /* TARGET_H8300S  */
223     /*  0    1    2    3    4    5    6    7  */
224     /*  8    9   10   11   12   13   14   15  */
225     { INL, INL, INL, INL, INL, INL, INL, INL,
226       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT   */
227     { INL, INL, INL, INL, INL, INL, INL, INL,
228       SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
229     { INL, INL, INL, INL, INL, INL, INL, INL,
230       SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
231   }
232 };
233
234 static enum shift_alg shift_alg_si[3][3][32] = {
235   {
236     /* TARGET_H8300  */
237     /*  0    1    2    3    4    5    6    7  */
238     /*  8    9   10   11   12   13   14   15  */
239     /* 16   17   18   19   20   21   22   23  */
240     /* 24   25   26   27   28   29   30   31  */
241     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
242       SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
243       SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
244       SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT   */
245     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
246       SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
247       SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
248       SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
249     { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
250       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
251       SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
252       SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
253   },
254   {
255     /* TARGET_H8300H  */
256     /*  0    1    2    3    4    5    6    7  */
257     /*  8    9   10   11   12   13   14   15  */
258     /* 16   17   18   19   20   21   22   23  */
259     /* 24   25   26   27   28   29   30   31  */
260     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
261       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
262       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
263       SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
264     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
265       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
266       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
267       SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
268     { INL, INL, INL, INL, INL, LOP, LOP, LOP,
269       SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
270       SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
271       SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
272   },
273   {
274     /* TARGET_H8300S  */
275     /*  0    1    2    3    4    5    6    7  */
276     /*  8    9   10   11   12   13   14   15  */
277     /* 16   17   18   19   20   21   22   23  */
278     /* 24   25   26   27   28   29   30   31  */
279     { INL, INL, INL, INL, INL, INL, INL, INL,
280       INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
281       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
282       SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT   */
283     { INL, INL, INL, INL, INL, INL, INL, INL,
284       INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
285       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
286       SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
287     { INL, INL, INL, INL, INL, INL, INL, INL,
288       INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
289       SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
290       SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
291   }
292 };
293
294 #undef INL
295 #undef ROT
296 #undef LOP
297 #undef SPC
298
299 enum h8_cpu
300 {
301   H8_300,
302   H8_300H,
303   H8_S
304 };
305
306 /* Initialize various cpu specific globals at start up.  */
307
308 void
309 h8300_init_once (void)
310 {
311   static const char *const h8_push_ops[2] = { "push" , "push.l" };
312   static const char *const h8_pop_ops[2]  = { "pop"  , "pop.l"  };
313   static const char *const h8_mov_ops[2]  = { "mov.w", "mov.l"  };
314
315   if (TARGET_H8300)
316     {
317       cpu_type = (int) CPU_H8300;
318       h8_reg_names = names_big;
319     }
320   else
321     {
322       /* For this we treat the H8/300H and H8S the same.  */
323       cpu_type = (int) CPU_H8300H;
324       h8_reg_names = names_extended;
325     }
326   h8_push_op = h8_push_ops[cpu_type];
327   h8_pop_op = h8_pop_ops[cpu_type];
328   h8_mov_op = h8_mov_ops[cpu_type];
329
330   if (!TARGET_H8300S && TARGET_MAC)
331     {
332       error ("-ms2600 is used without -ms");
333       target_flags |= MASK_H8300S_1;
334     }
335
336   if (TARGET_H8300 && TARGET_NORMAL_MODE)
337     {
338       error ("-mn is used without -mh or -ms");
339       target_flags ^= MASK_NORMAL_MODE;
340     }
341
342   /* Some of the shifts are optimized for speed by default.
343      See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
344      If optimizing for size, change shift_alg for those shift to
345      SHIFT_LOOP.  */
346   if (optimize_size)
347     {
348       /* H8/300 */
349       shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
350       shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
351       shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
352       shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
353
354       shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
355       shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
356
357       shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
358       shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
359
360       /* H8/300H */
361       shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
362       shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
363
364       shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
365       shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
366
367       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
368       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
369       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
370       shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
371
372       /* H8S */
373       shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
374     }
375
376   /* Work out a value for MOVE_RATIO.  */
377   if (!TARGET_H8300SX)
378     {
379       /* Memory-memory moves are quite expensive without the
380          h8sx instructions.  */
381       h8300_move_ratio = 3;
382     }
383   else if (flag_omit_frame_pointer)
384     {
385       /* movmd sequences are fairly cheap when er6 isn't fixed.  They can
386          sometimes be as short as two individual memory-to-memory moves,
387          but since they use all the call-saved registers, it seems better
388          to allow up to three moves here.  */
389       h8300_move_ratio = 4;
390     }
391   else if (optimize_size)
392     {
393       /* In this case we don't use movmd sequences since they tend
394          to be longer than calls to memcpy().  Memory-to-memory
395          moves are cheaper than for !TARGET_H8300SX, so it makes
396          sense to have a slightly higher threshold.  */
397       h8300_move_ratio = 4;
398     }
399   else
400     {
401       /* We use movmd sequences for some moves since it can be quicker
402          than calling memcpy().  The sequences will need to save and
403          restore er6 though, so bump up the cost.  */
404       h8300_move_ratio = 6;
405     }
406 }
407
408 /* Implement REG_CLASS_FROM_LETTER.
409
410    Some patterns need to use er6 as a scratch register.  This is
411    difficult to arrange since er6 is the frame pointer and usually
412    can't be spilled.
413
414    Such patterns should define two alternatives, one which allows only
415    er6 and one which allows any general register.  The former alternative
416    should have a 'd' constraint while the latter should be disparaged and
417    use 'D'.
418
419    Normally, 'd' maps to DESTINATION_REGS and 'D' maps to GENERAL_REGS.
420    However, there are cases where they should be NO_REGS:
421
422      - 'd' should be NO_REGS when reloading a function that uses the
423        frame pointer.  In this case, DESTINATION_REGS won't contain any
424        spillable registers, so the first alternative can't be used.
425
426      - -fno-omit-frame-pointer means that the frame pointer will
427        always be in use.  It's therefore better to map 'd' to NO_REGS
428        before reload so that register allocator will pick the second
429        alternative.
430
431      - we would like 'D' to be be NO_REGS when the frame pointer isn't
432        live, but we the frame pointer may turn out to be needed after
433        we start reload, and then we may have already decided we don't
434        have a choice, so we can't do that.  Forcing the register
435        allocator to use er6 if possible might produce better code for
436        small functions: it's more efficient to save and restore er6 in
437        the prologue & epilogue than to do it in a define_split.
438        Hopefully disparaging 'D' will have a similar effect, without
439        forcing a reload failure if the frame pointer is found to be
440        needed too late.  */
441
442 enum reg_class
443 h8300_reg_class_from_letter (int c)
444 {
445   switch (c)
446     {
447     case 'a':
448       return MAC_REGS;
449
450     case 'c':
451       return COUNTER_REGS;
452
453     case 'd':
454       if (!flag_omit_frame_pointer && !reload_completed)
455         return NO_REGS;
456       if (frame_pointer_needed && reload_in_progress)
457         return NO_REGS;
458       return DESTINATION_REGS;
459
460     case 'D':
461       /* The meaning of a constraint shouldn't change dynamically, so
462          we can't make this NO_REGS.  */
463       return GENERAL_REGS;
464
465     case 'f':
466       return SOURCE_REGS;
467
468     default:
469       return NO_REGS;
470     }
471 }
472
473 /* Return the byte register name for a register rtx X.  B should be 0
474    if you want a lower byte register.  B should be 1 if you want an
475    upper byte register.  */
476
477 static const char *
478 byte_reg (rtx x, int b)
479 {
480   static const char *const names_small[] = {
481     "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
482     "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
483   };
484
485   gcc_assert (REG_P (x));
486
487   return names_small[REGNO (x) * 2 + b];
488 }
489
490 /* REGNO must be saved/restored across calls if this macro is true.  */
491
492 #define WORD_REG_USED(regno)                                            \
493   (regno < SP_REG                                                       \
494    /* No need to save registers if this function will not return.  */   \
495    && ! TREE_THIS_VOLATILE (current_function_decl)                      \
496    && (h8300_saveall_function_p (current_function_decl)                 \
497        /* Save any call saved register that was used.  */               \
498        || (df_regs_ever_live_p (regno) && !call_used_regs[regno])       \
499        /* Save the frame pointer if it was used.  */                    \
500        || (regno == HARD_FRAME_POINTER_REGNUM && df_regs_ever_live_p (regno)) \
501        /* Save any register used in an interrupt handler.  */           \
502        || (h8300_current_function_interrupt_function_p ()               \
503            && df_regs_ever_live_p (regno))                              \
504        /* Save call clobbered registers in non-leaf interrupt           \
505           handlers.  */                                                 \
506        || (h8300_current_function_interrupt_function_p ()               \
507            && call_used_regs[regno]                                     \
508            && !current_function_is_leaf)))
509
510 /* We use this to wrap all emitted insns in the prologue.  */
511 static rtx
512 F (rtx x, bool set_it)
513 {
514   if (set_it)
515     RTX_FRAME_RELATED_P (x) = 1;
516   return x;
517 }
518
519 /* Mark all the subexpressions of the PARALLEL rtx PAR as
520    frame-related.  Return PAR.
521
522    dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
523    PARALLEL rtx other than the first if they do not have the
524    FRAME_RELATED flag set on them.  */
525 static rtx
526 Fpa (rtx par)
527 {
528   int len = XVECLEN (par, 0);
529   int i;
530
531   for (i = 0; i < len; i++)
532     F (XVECEXP (par, 0, i), true);
533
534   return par;
535 }
536
537 /* Output assembly language to FILE for the operation OP with operand size
538    SIZE to adjust the stack pointer.  */
539
540 static void
541 h8300_emit_stack_adjustment (int sign, HOST_WIDE_INT size, bool in_prologue)
542 {
543   /* If the frame size is 0, we don't have anything to do.  */
544   if (size == 0)
545     return;
546
547   /* H8/300 cannot add/subtract a large constant with a single
548      instruction.  If a temporary register is available, load the
549      constant to it and then do the addition.  */
550   if (TARGET_H8300
551       && size > 4
552       && !h8300_current_function_interrupt_function_p ()
553       && !(cfun->static_chain_decl != NULL && sign < 0))
554     {
555       rtx r3 = gen_rtx_REG (Pmode, 3);
556       F (emit_insn (gen_movhi (r3, GEN_INT (sign * size))), in_prologue);
557       F (emit_insn (gen_addhi3 (stack_pointer_rtx,
558                                 stack_pointer_rtx, r3)), in_prologue);
559     }
560   else
561     {
562       /* The stack adjustment made here is further optimized by the
563          splitter.  In case of H8/300, the splitter always splits the
564          addition emitted here to make the adjustment interrupt-safe.
565          FIXME: We don't always tag those, because we don't know what
566          the splitter will do.  */
567       if (Pmode == HImode)
568         {
569           rtx x = emit_insn (gen_addhi3 (stack_pointer_rtx,
570                                          stack_pointer_rtx, GEN_INT (sign * size)));
571           if (size < 4)
572             F (x, in_prologue);
573         }
574       else
575         F (emit_insn (gen_addsi3 (stack_pointer_rtx,
576                                   stack_pointer_rtx, GEN_INT (sign * size))), in_prologue);
577     }
578 }
579
580 /* Round up frame size SIZE.  */
581
582 static HOST_WIDE_INT
583 round_frame_size (HOST_WIDE_INT size)
584 {
585   return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
586           & -STACK_BOUNDARY / BITS_PER_UNIT);
587 }
588
589 /* Compute which registers to push/pop.
590    Return a bit vector of registers.  */
591
592 static unsigned int
593 compute_saved_regs (void)
594 {
595   unsigned int saved_regs = 0;
596   int regno;
597
598   /* Construct a bit vector of registers to be pushed/popped.  */
599   for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
600     {
601       if (WORD_REG_USED (regno))
602         saved_regs |= 1 << regno;
603     }
604
605   /* Don't push/pop the frame pointer as it is treated separately.  */
606   if (frame_pointer_needed)
607     saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
608
609   return saved_regs;
610 }
611
612 /* Emit an insn to push register RN.  */
613
614 static void
615 push (int rn)
616 {
617   rtx reg = gen_rtx_REG (word_mode, rn);
618   rtx x;
619
620   if (TARGET_H8300)
621     x = gen_push_h8300 (reg);
622   else if (!TARGET_NORMAL_MODE)
623     x = gen_push_h8300hs_advanced (reg);
624   else
625     x = gen_push_h8300hs_normal (reg);
626   x = F (emit_insn (x), true);
627   REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
628 }
629
630 /* Emit an insn to pop register RN.  */
631
632 static void
633 pop (int rn)
634 {
635   rtx reg = gen_rtx_REG (word_mode, rn);
636   rtx x;
637
638   if (TARGET_H8300)
639     x = gen_pop_h8300 (reg);
640   else if (!TARGET_NORMAL_MODE)
641     x = gen_pop_h8300hs_advanced (reg);
642   else
643     x = gen_pop_h8300hs_normal (reg);
644   x = emit_insn (x);
645   REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
646 }
647
648 /* Emit an instruction to push or pop NREGS consecutive registers
649    starting at register REGNO.  POP_P selects a pop rather than a
650    push and RETURN_P is true if the instruction should return.
651
652    It must be possible to do the requested operation in a single
653    instruction.  If NREGS == 1 && !RETURN_P, use a normal push
654    or pop insn.  Otherwise emit a parallel of the form:
655
656      (parallel
657        [(return)  ;; if RETURN_P
658         (save or restore REGNO)
659         (save or restore REGNO + 1)
660         ...
661         (save or restore REGNO + NREGS - 1)
662         (set sp (plus sp (const_int adjust)))]  */
663
664 static void
665 h8300_push_pop (int regno, int nregs, bool pop_p, bool return_p)
666 {
667   int i, j;
668   rtvec vec;
669   rtx sp, offset, x;
670
671   /* See whether we can use a simple push or pop.  */
672   if (!return_p && nregs == 1)
673     {
674       if (pop_p)
675         pop (regno);
676       else
677         push (regno);
678       return;
679     }
680
681   /* We need one element for the return insn, if present, one for each
682      register, and one for stack adjustment.  */
683   vec = rtvec_alloc ((return_p ? 1 : 0) + nregs + 1);
684   sp = stack_pointer_rtx;
685   i = 0;
686
687   /* Add the return instruction.  */
688   if (return_p)
689     {
690       RTVEC_ELT (vec, i) = gen_rtx_RETURN (VOIDmode);
691       i++;
692     }
693
694   /* Add the register moves.  */
695   for (j = 0; j < nregs; j++)
696     {
697       rtx lhs, rhs;
698
699       if (pop_p)
700         {
701           /* Register REGNO + NREGS - 1 is popped first.  Before the
702              stack adjustment, its slot is at address @sp.  */
703           lhs = gen_rtx_REG (SImode, regno + j);
704           rhs = gen_rtx_MEM (SImode, plus_constant (sp, (nregs - j - 1) * 4));
705         }
706       else
707         {
708           /* Register REGNO is pushed first and will be stored at @(-4,sp).  */
709           lhs = gen_rtx_MEM (SImode, plus_constant (sp, (j + 1) * -4));
710           rhs = gen_rtx_REG (SImode, regno + j);
711         }
712       RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, lhs, rhs);
713     }
714
715   /* Add the stack adjustment.  */
716   offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
717   RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, sp,
718                                         gen_rtx_PLUS (Pmode, sp, offset));
719
720   x = gen_rtx_PARALLEL (VOIDmode, vec);
721   if (!pop_p)
722     x = Fpa (x);
723
724   if (return_p)
725     emit_jump_insn (x);
726   else
727     emit_insn (x);
728 }
729
730 /* Return true if X has the value sp + OFFSET.  */
731
732 static int
733 h8300_stack_offset_p (rtx x, int offset)
734 {
735   if (offset == 0)
736     return x == stack_pointer_rtx;
737
738   return (GET_CODE (x) == PLUS
739           && XEXP (x, 0) == stack_pointer_rtx
740           && GET_CODE (XEXP (x, 1)) == CONST_INT
741           && INTVAL (XEXP (x, 1)) == offset);
742 }
743
744 /* A subroutine of h8300_ldm_stm_parallel.  X is one pattern in
745    something that may be an ldm or stm instruction.  If it fits
746    the required template, return the register it loads or stores,
747    otherwise return -1.
748
749    LOAD_P is true if X should be a load, false if it should be a store.
750    NREGS is the number of registers that the whole instruction is expected
751    to load or store.  INDEX is the index of the register that X should
752    load or store, relative to the lowest-numbered register.  */
753
754 static int
755 h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
756 {
757   int regindex, memindex, offset;
758
759   if (load_p)
760     regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
761   else
762     memindex = 0, regindex = 1, offset = (index + 1) * -4;
763
764   if (GET_CODE (x) == SET
765       && GET_CODE (XEXP (x, regindex)) == REG
766       && GET_CODE (XEXP (x, memindex)) == MEM
767       && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
768     return REGNO (XEXP (x, regindex));
769
770   return -1;
771 }
772
773 /* Return true if the elements of VEC starting at FIRST describe an
774    ldm or stm instruction (LOAD_P says which).  */
775
776 int
777 h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
778 {
779   rtx last;
780   int nregs, i, regno, adjust;
781
782   /* There must be a stack adjustment, a register move, and at least one
783      other operation (a return or another register move).  */
784   if (GET_NUM_ELEM (vec) < 3)
785     return false;
786
787   /* Get the range of registers to be pushed or popped.  */
788   nregs = GET_NUM_ELEM (vec) - first - 1;
789   regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
790
791   /* Check that the call to h8300_ldm_stm_regno succeeded and
792      that we're only dealing with GPRs.  */
793   if (regno < 0 || regno + nregs > 8)
794     return false;
795
796   /* 2-register h8s instructions must start with an even-numbered register.
797      3- and 4-register instructions must start with er0 or er4.  */
798   if (!TARGET_H8300SX)
799     {
800       if ((regno & 1) != 0)
801         return false;
802       if (nregs > 2 && (regno & 3) != 0)
803         return false;
804     }
805
806   /* Check the other loads or stores.  */
807   for (i = 1; i < nregs; i++)
808     if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
809         != regno + i)
810       return false;
811
812   /* Check the stack adjustment.  */
813   last = RTVEC_ELT (vec, first + nregs);
814   adjust = (load_p ? nregs : -nregs) * 4;
815   return (GET_CODE (last) == SET
816           && SET_DEST (last) == stack_pointer_rtx
817           && h8300_stack_offset_p (SET_SRC (last), adjust));
818 }
819
820 /* This is what the stack looks like after the prolog of
821    a function with a frame has been set up:
822
823    <args>
824    PC
825    FP                   <- fp
826    <locals>
827    <saved registers>    <- sp
828
829    This is what the stack looks like after the prolog of
830    a function which doesn't have a frame:
831
832    <args>
833    PC
834    <locals>
835    <saved registers>    <- sp
836 */
837
838 /* Generate RTL code for the function prologue.  */
839
840 void
841 h8300_expand_prologue (void)
842 {
843   int regno;
844   int saved_regs;
845   int n_regs;
846
847   /* If the current function has the OS_Task attribute set, then
848      we have a naked prologue.  */
849   if (h8300_os_task_function_p (current_function_decl))
850     return;
851
852   if (h8300_monitor_function_p (current_function_decl))
853     /* My understanding of monitor functions is they act just like
854        interrupt functions, except the prologue must mask
855        interrupts.  */
856     emit_insn (gen_monitor_prologue ());
857
858   if (frame_pointer_needed)
859     {
860       /* Push fp.  */
861       push (HARD_FRAME_POINTER_REGNUM);
862       F (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx), true);
863     }
864
865   /* Push the rest of the registers in ascending order.  */
866   saved_regs = compute_saved_regs ();
867   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
868     {
869       n_regs = 1;
870       if (saved_regs & (1 << regno))
871         {
872           if (TARGET_H8300S)
873             {
874               /* See how many registers we can push at the same time.  */
875               if ((!TARGET_H8300SX || (regno & 3) == 0)
876                   && ((saved_regs >> regno) & 0x0f) == 0x0f)
877                 n_regs = 4;
878
879               else if ((!TARGET_H8300SX || (regno & 3) == 0)
880                        && ((saved_regs >> regno) & 0x07) == 0x07)
881                 n_regs = 3;
882
883               else if ((!TARGET_H8300SX || (regno & 1) == 0)
884                        && ((saved_regs >> regno) & 0x03) == 0x03)
885                 n_regs = 2;
886             }
887
888           h8300_push_pop (regno, n_regs, false, false);
889         }
890     }
891
892   /* Leave room for locals.  */
893   h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()), true);
894 }
895
896 /* Return nonzero if we can use "rts" for the function currently being
897    compiled.  */
898
899 int
900 h8300_can_use_return_insn_p (void)
901 {
902   return (reload_completed
903           && !frame_pointer_needed
904           && get_frame_size () == 0
905           && compute_saved_regs () == 0);
906 }
907
908 /* Generate RTL code for the function epilogue.  */
909
910 void
911 h8300_expand_epilogue (void)
912 {
913   int regno;
914   int saved_regs;
915   int n_regs;
916   HOST_WIDE_INT frame_size;
917   bool returned_p;
918
919   if (h8300_os_task_function_p (current_function_decl))
920     /* OS_Task epilogues are nearly naked -- they just have an
921        rts instruction.  */
922     return;
923
924   frame_size = round_frame_size (get_frame_size ());
925   returned_p = false;
926
927   /* Deallocate locals.  */
928   h8300_emit_stack_adjustment (1, frame_size, false);
929
930   /* Pop the saved registers in descending order.  */
931   saved_regs = compute_saved_regs ();
932   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
933     {
934       n_regs = 1;
935       if (saved_regs & (1 << regno))
936         {
937           if (TARGET_H8300S)
938             {
939               /* See how many registers we can pop at the same time.  */
940               if ((TARGET_H8300SX || (regno & 3) == 3)
941                   && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
942                 n_regs = 4;
943
944               else if ((TARGET_H8300SX || (regno & 3) == 2)
945                        && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
946                 n_regs = 3;
947
948               else if ((TARGET_H8300SX || (regno & 1) == 1)
949                        && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
950                 n_regs = 2;
951             }
952
953           /* See if this pop would be the last insn before the return.
954              If so, use rte/l or rts/l instead of pop or ldm.l.  */
955           if (TARGET_H8300SX
956               && !frame_pointer_needed
957               && frame_size == 0
958               && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
959             returned_p = true;
960
961           h8300_push_pop (regno - n_regs + 1, n_regs, true, returned_p);
962         }
963     }
964
965   /* Pop frame pointer if we had one.  */
966   if (frame_pointer_needed)
967     {
968       if (TARGET_H8300SX)
969         returned_p = true;
970       h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, true, returned_p);
971     }
972
973   if (!returned_p)
974     emit_jump_insn (gen_rtx_RETURN (VOIDmode));
975 }
976
977 /* Return nonzero if the current function is an interrupt
978    function.  */
979
980 int
981 h8300_current_function_interrupt_function_p (void)
982 {
983   return (h8300_interrupt_function_p (current_function_decl)
984           || h8300_monitor_function_p (current_function_decl));
985 }
986
987 /* Output assembly code for the start of the file.  */
988
989 static void
990 h8300_file_start (void)
991 {
992   default_file_start ();
993
994   if (TARGET_H8300H)
995     fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
996   else if (TARGET_H8300SX)
997     fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
998   else if (TARGET_H8300S)
999     fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
1000 }
1001
1002 /* Output assembly language code for the end of file.  */
1003
1004 static void
1005 h8300_file_end (void)
1006 {
1007   fputs ("\t.end\n", asm_out_file);
1008 }
1009 \f
1010 /* Split an add of a small constant into two adds/subs insns.
1011
1012    If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
1013    instead of adds/subs.  */
1014
1015 void
1016 split_adds_subs (enum machine_mode mode, rtx *operands)
1017 {
1018   HOST_WIDE_INT val = INTVAL (operands[1]);
1019   rtx reg = operands[0];
1020   HOST_WIDE_INT sign = 1;
1021   HOST_WIDE_INT amount;
1022   rtx (*gen_add) (rtx, rtx, rtx);
1023
1024   /* Force VAL to be positive so that we do not have to consider the
1025      sign.  */
1026   if (val < 0)
1027     {
1028       val = -val;
1029       sign = -1;
1030     }
1031
1032   switch (mode)
1033     {
1034     case HImode:
1035       gen_add = gen_addhi3;
1036       break;
1037
1038     case SImode:
1039       gen_add = gen_addsi3;
1040       break;
1041
1042     default:
1043       gcc_unreachable ();
1044     }
1045
1046   /* Try different amounts in descending order.  */
1047   for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
1048        amount > 0;
1049        amount /= 2)
1050     {
1051       for (; val >= amount; val -= amount)
1052         emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
1053     }
1054
1055   return;
1056 }
1057
1058 /* Handle machine specific pragmas for compatibility with existing
1059    compilers for the H8/300.
1060
1061    pragma saveall generates prologue/epilogue code which saves and
1062    restores all the registers on function entry.
1063
1064    pragma interrupt saves and restores all registers, and exits with
1065    an rte instruction rather than an rts.  A pointer to a function
1066    with this attribute may be safely used in an interrupt vector.  */
1067
1068 void
1069 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1070 {
1071   pragma_interrupt = 1;
1072 }
1073
1074 void
1075 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1076 {
1077   pragma_saveall = 1;
1078 }
1079
1080 /* If the next function argument with MODE and TYPE is to be passed in
1081    a register, return a reg RTX for the hard register in which to pass
1082    the argument.  CUM represents the state after the last argument.
1083    If the argument is to be pushed, NULL_RTX is returned.  */
1084
1085 rtx
1086 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1087               tree type, int named)
1088 {
1089   static const char *const hand_list[] = {
1090     "__main",
1091     "__cmpsi2",
1092     "__divhi3",
1093     "__modhi3",
1094     "__udivhi3",
1095     "__umodhi3",
1096     "__divsi3",
1097     "__modsi3",
1098     "__udivsi3",
1099     "__umodsi3",
1100     "__mulhi3",
1101     "__mulsi3",
1102     "__reg_memcpy",
1103     "__reg_memset",
1104     "__ucmpsi2",
1105     0,
1106   };
1107
1108   rtx result = NULL_RTX;
1109   const char *fname;
1110   int regpass = 0;
1111
1112   /* Never pass unnamed arguments in registers.  */
1113   if (!named)
1114     return NULL_RTX;
1115
1116   /* Pass 3 regs worth of data in regs when user asked on the command line.  */
1117   if (TARGET_QUICKCALL)
1118     regpass = 3;
1119
1120   /* If calling hand written assembler, use 4 regs of args.  */
1121   if (cum->libcall)
1122     {
1123       const char * const *p;
1124
1125       fname = XSTR (cum->libcall, 0);
1126
1127       /* See if this libcall is one of the hand coded ones.  */
1128       for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1129         ;
1130
1131       if (*p)
1132         regpass = 4;
1133     }
1134
1135   if (regpass)
1136     {
1137       int size;
1138
1139       if (mode == BLKmode)
1140         size = int_size_in_bytes (type);
1141       else
1142         size = GET_MODE_SIZE (mode);
1143
1144       if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1145           && cum->nbytes / UNITS_PER_WORD <= 3)
1146         result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1147     }
1148
1149   return result;
1150 }
1151 \f
1152 /* Compute the cost of an and insn.  */
1153
1154 static int
1155 h8300_and_costs (rtx x)
1156 {
1157   rtx operands[4];
1158
1159   if (GET_MODE (x) == QImode)
1160     return 1;
1161
1162   if (GET_MODE (x) != HImode
1163       && GET_MODE (x) != SImode)
1164     return 100;
1165
1166   operands[0] = NULL;
1167   operands[1] = XEXP (x, 0);
1168   operands[2] = XEXP (x, 1);
1169   operands[3] = x;
1170   return compute_logical_op_length (GET_MODE (x), operands) / 2;
1171 }
1172
1173 /* Compute the cost of a shift insn.  */
1174
1175 static int
1176 h8300_shift_costs (rtx x)
1177 {
1178   rtx operands[4];
1179
1180   if (GET_MODE (x) != QImode
1181       && GET_MODE (x) != HImode
1182       && GET_MODE (x) != SImode)
1183     return 100;
1184
1185   operands[0] = NULL;
1186   operands[1] = NULL;
1187   operands[2] = XEXP (x, 1);
1188   operands[3] = x;
1189   return compute_a_shift_length (NULL, operands) / 2;
1190 }
1191
1192 /* Worker function for TARGET_RTX_COSTS.  */
1193
1194 static bool
1195 h8300_rtx_costs (rtx x, int code, int outer_code, int *total, bool speed)
1196 {
1197   if (TARGET_H8300SX && outer_code == MEM)
1198     {
1199       /* Estimate the number of execution states needed to calculate
1200          the address.  */
1201       if (register_operand (x, VOIDmode)
1202           || GET_CODE (x) == POST_INC
1203           || GET_CODE (x) == POST_DEC
1204           || CONSTANT_P (x))
1205         *total = 0;
1206       else
1207         *total = COSTS_N_INSNS (1);
1208       return true;
1209     }
1210
1211   switch (code)
1212     {
1213     case CONST_INT:
1214       {
1215         HOST_WIDE_INT n = INTVAL (x);
1216
1217         if (TARGET_H8300SX)
1218           {
1219             /* Constant operands need the same number of processor
1220                states as register operands.  Although we could try to
1221                use a size-based cost for !speed, the lack of
1222                of a mode makes the results very unpredictable.  */
1223             *total = 0;
1224             return true;
1225           }
1226         if (-4 <= n || n <= 4)
1227           {
1228             switch ((int) n)
1229               {
1230               case 0:
1231                 *total = 0;
1232                 return true;
1233               case 1:
1234               case 2:
1235               case -1:
1236               case -2:
1237                 *total = 0 + (outer_code == SET);
1238                 return true;
1239               case 4:
1240               case -4:
1241                 if (TARGET_H8300H || TARGET_H8300S)
1242                   *total = 0 + (outer_code == SET);
1243                 else
1244                   *total = 1;
1245                 return true;
1246               }
1247           }
1248         *total = 1;
1249         return true;
1250       }
1251
1252     case CONST:
1253     case LABEL_REF:
1254     case SYMBOL_REF:
1255       if (TARGET_H8300SX)
1256         {
1257           /* See comment for CONST_INT.  */
1258           *total = 0;
1259           return true;
1260         }
1261       *total = 3;
1262       return true;
1263
1264     case CONST_DOUBLE:
1265       *total = 20;
1266       return true;
1267
1268     case COMPARE:
1269       if (XEXP (x, 1) == const0_rtx)
1270         *total = 0;
1271       return false;
1272
1273     case AND:
1274       if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1275           || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1276         return false;
1277       *total = COSTS_N_INSNS (h8300_and_costs (x));
1278       return true;
1279
1280     /* We say that MOD and DIV are so expensive because otherwise we'll
1281        generate some really horrible code for division of a power of two.  */
1282     case MOD:
1283     case DIV:
1284     case UMOD:
1285     case UDIV:
1286       if (TARGET_H8300SX)
1287         switch (GET_MODE (x))
1288           {
1289           case QImode:
1290           case HImode:
1291             *total = COSTS_N_INSNS (!speed ? 4 : 10);
1292             return false;
1293
1294           case SImode:
1295             *total = COSTS_N_INSNS (!speed ? 4 : 18);
1296             return false;
1297
1298           default:
1299             break;
1300           }
1301       *total = COSTS_N_INSNS (12);
1302       return true;
1303
1304     case MULT:
1305       if (TARGET_H8300SX)
1306         switch (GET_MODE (x))
1307           {
1308           case QImode:
1309           case HImode:
1310             *total = COSTS_N_INSNS (2);
1311             return false;
1312
1313           case SImode:
1314             *total = COSTS_N_INSNS (5);
1315             return false;
1316
1317           default:
1318             break;
1319           }
1320       *total = COSTS_N_INSNS (4);
1321       return true;
1322
1323     case ASHIFT:
1324     case ASHIFTRT:
1325     case LSHIFTRT:
1326       if (h8sx_binary_shift_operator (x, VOIDmode))
1327         {
1328           *total = COSTS_N_INSNS (2);
1329           return false;
1330         }
1331       else if (h8sx_unary_shift_operator (x, VOIDmode))
1332         {
1333           *total = COSTS_N_INSNS (1);
1334           return false;
1335         }
1336       *total = COSTS_N_INSNS (h8300_shift_costs (x));
1337       return true;
1338
1339     case ROTATE:
1340     case ROTATERT:
1341       if (GET_MODE (x) == HImode)
1342         *total = 2;
1343       else
1344         *total = 8;
1345       return true;
1346
1347     default:
1348       *total = COSTS_N_INSNS (1);
1349       return false;
1350     }
1351 }
1352 \f
1353 /* Documentation for the machine specific operand escapes:
1354
1355    'E' like s but negative.
1356    'F' like t but negative.
1357    'G' constant just the negative
1358    'R' print operand as a byte:8 address if appropriate, else fall back to
1359        'X' handling.
1360    'S' print operand as a long word
1361    'T' print operand as a word
1362    'V' find the set bit, and print its number.
1363    'W' find the clear bit, and print its number.
1364    'X' print operand as a byte
1365    'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1366        If this operand isn't a register, fall back to 'R' handling.
1367    'Z' print int & 7.
1368    'c' print the opcode corresponding to rtl
1369    'e' first word of 32-bit value - if reg, then least reg. if mem
1370        then least. if const then most sig word
1371    'f' second word of 32-bit value - if reg, then biggest reg. if mem
1372        then +2. if const then least sig word
1373    'j' print operand as condition code.
1374    'k' print operand as reverse condition code.
1375    'm' convert an integer operand to a size suffix (.b, .w or .l)
1376    'o' print an integer without a leading '#'
1377    's' print as low byte of 16-bit value
1378    't' print as high byte of 16-bit value
1379    'w' print as low byte of 32-bit value
1380    'x' print as 2nd byte of 32-bit value
1381    'y' print as 3rd byte of 32-bit value
1382    'z' print as msb of 32-bit value
1383 */
1384
1385 /* Return assembly language string which identifies a comparison type.  */
1386
1387 static const char *
1388 cond_string (enum rtx_code code)
1389 {
1390   switch (code)
1391     {
1392     case NE:
1393       return "ne";
1394     case EQ:
1395       return "eq";
1396     case GE:
1397       return "ge";
1398     case GT:
1399       return "gt";
1400     case LE:
1401       return "le";
1402     case LT:
1403       return "lt";
1404     case GEU:
1405       return "hs";
1406     case GTU:
1407       return "hi";
1408     case LEU:
1409       return "ls";
1410     case LTU:
1411       return "lo";
1412     default:
1413       gcc_unreachable ();
1414     }
1415 }
1416
1417 /* Print operand X using operand code CODE to assembly language output file
1418    FILE.  */
1419
1420 void
1421 print_operand (FILE *file, rtx x, int code)
1422 {
1423   /* This is used for communication between codes V,W,Z and Y.  */
1424   static int bitint;
1425
1426   switch (code)
1427     {
1428     case 'E':
1429       switch (GET_CODE (x))
1430         {
1431         case REG:
1432           fprintf (file, "%sl", names_big[REGNO (x)]);
1433           break;
1434         case CONST_INT:
1435           fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1436           break;
1437         default:
1438           gcc_unreachable ();
1439         }
1440       break;
1441     case 'F':
1442       switch (GET_CODE (x))
1443         {
1444         case REG:
1445           fprintf (file, "%sh", names_big[REGNO (x)]);
1446           break;
1447         case CONST_INT:
1448           fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1449           break;
1450         default:
1451           gcc_unreachable ();
1452         }
1453       break;
1454     case 'G':
1455       gcc_assert (GET_CODE (x) == CONST_INT);
1456       fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1457       break;
1458     case 'S':
1459       if (GET_CODE (x) == REG)
1460         fprintf (file, "%s", names_extended[REGNO (x)]);
1461       else
1462         goto def;
1463       break;
1464     case 'T':
1465       if (GET_CODE (x) == REG)
1466         fprintf (file, "%s", names_big[REGNO (x)]);
1467       else
1468         goto def;
1469       break;
1470     case 'V':
1471       bitint = (INTVAL (x) & 0xffff);
1472       if ((exact_log2 ((bitint >> 8) & 0xff)) == -1)
1473         bitint = exact_log2 (bitint & 0xff);
1474       else
1475         bitint = exact_log2 ((bitint >> 8) & 0xff);           
1476       gcc_assert (bitint >= 0);
1477       fprintf (file, "#%d", bitint);
1478       break;
1479     case 'W':
1480       bitint = ((~INTVAL (x)) & 0xffff);
1481       if ((exact_log2 ((bitint >> 8) & 0xff)) == -1 )
1482         bitint = exact_log2 (bitint & 0xff);
1483       else
1484         bitint = (exact_log2 ((bitint >> 8) & 0xff));      
1485       gcc_assert (bitint >= 0);
1486       fprintf (file, "#%d", bitint);
1487       break;
1488     case 'R':
1489     case 'X':
1490       if (GET_CODE (x) == REG)
1491         fprintf (file, "%s", byte_reg (x, 0));
1492       else
1493         goto def;
1494       break;
1495     case 'Y':
1496       gcc_assert (bitint >= 0);
1497       if (GET_CODE (x) == REG)
1498         fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1499       else
1500         print_operand (file, x, 'R');
1501       bitint = -1;
1502       break;
1503     case 'Z':
1504       bitint = INTVAL (x);
1505       fprintf (file, "#%d", bitint & 7);
1506       break;
1507     case 'c':
1508       switch (GET_CODE (x))
1509         {
1510         case IOR:
1511           fprintf (file, "or");
1512           break;
1513         case XOR:
1514           fprintf (file, "xor");
1515           break;
1516         case AND:
1517           fprintf (file, "and");
1518           break;
1519         default:
1520           break;
1521         }
1522       break;
1523     case 'e':
1524       switch (GET_CODE (x))
1525         {
1526         case REG:
1527           if (TARGET_H8300)
1528             fprintf (file, "%s", names_big[REGNO (x)]);
1529           else
1530             fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1531           break;
1532         case MEM:
1533           print_operand (file, x, 0);
1534           break;
1535         case CONST_INT:
1536           fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1537           break;
1538         case CONST_DOUBLE:
1539           {
1540             long val;
1541             REAL_VALUE_TYPE rv;
1542             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1543             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1544             fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1545             break;
1546           }
1547         default:
1548           gcc_unreachable ();
1549           break;
1550         }
1551       break;
1552     case 'f':
1553       switch (GET_CODE (x))
1554         {
1555         case REG:
1556           if (TARGET_H8300)
1557             fprintf (file, "%s", names_big[REGNO (x) + 1]);
1558           else
1559             fprintf (file, "%s", names_big[REGNO (x)]);
1560           break;
1561         case MEM:
1562           x = adjust_address (x, HImode, 2);
1563           print_operand (file, x, 0);
1564           break;
1565         case CONST_INT:
1566           fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1567           break;
1568         case CONST_DOUBLE:
1569           {
1570             long val;
1571             REAL_VALUE_TYPE rv;
1572             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1573             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1574             fprintf (file, "#%ld", (val & 0xffff));
1575             break;
1576           }
1577         default:
1578           gcc_unreachable ();
1579         }
1580       break;
1581     case 'j':
1582       fputs (cond_string (GET_CODE (x)), file);
1583       break;
1584     case 'k':
1585       fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1586       break;
1587     case 'm':
1588       gcc_assert (GET_CODE (x) == CONST_INT);
1589       switch (INTVAL (x))
1590         {
1591         case 1:
1592           fputs (".b", file);
1593           break;
1594
1595         case 2:
1596           fputs (".w", file);
1597           break;
1598
1599         case 4:
1600           fputs (".l", file);
1601           break;
1602
1603         default:
1604           gcc_unreachable ();
1605         }
1606       break;
1607     case 'o':
1608       print_operand_address (file, x);
1609       break;
1610     case 's':
1611       if (GET_CODE (x) == CONST_INT)
1612         fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1613       else
1614         fprintf (file, "%s", byte_reg (x, 0));
1615       break;
1616     case 't':
1617       if (GET_CODE (x) == CONST_INT)
1618         fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1619       else
1620         fprintf (file, "%s", byte_reg (x, 1));
1621       break;
1622     case 'w':
1623       if (GET_CODE (x) == CONST_INT)
1624         fprintf (file, "#%ld", INTVAL (x) & 0xff);
1625       else
1626         fprintf (file, "%s",
1627                  byte_reg (x, TARGET_H8300 ? 2 : 0));
1628       break;
1629     case 'x':
1630       if (GET_CODE (x) == CONST_INT)
1631         fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1632       else
1633         fprintf (file, "%s",
1634                  byte_reg (x, TARGET_H8300 ? 3 : 1));
1635       break;
1636     case 'y':
1637       if (GET_CODE (x) == CONST_INT)
1638         fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1639       else
1640         fprintf (file, "%s", byte_reg (x, 0));
1641       break;
1642     case 'z':
1643       if (GET_CODE (x) == CONST_INT)
1644         fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1645       else
1646         fprintf (file, "%s", byte_reg (x, 1));
1647       break;
1648
1649     default:
1650     def:
1651       switch (GET_CODE (x))
1652         {
1653         case REG:
1654           switch (GET_MODE (x))
1655             {
1656             case QImode:
1657 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1658               fprintf (file, "%s", byte_reg (x, 0));
1659 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1660               fprintf (file, "%s", names_big[REGNO (x)]);
1661 #endif
1662               break;
1663             case HImode:
1664               fprintf (file, "%s", names_big[REGNO (x)]);
1665               break;
1666             case SImode:
1667             case SFmode:
1668               fprintf (file, "%s", names_extended[REGNO (x)]);
1669               break;
1670             default:
1671               gcc_unreachable ();
1672             }
1673           break;
1674
1675         case MEM:
1676           {
1677             rtx addr = XEXP (x, 0);
1678
1679             fprintf (file, "@");
1680             output_address (addr);
1681
1682             /* Add a length suffix to constant addresses.  Although this
1683                is often unnecessary, it helps to avoid ambiguity in the
1684                syntax of mova.  If we wrote an insn like:
1685
1686                     mova/w.l @(1,@foo.b),er0
1687
1688                then .b would be considered part of the symbol name.
1689                Adding a length after foo will avoid this.  */
1690             if (CONSTANT_P (addr))
1691               switch (code)
1692                 {
1693                 case 'R':
1694                   /* Used for mov.b and bit operations.  */
1695                   if (h8300_eightbit_constant_address_p (addr))
1696                     {
1697                       fprintf (file, ":8");
1698                       break;
1699                     }
1700
1701                   /* Fall through.  We should not get here if we are
1702                      processing bit operations on H8/300 or H8/300H
1703                      because 'U' constraint does not allow bit
1704                      operations on the tiny area on these machines.  */
1705
1706                 case 'X':
1707                 case 'T':
1708                 case 'S':
1709                   if (h8300_constant_length (addr) == 2)
1710                     fprintf (file, ":16");
1711                   else
1712                     fprintf (file, ":32");
1713                   break;
1714                 default:
1715                   break;
1716                 }
1717           }
1718           break;
1719
1720         case CONST_INT:
1721         case SYMBOL_REF:
1722         case CONST:
1723         case LABEL_REF:
1724           fprintf (file, "#");
1725           print_operand_address (file, x);
1726           break;
1727         case CONST_DOUBLE:
1728           {
1729             long val;
1730             REAL_VALUE_TYPE rv;
1731             REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1732             REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1733             fprintf (file, "#%ld", val);
1734             break;
1735           }
1736         default:
1737           break;
1738         }
1739     }
1740 }
1741
1742 /* Output assembly language output for the address ADDR to FILE.  */
1743
1744 void
1745 print_operand_address (FILE *file, rtx addr)
1746 {
1747   rtx index;
1748   int size;
1749
1750   switch (GET_CODE (addr))
1751     {
1752     case REG:
1753       fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1754       break;
1755
1756     case PRE_DEC:
1757       fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1758       break;
1759
1760     case POST_INC:
1761       fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1762       break;
1763
1764     case PRE_INC:
1765       fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1766       break;
1767
1768     case POST_DEC:
1769       fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1770       break;
1771
1772     case PLUS:
1773       fprintf (file, "(");
1774
1775       index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1776       if (GET_CODE (index) == REG)
1777         {
1778           /* reg,foo */
1779           print_operand_address (file, XEXP (addr, 1));
1780           fprintf (file, ",");
1781           switch (size)
1782             {
1783             case 0:
1784               print_operand_address (file, index);
1785               break;
1786
1787             case 1:
1788               print_operand (file, index, 'X');
1789               fputs (".b", file);
1790               break;
1791
1792             case 2:
1793               print_operand (file, index, 'T');
1794               fputs (".w", file);
1795               break;
1796
1797             case 4:
1798               print_operand (file, index, 'S');
1799               fputs (".l", file);
1800               break;
1801             }
1802           /* print_operand_address (file, XEXP (addr, 0)); */
1803         }
1804       else
1805         {
1806           /* foo+k */
1807           print_operand_address (file, XEXP (addr, 0));
1808           fprintf (file, "+");
1809           print_operand_address (file, XEXP (addr, 1));
1810         }
1811       fprintf (file, ")");
1812       break;
1813
1814     case CONST_INT:
1815       {
1816         /* Since the H8/300 only has 16-bit pointers, negative values are also
1817            those >= 32768.  This happens for example with pointer minus a
1818            constant.  We don't want to turn (char *p - 2) into
1819            (char *p + 65534) because loop unrolling can build upon this
1820            (IE: char *p + 131068).  */
1821         int n = INTVAL (addr);
1822         if (TARGET_H8300)
1823           n = (int) (short) n;
1824         fprintf (file, "%d", n);
1825         break;
1826       }
1827
1828     default:
1829       output_addr_const (file, addr);
1830       break;
1831     }
1832 }
1833 \f
1834 /* Output all insn addresses and their sizes into the assembly language
1835    output file.  This is helpful for debugging whether the length attributes
1836    in the md file are correct.  This is not meant to be a user selectable
1837    option.  */
1838
1839 void
1840 final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
1841                     int num_operands ATTRIBUTE_UNUSED)
1842 {
1843   /* This holds the last insn address.  */
1844   static int last_insn_address = 0;
1845
1846   const int uid = INSN_UID (insn);
1847
1848   if (TARGET_ADDRESSES)
1849     {
1850       fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1851                INSN_ADDRESSES (uid) - last_insn_address);
1852       last_insn_address = INSN_ADDRESSES (uid);
1853     }
1854 }
1855
1856 /* Prepare for an SI sized move.  */
1857
1858 int
1859 h8300_expand_movsi (rtx operands[])
1860 {
1861   rtx src = operands[1];
1862   rtx dst = operands[0];
1863   if (!reload_in_progress && !reload_completed)
1864     {
1865       if (!register_operand (dst, GET_MODE (dst)))
1866         {
1867           rtx tmp = gen_reg_rtx (GET_MODE (dst));
1868           emit_move_insn (tmp, src);
1869           operands[1] = tmp;
1870         }
1871     }
1872   return 0;
1873 }
1874
1875 /* Given FROM and TO register numbers, say whether this elimination is allowed.
1876    Frame pointer elimination is automatically handled.
1877
1878    For the h8300, if frame pointer elimination is being done, we would like to
1879    convert ap and rp into sp, not fp.
1880
1881    All other eliminations are valid.  */
1882
1883 static bool
1884 h8300_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1885 {
1886   return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
1887 }
1888
1889 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1890    Define the offset between two registers, one to be eliminated, and
1891    the other its replacement, at the start of a routine.  */
1892
1893 int
1894 h8300_initial_elimination_offset (int from, int to)
1895 {
1896   /* The number of bytes that the return address takes on the stack.  */
1897   int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1898
1899   /* The number of bytes that the saved frame pointer takes on the stack.  */
1900   int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1901
1902   /* The number of bytes that the saved registers, excluding the frame
1903      pointer, take on the stack.  */
1904   int saved_regs_size = 0;
1905
1906   /* The number of bytes that the locals takes on the stack.  */
1907   int frame_size = round_frame_size (get_frame_size ());
1908
1909   int regno;
1910
1911   for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1912     if (WORD_REG_USED (regno))
1913       saved_regs_size += UNITS_PER_WORD;
1914
1915   /* Adjust saved_regs_size because the above loop took the frame
1916      pointer int account.  */
1917   saved_regs_size -= fp_size;
1918
1919   switch (to)
1920     {
1921     case HARD_FRAME_POINTER_REGNUM:
1922       switch (from)
1923         {
1924         case ARG_POINTER_REGNUM:
1925           return pc_size + fp_size;
1926         case RETURN_ADDRESS_POINTER_REGNUM:
1927           return fp_size;
1928         case FRAME_POINTER_REGNUM:
1929           return -saved_regs_size;
1930         default:
1931           gcc_unreachable ();
1932         }
1933       break;
1934     case STACK_POINTER_REGNUM:
1935       switch (from)
1936         {
1937         case ARG_POINTER_REGNUM:
1938           return pc_size + saved_regs_size + frame_size;
1939         case RETURN_ADDRESS_POINTER_REGNUM:
1940           return saved_regs_size + frame_size;
1941         case FRAME_POINTER_REGNUM:
1942           return frame_size;
1943         default:
1944           gcc_unreachable ();
1945         }
1946       break;
1947     default:
1948       gcc_unreachable ();
1949     }
1950   gcc_unreachable ();
1951 }
1952
1953 /* Worker function for RETURN_ADDR_RTX.  */
1954
1955 rtx
1956 h8300_return_addr_rtx (int count, rtx frame)
1957 {
1958   rtx ret;
1959
1960   if (count == 0)
1961     ret = gen_rtx_MEM (Pmode,
1962                        gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
1963   else if (flag_omit_frame_pointer)
1964     return (rtx) 0;
1965   else
1966     ret = gen_rtx_MEM (Pmode,
1967                        memory_address (Pmode,
1968                                        plus_constant (frame, UNITS_PER_WORD)));
1969   set_mem_alias_set (ret, get_frame_alias_set ());
1970   return ret;
1971 }
1972
1973 /* Update the condition code from the insn.  */
1974
1975 void
1976 notice_update_cc (rtx body, rtx insn)
1977 {
1978   rtx set;
1979
1980   switch (get_attr_cc (insn))
1981     {
1982     case CC_NONE:
1983       /* Insn does not affect CC at all.  */
1984       break;
1985
1986     case CC_NONE_0HIT:
1987       /* Insn does not change CC, but the 0'th operand has been changed.  */
1988       if (cc_status.value1 != 0
1989           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1990         cc_status.value1 = 0;
1991       if (cc_status.value2 != 0
1992           && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
1993         cc_status.value2 = 0;
1994       break;
1995
1996     case CC_SET_ZN:
1997       /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1998          The V flag is unusable.  The C flag may or may not be known but
1999          that's ok because alter_cond will change tests to use EQ/NE.  */
2000       CC_STATUS_INIT;
2001       cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2002       set = single_set (insn);
2003       cc_status.value1 = SET_SRC (set);
2004       if (SET_DEST (set) != cc0_rtx)
2005         cc_status.value2 = SET_DEST (set);
2006       break;
2007
2008     case CC_SET_ZNV:
2009       /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2010          The C flag may or may not be known but that's ok because
2011          alter_cond will change tests to use EQ/NE.  */
2012       CC_STATUS_INIT;
2013       cc_status.flags |= CC_NO_CARRY;
2014       set = single_set (insn);
2015       cc_status.value1 = SET_SRC (set);
2016       if (SET_DEST (set) != cc0_rtx)
2017         {
2018           /* If the destination is STRICT_LOW_PART, strip off
2019              STRICT_LOW_PART.  */
2020           if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
2021             cc_status.value2 = XEXP (SET_DEST (set), 0);
2022           else
2023             cc_status.value2 = SET_DEST (set);
2024         }
2025       break;
2026
2027     case CC_COMPARE:
2028       /* The insn is a compare instruction.  */
2029       CC_STATUS_INIT;
2030       cc_status.value1 = SET_SRC (body);
2031       break;
2032
2033     case CC_CLOBBER:
2034       /* Insn doesn't leave CC in a usable state.  */
2035       CC_STATUS_INIT;
2036       break;
2037     }
2038 }
2039 \f
2040 /* Given that X occurs in an address of the form (plus X constant),
2041    return the part of X that is expected to be a register.  There are
2042    four kinds of addressing mode to recognize:
2043
2044         @(dd,Rn)
2045         @(dd,RnL.b)
2046         @(dd,Rn.w)
2047         @(dd,ERn.l)
2048
2049    If SIZE is nonnull, and the address is one of the last three forms,
2050    set *SIZE to the index multiplication factor.  Set it to 0 for
2051    plain @(dd,Rn) addresses.
2052
2053    MODE is the mode of the value being accessed.  It can be VOIDmode
2054    if the address is known to be valid, but its mode is unknown.  */
2055
2056 rtx
2057 h8300_get_index (rtx x, enum machine_mode mode, int *size)
2058 {
2059   int dummy, factor;
2060
2061   if (size == 0)
2062     size = &dummy;
2063
2064   factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
2065   if (TARGET_H8300SX
2066       && factor <= 4
2067       && (mode == VOIDmode
2068           || GET_MODE_CLASS (mode) == MODE_INT
2069           || GET_MODE_CLASS (mode) == MODE_FLOAT))
2070     {
2071       if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
2072         {
2073           /* When accessing byte-sized values, the index can be
2074              a zero-extended QImode or HImode register.  */
2075           *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
2076           return XEXP (x, 0);
2077         }
2078       else
2079         {
2080           /* We're looking for addresses of the form:
2081
2082                  (mult X I)
2083               or (mult (zero_extend X) I)
2084
2085              where I is the size of the operand being accessed.
2086              The canonical form of the second expression is:
2087
2088                  (and (mult (subreg X) I) J)
2089
2090              where J == GET_MODE_MASK (GET_MODE (X)) * I.  */
2091           rtx index;
2092
2093           if (GET_CODE (x) == AND
2094               && GET_CODE (XEXP (x, 1)) == CONST_INT
2095               && (factor == 0
2096                   || INTVAL (XEXP (x, 1)) == 0xff * factor
2097                   || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2098             {
2099               index = XEXP (x, 0);
2100               *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2101             }
2102           else
2103             {
2104               index = x;
2105               *size = 4;
2106             }
2107
2108           if (GET_CODE (index) == MULT
2109               && GET_CODE (XEXP (index, 1)) == CONST_INT
2110               && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2111             return XEXP (index, 0);
2112         }
2113     }
2114   *size = 0;
2115   return x;
2116 }
2117 \f
2118 static const h8300_length_table addb_length_table =
2119 {
2120   /* #xx  Rs   @aa  @Rs  @xx  */
2121   {  2,   2,   4,   4,   4  }, /* add.b xx,Rd  */
2122   {  4,   4,   4,   4,   6  }, /* add.b xx,@aa */
2123   {  4,   4,   4,   4,   6  }, /* add.b xx,@Rd */
2124   {  6,   4,   4,   4,   6  }  /* add.b xx,@xx */
2125 };
2126
2127 static const h8300_length_table addw_length_table =
2128 {
2129   /* #xx  Rs   @aa  @Rs  @xx  */
2130   {  2,   2,   4,   4,   4  }, /* add.w xx,Rd  */
2131   {  4,   4,   4,   4,   6  }, /* add.w xx,@aa */
2132   {  4,   4,   4,   4,   6  }, /* add.w xx,@Rd */
2133   {  4,   4,   4,   4,   6  }  /* add.w xx,@xx */
2134 };
2135
2136 static const h8300_length_table addl_length_table =
2137 {
2138   /* #xx  Rs   @aa  @Rs  @xx  */
2139   {  2,   2,   4,   4,   4  }, /* add.l xx,Rd  */
2140   {  4,   4,   6,   6,   6  }, /* add.l xx,@aa */
2141   {  4,   4,   6,   6,   6  }, /* add.l xx,@Rd */
2142   {  4,   4,   6,   6,   6  }  /* add.l xx,@xx */
2143 };
2144
2145 #define logicb_length_table addb_length_table
2146 #define logicw_length_table addw_length_table
2147
2148 static const h8300_length_table logicl_length_table =
2149 {
2150   /* #xx  Rs   @aa  @Rs  @xx  */
2151   {  2,   4,   4,   4,   4  }, /* and.l xx,Rd  */
2152   {  4,   4,   6,   6,   6  }, /* and.l xx,@aa */
2153   {  4,   4,   6,   6,   6  }, /* and.l xx,@Rd */
2154   {  4,   4,   6,   6,   6  }  /* and.l xx,@xx */
2155 };
2156
2157 static const h8300_length_table movb_length_table =
2158 {
2159   /* #xx  Rs   @aa  @Rs  @xx  */
2160   {  2,   2,   2,   2,   4  }, /* mov.b xx,Rd  */
2161   {  4,   2,   4,   4,   4  }, /* mov.b xx,@aa */
2162   {  4,   2,   4,   4,   4  }, /* mov.b xx,@Rd */
2163   {  4,   4,   4,   4,   4  }  /* mov.b xx,@xx */
2164 };
2165
2166 #define movw_length_table movb_length_table
2167
2168 static const h8300_length_table movl_length_table =
2169 {
2170   /* #xx  Rs   @aa  @Rs  @xx  */
2171   {  2,   2,   4,   4,   4  }, /* mov.l xx,Rd  */
2172   {  4,   4,   4,   4,   4  }, /* mov.l xx,@aa */
2173   {  4,   4,   4,   4,   4  }, /* mov.l xx,@Rd */
2174   {  4,   4,   4,   4,   4  }  /* mov.l xx,@xx */
2175 };
2176
2177 /* Return the size of the given address or displacement constant.  */
2178
2179 static unsigned int
2180 h8300_constant_length (rtx constant)
2181 {
2182   /* Check for (@d:16,Reg).  */
2183   if (GET_CODE (constant) == CONST_INT
2184       && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2185     return 2;
2186
2187   /* Check for (@d:16,Reg) in cases where the displacement is
2188      an absolute address.  */
2189   if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2190     return 2;
2191
2192   return 4;
2193 }
2194
2195 /* Return the size of a displacement field in address ADDR, which should
2196    have the form (plus X constant).  SIZE is the number of bytes being
2197    accessed.  */
2198
2199 static unsigned int
2200 h8300_displacement_length (rtx addr, int size)
2201 {
2202   rtx offset;
2203
2204   offset = XEXP (addr, 1);
2205
2206   /* Check for @(d:2,Reg).  */
2207   if (register_operand (XEXP (addr, 0), VOIDmode)
2208       && GET_CODE (offset) == CONST_INT
2209       && (INTVAL (offset) == size
2210           || INTVAL (offset) == size * 2
2211           || INTVAL (offset) == size * 3))
2212     return 0;
2213
2214   return h8300_constant_length (offset);
2215 }
2216
2217 /* Store the class of operand OP in *OPCLASS and return the length of any
2218    extra operand fields.  SIZE is the number of bytes in OP.  OPCLASS
2219    can be null if only the length is needed.  */
2220
2221 static unsigned int
2222 h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
2223 {
2224   enum h8300_operand_class dummy;
2225
2226   if (opclass == 0)
2227     opclass = &dummy;
2228
2229   if (CONSTANT_P (op))
2230     {
2231       *opclass = H8OP_IMMEDIATE;
2232
2233       /* Byte-sized immediates are stored in the opcode fields.  */
2234       if (size == 1)
2235         return 0;
2236
2237       /* If this is a 32-bit instruction, see whether the constant
2238          will fit into a 16-bit immediate field.  */
2239       if (TARGET_H8300SX
2240           && size == 4
2241           && GET_CODE (op) == CONST_INT
2242           && IN_RANGE (INTVAL (op), 0, 0xffff))
2243         return 2;
2244
2245       return size;
2246     }
2247   else if (GET_CODE (op) == MEM)
2248     {
2249       op = XEXP (op, 0);
2250       if (CONSTANT_P (op))
2251         {
2252           *opclass = H8OP_MEM_ABSOLUTE;
2253           return h8300_constant_length (op);
2254         }
2255       else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2256         {
2257           *opclass = H8OP_MEM_COMPLEX;
2258           return h8300_displacement_length (op, size);
2259         }
2260       else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2261         {
2262           *opclass = H8OP_MEM_COMPLEX;
2263           return 0;
2264         }
2265       else if (register_operand (op, VOIDmode))
2266         {
2267           *opclass = H8OP_MEM_BASE;
2268           return 0;
2269         }
2270     }
2271   gcc_assert (register_operand (op, VOIDmode));
2272   *opclass = H8OP_REGISTER;
2273   return 0;
2274 }
2275
2276 /* Return the length of the instruction described by TABLE given that
2277    its operands are OP1 and OP2.  OP1 must be an h8300_dst_operand
2278    and OP2 must be an h8300_src_operand.  */
2279
2280 static unsigned int
2281 h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2282 {
2283   enum h8300_operand_class op1_class, op2_class;
2284   unsigned int size, immediate_length;
2285
2286   size = GET_MODE_SIZE (GET_MODE (op1));
2287   immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2288                       + h8300_classify_operand (op2, size, &op2_class));
2289   return immediate_length + (*table)[op1_class - 1][op2_class];
2290 }
2291
2292 /* Return the length of a unary instruction such as neg or not given that
2293    its operand is OP.  */
2294
2295 unsigned int
2296 h8300_unary_length (rtx op)
2297 {
2298   enum h8300_operand_class opclass;
2299   unsigned int size, operand_length;
2300
2301   size = GET_MODE_SIZE (GET_MODE (op));
2302   operand_length = h8300_classify_operand (op, size, &opclass);
2303   switch (opclass)
2304     {
2305     case H8OP_REGISTER:
2306       return 2;
2307
2308     case H8OP_MEM_BASE:
2309       return (size == 4 ? 6 : 4);
2310
2311     case H8OP_MEM_ABSOLUTE:
2312       return operand_length + (size == 4 ? 6 : 4);
2313
2314     case H8OP_MEM_COMPLEX:
2315       return operand_length + 6;
2316
2317     default:
2318       gcc_unreachable ();
2319     }
2320 }
2321
2322 /* Likewise short immediate instructions such as add.w #xx:3,OP.  */
2323
2324 static unsigned int
2325 h8300_short_immediate_length (rtx op)
2326 {
2327   enum h8300_operand_class opclass;
2328   unsigned int size, operand_length;
2329
2330   size = GET_MODE_SIZE (GET_MODE (op));
2331   operand_length = h8300_classify_operand (op, size, &opclass);
2332
2333   switch (opclass)
2334     {
2335     case H8OP_REGISTER:
2336       return 2;
2337
2338     case H8OP_MEM_BASE:
2339     case H8OP_MEM_ABSOLUTE:
2340     case H8OP_MEM_COMPLEX:
2341       return 4 + operand_length;
2342
2343     default:
2344       gcc_unreachable ();
2345     }
2346 }
2347
2348 /* Likewise bitfield load and store instructions.  */
2349
2350 static unsigned int
2351 h8300_bitfield_length (rtx op, rtx op2)
2352 {
2353   enum h8300_operand_class opclass;
2354   unsigned int size, operand_length;
2355
2356   if (GET_CODE (op) == REG)
2357     op = op2;
2358   gcc_assert (GET_CODE (op) != REG);
2359   
2360   size = GET_MODE_SIZE (GET_MODE (op));
2361   operand_length = h8300_classify_operand (op, size, &opclass);
2362
2363   switch (opclass)
2364     {
2365     case H8OP_MEM_BASE:
2366     case H8OP_MEM_ABSOLUTE:
2367     case H8OP_MEM_COMPLEX:
2368       return 4 + operand_length;
2369
2370     default:
2371       gcc_unreachable ();
2372     }
2373 }
2374
2375 /* Calculate the length of general binary instruction INSN using TABLE.  */
2376
2377 static unsigned int
2378 h8300_binary_length (rtx insn, const h8300_length_table *table)
2379 {
2380   rtx set;
2381
2382   set = single_set (insn);
2383   gcc_assert (set);
2384
2385   if (BINARY_P (SET_SRC (set)))
2386     return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2387                                     XEXP (SET_SRC (set), 1), table);
2388   else
2389     {
2390       gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2391       return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2392                                       XEXP (XEXP (SET_SRC (set), 1), 1),
2393                                       table);
2394     }
2395 }
2396
2397 /* Subroutine of h8300_move_length.  Return true if OP is 1- or 2-byte
2398    memory reference and either (1) it has the form @(d:16,Rn) or
2399    (2) its address has the code given by INC_CODE.  */
2400
2401 static bool
2402 h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
2403 {
2404   rtx addr;
2405   unsigned int size;
2406
2407   if (GET_CODE (op) != MEM)
2408     return false;
2409
2410   addr = XEXP (op, 0);
2411   size = GET_MODE_SIZE (GET_MODE (op));
2412   if (size != 1 && size != 2)
2413     return false;
2414
2415   return (GET_CODE (addr) == inc_code
2416           || (GET_CODE (addr) == PLUS
2417               && GET_CODE (XEXP (addr, 0)) == REG
2418               && h8300_displacement_length (addr, size) == 2));
2419 }
2420
2421 /* Calculate the length of move instruction INSN using the given length
2422    table.  Although the tables are correct for most cases, there is some
2423    irregularity in the length of mov.b and mov.w.  The following forms:
2424
2425         mov @ERs+, Rd
2426         mov @(d:16,ERs), Rd
2427         mov Rs, @-ERd
2428         mov Rs, @(d:16,ERd)
2429
2430    are two bytes shorter than most other "mov Rs, @complex" or
2431    "mov @complex,Rd" combinations.  */
2432
2433 static unsigned int
2434 h8300_move_length (rtx *operands, const h8300_length_table *table)
2435 {
2436   unsigned int size;
2437
2438   size = h8300_length_from_table (operands[0], operands[1], table);
2439   if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2440     size -= 2;
2441   if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2442     size -= 2;
2443   return size;
2444 }
2445
2446 /* Return the length of a mova instruction with the given operands.
2447    DEST is the register destination, SRC is the source address and
2448    OFFSET is the 16-bit or 32-bit displacement.  */
2449
2450 static unsigned int
2451 h8300_mova_length (rtx dest, rtx src, rtx offset)
2452 {
2453   unsigned int size;
2454
2455   size = (2
2456           + h8300_constant_length (offset)
2457           + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2458   if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2459     size += 2;
2460   return size;
2461 }
2462
2463 /* Compute the length of INSN based on its length_table attribute.
2464    OPERANDS is the array of its operands.  */
2465
2466 unsigned int
2467 h8300_insn_length_from_table (rtx insn, rtx * operands)
2468 {
2469   switch (get_attr_length_table (insn))
2470     {
2471     case LENGTH_TABLE_NONE:
2472       gcc_unreachable ();
2473
2474     case LENGTH_TABLE_ADDB:
2475       return h8300_binary_length (insn, &addb_length_table);
2476
2477     case LENGTH_TABLE_ADDW:
2478       return h8300_binary_length (insn, &addw_length_table);
2479
2480     case LENGTH_TABLE_ADDL:
2481       return h8300_binary_length (insn, &addl_length_table);
2482
2483     case LENGTH_TABLE_LOGICB:
2484       return h8300_binary_length (insn, &logicb_length_table);
2485
2486     case LENGTH_TABLE_MOVB:
2487       return h8300_move_length (operands, &movb_length_table);
2488
2489     case LENGTH_TABLE_MOVW:
2490       return h8300_move_length (operands, &movw_length_table);
2491
2492     case LENGTH_TABLE_MOVL:
2493       return h8300_move_length (operands, &movl_length_table);
2494
2495     case LENGTH_TABLE_MOVA:
2496       return h8300_mova_length (operands[0], operands[1], operands[2]);
2497
2498     case LENGTH_TABLE_MOVA_ZERO:
2499       return h8300_mova_length (operands[0], operands[1], const0_rtx);
2500
2501     case LENGTH_TABLE_UNARY:
2502       return h8300_unary_length (operands[0]);
2503
2504     case LENGTH_TABLE_MOV_IMM4:
2505       return 2 + h8300_classify_operand (operands[0], 0, 0);
2506
2507     case LENGTH_TABLE_SHORT_IMMEDIATE:
2508       return h8300_short_immediate_length (operands[0]);
2509
2510     case LENGTH_TABLE_BITFIELD:
2511       return h8300_bitfield_length (operands[0], operands[1]);
2512       
2513     case LENGTH_TABLE_BITBRANCH:
2514       return h8300_bitfield_length (operands[1], operands[2]) - 2;
2515
2516     default:
2517       gcc_unreachable ();
2518     }
2519 }
2520
2521 /* Return true if LHS and RHS are memory references that can be mapped
2522    to the same h8sx assembly operand.  LHS appears as the destination of
2523    an instruction and RHS appears as a source.
2524
2525    Three cases are allowed:
2526
2527         - RHS is @+Rn or @-Rn, LHS is @Rn
2528         - RHS is @Rn, LHS is @Rn+ or @Rn-
2529         - RHS and LHS have the same address and neither has side effects.  */
2530
2531 bool
2532 h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
2533 {
2534   if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2535     {
2536       rhs = XEXP (rhs, 0);
2537       lhs = XEXP (lhs, 0);
2538
2539       if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2540         return rtx_equal_p (XEXP (rhs, 0), lhs);
2541
2542       if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2543         return rtx_equal_p (rhs, XEXP (lhs, 0));
2544
2545       if (rtx_equal_p (rhs, lhs))
2546         return true;
2547     }
2548   return false;
2549 }
2550
2551 /* Return true if OPERANDS[1] can be mapped to the same assembly
2552    operand as OPERANDS[0].  */
2553
2554 bool
2555 h8300_operands_match_p (rtx *operands)
2556 {
2557   if (register_operand (operands[0], VOIDmode)
2558       && register_operand (operands[1], VOIDmode))
2559     return true;
2560
2561   if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2562     return true;
2563
2564   return false;
2565 }
2566 \f
2567 /* Try using movmd to move LENGTH bytes from memory region SRC to memory
2568    region DEST.  The two regions do not overlap and have the common
2569    alignment given by ALIGNMENT.  Return true on success.
2570
2571    Using movmd for variable-length moves seems to involve some
2572    complex trade-offs.  For instance:
2573
2574       - Preparing for a movmd instruction is similar to preparing
2575         for a memcpy.  The main difference is that the arguments
2576         are moved into er4, er5 and er6 rather than er0, er1 and er2.
2577
2578       - Since movmd clobbers the frame pointer, we need to save
2579         and restore it somehow when frame_pointer_needed.  This can
2580         sometimes make movmd sequences longer than calls to memcpy().
2581
2582       - The counter register is 16 bits, so the instruction is only
2583         suitable for variable-length moves when sizeof (size_t) == 2.
2584         That's only true in normal mode.
2585
2586       - We will often lack static alignment information.  Falling back
2587         on movmd.b would likely be slower than calling memcpy(), at least
2588         for big moves.
2589
2590    This function therefore only uses movmd when the length is a
2591    known constant, and only then if -fomit-frame-pointer is in
2592    effect or if we're not optimizing for size.
2593
2594    At the moment the function uses movmd for all in-range constants,
2595    but it might be better to fall back on memcpy() for large moves
2596    if ALIGNMENT == 1.  */
2597
2598 bool
2599 h8sx_emit_movmd (rtx dest, rtx src, rtx length,
2600                  HOST_WIDE_INT alignment)
2601 {
2602   if (!flag_omit_frame_pointer && optimize_size)
2603     return false;
2604
2605   if (GET_CODE (length) == CONST_INT)
2606     {
2607       rtx dest_reg, src_reg, first_dest, first_src;
2608       HOST_WIDE_INT n;
2609       int factor;
2610
2611       /* Use movmd.l if the alignment allows it, otherwise fall back
2612          on movmd.b.  */
2613       factor = (alignment >= 2 ? 4 : 1);
2614
2615       /* Make sure the length is within range.  We can handle counter
2616          values up to 65536, although HImode truncation will make
2617          the count appear negative in rtl dumps.  */
2618       n = INTVAL (length);
2619       if (n <= 0 || n / factor > 65536)
2620         return false;
2621
2622       /* Create temporary registers for the source and destination
2623          pointers.  Initialize them to the start of each region.  */
2624       dest_reg = copy_addr_to_reg (XEXP (dest, 0));
2625       src_reg = copy_addr_to_reg (XEXP (src, 0));
2626
2627       /* Create references to the movmd source and destination blocks.  */
2628       first_dest = replace_equiv_address (dest, dest_reg);
2629       first_src = replace_equiv_address (src, src_reg);
2630
2631       set_mem_size (first_dest, GEN_INT (n & -factor));
2632       set_mem_size (first_src, GEN_INT (n & -factor));
2633
2634       length = copy_to_mode_reg (HImode, gen_int_mode (n / factor, HImode));
2635       emit_insn (gen_movmd (first_dest, first_src, length, GEN_INT (factor)));
2636
2637       if ((n & -factor) != n)
2638         {
2639           /* Move SRC and DEST past the region we just copied.
2640              This is done to update the memory attributes.  */
2641           dest = adjust_address (dest, BLKmode, n & -factor);
2642           src = adjust_address (src, BLKmode, n & -factor);
2643
2644           /* Replace the addresses with the source and destination
2645              registers, which movmd has left with the right values.  */
2646           dest = replace_equiv_address (dest, dest_reg);
2647           src = replace_equiv_address (src, src_reg);
2648
2649           /* Mop up the left-over bytes.  */
2650           if (n & 2)
2651             emit_move_insn (adjust_address (dest, HImode, 0),
2652                             adjust_address (src, HImode, 0));
2653           if (n & 1)
2654             emit_move_insn (adjust_address (dest, QImode, n & 2),
2655                             adjust_address (src, QImode, n & 2));
2656         }
2657       return true;
2658     }
2659   return false;
2660 }
2661
2662 /* Move ADDR into er6 after pushing its old value onto the stack.  */
2663
2664 void
2665 h8300_swap_into_er6 (rtx addr)
2666 {
2667   push (HARD_FRAME_POINTER_REGNUM);
2668   emit_move_insn (hard_frame_pointer_rtx, addr);
2669   if (REGNO (addr) == SP_REG)
2670     emit_move_insn (hard_frame_pointer_rtx,
2671                     plus_constant (hard_frame_pointer_rtx,
2672                                    GET_MODE_SIZE (word_mode)));
2673 }
2674
2675 /* Move the current value of er6 into ADDR and pop its old value
2676    from the stack.  */
2677
2678 void
2679 h8300_swap_out_of_er6 (rtx addr)
2680 {
2681   if (REGNO (addr) != SP_REG)
2682     emit_move_insn (addr, hard_frame_pointer_rtx);
2683   pop (HARD_FRAME_POINTER_REGNUM);
2684 }
2685 \f
2686 /* Return the length of mov instruction.  */
2687
2688 unsigned int
2689 compute_mov_length (rtx *operands)
2690 {
2691   /* If the mov instruction involves a memory operand, we compute the
2692      length, assuming the largest addressing mode is used, and then
2693      adjust later in the function.  Otherwise, we compute and return
2694      the exact length in one step.  */
2695   enum machine_mode mode = GET_MODE (operands[0]);
2696   rtx dest = operands[0];
2697   rtx src = operands[1];
2698   rtx addr;
2699
2700   if (GET_CODE (src) == MEM)
2701     addr = XEXP (src, 0);
2702   else if (GET_CODE (dest) == MEM)
2703     addr = XEXP (dest, 0);
2704   else
2705     addr = NULL_RTX;
2706
2707   if (TARGET_H8300)
2708     {
2709       unsigned int base_length;
2710
2711       switch (mode)
2712         {
2713         case QImode:
2714           if (addr == NULL_RTX)
2715             return 2;
2716
2717           /* The eightbit addressing is available only in QImode, so
2718              go ahead and take care of it.  */
2719           if (h8300_eightbit_constant_address_p (addr))
2720             return 2;
2721
2722           base_length = 4;
2723           break;
2724
2725         case HImode:
2726           if (addr == NULL_RTX)
2727             {
2728               if (REG_P (src))
2729                 return 2;
2730
2731               if (src == const0_rtx)
2732                 return 2;
2733
2734               return 4;
2735             }
2736
2737           base_length = 4;
2738           break;
2739
2740         case SImode:
2741           if (addr == NULL_RTX)
2742             {
2743               if (REG_P (src))
2744                 return 4;
2745
2746               if (GET_CODE (src) == CONST_INT)
2747                 {
2748                   if (src == const0_rtx)
2749                     return 4;
2750
2751                   if ((INTVAL (src) & 0xffff) == 0)
2752                     return 6;
2753
2754                   if ((INTVAL (src) & 0xffff) == 0)
2755                     return 6;
2756
2757                   if ((INTVAL (src) & 0xffff)
2758                       == ((INTVAL (src) >> 16) & 0xffff))
2759                     return 6;
2760                 }
2761               return 8;
2762             }
2763
2764           base_length = 8;
2765           break;
2766
2767         case SFmode:
2768           if (addr == NULL_RTX)
2769             {
2770               if (REG_P (src))
2771                 return 4;
2772
2773               if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2774                 return 4;
2775
2776               return 8;
2777             }
2778
2779           base_length = 8;
2780           break;
2781
2782         default:
2783           gcc_unreachable ();
2784         }
2785
2786       /* Adjust the length based on the addressing mode used.
2787          Specifically, we subtract the difference between the actual
2788          length and the longest one, which is @(d:16,Rs).  For SImode
2789          and SFmode, we double the adjustment because two mov.w are
2790          used to do the job.  */
2791
2792       /* @Rs+ and @-Rd are 2 bytes shorter than the longest.  */
2793       if (GET_CODE (addr) == PRE_DEC
2794           || GET_CODE (addr) == POST_INC)
2795         {
2796           if (mode == QImode || mode == HImode)
2797             return base_length - 2;
2798           else
2799             /* In SImode and SFmode, we use two mov.w instructions, so
2800                double the adjustment.  */
2801             return base_length - 4;
2802         }
2803
2804       /* @Rs and @Rd are 2 bytes shorter than the longest.  Note that
2805          in SImode and SFmode, the second mov.w involves an address
2806          with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
2807          only 2 bytes.  */
2808       if (GET_CODE (addr) == REG)
2809         return base_length - 2;
2810
2811       return base_length;
2812     }
2813   else
2814     {
2815       unsigned int base_length;
2816
2817       switch (mode)
2818         {
2819         case QImode:
2820           if (addr == NULL_RTX)
2821             return 2;
2822
2823           /* The eightbit addressing is available only in QImode, so
2824              go ahead and take care of it.  */
2825           if (h8300_eightbit_constant_address_p (addr))
2826             return 2;
2827
2828           base_length = 8;
2829           break;
2830
2831         case HImode:
2832           if (addr == NULL_RTX)
2833             {
2834               if (REG_P (src))
2835                 return 2;
2836
2837               if (src == const0_rtx)
2838                 return 2;
2839
2840               return 4;
2841             }
2842
2843           base_length = 8;
2844           break;
2845
2846         case SImode:
2847           if (addr == NULL_RTX)
2848             {
2849               if (REG_P (src))
2850                 {
2851                   if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2852                     return 4;
2853                   else
2854                     return 2;
2855                 }
2856
2857               if (GET_CODE (src) == CONST_INT)
2858                 {
2859                   int val = INTVAL (src);
2860
2861                   if (val == 0)
2862                     return 2;
2863
2864                   if (val == (val & 0x00ff) || val == (val & 0xff00))
2865                     return 4;
2866
2867                   switch (val & 0xffffffff)
2868                     {
2869                     case 0xffffffff:
2870                     case 0xfffffffe:
2871                     case 0xfffffffc:
2872                     case 0x0000ffff:
2873                     case 0x0000fffe:
2874                     case 0xffff0000:
2875                     case 0xfffe0000:
2876                     case 0x00010000:
2877                     case 0x00020000:
2878                       return 4;
2879                     }
2880                 }
2881               return 6;
2882             }
2883
2884           base_length = 10;
2885           break;
2886
2887         case SFmode:
2888           if (addr == NULL_RTX)
2889             {
2890               if (REG_P (src))
2891                 return 2;
2892
2893               if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2894                 return 2;
2895
2896               return 6;
2897             }
2898
2899           base_length = 10;
2900           break;
2901
2902         default:
2903           gcc_unreachable ();
2904         }
2905
2906       /* Adjust the length based on the addressing mode used.
2907          Specifically, we subtract the difference between the actual
2908          length and the longest one, which is @(d:24,ERs).  */
2909
2910       /* @ERs+ and @-ERd are 6 bytes shorter than the longest.  */
2911       if (GET_CODE (addr) == PRE_DEC
2912           || GET_CODE (addr) == POST_INC)
2913         return base_length - 6;
2914
2915       /* @ERs and @ERd are 6 bytes shorter than the longest.  */
2916       if (GET_CODE (addr) == REG)
2917         return base_length - 6;
2918
2919       /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2920          longest.  */
2921       if (GET_CODE (addr) == PLUS
2922           && GET_CODE (XEXP (addr, 0)) == REG
2923           && GET_CODE (XEXP (addr, 1)) == CONST_INT
2924           && INTVAL (XEXP (addr, 1)) > -32768
2925           && INTVAL (XEXP (addr, 1)) < 32767)
2926         return base_length - 4;
2927
2928       /* @aa:16 is 4 bytes shorter than the longest.  */
2929       if (h8300_tiny_constant_address_p (addr))
2930         return base_length - 4;
2931
2932       /* @aa:24 is 2 bytes shorter than the longest.  */
2933       if (CONSTANT_P (addr))
2934         return base_length - 2;
2935
2936       return base_length;
2937     }
2938 }
2939 \f
2940 /* Output an addition insn.  */
2941
2942 const char *
2943 output_plussi (rtx *operands)
2944 {
2945   enum machine_mode mode = GET_MODE (operands[0]);
2946
2947   gcc_assert (mode == SImode);
2948
2949   if (TARGET_H8300)
2950     {
2951       if (GET_CODE (operands[2]) == REG)
2952         return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2953
2954       if (GET_CODE (operands[2]) == CONST_INT)
2955         {
2956           HOST_WIDE_INT n = INTVAL (operands[2]);
2957
2958           if ((n & 0xffffff) == 0)
2959             return "add\t%z2,%z0";
2960           if ((n & 0xffff) == 0)
2961             return "add\t%y2,%y0\n\taddx\t%z2,%z0";
2962           if ((n & 0xff) == 0)
2963             return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2964         }
2965
2966       return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2967     }
2968   else
2969     {
2970       if (GET_CODE (operands[2]) == CONST_INT
2971           && register_operand (operands[1], VOIDmode))
2972         {
2973           HOST_WIDE_INT intval = INTVAL (operands[2]);
2974
2975           if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2976             return "add.l\t%S2,%S0";
2977           if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2978             return "sub.l\t%G2,%S0";
2979
2980           /* See if we can finish with 2 bytes.  */
2981
2982           switch ((unsigned int) intval & 0xffffffff)
2983             {
2984             case 0x00000001:
2985             case 0x00000002:
2986             case 0x00000004:
2987               return "adds\t%2,%S0";
2988
2989             case 0xffffffff:
2990             case 0xfffffffe:
2991             case 0xfffffffc:
2992               return "subs\t%G2,%S0";
2993
2994             case 0x00010000:
2995             case 0x00020000:
2996               operands[2] = GEN_INT (intval >> 16);
2997               return "inc.w\t%2,%e0";
2998
2999             case 0xffff0000:
3000             case 0xfffe0000:
3001               operands[2] = GEN_INT (intval >> 16);
3002               return "dec.w\t%G2,%e0";
3003             }
3004
3005           /* See if we can finish with 4 bytes.  */
3006           if ((intval & 0xffff) == 0)
3007             {
3008               operands[2] = GEN_INT (intval >> 16);
3009               return "add.w\t%2,%e0";
3010             }
3011         }
3012
3013       if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3014         {
3015           operands[2] = GEN_INT (-INTVAL (operands[2]));
3016           return "sub.l\t%S2,%S0";
3017         }
3018       return "add.l\t%S2,%S0";
3019     }
3020 }
3021
3022 /* ??? It would be much easier to add the h8sx stuff if a single function
3023    classified the addition as either inc/dec, adds/subs, add.w or add.l.  */
3024 /* Compute the length of an addition insn.  */
3025
3026 unsigned int
3027 compute_plussi_length (rtx *operands)
3028 {
3029   enum machine_mode mode = GET_MODE (operands[0]);
3030
3031   gcc_assert (mode == SImode);
3032
3033   if (TARGET_H8300)
3034     {
3035       if (GET_CODE (operands[2]) == REG)
3036         return 6;
3037
3038       if (GET_CODE (operands[2]) == CONST_INT)
3039         {
3040           HOST_WIDE_INT n = INTVAL (operands[2]);
3041
3042           if ((n & 0xffffff) == 0)
3043             return 2;
3044           if ((n & 0xffff) == 0)
3045             return 4;
3046           if ((n & 0xff) == 0)
3047             return 6;
3048         }
3049
3050       return 8;
3051     }
3052   else
3053     {
3054       if (GET_CODE (operands[2]) == CONST_INT
3055           && register_operand (operands[1], VOIDmode))
3056         {
3057           HOST_WIDE_INT intval = INTVAL (operands[2]);
3058
3059           if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3060             return 2;
3061           if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3062             return 2;
3063
3064           /* See if we can finish with 2 bytes.  */
3065
3066           switch ((unsigned int) intval & 0xffffffff)
3067             {
3068             case 0x00000001:
3069             case 0x00000002:
3070             case 0x00000004:
3071               return 2;
3072
3073             case 0xffffffff:
3074             case 0xfffffffe:
3075             case 0xfffffffc:
3076               return 2;
3077
3078             case 0x00010000:
3079             case 0x00020000:
3080               return 2;
3081
3082             case 0xffff0000:
3083             case 0xfffe0000:
3084               return 2;
3085             }
3086
3087           /* See if we can finish with 4 bytes.  */
3088           if ((intval & 0xffff) == 0)
3089             return 4;
3090         }
3091
3092       if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3093         return h8300_length_from_table (operands[0],
3094                                         GEN_INT (-INTVAL (operands[2])),
3095                                         &addl_length_table);
3096       else
3097         return h8300_length_from_table (operands[0], operands[2],
3098                                         &addl_length_table);
3099       return 6;
3100     }
3101 }
3102
3103 /* Compute which flag bits are valid after an addition insn.  */
3104
3105 int
3106 compute_plussi_cc (rtx *operands)
3107 {
3108   enum machine_mode mode = GET_MODE (operands[0]);
3109
3110   gcc_assert (mode == SImode);
3111
3112   if (TARGET_H8300)
3113     {
3114       return CC_CLOBBER;
3115     }
3116   else
3117     {
3118       if (GET_CODE (operands[2]) == CONST_INT
3119           && register_operand (operands[1], VOIDmode))
3120         {
3121           HOST_WIDE_INT intval = INTVAL (operands[2]);
3122
3123           if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3124             return CC_SET_ZN;
3125           if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3126             return CC_SET_ZN;
3127
3128           /* See if we can finish with 2 bytes.  */
3129
3130           switch ((unsigned int) intval & 0xffffffff)
3131             {
3132             case 0x00000001:
3133             case 0x00000002:
3134             case 0x00000004:
3135               return CC_NONE_0HIT;
3136
3137             case 0xffffffff:
3138             case 0xfffffffe:
3139             case 0xfffffffc:
3140               return CC_NONE_0HIT;
3141
3142             case 0x00010000:
3143             case 0x00020000:
3144               return CC_CLOBBER;
3145
3146             case 0xffff0000:
3147             case 0xfffe0000:
3148               return CC_CLOBBER;
3149             }
3150
3151           /* See if we can finish with 4 bytes.  */
3152           if ((intval & 0xffff) == 0)
3153             return CC_CLOBBER;
3154         }
3155
3156       return CC_SET_ZN;
3157     }
3158 }
3159 \f
3160 /* Output a logical insn.  */
3161
3162 const char *
3163 output_logical_op (enum machine_mode mode, rtx *operands)
3164 {
3165   /* Figure out the logical op that we need to perform.  */
3166   enum rtx_code code = GET_CODE (operands[3]);
3167   /* Pretend that every byte is affected if both operands are registers.  */
3168   const unsigned HOST_WIDE_INT intval =
3169     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3170                               /* Always use the full instruction if the
3171                                  first operand is in memory.  It is better
3172                                  to use define_splits to generate the shorter
3173                                  sequence where valid.  */
3174                               && register_operand (operands[1], VOIDmode)
3175                               ? INTVAL (operands[2]) : 0x55555555);
3176   /* The determinant of the algorithm.  If we perform an AND, 0
3177      affects a bit.  Otherwise, 1 affects a bit.  */
3178   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3179   /* Break up DET into pieces.  */
3180   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
3181   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
3182   const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3183   const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3184   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
3185   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3186   int lower_half_easy_p = 0;
3187   int upper_half_easy_p = 0;
3188   /* The name of an insn.  */
3189   const char *opname;
3190   char insn_buf[100];
3191
3192   switch (code)
3193     {
3194     case AND:
3195       opname = "and";
3196       break;
3197     case IOR:
3198       opname = "or";
3199       break;
3200     case XOR:
3201       opname = "xor";
3202       break;
3203     default:
3204       gcc_unreachable ();
3205     }
3206
3207   switch (mode)
3208     {
3209     case HImode:
3210       /* First, see if we can finish with one insn.  */
3211       if ((TARGET_H8300H || TARGET_H8300S)
3212           && b0 != 0
3213           && b1 != 0)
3214         {
3215           sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
3216           output_asm_insn (insn_buf, operands);
3217         }
3218       else
3219         {
3220           /* Take care of the lower byte.  */
3221           if (b0 != 0)
3222             {
3223               sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
3224               output_asm_insn (insn_buf, operands);
3225             }
3226           /* Take care of the upper byte.  */
3227           if (b1 != 0)
3228             {
3229               sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
3230               output_asm_insn (insn_buf, operands);
3231             }
3232         }
3233       break;
3234     case SImode:
3235       if (TARGET_H8300H || TARGET_H8300S)
3236         {
3237           /* Determine if the lower half can be taken care of in no more
3238              than two bytes.  */
3239           lower_half_easy_p = (b0 == 0
3240                                || b1 == 0
3241                                || (code != IOR && w0 == 0xffff));
3242
3243           /* Determine if the upper half can be taken care of in no more
3244              than two bytes.  */
3245           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3246                                || (code == AND && w1 == 0xff00));
3247         }
3248
3249       /* Check if doing everything with one insn is no worse than
3250          using multiple insns.  */
3251       if ((TARGET_H8300H || TARGET_H8300S)
3252           && w0 != 0 && w1 != 0
3253           && !(lower_half_easy_p && upper_half_easy_p)
3254           && !(code == IOR && w1 == 0xffff
3255                && (w0 & 0x8000) != 0 && lower_half_easy_p))
3256         {
3257           sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
3258           output_asm_insn (insn_buf, operands);
3259         }
3260       else
3261         {
3262           /* Take care of the lower and upper words individually.  For
3263              each word, we try different methods in the order of
3264
3265              1) the special insn (in case of AND or XOR),
3266              2) the word-wise insn, and
3267              3) The byte-wise insn.  */
3268           if (w0 == 0xffff
3269               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3270             output_asm_insn ((code == AND)
3271                              ? "sub.w\t%f0,%f0" : "not.w\t%f0",
3272                              operands);
3273           else if ((TARGET_H8300H || TARGET_H8300S)
3274                    && (b0 != 0)
3275                    && (b1 != 0))
3276             {
3277               sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
3278               output_asm_insn (insn_buf, operands);
3279             }
3280           else
3281             {
3282               if (b0 != 0)
3283                 {
3284                   sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
3285                   output_asm_insn (insn_buf, operands);
3286                 }
3287               if (b1 != 0)
3288                 {
3289                   sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3290                   output_asm_insn (insn_buf, operands);
3291                 }
3292             }
3293
3294           if ((w1 == 0xffff)
3295               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3296             output_asm_insn ((code == AND)
3297                              ? "sub.w\t%e0,%e0" : "not.w\t%e0",
3298                              operands);
3299           else if ((TARGET_H8300H || TARGET_H8300S)
3300                    && code == IOR
3301                    && w1 == 0xffff
3302                    && (w0 & 0x8000) != 0)
3303             {
3304               output_asm_insn ("exts.l\t%S0", operands);
3305             }
3306           else if ((TARGET_H8300H || TARGET_H8300S)
3307                    && code == AND
3308                    && w1 == 0xff00)
3309             {
3310               output_asm_insn ("extu.w\t%e0", operands);
3311             }
3312           else if (TARGET_H8300H || TARGET_H8300S)
3313             {
3314               if (w1 != 0)
3315                 {
3316                   sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3317                   output_asm_insn (insn_buf, operands);
3318                 }
3319             }
3320           else
3321             {
3322               if (b2 != 0)
3323                 {
3324                   sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
3325                   output_asm_insn (insn_buf, operands);
3326                 }
3327               if (b3 != 0)
3328                 {
3329                   sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
3330                   output_asm_insn (insn_buf, operands);
3331                 }
3332             }
3333         }
3334       break;
3335     default:
3336       gcc_unreachable ();
3337     }
3338   return "";
3339 }
3340
3341 /* Compute the length of a logical insn.  */
3342
3343 unsigned int
3344 compute_logical_op_length (enum machine_mode mode, rtx *operands)
3345 {
3346   /* Figure out the logical op that we need to perform.  */
3347   enum rtx_code code = GET_CODE (operands[3]);
3348   /* Pretend that every byte is affected if both operands are registers.  */
3349   const unsigned HOST_WIDE_INT intval =
3350     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3351                               /* Always use the full instruction if the
3352                                  first operand is in memory.  It is better
3353                                  to use define_splits to generate the shorter
3354                                  sequence where valid.  */
3355                               && register_operand (operands[1], VOIDmode)
3356                               ? INTVAL (operands[2]) : 0x55555555);
3357   /* The determinant of the algorithm.  If we perform an AND, 0
3358      affects a bit.  Otherwise, 1 affects a bit.  */
3359   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3360   /* Break up DET into pieces.  */
3361   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
3362   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
3363   const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3364   const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3365   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
3366   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3367   int lower_half_easy_p = 0;
3368   int upper_half_easy_p = 0;
3369   /* Insn length.  */
3370   unsigned int length = 0;
3371
3372   switch (mode)
3373     {
3374     case HImode:
3375       /* First, see if we can finish with one insn.  */
3376       if ((TARGET_H8300H || TARGET_H8300S)
3377           && b0 != 0
3378           && b1 != 0)
3379         {
3380           length = h8300_length_from_table (operands[1], operands[2],
3381                                             &logicw_length_table);
3382         }
3383       else
3384         {
3385           /* Take care of the lower byte.  */
3386           if (b0 != 0)
3387             length += 2;
3388
3389           /* Take care of the upper byte.  */
3390           if (b1 != 0)
3391             length += 2;
3392         }
3393       break;
3394     case SImode:
3395       if (TARGET_H8300H || TARGET_H8300S)
3396         {
3397           /* Determine if the lower half can be taken care of in no more
3398              than two bytes.  */
3399           lower_half_easy_p = (b0 == 0
3400                                || b1 == 0
3401                                || (code != IOR && w0 == 0xffff));
3402
3403           /* Determine if the upper half can be taken care of in no more
3404              than two bytes.  */
3405           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3406                                || (code == AND && w1 == 0xff00));
3407         }
3408
3409       /* Check if doing everything with one insn is no worse than
3410          using multiple insns.  */
3411       if ((TARGET_H8300H || TARGET_H8300S)
3412           && w0 != 0 && w1 != 0
3413           && !(lower_half_easy_p && upper_half_easy_p)
3414           && !(code == IOR && w1 == 0xffff
3415                && (w0 & 0x8000) != 0 && lower_half_easy_p))
3416         {
3417           length = h8300_length_from_table (operands[1], operands[2],
3418                                             &logicl_length_table);
3419         }
3420       else
3421         {
3422           /* Take care of the lower and upper words individually.  For
3423              each word, we try different methods in the order of
3424
3425              1) the special insn (in case of AND or XOR),
3426              2) the word-wise insn, and
3427              3) The byte-wise insn.  */
3428           if (w0 == 0xffff
3429               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3430             {
3431               length += 2;
3432             }
3433           else if ((TARGET_H8300H || TARGET_H8300S)
3434                    && (b0 != 0)
3435                    && (b1 != 0))
3436             {
3437               length += 4;
3438             }
3439           else
3440             {
3441               if (b0 != 0)
3442                 length += 2;
3443
3444               if (b1 != 0)
3445                 length += 2;
3446             }
3447
3448           if (w1 == 0xffff
3449               && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3450             {
3451               length += 2;
3452             }
3453           else if ((TARGET_H8300H || TARGET_H8300S)
3454                    && code == IOR
3455                    && w1 == 0xffff
3456                    && (w0 & 0x8000) != 0)
3457             {
3458               length += 2;
3459             }
3460           else if ((TARGET_H8300H || TARGET_H8300S)
3461                    && code == AND
3462                    && w1 == 0xff00)
3463             {
3464               length += 2;
3465             }
3466           else if (TARGET_H8300H || TARGET_H8300S)
3467             {
3468               if (w1 != 0)
3469                 length += 4;
3470             }
3471           else
3472             {
3473               if (b2 != 0)
3474                 length += 2;
3475
3476               if (b3 != 0)
3477                 length += 2;
3478             }
3479         }
3480       break;
3481     default:
3482       gcc_unreachable ();
3483     }
3484   return length;
3485 }
3486
3487 /* Compute which flag bits are valid after a logical insn.  */
3488
3489 int
3490 compute_logical_op_cc (enum machine_mode mode, rtx *operands)
3491 {
3492   /* Figure out the logical op that we need to perform.  */
3493   enum rtx_code code = GET_CODE (operands[3]);
3494   /* Pretend that every byte is affected if both operands are registers.  */
3495   const unsigned HOST_WIDE_INT intval =
3496     (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3497                               /* Always use the full instruction if the
3498                                  first operand is in memory.  It is better
3499                                  to use define_splits to generate the shorter
3500                                  sequence where valid.  */
3501                               && register_operand (operands[1], VOIDmode)
3502                               ? INTVAL (operands[2]) : 0x55555555);
3503   /* The determinant of the algorithm.  If we perform an AND, 0
3504      affects a bit.  Otherwise, 1 affects a bit.  */
3505   const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3506   /* Break up DET into pieces.  */
3507   const unsigned HOST_WIDE_INT b0 = (det >>  0) & 0xff;
3508   const unsigned HOST_WIDE_INT b1 = (det >>  8) & 0xff;
3509   const unsigned HOST_WIDE_INT w0 = (det >>  0) & 0xffff;
3510   const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3511   int lower_half_easy_p = 0;
3512   int upper_half_easy_p = 0;
3513   /* Condition code.  */
3514   enum attr_cc cc = CC_CLOBBER;
3515
3516   switch (mode)
3517     {
3518     case HImode:
3519       /* First, see if we can finish with one insn.  */
3520       if ((TARGET_H8300H || TARGET_H8300S)
3521           && b0 != 0
3522           && b1 != 0)
3523         {
3524           cc = CC_SET_ZNV;
3525         }
3526       break;
3527     case SImode:
3528       if (TARGET_H8300H || TARGET_H8300S)
3529         {
3530           /* Determine if the lower half can be taken care of in no more
3531              than two bytes.  */
3532           lower_half_easy_p = (b0 == 0
3533                                || b1 == 0
3534                                || (code != IOR && w0 == 0xffff));
3535
3536           /* Determine if the upper half can be taken care of in no more
3537              than two bytes.  */
3538           upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3539                                || (code == AND && w1 == 0xff00));
3540         }
3541
3542       /* Check if doing everything with one insn is no worse than
3543          using multiple insns.  */
3544       if ((TARGET_H8300H || TARGET_H8300S)
3545           && w0 != 0 && w1 != 0
3546           && !(lower_half_easy_p && upper_half_easy_p)
3547           && !(code == IOR && w1 == 0xffff
3548                && (w0 & 0x8000) != 0 && lower_half_easy_p))
3549         {
3550           cc = CC_SET_ZNV;
3551         }
3552       else
3553         {
3554           if ((TARGET_H8300H || TARGET_H8300S)
3555               && code == IOR
3556               && w1 == 0xffff
3557               && (w0 & 0x8000) != 0)
3558             {
3559               cc = CC_SET_ZNV;
3560             }
3561         }
3562       break;
3563     default:
3564       gcc_unreachable ();
3565     }
3566   return cc;
3567 }
3568 \f
3569 /* Expand a conditional branch.  */
3570
3571 void
3572 h8300_expand_branch (rtx operands[])
3573 {
3574   enum rtx_code code = GET_CODE (operands[0]);
3575   rtx op0 = operands[1];
3576   rtx op1 = operands[2];
3577   rtx label = operands[3];
3578   rtx tmp;
3579
3580   tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3581   emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3582
3583   tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3584   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3585                               gen_rtx_LABEL_REF (VOIDmode, label),
3586                               pc_rtx);
3587   emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
3588 }
3589
3590
3591 /* Expand a conditional store.  */
3592
3593 void
3594 h8300_expand_store (rtx operands[])
3595 {
3596   rtx dest = operands[0];
3597   enum rtx_code code = GET_CODE (operands[1]);
3598   rtx op0 = operands[2];
3599   rtx op1 = operands[3];
3600   rtx tmp;
3601
3602   tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3603   emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3604
3605   tmp = gen_rtx_fmt_ee (code, GET_MODE (dest), cc0_rtx, const0_rtx);
3606   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
3607 }
3608 \f
3609 /* Shifts.
3610
3611    We devote a fair bit of code to getting efficient shifts since we
3612    can only shift one bit at a time on the H8/300 and H8/300H and only
3613    one or two bits at a time on the H8S.
3614
3615    All shift code falls into one of the following ways of
3616    implementation:
3617
3618    o SHIFT_INLINE: Emit straight line code for the shift; this is used
3619      when a straight line shift is about the same size or smaller than
3620      a loop.
3621
3622    o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3623      off the bits we don't need.  This is used when only a few of the
3624      bits in the original value will survive in the shifted value.
3625
3626    o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3627      simulate a shift by 8, 16, or 24 bits.  Once moved, a few inline
3628      shifts can be added if the shift count is slightly more than 8 or
3629      16.  This case also includes other oddballs that are not worth
3630      explaining here.
3631
3632    o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
3633
3634    For each shift count, we try to use code that has no trade-off
3635    between code size and speed whenever possible.
3636
3637    If the trade-off is unavoidable, we try to be reasonable.
3638    Specifically, the fastest version is one instruction longer than
3639    the shortest version, we take the fastest version.  We also provide
3640    the use a way to switch back to the shortest version with -Os.
3641
3642    For the details of the shift algorithms for various shift counts,
3643    refer to shift_alg_[qhs]i.  */
3644
3645 /* Classify a shift with the given mode and code.  OP is the shift amount.  */
3646
3647 enum h8sx_shift_type
3648 h8sx_classify_shift (enum machine_mode mode, enum rtx_code code, rtx op)
3649 {
3650   if (!TARGET_H8300SX)
3651     return H8SX_SHIFT_NONE;
3652
3653   switch (code)
3654     {
3655     case ASHIFT:
3656     case LSHIFTRT:
3657       /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd).  */
3658       if (GET_CODE (op) != CONST_INT)
3659         return H8SX_SHIFT_BINARY;
3660
3661       /* Reject out-of-range shift amounts.  */
3662       if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3663         return H8SX_SHIFT_NONE;
3664
3665       /* Power-of-2 shifts are effectively unary operations.  */
3666       if (exact_log2 (INTVAL (op)) >= 0)
3667         return H8SX_SHIFT_UNARY;
3668
3669       return H8SX_SHIFT_BINARY;
3670
3671     case ASHIFTRT:
3672       if (op == const1_rtx || op == const2_rtx)
3673         return H8SX_SHIFT_UNARY;
3674       return H8SX_SHIFT_NONE;
3675
3676     case ROTATE:
3677       if (GET_CODE (op) == CONST_INT
3678           && (INTVAL (op) == 1
3679               || INTVAL (op) == 2
3680               || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3681               || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3682         return H8SX_SHIFT_UNARY;
3683       return H8SX_SHIFT_NONE;
3684
3685     default:
3686       return H8SX_SHIFT_NONE;
3687     }
3688 }
3689
3690 /* Return the asm template for a single h8sx shift instruction.
3691    OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3692    is the source and OPERANDS[3] is the shift.  SUFFIX is the
3693    size suffix ('b', 'w' or 'l') and OPTYPE is the print_operand
3694    prefix for the destination operand.  */
3695
3696 const char *
3697 output_h8sx_shift (rtx *operands, int suffix, int optype)
3698 {
3699   static char buffer[16];
3700   const char *stem;
3701
3702   switch (GET_CODE (operands[3]))
3703     {
3704     case ASHIFT:
3705       stem = "shll";
3706       break;
3707
3708     case ASHIFTRT:
3709       stem = "shar";
3710       break;
3711
3712     case LSHIFTRT:
3713       stem = "shlr";
3714       break;
3715
3716     case ROTATE:
3717       stem = "rotl";
3718       if (INTVAL (operands[2]) > 2)
3719         {
3720           /* This is really a right rotate.  */
3721           operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3722                                  - INTVAL (operands[2]));
3723           stem = "rotr";
3724         }
3725       break;
3726
3727     default:
3728       gcc_unreachable ();
3729     }
3730   if (operands[2] == const1_rtx)
3731     sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3732   else
3733     sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3734   return buffer;
3735 }
3736
3737 /* Emit code to do shifts.  */
3738
3739 bool
3740 expand_a_shift (enum machine_mode mode, int code, rtx operands[])
3741 {
3742   switch (h8sx_classify_shift (mode, code, operands[2]))
3743     {
3744     case H8SX_SHIFT_BINARY:
3745       operands[1] = force_reg (mode, operands[1]);
3746       return false;
3747
3748     case H8SX_SHIFT_UNARY:
3749       return false;
3750
3751     case H8SX_SHIFT_NONE:
3752       break;
3753     }
3754
3755   emit_move_insn (copy_rtx (operands[0]), operands[1]);
3756
3757   /* Need a loop to get all the bits we want  - we generate the
3758      code at emit time, but need to allocate a scratch reg now.  */
3759
3760   emit_insn (gen_rtx_PARALLEL
3761              (VOIDmode,
3762               gen_rtvec (2,
3763                          gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
3764                                       gen_rtx_fmt_ee (code, mode,
3765                                                       copy_rtx (operands[0]), operands[2])),
3766                          gen_rtx_CLOBBER (VOIDmode,
3767                                           gen_rtx_SCRATCH (QImode)))));
3768   return true;
3769 }
3770
3771 /* Symbols of the various modes which can be used as indices.  */
3772
3773 enum shift_mode
3774 {
3775   QIshift, HIshift, SIshift
3776 };
3777
3778 /* For single bit shift insns, record assembler and what bits of the
3779    condition code are valid afterwards (represented as various CC_FOO
3780    bits, 0 means CC isn't left in a usable state).  */
3781
3782 struct shift_insn
3783 {
3784   const char *const assembler;
3785   const int cc_valid;
3786 };
3787
3788 /* Assembler instruction shift table.
3789
3790    These tables are used to look up the basic shifts.
3791    They are indexed by cpu, shift_type, and mode.  */
3792
3793 static const struct shift_insn shift_one[2][3][3] =
3794 {
3795 /* H8/300 */
3796   {
3797 /* SHIFT_ASHIFT */
3798     {
3799       { "shll\t%X0", CC_SET_ZNV },
3800       { "add.w\t%T0,%T0", CC_SET_ZN },
3801       { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", CC_CLOBBER }
3802     },
3803 /* SHIFT_LSHIFTRT */
3804     {
3805       { "shlr\t%X0", CC_SET_ZNV },
3806       { "shlr\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3807       { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3808     },
3809 /* SHIFT_ASHIFTRT */
3810     {
3811       { "shar\t%X0", CC_SET_ZNV },
3812       { "shar\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3813       { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3814     }
3815   },
3816 /* H8/300H */
3817   {
3818 /* SHIFT_ASHIFT */
3819     {
3820       { "shll.b\t%X0", CC_SET_ZNV },
3821       { "shll.w\t%T0", CC_SET_ZNV },
3822       { "shll.l\t%S0", CC_SET_ZNV }
3823     },
3824 /* SHIFT_LSHIFTRT */
3825     {
3826       { "shlr.b\t%X0", CC_SET_ZNV },
3827       { "shlr.w\t%T0", CC_SET_ZNV },
3828       { "shlr.l\t%S0", CC_SET_ZNV }
3829     },
3830 /* SHIFT_ASHIFTRT */
3831     {
3832       { "shar.b\t%X0", CC_SET_ZNV },
3833       { "shar.w\t%T0", CC_SET_ZNV },
3834       { "shar.l\t%S0", CC_SET_ZNV }
3835     }
3836   }
3837 };
3838
3839 static const struct shift_insn shift_two[3][3] =
3840 {
3841 /* SHIFT_ASHIFT */
3842     {
3843       { "shll.b\t#2,%X0", CC_SET_ZNV },
3844       { "shll.w\t#2,%T0", CC_SET_ZNV },
3845       { "shll.l\t#2,%S0", CC_SET_ZNV }
3846     },
3847 /* SHIFT_LSHIFTRT */
3848     {
3849       { "shlr.b\t#2,%X0", CC_SET_ZNV },
3850       { "shlr.w\t#2,%T0", CC_SET_ZNV },
3851       { "shlr.l\t#2,%S0", CC_SET_ZNV }
3852     },
3853 /* SHIFT_ASHIFTRT */
3854     {
3855       { "shar.b\t#2,%X0", CC_SET_ZNV },
3856       { "shar.w\t#2,%T0", CC_SET_ZNV },
3857       { "shar.l\t#2,%S0", CC_SET_ZNV }
3858     }
3859 };
3860
3861 /* Rotates are organized by which shift they'll be used in implementing.
3862    There's no need to record whether the cc is valid afterwards because
3863    it is the AND insn that will decide this.  */
3864
3865 static const char *const rotate_one[2][3][3] =
3866 {
3867 /* H8/300 */
3868   {
3869 /* SHIFT_ASHIFT */
3870     {
3871       "rotr\t%X0",
3872       "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
3873       0
3874     },
3875 /* SHIFT_LSHIFTRT */
3876     {
3877       "rotl\t%X0",
3878       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3879       0
3880     },
3881 /* SHIFT_ASHIFTRT */
3882     {
3883       "rotl\t%X0",
3884       "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3885       0
3886     }
3887   },
3888 /* H8/300H */
3889   {
3890 /* SHIFT_ASHIFT */
3891     {
3892       "rotr.b\t%X0",
3893       "rotr.w\t%T0",
3894       "rotr.l\t%S0"
3895     },
3896 /* SHIFT_LSHIFTRT */
3897     {
3898       "rotl.b\t%X0",
3899       "rotl.w\t%T0",
3900       "rotl.l\t%S0"
3901     },
3902 /* SHIFT_ASHIFTRT */
3903     {
3904       "rotl.b\t%X0",
3905       "rotl.w\t%T0",
3906       "rotl.l\t%S0"
3907     }
3908   }
3909 };
3910
3911 static const char *const rotate_two[3][3] =
3912 {
3913 /* SHIFT_ASHIFT */
3914     {
3915       "rotr.b\t#2,%X0",
3916       "rotr.w\t#2,%T0",
3917       "rotr.l\t#2,%S0"
3918     },
3919 /* SHIFT_LSHIFTRT */
3920     {
3921       "rotl.b\t#2,%X0",
3922       "rotl.w\t#2,%T0",
3923       "rotl.l\t#2,%S0"
3924     },
3925 /* SHIFT_ASHIFTRT */
3926     {
3927       "rotl.b\t#2,%X0",
3928       "rotl.w\t#2,%T0",
3929       "rotl.l\t#2,%S0"
3930     }
3931 };
3932
3933 struct shift_info {
3934   /* Shift algorithm.  */
3935   enum shift_alg alg;
3936
3937   /* The number of bits to be shifted by shift1 and shift2.  Valid
3938      when ALG is SHIFT_SPECIAL.  */
3939   unsigned int remainder;
3940
3941   /* Special insn for a shift.  Valid when ALG is SHIFT_SPECIAL.  */
3942   const char *special;
3943
3944   /* Insn for a one-bit shift.  Valid when ALG is either SHIFT_INLINE
3945      or SHIFT_SPECIAL, and REMAINDER is nonzero.  */
3946   const char *shift1;
3947
3948   /* Insn for a two-bit shift.  Valid when ALG is either SHIFT_INLINE
3949      or SHIFT_SPECIAL, and REMAINDER is nonzero.  */
3950   const char *shift2;
3951
3952   /* CC status for SHIFT_INLINE.  */
3953   int cc_inline;
3954
3955   /* CC status  for SHIFT_SPECIAL.  */
3956   int cc_special;
3957 };
3958
3959 static void get_shift_alg (enum shift_type,
3960                            enum shift_mode, unsigned int,
3961                            struct shift_info *);
3962
3963 /* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
3964    best algorithm for doing the shift.  The assembler code is stored
3965    in the pointers in INFO.  We achieve the maximum efficiency in most
3966    cases when !TARGET_H8300.  In case of TARGET_H8300, shifts in
3967    SImode in particular have a lot of room to optimize.
3968
3969    We first determine the strategy of the shift algorithm by a table
3970    lookup.  If that tells us to use a hand crafted assembly code, we
3971    go into the big switch statement to find what that is.  Otherwise,
3972    we resort to a generic way, such as inlining.  In either case, the
3973    result is returned through INFO.  */
3974
3975 static void
3976 get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
3977                unsigned int count, struct shift_info *info)
3978 {
3979   enum h8_cpu cpu;
3980
3981   /* Find the target CPU.  */
3982   if (TARGET_H8300)
3983     cpu = H8_300;
3984   else if (TARGET_H8300H)
3985     cpu = H8_300H;
3986   else
3987     cpu = H8_S;
3988
3989   /* Find the shift algorithm.  */
3990   info->alg = SHIFT_LOOP;
3991   switch (shift_mode)
3992     {
3993     case QIshift:
3994       if (count < GET_MODE_BITSIZE (QImode))
3995         info->alg = shift_alg_qi[cpu][shift_type][count];
3996       break;
3997
3998     case HIshift:
3999       if (count < GET_MODE_BITSIZE (HImode))
4000         info->alg = shift_alg_hi[cpu][shift_type][count];
4001       break;
4002
4003     case SIshift:
4004       if (count < GET_MODE_BITSIZE (SImode))
4005         info->alg = shift_alg_si[cpu][shift_type][count];
4006       break;
4007
4008     default:
4009       gcc_unreachable ();
4010     }
4011
4012   /* Fill in INFO.  Return unless we have SHIFT_SPECIAL.  */
4013   switch (info->alg)
4014     {
4015     case SHIFT_INLINE:
4016       info->remainder = count;
4017       /* Fall through.  */
4018
4019     case SHIFT_LOOP:
4020       /* It is up to the caller to know that looping clobbers cc.  */
4021       info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4022       info->shift2 = shift_two[shift_type][shift_mode].assembler;
4023       info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4024       goto end;
4025
4026     case SHIFT_ROT_AND:
4027       info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
4028       info->shift2 = rotate_two[shift_type][shift_mode];
4029       info->cc_inline = CC_CLOBBER;
4030       goto end;
4031
4032     case SHIFT_SPECIAL:
4033       /* REMAINDER is 0 for most cases, so initialize it to 0.  */
4034       info->remainder = 0;
4035       info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4036       info->shift2 = shift_two[shift_type][shift_mode].assembler;
4037       info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4038       info->cc_special = CC_CLOBBER;
4039       break;
4040     }
4041
4042   /* Here we only deal with SHIFT_SPECIAL.  */
4043   switch (shift_mode)
4044     {
4045     case QIshift:
4046       /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
4047          through the entire value.  */
4048       gcc_assert (shift_type == SHIFT_ASHIFTRT && count == 7);
4049       info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
4050       goto end;
4051
4052     case HIshift:
4053       if (count == 7)
4054         {
4055           switch (shift_type)
4056             {
4057             case SHIFT_ASHIFT:
4058               if (TARGET_H8300)
4059                 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";
4060               else
4061                 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
4062               goto end;
4063             case SHIFT_LSHIFTRT:
4064               if (TARGET_H8300)
4065                 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";
4066               else
4067                 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
4068               goto end;
4069             case SHIFT_ASHIFTRT:
4070               info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
4071               goto end;
4072             }
4073         }
4074       else if ((8 <= count && count <= 13)
4075                || (TARGET_H8300S && count == 14))
4076         {
4077           info->remainder = count - 8;
4078
4079           switch (shift_type)
4080             {
4081             case SHIFT_ASHIFT:
4082               info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
4083               goto end;
4084             case SHIFT_LSHIFTRT:
4085               if (TARGET_H8300)
4086                 {
4087                   info->special = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
4088                   info->shift1  = "shlr.b\t%s0";
4089                   info->cc_inline = CC_SET_ZNV;
4090                 }
4091               else
4092                 {
4093                   info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
4094                   info->cc_special = CC_SET_ZNV;
4095                 }
4096               goto end;
4097             case SHIFT_ASHIFTRT:
4098               if (TARGET_H8300)
4099                 {
4100                   info->special = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0";
4101                   info->shift1  = "shar.b\t%s0";
4102                 }
4103               else
4104                 {
4105                   info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
4106                   info->cc_special = CC_SET_ZNV;
4107                 }
4108               goto end;
4109             }
4110         }
4111       else if (count == 14)
4112         {
4113           switch (shift_type)
4114             {
4115             case SHIFT_ASHIFT:
4116               if (TARGET_H8300)
4117                 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";
4118               goto end;
4119             case SHIFT_LSHIFTRT:
4120               if (TARGET_H8300)
4121                 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";
4122               goto end;
4123             case SHIFT_ASHIFTRT:
4124               if (TARGET_H8300)
4125                 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";
4126               else if (TARGET_H8300H)
4127                 {
4128                   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";
4129                   info->cc_special = CC_SET_ZNV;
4130                 }
4131               else /* TARGET_H8300S */
4132                 gcc_unreachable ();
4133               goto end;
4134             }
4135         }
4136       else if (count == 15)
4137         {
4138           switch (shift_type)
4139             {
4140             case SHIFT_ASHIFT:
4141               info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
4142               goto end;
4143             case SHIFT_LSHIFTRT:
4144               info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
4145               goto end;
4146             case SHIFT_ASHIFTRT:
4147               info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
4148               goto end;
4149             }
4150         }
4151       gcc_unreachable ();
4152
4153     case SIshift:
4154       if (TARGET_H8300 && 8 <= count && count <= 9)
4155         {
4156           info->remainder = count - 8;
4157
4158           switch (shift_type)
4159             {
4160             case SHIFT_ASHIFT:
4161               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";
4162               goto end;
4163             case SHIFT_LSHIFTRT:
4164               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";
4165               info->shift1  = "shlr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0";
4166               goto end;
4167             case SHIFT_ASHIFTRT:
4168               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";
4169               goto end;
4170             }
4171         }
4172       else if (count == 8 && !TARGET_H8300)
4173         {
4174           switch (shift_type)
4175             {
4176             case SHIFT_ASHIFT:
4177               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";
4178               goto end;
4179             case SHIFT_LSHIFTRT:
4180               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";
4181               goto end;
4182             case SHIFT_ASHIFTRT:
4183               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";
4184               goto end;
4185             }
4186         }
4187       else if (count == 15 && TARGET_H8300)
4188         {
4189           switch (shift_type)
4190             {
4191             case SHIFT_ASHIFT:
4192               gcc_unreachable ();
4193             case SHIFT_LSHIFTRT:
4194               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";
4195               goto end;
4196             case SHIFT_ASHIFTRT:
4197               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";
4198               goto end;
4199             }
4200         }
4201       else if (count == 15 && !TARGET_H8300)
4202         {
4203           switch (shift_type)
4204             {
4205             case SHIFT_ASHIFT:
4206               info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
4207               info->cc_special = CC_SET_ZNV;
4208               goto end;
4209             case SHIFT_LSHIFTRT:
4210               info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
4211               info->cc_special = CC_SET_ZNV;
4212               goto end;
4213             case SHIFT_ASHIFTRT:
4214               gcc_unreachable ();
4215             }
4216         }
4217       else if ((TARGET_H8300 && 16 <= count && count <= 20)
4218                || (TARGET_H8300H && 16 <= count && count <= 19)
4219                || (TARGET_H8300S && 16 <= count && count <= 21))
4220         {
4221           info->remainder = count - 16;
4222
4223           switch (shift_type)
4224             {
4225             case SHIFT_ASHIFT:
4226               info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
4227               if (TARGET_H8300)
4228                 info->shift1 = "add.w\t%e0,%e0";
4229               goto end;
4230             case SHIFT_LSHIFTRT:
4231               if (TARGET_H8300)
4232                 {
4233                   info->special = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
4234                   info->shift1  = "shlr\t%x0\n\trotxr\t%w0";
4235                 }
4236               else
4237                 {
4238                   info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
4239                   info->cc_special = CC_SET_ZNV;
4240                 }
4241               goto end;
4242             case SHIFT_ASHIFTRT:
4243               if (TARGET_H8300)
4244                 {
4245                   info->special = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
4246                   info->shift1  = "shar\t%x0\n\trotxr\t%w0";
4247                 }
4248               else
4249                 {
4250                   info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
4251                   info->cc_special = CC_SET_ZNV;
4252                 }
4253               goto end;
4254             }
4255         }
4256       else if (TARGET_H8300 && 24 <= count && count <= 28)
4257         {
4258           info->remainder = count - 24;
4259
4260           switch (shift_type)
4261             {
4262             case SHIFT_ASHIFT:
4263               info->special = "mov.b\t%w0,%z0\n\tsub.b\t%y0,%y0\n\tsub.w\t%f0,%f0";
4264               info->shift1  = "shll.b\t%z0";
4265               info->cc_inline = CC_SET_ZNV;
4266               goto end;
4267             case SHIFT_LSHIFTRT:
4268               info->special = "mov.b\t%z0,%w0\n\tsub.b\t%x0,%x0\n\tsub.w\t%e0,%e0";
4269               info->shift1  = "shlr.b\t%w0";
4270               info->cc_inline = CC_SET_ZNV;
4271               goto end;
4272             case SHIFT_ASHIFTRT:
4273               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";
4274               info->shift1  = "shar.b\t%w0";
4275               info->cc_inline = CC_SET_ZNV;
4276               goto end;
4277             }
4278         }
4279       else if ((TARGET_H8300H && count == 24)
4280                || (TARGET_H8300S && 24 <= count && count <= 25))
4281         {
4282           info->remainder = count - 24;
4283
4284           switch (shift_type)
4285             {
4286             case SHIFT_ASHIFT:
4287               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";
4288               goto end;
4289             case SHIFT_LSHIFTRT:
4290               info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
4291               info->cc_special = CC_SET_ZNV;
4292               goto end;
4293             case SHIFT_ASHIFTRT:
4294               info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
4295               info->cc_special = CC_SET_ZNV;
4296               goto end;
4297             }
4298         }
4299       else if (!TARGET_H8300 && count == 28)
4300         {
4301           switch (shift_type)
4302             {
4303             case SHIFT_ASHIFT:
4304               if (TARGET_H8300H)
4305                 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";
4306               else
4307                 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";
4308               goto end;
4309             case SHIFT_LSHIFTRT:
4310               if (TARGET_H8300H)
4311                 {
4312                   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";
4313                   info->cc_special = CC_SET_ZNV;
4314                 }
4315               else
4316                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4317               goto end;
4318             case SHIFT_ASHIFTRT:
4319               gcc_unreachable ();
4320             }
4321         }
4322       else if (!TARGET_H8300 && count == 29)
4323         {
4324           switch (shift_type)
4325             {
4326             case SHIFT_ASHIFT:
4327               if (TARGET_H8300H)
4328                 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";
4329               else
4330                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4331               goto end;
4332             case SHIFT_LSHIFTRT:
4333               if (TARGET_H8300H)
4334                 {
4335                   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";
4336                   info->cc_special = CC_SET_ZNV;
4337                 }
4338               else
4339                 {
4340                   info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4341                   info->cc_special = CC_SET_ZNV;
4342                 }
4343               goto end;
4344             case SHIFT_ASHIFTRT:
4345               gcc_unreachable ();
4346             }
4347         }
4348       else if (!TARGET_H8300 && count == 30)
4349         {
4350           switch (shift_type)
4351             {
4352             case SHIFT_ASHIFT:
4353               if (TARGET_H8300H)
4354                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4355               else
4356                 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
4357               goto end;
4358             case SHIFT_LSHIFTRT:
4359               if (TARGET_H8300H)
4360                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4361               else
4362                 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4363               goto end;
4364             case SHIFT_ASHIFTRT:
4365               gcc_unreachable ();
4366             }
4367         }
4368       else if (count == 31)
4369         {
4370           if (TARGET_H8300)
4371             {
4372               switch (shift_type)
4373                 {
4374                 case SHIFT_ASHIFT:
4375                   info->special = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
4376                   goto end;
4377                 case SHIFT_LSHIFTRT:
4378                   info->special = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
4379                   goto end;
4380                 case SHIFT_ASHIFTRT:
4381                   info->special = "shll\t%z0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
4382                   goto end;
4383                 }
4384             }
4385           else
4386             {
4387               switch (shift_type)
4388                 {
4389                 case SHIFT_ASHIFT:
4390                   info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
4391                   info->cc_special = CC_SET_ZNV;
4392                   goto end;
4393                 case SHIFT_LSHIFTRT:
4394                   info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
4395                   info->cc_special = CC_SET_ZNV;
4396                   goto end;
4397                 case SHIFT_ASHIFTRT:
4398                   info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
4399                   info->cc_special = CC_SET_ZNV;
4400                   goto end;
4401                 }
4402             }
4403         }
4404       gcc_unreachable ();
4405
4406     default:
4407       gcc_unreachable ();
4408     }
4409
4410  end:
4411   if (!TARGET_H8300S)
4412     info->shift2 = NULL;
4413 }
4414
4415 /* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
4416    needed for some shift with COUNT and MODE.  Return 0 otherwise.  */
4417
4418 int
4419 h8300_shift_needs_scratch_p (int count, enum machine_mode mode)
4420 {
4421   enum h8_cpu cpu;
4422   int a, lr, ar;
4423
4424   if (GET_MODE_BITSIZE (mode) <= count)
4425     return 1;
4426
4427   /* Find out the target CPU.  */
4428   if (TARGET_H8300)
4429     cpu = H8_300;
4430   else if (TARGET_H8300H)
4431     cpu = H8_300H;
4432   else
4433     cpu = H8_S;
4434
4435   /* Find the shift algorithm.  */
4436   switch (mode)
4437     {
4438     case QImode:
4439       a  = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
4440       lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
4441       ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
4442       break;
4443
4444     case HImode:
4445       a  = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
4446       lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
4447       ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
4448       break;
4449
4450     case SImode:
4451       a  = shift_alg_si[cpu][SHIFT_ASHIFT][count];
4452       lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
4453       ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4454       break;
4455
4456     default:
4457       gcc_unreachable ();
4458     }
4459
4460   /* On H8/300H, count == 8 uses a scratch register.  */
4461   return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4462           || (TARGET_H8300H && mode == SImode && count == 8));
4463 }
4464
4465 /* Output the assembler code for doing shifts.  */
4466
4467 const char *
4468 output_a_shift (rtx *operands)
4469 {
4470   static int loopend_lab;
4471   rtx shift = operands[3];
4472   enum machine_mode mode = GET_MODE (shift);
4473   enum rtx_code code = GET_CODE (shift);
4474   enum shift_type shift_type;
4475   enum shift_mode shift_mode;
4476   struct shift_info info;
4477   int n;
4478
4479   loopend_lab++;
4480
4481   switch (mode)
4482     {
4483     case QImode:
4484       shift_mode = QIshift;
4485       break;
4486     case HImode:
4487       shift_mode = HIshift;
4488       break;
4489     case SImode:
4490       shift_mode = SIshift;
4491       break;
4492     default:
4493       gcc_unreachable ();
4494     }
4495
4496   switch (code)
4497     {
4498     case ASHIFTRT:
4499       shift_type = SHIFT_ASHIFTRT;
4500       break;
4501     case LSHIFTRT:
4502       shift_type = SHIFT_LSHIFTRT;
4503       break;
4504     case ASHIFT:
4505       shift_type = SHIFT_ASHIFT;
4506       break;
4507     default:
4508       gcc_unreachable ();
4509     }
4510
4511   /* This case must be taken care of by one of the two splitters
4512      that convert a variable shift into a loop.  */
4513   gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4514   
4515   n = INTVAL (operands[2]);
4516
4517   /* If the count is negative, make it 0.  */
4518   if (n < 0)
4519     n = 0;
4520   /* If the count is too big, truncate it.
4521      ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4522      do the intuitive thing.  */
4523   else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4524     n = GET_MODE_BITSIZE (mode);
4525
4526   get_shift_alg (shift_type, shift_mode, n, &info);
4527   
4528   switch (info.alg)
4529     {
4530     case SHIFT_SPECIAL:
4531       output_asm_insn (info.special, operands);
4532       /* Fall through.  */
4533
4534     case SHIFT_INLINE:
4535       n = info.remainder;
4536
4537       /* Emit two bit shifts first.  */
4538       if (info.shift2 != NULL)
4539         {
4540           for (; n > 1; n -= 2)
4541             output_asm_insn (info.shift2, operands);
4542         }
4543
4544       /* Now emit one bit shifts for any residual.  */
4545       for (; n > 0; n--)
4546         output_asm_insn (info.shift1, operands);
4547       return "";
4548       
4549     case SHIFT_ROT_AND:
4550       {
4551         int m = GET_MODE_BITSIZE (mode) - n;
4552         const int mask = (shift_type == SHIFT_ASHIFT
4553                           ? ((1 << m) - 1) << n
4554                           : (1 << m) - 1);
4555         char insn_buf[200];
4556
4557         /* Not all possibilities of rotate are supported.  They shouldn't
4558            be generated, but let's watch for 'em.  */
4559         gcc_assert (info.shift1);
4560         
4561         /* Emit two bit rotates first.  */
4562         if (info.shift2 != NULL)
4563           {
4564             for (; m > 1; m -= 2)
4565               output_asm_insn (info.shift2, operands);
4566           }
4567         
4568         /* Now single bit rotates for any residual.  */
4569         for (; m > 0; m--)
4570           output_asm_insn (info.shift1, operands);
4571         
4572         /* Now mask off the high bits.  */
4573         switch (mode)
4574           {
4575           case QImode:
4576             sprintf (insn_buf, "and\t#%d,%%X0", mask);
4577             break;
4578
4579           case HImode:
4580             gcc_assert (TARGET_H8300H || TARGET_H8300S);
4581             sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4582             break;
4583
4584           default:
4585             gcc_unreachable ();
4586           }
4587
4588         output_asm_insn (insn_buf, operands);
4589         return "";
4590       }
4591
4592     case SHIFT_LOOP:
4593       /* A loop to shift by a "large" constant value.
4594          If we have shift-by-2 insns, use them.  */
4595       if (info.shift2 != NULL)
4596         {
4597           fprintf (asm_out_file, "\tmov.b       #%d,%sl\n", n / 2,
4598                    names_big[REGNO (operands[4])]);
4599           fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4600           output_asm_insn (info.shift2, operands);
4601           output_asm_insn ("add #0xff,%X4", operands);
4602           fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4603           if (n % 2)
4604             output_asm_insn (info.shift1, operands);
4605         }
4606       else
4607         {
4608           fprintf (asm_out_file, "\tmov.b       #%d,%sl\n", n,
4609                    names_big[REGNO (operands[4])]);
4610           fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4611           output_asm_insn (info.shift1, operands);
4612           output_asm_insn ("add #0xff,%X4", operands);
4613           fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4614         }
4615       return "";
4616       
4617     default:
4618       gcc_unreachable ();
4619     }
4620 }
4621
4622 /* Count the number of assembly instructions in a string TEMPL.  */
4623
4624 static unsigned int
4625 h8300_asm_insn_count (const char *templ)
4626 {
4627   unsigned int count = 1;
4628
4629   for (; *templ; templ++)
4630     if (*templ == '\n')
4631       count++;
4632
4633   return count;
4634 }
4635
4636 /* Compute the length of a shift insn.  */
4637
4638 unsigned int
4639 compute_a_shift_length (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4640 {
4641   rtx shift = operands[3];
4642   enum machine_mode mode = GET_MODE (shift);
4643   enum rtx_code code = GET_CODE (shift);
4644   enum shift_type shift_type;
4645   enum shift_mode shift_mode;
4646   struct shift_info info;
4647   unsigned int wlength = 0;
4648
4649   switch (mode)
4650     {
4651     case QImode:
4652       shift_mode = QIshift;
4653       break;
4654     case HImode:
4655       shift_mode = HIshift;
4656       break;
4657     case SImode:
4658       shift_mode = SIshift;
4659       break;
4660     default:
4661       gcc_unreachable ();
4662     }
4663
4664   switch (code)
4665     {
4666     case ASHIFTRT:
4667       shift_type = SHIFT_ASHIFTRT;
4668       break;
4669     case LSHIFTRT:
4670       shift_type = SHIFT_LSHIFTRT;
4671       break;
4672     case ASHIFT:
4673       shift_type = SHIFT_ASHIFT;
4674       break;
4675     default:
4676       gcc_unreachable ();
4677     }
4678
4679   if (GET_CODE (operands[2]) != CONST_INT)
4680     {
4681       /* Get the assembler code to do one shift.  */
4682       get_shift_alg (shift_type, shift_mode, 1, &info);
4683
4684       return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4685     }
4686   else
4687     {
4688       int n = INTVAL (operands[2]);
4689
4690       /* If the count is negative, make it 0.  */
4691       if (n < 0)
4692         n = 0;
4693       /* If the count is too big, truncate it.
4694          ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4695          do the intuitive thing.  */
4696       else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4697         n = GET_MODE_BITSIZE (mode);
4698
4699       get_shift_alg (shift_type, shift_mode, n, &info);
4700
4701       switch (info.alg)
4702         {
4703         case SHIFT_SPECIAL:
4704           wlength += h8300_asm_insn_count (info.special);
4705
4706           /* Every assembly instruction used in SHIFT_SPECIAL case
4707              takes 2 bytes except xor.l, which takes 4 bytes, so if we
4708              see xor.l, we just pretend that xor.l counts as two insns
4709              so that the insn length will be computed correctly.  */
4710           if (strstr (info.special, "xor.l") != NULL)
4711             wlength++;
4712
4713           /* Fall through.  */
4714
4715         case SHIFT_INLINE:
4716           n = info.remainder;
4717
4718           if (info.shift2 != NULL)
4719             {
4720               wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4721               n = n % 2;
4722             }
4723
4724           wlength += h8300_asm_insn_count (info.shift1) * n;
4725
4726           return 2 * wlength;
4727
4728         case SHIFT_ROT_AND:
4729           {
4730             int m = GET_MODE_BITSIZE (mode) - n;
4731
4732             /* Not all possibilities of rotate are supported.  They shouldn't
4733                be generated, but let's watch for 'em.  */
4734             gcc_assert (info.shift1);
4735
4736             if (info.shift2 != NULL)
4737               {
4738                 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4739                 m = m % 2;
4740               }
4741
4742             wlength += h8300_asm_insn_count (info.shift1) * m;
4743
4744             /* Now mask off the high bits.  */
4745             switch (mode)
4746               {
4747               case QImode:
4748                 wlength += 1;
4749                 break;
4750               case HImode:
4751                 wlength += 2;
4752                 break;
4753               case SImode:
4754                 gcc_assert (!TARGET_H8300);
4755                 wlength += 3;
4756                 break;
4757               default:
4758                 gcc_unreachable ();
4759               }
4760             return 2 * wlength;
4761           }
4762
4763         case SHIFT_LOOP:
4764           /* A loop to shift by a "large" constant value.
4765              If we have shift-by-2 insns, use them.  */
4766           if (info.shift2 != NULL)
4767             {
4768               wlength += 3 + h8300_asm_insn_count (info.shift2);
4769               if (n % 2)
4770                 wlength += h8300_asm_insn_count (info.shift1);
4771             }
4772           else
4773             {
4774               wlength += 3 + h8300_asm_insn_count (info.shift1);
4775             }
4776           return 2 * wlength;
4777
4778         default:
4779           gcc_unreachable ();
4780         }
4781     }
4782 }
4783
4784 /* Compute which flag bits are valid after a shift insn.  */
4785
4786 int
4787 compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4788 {
4789   rtx shift = operands[3];
4790   enum machine_mode mode = GET_MODE (shift);
4791   enum rtx_code code = GET_CODE (shift);
4792   enum shift_type shift_type;
4793   enum shift_mode shift_mode;
4794   struct shift_info info;
4795   int n;
4796   
4797   switch (mode)
4798     {
4799     case QImode:
4800       shift_mode = QIshift;
4801       break;
4802     case HImode:
4803       shift_mode = HIshift;
4804       break;
4805     case SImode:
4806       shift_mode = SIshift;
4807       break;
4808     default:
4809       gcc_unreachable ();
4810     }
4811
4812   switch (code)
4813     {
4814     case ASHIFTRT:
4815       shift_type = SHIFT_ASHIFTRT;
4816       break;
4817     case LSHIFTRT:
4818       shift_type = SHIFT_LSHIFTRT;
4819       break;
4820     case ASHIFT:
4821       shift_type = SHIFT_ASHIFT;
4822       break;
4823     default:
4824       gcc_unreachable ();
4825     }
4826
4827   /* This case must be taken care of by one of the two splitters
4828      that convert a variable shift into a loop.  */
4829   gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4830   
4831   n = INTVAL (operands[2]);
4832
4833   /* If the count is negative, make it 0.  */
4834   if (n < 0)
4835     n = 0;
4836   /* If the count is too big, truncate it.
4837      ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4838      do the intuitive thing.  */
4839   else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4840     n = GET_MODE_BITSIZE (mode);
4841   
4842   get_shift_alg (shift_type, shift_mode, n, &info);
4843   
4844   switch (info.alg)
4845     {
4846     case SHIFT_SPECIAL:
4847       if (info.remainder == 0)
4848         return info.cc_special;
4849
4850       /* Fall through.  */
4851
4852     case SHIFT_INLINE:
4853       return info.cc_inline;
4854       
4855     case SHIFT_ROT_AND:
4856       /* This case always ends with an and instruction.  */
4857       return CC_SET_ZNV;
4858       
4859     case SHIFT_LOOP:
4860       /* A loop to shift by a "large" constant value.
4861          If we have shift-by-2 insns, use them.  */
4862       if (info.shift2 != NULL)
4863         {
4864           if (n % 2)
4865             return info.cc_inline;
4866         }
4867       return CC_CLOBBER;
4868       
4869     default:
4870       gcc_unreachable ();
4871     }
4872 }
4873 \f
4874 /* A rotation by a non-constant will cause a loop to be generated, in
4875    which a rotation by one bit is used.  A rotation by a constant,
4876    including the one in the loop, will be taken care of by
4877    output_a_rotate () at the insn emit time.  */
4878
4879 int
4880 expand_a_rotate (rtx operands[])
4881 {
4882   rtx dst = operands[0];
4883   rtx src = operands[1];
4884   rtx rotate_amount = operands[2];
4885   enum machine_mode mode = GET_MODE (dst);
4886
4887   if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4888     return false;
4889
4890   /* We rotate in place.  */
4891   emit_move_insn (dst, src);
4892
4893   if (GET_CODE (rotate_amount) != CONST_INT)
4894     {
4895       rtx counter = gen_reg_rtx (QImode);
4896       rtx start_label = gen_label_rtx ();
4897       rtx end_label = gen_label_rtx ();
4898
4899       /* If the rotate amount is less than or equal to 0,
4900          we go out of the loop.  */
4901       emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
4902                                QImode, 0, end_label);
4903
4904       /* Initialize the loop counter.  */
4905       emit_move_insn (counter, rotate_amount);
4906
4907       emit_label (start_label);
4908
4909       /* Rotate by one bit.  */
4910       switch (mode)
4911         {
4912         case QImode:
4913           emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
4914           break;
4915         case HImode:
4916           emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
4917           break;
4918         case SImode:
4919           emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
4920           break;
4921         default:
4922           gcc_unreachable ();
4923         }
4924
4925       /* Decrement the counter by 1.  */
4926       emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
4927
4928       /* If the loop counter is nonzero, we go back to the beginning
4929          of the loop.  */
4930       emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
4931                                start_label);
4932
4933       emit_label (end_label);
4934     }
4935   else
4936     {
4937       /* Rotate by AMOUNT bits.  */
4938       switch (mode)
4939         {
4940         case QImode:
4941           emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
4942           break;
4943         case HImode:
4944           emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
4945           break;
4946         case SImode:
4947           emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
4948           break;
4949         default:
4950           gcc_unreachable ();
4951         }
4952     }
4953
4954   return 1;
4955 }
4956
4957 /* Output a rotate insn.  */
4958
4959 const char *
4960 output_a_rotate (enum rtx_code code, rtx *operands)
4961 {
4962   rtx dst = operands[0];
4963   rtx rotate_amount = operands[2];
4964   enum shift_mode rotate_mode;
4965   enum shift_type rotate_type;
4966   const char *insn_buf;
4967   int bits;
4968   int amount;
4969   enum machine_mode mode = GET_MODE (dst);
4970
4971   gcc_assert (GET_CODE (rotate_amount) == CONST_INT);
4972
4973   switch (mode)
4974     {
4975     case QImode:
4976       rotate_mode = QIshift;
4977       break;
4978     case HImode:
4979       rotate_mode = HIshift;
4980       break;
4981     case SImode:
4982       rotate_mode = SIshift;
4983       break;
4984     default:
4985       gcc_unreachable ();
4986     }
4987
4988   switch (code)
4989     {
4990     case ROTATERT:
4991       rotate_type = SHIFT_ASHIFT;
4992       break;
4993     case ROTATE:
4994       rotate_type = SHIFT_LSHIFTRT;
4995       break;
4996     default:
4997       gcc_unreachable ();
4998     }
4999
5000   amount = INTVAL (rotate_amount);
5001
5002   /* Clean up AMOUNT.  */
5003   if (amount < 0)
5004     amount = 0;
5005   if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5006     amount = GET_MODE_BITSIZE (mode);
5007
5008   /* Determine the faster direction.  After this phase, amount will be
5009      at most a half of GET_MODE_BITSIZE (mode).  */
5010   if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5011     {
5012       /* Flip the direction.  */
5013       amount = GET_MODE_BITSIZE (mode) - amount;
5014       rotate_type =
5015         (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5016     }
5017
5018   /* See if a byte swap (in HImode) or a word swap (in SImode) can
5019      boost up the rotation.  */
5020   if ((mode == HImode && TARGET_H8300 && amount >= 5)
5021       || (mode == HImode && TARGET_H8300H && amount >= 6)
5022       || (mode == HImode && TARGET_H8300S && amount == 8)
5023       || (mode == SImode && TARGET_H8300H && amount >= 10)
5024       || (mode == SImode && TARGET_H8300S && amount >= 13))
5025     {
5026       switch (mode)
5027         {
5028         case HImode:
5029           /* This code works on any family.  */
5030           insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
5031           output_asm_insn (insn_buf, operands);
5032           break;
5033
5034         case SImode:
5035           /* This code works on the H8/300H and H8S.  */
5036           insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
5037           output_asm_insn (insn_buf, operands);
5038           break;
5039
5040         default:
5041           gcc_unreachable ();
5042         }
5043
5044       /* Adjust AMOUNT and flip the direction.  */
5045       amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5046       rotate_type =
5047         (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5048     }
5049
5050   /* Output rotate insns.  */
5051   for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
5052     {
5053       if (bits == 2)
5054         insn_buf = rotate_two[rotate_type][rotate_mode];
5055       else
5056         insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
5057
5058       for (; amount >= bits; amount -= bits)
5059         output_asm_insn (insn_buf, operands);
5060     }
5061
5062   return "";
5063 }
5064
5065 /* Compute the length of a rotate insn.  */
5066
5067 unsigned int
5068 compute_a_rotate_length (rtx *operands)
5069 {
5070   rtx src = operands[1];
5071   rtx amount_rtx = operands[2];
5072   enum machine_mode mode = GET_MODE (src);
5073   int amount;
5074   unsigned int length = 0;
5075
5076   gcc_assert (GET_CODE (amount_rtx) == CONST_INT);
5077
5078   amount = INTVAL (amount_rtx);
5079
5080   /* Clean up AMOUNT.  */
5081   if (amount < 0)
5082     amount = 0;
5083   if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5084     amount = GET_MODE_BITSIZE (mode);
5085
5086   /* Determine the faster direction.  After this phase, amount
5087      will be at most a half of GET_MODE_BITSIZE (mode).  */
5088   if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5089     /* Flip the direction.  */
5090     amount = GET_MODE_BITSIZE (mode) - amount;
5091
5092   /* See if a byte swap (in HImode) or a word swap (in SImode) can
5093      boost up the rotation.  */
5094   if ((mode == HImode && TARGET_H8300 && amount >= 5)
5095       || (mode == HImode && TARGET_H8300H && amount >= 6)
5096       || (mode == HImode && TARGET_H8300S && amount == 8)
5097       || (mode == SImode && TARGET_H8300H && amount >= 10)
5098       || (mode == SImode && TARGET_H8300S && amount >= 13))
5099     {
5100       /* Adjust AMOUNT and flip the direction.  */
5101       amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5102       length += 6;
5103     }
5104
5105   /* We use 2-bit rotations on the H8S.  */
5106   if (TARGET_H8300S)
5107     amount = amount / 2 + amount % 2;
5108
5109   /* The H8/300 uses three insns to rotate one bit, taking 6
5110      length.  */
5111   length += amount * ((TARGET_H8300 && mode == HImode) ? 6 : 2);
5112
5113   return length;
5114 }
5115 \f
5116 /* Fix the operands of a gen_xxx so that it could become a bit
5117    operating insn.  */
5118
5119 int
5120 fix_bit_operand (rtx *operands, enum rtx_code code)
5121 {
5122   /* The bit_operand predicate accepts any memory during RTL generation, but
5123      only 'U' memory afterwards, so if this is a MEM operand, we must force
5124      it to be valid for 'U' by reloading the address.  */
5125
5126   if (code == AND
5127       ? single_zero_operand (operands[2], QImode)
5128       : single_one_operand (operands[2], QImode))
5129     {
5130       /* OK to have a memory dest.  */
5131       if (GET_CODE (operands[0]) == MEM
5132           && !OK_FOR_U (operands[0]))
5133         {
5134           rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
5135                                  copy_to_mode_reg (Pmode,
5136                                                    XEXP (operands[0], 0)));
5137           MEM_COPY_ATTRIBUTES (mem, operands[0]);
5138           operands[0] = mem;
5139         }
5140
5141       if (GET_CODE (operands[1]) == MEM
5142           && !OK_FOR_U (operands[1]))
5143         {
5144           rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
5145                                  copy_to_mode_reg (Pmode,
5146                                                    XEXP (operands[1], 0)));
5147           MEM_COPY_ATTRIBUTES (mem, operands[0]);
5148           operands[1] = mem;
5149         }
5150       return 0;
5151     }
5152
5153   /* Dest and src op must be register.  */
5154
5155   operands[1] = force_reg (QImode, operands[1]);
5156   {
5157     rtx res = gen_reg_rtx (QImode);
5158     switch (code)
5159       {
5160       case AND:
5161         emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
5162         break;
5163       case IOR:
5164         emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
5165         break;
5166       case XOR:
5167         emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
5168         break;
5169       default:
5170         gcc_unreachable ();
5171       }
5172     emit_insn (gen_movqi (operands[0], res));
5173   }
5174   return 1;
5175 }
5176
5177 /* Return nonzero if FUNC is an interrupt function as specified
5178    by the "interrupt" attribute.  */
5179
5180 static int
5181 h8300_interrupt_function_p (tree func)
5182 {
5183   tree a;
5184
5185   if (TREE_CODE (func) != FUNCTION_DECL)
5186     return 0;
5187
5188   a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
5189   return a != NULL_TREE;
5190 }
5191
5192 /* Return nonzero if FUNC is a saveall function as specified by the
5193    "saveall" attribute.  */
5194
5195 static int
5196 h8300_saveall_function_p (tree func)
5197 {
5198   tree a;
5199
5200   if (TREE_CODE (func) != FUNCTION_DECL)
5201     return 0;
5202
5203   a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
5204   return a != NULL_TREE;
5205 }
5206
5207 /* Return nonzero if FUNC is an OS_Task function as specified
5208    by the "OS_Task" attribute.  */
5209
5210 static int
5211 h8300_os_task_function_p (tree func)
5212 {
5213   tree a;
5214
5215   if (TREE_CODE (func) != FUNCTION_DECL)
5216     return 0;
5217
5218   a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
5219   return a != NULL_TREE;
5220 }
5221
5222 /* Return nonzero if FUNC is a monitor function as specified
5223    by the "monitor" attribute.  */
5224
5225 static int
5226 h8300_monitor_function_p (tree func)
5227 {
5228   tree a;
5229
5230   if (TREE_CODE (func) != FUNCTION_DECL)
5231     return 0;
5232
5233   a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
5234   return a != NULL_TREE;
5235 }
5236
5237 /* Return nonzero if FUNC is a function that should be called
5238    through the function vector.  */
5239
5240 int
5241 h8300_funcvec_function_p (tree func)
5242 {
5243   tree a;
5244
5245   if (TREE_CODE (func) != FUNCTION_DECL)
5246     return 0;
5247
5248   a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
5249   return a != NULL_TREE;
5250 }
5251
5252 /* Return nonzero if DECL is a variable that's in the eight bit
5253    data area.  */
5254
5255 int
5256 h8300_eightbit_data_p (tree decl)
5257 {
5258   tree a;
5259
5260   if (TREE_CODE (decl) != VAR_DECL)
5261     return 0;
5262
5263   a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
5264   return a != NULL_TREE;
5265 }
5266
5267 /* Return nonzero if DECL is a variable that's in the tiny
5268    data area.  */
5269
5270 int
5271 h8300_tiny_data_p (tree decl)
5272 {
5273   tree a;
5274
5275   if (TREE_CODE (decl) != VAR_DECL)
5276     return 0;
5277
5278   a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
5279   return a != NULL_TREE;
5280 }
5281
5282 /* Generate an 'interrupt_handler' attribute for decls.  We convert
5283    all the pragmas to corresponding attributes.  */
5284
5285 static void
5286 h8300_insert_attributes (tree node, tree *attributes)
5287 {
5288   if (TREE_CODE (node) == FUNCTION_DECL)
5289     {
5290       if (pragma_interrupt)
5291         {
5292           pragma_interrupt = 0;
5293
5294           /* Add an 'interrupt_handler' attribute.  */
5295           *attributes = tree_cons (get_identifier ("interrupt_handler"),
5296                                    NULL, *attributes);
5297         }
5298
5299       if (pragma_saveall)
5300         {
5301           pragma_saveall = 0;
5302
5303           /* Add an 'saveall' attribute.  */
5304           *attributes = tree_cons (get_identifier ("saveall"),
5305                                    NULL, *attributes);
5306         }
5307     }
5308 }
5309
5310 /* Supported attributes:
5311
5312    interrupt_handler: output a prologue and epilogue suitable for an
5313    interrupt handler.
5314
5315    saveall: output a prologue and epilogue that saves and restores
5316    all registers except the stack pointer.
5317
5318    function_vector: This function should be called through the
5319    function vector.
5320
5321    eightbit_data: This variable lives in the 8-bit data area and can
5322    be referenced with 8-bit absolute memory addresses.
5323
5324    tiny_data: This variable lives in the tiny data area and can be
5325    referenced with 16-bit absolute memory references.  */
5326
5327 static const struct attribute_spec h8300_attribute_table[] =
5328 {
5329   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
5330   { "interrupt_handler", 0, 0, true,  false, false, h8300_handle_fndecl_attribute },
5331   { "saveall",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
5332   { "OS_Task",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
5333   { "monitor",           0, 0, true,  false, false, h8300_handle_fndecl_attribute },
5334   { "function_vector",   0, 0, true,  false, false, h8300_handle_fndecl_attribute },
5335   { "eightbit_data",     0, 0, true,  false, false, h8300_handle_eightbit_data_attribute },
5336   { "tiny_data",         0, 0, true,  false, false, h8300_handle_tiny_data_attribute },
5337   { NULL,                0, 0, false, false, false, NULL }
5338 };
5339
5340
5341 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
5342    struct attribute_spec.handler.  */
5343 static tree
5344 h8300_handle_fndecl_attribute (tree *node, tree name,
5345                                tree args ATTRIBUTE_UNUSED,
5346                                int flags ATTRIBUTE_UNUSED,
5347                                bool *no_add_attrs)
5348 {
5349   if (TREE_CODE (*node) != FUNCTION_DECL)
5350     {
5351       warning (OPT_Wattributes, "%qE attribute only applies to functions",
5352                name);
5353       *no_add_attrs = true;
5354     }
5355
5356   return NULL_TREE;
5357 }
5358
5359 /* Handle an "eightbit_data" attribute; arguments as in
5360    struct attribute_spec.handler.  */
5361 static tree
5362 h8300_handle_eightbit_data_attribute (tree *node, tree name,
5363                                       tree args ATTRIBUTE_UNUSED,
5364                                       int flags ATTRIBUTE_UNUSED,
5365                                       bool *no_add_attrs)
5366 {
5367   tree decl = *node;
5368
5369   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5370     {
5371       DECL_SECTION_NAME (decl) = build_string (7, ".eight");
5372     }
5373   else
5374     {
5375       warning (OPT_Wattributes, "%qE attribute ignored",
5376                name);
5377       *no_add_attrs = true;
5378     }
5379
5380   return NULL_TREE;
5381 }
5382
5383 /* Handle an "tiny_data" attribute; arguments as in
5384    struct attribute_spec.handler.  */
5385 static tree
5386 h8300_handle_tiny_data_attribute (tree *node, tree name,
5387                                   tree args ATTRIBUTE_UNUSED,
5388                                   int flags ATTRIBUTE_UNUSED,
5389                                   bool *no_add_attrs)
5390 {
5391   tree decl = *node;
5392
5393   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5394     {
5395       DECL_SECTION_NAME (decl) = build_string (6, ".tiny");
5396     }
5397   else
5398     {
5399       warning (OPT_Wattributes, "%qE attribute ignored",
5400                name);
5401       *no_add_attrs = true;
5402     }
5403
5404   return NULL_TREE;
5405 }
5406
5407 /* Mark function vectors, and various small data objects.  */
5408
5409 static void
5410 h8300_encode_section_info (tree decl, rtx rtl, int first)
5411 {
5412   int extra_flags = 0;
5413
5414   default_encode_section_info (decl, rtl, first);
5415
5416   if (TREE_CODE (decl) == FUNCTION_DECL
5417       && h8300_funcvec_function_p (decl))
5418     extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
5419   else if (TREE_CODE (decl) == VAR_DECL
5420            && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
5421     {
5422       if (h8300_eightbit_data_p (decl))
5423         extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
5424       else if (first && h8300_tiny_data_p (decl))
5425         extra_flags = SYMBOL_FLAG_TINY_DATA;
5426     }
5427
5428   if (extra_flags)
5429     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
5430 }
5431
5432 /* Output a single-bit extraction.  */
5433
5434 const char *
5435 output_simode_bld (int bild, rtx operands[])
5436 {
5437   if (TARGET_H8300)
5438     {
5439       /* Clear the destination register.  */
5440       output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
5441
5442       /* Now output the bit load or bit inverse load, and store it in
5443          the destination.  */
5444       if (bild)
5445         output_asm_insn ("bild\t%Z2,%Y1", operands);
5446       else
5447         output_asm_insn ("bld\t%Z2,%Y1", operands);
5448
5449       output_asm_insn ("bst\t#0,%w0", operands);
5450     }
5451   else
5452     {
5453       /* Determine if we can clear the destination first.  */
5454       int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5455                          && REGNO (operands[0]) != REGNO (operands[1]));
5456
5457       if (clear_first)
5458         output_asm_insn ("sub.l\t%S0,%S0", operands);
5459
5460       /* Output the bit load or bit inverse load.  */
5461       if (bild)
5462         output_asm_insn ("bild\t%Z2,%Y1", operands);
5463       else
5464         output_asm_insn ("bld\t%Z2,%Y1", operands);
5465
5466       if (!clear_first)
5467         output_asm_insn ("xor.l\t%S0,%S0", operands);
5468
5469       /* Perform the bit store.  */
5470       output_asm_insn ("rotxl.l\t%S0", operands);
5471     }
5472
5473   /* All done.  */
5474   return "";
5475 }
5476
5477 /* Delayed-branch scheduling is more effective if we have some idea
5478    how long each instruction will be.  Use a shorten_branches pass
5479    to get an initial estimate.  */
5480
5481 static void
5482 h8300_reorg (void)
5483 {
5484   if (flag_delayed_branch)
5485     shorten_branches (get_insns ());
5486 }
5487
5488 #ifndef OBJECT_FORMAT_ELF
5489 static void
5490 h8300_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED,
5491                          tree decl)
5492 {
5493   /* ??? Perhaps we should be using default_coff_asm_named_section.  */
5494   fprintf (asm_out_file, "\t.section %s\n", name);
5495 }
5496 #endif /* ! OBJECT_FORMAT_ELF */
5497
5498 /* Nonzero if X is a constant address suitable as an 8-bit absolute,
5499    which is a special case of the 'R' operand.  */
5500
5501 int
5502 h8300_eightbit_constant_address_p (rtx x)
5503 {
5504   /* The ranges of the 8-bit area.  */
5505   const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5506   const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
5507   const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5508   const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5509   const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5510   const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5511
5512   unsigned HOST_WIDE_INT addr;
5513
5514   /* We accept symbols declared with eightbit_data.  */
5515   if (GET_CODE (x) == SYMBOL_REF)
5516     return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
5517
5518   if (GET_CODE (x) != CONST_INT)
5519     return 0;
5520
5521   addr = INTVAL (x);
5522
5523   return (0
5524           || ((TARGET_H8300 || TARGET_NORMAL_MODE) && IN_RANGE (addr, n1, n2))
5525           || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5526           || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5527 }
5528
5529 /* Nonzero if X is a constant address suitable as an 16-bit absolute
5530    on H8/300H and H8S.  */
5531
5532 int
5533 h8300_tiny_constant_address_p (rtx x)
5534 {
5535   /* The ranges of the 16-bit area.  */
5536   const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5537   const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5538   const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5539   const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5540   const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5541   const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5542   const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5543   const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5544
5545   unsigned HOST_WIDE_INT addr;
5546
5547   switch (GET_CODE (x))
5548     {
5549     case SYMBOL_REF:
5550       /* In the normal mode, any symbol fits in the 16-bit absolute
5551          address range.  We also accept symbols declared with
5552          tiny_data.  */
5553       return (TARGET_NORMAL_MODE
5554               || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
5555
5556     case CONST_INT:
5557       addr = INTVAL (x);
5558       return (TARGET_NORMAL_MODE
5559               || (TARGET_H8300H
5560                   && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5561               || (TARGET_H8300S
5562                   && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
5563
5564     case CONST:
5565       return TARGET_NORMAL_MODE;
5566
5567     default:
5568       return 0;
5569     }
5570
5571 }
5572
5573 /* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5574    locations that can be accessed as a 16-bit word.  */
5575
5576 int
5577 byte_accesses_mergeable_p (rtx addr1, rtx addr2)
5578 {
5579   HOST_WIDE_INT offset1, offset2;
5580   rtx reg1, reg2;
5581
5582   if (REG_P (addr1))
5583     {
5584       reg1 = addr1;
5585       offset1 = 0;
5586     }
5587   else if (GET_CODE (addr1) == PLUS
5588            && REG_P (XEXP (addr1, 0))
5589            && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5590     {
5591       reg1 = XEXP (addr1, 0);
5592       offset1 = INTVAL (XEXP (addr1, 1));
5593     }
5594   else
5595     return 0;
5596
5597   if (REG_P (addr2))
5598     {
5599       reg2 = addr2;
5600       offset2 = 0;
5601     }
5602   else if (GET_CODE (addr2) == PLUS
5603            && REG_P (XEXP (addr2, 0))
5604            && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5605     {
5606       reg2 = XEXP (addr2, 0);
5607       offset2 = INTVAL (XEXP (addr2, 1));
5608     }
5609   else
5610     return 0;
5611
5612   if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5613        || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5614       && offset1 % 2 == 0
5615       && offset1 + 1 == offset2)
5616     return 1;
5617
5618   return 0;
5619 }
5620
5621 /* Return nonzero if we have the same comparison insn as I3 two insns
5622    before I3.  I3 is assumed to be a comparison insn.  */
5623
5624 int
5625 same_cmp_preceding_p (rtx i3)
5626 {
5627   rtx i1, i2;
5628
5629   /* Make sure we have a sequence of three insns.  */
5630   i2 = prev_nonnote_insn (i3);
5631   if (i2 == NULL_RTX)
5632     return 0;
5633   i1 = prev_nonnote_insn (i2);
5634   if (i1 == NULL_RTX)
5635     return 0;
5636
5637   return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5638           && any_condjump_p (i2) && onlyjump_p (i2));
5639 }
5640
5641 /* Return nonzero if we have the same comparison insn as I1 two insns
5642    after I1.  I1 is assumed to be a comparison insn.  */
5643
5644 int
5645 same_cmp_following_p (rtx i1)
5646 {
5647   rtx i2, i3;
5648
5649   /* Make sure we have a sequence of three insns.  */
5650   i2 = next_nonnote_insn (i1);
5651   if (i2 == NULL_RTX)
5652     return 0;
5653   i3 = next_nonnote_insn (i2);
5654   if (i3 == NULL_RTX)
5655     return 0;
5656
5657   return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5658           && any_condjump_p (i2) && onlyjump_p (i2));
5659 }
5660
5661 /* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
5662    (or pops) N registers.  OPERANDS are assumed to be an array of
5663    registers.  */
5664
5665 int
5666 h8300_regs_ok_for_stm (int n, rtx operands[])
5667 {
5668   switch (n)
5669     {
5670     case 2:
5671       return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5672               || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5673               || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5674     case 3:
5675       return ((REGNO (operands[0]) == 0
5676                && REGNO (operands[1]) == 1
5677                && REGNO (operands[2]) == 2)
5678               || (REGNO (operands[0]) == 4
5679                   && REGNO (operands[1]) == 5
5680                   && REGNO (operands[2]) == 6));
5681
5682     case 4:
5683       return (REGNO (operands[0]) == 0
5684               && REGNO (operands[1]) == 1
5685               && REGNO (operands[2]) == 2
5686               && REGNO (operands[3]) == 3);
5687     default:
5688       gcc_unreachable ();
5689     }
5690 }
5691
5692 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG.  */
5693
5694 int
5695 h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5696                             unsigned int new_reg)
5697 {
5698   /* Interrupt functions can only use registers that have already been
5699      saved by the prologue, even if they would normally be
5700      call-clobbered.  */
5701
5702   if (h8300_current_function_interrupt_function_p ()
5703       && !df_regs_ever_live_p (new_reg))
5704     return 0;
5705
5706   return 1;
5707 }
5708
5709 /* Returns true if register REGNO is safe to be allocated as a scratch
5710    register in the current function.  */
5711
5712 static bool
5713 h8300_hard_regno_scratch_ok (unsigned int regno)
5714 {
5715   if (h8300_current_function_interrupt_function_p ()
5716       && ! WORD_REG_USED (regno))
5717     return false;
5718
5719   return true;
5720 }
5721
5722
5723 /* Return nonzero if X is a legitimate constant.  */
5724
5725 int
5726 h8300_legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
5727 {
5728   return 1;
5729 }
5730
5731 /* Return nonzero if X is a REG or SUBREG suitable as a base register.  */
5732
5733 static int
5734 h8300_rtx_ok_for_base_p (rtx x, int strict)
5735 {
5736   /* Strip off SUBREG if any.  */
5737   if (GET_CODE (x) == SUBREG)
5738     x = SUBREG_REG (x);
5739
5740   return (REG_P (x)
5741           && (strict
5742               ? REG_OK_FOR_BASE_STRICT_P (x)
5743               : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5744 }
5745
5746 /* Return nozero if X is a legitimate address.  On the H8/300, a
5747    legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5748    CONSTANT_ADDRESS.  */
5749
5750 static bool
5751 h8300_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
5752 {
5753   /* The register indirect addresses like @er0 is always valid.  */
5754   if (h8300_rtx_ok_for_base_p (x, strict))
5755     return 1;
5756
5757   if (CONSTANT_ADDRESS_P (x))
5758     return 1;
5759
5760   if (TARGET_H8300SX
5761       && (   GET_CODE (x) == PRE_INC
5762           || GET_CODE (x) == PRE_DEC
5763           || GET_CODE (x) == POST_INC
5764           || GET_CODE (x) == POST_DEC)
5765       && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5766     return 1;
5767
5768   if (GET_CODE (x) == PLUS
5769       && CONSTANT_ADDRESS_P (XEXP (x, 1))
5770       && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5771                                                    mode, 0), strict))
5772     return 1;
5773
5774   return 0;
5775 }
5776
5777 /* Worker function for HARD_REGNO_NREGS.
5778
5779    We pretend the MAC register is 32bits -- we don't have any data
5780    types on the H8 series to handle more than 32bits.  */
5781
5782 int
5783 h8300_hard_regno_nregs (int regno ATTRIBUTE_UNUSED, enum machine_mode mode)
5784 {
5785   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5786 }
5787
5788 /* Worker function for HARD_REGNO_MODE_OK.  */
5789
5790 int
5791 h8300_hard_regno_mode_ok (int regno, enum machine_mode mode)
5792 {
5793   if (TARGET_H8300)
5794     /* If an even reg, then anything goes.  Otherwise the mode must be
5795        QI or HI.  */
5796     return ((regno & 1) == 0) || (mode == HImode) || (mode == QImode);
5797   else
5798     /* MAC register can only be of SImode.  Otherwise, anything
5799        goes.  */
5800     return regno == MAC_REG ? mode == SImode : 1;
5801 }
5802 \f
5803 /* Perform target dependent optabs initialization.  */
5804 static void
5805 h8300_init_libfuncs (void)
5806 {
5807   set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5808   set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5809   set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5810   set_optab_libfunc (smod_optab, HImode, "__modhi3");
5811   set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5812 }
5813 \f
5814 /* Worker function for TARGET_RETURN_IN_MEMORY.  */
5815
5816 static bool
5817 h8300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
5818 {
5819   return (TYPE_MODE (type) == BLKmode
5820           || GET_MODE_SIZE (TYPE_MODE (type)) > (TARGET_H8300 ? 4 : 8));
5821 }
5822 \f
5823 /* We emit the entire trampoline here.  Depending on the pointer size,
5824    we use a different trampoline.
5825
5826    Pmode == HImode
5827               vvvv context
5828    1 0000 7903xxxx              mov.w   #0x1234,r3
5829    2 0004 5A00xxxx              jmp     @0x1234
5830               ^^^^ function
5831
5832    Pmode == SImode
5833               vvvvvvvv context
5834    2 0000 7A03xxxxxxxx          mov.l   #0x12345678,er3
5835    3 0006 5Axxxxxx              jmp     @0x123456
5836             ^^^^^^ function
5837 */
5838
5839 static void
5840 h8300_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
5841 {
5842   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
5843   rtx mem;
5844
5845   if (Pmode == HImode)
5846     {
5847       mem = adjust_address (m_tramp, HImode, 0);
5848       emit_move_insn (mem, GEN_INT (0x7903));
5849       mem = adjust_address (m_tramp, Pmode, 2);
5850       emit_move_insn (mem, cxt);
5851       mem = adjust_address (m_tramp, HImode, 4);
5852       emit_move_insn (mem, GEN_INT (0x5a00));
5853       mem = adjust_address (m_tramp, Pmode, 6);
5854       emit_move_insn (mem, fnaddr);
5855     }
5856   else
5857     {
5858       rtx tem;
5859
5860       mem = adjust_address (m_tramp, HImode, 0);
5861       emit_move_insn (mem, GEN_INT (0x7a03));
5862       mem = adjust_address (m_tramp, Pmode, 2);
5863       emit_move_insn (mem, cxt);
5864
5865       tem = copy_to_reg (fnaddr);
5866       emit_insn (gen_andsi3 (tem, tem, GEN_INT (0x00ffffff)));
5867       emit_insn (gen_iorsi3 (tem, tem, GEN_INT (0x5a000000)));
5868       mem = adjust_address (m_tramp, SImode, 6);
5869       emit_move_insn (mem, tem);
5870     }
5871 }
5872 \f
5873 /* Initialize the GCC target structure.  */
5874 #undef TARGET_ATTRIBUTE_TABLE
5875 #define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
5876
5877 #undef TARGET_ASM_ALIGNED_HI_OP
5878 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
5879
5880 #undef TARGET_ASM_FILE_START
5881 #define TARGET_ASM_FILE_START h8300_file_start
5882 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
5883 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
5884
5885 #undef TARGET_ASM_FILE_END
5886 #define TARGET_ASM_FILE_END h8300_file_end
5887
5888 #undef TARGET_ENCODE_SECTION_INFO
5889 #define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
5890
5891 #undef TARGET_INSERT_ATTRIBUTES
5892 #define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
5893
5894 #undef TARGET_RTX_COSTS
5895 #define TARGET_RTX_COSTS h8300_rtx_costs
5896
5897 #undef TARGET_INIT_LIBFUNCS
5898 #define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
5899
5900 #undef TARGET_RETURN_IN_MEMORY
5901 #define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
5902
5903 #undef  TARGET_MACHINE_DEPENDENT_REORG
5904 #define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
5905
5906 #undef TARGET_HARD_REGNO_SCRATCH_OK
5907 #define TARGET_HARD_REGNO_SCRATCH_OK h8300_hard_regno_scratch_ok
5908
5909 #undef TARGET_LEGITIMATE_ADDRESS_P
5910 #define TARGET_LEGITIMATE_ADDRESS_P     h8300_legitimate_address_p
5911
5912 #undef TARGET_DEFAULT_TARGET_FLAGS
5913 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT
5914
5915 #undef TARGET_CAN_ELIMINATE
5916 #define TARGET_CAN_ELIMINATE h8300_can_eliminate
5917
5918 #undef TARGET_TRAMPOLINE_INIT
5919 #define TARGET_TRAMPOLINE_INIT h8300_trampoline_init
5920
5921 struct gcc_target targetm = TARGET_INITIALIZER;