OSDN Git Service

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