2013-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / config / h8300 / h8300.c
blob7ec13264a11d10b4aa44f066373997b4e7e9d69e
1 /* Subroutines for insn-output.c for Renesas H8/300.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Steve Chamberlain (sac@cygnus.com),
6 Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
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 "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "recog.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "optabs.h"
41 #include "diagnostic-core.h"
42 #include "c-family/c-pragma.h" /* ??? */
43 #include "tm_p.h"
44 #include "tm-constrs.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "df.h"
50 /* Classifies a h8300_src_operand or h8300_dst_operand.
52 H8OP_IMMEDIATE
53 A constant operand of some sort.
55 H8OP_REGISTER
56 An ordinary register.
58 H8OP_MEM_ABSOLUTE
59 A memory reference with a constant address.
61 H8OP_MEM_BASE
62 A memory reference with a register as its address.
64 H8OP_MEM_COMPLEX
65 Some other kind of memory reference. */
66 enum h8300_operand_class
68 H8OP_IMMEDIATE,
69 H8OP_REGISTER,
70 H8OP_MEM_ABSOLUTE,
71 H8OP_MEM_BASE,
72 H8OP_MEM_COMPLEX,
73 NUM_H8OPS
76 /* For a general two-operand instruction, element [X][Y] gives
77 the length of the opcode fields when the first operand has class
78 (X + 1) and the second has class Y. */
79 typedef unsigned char h8300_length_table[NUM_H8OPS - 1][NUM_H8OPS];
81 /* Forward declarations. */
82 static const char *byte_reg (rtx, int);
83 static int h8300_interrupt_function_p (tree);
84 static int h8300_saveall_function_p (tree);
85 static int h8300_monitor_function_p (tree);
86 static int h8300_os_task_function_p (tree);
87 static void h8300_emit_stack_adjustment (int, HOST_WIDE_INT, bool);
88 static HOST_WIDE_INT round_frame_size (HOST_WIDE_INT);
89 static unsigned int compute_saved_regs (void);
90 static const char *cond_string (enum rtx_code);
91 static unsigned int h8300_asm_insn_count (const char *);
92 static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
93 static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
94 static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
95 static void h8300_print_operand_address (FILE *, rtx);
96 static void h8300_print_operand (FILE *, rtx, int);
97 static bool h8300_print_operand_punct_valid_p (unsigned char code);
98 #ifndef OBJECT_FORMAT_ELF
99 static void h8300_asm_named_section (const char *, unsigned int, tree);
100 #endif
101 static int h8300_register_move_cost (enum machine_mode, reg_class_t, reg_class_t);
102 static int h8300_and_costs (rtx);
103 static int h8300_shift_costs (rtx);
104 static void h8300_push_pop (int, int, bool, bool);
105 static int h8300_stack_offset_p (rtx, int);
106 static int h8300_ldm_stm_regno (rtx, int, int, int);
107 static void h8300_reorg (void);
108 static unsigned int h8300_constant_length (rtx);
109 static unsigned int h8300_displacement_length (rtx, int);
110 static unsigned int h8300_classify_operand (rtx, int, enum h8300_operand_class *);
111 static unsigned int h8300_length_from_table (rtx, rtx, const h8300_length_table *);
112 static unsigned int h8300_unary_length (rtx);
113 static unsigned int h8300_short_immediate_length (rtx);
114 static unsigned int h8300_bitfield_length (rtx, rtx);
115 static unsigned int h8300_binary_length (rtx, const h8300_length_table *);
116 static bool h8300_short_move_mem_p (rtx, enum rtx_code);
117 static unsigned int h8300_move_length (rtx *, const h8300_length_table *);
118 static bool h8300_hard_regno_scratch_ok (unsigned int);
119 static rtx h8300_get_index (rtx, enum machine_mode mode, int *);
121 /* CPU_TYPE, says what cpu we're compiling for. */
122 int cpu_type;
124 /* True if a #pragma interrupt has been seen for the current function. */
125 static int pragma_interrupt;
127 /* True if a #pragma saveall has been seen for the current function. */
128 static int pragma_saveall;
130 static const char *const names_big[] =
131 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
133 static const char *const names_extended[] =
134 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
136 static const char *const names_upper_extended[] =
137 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
139 /* Points to one of the above. */
140 /* ??? The above could be put in an array indexed by CPU_TYPE. */
141 const char * const *h8_reg_names;
143 /* Various operations needed by the following, indexed by CPU_TYPE. */
145 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
147 /* Value of MOVE_RATIO. */
148 int h8300_move_ratio;
150 /* See below where shifts are handled for explanation of this enum. */
152 enum shift_alg
154 SHIFT_INLINE,
155 SHIFT_ROT_AND,
156 SHIFT_SPECIAL,
157 SHIFT_LOOP
160 /* Symbols of the various shifts which can be used as indices. */
162 enum shift_type
164 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
167 /* Macros to keep the shift algorithm tables small. */
168 #define INL SHIFT_INLINE
169 #define ROT SHIFT_ROT_AND
170 #define LOP SHIFT_LOOP
171 #define SPC SHIFT_SPECIAL
173 /* The shift algorithms for each machine, mode, shift type, and shift
174 count are defined below. The three tables below correspond to
175 QImode, HImode, and SImode, respectively. Each table is organized
176 by, in the order of indices, machine, shift type, and shift count. */
178 static enum shift_alg shift_alg_qi[3][3][8] = {
180 /* TARGET_H8300 */
181 /* 0 1 2 3 4 5 6 7 */
182 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
183 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
184 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
187 /* TARGET_H8300H */
188 /* 0 1 2 3 4 5 6 7 */
189 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
190 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
191 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
194 /* TARGET_H8300S */
195 /* 0 1 2 3 4 5 6 7 */
196 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT */
197 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
198 { INL, INL, INL, INL, INL, INL, INL, SPC } /* SHIFT_ASHIFTRT */
202 static enum shift_alg shift_alg_hi[3][3][16] = {
204 /* TARGET_H8300 */
205 /* 0 1 2 3 4 5 6 7 */
206 /* 8 9 10 11 12 13 14 15 */
207 { INL, INL, INL, INL, INL, INL, INL, SPC,
208 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
209 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
210 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
211 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
212 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
215 /* TARGET_H8300H */
216 /* 0 1 2 3 4 5 6 7 */
217 /* 8 9 10 11 12 13 14 15 */
218 { INL, INL, INL, INL, INL, INL, INL, SPC,
219 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
220 { INL, INL, INL, INL, INL, INL, INL, SPC,
221 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
222 { INL, INL, INL, INL, INL, INL, INL, SPC,
223 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
226 /* TARGET_H8300S */
227 /* 0 1 2 3 4 5 6 7 */
228 /* 8 9 10 11 12 13 14 15 */
229 { INL, INL, INL, INL, INL, INL, INL, INL,
230 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
231 { INL, INL, INL, INL, INL, INL, INL, INL,
232 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
233 { INL, INL, INL, INL, INL, INL, INL, INL,
234 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
238 static enum shift_alg shift_alg_si[3][3][32] = {
240 /* TARGET_H8300 */
241 /* 0 1 2 3 4 5 6 7 */
242 /* 8 9 10 11 12 13 14 15 */
243 /* 16 17 18 19 20 21 22 23 */
244 /* 24 25 26 27 28 29 30 31 */
245 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
246 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
247 SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
248 SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT */
249 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
250 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
251 SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
252 SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
253 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
254 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
255 SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
256 SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
259 /* TARGET_H8300H */
260 /* 0 1 2 3 4 5 6 7 */
261 /* 8 9 10 11 12 13 14 15 */
262 /* 16 17 18 19 20 21 22 23 */
263 /* 24 25 26 27 28 29 30 31 */
264 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
265 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
266 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
267 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
268 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
269 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
270 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
271 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
272 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
273 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
274 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
275 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
278 /* TARGET_H8300S */
279 /* 0 1 2 3 4 5 6 7 */
280 /* 8 9 10 11 12 13 14 15 */
281 /* 16 17 18 19 20 21 22 23 */
282 /* 24 25 26 27 28 29 30 31 */
283 { INL, INL, INL, INL, INL, INL, INL, INL,
284 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
285 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
286 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
287 { INL, INL, INL, INL, INL, INL, INL, INL,
288 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
289 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
290 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
291 { INL, INL, INL, INL, INL, INL, INL, INL,
292 INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
293 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
294 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
298 #undef INL
299 #undef ROT
300 #undef LOP
301 #undef SPC
303 enum h8_cpu
305 H8_300,
306 H8_300H,
307 H8_S
310 /* Initialize various cpu specific globals at start up. */
312 static void
313 h8300_option_override (void)
315 static const char *const h8_push_ops[2] = { "push" , "push.l" };
316 static const char *const h8_pop_ops[2] = { "pop" , "pop.l" };
317 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
319 #ifndef OBJECT_FORMAT_ELF
320 if (TARGET_H8300SX)
322 error ("-msx is not supported in coff");
323 target_flags |= MASK_H8300S;
325 #endif
327 if (TARGET_H8300)
329 cpu_type = (int) CPU_H8300;
330 h8_reg_names = names_big;
332 else
334 /* For this we treat the H8/300H and H8S the same. */
335 cpu_type = (int) CPU_H8300H;
336 h8_reg_names = names_extended;
338 h8_push_op = h8_push_ops[cpu_type];
339 h8_pop_op = h8_pop_ops[cpu_type];
340 h8_mov_op = h8_mov_ops[cpu_type];
342 if (!TARGET_H8300S && TARGET_MAC)
344 error ("-ms2600 is used without -ms");
345 target_flags |= MASK_H8300S_1;
348 if (TARGET_H8300 && TARGET_NORMAL_MODE)
350 error ("-mn is used without -mh or -ms or -msx");
351 target_flags ^= MASK_NORMAL_MODE;
354 if (! TARGET_H8300S && TARGET_EXR)
356 error ("-mexr is used without -ms");
357 target_flags |= MASK_H8300S_1;
360 if (TARGET_H8300 && TARGET_INT32)
362 error ("-mint32 is not supported for H8300 and H8300L targets");
363 target_flags ^= MASK_INT32;
366 if ((!TARGET_H8300S && TARGET_EXR) && (!TARGET_H8300SX && TARGET_EXR))
368 error ("-mexr is used without -ms or -msx");
369 target_flags |= MASK_H8300S_1;
372 if ((!TARGET_H8300S && TARGET_NEXR) && (!TARGET_H8300SX && TARGET_NEXR))
374 warning (OPT_mno_exr, "-mno-exr valid only with -ms or -msx \
375 - Option ignored!");
378 /* Some of the shifts are optimized for speed by default.
379 See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
380 If optimizing for size, change shift_alg for those shift to
381 SHIFT_LOOP. */
382 if (optimize_size)
384 /* H8/300 */
385 shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
386 shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
387 shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
388 shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
390 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
391 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
393 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
394 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
396 /* H8/300H */
397 shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
398 shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
400 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
401 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
403 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
404 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
405 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
406 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
408 /* H8S */
409 shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
412 /* Work out a value for MOVE_RATIO. */
413 if (!TARGET_H8300SX)
415 /* Memory-memory moves are quite expensive without the
416 h8sx instructions. */
417 h8300_move_ratio = 3;
419 else if (flag_omit_frame_pointer)
421 /* movmd sequences are fairly cheap when er6 isn't fixed. They can
422 sometimes be as short as two individual memory-to-memory moves,
423 but since they use all the call-saved registers, it seems better
424 to allow up to three moves here. */
425 h8300_move_ratio = 4;
427 else if (optimize_size)
429 /* In this case we don't use movmd sequences since they tend
430 to be longer than calls to memcpy(). Memory-to-memory
431 moves are cheaper than for !TARGET_H8300SX, so it makes
432 sense to have a slightly higher threshold. */
433 h8300_move_ratio = 4;
435 else
437 /* We use movmd sequences for some moves since it can be quicker
438 than calling memcpy(). The sequences will need to save and
439 restore er6 though, so bump up the cost. */
440 h8300_move_ratio = 6;
443 /* This target defaults to strict volatile bitfields. */
444 if (flag_strict_volatile_bitfields < 0 && abi_version_at_least(2))
445 flag_strict_volatile_bitfields = 1;
448 /* Return the byte register name for a register rtx X. B should be 0
449 if you want a lower byte register. B should be 1 if you want an
450 upper byte register. */
452 static const char *
453 byte_reg (rtx x, int b)
455 static const char *const names_small[] = {
456 "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
457 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
460 gcc_assert (REG_P (x));
462 return names_small[REGNO (x) * 2 + b];
465 /* REGNO must be saved/restored across calls if this macro is true. */
467 #define WORD_REG_USED(regno) \
468 (regno < SP_REG \
469 /* No need to save registers if this function will not return. */ \
470 && ! TREE_THIS_VOLATILE (current_function_decl) \
471 && (h8300_saveall_function_p (current_function_decl) \
472 /* Save any call saved register that was used. */ \
473 || (df_regs_ever_live_p (regno) && !call_used_regs[regno]) \
474 /* Save the frame pointer if it was used. */ \
475 || (regno == HARD_FRAME_POINTER_REGNUM && df_regs_ever_live_p (regno)) \
476 /* Save any register used in an interrupt handler. */ \
477 || (h8300_current_function_interrupt_function_p () \
478 && df_regs_ever_live_p (regno)) \
479 /* Save call clobbered registers in non-leaf interrupt \
480 handlers. */ \
481 || (h8300_current_function_interrupt_function_p () \
482 && call_used_regs[regno] \
483 && !crtl->is_leaf)))
485 /* We use this to wrap all emitted insns in the prologue. */
486 static rtx
487 F (rtx x, bool set_it)
489 if (set_it)
490 RTX_FRAME_RELATED_P (x) = 1;
491 return x;
494 /* Mark all the subexpressions of the PARALLEL rtx PAR as
495 frame-related. Return PAR.
497 dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
498 PARALLEL rtx other than the first if they do not have the
499 FRAME_RELATED flag set on them. */
500 static rtx
501 Fpa (rtx par)
503 int len = XVECLEN (par, 0);
504 int i;
506 for (i = 0; i < len; i++)
507 F (XVECEXP (par, 0, i), true);
509 return par;
512 /* Output assembly language to FILE for the operation OP with operand size
513 SIZE to adjust the stack pointer. */
515 static void
516 h8300_emit_stack_adjustment (int sign, HOST_WIDE_INT size, bool in_prologue)
518 /* If the frame size is 0, we don't have anything to do. */
519 if (size == 0)
520 return;
522 /* H8/300 cannot add/subtract a large constant with a single
523 instruction. If a temporary register is available, load the
524 constant to it and then do the addition. */
525 if (TARGET_H8300
526 && size > 4
527 && !h8300_current_function_interrupt_function_p ()
528 && !(cfun->static_chain_decl != NULL && sign < 0))
530 rtx r3 = gen_rtx_REG (Pmode, 3);
531 F (emit_insn (gen_movhi (r3, GEN_INT (sign * size))), in_prologue);
532 F (emit_insn (gen_addhi3 (stack_pointer_rtx,
533 stack_pointer_rtx, r3)), in_prologue);
535 else
537 /* The stack adjustment made here is further optimized by the
538 splitter. In case of H8/300, the splitter always splits the
539 addition emitted here to make the adjustment interrupt-safe.
540 FIXME: We don't always tag those, because we don't know what
541 the splitter will do. */
542 if (Pmode == HImode)
544 rtx x = emit_insn (gen_addhi3 (stack_pointer_rtx,
545 stack_pointer_rtx, GEN_INT (sign * size)));
546 if (size < 4)
547 F (x, in_prologue);
549 else
550 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
551 stack_pointer_rtx, GEN_INT (sign * size))), in_prologue);
555 /* Round up frame size SIZE. */
557 static HOST_WIDE_INT
558 round_frame_size (HOST_WIDE_INT size)
560 return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
561 & -STACK_BOUNDARY / BITS_PER_UNIT);
564 /* Compute which registers to push/pop.
565 Return a bit vector of registers. */
567 static unsigned int
568 compute_saved_regs (void)
570 unsigned int saved_regs = 0;
571 int regno;
573 /* Construct a bit vector of registers to be pushed/popped. */
574 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
576 if (WORD_REG_USED (regno))
577 saved_regs |= 1 << regno;
580 /* Don't push/pop the frame pointer as it is treated separately. */
581 if (frame_pointer_needed)
582 saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
584 return saved_regs;
587 /* Emit an insn to push register RN. */
589 static rtx
590 push (int rn)
592 rtx reg = gen_rtx_REG (word_mode, rn);
593 rtx x;
595 if (TARGET_H8300)
596 x = gen_push_h8300 (reg);
597 else if (!TARGET_NORMAL_MODE)
598 x = gen_push_h8300hs_advanced (reg);
599 else
600 x = gen_push_h8300hs_normal (reg);
601 x = F (emit_insn (x), true);
602 add_reg_note (x, REG_INC, stack_pointer_rtx);
603 return x;
606 /* Emit an insn to pop register RN. */
608 static rtx
609 pop (int rn)
611 rtx reg = gen_rtx_REG (word_mode, rn);
612 rtx x;
614 if (TARGET_H8300)
615 x = gen_pop_h8300 (reg);
616 else if (!TARGET_NORMAL_MODE)
617 x = gen_pop_h8300hs_advanced (reg);
618 else
619 x = gen_pop_h8300hs_normal (reg);
620 x = emit_insn (x);
621 add_reg_note (x, REG_INC, stack_pointer_rtx);
622 return x;
625 /* Emit an instruction to push or pop NREGS consecutive registers
626 starting at register REGNO. POP_P selects a pop rather than a
627 push and RETURN_P is true if the instruction should return.
629 It must be possible to do the requested operation in a single
630 instruction. If NREGS == 1 && !RETURN_P, use a normal push
631 or pop insn. Otherwise emit a parallel of the form:
633 (parallel
634 [(return) ;; if RETURN_P
635 (save or restore REGNO)
636 (save or restore REGNO + 1)
638 (save or restore REGNO + NREGS - 1)
639 (set sp (plus sp (const_int adjust)))] */
641 static void
642 h8300_push_pop (int regno, int nregs, bool pop_p, bool return_p)
644 int i, j;
645 rtvec vec;
646 rtx sp, offset, x;
648 /* See whether we can use a simple push or pop. */
649 if (!return_p && nregs == 1)
651 if (pop_p)
652 pop (regno);
653 else
654 push (regno);
655 return;
658 /* We need one element for the return insn, if present, one for each
659 register, and one for stack adjustment. */
660 vec = rtvec_alloc ((return_p ? 1 : 0) + nregs + 1);
661 sp = stack_pointer_rtx;
662 i = 0;
664 /* Add the return instruction. */
665 if (return_p)
667 RTVEC_ELT (vec, i) = ret_rtx;
668 i++;
671 /* Add the register moves. */
672 for (j = 0; j < nregs; j++)
674 rtx lhs, rhs;
676 if (pop_p)
678 /* Register REGNO + NREGS - 1 is popped first. Before the
679 stack adjustment, its slot is at address @sp. */
680 lhs = gen_rtx_REG (SImode, regno + j);
681 rhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp,
682 (nregs - j - 1) * 4));
684 else
686 /* Register REGNO is pushed first and will be stored at @(-4,sp). */
687 lhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp, (j + 1) * -4));
688 rhs = gen_rtx_REG (SImode, regno + j);
690 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, lhs, rhs);
693 /* Add the stack adjustment. */
694 offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
695 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, sp,
696 gen_rtx_PLUS (Pmode, sp, offset));
698 x = gen_rtx_PARALLEL (VOIDmode, vec);
699 if (!pop_p)
700 x = Fpa (x);
702 if (return_p)
703 emit_jump_insn (x);
704 else
705 emit_insn (x);
708 /* Return true if X has the value sp + OFFSET. */
710 static int
711 h8300_stack_offset_p (rtx x, int offset)
713 if (offset == 0)
714 return x == stack_pointer_rtx;
716 return (GET_CODE (x) == PLUS
717 && XEXP (x, 0) == stack_pointer_rtx
718 && GET_CODE (XEXP (x, 1)) == CONST_INT
719 && INTVAL (XEXP (x, 1)) == offset);
722 /* A subroutine of h8300_ldm_stm_parallel. X is one pattern in
723 something that may be an ldm or stm instruction. If it fits
724 the required template, return the register it loads or stores,
725 otherwise return -1.
727 LOAD_P is true if X should be a load, false if it should be a store.
728 NREGS is the number of registers that the whole instruction is expected
729 to load or store. INDEX is the index of the register that X should
730 load or store, relative to the lowest-numbered register. */
732 static int
733 h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
735 int regindex, memindex, offset;
737 if (load_p)
738 regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
739 else
740 memindex = 0, regindex = 1, offset = (index + 1) * -4;
742 if (GET_CODE (x) == SET
743 && GET_CODE (XEXP (x, regindex)) == REG
744 && GET_CODE (XEXP (x, memindex)) == MEM
745 && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
746 return REGNO (XEXP (x, regindex));
748 return -1;
751 /* Return true if the elements of VEC starting at FIRST describe an
752 ldm or stm instruction (LOAD_P says which). */
755 h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
757 rtx last;
758 int nregs, i, regno, adjust;
760 /* There must be a stack adjustment, a register move, and at least one
761 other operation (a return or another register move). */
762 if (GET_NUM_ELEM (vec) < 3)
763 return false;
765 /* Get the range of registers to be pushed or popped. */
766 nregs = GET_NUM_ELEM (vec) - first - 1;
767 regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
769 /* Check that the call to h8300_ldm_stm_regno succeeded and
770 that we're only dealing with GPRs. */
771 if (regno < 0 || regno + nregs > 8)
772 return false;
774 /* 2-register h8s instructions must start with an even-numbered register.
775 3- and 4-register instructions must start with er0 or er4. */
776 if (!TARGET_H8300SX)
778 if ((regno & 1) != 0)
779 return false;
780 if (nregs > 2 && (regno & 3) != 0)
781 return false;
784 /* Check the other loads or stores. */
785 for (i = 1; i < nregs; i++)
786 if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
787 != regno + i)
788 return false;
790 /* Check the stack adjustment. */
791 last = RTVEC_ELT (vec, first + nregs);
792 adjust = (load_p ? nregs : -nregs) * 4;
793 return (GET_CODE (last) == SET
794 && SET_DEST (last) == stack_pointer_rtx
795 && h8300_stack_offset_p (SET_SRC (last), adjust));
798 /* This is what the stack looks like after the prolog of
799 a function with a frame has been set up:
801 <args>
803 FP <- fp
804 <locals>
805 <saved registers> <- sp
807 This is what the stack looks like after the prolog of
808 a function which doesn't have a frame:
810 <args>
812 <locals>
813 <saved registers> <- sp
816 /* Generate RTL code for the function prologue. */
818 void
819 h8300_expand_prologue (void)
821 int regno;
822 int saved_regs;
823 int n_regs;
825 /* If the current function has the OS_Task attribute set, then
826 we have a naked prologue. */
827 if (h8300_os_task_function_p (current_function_decl))
828 return;
830 if (h8300_monitor_function_p (current_function_decl))
831 /* The monitor function act as normal functions, which means it
832 can accept parameters and return values. In addition to this,
833 interrupts are masked in prologue and return with "rte" in epilogue. */
834 emit_insn (gen_monitor_prologue ());
836 if (frame_pointer_needed)
838 /* Push fp. */
839 push (HARD_FRAME_POINTER_REGNUM);
840 F (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx), true);
843 /* Push the rest of the registers in ascending order. */
844 saved_regs = compute_saved_regs ();
845 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
847 n_regs = 1;
848 if (saved_regs & (1 << regno))
850 if (TARGET_H8300S)
852 /* See how many registers we can push at the same time. */
853 if ((!TARGET_H8300SX || (regno & 3) == 0)
854 && ((saved_regs >> regno) & 0x0f) == 0x0f)
855 n_regs = 4;
857 else if ((!TARGET_H8300SX || (regno & 3) == 0)
858 && ((saved_regs >> regno) & 0x07) == 0x07)
859 n_regs = 3;
861 else if ((!TARGET_H8300SX || (regno & 1) == 0)
862 && ((saved_regs >> regno) & 0x03) == 0x03)
863 n_regs = 2;
866 h8300_push_pop (regno, n_regs, false, false);
870 /* Leave room for locals. */
871 h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()), true);
874 /* Return nonzero if we can use "rts" for the function currently being
875 compiled. */
878 h8300_can_use_return_insn_p (void)
880 return (reload_completed
881 && !frame_pointer_needed
882 && get_frame_size () == 0
883 && compute_saved_regs () == 0);
886 /* Generate RTL code for the function epilogue. */
888 void
889 h8300_expand_epilogue (void)
891 int regno;
892 int saved_regs;
893 int n_regs;
894 HOST_WIDE_INT frame_size;
895 bool returned_p;
897 if (h8300_os_task_function_p (current_function_decl))
898 /* OS_Task epilogues are nearly naked -- they just have an
899 rts instruction. */
900 return;
902 frame_size = round_frame_size (get_frame_size ());
903 returned_p = false;
905 /* Deallocate locals. */
906 h8300_emit_stack_adjustment (1, frame_size, false);
908 /* Pop the saved registers in descending order. */
909 saved_regs = compute_saved_regs ();
910 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
912 n_regs = 1;
913 if (saved_regs & (1 << regno))
915 if (TARGET_H8300S)
917 /* See how many registers we can pop at the same time. */
918 if ((TARGET_H8300SX || (regno & 3) == 3)
919 && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
920 n_regs = 4;
922 else if ((TARGET_H8300SX || (regno & 3) == 2)
923 && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
924 n_regs = 3;
926 else if ((TARGET_H8300SX || (regno & 1) == 1)
927 && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
928 n_regs = 2;
931 /* See if this pop would be the last insn before the return.
932 If so, use rte/l or rts/l instead of pop or ldm.l. */
933 if (TARGET_H8300SX
934 && !frame_pointer_needed
935 && frame_size == 0
936 && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
937 returned_p = true;
939 h8300_push_pop (regno - n_regs + 1, n_regs, true, returned_p);
943 /* Pop frame pointer if we had one. */
944 if (frame_pointer_needed)
946 if (TARGET_H8300SX)
947 returned_p = true;
948 h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, true, returned_p);
951 if (!returned_p)
952 emit_jump_insn (ret_rtx);
955 /* Return nonzero if the current function is an interrupt
956 function. */
959 h8300_current_function_interrupt_function_p (void)
961 return (h8300_interrupt_function_p (current_function_decl));
965 h8300_current_function_monitor_function_p ()
967 return (h8300_monitor_function_p (current_function_decl));
970 /* Output assembly code for the start of the file. */
972 static void
973 h8300_file_start (void)
975 default_file_start ();
977 if (TARGET_H8300H)
978 fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
979 else if (TARGET_H8300SX)
980 fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
981 else if (TARGET_H8300S)
982 fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
985 /* Output assembly language code for the end of file. */
987 static void
988 h8300_file_end (void)
990 fputs ("\t.end\n", asm_out_file);
993 /* Split an add of a small constant into two adds/subs insns.
995 If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
996 instead of adds/subs. */
998 void
999 split_adds_subs (enum machine_mode mode, rtx *operands)
1001 HOST_WIDE_INT val = INTVAL (operands[1]);
1002 rtx reg = operands[0];
1003 HOST_WIDE_INT sign = 1;
1004 HOST_WIDE_INT amount;
1005 rtx (*gen_add) (rtx, rtx, rtx);
1007 /* Force VAL to be positive so that we do not have to consider the
1008 sign. */
1009 if (val < 0)
1011 val = -val;
1012 sign = -1;
1015 switch (mode)
1017 case HImode:
1018 gen_add = gen_addhi3;
1019 break;
1021 case SImode:
1022 gen_add = gen_addsi3;
1023 break;
1025 default:
1026 gcc_unreachable ();
1029 /* Try different amounts in descending order. */
1030 for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
1031 amount > 0;
1032 amount /= 2)
1034 for (; val >= amount; val -= amount)
1035 emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
1038 return;
1041 /* Handle machine specific pragmas for compatibility with existing
1042 compilers for the H8/300.
1044 pragma saveall generates prologue/epilogue code which saves and
1045 restores all the registers on function entry.
1047 pragma interrupt saves and restores all registers, and exits with
1048 an rte instruction rather than an rts. A pointer to a function
1049 with this attribute may be safely used in an interrupt vector. */
1051 void
1052 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1054 pragma_interrupt = 1;
1057 void
1058 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1060 pragma_saveall = 1;
1063 /* If the next function argument with MODE and TYPE is to be passed in
1064 a register, return a reg RTX for the hard register in which to pass
1065 the argument. CUM represents the state after the last argument.
1066 If the argument is to be pushed, NULL_RTX is returned.
1068 On the H8/300 all normal args are pushed, unless -mquickcall in which
1069 case the first 3 arguments are passed in registers. */
1071 static rtx
1072 h8300_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
1073 const_tree type, bool named)
1075 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1077 static const char *const hand_list[] = {
1078 "__main",
1079 "__cmpsi2",
1080 "__divhi3",
1081 "__modhi3",
1082 "__udivhi3",
1083 "__umodhi3",
1084 "__divsi3",
1085 "__modsi3",
1086 "__udivsi3",
1087 "__umodsi3",
1088 "__mulhi3",
1089 "__mulsi3",
1090 "__reg_memcpy",
1091 "__reg_memset",
1092 "__ucmpsi2",
1096 rtx result = NULL_RTX;
1097 const char *fname;
1098 int regpass = 0;
1100 /* Never pass unnamed arguments in registers. */
1101 if (!named)
1102 return NULL_RTX;
1104 /* Pass 3 regs worth of data in regs when user asked on the command line. */
1105 if (TARGET_QUICKCALL)
1106 regpass = 3;
1108 /* If calling hand written assembler, use 4 regs of args. */
1109 if (cum->libcall)
1111 const char * const *p;
1113 fname = XSTR (cum->libcall, 0);
1115 /* See if this libcall is one of the hand coded ones. */
1116 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1119 if (*p)
1120 regpass = 4;
1123 if (regpass)
1125 int size;
1127 if (mode == BLKmode)
1128 size = int_size_in_bytes (type);
1129 else
1130 size = GET_MODE_SIZE (mode);
1132 if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1133 && cum->nbytes / UNITS_PER_WORD <= 3)
1134 result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1137 return result;
1140 /* Update the data in CUM to advance over an argument
1141 of mode MODE and data type TYPE.
1142 (TYPE is null for libcalls where that information may not be available.) */
1144 static void
1145 h8300_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
1146 const_tree type, bool named ATTRIBUTE_UNUSED)
1148 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1150 cum->nbytes += (mode != BLKmode
1151 ? (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD
1152 : (int_size_in_bytes (type) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD);
1156 /* Implements TARGET_REGISTER_MOVE_COST.
1158 Any SI register-to-register move may need to be reloaded,
1159 so inmplement h8300_register_move_cost to return > 2 so that reload never
1160 shortcuts. */
1162 static int
1163 h8300_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
1164 reg_class_t from, reg_class_t to)
1166 if (from == MAC_REGS || to == MAC_REG)
1167 return 6;
1168 else
1169 return 3;
1172 /* Compute the cost of an and insn. */
1174 static int
1175 h8300_and_costs (rtx x)
1177 rtx operands[4];
1179 if (GET_MODE (x) == QImode)
1180 return 1;
1182 if (GET_MODE (x) != HImode
1183 && GET_MODE (x) != SImode)
1184 return 100;
1186 operands[0] = NULL;
1187 operands[1] = XEXP (x, 0);
1188 operands[2] = XEXP (x, 1);
1189 operands[3] = x;
1190 return compute_logical_op_length (GET_MODE (x), operands) / 2;
1193 /* Compute the cost of a shift insn. */
1195 static int
1196 h8300_shift_costs (rtx x)
1198 rtx operands[4];
1200 if (GET_MODE (x) != QImode
1201 && GET_MODE (x) != HImode
1202 && GET_MODE (x) != SImode)
1203 return 100;
1205 operands[0] = NULL;
1206 operands[1] = NULL;
1207 operands[2] = XEXP (x, 1);
1208 operands[3] = x;
1209 return compute_a_shift_length (NULL, operands) / 2;
1212 /* Worker function for TARGET_RTX_COSTS. */
1214 static bool
1215 h8300_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
1216 int *total, bool speed)
1218 if (TARGET_H8300SX && outer_code == MEM)
1220 /* Estimate the number of execution states needed to calculate
1221 the address. */
1222 if (register_operand (x, VOIDmode)
1223 || GET_CODE (x) == POST_INC
1224 || GET_CODE (x) == POST_DEC
1225 || CONSTANT_P (x))
1226 *total = 0;
1227 else
1228 *total = COSTS_N_INSNS (1);
1229 return true;
1232 switch (code)
1234 case CONST_INT:
1236 HOST_WIDE_INT n = INTVAL (x);
1238 if (TARGET_H8300SX)
1240 /* Constant operands need the same number of processor
1241 states as register operands. Although we could try to
1242 use a size-based cost for !speed, the lack of
1243 of a mode makes the results very unpredictable. */
1244 *total = 0;
1245 return true;
1247 if (-4 <= n && n <= 4)
1249 switch ((int) n)
1251 case 0:
1252 *total = 0;
1253 return true;
1254 case 1:
1255 case 2:
1256 case -1:
1257 case -2:
1258 *total = 0 + (outer_code == SET);
1259 return true;
1260 case 4:
1261 case -4:
1262 if (TARGET_H8300H || TARGET_H8300S)
1263 *total = 0 + (outer_code == SET);
1264 else
1265 *total = 1;
1266 return true;
1269 *total = 1;
1270 return true;
1273 case CONST:
1274 case LABEL_REF:
1275 case SYMBOL_REF:
1276 if (TARGET_H8300SX)
1278 /* See comment for CONST_INT. */
1279 *total = 0;
1280 return true;
1282 *total = 3;
1283 return true;
1285 case CONST_DOUBLE:
1286 *total = 20;
1287 return true;
1289 case COMPARE:
1290 if (XEXP (x, 1) == const0_rtx)
1291 *total = 0;
1292 return false;
1294 case AND:
1295 if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1296 || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1297 return false;
1298 *total = COSTS_N_INSNS (h8300_and_costs (x));
1299 return true;
1301 /* We say that MOD and DIV are so expensive because otherwise we'll
1302 generate some really horrible code for division of a power of two. */
1303 case MOD:
1304 case DIV:
1305 case UMOD:
1306 case UDIV:
1307 if (TARGET_H8300SX)
1308 switch (GET_MODE (x))
1310 case QImode:
1311 case HImode:
1312 *total = COSTS_N_INSNS (!speed ? 4 : 10);
1313 return false;
1315 case SImode:
1316 *total = COSTS_N_INSNS (!speed ? 4 : 18);
1317 return false;
1319 default:
1320 break;
1322 *total = COSTS_N_INSNS (12);
1323 return true;
1325 case MULT:
1326 if (TARGET_H8300SX)
1327 switch (GET_MODE (x))
1329 case QImode:
1330 case HImode:
1331 *total = COSTS_N_INSNS (2);
1332 return false;
1334 case SImode:
1335 *total = COSTS_N_INSNS (5);
1336 return false;
1338 default:
1339 break;
1341 *total = COSTS_N_INSNS (4);
1342 return true;
1344 case ASHIFT:
1345 case ASHIFTRT:
1346 case LSHIFTRT:
1347 if (h8sx_binary_shift_operator (x, VOIDmode))
1349 *total = COSTS_N_INSNS (2);
1350 return false;
1352 else if (h8sx_unary_shift_operator (x, VOIDmode))
1354 *total = COSTS_N_INSNS (1);
1355 return false;
1357 *total = COSTS_N_INSNS (h8300_shift_costs (x));
1358 return true;
1360 case ROTATE:
1361 case ROTATERT:
1362 if (GET_MODE (x) == HImode)
1363 *total = 2;
1364 else
1365 *total = 8;
1366 return true;
1368 default:
1369 *total = COSTS_N_INSNS (1);
1370 return false;
1374 /* Documentation for the machine specific operand escapes:
1376 'E' like s but negative.
1377 'F' like t but negative.
1378 'G' constant just the negative
1379 'R' print operand as a byte:8 address if appropriate, else fall back to
1380 'X' handling.
1381 'S' print operand as a long word
1382 'T' print operand as a word
1383 'V' find the set bit, and print its number.
1384 'W' find the clear bit, and print its number.
1385 'X' print operand as a byte
1386 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1387 If this operand isn't a register, fall back to 'R' handling.
1388 'Z' print int & 7.
1389 'c' print the opcode corresponding to rtl
1390 'e' first word of 32-bit value - if reg, then least reg. if mem
1391 then least. if const then most sig word
1392 'f' second word of 32-bit value - if reg, then biggest reg. if mem
1393 then +2. if const then least sig word
1394 'j' print operand as condition code.
1395 'k' print operand as reverse condition code.
1396 'm' convert an integer operand to a size suffix (.b, .w or .l)
1397 'o' print an integer without a leading '#'
1398 's' print as low byte of 16-bit value
1399 't' print as high byte of 16-bit value
1400 'w' print as low byte of 32-bit value
1401 'x' print as 2nd byte of 32-bit value
1402 'y' print as 3rd byte of 32-bit value
1403 'z' print as msb of 32-bit value
1406 /* Return assembly language string which identifies a comparison type. */
1408 static const char *
1409 cond_string (enum rtx_code code)
1411 switch (code)
1413 case NE:
1414 return "ne";
1415 case EQ:
1416 return "eq";
1417 case GE:
1418 return "ge";
1419 case GT:
1420 return "gt";
1421 case LE:
1422 return "le";
1423 case LT:
1424 return "lt";
1425 case GEU:
1426 return "hs";
1427 case GTU:
1428 return "hi";
1429 case LEU:
1430 return "ls";
1431 case LTU:
1432 return "lo";
1433 default:
1434 gcc_unreachable ();
1438 /* Print operand X using operand code CODE to assembly language output file
1439 FILE. */
1441 static void
1442 h8300_print_operand (FILE *file, rtx x, int code)
1444 /* This is used for communication between codes V,W,Z and Y. */
1445 static int bitint;
1447 switch (code)
1449 case 'C':
1450 if (h8300_constant_length (x) == 2)
1451 fprintf (file, ":16");
1452 else
1453 fprintf (file, ":32");
1454 return;
1455 case 'E':
1456 switch (GET_CODE (x))
1458 case REG:
1459 fprintf (file, "%sl", names_big[REGNO (x)]);
1460 break;
1461 case CONST_INT:
1462 fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1463 break;
1464 default:
1465 gcc_unreachable ();
1467 break;
1468 case 'F':
1469 switch (GET_CODE (x))
1471 case REG:
1472 fprintf (file, "%sh", names_big[REGNO (x)]);
1473 break;
1474 case CONST_INT:
1475 fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1476 break;
1477 default:
1478 gcc_unreachable ();
1480 break;
1481 case 'G':
1482 gcc_assert (GET_CODE (x) == CONST_INT);
1483 fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1484 break;
1485 case 'S':
1486 if (GET_CODE (x) == REG)
1487 fprintf (file, "%s", names_extended[REGNO (x)]);
1488 else
1489 goto def;
1490 break;
1491 case 'T':
1492 if (GET_CODE (x) == REG)
1493 fprintf (file, "%s", names_big[REGNO (x)]);
1494 else
1495 goto def;
1496 break;
1497 case 'V':
1498 bitint = (INTVAL (x) & 0xffff);
1499 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1)
1500 bitint = exact_log2 (bitint & 0xff);
1501 else
1502 bitint = exact_log2 ((bitint >> 8) & 0xff);
1503 gcc_assert (bitint >= 0);
1504 fprintf (file, "#%d", bitint);
1505 break;
1506 case 'W':
1507 bitint = ((~INTVAL (x)) & 0xffff);
1508 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1 )
1509 bitint = exact_log2 (bitint & 0xff);
1510 else
1511 bitint = (exact_log2 ((bitint >> 8) & 0xff));
1512 gcc_assert (bitint >= 0);
1513 fprintf (file, "#%d", bitint);
1514 break;
1515 case 'R':
1516 case 'X':
1517 if (GET_CODE (x) == REG)
1518 fprintf (file, "%s", byte_reg (x, 0));
1519 else
1520 goto def;
1521 break;
1522 case 'Y':
1523 gcc_assert (bitint >= 0);
1524 if (GET_CODE (x) == REG)
1525 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1526 else
1527 h8300_print_operand (file, x, 'R');
1528 bitint = -1;
1529 break;
1530 case 'Z':
1531 bitint = INTVAL (x);
1532 fprintf (file, "#%d", bitint & 7);
1533 break;
1534 case 'c':
1535 switch (GET_CODE (x))
1537 case IOR:
1538 fprintf (file, "or");
1539 break;
1540 case XOR:
1541 fprintf (file, "xor");
1542 break;
1543 case AND:
1544 fprintf (file, "and");
1545 break;
1546 default:
1547 break;
1549 break;
1550 case 'e':
1551 switch (GET_CODE (x))
1553 case REG:
1554 if (TARGET_H8300)
1555 fprintf (file, "%s", names_big[REGNO (x)]);
1556 else
1557 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1558 break;
1559 case MEM:
1560 h8300_print_operand (file, x, 0);
1561 break;
1562 case CONST_INT:
1563 fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1564 break;
1565 case CONST_DOUBLE:
1567 long val;
1568 REAL_VALUE_TYPE rv;
1569 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1570 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1571 fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1572 break;
1574 default:
1575 gcc_unreachable ();
1576 break;
1578 break;
1579 case 'f':
1580 switch (GET_CODE (x))
1582 case REG:
1583 if (TARGET_H8300)
1584 fprintf (file, "%s", names_big[REGNO (x) + 1]);
1585 else
1586 fprintf (file, "%s", names_big[REGNO (x)]);
1587 break;
1588 case MEM:
1589 x = adjust_address (x, HImode, 2);
1590 h8300_print_operand (file, x, 0);
1591 break;
1592 case CONST_INT:
1593 fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1594 break;
1595 case CONST_DOUBLE:
1597 long val;
1598 REAL_VALUE_TYPE rv;
1599 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1600 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1601 fprintf (file, "#%ld", (val & 0xffff));
1602 break;
1604 default:
1605 gcc_unreachable ();
1607 break;
1608 case 'j':
1609 fputs (cond_string (GET_CODE (x)), file);
1610 break;
1611 case 'k':
1612 fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1613 break;
1614 case 'm':
1615 gcc_assert (GET_CODE (x) == CONST_INT);
1616 switch (INTVAL (x))
1618 case 1:
1619 fputs (".b", file);
1620 break;
1622 case 2:
1623 fputs (".w", file);
1624 break;
1626 case 4:
1627 fputs (".l", file);
1628 break;
1630 default:
1631 gcc_unreachable ();
1633 break;
1634 case 'o':
1635 h8300_print_operand_address (file, x);
1636 break;
1637 case 's':
1638 if (GET_CODE (x) == CONST_INT)
1639 fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1640 else
1641 fprintf (file, "%s", byte_reg (x, 0));
1642 break;
1643 case 't':
1644 if (GET_CODE (x) == CONST_INT)
1645 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1646 else
1647 fprintf (file, "%s", byte_reg (x, 1));
1648 break;
1649 case 'w':
1650 if (GET_CODE (x) == CONST_INT)
1651 fprintf (file, "#%ld", INTVAL (x) & 0xff);
1652 else
1653 fprintf (file, "%s",
1654 byte_reg (x, TARGET_H8300 ? 2 : 0));
1655 break;
1656 case 'x':
1657 if (GET_CODE (x) == CONST_INT)
1658 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1659 else
1660 fprintf (file, "%s",
1661 byte_reg (x, TARGET_H8300 ? 3 : 1));
1662 break;
1663 case 'y':
1664 if (GET_CODE (x) == CONST_INT)
1665 fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1666 else
1667 fprintf (file, "%s", byte_reg (x, 0));
1668 break;
1669 case 'z':
1670 if (GET_CODE (x) == CONST_INT)
1671 fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1672 else
1673 fprintf (file, "%s", byte_reg (x, 1));
1674 break;
1676 default:
1677 def:
1678 switch (GET_CODE (x))
1680 case REG:
1681 switch (GET_MODE (x))
1683 case QImode:
1684 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1685 fprintf (file, "%s", byte_reg (x, 0));
1686 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1687 fprintf (file, "%s", names_big[REGNO (x)]);
1688 #endif
1689 break;
1690 case HImode:
1691 fprintf (file, "%s", names_big[REGNO (x)]);
1692 break;
1693 case SImode:
1694 case SFmode:
1695 fprintf (file, "%s", names_extended[REGNO (x)]);
1696 break;
1697 default:
1698 gcc_unreachable ();
1700 break;
1702 case MEM:
1704 rtx addr = XEXP (x, 0);
1706 fprintf (file, "@");
1707 output_address (addr);
1709 /* Add a length suffix to constant addresses. Although this
1710 is often unnecessary, it helps to avoid ambiguity in the
1711 syntax of mova. If we wrote an insn like:
1713 mova/w.l @(1,@foo.b),er0
1715 then .b would be considered part of the symbol name.
1716 Adding a length after foo will avoid this. */
1717 if (CONSTANT_P (addr))
1718 switch (code)
1720 case 'R':
1721 /* Used for mov.b and bit operations. */
1722 if (h8300_eightbit_constant_address_p (addr))
1724 fprintf (file, ":8");
1725 break;
1728 /* Fall through. We should not get here if we are
1729 processing bit operations on H8/300 or H8/300H
1730 because 'U' constraint does not allow bit
1731 operations on the tiny area on these machines. */
1733 case 'X':
1734 case 'T':
1735 case 'S':
1736 if (h8300_constant_length (addr) == 2)
1737 fprintf (file, ":16");
1738 else
1739 fprintf (file, ":32");
1740 break;
1741 default:
1742 break;
1745 break;
1747 case CONST_INT:
1748 case SYMBOL_REF:
1749 case CONST:
1750 case LABEL_REF:
1751 fprintf (file, "#");
1752 h8300_print_operand_address (file, x);
1753 break;
1754 case CONST_DOUBLE:
1756 long val;
1757 REAL_VALUE_TYPE rv;
1758 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1759 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1760 fprintf (file, "#%ld", val);
1761 break;
1763 default:
1764 break;
1769 /* Implements TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
1771 static bool
1772 h8300_print_operand_punct_valid_p (unsigned char code)
1774 return (code == '#');
1777 /* Output assembly language output for the address ADDR to FILE. */
1779 static void
1780 h8300_print_operand_address (FILE *file, rtx addr)
1782 rtx index;
1783 int size;
1785 switch (GET_CODE (addr))
1787 case REG:
1788 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1789 break;
1791 case PRE_DEC:
1792 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1793 break;
1795 case POST_INC:
1796 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1797 break;
1799 case PRE_INC:
1800 fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1801 break;
1803 case POST_DEC:
1804 fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1805 break;
1807 case PLUS:
1808 fprintf (file, "(");
1810 index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1811 if (GET_CODE (index) == REG)
1813 /* reg,foo */
1814 h8300_print_operand_address (file, XEXP (addr, 1));
1815 fprintf (file, ",");
1816 switch (size)
1818 case 0:
1819 h8300_print_operand_address (file, index);
1820 break;
1822 case 1:
1823 h8300_print_operand (file, index, 'X');
1824 fputs (".b", file);
1825 break;
1827 case 2:
1828 h8300_print_operand (file, index, 'T');
1829 fputs (".w", file);
1830 break;
1832 case 4:
1833 h8300_print_operand (file, index, 'S');
1834 fputs (".l", file);
1835 break;
1837 /* h8300_print_operand_address (file, XEXP (addr, 0)); */
1839 else
1841 /* foo+k */
1842 h8300_print_operand_address (file, XEXP (addr, 0));
1843 fprintf (file, "+");
1844 h8300_print_operand_address (file, XEXP (addr, 1));
1846 fprintf (file, ")");
1847 break;
1849 case CONST_INT:
1851 /* Since the H8/300 only has 16-bit pointers, negative values are also
1852 those >= 32768. This happens for example with pointer minus a
1853 constant. We don't want to turn (char *p - 2) into
1854 (char *p + 65534) because loop unrolling can build upon this
1855 (IE: char *p + 131068). */
1856 int n = INTVAL (addr);
1857 if (TARGET_H8300)
1858 n = (int) (short) n;
1859 fprintf (file, "%d", n);
1860 break;
1863 default:
1864 output_addr_const (file, addr);
1865 break;
1869 /* Output all insn addresses and their sizes into the assembly language
1870 output file. This is helpful for debugging whether the length attributes
1871 in the md file are correct. This is not meant to be a user selectable
1872 option. */
1874 void
1875 final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
1876 int num_operands ATTRIBUTE_UNUSED)
1878 /* This holds the last insn address. */
1879 static int last_insn_address = 0;
1881 const int uid = INSN_UID (insn);
1883 if (TARGET_ADDRESSES)
1885 fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1886 INSN_ADDRESSES (uid) - last_insn_address);
1887 last_insn_address = INSN_ADDRESSES (uid);
1891 /* Prepare for an SI sized move. */
1894 h8300_expand_movsi (rtx operands[])
1896 rtx src = operands[1];
1897 rtx dst = operands[0];
1898 if (!reload_in_progress && !reload_completed)
1900 if (!register_operand (dst, GET_MODE (dst)))
1902 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1903 emit_move_insn (tmp, src);
1904 operands[1] = tmp;
1907 return 0;
1910 /* Given FROM and TO register numbers, say whether this elimination is allowed.
1911 Frame pointer elimination is automatically handled.
1913 For the h8300, if frame pointer elimination is being done, we would like to
1914 convert ap and rp into sp, not fp.
1916 All other eliminations are valid. */
1918 static bool
1919 h8300_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1921 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
1924 /* Conditionally modify register usage based on target flags. */
1926 static void
1927 h8300_conditional_register_usage (void)
1929 if (!TARGET_MAC)
1930 fixed_regs[MAC_REG] = call_used_regs[MAC_REG] = 1;
1933 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1934 Define the offset between two registers, one to be eliminated, and
1935 the other its replacement, at the start of a routine. */
1938 h8300_initial_elimination_offset (int from, int to)
1940 /* The number of bytes that the return address takes on the stack. */
1941 int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1943 /* The number of bytes that the saved frame pointer takes on the stack. */
1944 int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1946 /* The number of bytes that the saved registers, excluding the frame
1947 pointer, take on the stack. */
1948 int saved_regs_size = 0;
1950 /* The number of bytes that the locals takes on the stack. */
1951 int frame_size = round_frame_size (get_frame_size ());
1953 int regno;
1955 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1956 if (WORD_REG_USED (regno))
1957 saved_regs_size += UNITS_PER_WORD;
1959 /* Adjust saved_regs_size because the above loop took the frame
1960 pointer int account. */
1961 saved_regs_size -= fp_size;
1963 switch (to)
1965 case HARD_FRAME_POINTER_REGNUM:
1966 switch (from)
1968 case ARG_POINTER_REGNUM:
1969 return pc_size + fp_size;
1970 case RETURN_ADDRESS_POINTER_REGNUM:
1971 return fp_size;
1972 case FRAME_POINTER_REGNUM:
1973 return -saved_regs_size;
1974 default:
1975 gcc_unreachable ();
1977 break;
1978 case STACK_POINTER_REGNUM:
1979 switch (from)
1981 case ARG_POINTER_REGNUM:
1982 return pc_size + saved_regs_size + frame_size;
1983 case RETURN_ADDRESS_POINTER_REGNUM:
1984 return saved_regs_size + frame_size;
1985 case FRAME_POINTER_REGNUM:
1986 return frame_size;
1987 default:
1988 gcc_unreachable ();
1990 break;
1991 default:
1992 gcc_unreachable ();
1994 gcc_unreachable ();
1997 /* Worker function for RETURN_ADDR_RTX. */
2000 h8300_return_addr_rtx (int count, rtx frame)
2002 rtx ret;
2004 if (count == 0)
2005 ret = gen_rtx_MEM (Pmode,
2006 gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
2007 else if (flag_omit_frame_pointer)
2008 return (rtx) 0;
2009 else
2010 ret = gen_rtx_MEM (Pmode,
2011 memory_address (Pmode,
2012 plus_constant (Pmode, frame,
2013 UNITS_PER_WORD)));
2014 set_mem_alias_set (ret, get_frame_alias_set ());
2015 return ret;
2018 /* Update the condition code from the insn. */
2020 void
2021 notice_update_cc (rtx body, rtx insn)
2023 rtx set;
2025 switch (get_attr_cc (insn))
2027 case CC_NONE:
2028 /* Insn does not affect CC at all. */
2029 break;
2031 case CC_NONE_0HIT:
2032 /* Insn does not change CC, but the 0'th operand has been changed. */
2033 if (cc_status.value1 != 0
2034 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2035 cc_status.value1 = 0;
2036 if (cc_status.value2 != 0
2037 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
2038 cc_status.value2 = 0;
2039 break;
2041 case CC_SET_ZN:
2042 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2043 The V flag is unusable. The C flag may or may not be known but
2044 that's ok because alter_cond will change tests to use EQ/NE. */
2045 CC_STATUS_INIT;
2046 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2047 set = single_set (insn);
2048 cc_status.value1 = SET_SRC (set);
2049 if (SET_DEST (set) != cc0_rtx)
2050 cc_status.value2 = SET_DEST (set);
2051 break;
2053 case CC_SET_ZNV:
2054 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2055 The C flag may or may not be known but that's ok because
2056 alter_cond will change tests to use EQ/NE. */
2057 CC_STATUS_INIT;
2058 cc_status.flags |= CC_NO_CARRY;
2059 set = single_set (insn);
2060 cc_status.value1 = SET_SRC (set);
2061 if (SET_DEST (set) != cc0_rtx)
2063 /* If the destination is STRICT_LOW_PART, strip off
2064 STRICT_LOW_PART. */
2065 if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
2066 cc_status.value2 = XEXP (SET_DEST (set), 0);
2067 else
2068 cc_status.value2 = SET_DEST (set);
2070 break;
2072 case CC_COMPARE:
2073 /* The insn is a compare instruction. */
2074 CC_STATUS_INIT;
2075 cc_status.value1 = SET_SRC (body);
2076 break;
2078 case CC_CLOBBER:
2079 /* Insn doesn't leave CC in a usable state. */
2080 CC_STATUS_INIT;
2081 break;
2085 /* Given that X occurs in an address of the form (plus X constant),
2086 return the part of X that is expected to be a register. There are
2087 four kinds of addressing mode to recognize:
2089 @(dd,Rn)
2090 @(dd,RnL.b)
2091 @(dd,Rn.w)
2092 @(dd,ERn.l)
2094 If SIZE is nonnull, and the address is one of the last three forms,
2095 set *SIZE to the index multiplication factor. Set it to 0 for
2096 plain @(dd,Rn) addresses.
2098 MODE is the mode of the value being accessed. It can be VOIDmode
2099 if the address is known to be valid, but its mode is unknown. */
2101 static rtx
2102 h8300_get_index (rtx x, enum machine_mode mode, int *size)
2104 int dummy, factor;
2106 if (size == 0)
2107 size = &dummy;
2109 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
2110 if (TARGET_H8300SX
2111 && factor <= 4
2112 && (mode == VOIDmode
2113 || GET_MODE_CLASS (mode) == MODE_INT
2114 || GET_MODE_CLASS (mode) == MODE_FLOAT))
2116 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
2118 /* When accessing byte-sized values, the index can be
2119 a zero-extended QImode or HImode register. */
2120 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
2121 return XEXP (x, 0);
2123 else
2125 /* We're looking for addresses of the form:
2127 (mult X I)
2128 or (mult (zero_extend X) I)
2130 where I is the size of the operand being accessed.
2131 The canonical form of the second expression is:
2133 (and (mult (subreg X) I) J)
2135 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2136 rtx index;
2138 if (GET_CODE (x) == AND
2139 && GET_CODE (XEXP (x, 1)) == CONST_INT
2140 && (factor == 0
2141 || INTVAL (XEXP (x, 1)) == 0xff * factor
2142 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2144 index = XEXP (x, 0);
2145 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2147 else
2149 index = x;
2150 *size = 4;
2153 if (GET_CODE (index) == MULT
2154 && GET_CODE (XEXP (index, 1)) == CONST_INT
2155 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2156 return XEXP (index, 0);
2159 *size = 0;
2160 return x;
2163 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
2165 On the H8/300, the predecrement and postincrement address depend thus
2166 (the amount of decrement or increment being the length of the operand). */
2168 static bool
2169 h8300_mode_dependent_address_p (const_rtx addr,
2170 addr_space_t as ATTRIBUTE_UNUSED)
2172 if (GET_CODE (addr) == PLUS
2173 && h8300_get_index (XEXP (addr, 0), VOIDmode, 0) != XEXP (addr, 0))
2174 return true;
2176 return false;
2179 static const h8300_length_table addb_length_table =
2181 /* #xx Rs @aa @Rs @xx */
2182 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2183 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2184 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2185 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2188 static const h8300_length_table addw_length_table =
2190 /* #xx Rs @aa @Rs @xx */
2191 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2192 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2193 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2194 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2197 static const h8300_length_table addl_length_table =
2199 /* #xx Rs @aa @Rs @xx */
2200 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2201 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2202 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2203 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2206 #define logicb_length_table addb_length_table
2207 #define logicw_length_table addw_length_table
2209 static const h8300_length_table logicl_length_table =
2211 /* #xx Rs @aa @Rs @xx */
2212 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2213 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2214 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2215 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2218 static const h8300_length_table movb_length_table =
2220 /* #xx Rs @aa @Rs @xx */
2221 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2222 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2223 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2224 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2227 #define movw_length_table movb_length_table
2229 static const h8300_length_table movl_length_table =
2231 /* #xx Rs @aa @Rs @xx */
2232 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2233 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2234 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2235 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2238 /* Return the size of the given address or displacement constant. */
2240 static unsigned int
2241 h8300_constant_length (rtx constant)
2243 /* Check for (@d:16,Reg). */
2244 if (GET_CODE (constant) == CONST_INT
2245 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2246 return 2;
2248 /* Check for (@d:16,Reg) in cases where the displacement is
2249 an absolute address. */
2250 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2251 return 2;
2253 return 4;
2256 /* Return the size of a displacement field in address ADDR, which should
2257 have the form (plus X constant). SIZE is the number of bytes being
2258 accessed. */
2260 static unsigned int
2261 h8300_displacement_length (rtx addr, int size)
2263 rtx offset;
2265 offset = XEXP (addr, 1);
2267 /* Check for @(d:2,Reg). */
2268 if (register_operand (XEXP (addr, 0), VOIDmode)
2269 && GET_CODE (offset) == CONST_INT
2270 && (INTVAL (offset) == size
2271 || INTVAL (offset) == size * 2
2272 || INTVAL (offset) == size * 3))
2273 return 0;
2275 return h8300_constant_length (offset);
2278 /* Store the class of operand OP in *OPCLASS and return the length of any
2279 extra operand fields. SIZE is the number of bytes in OP. OPCLASS
2280 can be null if only the length is needed. */
2282 static unsigned int
2283 h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
2285 enum h8300_operand_class dummy;
2287 if (opclass == 0)
2288 opclass = &dummy;
2290 if (CONSTANT_P (op))
2292 *opclass = H8OP_IMMEDIATE;
2294 /* Byte-sized immediates are stored in the opcode fields. */
2295 if (size == 1)
2296 return 0;
2298 /* If this is a 32-bit instruction, see whether the constant
2299 will fit into a 16-bit immediate field. */
2300 if (TARGET_H8300SX
2301 && size == 4
2302 && GET_CODE (op) == CONST_INT
2303 && IN_RANGE (INTVAL (op), 0, 0xffff))
2304 return 2;
2306 return size;
2308 else if (GET_CODE (op) == MEM)
2310 op = XEXP (op, 0);
2311 if (CONSTANT_P (op))
2313 *opclass = H8OP_MEM_ABSOLUTE;
2314 return h8300_constant_length (op);
2316 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2318 *opclass = H8OP_MEM_COMPLEX;
2319 return h8300_displacement_length (op, size);
2321 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2323 *opclass = H8OP_MEM_COMPLEX;
2324 return 0;
2326 else if (register_operand (op, VOIDmode))
2328 *opclass = H8OP_MEM_BASE;
2329 return 0;
2332 gcc_assert (register_operand (op, VOIDmode));
2333 *opclass = H8OP_REGISTER;
2334 return 0;
2337 /* Return the length of the instruction described by TABLE given that
2338 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2339 and OP2 must be an h8300_src_operand. */
2341 static unsigned int
2342 h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2344 enum h8300_operand_class op1_class, op2_class;
2345 unsigned int size, immediate_length;
2347 size = GET_MODE_SIZE (GET_MODE (op1));
2348 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2349 + h8300_classify_operand (op2, size, &op2_class));
2350 return immediate_length + (*table)[op1_class - 1][op2_class];
2353 /* Return the length of a unary instruction such as neg or not given that
2354 its operand is OP. */
2356 unsigned int
2357 h8300_unary_length (rtx op)
2359 enum h8300_operand_class opclass;
2360 unsigned int size, operand_length;
2362 size = GET_MODE_SIZE (GET_MODE (op));
2363 operand_length = h8300_classify_operand (op, size, &opclass);
2364 switch (opclass)
2366 case H8OP_REGISTER:
2367 return 2;
2369 case H8OP_MEM_BASE:
2370 return (size == 4 ? 6 : 4);
2372 case H8OP_MEM_ABSOLUTE:
2373 return operand_length + (size == 4 ? 6 : 4);
2375 case H8OP_MEM_COMPLEX:
2376 return operand_length + 6;
2378 default:
2379 gcc_unreachable ();
2383 /* Likewise short immediate instructions such as add.w #xx:3,OP. */
2385 static unsigned int
2386 h8300_short_immediate_length (rtx op)
2388 enum h8300_operand_class opclass;
2389 unsigned int size, operand_length;
2391 size = GET_MODE_SIZE (GET_MODE (op));
2392 operand_length = h8300_classify_operand (op, size, &opclass);
2394 switch (opclass)
2396 case H8OP_REGISTER:
2397 return 2;
2399 case H8OP_MEM_BASE:
2400 case H8OP_MEM_ABSOLUTE:
2401 case H8OP_MEM_COMPLEX:
2402 return 4 + operand_length;
2404 default:
2405 gcc_unreachable ();
2409 /* Likewise bitfield load and store instructions. */
2411 static unsigned int
2412 h8300_bitfield_length (rtx op, rtx op2)
2414 enum h8300_operand_class opclass;
2415 unsigned int size, operand_length;
2417 if (GET_CODE (op) == REG)
2418 op = op2;
2419 gcc_assert (GET_CODE (op) != REG);
2421 size = GET_MODE_SIZE (GET_MODE (op));
2422 operand_length = h8300_classify_operand (op, size, &opclass);
2424 switch (opclass)
2426 case H8OP_MEM_BASE:
2427 case H8OP_MEM_ABSOLUTE:
2428 case H8OP_MEM_COMPLEX:
2429 return 4 + operand_length;
2431 default:
2432 gcc_unreachable ();
2436 /* Calculate the length of general binary instruction INSN using TABLE. */
2438 static unsigned int
2439 h8300_binary_length (rtx insn, const h8300_length_table *table)
2441 rtx set;
2443 set = single_set (insn);
2444 gcc_assert (set);
2446 if (BINARY_P (SET_SRC (set)))
2447 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2448 XEXP (SET_SRC (set), 1), table);
2449 else
2451 gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2452 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2453 XEXP (XEXP (SET_SRC (set), 1), 1),
2454 table);
2458 /* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2459 memory reference and either (1) it has the form @(d:16,Rn) or
2460 (2) its address has the code given by INC_CODE. */
2462 static bool
2463 h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
2465 rtx addr;
2466 unsigned int size;
2468 if (GET_CODE (op) != MEM)
2469 return false;
2471 addr = XEXP (op, 0);
2472 size = GET_MODE_SIZE (GET_MODE (op));
2473 if (size != 1 && size != 2)
2474 return false;
2476 return (GET_CODE (addr) == inc_code
2477 || (GET_CODE (addr) == PLUS
2478 && GET_CODE (XEXP (addr, 0)) == REG
2479 && h8300_displacement_length (addr, size) == 2));
2482 /* Calculate the length of move instruction INSN using the given length
2483 table. Although the tables are correct for most cases, there is some
2484 irregularity in the length of mov.b and mov.w. The following forms:
2486 mov @ERs+, Rd
2487 mov @(d:16,ERs), Rd
2488 mov Rs, @-ERd
2489 mov Rs, @(d:16,ERd)
2491 are two bytes shorter than most other "mov Rs, @complex" or
2492 "mov @complex,Rd" combinations. */
2494 static unsigned int
2495 h8300_move_length (rtx *operands, const h8300_length_table *table)
2497 unsigned int size;
2499 size = h8300_length_from_table (operands[0], operands[1], table);
2500 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2501 size -= 2;
2502 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2503 size -= 2;
2504 return size;
2507 /* Return the length of a mova instruction with the given operands.
2508 DEST is the register destination, SRC is the source address and
2509 OFFSET is the 16-bit or 32-bit displacement. */
2511 static unsigned int
2512 h8300_mova_length (rtx dest, rtx src, rtx offset)
2514 unsigned int size;
2516 size = (2
2517 + h8300_constant_length (offset)
2518 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2519 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2520 size += 2;
2521 return size;
2524 /* Compute the length of INSN based on its length_table attribute.
2525 OPERANDS is the array of its operands. */
2527 unsigned int
2528 h8300_insn_length_from_table (rtx insn, rtx * operands)
2530 switch (get_attr_length_table (insn))
2532 case LENGTH_TABLE_NONE:
2533 gcc_unreachable ();
2535 case LENGTH_TABLE_ADDB:
2536 return h8300_binary_length (insn, &addb_length_table);
2538 case LENGTH_TABLE_ADDW:
2539 return h8300_binary_length (insn, &addw_length_table);
2541 case LENGTH_TABLE_ADDL:
2542 return h8300_binary_length (insn, &addl_length_table);
2544 case LENGTH_TABLE_LOGICB:
2545 return h8300_binary_length (insn, &logicb_length_table);
2547 case LENGTH_TABLE_MOVB:
2548 return h8300_move_length (operands, &movb_length_table);
2550 case LENGTH_TABLE_MOVW:
2551 return h8300_move_length (operands, &movw_length_table);
2553 case LENGTH_TABLE_MOVL:
2554 return h8300_move_length (operands, &movl_length_table);
2556 case LENGTH_TABLE_MOVA:
2557 return h8300_mova_length (operands[0], operands[1], operands[2]);
2559 case LENGTH_TABLE_MOVA_ZERO:
2560 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2562 case LENGTH_TABLE_UNARY:
2563 return h8300_unary_length (operands[0]);
2565 case LENGTH_TABLE_MOV_IMM4:
2566 return 2 + h8300_classify_operand (operands[0], 0, 0);
2568 case LENGTH_TABLE_SHORT_IMMEDIATE:
2569 return h8300_short_immediate_length (operands[0]);
2571 case LENGTH_TABLE_BITFIELD:
2572 return h8300_bitfield_length (operands[0], operands[1]);
2574 case LENGTH_TABLE_BITBRANCH:
2575 return h8300_bitfield_length (operands[1], operands[2]) - 2;
2577 default:
2578 gcc_unreachable ();
2582 /* Return true if LHS and RHS are memory references that can be mapped
2583 to the same h8sx assembly operand. LHS appears as the destination of
2584 an instruction and RHS appears as a source.
2586 Three cases are allowed:
2588 - RHS is @+Rn or @-Rn, LHS is @Rn
2589 - RHS is @Rn, LHS is @Rn+ or @Rn-
2590 - RHS and LHS have the same address and neither has side effects. */
2592 bool
2593 h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
2595 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2597 rhs = XEXP (rhs, 0);
2598 lhs = XEXP (lhs, 0);
2600 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2601 return rtx_equal_p (XEXP (rhs, 0), lhs);
2603 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2604 return rtx_equal_p (rhs, XEXP (lhs, 0));
2606 if (rtx_equal_p (rhs, lhs))
2607 return true;
2609 return false;
2612 /* Return true if OPERANDS[1] can be mapped to the same assembly
2613 operand as OPERANDS[0]. */
2615 bool
2616 h8300_operands_match_p (rtx *operands)
2618 if (register_operand (operands[0], VOIDmode)
2619 && register_operand (operands[1], VOIDmode))
2620 return true;
2622 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2623 return true;
2625 return false;
2628 /* Try using movmd to move LENGTH bytes from memory region SRC to memory
2629 region DEST. The two regions do not overlap and have the common
2630 alignment given by ALIGNMENT. Return true on success.
2632 Using movmd for variable-length moves seems to involve some
2633 complex trade-offs. For instance:
2635 - Preparing for a movmd instruction is similar to preparing
2636 for a memcpy. The main difference is that the arguments
2637 are moved into er4, er5 and er6 rather than er0, er1 and er2.
2639 - Since movmd clobbers the frame pointer, we need to save
2640 and restore it somehow when frame_pointer_needed. This can
2641 sometimes make movmd sequences longer than calls to memcpy().
2643 - The counter register is 16 bits, so the instruction is only
2644 suitable for variable-length moves when sizeof (size_t) == 2.
2645 That's only true in normal mode.
2647 - We will often lack static alignment information. Falling back
2648 on movmd.b would likely be slower than calling memcpy(), at least
2649 for big moves.
2651 This function therefore only uses movmd when the length is a
2652 known constant, and only then if -fomit-frame-pointer is in
2653 effect or if we're not optimizing for size.
2655 At the moment the function uses movmd for all in-range constants,
2656 but it might be better to fall back on memcpy() for large moves
2657 if ALIGNMENT == 1. */
2659 bool
2660 h8sx_emit_movmd (rtx dest, rtx src, rtx length,
2661 HOST_WIDE_INT alignment)
2663 if (!flag_omit_frame_pointer && optimize_size)
2664 return false;
2666 if (GET_CODE (length) == CONST_INT)
2668 rtx dest_reg, src_reg, first_dest, first_src;
2669 HOST_WIDE_INT n;
2670 int factor;
2672 /* Use movmd.l if the alignment allows it, otherwise fall back
2673 on movmd.b. */
2674 factor = (alignment >= 2 ? 4 : 1);
2676 /* Make sure the length is within range. We can handle counter
2677 values up to 65536, although HImode truncation will make
2678 the count appear negative in rtl dumps. */
2679 n = INTVAL (length);
2680 if (n <= 0 || n / factor > 65536)
2681 return false;
2683 /* Create temporary registers for the source and destination
2684 pointers. Initialize them to the start of each region. */
2685 dest_reg = copy_addr_to_reg (XEXP (dest, 0));
2686 src_reg = copy_addr_to_reg (XEXP (src, 0));
2688 /* Create references to the movmd source and destination blocks. */
2689 first_dest = replace_equiv_address (dest, dest_reg);
2690 first_src = replace_equiv_address (src, src_reg);
2692 set_mem_size (first_dest, n & -factor);
2693 set_mem_size (first_src, n & -factor);
2695 length = copy_to_mode_reg (HImode, gen_int_mode (n / factor, HImode));
2696 emit_insn (gen_movmd (first_dest, first_src, length, GEN_INT (factor)));
2698 if ((n & -factor) != n)
2700 /* Move SRC and DEST past the region we just copied.
2701 This is done to update the memory attributes. */
2702 dest = adjust_address (dest, BLKmode, n & -factor);
2703 src = adjust_address (src, BLKmode, n & -factor);
2705 /* Replace the addresses with the source and destination
2706 registers, which movmd has left with the right values. */
2707 dest = replace_equiv_address (dest, dest_reg);
2708 src = replace_equiv_address (src, src_reg);
2710 /* Mop up the left-over bytes. */
2711 if (n & 2)
2712 emit_move_insn (adjust_address (dest, HImode, 0),
2713 adjust_address (src, HImode, 0));
2714 if (n & 1)
2715 emit_move_insn (adjust_address (dest, QImode, n & 2),
2716 adjust_address (src, QImode, n & 2));
2718 return true;
2720 return false;
2723 /* Move ADDR into er6 after pushing its old value onto the stack. */
2725 void
2726 h8300_swap_into_er6 (rtx addr)
2728 rtx insn = push (HARD_FRAME_POINTER_REGNUM);
2729 if (frame_pointer_needed)
2730 add_reg_note (insn, REG_CFA_DEF_CFA,
2731 plus_constant (Pmode, gen_rtx_MEM (Pmode, stack_pointer_rtx),
2732 2 * UNITS_PER_WORD));
2733 else
2734 add_reg_note (insn, REG_CFA_ADJUST_CFA,
2735 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
2736 plus_constant (Pmode, stack_pointer_rtx, 4)));
2738 emit_move_insn (hard_frame_pointer_rtx, addr);
2739 if (REGNO (addr) == SP_REG)
2740 emit_move_insn (hard_frame_pointer_rtx,
2741 plus_constant (Pmode, hard_frame_pointer_rtx,
2742 GET_MODE_SIZE (word_mode)));
2745 /* Move the current value of er6 into ADDR and pop its old value
2746 from the stack. */
2748 void
2749 h8300_swap_out_of_er6 (rtx addr)
2751 rtx insn;
2753 if (REGNO (addr) != SP_REG)
2754 emit_move_insn (addr, hard_frame_pointer_rtx);
2756 insn = pop (HARD_FRAME_POINTER_REGNUM);
2757 RTX_FRAME_RELATED_P (insn) = 1;
2758 if (frame_pointer_needed)
2759 add_reg_note (insn, REG_CFA_DEF_CFA,
2760 plus_constant (Pmode, hard_frame_pointer_rtx,
2761 2 * UNITS_PER_WORD));
2762 else
2763 add_reg_note (insn, REG_CFA_ADJUST_CFA,
2764 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
2765 plus_constant (Pmode, stack_pointer_rtx, -4)));
2768 /* Return the length of mov instruction. */
2770 unsigned int
2771 compute_mov_length (rtx *operands)
2773 /* If the mov instruction involves a memory operand, we compute the
2774 length, assuming the largest addressing mode is used, and then
2775 adjust later in the function. Otherwise, we compute and return
2776 the exact length in one step. */
2777 enum machine_mode mode = GET_MODE (operands[0]);
2778 rtx dest = operands[0];
2779 rtx src = operands[1];
2780 rtx addr;
2782 if (GET_CODE (src) == MEM)
2783 addr = XEXP (src, 0);
2784 else if (GET_CODE (dest) == MEM)
2785 addr = XEXP (dest, 0);
2786 else
2787 addr = NULL_RTX;
2789 if (TARGET_H8300)
2791 unsigned int base_length;
2793 switch (mode)
2795 case QImode:
2796 if (addr == NULL_RTX)
2797 return 2;
2799 /* The eightbit addressing is available only in QImode, so
2800 go ahead and take care of it. */
2801 if (h8300_eightbit_constant_address_p (addr))
2802 return 2;
2804 base_length = 4;
2805 break;
2807 case HImode:
2808 if (addr == NULL_RTX)
2810 if (REG_P (src))
2811 return 2;
2813 if (src == const0_rtx)
2814 return 2;
2816 return 4;
2819 base_length = 4;
2820 break;
2822 case SImode:
2823 if (addr == NULL_RTX)
2825 if (REG_P (src))
2826 return 4;
2828 if (GET_CODE (src) == CONST_INT)
2830 if (src == const0_rtx)
2831 return 4;
2833 if ((INTVAL (src) & 0xffff) == 0)
2834 return 6;
2836 if ((INTVAL (src) & 0xffff) == 0)
2837 return 6;
2839 if ((INTVAL (src) & 0xffff)
2840 == ((INTVAL (src) >> 16) & 0xffff))
2841 return 6;
2843 return 8;
2846 base_length = 8;
2847 break;
2849 case SFmode:
2850 if (addr == NULL_RTX)
2852 if (REG_P (src))
2853 return 4;
2855 if (satisfies_constraint_G (src))
2856 return 4;
2858 return 8;
2861 base_length = 8;
2862 break;
2864 default:
2865 gcc_unreachable ();
2868 /* Adjust the length based on the addressing mode used.
2869 Specifically, we subtract the difference between the actual
2870 length and the longest one, which is @(d:16,Rs). For SImode
2871 and SFmode, we double the adjustment because two mov.w are
2872 used to do the job. */
2874 /* @Rs+ and @-Rd are 2 bytes shorter than the longest. */
2875 if (GET_CODE (addr) == PRE_DEC
2876 || GET_CODE (addr) == POST_INC)
2878 if (mode == QImode || mode == HImode)
2879 return base_length - 2;
2880 else
2881 /* In SImode and SFmode, we use two mov.w instructions, so
2882 double the adjustment. */
2883 return base_length - 4;
2886 /* @Rs and @Rd are 2 bytes shorter than the longest. Note that
2887 in SImode and SFmode, the second mov.w involves an address
2888 with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
2889 only 2 bytes. */
2890 if (GET_CODE (addr) == REG)
2891 return base_length - 2;
2893 return base_length;
2895 else
2897 unsigned int base_length;
2899 switch (mode)
2901 case QImode:
2902 if (addr == NULL_RTX)
2903 return 2;
2905 /* The eightbit addressing is available only in QImode, so
2906 go ahead and take care of it. */
2907 if (h8300_eightbit_constant_address_p (addr))
2908 return 2;
2910 base_length = 8;
2911 break;
2913 case HImode:
2914 if (addr == NULL_RTX)
2916 if (REG_P (src))
2917 return 2;
2919 if (src == const0_rtx)
2920 return 2;
2922 return 4;
2925 base_length = 8;
2926 break;
2928 case SImode:
2929 if (addr == NULL_RTX)
2931 if (REG_P (src))
2933 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2934 return 4;
2935 else
2936 return 2;
2939 if (GET_CODE (src) == CONST_INT)
2941 int val = INTVAL (src);
2943 if (val == 0)
2944 return 2;
2946 if (val == (val & 0x00ff) || val == (val & 0xff00))
2947 return 4;
2949 switch (val & 0xffffffff)
2951 case 0xffffffff:
2952 case 0xfffffffe:
2953 case 0xfffffffc:
2954 case 0x0000ffff:
2955 case 0x0000fffe:
2956 case 0xffff0000:
2957 case 0xfffe0000:
2958 case 0x00010000:
2959 case 0x00020000:
2960 return 4;
2963 return 6;
2966 base_length = 10;
2967 break;
2969 case SFmode:
2970 if (addr == NULL_RTX)
2972 if (REG_P (src))
2973 return 2;
2975 if (satisfies_constraint_G (src))
2976 return 2;
2978 return 6;
2981 base_length = 10;
2982 break;
2984 default:
2985 gcc_unreachable ();
2988 /* Adjust the length based on the addressing mode used.
2989 Specifically, we subtract the difference between the actual
2990 length and the longest one, which is @(d:24,ERs). */
2992 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
2993 if (GET_CODE (addr) == PRE_DEC
2994 || GET_CODE (addr) == POST_INC)
2995 return base_length - 6;
2997 /* @ERs and @ERd are 6 bytes shorter than the longest. */
2998 if (GET_CODE (addr) == REG)
2999 return base_length - 6;
3001 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
3002 longest. */
3003 if (GET_CODE (addr) == PLUS
3004 && GET_CODE (XEXP (addr, 0)) == REG
3005 && GET_CODE (XEXP (addr, 1)) == CONST_INT
3006 && INTVAL (XEXP (addr, 1)) > -32768
3007 && INTVAL (XEXP (addr, 1)) < 32767)
3008 return base_length - 4;
3010 /* @aa:16 is 4 bytes shorter than the longest. */
3011 if (h8300_tiny_constant_address_p (addr))
3012 return base_length - 4;
3014 /* @aa:24 is 2 bytes shorter than the longest. */
3015 if (CONSTANT_P (addr))
3016 return base_length - 2;
3018 return base_length;
3022 /* Output an addition insn. */
3024 const char *
3025 output_plussi (rtx *operands)
3027 enum machine_mode mode = GET_MODE (operands[0]);
3029 gcc_assert (mode == SImode);
3031 if (TARGET_H8300)
3033 if (GET_CODE (operands[2]) == REG)
3034 return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3036 if (GET_CODE (operands[2]) == CONST_INT)
3038 HOST_WIDE_INT n = INTVAL (operands[2]);
3040 if ((n & 0xffffff) == 0)
3041 return "add\t%z2,%z0";
3042 if ((n & 0xffff) == 0)
3043 return "add\t%y2,%y0\n\taddx\t%z2,%z0";
3044 if ((n & 0xff) == 0)
3045 return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3048 return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3050 else
3052 if (GET_CODE (operands[2]) == CONST_INT
3053 && register_operand (operands[1], VOIDmode))
3055 HOST_WIDE_INT intval = INTVAL (operands[2]);
3057 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3058 return "add.l\t%S2,%S0";
3059 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3060 return "sub.l\t%G2,%S0";
3062 /* See if we can finish with 2 bytes. */
3064 switch ((unsigned int) intval & 0xffffffff)
3066 case 0x00000001:
3067 case 0x00000002:
3068 case 0x00000004:
3069 return "adds\t%2,%S0";
3071 case 0xffffffff:
3072 case 0xfffffffe:
3073 case 0xfffffffc:
3074 return "subs\t%G2,%S0";
3076 case 0x00010000:
3077 case 0x00020000:
3078 operands[2] = GEN_INT (intval >> 16);
3079 return "inc.w\t%2,%e0";
3081 case 0xffff0000:
3082 case 0xfffe0000:
3083 operands[2] = GEN_INT (intval >> 16);
3084 return "dec.w\t%G2,%e0";
3087 /* See if we can finish with 4 bytes. */
3088 if ((intval & 0xffff) == 0)
3090 operands[2] = GEN_INT (intval >> 16);
3091 return "add.w\t%2,%e0";
3095 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3097 operands[2] = GEN_INT (-INTVAL (operands[2]));
3098 return "sub.l\t%S2,%S0";
3100 return "add.l\t%S2,%S0";
3104 /* ??? It would be much easier to add the h8sx stuff if a single function
3105 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
3106 /* Compute the length of an addition insn. */
3108 unsigned int
3109 compute_plussi_length (rtx *operands)
3111 enum machine_mode mode = GET_MODE (operands[0]);
3113 gcc_assert (mode == SImode);
3115 if (TARGET_H8300)
3117 if (GET_CODE (operands[2]) == REG)
3118 return 6;
3120 if (GET_CODE (operands[2]) == CONST_INT)
3122 HOST_WIDE_INT n = INTVAL (operands[2]);
3124 if ((n & 0xffffff) == 0)
3125 return 2;
3126 if ((n & 0xffff) == 0)
3127 return 4;
3128 if ((n & 0xff) == 0)
3129 return 6;
3132 return 8;
3134 else
3136 if (GET_CODE (operands[2]) == CONST_INT
3137 && register_operand (operands[1], VOIDmode))
3139 HOST_WIDE_INT intval = INTVAL (operands[2]);
3141 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3142 return 2;
3143 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3144 return 2;
3146 /* See if we can finish with 2 bytes. */
3148 switch ((unsigned int) intval & 0xffffffff)
3150 case 0x00000001:
3151 case 0x00000002:
3152 case 0x00000004:
3153 return 2;
3155 case 0xffffffff:
3156 case 0xfffffffe:
3157 case 0xfffffffc:
3158 return 2;
3160 case 0x00010000:
3161 case 0x00020000:
3162 return 2;
3164 case 0xffff0000:
3165 case 0xfffe0000:
3166 return 2;
3169 /* See if we can finish with 4 bytes. */
3170 if ((intval & 0xffff) == 0)
3171 return 4;
3174 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3175 return h8300_length_from_table (operands[0],
3176 GEN_INT (-INTVAL (operands[2])),
3177 &addl_length_table);
3178 else
3179 return h8300_length_from_table (operands[0], operands[2],
3180 &addl_length_table);
3181 return 6;
3185 /* Compute which flag bits are valid after an addition insn. */
3187 enum attr_cc
3188 compute_plussi_cc (rtx *operands)
3190 enum machine_mode mode = GET_MODE (operands[0]);
3192 gcc_assert (mode == SImode);
3194 if (TARGET_H8300)
3196 return CC_CLOBBER;
3198 else
3200 if (GET_CODE (operands[2]) == CONST_INT
3201 && register_operand (operands[1], VOIDmode))
3203 HOST_WIDE_INT intval = INTVAL (operands[2]);
3205 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3206 return CC_SET_ZN;
3207 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3208 return CC_SET_ZN;
3210 /* See if we can finish with 2 bytes. */
3212 switch ((unsigned int) intval & 0xffffffff)
3214 case 0x00000001:
3215 case 0x00000002:
3216 case 0x00000004:
3217 return CC_NONE_0HIT;
3219 case 0xffffffff:
3220 case 0xfffffffe:
3221 case 0xfffffffc:
3222 return CC_NONE_0HIT;
3224 case 0x00010000:
3225 case 0x00020000:
3226 return CC_CLOBBER;
3228 case 0xffff0000:
3229 case 0xfffe0000:
3230 return CC_CLOBBER;
3233 /* See if we can finish with 4 bytes. */
3234 if ((intval & 0xffff) == 0)
3235 return CC_CLOBBER;
3238 return CC_SET_ZN;
3242 /* Output a logical insn. */
3244 const char *
3245 output_logical_op (enum machine_mode mode, rtx *operands)
3247 /* Figure out the logical op that we need to perform. */
3248 enum rtx_code code = GET_CODE (operands[3]);
3249 /* Pretend that every byte is affected if both operands are registers. */
3250 const unsigned HOST_WIDE_INT intval =
3251 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3252 /* Always use the full instruction if the
3253 first operand is in memory. It is better
3254 to use define_splits to generate the shorter
3255 sequence where valid. */
3256 && register_operand (operands[1], VOIDmode)
3257 ? INTVAL (operands[2]) : 0x55555555);
3258 /* The determinant of the algorithm. If we perform an AND, 0
3259 affects a bit. Otherwise, 1 affects a bit. */
3260 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3261 /* Break up DET into pieces. */
3262 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3263 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3264 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3265 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3266 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3267 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3268 int lower_half_easy_p = 0;
3269 int upper_half_easy_p = 0;
3270 /* The name of an insn. */
3271 const char *opname;
3272 char insn_buf[100];
3274 switch (code)
3276 case AND:
3277 opname = "and";
3278 break;
3279 case IOR:
3280 opname = "or";
3281 break;
3282 case XOR:
3283 opname = "xor";
3284 break;
3285 default:
3286 gcc_unreachable ();
3289 switch (mode)
3291 case HImode:
3292 /* First, see if we can finish with one insn. */
3293 if ((TARGET_H8300H || TARGET_H8300S)
3294 && b0 != 0
3295 && b1 != 0)
3297 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
3298 output_asm_insn (insn_buf, operands);
3300 else
3302 /* Take care of the lower byte. */
3303 if (b0 != 0)
3305 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
3306 output_asm_insn (insn_buf, operands);
3308 /* Take care of the upper byte. */
3309 if (b1 != 0)
3311 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
3312 output_asm_insn (insn_buf, operands);
3315 break;
3316 case SImode:
3317 if (TARGET_H8300H || TARGET_H8300S)
3319 /* Determine if the lower half can be taken care of in no more
3320 than two bytes. */
3321 lower_half_easy_p = (b0 == 0
3322 || b1 == 0
3323 || (code != IOR && w0 == 0xffff));
3325 /* Determine if the upper half can be taken care of in no more
3326 than two bytes. */
3327 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3328 || (code == AND && w1 == 0xff00));
3331 /* Check if doing everything with one insn is no worse than
3332 using multiple insns. */
3333 if ((TARGET_H8300H || TARGET_H8300S)
3334 && w0 != 0 && w1 != 0
3335 && !(lower_half_easy_p && upper_half_easy_p)
3336 && !(code == IOR && w1 == 0xffff
3337 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3339 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
3340 output_asm_insn (insn_buf, operands);
3342 else
3344 /* Take care of the lower and upper words individually. For
3345 each word, we try different methods in the order of
3347 1) the special insn (in case of AND or XOR),
3348 2) the word-wise insn, and
3349 3) The byte-wise insn. */
3350 if (w0 == 0xffff
3351 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3352 output_asm_insn ((code == AND)
3353 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
3354 operands);
3355 else if ((TARGET_H8300H || TARGET_H8300S)
3356 && (b0 != 0)
3357 && (b1 != 0))
3359 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
3360 output_asm_insn (insn_buf, operands);
3362 else
3364 if (b0 != 0)
3366 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
3367 output_asm_insn (insn_buf, operands);
3369 if (b1 != 0)
3371 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3372 output_asm_insn (insn_buf, operands);
3376 if ((w1 == 0xffff)
3377 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3378 output_asm_insn ((code == AND)
3379 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
3380 operands);
3381 else if ((TARGET_H8300H || TARGET_H8300S)
3382 && code == IOR
3383 && w1 == 0xffff
3384 && (w0 & 0x8000) != 0)
3386 output_asm_insn ("exts.l\t%S0", operands);
3388 else if ((TARGET_H8300H || TARGET_H8300S)
3389 && code == AND
3390 && w1 == 0xff00)
3392 output_asm_insn ("extu.w\t%e0", operands);
3394 else if (TARGET_H8300H || TARGET_H8300S)
3396 if (w1 != 0)
3398 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3399 output_asm_insn (insn_buf, operands);
3402 else
3404 if (b2 != 0)
3406 sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
3407 output_asm_insn (insn_buf, operands);
3409 if (b3 != 0)
3411 sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
3412 output_asm_insn (insn_buf, operands);
3416 break;
3417 default:
3418 gcc_unreachable ();
3420 return "";
3423 /* Compute the length of a logical insn. */
3425 unsigned int
3426 compute_logical_op_length (enum machine_mode mode, rtx *operands)
3428 /* Figure out the logical op that we need to perform. */
3429 enum rtx_code code = GET_CODE (operands[3]);
3430 /* Pretend that every byte is affected if both operands are registers. */
3431 const unsigned HOST_WIDE_INT intval =
3432 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3433 /* Always use the full instruction if the
3434 first operand is in memory. It is better
3435 to use define_splits to generate the shorter
3436 sequence where valid. */
3437 && register_operand (operands[1], VOIDmode)
3438 ? INTVAL (operands[2]) : 0x55555555);
3439 /* The determinant of the algorithm. If we perform an AND, 0
3440 affects a bit. Otherwise, 1 affects a bit. */
3441 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3442 /* Break up DET into pieces. */
3443 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3444 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3445 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3446 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3447 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3448 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3449 int lower_half_easy_p = 0;
3450 int upper_half_easy_p = 0;
3451 /* Insn length. */
3452 unsigned int length = 0;
3454 switch (mode)
3456 case HImode:
3457 /* First, see if we can finish with one insn. */
3458 if ((TARGET_H8300H || TARGET_H8300S)
3459 && b0 != 0
3460 && b1 != 0)
3462 length = h8300_length_from_table (operands[1], operands[2],
3463 &logicw_length_table);
3465 else
3467 /* Take care of the lower byte. */
3468 if (b0 != 0)
3469 length += 2;
3471 /* Take care of the upper byte. */
3472 if (b1 != 0)
3473 length += 2;
3475 break;
3476 case SImode:
3477 if (TARGET_H8300H || TARGET_H8300S)
3479 /* Determine if the lower half can be taken care of in no more
3480 than two bytes. */
3481 lower_half_easy_p = (b0 == 0
3482 || b1 == 0
3483 || (code != IOR && w0 == 0xffff));
3485 /* Determine if the upper half can be taken care of in no more
3486 than two bytes. */
3487 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3488 || (code == AND && w1 == 0xff00));
3491 /* Check if doing everything with one insn is no worse than
3492 using multiple insns. */
3493 if ((TARGET_H8300H || TARGET_H8300S)
3494 && w0 != 0 && w1 != 0
3495 && !(lower_half_easy_p && upper_half_easy_p)
3496 && !(code == IOR && w1 == 0xffff
3497 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3499 length = h8300_length_from_table (operands[1], operands[2],
3500 &logicl_length_table);
3502 else
3504 /* Take care of the lower and upper words individually. For
3505 each word, we try different methods in the order of
3507 1) the special insn (in case of AND or XOR),
3508 2) the word-wise insn, and
3509 3) The byte-wise insn. */
3510 if (w0 == 0xffff
3511 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3513 length += 2;
3515 else if ((TARGET_H8300H || TARGET_H8300S)
3516 && (b0 != 0)
3517 && (b1 != 0))
3519 length += 4;
3521 else
3523 if (b0 != 0)
3524 length += 2;
3526 if (b1 != 0)
3527 length += 2;
3530 if (w1 == 0xffff
3531 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3533 length += 2;
3535 else if ((TARGET_H8300H || TARGET_H8300S)
3536 && code == IOR
3537 && w1 == 0xffff
3538 && (w0 & 0x8000) != 0)
3540 length += 2;
3542 else if ((TARGET_H8300H || TARGET_H8300S)
3543 && code == AND
3544 && w1 == 0xff00)
3546 length += 2;
3548 else if (TARGET_H8300H || TARGET_H8300S)
3550 if (w1 != 0)
3551 length += 4;
3553 else
3555 if (b2 != 0)
3556 length += 2;
3558 if (b3 != 0)
3559 length += 2;
3562 break;
3563 default:
3564 gcc_unreachable ();
3566 return length;
3569 /* Compute which flag bits are valid after a logical insn. */
3571 enum attr_cc
3572 compute_logical_op_cc (enum machine_mode mode, rtx *operands)
3574 /* Figure out the logical op that we need to perform. */
3575 enum rtx_code code = GET_CODE (operands[3]);
3576 /* Pretend that every byte is affected if both operands are registers. */
3577 const unsigned HOST_WIDE_INT intval =
3578 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3579 /* Always use the full instruction if the
3580 first operand is in memory. It is better
3581 to use define_splits to generate the shorter
3582 sequence where valid. */
3583 && register_operand (operands[1], VOIDmode)
3584 ? INTVAL (operands[2]) : 0x55555555);
3585 /* The determinant of the algorithm. If we perform an AND, 0
3586 affects a bit. Otherwise, 1 affects a bit. */
3587 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3588 /* Break up DET into pieces. */
3589 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3590 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3591 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3592 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3593 int lower_half_easy_p = 0;
3594 int upper_half_easy_p = 0;
3595 /* Condition code. */
3596 enum attr_cc cc = CC_CLOBBER;
3598 switch (mode)
3600 case HImode:
3601 /* First, see if we can finish with one insn. */
3602 if ((TARGET_H8300H || TARGET_H8300S)
3603 && b0 != 0
3604 && b1 != 0)
3606 cc = CC_SET_ZNV;
3608 break;
3609 case SImode:
3610 if (TARGET_H8300H || TARGET_H8300S)
3612 /* Determine if the lower half can be taken care of in no more
3613 than two bytes. */
3614 lower_half_easy_p = (b0 == 0
3615 || b1 == 0
3616 || (code != IOR && w0 == 0xffff));
3618 /* Determine if the upper half can be taken care of in no more
3619 than two bytes. */
3620 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3621 || (code == AND && w1 == 0xff00));
3624 /* Check if doing everything with one insn is no worse than
3625 using multiple insns. */
3626 if ((TARGET_H8300H || TARGET_H8300S)
3627 && w0 != 0 && w1 != 0
3628 && !(lower_half_easy_p && upper_half_easy_p)
3629 && !(code == IOR && w1 == 0xffff
3630 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3632 cc = CC_SET_ZNV;
3634 else
3636 if ((TARGET_H8300H || TARGET_H8300S)
3637 && code == IOR
3638 && w1 == 0xffff
3639 && (w0 & 0x8000) != 0)
3641 cc = CC_SET_ZNV;
3644 break;
3645 default:
3646 gcc_unreachable ();
3648 return cc;
3651 /* Expand a conditional branch. */
3653 void
3654 h8300_expand_branch (rtx operands[])
3656 enum rtx_code code = GET_CODE (operands[0]);
3657 rtx op0 = operands[1];
3658 rtx op1 = operands[2];
3659 rtx label = operands[3];
3660 rtx tmp;
3662 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3663 emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3665 tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3666 tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3667 gen_rtx_LABEL_REF (VOIDmode, label),
3668 pc_rtx);
3669 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
3673 /* Expand a conditional store. */
3675 void
3676 h8300_expand_store (rtx operands[])
3678 rtx dest = operands[0];
3679 enum rtx_code code = GET_CODE (operands[1]);
3680 rtx op0 = operands[2];
3681 rtx op1 = operands[3];
3682 rtx tmp;
3684 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3685 emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3687 tmp = gen_rtx_fmt_ee (code, GET_MODE (dest), cc0_rtx, const0_rtx);
3688 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
3691 /* Shifts.
3693 We devote a fair bit of code to getting efficient shifts since we
3694 can only shift one bit at a time on the H8/300 and H8/300H and only
3695 one or two bits at a time on the H8S.
3697 All shift code falls into one of the following ways of
3698 implementation:
3700 o SHIFT_INLINE: Emit straight line code for the shift; this is used
3701 when a straight line shift is about the same size or smaller than
3702 a loop.
3704 o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3705 off the bits we don't need. This is used when only a few of the
3706 bits in the original value will survive in the shifted value.
3708 o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3709 simulate a shift by 8, 16, or 24 bits. Once moved, a few inline
3710 shifts can be added if the shift count is slightly more than 8 or
3711 16. This case also includes other oddballs that are not worth
3712 explaining here.
3714 o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
3716 For each shift count, we try to use code that has no trade-off
3717 between code size and speed whenever possible.
3719 If the trade-off is unavoidable, we try to be reasonable.
3720 Specifically, the fastest version is one instruction longer than
3721 the shortest version, we take the fastest version. We also provide
3722 the use a way to switch back to the shortest version with -Os.
3724 For the details of the shift algorithms for various shift counts,
3725 refer to shift_alg_[qhs]i. */
3727 /* Classify a shift with the given mode and code. OP is the shift amount. */
3729 enum h8sx_shift_type
3730 h8sx_classify_shift (enum machine_mode mode, enum rtx_code code, rtx op)
3732 if (!TARGET_H8300SX)
3733 return H8SX_SHIFT_NONE;
3735 switch (code)
3737 case ASHIFT:
3738 case LSHIFTRT:
3739 /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd). */
3740 if (GET_CODE (op) != CONST_INT)
3741 return H8SX_SHIFT_BINARY;
3743 /* Reject out-of-range shift amounts. */
3744 if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3745 return H8SX_SHIFT_NONE;
3747 /* Power-of-2 shifts are effectively unary operations. */
3748 if (exact_log2 (INTVAL (op)) >= 0)
3749 return H8SX_SHIFT_UNARY;
3751 return H8SX_SHIFT_BINARY;
3753 case ASHIFTRT:
3754 if (op == const1_rtx || op == const2_rtx)
3755 return H8SX_SHIFT_UNARY;
3756 return H8SX_SHIFT_NONE;
3758 case ROTATE:
3759 if (GET_CODE (op) == CONST_INT
3760 && (INTVAL (op) == 1
3761 || INTVAL (op) == 2
3762 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3763 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3764 return H8SX_SHIFT_UNARY;
3765 return H8SX_SHIFT_NONE;
3767 default:
3768 return H8SX_SHIFT_NONE;
3772 /* Return the asm template for a single h8sx shift instruction.
3773 OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3774 is the source and OPERANDS[3] is the shift. SUFFIX is the
3775 size suffix ('b', 'w' or 'l') and OPTYPE is the h8300_print_operand
3776 prefix for the destination operand. */
3778 const char *
3779 output_h8sx_shift (rtx *operands, int suffix, int optype)
3781 static char buffer[16];
3782 const char *stem;
3784 switch (GET_CODE (operands[3]))
3786 case ASHIFT:
3787 stem = "shll";
3788 break;
3790 case ASHIFTRT:
3791 stem = "shar";
3792 break;
3794 case LSHIFTRT:
3795 stem = "shlr";
3796 break;
3798 case ROTATE:
3799 stem = "rotl";
3800 if (INTVAL (operands[2]) > 2)
3802 /* This is really a right rotate. */
3803 operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3804 - INTVAL (operands[2]));
3805 stem = "rotr";
3807 break;
3809 default:
3810 gcc_unreachable ();
3812 if (operands[2] == const1_rtx)
3813 sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3814 else
3815 sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3816 return buffer;
3819 /* Emit code to do shifts. */
3821 bool
3822 expand_a_shift (enum machine_mode mode, enum rtx_code code, rtx operands[])
3824 switch (h8sx_classify_shift (mode, code, operands[2]))
3826 case H8SX_SHIFT_BINARY:
3827 operands[1] = force_reg (mode, operands[1]);
3828 return false;
3830 case H8SX_SHIFT_UNARY:
3831 return false;
3833 case H8SX_SHIFT_NONE:
3834 break;
3837 emit_move_insn (copy_rtx (operands[0]), operands[1]);
3839 /* Need a loop to get all the bits we want - we generate the
3840 code at emit time, but need to allocate a scratch reg now. */
3842 emit_insn (gen_rtx_PARALLEL
3843 (VOIDmode,
3844 gen_rtvec (2,
3845 gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
3846 gen_rtx_fmt_ee (code, mode,
3847 copy_rtx (operands[0]), operands[2])),
3848 gen_rtx_CLOBBER (VOIDmode,
3849 gen_rtx_SCRATCH (QImode)))));
3850 return true;
3853 /* Symbols of the various modes which can be used as indices. */
3855 enum shift_mode
3857 QIshift, HIshift, SIshift
3860 /* For single bit shift insns, record assembler and what bits of the
3861 condition code are valid afterwards (represented as various CC_FOO
3862 bits, 0 means CC isn't left in a usable state). */
3864 struct shift_insn
3866 const char *const assembler;
3867 const enum attr_cc cc_valid;
3870 /* Assembler instruction shift table.
3872 These tables are used to look up the basic shifts.
3873 They are indexed by cpu, shift_type, and mode. */
3875 static const struct shift_insn shift_one[2][3][3] =
3877 /* H8/300 */
3879 /* SHIFT_ASHIFT */
3881 { "shll\t%X0", CC_SET_ZNV },
3882 { "add.w\t%T0,%T0", CC_SET_ZN },
3883 { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", CC_CLOBBER }
3885 /* SHIFT_LSHIFTRT */
3887 { "shlr\t%X0", CC_SET_ZNV },
3888 { "shlr\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3889 { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3891 /* SHIFT_ASHIFTRT */
3893 { "shar\t%X0", CC_SET_ZNV },
3894 { "shar\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3895 { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3898 /* H8/300H */
3900 /* SHIFT_ASHIFT */
3902 { "shll.b\t%X0", CC_SET_ZNV },
3903 { "shll.w\t%T0", CC_SET_ZNV },
3904 { "shll.l\t%S0", CC_SET_ZNV }
3906 /* SHIFT_LSHIFTRT */
3908 { "shlr.b\t%X0", CC_SET_ZNV },
3909 { "shlr.w\t%T0", CC_SET_ZNV },
3910 { "shlr.l\t%S0", CC_SET_ZNV }
3912 /* SHIFT_ASHIFTRT */
3914 { "shar.b\t%X0", CC_SET_ZNV },
3915 { "shar.w\t%T0", CC_SET_ZNV },
3916 { "shar.l\t%S0", CC_SET_ZNV }
3921 static const struct shift_insn shift_two[3][3] =
3923 /* SHIFT_ASHIFT */
3925 { "shll.b\t#2,%X0", CC_SET_ZNV },
3926 { "shll.w\t#2,%T0", CC_SET_ZNV },
3927 { "shll.l\t#2,%S0", CC_SET_ZNV }
3929 /* SHIFT_LSHIFTRT */
3931 { "shlr.b\t#2,%X0", CC_SET_ZNV },
3932 { "shlr.w\t#2,%T0", CC_SET_ZNV },
3933 { "shlr.l\t#2,%S0", CC_SET_ZNV }
3935 /* SHIFT_ASHIFTRT */
3937 { "shar.b\t#2,%X0", CC_SET_ZNV },
3938 { "shar.w\t#2,%T0", CC_SET_ZNV },
3939 { "shar.l\t#2,%S0", CC_SET_ZNV }
3943 /* Rotates are organized by which shift they'll be used in implementing.
3944 There's no need to record whether the cc is valid afterwards because
3945 it is the AND insn that will decide this. */
3947 static const char *const rotate_one[2][3][3] =
3949 /* H8/300 */
3951 /* SHIFT_ASHIFT */
3953 "rotr\t%X0",
3954 "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
3957 /* SHIFT_LSHIFTRT */
3959 "rotl\t%X0",
3960 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3963 /* SHIFT_ASHIFTRT */
3965 "rotl\t%X0",
3966 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3970 /* H8/300H */
3972 /* SHIFT_ASHIFT */
3974 "rotr.b\t%X0",
3975 "rotr.w\t%T0",
3976 "rotr.l\t%S0"
3978 /* SHIFT_LSHIFTRT */
3980 "rotl.b\t%X0",
3981 "rotl.w\t%T0",
3982 "rotl.l\t%S0"
3984 /* SHIFT_ASHIFTRT */
3986 "rotl.b\t%X0",
3987 "rotl.w\t%T0",
3988 "rotl.l\t%S0"
3993 static const char *const rotate_two[3][3] =
3995 /* SHIFT_ASHIFT */
3997 "rotr.b\t#2,%X0",
3998 "rotr.w\t#2,%T0",
3999 "rotr.l\t#2,%S0"
4001 /* SHIFT_LSHIFTRT */
4003 "rotl.b\t#2,%X0",
4004 "rotl.w\t#2,%T0",
4005 "rotl.l\t#2,%S0"
4007 /* SHIFT_ASHIFTRT */
4009 "rotl.b\t#2,%X0",
4010 "rotl.w\t#2,%T0",
4011 "rotl.l\t#2,%S0"
4015 struct shift_info {
4016 /* Shift algorithm. */
4017 enum shift_alg alg;
4019 /* The number of bits to be shifted by shift1 and shift2. Valid
4020 when ALG is SHIFT_SPECIAL. */
4021 unsigned int remainder;
4023 /* Special insn for a shift. Valid when ALG is SHIFT_SPECIAL. */
4024 const char *special;
4026 /* Insn for a one-bit shift. Valid when ALG is either SHIFT_INLINE
4027 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
4028 const char *shift1;
4030 /* Insn for a two-bit shift. Valid when ALG is either SHIFT_INLINE
4031 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
4032 const char *shift2;
4034 /* CC status for SHIFT_INLINE. */
4035 enum attr_cc cc_inline;
4037 /* CC status for SHIFT_SPECIAL. */
4038 enum attr_cc cc_special;
4041 static void get_shift_alg (enum shift_type,
4042 enum shift_mode, unsigned int,
4043 struct shift_info *);
4045 /* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
4046 best algorithm for doing the shift. The assembler code is stored
4047 in the pointers in INFO. We achieve the maximum efficiency in most
4048 cases when !TARGET_H8300. In case of TARGET_H8300, shifts in
4049 SImode in particular have a lot of room to optimize.
4051 We first determine the strategy of the shift algorithm by a table
4052 lookup. If that tells us to use a hand crafted assembly code, we
4053 go into the big switch statement to find what that is. Otherwise,
4054 we resort to a generic way, such as inlining. In either case, the
4055 result is returned through INFO. */
4057 static void
4058 get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
4059 unsigned int count, struct shift_info *info)
4061 enum h8_cpu cpu;
4063 /* Find the target CPU. */
4064 if (TARGET_H8300)
4065 cpu = H8_300;
4066 else if (TARGET_H8300H)
4067 cpu = H8_300H;
4068 else
4069 cpu = H8_S;
4071 /* Find the shift algorithm. */
4072 info->alg = SHIFT_LOOP;
4073 switch (shift_mode)
4075 case QIshift:
4076 if (count < GET_MODE_BITSIZE (QImode))
4077 info->alg = shift_alg_qi[cpu][shift_type][count];
4078 break;
4080 case HIshift:
4081 if (count < GET_MODE_BITSIZE (HImode))
4082 info->alg = shift_alg_hi[cpu][shift_type][count];
4083 break;
4085 case SIshift:
4086 if (count < GET_MODE_BITSIZE (SImode))
4087 info->alg = shift_alg_si[cpu][shift_type][count];
4088 break;
4090 default:
4091 gcc_unreachable ();
4094 /* Fill in INFO. Return unless we have SHIFT_SPECIAL. */
4095 switch (info->alg)
4097 case SHIFT_INLINE:
4098 info->remainder = count;
4099 /* Fall through. */
4101 case SHIFT_LOOP:
4102 /* It is up to the caller to know that looping clobbers cc. */
4103 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4104 info->shift2 = shift_two[shift_type][shift_mode].assembler;
4105 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4106 goto end;
4108 case SHIFT_ROT_AND:
4109 info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
4110 info->shift2 = rotate_two[shift_type][shift_mode];
4111 info->cc_inline = CC_CLOBBER;
4112 goto end;
4114 case SHIFT_SPECIAL:
4115 /* REMAINDER is 0 for most cases, so initialize it to 0. */
4116 info->remainder = 0;
4117 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4118 info->shift2 = shift_two[shift_type][shift_mode].assembler;
4119 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4120 info->cc_special = CC_CLOBBER;
4121 break;
4124 /* Here we only deal with SHIFT_SPECIAL. */
4125 switch (shift_mode)
4127 case QIshift:
4128 /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
4129 through the entire value. */
4130 gcc_assert (shift_type == SHIFT_ASHIFTRT && count == 7);
4131 info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
4132 goto end;
4134 case HIshift:
4135 if (count == 7)
4137 switch (shift_type)
4139 case SHIFT_ASHIFT:
4140 if (TARGET_H8300)
4141 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";
4142 else
4143 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
4144 goto end;
4145 case SHIFT_LSHIFTRT:
4146 if (TARGET_H8300)
4147 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";
4148 else
4149 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
4150 goto end;
4151 case SHIFT_ASHIFTRT:
4152 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
4153 goto end;
4156 else if ((8 <= count && count <= 13)
4157 || (TARGET_H8300S && count == 14))
4159 info->remainder = count - 8;
4161 switch (shift_type)
4163 case SHIFT_ASHIFT:
4164 info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
4165 goto end;
4166 case SHIFT_LSHIFTRT:
4167 if (TARGET_H8300)
4169 info->special = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
4170 info->shift1 = "shlr.b\t%s0";
4171 info->cc_inline = CC_SET_ZNV;
4173 else
4175 info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
4176 info->cc_special = CC_SET_ZNV;
4178 goto end;
4179 case SHIFT_ASHIFTRT:
4180 if (TARGET_H8300)
4182 info->special = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0";
4183 info->shift1 = "shar.b\t%s0";
4185 else
4187 info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
4188 info->cc_special = CC_SET_ZNV;
4190 goto end;
4193 else if (count == 14)
4195 switch (shift_type)
4197 case SHIFT_ASHIFT:
4198 if (TARGET_H8300)
4199 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";
4200 goto end;
4201 case SHIFT_LSHIFTRT:
4202 if (TARGET_H8300)
4203 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";
4204 goto end;
4205 case SHIFT_ASHIFTRT:
4206 if (TARGET_H8300)
4207 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";
4208 else if (TARGET_H8300H)
4210 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";
4211 info->cc_special = CC_SET_ZNV;
4213 else /* TARGET_H8300S */
4214 gcc_unreachable ();
4215 goto end;
4218 else if (count == 15)
4220 switch (shift_type)
4222 case SHIFT_ASHIFT:
4223 info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
4224 goto end;
4225 case SHIFT_LSHIFTRT:
4226 info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
4227 goto end;
4228 case SHIFT_ASHIFTRT:
4229 info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
4230 goto end;
4233 gcc_unreachable ();
4235 case SIshift:
4236 if (TARGET_H8300 && 8 <= count && count <= 9)
4238 info->remainder = count - 8;
4240 switch (shift_type)
4242 case SHIFT_ASHIFT:
4243 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";
4244 goto end;
4245 case SHIFT_LSHIFTRT:
4246 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";
4247 info->shift1 = "shlr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0";
4248 goto end;
4249 case SHIFT_ASHIFTRT:
4250 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";
4251 goto end;
4254 else if (count == 8 && !TARGET_H8300)
4256 switch (shift_type)
4258 case SHIFT_ASHIFT:
4259 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";
4260 goto end;
4261 case SHIFT_LSHIFTRT:
4262 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";
4263 goto end;
4264 case SHIFT_ASHIFTRT:
4265 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";
4266 goto end;
4269 else if (count == 15 && TARGET_H8300)
4271 switch (shift_type)
4273 case SHIFT_ASHIFT:
4274 gcc_unreachable ();
4275 case SHIFT_LSHIFTRT:
4276 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";
4277 goto end;
4278 case SHIFT_ASHIFTRT:
4279 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";
4280 goto end;
4283 else if (count == 15 && !TARGET_H8300)
4285 switch (shift_type)
4287 case SHIFT_ASHIFT:
4288 info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
4289 info->cc_special = CC_SET_ZNV;
4290 goto end;
4291 case SHIFT_LSHIFTRT:
4292 info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
4293 info->cc_special = CC_SET_ZNV;
4294 goto end;
4295 case SHIFT_ASHIFTRT:
4296 gcc_unreachable ();
4299 else if ((TARGET_H8300 && 16 <= count && count <= 20)
4300 || (TARGET_H8300H && 16 <= count && count <= 19)
4301 || (TARGET_H8300S && 16 <= count && count <= 21))
4303 info->remainder = count - 16;
4305 switch (shift_type)
4307 case SHIFT_ASHIFT:
4308 info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
4309 if (TARGET_H8300)
4310 info->shift1 = "add.w\t%e0,%e0";
4311 goto end;
4312 case SHIFT_LSHIFTRT:
4313 if (TARGET_H8300)
4315 info->special = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
4316 info->shift1 = "shlr\t%x0\n\trotxr\t%w0";
4318 else
4320 info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
4321 info->cc_special = CC_SET_ZNV;
4323 goto end;
4324 case SHIFT_ASHIFTRT:
4325 if (TARGET_H8300)
4327 info->special = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
4328 info->shift1 = "shar\t%x0\n\trotxr\t%w0";
4330 else
4332 info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
4333 info->cc_special = CC_SET_ZNV;
4335 goto end;
4338 else if (TARGET_H8300 && 24 <= count && count <= 28)
4340 info->remainder = count - 24;
4342 switch (shift_type)
4344 case SHIFT_ASHIFT:
4345 info->special = "mov.b\t%w0,%z0\n\tsub.b\t%y0,%y0\n\tsub.w\t%f0,%f0";
4346 info->shift1 = "shll.b\t%z0";
4347 info->cc_inline = CC_SET_ZNV;
4348 goto end;
4349 case SHIFT_LSHIFTRT:
4350 info->special = "mov.b\t%z0,%w0\n\tsub.b\t%x0,%x0\n\tsub.w\t%e0,%e0";
4351 info->shift1 = "shlr.b\t%w0";
4352 info->cc_inline = CC_SET_ZNV;
4353 goto end;
4354 case SHIFT_ASHIFTRT:
4355 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";
4356 info->shift1 = "shar.b\t%w0";
4357 info->cc_inline = CC_SET_ZNV;
4358 goto end;
4361 else if ((TARGET_H8300H && count == 24)
4362 || (TARGET_H8300S && 24 <= count && count <= 25))
4364 info->remainder = count - 24;
4366 switch (shift_type)
4368 case SHIFT_ASHIFT:
4369 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";
4370 goto end;
4371 case SHIFT_LSHIFTRT:
4372 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
4373 info->cc_special = CC_SET_ZNV;
4374 goto end;
4375 case SHIFT_ASHIFTRT:
4376 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
4377 info->cc_special = CC_SET_ZNV;
4378 goto end;
4381 else if (!TARGET_H8300 && count == 28)
4383 switch (shift_type)
4385 case SHIFT_ASHIFT:
4386 if (TARGET_H8300H)
4387 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";
4388 else
4389 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";
4390 goto end;
4391 case SHIFT_LSHIFTRT:
4392 if (TARGET_H8300H)
4394 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";
4395 info->cc_special = CC_SET_ZNV;
4397 else
4398 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4399 goto end;
4400 case SHIFT_ASHIFTRT:
4401 gcc_unreachable ();
4404 else if (!TARGET_H8300 && count == 29)
4406 switch (shift_type)
4408 case SHIFT_ASHIFT:
4409 if (TARGET_H8300H)
4410 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";
4411 else
4412 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4413 goto end;
4414 case SHIFT_LSHIFTRT:
4415 if (TARGET_H8300H)
4417 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";
4418 info->cc_special = CC_SET_ZNV;
4420 else
4422 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4423 info->cc_special = CC_SET_ZNV;
4425 goto end;
4426 case SHIFT_ASHIFTRT:
4427 gcc_unreachable ();
4430 else if (!TARGET_H8300 && count == 30)
4432 switch (shift_type)
4434 case SHIFT_ASHIFT:
4435 if (TARGET_H8300H)
4436 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4437 else
4438 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
4439 goto end;
4440 case SHIFT_LSHIFTRT:
4441 if (TARGET_H8300H)
4442 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4443 else
4444 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4445 goto end;
4446 case SHIFT_ASHIFTRT:
4447 gcc_unreachable ();
4450 else if (count == 31)
4452 if (TARGET_H8300)
4454 switch (shift_type)
4456 case SHIFT_ASHIFT:
4457 info->special = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
4458 goto end;
4459 case SHIFT_LSHIFTRT:
4460 info->special = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
4461 goto end;
4462 case SHIFT_ASHIFTRT:
4463 info->special = "shll\t%z0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
4464 goto end;
4467 else
4469 switch (shift_type)
4471 case SHIFT_ASHIFT:
4472 info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
4473 info->cc_special = CC_SET_ZNV;
4474 goto end;
4475 case SHIFT_LSHIFTRT:
4476 info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
4477 info->cc_special = CC_SET_ZNV;
4478 goto end;
4479 case SHIFT_ASHIFTRT:
4480 info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
4481 info->cc_special = CC_SET_ZNV;
4482 goto end;
4486 gcc_unreachable ();
4488 default:
4489 gcc_unreachable ();
4492 end:
4493 if (!TARGET_H8300S)
4494 info->shift2 = NULL;
4497 /* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
4498 needed for some shift with COUNT and MODE. Return 0 otherwise. */
4501 h8300_shift_needs_scratch_p (int count, enum machine_mode mode)
4503 enum h8_cpu cpu;
4504 int a, lr, ar;
4506 if (GET_MODE_BITSIZE (mode) <= count)
4507 return 1;
4509 /* Find out the target CPU. */
4510 if (TARGET_H8300)
4511 cpu = H8_300;
4512 else if (TARGET_H8300H)
4513 cpu = H8_300H;
4514 else
4515 cpu = H8_S;
4517 /* Find the shift algorithm. */
4518 switch (mode)
4520 case QImode:
4521 a = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
4522 lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
4523 ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
4524 break;
4526 case HImode:
4527 a = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
4528 lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
4529 ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
4530 break;
4532 case SImode:
4533 a = shift_alg_si[cpu][SHIFT_ASHIFT][count];
4534 lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
4535 ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4536 break;
4538 default:
4539 gcc_unreachable ();
4542 /* On H8/300H, count == 8 uses a scratch register. */
4543 return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4544 || (TARGET_H8300H && mode == SImode && count == 8));
4547 /* Output the assembler code for doing shifts. */
4549 const char *
4550 output_a_shift (rtx *operands)
4552 static int loopend_lab;
4553 rtx shift = operands[3];
4554 enum machine_mode mode = GET_MODE (shift);
4555 enum rtx_code code = GET_CODE (shift);
4556 enum shift_type shift_type;
4557 enum shift_mode shift_mode;
4558 struct shift_info info;
4559 int n;
4561 loopend_lab++;
4563 switch (mode)
4565 case QImode:
4566 shift_mode = QIshift;
4567 break;
4568 case HImode:
4569 shift_mode = HIshift;
4570 break;
4571 case SImode:
4572 shift_mode = SIshift;
4573 break;
4574 default:
4575 gcc_unreachable ();
4578 switch (code)
4580 case ASHIFTRT:
4581 shift_type = SHIFT_ASHIFTRT;
4582 break;
4583 case LSHIFTRT:
4584 shift_type = SHIFT_LSHIFTRT;
4585 break;
4586 case ASHIFT:
4587 shift_type = SHIFT_ASHIFT;
4588 break;
4589 default:
4590 gcc_unreachable ();
4593 /* This case must be taken care of by one of the two splitters
4594 that convert a variable shift into a loop. */
4595 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4597 n = INTVAL (operands[2]);
4599 /* If the count is negative, make it 0. */
4600 if (n < 0)
4601 n = 0;
4602 /* If the count is too big, truncate it.
4603 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4604 do the intuitive thing. */
4605 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4606 n = GET_MODE_BITSIZE (mode);
4608 get_shift_alg (shift_type, shift_mode, n, &info);
4610 switch (info.alg)
4612 case SHIFT_SPECIAL:
4613 output_asm_insn (info.special, operands);
4614 /* Fall through. */
4616 case SHIFT_INLINE:
4617 n = info.remainder;
4619 /* Emit two bit shifts first. */
4620 if (info.shift2 != NULL)
4622 for (; n > 1; n -= 2)
4623 output_asm_insn (info.shift2, operands);
4626 /* Now emit one bit shifts for any residual. */
4627 for (; n > 0; n--)
4628 output_asm_insn (info.shift1, operands);
4629 return "";
4631 case SHIFT_ROT_AND:
4633 int m = GET_MODE_BITSIZE (mode) - n;
4634 const int mask = (shift_type == SHIFT_ASHIFT
4635 ? ((1 << m) - 1) << n
4636 : (1 << m) - 1);
4637 char insn_buf[200];
4639 /* Not all possibilities of rotate are supported. They shouldn't
4640 be generated, but let's watch for 'em. */
4641 gcc_assert (info.shift1);
4643 /* Emit two bit rotates first. */
4644 if (info.shift2 != NULL)
4646 for (; m > 1; m -= 2)
4647 output_asm_insn (info.shift2, operands);
4650 /* Now single bit rotates for any residual. */
4651 for (; m > 0; m--)
4652 output_asm_insn (info.shift1, operands);
4654 /* Now mask off the high bits. */
4655 switch (mode)
4657 case QImode:
4658 sprintf (insn_buf, "and\t#%d,%%X0", mask);
4659 break;
4661 case HImode:
4662 gcc_assert (TARGET_H8300H || TARGET_H8300S);
4663 sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4664 break;
4666 default:
4667 gcc_unreachable ();
4670 output_asm_insn (insn_buf, operands);
4671 return "";
4674 case SHIFT_LOOP:
4675 /* A loop to shift by a "large" constant value.
4676 If we have shift-by-2 insns, use them. */
4677 if (info.shift2 != NULL)
4679 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n / 2,
4680 names_big[REGNO (operands[4])]);
4681 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4682 output_asm_insn (info.shift2, operands);
4683 output_asm_insn ("add #0xff,%X4", operands);
4684 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4685 if (n % 2)
4686 output_asm_insn (info.shift1, operands);
4688 else
4690 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n,
4691 names_big[REGNO (operands[4])]);
4692 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4693 output_asm_insn (info.shift1, operands);
4694 output_asm_insn ("add #0xff,%X4", operands);
4695 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4697 return "";
4699 default:
4700 gcc_unreachable ();
4704 /* Count the number of assembly instructions in a string TEMPL. */
4706 static unsigned int
4707 h8300_asm_insn_count (const char *templ)
4709 unsigned int count = 1;
4711 for (; *templ; templ++)
4712 if (*templ == '\n')
4713 count++;
4715 return count;
4718 /* Compute the length of a shift insn. */
4720 unsigned int
4721 compute_a_shift_length (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4723 rtx shift = operands[3];
4724 enum machine_mode mode = GET_MODE (shift);
4725 enum rtx_code code = GET_CODE (shift);
4726 enum shift_type shift_type;
4727 enum shift_mode shift_mode;
4728 struct shift_info info;
4729 unsigned int wlength = 0;
4731 switch (mode)
4733 case QImode:
4734 shift_mode = QIshift;
4735 break;
4736 case HImode:
4737 shift_mode = HIshift;
4738 break;
4739 case SImode:
4740 shift_mode = SIshift;
4741 break;
4742 default:
4743 gcc_unreachable ();
4746 switch (code)
4748 case ASHIFTRT:
4749 shift_type = SHIFT_ASHIFTRT;
4750 break;
4751 case LSHIFTRT:
4752 shift_type = SHIFT_LSHIFTRT;
4753 break;
4754 case ASHIFT:
4755 shift_type = SHIFT_ASHIFT;
4756 break;
4757 default:
4758 gcc_unreachable ();
4761 if (GET_CODE (operands[2]) != CONST_INT)
4763 /* Get the assembler code to do one shift. */
4764 get_shift_alg (shift_type, shift_mode, 1, &info);
4766 return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4768 else
4770 int n = INTVAL (operands[2]);
4772 /* If the count is negative, make it 0. */
4773 if (n < 0)
4774 n = 0;
4775 /* If the count is too big, truncate it.
4776 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4777 do the intuitive thing. */
4778 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4779 n = GET_MODE_BITSIZE (mode);
4781 get_shift_alg (shift_type, shift_mode, n, &info);
4783 switch (info.alg)
4785 case SHIFT_SPECIAL:
4786 wlength += h8300_asm_insn_count (info.special);
4788 /* Every assembly instruction used in SHIFT_SPECIAL case
4789 takes 2 bytes except xor.l, which takes 4 bytes, so if we
4790 see xor.l, we just pretend that xor.l counts as two insns
4791 so that the insn length will be computed correctly. */
4792 if (strstr (info.special, "xor.l") != NULL)
4793 wlength++;
4795 /* Fall through. */
4797 case SHIFT_INLINE:
4798 n = info.remainder;
4800 if (info.shift2 != NULL)
4802 wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4803 n = n % 2;
4806 wlength += h8300_asm_insn_count (info.shift1) * n;
4808 return 2 * wlength;
4810 case SHIFT_ROT_AND:
4812 int m = GET_MODE_BITSIZE (mode) - n;
4814 /* Not all possibilities of rotate are supported. They shouldn't
4815 be generated, but let's watch for 'em. */
4816 gcc_assert (info.shift1);
4818 if (info.shift2 != NULL)
4820 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4821 m = m % 2;
4824 wlength += h8300_asm_insn_count (info.shift1) * m;
4826 /* Now mask off the high bits. */
4827 switch (mode)
4829 case QImode:
4830 wlength += 1;
4831 break;
4832 case HImode:
4833 wlength += 2;
4834 break;
4835 case SImode:
4836 gcc_assert (!TARGET_H8300);
4837 wlength += 3;
4838 break;
4839 default:
4840 gcc_unreachable ();
4842 return 2 * wlength;
4845 case SHIFT_LOOP:
4846 /* A loop to shift by a "large" constant value.
4847 If we have shift-by-2 insns, use them. */
4848 if (info.shift2 != NULL)
4850 wlength += 3 + h8300_asm_insn_count (info.shift2);
4851 if (n % 2)
4852 wlength += h8300_asm_insn_count (info.shift1);
4854 else
4856 wlength += 3 + h8300_asm_insn_count (info.shift1);
4858 return 2 * wlength;
4860 default:
4861 gcc_unreachable ();
4866 /* Compute which flag bits are valid after a shift insn. */
4868 enum attr_cc
4869 compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4871 rtx shift = operands[3];
4872 enum machine_mode mode = GET_MODE (shift);
4873 enum rtx_code code = GET_CODE (shift);
4874 enum shift_type shift_type;
4875 enum shift_mode shift_mode;
4876 struct shift_info info;
4877 int n;
4879 switch (mode)
4881 case QImode:
4882 shift_mode = QIshift;
4883 break;
4884 case HImode:
4885 shift_mode = HIshift;
4886 break;
4887 case SImode:
4888 shift_mode = SIshift;
4889 break;
4890 default:
4891 gcc_unreachable ();
4894 switch (code)
4896 case ASHIFTRT:
4897 shift_type = SHIFT_ASHIFTRT;
4898 break;
4899 case LSHIFTRT:
4900 shift_type = SHIFT_LSHIFTRT;
4901 break;
4902 case ASHIFT:
4903 shift_type = SHIFT_ASHIFT;
4904 break;
4905 default:
4906 gcc_unreachable ();
4909 /* This case must be taken care of by one of the two splitters
4910 that convert a variable shift into a loop. */
4911 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4913 n = INTVAL (operands[2]);
4915 /* If the count is negative, make it 0. */
4916 if (n < 0)
4917 n = 0;
4918 /* If the count is too big, truncate it.
4919 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4920 do the intuitive thing. */
4921 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4922 n = GET_MODE_BITSIZE (mode);
4924 get_shift_alg (shift_type, shift_mode, n, &info);
4926 switch (info.alg)
4928 case SHIFT_SPECIAL:
4929 if (info.remainder == 0)
4930 return info.cc_special;
4932 /* Fall through. */
4934 case SHIFT_INLINE:
4935 return info.cc_inline;
4937 case SHIFT_ROT_AND:
4938 /* This case always ends with an and instruction. */
4939 return CC_SET_ZNV;
4941 case SHIFT_LOOP:
4942 /* A loop to shift by a "large" constant value.
4943 If we have shift-by-2 insns, use them. */
4944 if (info.shift2 != NULL)
4946 if (n % 2)
4947 return info.cc_inline;
4949 return CC_CLOBBER;
4951 default:
4952 gcc_unreachable ();
4956 /* A rotation by a non-constant will cause a loop to be generated, in
4957 which a rotation by one bit is used. A rotation by a constant,
4958 including the one in the loop, will be taken care of by
4959 output_a_rotate () at the insn emit time. */
4962 expand_a_rotate (rtx operands[])
4964 rtx dst = operands[0];
4965 rtx src = operands[1];
4966 rtx rotate_amount = operands[2];
4967 enum machine_mode mode = GET_MODE (dst);
4969 if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4970 return false;
4972 /* We rotate in place. */
4973 emit_move_insn (dst, src);
4975 if (GET_CODE (rotate_amount) != CONST_INT)
4977 rtx counter = gen_reg_rtx (QImode);
4978 rtx start_label = gen_label_rtx ();
4979 rtx end_label = gen_label_rtx ();
4981 /* If the rotate amount is less than or equal to 0,
4982 we go out of the loop. */
4983 emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
4984 QImode, 0, end_label);
4986 /* Initialize the loop counter. */
4987 emit_move_insn (counter, rotate_amount);
4989 emit_label (start_label);
4991 /* Rotate by one bit. */
4992 switch (mode)
4994 case QImode:
4995 emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
4996 break;
4997 case HImode:
4998 emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
4999 break;
5000 case SImode:
5001 emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
5002 break;
5003 default:
5004 gcc_unreachable ();
5007 /* Decrement the counter by 1. */
5008 emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
5010 /* If the loop counter is nonzero, we go back to the beginning
5011 of the loop. */
5012 emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
5013 start_label);
5015 emit_label (end_label);
5017 else
5019 /* Rotate by AMOUNT bits. */
5020 switch (mode)
5022 case QImode:
5023 emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
5024 break;
5025 case HImode:
5026 emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
5027 break;
5028 case SImode:
5029 emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
5030 break;
5031 default:
5032 gcc_unreachable ();
5036 return 1;
5039 /* Output a rotate insn. */
5041 const char *
5042 output_a_rotate (enum rtx_code code, rtx *operands)
5044 rtx dst = operands[0];
5045 rtx rotate_amount = operands[2];
5046 enum shift_mode rotate_mode;
5047 enum shift_type rotate_type;
5048 const char *insn_buf;
5049 int bits;
5050 int amount;
5051 enum machine_mode mode = GET_MODE (dst);
5053 gcc_assert (GET_CODE (rotate_amount) == CONST_INT);
5055 switch (mode)
5057 case QImode:
5058 rotate_mode = QIshift;
5059 break;
5060 case HImode:
5061 rotate_mode = HIshift;
5062 break;
5063 case SImode:
5064 rotate_mode = SIshift;
5065 break;
5066 default:
5067 gcc_unreachable ();
5070 switch (code)
5072 case ROTATERT:
5073 rotate_type = SHIFT_ASHIFT;
5074 break;
5075 case ROTATE:
5076 rotate_type = SHIFT_LSHIFTRT;
5077 break;
5078 default:
5079 gcc_unreachable ();
5082 amount = INTVAL (rotate_amount);
5084 /* Clean up AMOUNT. */
5085 if (amount < 0)
5086 amount = 0;
5087 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5088 amount = GET_MODE_BITSIZE (mode);
5090 /* Determine the faster direction. After this phase, amount will be
5091 at most a half of GET_MODE_BITSIZE (mode). */
5092 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5094 /* Flip the direction. */
5095 amount = GET_MODE_BITSIZE (mode) - amount;
5096 rotate_type =
5097 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5100 /* See if a byte swap (in HImode) or a word swap (in SImode) can
5101 boost up the rotation. */
5102 if ((mode == HImode && TARGET_H8300 && amount >= 5)
5103 || (mode == HImode && TARGET_H8300H && amount >= 6)
5104 || (mode == HImode && TARGET_H8300S && amount == 8)
5105 || (mode == SImode && TARGET_H8300H && amount >= 10)
5106 || (mode == SImode && TARGET_H8300S && amount >= 13))
5108 switch (mode)
5110 case HImode:
5111 /* This code works on any family. */
5112 insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
5113 output_asm_insn (insn_buf, operands);
5114 break;
5116 case SImode:
5117 /* This code works on the H8/300H and H8S. */
5118 insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
5119 output_asm_insn (insn_buf, operands);
5120 break;
5122 default:
5123 gcc_unreachable ();
5126 /* Adjust AMOUNT and flip the direction. */
5127 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5128 rotate_type =
5129 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5132 /* Output rotate insns. */
5133 for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
5135 if (bits == 2)
5136 insn_buf = rotate_two[rotate_type][rotate_mode];
5137 else
5138 insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
5140 for (; amount >= bits; amount -= bits)
5141 output_asm_insn (insn_buf, operands);
5144 return "";
5147 /* Compute the length of a rotate insn. */
5149 unsigned int
5150 compute_a_rotate_length (rtx *operands)
5152 rtx src = operands[1];
5153 rtx amount_rtx = operands[2];
5154 enum machine_mode mode = GET_MODE (src);
5155 int amount;
5156 unsigned int length = 0;
5158 gcc_assert (GET_CODE (amount_rtx) == CONST_INT);
5160 amount = INTVAL (amount_rtx);
5162 /* Clean up AMOUNT. */
5163 if (amount < 0)
5164 amount = 0;
5165 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5166 amount = GET_MODE_BITSIZE (mode);
5168 /* Determine the faster direction. After this phase, amount
5169 will be at most a half of GET_MODE_BITSIZE (mode). */
5170 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5171 /* Flip the direction. */
5172 amount = GET_MODE_BITSIZE (mode) - amount;
5174 /* See if a byte swap (in HImode) or a word swap (in SImode) can
5175 boost up the rotation. */
5176 if ((mode == HImode && TARGET_H8300 && amount >= 5)
5177 || (mode == HImode && TARGET_H8300H && amount >= 6)
5178 || (mode == HImode && TARGET_H8300S && amount == 8)
5179 || (mode == SImode && TARGET_H8300H && amount >= 10)
5180 || (mode == SImode && TARGET_H8300S && amount >= 13))
5182 /* Adjust AMOUNT and flip the direction. */
5183 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5184 length += 6;
5187 /* We use 2-bit rotations on the H8S. */
5188 if (TARGET_H8300S)
5189 amount = amount / 2 + amount % 2;
5191 /* The H8/300 uses three insns to rotate one bit, taking 6
5192 length. */
5193 length += amount * ((TARGET_H8300 && mode == HImode) ? 6 : 2);
5195 return length;
5198 /* Fix the operands of a gen_xxx so that it could become a bit
5199 operating insn. */
5202 fix_bit_operand (rtx *operands, enum rtx_code code)
5204 /* The bit_operand predicate accepts any memory during RTL generation, but
5205 only 'U' memory afterwards, so if this is a MEM operand, we must force
5206 it to be valid for 'U' by reloading the address. */
5208 if (code == AND
5209 ? single_zero_operand (operands[2], QImode)
5210 : single_one_operand (operands[2], QImode))
5212 /* OK to have a memory dest. */
5213 if (GET_CODE (operands[0]) == MEM
5214 && !satisfies_constraint_U (operands[0]))
5216 rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
5217 copy_to_mode_reg (Pmode,
5218 XEXP (operands[0], 0)));
5219 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5220 operands[0] = mem;
5223 if (GET_CODE (operands[1]) == MEM
5224 && !satisfies_constraint_U (operands[1]))
5226 rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
5227 copy_to_mode_reg (Pmode,
5228 XEXP (operands[1], 0)));
5229 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5230 operands[1] = mem;
5232 return 0;
5235 /* Dest and src op must be register. */
5237 operands[1] = force_reg (QImode, operands[1]);
5239 rtx res = gen_reg_rtx (QImode);
5240 switch (code)
5242 case AND:
5243 emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
5244 break;
5245 case IOR:
5246 emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
5247 break;
5248 case XOR:
5249 emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
5250 break;
5251 default:
5252 gcc_unreachable ();
5254 emit_insn (gen_movqi (operands[0], res));
5256 return 1;
5259 /* Return nonzero if FUNC is an interrupt function as specified
5260 by the "interrupt" attribute. */
5262 static int
5263 h8300_interrupt_function_p (tree func)
5265 tree a;
5267 if (TREE_CODE (func) != FUNCTION_DECL)
5268 return 0;
5270 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
5271 return a != NULL_TREE;
5274 /* Return nonzero if FUNC is a saveall function as specified by the
5275 "saveall" attribute. */
5277 static int
5278 h8300_saveall_function_p (tree func)
5280 tree a;
5282 if (TREE_CODE (func) != FUNCTION_DECL)
5283 return 0;
5285 a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
5286 return a != NULL_TREE;
5289 /* Return nonzero if FUNC is an OS_Task function as specified
5290 by the "OS_Task" attribute. */
5292 static int
5293 h8300_os_task_function_p (tree func)
5295 tree a;
5297 if (TREE_CODE (func) != FUNCTION_DECL)
5298 return 0;
5300 a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
5301 return a != NULL_TREE;
5304 /* Return nonzero if FUNC is a monitor function as specified
5305 by the "monitor" attribute. */
5307 static int
5308 h8300_monitor_function_p (tree func)
5310 tree a;
5312 if (TREE_CODE (func) != FUNCTION_DECL)
5313 return 0;
5315 a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
5316 return a != NULL_TREE;
5319 /* Return nonzero if FUNC is a function that should be called
5320 through the function vector. */
5323 h8300_funcvec_function_p (tree func)
5325 tree a;
5327 if (TREE_CODE (func) != FUNCTION_DECL)
5328 return 0;
5330 a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
5331 return a != NULL_TREE;
5334 /* Return nonzero if DECL is a variable that's in the eight bit
5335 data area. */
5338 h8300_eightbit_data_p (tree decl)
5340 tree a;
5342 if (TREE_CODE (decl) != VAR_DECL)
5343 return 0;
5345 a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
5346 return a != NULL_TREE;
5349 /* Return nonzero if DECL is a variable that's in the tiny
5350 data area. */
5353 h8300_tiny_data_p (tree decl)
5355 tree a;
5357 if (TREE_CODE (decl) != VAR_DECL)
5358 return 0;
5360 a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
5361 return a != NULL_TREE;
5364 /* Generate an 'interrupt_handler' attribute for decls. We convert
5365 all the pragmas to corresponding attributes. */
5367 static void
5368 h8300_insert_attributes (tree node, tree *attributes)
5370 if (TREE_CODE (node) == FUNCTION_DECL)
5372 if (pragma_interrupt)
5374 pragma_interrupt = 0;
5376 /* Add an 'interrupt_handler' attribute. */
5377 *attributes = tree_cons (get_identifier ("interrupt_handler"),
5378 NULL, *attributes);
5381 if (pragma_saveall)
5383 pragma_saveall = 0;
5385 /* Add an 'saveall' attribute. */
5386 *attributes = tree_cons (get_identifier ("saveall"),
5387 NULL, *attributes);
5392 /* Supported attributes:
5394 interrupt_handler: output a prologue and epilogue suitable for an
5395 interrupt handler.
5397 saveall: output a prologue and epilogue that saves and restores
5398 all registers except the stack pointer.
5400 function_vector: This function should be called through the
5401 function vector.
5403 eightbit_data: This variable lives in the 8-bit data area and can
5404 be referenced with 8-bit absolute memory addresses.
5406 tiny_data: This variable lives in the tiny data area and can be
5407 referenced with 16-bit absolute memory references. */
5409 static const struct attribute_spec h8300_attribute_table[] =
5411 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
5412 affects_type_identity } */
5413 { "interrupt_handler", 0, 0, true, false, false,
5414 h8300_handle_fndecl_attribute, false },
5415 { "saveall", 0, 0, true, false, false,
5416 h8300_handle_fndecl_attribute, false },
5417 { "OS_Task", 0, 0, true, false, false,
5418 h8300_handle_fndecl_attribute, false },
5419 { "monitor", 0, 0, true, false, false,
5420 h8300_handle_fndecl_attribute, false },
5421 { "function_vector", 0, 0, true, false, false,
5422 h8300_handle_fndecl_attribute, false },
5423 { "eightbit_data", 0, 0, true, false, false,
5424 h8300_handle_eightbit_data_attribute, false },
5425 { "tiny_data", 0, 0, true, false, false,
5426 h8300_handle_tiny_data_attribute, false },
5427 { NULL, 0, 0, false, false, false, NULL, false }
5431 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
5432 struct attribute_spec.handler. */
5433 static tree
5434 h8300_handle_fndecl_attribute (tree *node, tree name,
5435 tree args ATTRIBUTE_UNUSED,
5436 int flags ATTRIBUTE_UNUSED,
5437 bool *no_add_attrs)
5439 if (TREE_CODE (*node) != FUNCTION_DECL)
5441 warning (OPT_Wattributes, "%qE attribute only applies to functions",
5442 name);
5443 *no_add_attrs = true;
5446 return NULL_TREE;
5449 /* Handle an "eightbit_data" attribute; arguments as in
5450 struct attribute_spec.handler. */
5451 static tree
5452 h8300_handle_eightbit_data_attribute (tree *node, tree name,
5453 tree args ATTRIBUTE_UNUSED,
5454 int flags ATTRIBUTE_UNUSED,
5455 bool *no_add_attrs)
5457 tree decl = *node;
5459 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5461 DECL_SECTION_NAME (decl) = build_string (7, ".eight");
5463 else
5465 warning (OPT_Wattributes, "%qE attribute ignored",
5466 name);
5467 *no_add_attrs = true;
5470 return NULL_TREE;
5473 /* Handle an "tiny_data" attribute; arguments as in
5474 struct attribute_spec.handler. */
5475 static tree
5476 h8300_handle_tiny_data_attribute (tree *node, tree name,
5477 tree args ATTRIBUTE_UNUSED,
5478 int flags ATTRIBUTE_UNUSED,
5479 bool *no_add_attrs)
5481 tree decl = *node;
5483 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5485 DECL_SECTION_NAME (decl) = build_string (6, ".tiny");
5487 else
5489 warning (OPT_Wattributes, "%qE attribute ignored",
5490 name);
5491 *no_add_attrs = true;
5494 return NULL_TREE;
5497 /* Mark function vectors, and various small data objects. */
5499 static void
5500 h8300_encode_section_info (tree decl, rtx rtl, int first)
5502 int extra_flags = 0;
5504 default_encode_section_info (decl, rtl, first);
5506 if (TREE_CODE (decl) == FUNCTION_DECL
5507 && h8300_funcvec_function_p (decl))
5508 extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
5509 else if (TREE_CODE (decl) == VAR_DECL
5510 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
5512 if (h8300_eightbit_data_p (decl))
5513 extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
5514 else if (first && h8300_tiny_data_p (decl))
5515 extra_flags = SYMBOL_FLAG_TINY_DATA;
5518 if (extra_flags)
5519 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
5522 /* Output a single-bit extraction. */
5524 const char *
5525 output_simode_bld (int bild, rtx operands[])
5527 if (TARGET_H8300)
5529 /* Clear the destination register. */
5530 output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
5532 /* Now output the bit load or bit inverse load, and store it in
5533 the destination. */
5534 if (bild)
5535 output_asm_insn ("bild\t%Z2,%Y1", operands);
5536 else
5537 output_asm_insn ("bld\t%Z2,%Y1", operands);
5539 output_asm_insn ("bst\t#0,%w0", operands);
5541 else
5543 /* Determine if we can clear the destination first. */
5544 int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5545 && REGNO (operands[0]) != REGNO (operands[1]));
5547 if (clear_first)
5548 output_asm_insn ("sub.l\t%S0,%S0", operands);
5550 /* Output the bit load or bit inverse load. */
5551 if (bild)
5552 output_asm_insn ("bild\t%Z2,%Y1", operands);
5553 else
5554 output_asm_insn ("bld\t%Z2,%Y1", operands);
5556 if (!clear_first)
5557 output_asm_insn ("xor.l\t%S0,%S0", operands);
5559 /* Perform the bit store. */
5560 output_asm_insn ("rotxl.l\t%S0", operands);
5563 /* All done. */
5564 return "";
5567 /* Delayed-branch scheduling is more effective if we have some idea
5568 how long each instruction will be. Use a shorten_branches pass
5569 to get an initial estimate. */
5571 static void
5572 h8300_reorg (void)
5574 if (flag_delayed_branch)
5575 shorten_branches (get_insns ());
5578 #ifndef OBJECT_FORMAT_ELF
5579 static void
5580 h8300_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED,
5581 tree decl)
5583 /* ??? Perhaps we should be using default_coff_asm_named_section. */
5584 fprintf (asm_out_file, "\t.section %s\n", name);
5586 #endif /* ! OBJECT_FORMAT_ELF */
5588 /* Nonzero if X is a constant address suitable as an 8-bit absolute,
5589 which is a special case of the 'R' operand. */
5592 h8300_eightbit_constant_address_p (rtx x)
5594 /* The ranges of the 8-bit area. */
5595 const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5596 const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
5597 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5598 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5599 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5600 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5602 unsigned HOST_WIDE_INT addr;
5604 /* We accept symbols declared with eightbit_data. */
5605 if (GET_CODE (x) == SYMBOL_REF)
5606 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
5608 if (GET_CODE (x) != CONST_INT)
5609 return 0;
5611 addr = INTVAL (x);
5613 return (0
5614 || ((TARGET_H8300 || TARGET_NORMAL_MODE) && IN_RANGE (addr, n1, n2))
5615 || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5616 || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5619 /* Nonzero if X is a constant address suitable as an 16-bit absolute
5620 on H8/300H and H8S. */
5623 h8300_tiny_constant_address_p (rtx x)
5625 /* The ranges of the 16-bit area. */
5626 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5627 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5628 const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5629 const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5630 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5631 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5632 const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5633 const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5635 unsigned HOST_WIDE_INT addr;
5637 switch (GET_CODE (x))
5639 case SYMBOL_REF:
5640 /* In the normal mode, any symbol fits in the 16-bit absolute
5641 address range. We also accept symbols declared with
5642 tiny_data. */
5643 return (TARGET_NORMAL_MODE
5644 || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
5646 case CONST_INT:
5647 addr = INTVAL (x);
5648 return (TARGET_NORMAL_MODE
5649 || (TARGET_H8300H
5650 && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5651 || (TARGET_H8300S
5652 && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
5654 case CONST:
5655 return TARGET_NORMAL_MODE;
5657 default:
5658 return 0;
5663 /* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5664 locations that can be accessed as a 16-bit word. */
5667 byte_accesses_mergeable_p (rtx addr1, rtx addr2)
5669 HOST_WIDE_INT offset1, offset2;
5670 rtx reg1, reg2;
5672 if (REG_P (addr1))
5674 reg1 = addr1;
5675 offset1 = 0;
5677 else if (GET_CODE (addr1) == PLUS
5678 && REG_P (XEXP (addr1, 0))
5679 && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5681 reg1 = XEXP (addr1, 0);
5682 offset1 = INTVAL (XEXP (addr1, 1));
5684 else
5685 return 0;
5687 if (REG_P (addr2))
5689 reg2 = addr2;
5690 offset2 = 0;
5692 else if (GET_CODE (addr2) == PLUS
5693 && REG_P (XEXP (addr2, 0))
5694 && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5696 reg2 = XEXP (addr2, 0);
5697 offset2 = INTVAL (XEXP (addr2, 1));
5699 else
5700 return 0;
5702 if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5703 || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5704 && offset1 % 2 == 0
5705 && offset1 + 1 == offset2)
5706 return 1;
5708 return 0;
5711 /* Return nonzero if we have the same comparison insn as I3 two insns
5712 before I3. I3 is assumed to be a comparison insn. */
5715 same_cmp_preceding_p (rtx i3)
5717 rtx i1, i2;
5719 /* Make sure we have a sequence of three insns. */
5720 i2 = prev_nonnote_insn (i3);
5721 if (i2 == NULL_RTX)
5722 return 0;
5723 i1 = prev_nonnote_insn (i2);
5724 if (i1 == NULL_RTX)
5725 return 0;
5727 return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5728 && any_condjump_p (i2) && onlyjump_p (i2));
5731 /* Return nonzero if we have the same comparison insn as I1 two insns
5732 after I1. I1 is assumed to be a comparison insn. */
5735 same_cmp_following_p (rtx i1)
5737 rtx i2, i3;
5739 /* Make sure we have a sequence of three insns. */
5740 i2 = next_nonnote_insn (i1);
5741 if (i2 == NULL_RTX)
5742 return 0;
5743 i3 = next_nonnote_insn (i2);
5744 if (i3 == NULL_RTX)
5745 return 0;
5747 return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5748 && any_condjump_p (i2) && onlyjump_p (i2));
5751 /* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
5752 (or pops) N registers. OPERANDS are assumed to be an array of
5753 registers. */
5756 h8300_regs_ok_for_stm (int n, rtx operands[])
5758 switch (n)
5760 case 2:
5761 return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5762 || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5763 || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5764 case 3:
5765 return ((REGNO (operands[0]) == 0
5766 && REGNO (operands[1]) == 1
5767 && REGNO (operands[2]) == 2)
5768 || (REGNO (operands[0]) == 4
5769 && REGNO (operands[1]) == 5
5770 && REGNO (operands[2]) == 6));
5772 case 4:
5773 return (REGNO (operands[0]) == 0
5774 && REGNO (operands[1]) == 1
5775 && REGNO (operands[2]) == 2
5776 && REGNO (operands[3]) == 3);
5777 default:
5778 gcc_unreachable ();
5782 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
5785 h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5786 unsigned int new_reg)
5788 /* Interrupt functions can only use registers that have already been
5789 saved by the prologue, even if they would normally be
5790 call-clobbered. */
5792 if (h8300_current_function_interrupt_function_p ()
5793 && !df_regs_ever_live_p (new_reg))
5794 return 0;
5796 return 1;
5799 /* Returns true if register REGNO is safe to be allocated as a scratch
5800 register in the current function. */
5802 static bool
5803 h8300_hard_regno_scratch_ok (unsigned int regno)
5805 if (h8300_current_function_interrupt_function_p ()
5806 && ! WORD_REG_USED (regno))
5807 return false;
5809 return true;
5813 /* Return nonzero if X is a REG or SUBREG suitable as a base register. */
5815 static int
5816 h8300_rtx_ok_for_base_p (rtx x, int strict)
5818 /* Strip off SUBREG if any. */
5819 if (GET_CODE (x) == SUBREG)
5820 x = SUBREG_REG (x);
5822 return (REG_P (x)
5823 && (strict
5824 ? REG_OK_FOR_BASE_STRICT_P (x)
5825 : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5828 /* Return nozero if X is a legitimate address. On the H8/300, a
5829 legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5830 CONSTANT_ADDRESS. */
5832 static bool
5833 h8300_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
5835 /* The register indirect addresses like @er0 is always valid. */
5836 if (h8300_rtx_ok_for_base_p (x, strict))
5837 return 1;
5839 if (CONSTANT_ADDRESS_P (x))
5840 return 1;
5842 if (TARGET_H8300SX
5843 && ( GET_CODE (x) == PRE_INC
5844 || GET_CODE (x) == PRE_DEC
5845 || GET_CODE (x) == POST_INC
5846 || GET_CODE (x) == POST_DEC)
5847 && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5848 return 1;
5850 if (GET_CODE (x) == PLUS
5851 && CONSTANT_ADDRESS_P (XEXP (x, 1))
5852 && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5853 mode, 0), strict))
5854 return 1;
5856 return 0;
5859 /* Worker function for HARD_REGNO_NREGS.
5861 We pretend the MAC register is 32bits -- we don't have any data
5862 types on the H8 series to handle more than 32bits. */
5865 h8300_hard_regno_nregs (int regno ATTRIBUTE_UNUSED, enum machine_mode mode)
5867 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5870 /* Worker function for HARD_REGNO_MODE_OK. */
5873 h8300_hard_regno_mode_ok (int regno, enum machine_mode mode)
5875 if (TARGET_H8300)
5876 /* If an even reg, then anything goes. Otherwise the mode must be
5877 QI or HI. */
5878 return ((regno & 1) == 0) || (mode == HImode) || (mode == QImode);
5879 else
5880 /* MAC register can only be of SImode. Otherwise, anything
5881 goes. */
5882 return regno == MAC_REG ? mode == SImode : 1;
5885 /* Helper function for the move patterns. Make sure a move is legitimate. */
5887 bool
5888 h8300_move_ok (rtx dest, rtx src)
5890 rtx addr, other;
5892 /* Validate that at least one operand is a register. */
5893 if (MEM_P (dest))
5895 if (MEM_P (src) || CONSTANT_P (src))
5896 return false;
5897 addr = XEXP (dest, 0);
5898 other = src;
5900 else if (MEM_P (src))
5902 addr = XEXP (src, 0);
5903 other = dest;
5905 else
5906 return true;
5908 /* Validate that auto-inc doesn't affect OTHER. */
5909 if (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC)
5910 return true;
5911 addr = XEXP (addr, 0);
5913 if (addr == stack_pointer_rtx)
5914 return register_no_sp_elim_operand (other, VOIDmode);
5915 else
5916 return !reg_overlap_mentioned_p(other, addr);
5919 /* Perform target dependent optabs initialization. */
5920 static void
5921 h8300_init_libfuncs (void)
5923 set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5924 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5925 set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5926 set_optab_libfunc (smod_optab, HImode, "__modhi3");
5927 set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5930 /* Worker function for TARGET_FUNCTION_VALUE.
5932 On the H8 the return value is in R0/R1. */
5934 static rtx
5935 h8300_function_value (const_tree ret_type,
5936 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
5937 bool outgoing ATTRIBUTE_UNUSED)
5939 return gen_rtx_REG (TYPE_MODE (ret_type), R0_REG);
5942 /* Worker function for TARGET_LIBCALL_VALUE.
5944 On the H8 the return value is in R0/R1. */
5946 static rtx
5947 h8300_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5949 return gen_rtx_REG (mode, R0_REG);
5952 /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P.
5954 On the H8, R0 is the only register thus used. */
5956 static bool
5957 h8300_function_value_regno_p (const unsigned int regno)
5959 return (regno == R0_REG);
5962 /* Worker function for TARGET_RETURN_IN_MEMORY. */
5964 static bool
5965 h8300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
5967 return (TYPE_MODE (type) == BLKmode
5968 || GET_MODE_SIZE (TYPE_MODE (type)) > (TARGET_H8300 ? 4 : 8));
5971 /* We emit the entire trampoline here. Depending on the pointer size,
5972 we use a different trampoline.
5974 Pmode == HImode
5975 vvvv context
5976 1 0000 7903xxxx mov.w #0x1234,r3
5977 2 0004 5A00xxxx jmp @0x1234
5978 ^^^^ function
5980 Pmode == SImode
5981 vvvvvvvv context
5982 2 0000 7A03xxxxxxxx mov.l #0x12345678,er3
5983 3 0006 5Axxxxxx jmp @0x123456
5984 ^^^^^^ function
5987 static void
5988 h8300_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
5990 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
5991 rtx mem;
5993 if (Pmode == HImode)
5995 mem = adjust_address (m_tramp, HImode, 0);
5996 emit_move_insn (mem, GEN_INT (0x7903));
5997 mem = adjust_address (m_tramp, Pmode, 2);
5998 emit_move_insn (mem, cxt);
5999 mem = adjust_address (m_tramp, HImode, 4);
6000 emit_move_insn (mem, GEN_INT (0x5a00));
6001 mem = adjust_address (m_tramp, Pmode, 6);
6002 emit_move_insn (mem, fnaddr);
6004 else
6006 rtx tem;
6008 mem = adjust_address (m_tramp, HImode, 0);
6009 emit_move_insn (mem, GEN_INT (0x7a03));
6010 mem = adjust_address (m_tramp, Pmode, 2);
6011 emit_move_insn (mem, cxt);
6013 tem = copy_to_reg (fnaddr);
6014 emit_insn (gen_andsi3 (tem, tem, GEN_INT (0x00ffffff)));
6015 emit_insn (gen_iorsi3 (tem, tem, GEN_INT (0x5a000000)));
6016 mem = adjust_address (m_tramp, SImode, 6);
6017 emit_move_insn (mem, tem);
6021 /* Initialize the GCC target structure. */
6022 #undef TARGET_ATTRIBUTE_TABLE
6023 #define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
6025 #undef TARGET_ASM_ALIGNED_HI_OP
6026 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
6028 #undef TARGET_ASM_FILE_START
6029 #define TARGET_ASM_FILE_START h8300_file_start
6030 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
6031 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
6033 #undef TARGET_ASM_FILE_END
6034 #define TARGET_ASM_FILE_END h8300_file_end
6036 #undef TARGET_PRINT_OPERAND
6037 #define TARGET_PRINT_OPERAND h8300_print_operand
6038 #undef TARGET_PRINT_OPERAND_ADDRESS
6039 #define TARGET_PRINT_OPERAND_ADDRESS h8300_print_operand_address
6040 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
6041 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P h8300_print_operand_punct_valid_p
6043 #undef TARGET_ENCODE_SECTION_INFO
6044 #define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
6046 #undef TARGET_INSERT_ATTRIBUTES
6047 #define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
6049 #undef TARGET_REGISTER_MOVE_COST
6050 #define TARGET_REGISTER_MOVE_COST h8300_register_move_cost
6052 #undef TARGET_RTX_COSTS
6053 #define TARGET_RTX_COSTS h8300_rtx_costs
6055 #undef TARGET_INIT_LIBFUNCS
6056 #define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
6058 #undef TARGET_FUNCTION_VALUE
6059 #define TARGET_FUNCTION_VALUE h8300_function_value
6061 #undef TARGET_LIBCALL_VALUE
6062 #define TARGET_LIBCALL_VALUE h8300_libcall_value
6064 #undef TARGET_FUNCTION_VALUE_REGNO_P
6065 #define TARGET_FUNCTION_VALUE_REGNO_P h8300_function_value_regno_p
6067 #undef TARGET_RETURN_IN_MEMORY
6068 #define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
6070 #undef TARGET_FUNCTION_ARG
6071 #define TARGET_FUNCTION_ARG h8300_function_arg
6073 #undef TARGET_FUNCTION_ARG_ADVANCE
6074 #define TARGET_FUNCTION_ARG_ADVANCE h8300_function_arg_advance
6076 #undef TARGET_MACHINE_DEPENDENT_REORG
6077 #define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
6079 #undef TARGET_HARD_REGNO_SCRATCH_OK
6080 #define TARGET_HARD_REGNO_SCRATCH_OK h8300_hard_regno_scratch_ok
6082 #undef TARGET_LEGITIMATE_ADDRESS_P
6083 #define TARGET_LEGITIMATE_ADDRESS_P h8300_legitimate_address_p
6085 #undef TARGET_CAN_ELIMINATE
6086 #define TARGET_CAN_ELIMINATE h8300_can_eliminate
6088 #undef TARGET_CONDITIONAL_REGISTER_USAGE
6089 #define TARGET_CONDITIONAL_REGISTER_USAGE h8300_conditional_register_usage
6091 #undef TARGET_TRAMPOLINE_INIT
6092 #define TARGET_TRAMPOLINE_INIT h8300_trampoline_init
6094 #undef TARGET_OPTION_OVERRIDE
6095 #define TARGET_OPTION_OVERRIDE h8300_option_override
6097 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
6098 #define TARGET_MODE_DEPENDENT_ADDRESS_P h8300_mode_dependent_address_p
6100 struct gcc_target targetm = TARGET_INITIALIZER;