OSDN Git Service

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