OSDN Git Service

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