OSDN Git Service

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