OSDN Git Service

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