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
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)
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/>. */
26 #include "coretypes.h"
31 #include "hard-reg-set.h"
33 #include "insn-config.h"
34 #include "conditions.h"
36 #include "insn-attr.h"
47 #include "target-def.h"
49 /* Classifies a h8300_src_operand or h8300_dst_operand.
52 A constant operand of some sort.
58 A memory reference with a constant address.
61 A memory reference with a register as its address.
64 Some other kind of memory reference. */
65 enum h8300_operand_class
75 /* For a general two-operand instruction, element [X][Y] gives
76 the length of the opcode fields when the first operand has class
77 (X + 1) and the second has class Y. */
78 typedef unsigned char h8300_length_table[NUM_H8OPS - 1][NUM_H8OPS];
80 /* Forward declarations. */
81 static const char *byte_reg (rtx, int);
82 static int h8300_interrupt_function_p (tree);
83 static int h8300_saveall_function_p (tree);
84 static int h8300_monitor_function_p (tree);
85 static int h8300_os_task_function_p (tree);
86 static void h8300_emit_stack_adjustment (int, HOST_WIDE_INT, bool);
87 static HOST_WIDE_INT round_frame_size (HOST_WIDE_INT);
88 static unsigned int compute_saved_regs (void);
89 static void push (int);
90 static void pop (int);
91 static const char *cond_string (enum rtx_code);
92 static unsigned int h8300_asm_insn_count (const char *);
93 static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
94 static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
95 static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
96 #ifndef OBJECT_FORMAT_ELF
97 static void h8300_asm_named_section (const char *, unsigned int, tree);
99 static int h8300_and_costs (rtx);
100 static int h8300_shift_costs (rtx);
101 static void h8300_push_pop (int, int, bool, bool);
102 static int h8300_stack_offset_p (rtx, int);
103 static int h8300_ldm_stm_regno (rtx, int, int, int);
104 static void h8300_reorg (void);
105 static unsigned int h8300_constant_length (rtx);
106 static unsigned int h8300_displacement_length (rtx, int);
107 static unsigned int h8300_classify_operand (rtx, int, enum h8300_operand_class *);
108 static unsigned int h8300_length_from_table (rtx, rtx, const h8300_length_table *);
109 static unsigned int h8300_unary_length (rtx);
110 static unsigned int h8300_short_immediate_length (rtx);
111 static unsigned int h8300_bitfield_length (rtx, rtx);
112 static unsigned int h8300_binary_length (rtx, const h8300_length_table *);
113 static bool h8300_short_move_mem_p (rtx, enum rtx_code);
114 static unsigned int h8300_move_length (rtx *, const h8300_length_table *);
115 static bool h8300_hard_regno_scratch_ok (unsigned int);
117 /* CPU_TYPE, says what cpu we're compiling for. */
120 /* True if a #pragma interrupt has been seen for the current function. */
121 static int pragma_interrupt;
123 /* True if a #pragma saveall has been seen for the current function. */
124 static int pragma_saveall;
126 static const char *const names_big[] =
127 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
129 static const char *const names_extended[] =
130 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
132 static const char *const names_upper_extended[] =
133 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
135 /* Points to one of the above. */
136 /* ??? The above could be put in an array indexed by CPU_TYPE. */
137 const char * const *h8_reg_names;
139 /* Various operations needed by the following, indexed by CPU_TYPE. */
141 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
143 /* Value of MOVE_RATIO. */
144 int h8300_move_ratio;
146 /* See below where shifts are handled for explanation of this enum. */
156 /* Symbols of the various shifts which can be used as indices. */
160 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
163 /* Macros to keep the shift algorithm tables small. */
164 #define INL SHIFT_INLINE
165 #define ROT SHIFT_ROT_AND
166 #define LOP SHIFT_LOOP
167 #define SPC SHIFT_SPECIAL
169 /* The shift algorithms for each machine, mode, shift type, and shift
170 count are defined below. The three tables below correspond to
171 QImode, HImode, and SImode, respectively. Each table is organized
172 by, in the order of indices, machine, shift type, and shift count. */
174 static enum shift_alg shift_alg_qi[3][3][8] = {
177 /* 0 1 2 3 4 5 6 7 */
178 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
179 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
180 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
184 /* 0 1 2 3 4 5 6 7 */
185 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
186 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
187 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
191 /* 0 1 2 3 4 5 6 7 */
192 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT */
193 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
194 { INL, INL, INL, INL, INL, INL, INL, SPC } /* SHIFT_ASHIFTRT */
198 static enum shift_alg shift_alg_hi[3][3][16] = {
201 /* 0 1 2 3 4 5 6 7 */
202 /* 8 9 10 11 12 13 14 15 */
203 { INL, INL, INL, INL, INL, INL, INL, SPC,
204 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
205 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
206 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
207 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
208 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
212 /* 0 1 2 3 4 5 6 7 */
213 /* 8 9 10 11 12 13 14 15 */
214 { INL, INL, INL, INL, INL, INL, INL, SPC,
215 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
216 { INL, INL, INL, INL, INL, INL, INL, SPC,
217 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
218 { INL, INL, INL, INL, INL, INL, INL, SPC,
219 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
223 /* 0 1 2 3 4 5 6 7 */
224 /* 8 9 10 11 12 13 14 15 */
225 { INL, INL, INL, INL, INL, INL, INL, INL,
226 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
227 { INL, INL, INL, INL, INL, INL, INL, INL,
228 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
229 { INL, INL, INL, INL, INL, INL, INL, INL,
230 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
234 static enum shift_alg shift_alg_si[3][3][32] = {
237 /* 0 1 2 3 4 5 6 7 */
238 /* 8 9 10 11 12 13 14 15 */
239 /* 16 17 18 19 20 21 22 23 */
240 /* 24 25 26 27 28 29 30 31 */
241 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
242 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
243 SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
244 SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT */
245 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
246 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
247 SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
248 SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
249 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
250 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
251 SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
252 SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
256 /* 0 1 2 3 4 5 6 7 */
257 /* 8 9 10 11 12 13 14 15 */
258 /* 16 17 18 19 20 21 22 23 */
259 /* 24 25 26 27 28 29 30 31 */
260 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
261 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
262 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
263 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
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_LSHIFTRT */
268 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
269 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
270 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
271 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
275 /* 0 1 2 3 4 5 6 7 */
276 /* 8 9 10 11 12 13 14 15 */
277 /* 16 17 18 19 20 21 22 23 */
278 /* 24 25 26 27 28 29 30 31 */
279 { INL, INL, INL, INL, INL, INL, INL, INL,
280 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
281 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
282 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
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_LSHIFTRT */
287 { INL, INL, INL, INL, INL, INL, INL, INL,
288 INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
289 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
290 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
306 /* Initialize various cpu specific globals at start up. */
309 h8300_init_once (void)
311 static const char *const h8_push_ops[2] = { "push" , "push.l" };
312 static const char *const h8_pop_ops[2] = { "pop" , "pop.l" };
313 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
317 cpu_type = (int) CPU_H8300;
318 h8_reg_names = names_big;
322 /* For this we treat the H8/300H and H8S the same. */
323 cpu_type = (int) CPU_H8300H;
324 h8_reg_names = names_extended;
326 h8_push_op = h8_push_ops[cpu_type];
327 h8_pop_op = h8_pop_ops[cpu_type];
328 h8_mov_op = h8_mov_ops[cpu_type];
330 if (!TARGET_H8300S && TARGET_MAC)
332 error ("-ms2600 is used without -ms");
333 target_flags |= MASK_H8300S_1;
336 if (TARGET_H8300 && TARGET_NORMAL_MODE)
338 error ("-mn is used without -mh or -ms");
339 target_flags ^= MASK_NORMAL_MODE;
342 /* Some of the shifts are optimized for speed by default.
343 See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
344 If optimizing for size, change shift_alg for those shift to
349 shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
350 shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
351 shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
352 shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
354 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
355 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
357 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
358 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
361 shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
362 shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
364 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
365 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
367 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
368 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
369 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
370 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
373 shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
376 /* Work out a value for MOVE_RATIO. */
379 /* Memory-memory moves are quite expensive without the
380 h8sx instructions. */
381 h8300_move_ratio = 3;
383 else if (flag_omit_frame_pointer)
385 /* movmd sequences are fairly cheap when er6 isn't fixed. They can
386 sometimes be as short as two individual memory-to-memory moves,
387 but since they use all the call-saved registers, it seems better
388 to allow up to three moves here. */
389 h8300_move_ratio = 4;
391 else if (optimize_size)
393 /* In this case we don't use movmd sequences since they tend
394 to be longer than calls to memcpy(). Memory-to-memory
395 moves are cheaper than for !TARGET_H8300SX, so it makes
396 sense to have a slightly higher threshold. */
397 h8300_move_ratio = 4;
401 /* We use movmd sequences for some moves since it can be quicker
402 than calling memcpy(). The sequences will need to save and
403 restore er6 though, so bump up the cost. */
404 h8300_move_ratio = 6;
408 /* Implement REG_CLASS_FROM_LETTER.
410 Some patterns need to use er6 as a scratch register. This is
411 difficult to arrange since er6 is the frame pointer and usually
414 Such patterns should define two alternatives, one which allows only
415 er6 and one which allows any general register. The former alternative
416 should have a 'd' constraint while the latter should be disparaged and
419 Normally, 'd' maps to DESTINATION_REGS and 'D' maps to GENERAL_REGS.
420 However, there are cases where they should be NO_REGS:
422 - 'd' should be NO_REGS when reloading a function that uses the
423 frame pointer. In this case, DESTINATION_REGS won't contain any
424 spillable registers, so the first alternative can't be used.
426 - -fno-omit-frame-pointer means that the frame pointer will
427 always be in use. It's therefore better to map 'd' to NO_REGS
428 before reload so that register allocator will pick the second
431 - we would like 'D' to be be NO_REGS when the frame pointer isn't
432 live, but we the frame pointer may turn out to be needed after
433 we start reload, and then we may have already decided we don't
434 have a choice, so we can't do that. Forcing the register
435 allocator to use er6 if possible might produce better code for
436 small functions: it's more efficient to save and restore er6 in
437 the prologue & epilogue than to do it in a define_split.
438 Hopefully disparaging 'D' will have a similar effect, without
439 forcing a reload failure if the frame pointer is found to be
443 h8300_reg_class_from_letter (int c)
454 if (!flag_omit_frame_pointer && !reload_completed)
456 if (frame_pointer_needed && reload_in_progress)
458 return DESTINATION_REGS;
461 /* The meaning of a constraint shouldn't change dynamically, so
462 we can't make this NO_REGS. */
473 /* Return the byte register name for a register rtx X. B should be 0
474 if you want a lower byte register. B should be 1 if you want an
475 upper byte register. */
478 byte_reg (rtx x, int b)
480 static const char *const names_small[] = {
481 "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
482 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
485 gcc_assert (REG_P (x));
487 return names_small[REGNO (x) * 2 + b];
490 /* REGNO must be saved/restored across calls if this macro is true. */
492 #define WORD_REG_USED(regno) \
494 /* No need to save registers if this function will not return. */ \
495 && ! TREE_THIS_VOLATILE (current_function_decl) \
496 && (h8300_saveall_function_p (current_function_decl) \
497 /* Save any call saved register that was used. */ \
498 || (df_regs_ever_live_p (regno) && !call_used_regs[regno]) \
499 /* Save the frame pointer if it was used. */ \
500 || (regno == HARD_FRAME_POINTER_REGNUM && df_regs_ever_live_p (regno)) \
501 /* Save any register used in an interrupt handler. */ \
502 || (h8300_current_function_interrupt_function_p () \
503 && df_regs_ever_live_p (regno)) \
504 /* Save call clobbered registers in non-leaf interrupt \
506 || (h8300_current_function_interrupt_function_p () \
507 && call_used_regs[regno] \
508 && !current_function_is_leaf)))
510 /* We use this to wrap all emitted insns in the prologue. */
512 F (rtx x, bool set_it)
515 RTX_FRAME_RELATED_P (x) = 1;
519 /* Mark all the subexpressions of the PARALLEL rtx PAR as
520 frame-related. Return PAR.
522 dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
523 PARALLEL rtx other than the first if they do not have the
524 FRAME_RELATED flag set on them. */
528 int len = XVECLEN (par, 0);
531 for (i = 0; i < len; i++)
532 F (XVECEXP (par, 0, i), true);
537 /* Output assembly language to FILE for the operation OP with operand size
538 SIZE to adjust the stack pointer. */
541 h8300_emit_stack_adjustment (int sign, HOST_WIDE_INT size, bool in_prologue)
543 /* If the frame size is 0, we don't have anything to do. */
547 /* H8/300 cannot add/subtract a large constant with a single
548 instruction. If a temporary register is available, load the
549 constant to it and then do the addition. */
552 && !h8300_current_function_interrupt_function_p ()
553 && !(cfun->static_chain_decl != NULL && sign < 0))
555 rtx r3 = gen_rtx_REG (Pmode, 3);
556 F (emit_insn (gen_movhi (r3, GEN_INT (sign * size))), in_prologue);
557 F (emit_insn (gen_addhi3 (stack_pointer_rtx,
558 stack_pointer_rtx, r3)), in_prologue);
562 /* The stack adjustment made here is further optimized by the
563 splitter. In case of H8/300, the splitter always splits the
564 addition emitted here to make the adjustment interrupt-safe.
565 FIXME: We don't always tag those, because we don't know what
566 the splitter will do. */
569 rtx x = emit_insn (gen_addhi3 (stack_pointer_rtx,
570 stack_pointer_rtx, GEN_INT (sign * size)));
575 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
576 stack_pointer_rtx, GEN_INT (sign * size))), in_prologue);
580 /* Round up frame size SIZE. */
583 round_frame_size (HOST_WIDE_INT size)
585 return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
586 & -STACK_BOUNDARY / BITS_PER_UNIT);
589 /* Compute which registers to push/pop.
590 Return a bit vector of registers. */
593 compute_saved_regs (void)
595 unsigned int saved_regs = 0;
598 /* Construct a bit vector of registers to be pushed/popped. */
599 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
601 if (WORD_REG_USED (regno))
602 saved_regs |= 1 << regno;
605 /* Don't push/pop the frame pointer as it is treated separately. */
606 if (frame_pointer_needed)
607 saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
612 /* Emit an insn to push register RN. */
617 rtx reg = gen_rtx_REG (word_mode, rn);
621 x = gen_push_h8300 (reg);
622 else if (!TARGET_NORMAL_MODE)
623 x = gen_push_h8300hs_advanced (reg);
625 x = gen_push_h8300hs_normal (reg);
626 x = F (emit_insn (x), true);
627 REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
630 /* Emit an insn to pop register RN. */
635 rtx reg = gen_rtx_REG (word_mode, rn);
639 x = gen_pop_h8300 (reg);
640 else if (!TARGET_NORMAL_MODE)
641 x = gen_pop_h8300hs_advanced (reg);
643 x = gen_pop_h8300hs_normal (reg);
645 REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
648 /* Emit an instruction to push or pop NREGS consecutive registers
649 starting at register REGNO. POP_P selects a pop rather than a
650 push and RETURN_P is true if the instruction should return.
652 It must be possible to do the requested operation in a single
653 instruction. If NREGS == 1 && !RETURN_P, use a normal push
654 or pop insn. Otherwise emit a parallel of the form:
657 [(return) ;; if RETURN_P
658 (save or restore REGNO)
659 (save or restore REGNO + 1)
661 (save or restore REGNO + NREGS - 1)
662 (set sp (plus sp (const_int adjust)))] */
665 h8300_push_pop (int regno, int nregs, bool pop_p, bool return_p)
671 /* See whether we can use a simple push or pop. */
672 if (!return_p && nregs == 1)
681 /* We need one element for the return insn, if present, one for each
682 register, and one for stack adjustment. */
683 vec = rtvec_alloc ((return_p ? 1 : 0) + nregs + 1);
684 sp = stack_pointer_rtx;
687 /* Add the return instruction. */
690 RTVEC_ELT (vec, i) = gen_rtx_RETURN (VOIDmode);
694 /* Add the register moves. */
695 for (j = 0; j < nregs; j++)
701 /* Register REGNO + NREGS - 1 is popped first. Before the
702 stack adjustment, its slot is at address @sp. */
703 lhs = gen_rtx_REG (SImode, regno + j);
704 rhs = gen_rtx_MEM (SImode, plus_constant (sp, (nregs - j - 1) * 4));
708 /* Register REGNO is pushed first and will be stored at @(-4,sp). */
709 lhs = gen_rtx_MEM (SImode, plus_constant (sp, (j + 1) * -4));
710 rhs = gen_rtx_REG (SImode, regno + j);
712 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, lhs, rhs);
715 /* Add the stack adjustment. */
716 offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
717 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, sp,
718 gen_rtx_PLUS (Pmode, sp, offset));
720 x = gen_rtx_PARALLEL (VOIDmode, vec);
730 /* Return true if X has the value sp + OFFSET. */
733 h8300_stack_offset_p (rtx x, int offset)
736 return x == stack_pointer_rtx;
738 return (GET_CODE (x) == PLUS
739 && XEXP (x, 0) == stack_pointer_rtx
740 && GET_CODE (XEXP (x, 1)) == CONST_INT
741 && INTVAL (XEXP (x, 1)) == offset);
744 /* A subroutine of h8300_ldm_stm_parallel. X is one pattern in
745 something that may be an ldm or stm instruction. If it fits
746 the required template, return the register it loads or stores,
749 LOAD_P is true if X should be a load, false if it should be a store.
750 NREGS is the number of registers that the whole instruction is expected
751 to load or store. INDEX is the index of the register that X should
752 load or store, relative to the lowest-numbered register. */
755 h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
757 int regindex, memindex, offset;
760 regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
762 memindex = 0, regindex = 1, offset = (index + 1) * -4;
764 if (GET_CODE (x) == SET
765 && GET_CODE (XEXP (x, regindex)) == REG
766 && GET_CODE (XEXP (x, memindex)) == MEM
767 && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
768 return REGNO (XEXP (x, regindex));
773 /* Return true if the elements of VEC starting at FIRST describe an
774 ldm or stm instruction (LOAD_P says which). */
777 h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
780 int nregs, i, regno, adjust;
782 /* There must be a stack adjustment, a register move, and at least one
783 other operation (a return or another register move). */
784 if (GET_NUM_ELEM (vec) < 3)
787 /* Get the range of registers to be pushed or popped. */
788 nregs = GET_NUM_ELEM (vec) - first - 1;
789 regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
791 /* Check that the call to h8300_ldm_stm_regno succeeded and
792 that we're only dealing with GPRs. */
793 if (regno < 0 || regno + nregs > 8)
796 /* 2-register h8s instructions must start with an even-numbered register.
797 3- and 4-register instructions must start with er0 or er4. */
800 if ((regno & 1) != 0)
802 if (nregs > 2 && (regno & 3) != 0)
806 /* Check the other loads or stores. */
807 for (i = 1; i < nregs; i++)
808 if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
812 /* Check the stack adjustment. */
813 last = RTVEC_ELT (vec, first + nregs);
814 adjust = (load_p ? nregs : -nregs) * 4;
815 return (GET_CODE (last) == SET
816 && SET_DEST (last) == stack_pointer_rtx
817 && h8300_stack_offset_p (SET_SRC (last), adjust));
820 /* This is what the stack looks like after the prolog of
821 a function with a frame has been set up:
827 <saved registers> <- sp
829 This is what the stack looks like after the prolog of
830 a function which doesn't have a frame:
835 <saved registers> <- sp
838 /* Generate RTL code for the function prologue. */
841 h8300_expand_prologue (void)
847 /* If the current function has the OS_Task attribute set, then
848 we have a naked prologue. */
849 if (h8300_os_task_function_p (current_function_decl))
852 if (h8300_monitor_function_p (current_function_decl))
853 /* My understanding of monitor functions is they act just like
854 interrupt functions, except the prologue must mask
856 emit_insn (gen_monitor_prologue ());
858 if (frame_pointer_needed)
861 push (HARD_FRAME_POINTER_REGNUM);
862 F (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx), true);
865 /* Push the rest of the registers in ascending order. */
866 saved_regs = compute_saved_regs ();
867 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
870 if (saved_regs & (1 << regno))
874 /* See how many registers we can push at the same time. */
875 if ((!TARGET_H8300SX || (regno & 3) == 0)
876 && ((saved_regs >> regno) & 0x0f) == 0x0f)
879 else if ((!TARGET_H8300SX || (regno & 3) == 0)
880 && ((saved_regs >> regno) & 0x07) == 0x07)
883 else if ((!TARGET_H8300SX || (regno & 1) == 0)
884 && ((saved_regs >> regno) & 0x03) == 0x03)
888 h8300_push_pop (regno, n_regs, false, false);
892 /* Leave room for locals. */
893 h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()), true);
896 /* Return nonzero if we can use "rts" for the function currently being
900 h8300_can_use_return_insn_p (void)
902 return (reload_completed
903 && !frame_pointer_needed
904 && get_frame_size () == 0
905 && compute_saved_regs () == 0);
908 /* Generate RTL code for the function epilogue. */
911 h8300_expand_epilogue (void)
916 HOST_WIDE_INT frame_size;
919 if (h8300_os_task_function_p (current_function_decl))
920 /* OS_Task epilogues are nearly naked -- they just have an
924 frame_size = round_frame_size (get_frame_size ());
927 /* Deallocate locals. */
928 h8300_emit_stack_adjustment (1, frame_size, false);
930 /* Pop the saved registers in descending order. */
931 saved_regs = compute_saved_regs ();
932 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
935 if (saved_regs & (1 << regno))
939 /* See how many registers we can pop at the same time. */
940 if ((TARGET_H8300SX || (regno & 3) == 3)
941 && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
944 else if ((TARGET_H8300SX || (regno & 3) == 2)
945 && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
948 else if ((TARGET_H8300SX || (regno & 1) == 1)
949 && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
953 /* See if this pop would be the last insn before the return.
954 If so, use rte/l or rts/l instead of pop or ldm.l. */
956 && !frame_pointer_needed
958 && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
961 h8300_push_pop (regno - n_regs + 1, n_regs, true, returned_p);
965 /* Pop frame pointer if we had one. */
966 if (frame_pointer_needed)
970 h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, true, returned_p);
974 emit_jump_insn (gen_rtx_RETURN (VOIDmode));
977 /* Return nonzero if the current function is an interrupt
981 h8300_current_function_interrupt_function_p (void)
983 return (h8300_interrupt_function_p (current_function_decl)
984 || h8300_monitor_function_p (current_function_decl));
987 /* Output assembly code for the start of the file. */
990 h8300_file_start (void)
992 default_file_start ();
995 fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
996 else if (TARGET_H8300SX)
997 fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
998 else if (TARGET_H8300S)
999 fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
1002 /* Output assembly language code for the end of file. */
1005 h8300_file_end (void)
1007 fputs ("\t.end\n", asm_out_file);
1010 /* Split an add of a small constant into two adds/subs insns.
1012 If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
1013 instead of adds/subs. */
1016 split_adds_subs (enum machine_mode mode, rtx *operands)
1018 HOST_WIDE_INT val = INTVAL (operands[1]);
1019 rtx reg = operands[0];
1020 HOST_WIDE_INT sign = 1;
1021 HOST_WIDE_INT amount;
1022 rtx (*gen_add) (rtx, rtx, rtx);
1024 /* Force VAL to be positive so that we do not have to consider the
1035 gen_add = gen_addhi3;
1039 gen_add = gen_addsi3;
1046 /* Try different amounts in descending order. */
1047 for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
1051 for (; val >= amount; val -= amount)
1052 emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
1058 /* Handle machine specific pragmas for compatibility with existing
1059 compilers for the H8/300.
1061 pragma saveall generates prologue/epilogue code which saves and
1062 restores all the registers on function entry.
1064 pragma interrupt saves and restores all registers, and exits with
1065 an rte instruction rather than an rts. A pointer to a function
1066 with this attribute may be safely used in an interrupt vector. */
1069 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1071 pragma_interrupt = 1;
1075 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1080 /* If the next function argument with MODE and TYPE is to be passed in
1081 a register, return a reg RTX for the hard register in which to pass
1082 the argument. CUM represents the state after the last argument.
1083 If the argument is to be pushed, NULL_RTX is returned. */
1086 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1087 tree type, int named)
1089 static const char *const hand_list[] = {
1108 rtx result = NULL_RTX;
1112 /* Never pass unnamed arguments in registers. */
1116 /* Pass 3 regs worth of data in regs when user asked on the command line. */
1117 if (TARGET_QUICKCALL)
1120 /* If calling hand written assembler, use 4 regs of args. */
1123 const char * const *p;
1125 fname = XSTR (cum->libcall, 0);
1127 /* See if this libcall is one of the hand coded ones. */
1128 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1139 if (mode == BLKmode)
1140 size = int_size_in_bytes (type);
1142 size = GET_MODE_SIZE (mode);
1144 if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1145 && cum->nbytes / UNITS_PER_WORD <= 3)
1146 result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1152 /* Compute the cost of an and insn. */
1155 h8300_and_costs (rtx x)
1159 if (GET_MODE (x) == QImode)
1162 if (GET_MODE (x) != HImode
1163 && GET_MODE (x) != SImode)
1167 operands[1] = XEXP (x, 0);
1168 operands[2] = XEXP (x, 1);
1170 return compute_logical_op_length (GET_MODE (x), operands) / 2;
1173 /* Compute the cost of a shift insn. */
1176 h8300_shift_costs (rtx x)
1180 if (GET_MODE (x) != QImode
1181 && GET_MODE (x) != HImode
1182 && GET_MODE (x) != SImode)
1187 operands[2] = XEXP (x, 1);
1189 return compute_a_shift_length (NULL, operands) / 2;
1192 /* Worker function for TARGET_RTX_COSTS. */
1195 h8300_rtx_costs (rtx x, int code, int outer_code, int *total, bool speed)
1197 if (TARGET_H8300SX && outer_code == MEM)
1199 /* Estimate the number of execution states needed to calculate
1201 if (register_operand (x, VOIDmode)
1202 || GET_CODE (x) == POST_INC
1203 || GET_CODE (x) == POST_DEC
1207 *total = COSTS_N_INSNS (1);
1215 HOST_WIDE_INT n = INTVAL (x);
1219 /* Constant operands need the same number of processor
1220 states as register operands. Although we could try to
1221 use a size-based cost for !speed, the lack of
1222 of a mode makes the results very unpredictable. */
1226 if (-4 <= n || n <= 4)
1237 *total = 0 + (outer_code == SET);
1241 if (TARGET_H8300H || TARGET_H8300S)
1242 *total = 0 + (outer_code == SET);
1257 /* See comment for CONST_INT. */
1269 if (XEXP (x, 1) == const0_rtx)
1274 if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1275 || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1277 *total = COSTS_N_INSNS (h8300_and_costs (x));
1280 /* We say that MOD and DIV are so expensive because otherwise we'll
1281 generate some really horrible code for division of a power of two. */
1287 switch (GET_MODE (x))
1291 *total = COSTS_N_INSNS (!speed ? 4 : 10);
1295 *total = COSTS_N_INSNS (!speed ? 4 : 18);
1301 *total = COSTS_N_INSNS (12);
1306 switch (GET_MODE (x))
1310 *total = COSTS_N_INSNS (2);
1314 *total = COSTS_N_INSNS (5);
1320 *total = COSTS_N_INSNS (4);
1326 if (h8sx_binary_shift_operator (x, VOIDmode))
1328 *total = COSTS_N_INSNS (2);
1331 else if (h8sx_unary_shift_operator (x, VOIDmode))
1333 *total = COSTS_N_INSNS (1);
1336 *total = COSTS_N_INSNS (h8300_shift_costs (x));
1341 if (GET_MODE (x) == HImode)
1348 *total = COSTS_N_INSNS (1);
1353 /* Documentation for the machine specific operand escapes:
1355 'E' like s but negative.
1356 'F' like t but negative.
1357 'G' constant just the negative
1358 'R' print operand as a byte:8 address if appropriate, else fall back to
1360 'S' print operand as a long word
1361 'T' print operand as a word
1362 'V' find the set bit, and print its number.
1363 'W' find the clear bit, and print its number.
1364 'X' print operand as a byte
1365 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1366 If this operand isn't a register, fall back to 'R' handling.
1368 'c' print the opcode corresponding to rtl
1369 'e' first word of 32-bit value - if reg, then least reg. if mem
1370 then least. if const then most sig word
1371 'f' second word of 32-bit value - if reg, then biggest reg. if mem
1372 then +2. if const then least sig word
1373 'j' print operand as condition code.
1374 'k' print operand as reverse condition code.
1375 'm' convert an integer operand to a size suffix (.b, .w or .l)
1376 'o' print an integer without a leading '#'
1377 's' print as low byte of 16-bit value
1378 't' print as high byte of 16-bit value
1379 'w' print as low byte of 32-bit value
1380 'x' print as 2nd byte of 32-bit value
1381 'y' print as 3rd byte of 32-bit value
1382 'z' print as msb of 32-bit value
1385 /* Return assembly language string which identifies a comparison type. */
1388 cond_string (enum rtx_code code)
1417 /* Print operand X using operand code CODE to assembly language output file
1421 print_operand (FILE *file, rtx x, int code)
1423 /* This is used for communication between codes V,W,Z and Y. */
1429 switch (GET_CODE (x))
1432 fprintf (file, "%sl", names_big[REGNO (x)]);
1435 fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1442 switch (GET_CODE (x))
1445 fprintf (file, "%sh", names_big[REGNO (x)]);
1448 fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1455 gcc_assert (GET_CODE (x) == CONST_INT);
1456 fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1459 if (GET_CODE (x) == REG)
1460 fprintf (file, "%s", names_extended[REGNO (x)]);
1465 if (GET_CODE (x) == REG)
1466 fprintf (file, "%s", names_big[REGNO (x)]);
1471 bitint = (INTVAL (x) & 0xffff);
1472 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1)
1473 bitint = exact_log2 (bitint & 0xff);
1475 bitint = exact_log2 ((bitint >> 8) & 0xff);
1476 gcc_assert (bitint >= 0);
1477 fprintf (file, "#%d", bitint);
1480 bitint = ((~INTVAL (x)) & 0xffff);
1481 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1 )
1482 bitint = exact_log2 (bitint & 0xff);
1484 bitint = (exact_log2 ((bitint >> 8) & 0xff));
1485 gcc_assert (bitint >= 0);
1486 fprintf (file, "#%d", bitint);
1490 if (GET_CODE (x) == REG)
1491 fprintf (file, "%s", byte_reg (x, 0));
1496 gcc_assert (bitint >= 0);
1497 if (GET_CODE (x) == REG)
1498 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1500 print_operand (file, x, 'R');
1504 bitint = INTVAL (x);
1505 fprintf (file, "#%d", bitint & 7);
1508 switch (GET_CODE (x))
1511 fprintf (file, "or");
1514 fprintf (file, "xor");
1517 fprintf (file, "and");
1524 switch (GET_CODE (x))
1528 fprintf (file, "%s", names_big[REGNO (x)]);
1530 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1533 print_operand (file, x, 0);
1536 fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1542 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1543 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1544 fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1553 switch (GET_CODE (x))
1557 fprintf (file, "%s", names_big[REGNO (x) + 1]);
1559 fprintf (file, "%s", names_big[REGNO (x)]);
1562 x = adjust_address (x, HImode, 2);
1563 print_operand (file, x, 0);
1566 fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1572 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1573 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1574 fprintf (file, "#%ld", (val & 0xffff));
1582 fputs (cond_string (GET_CODE (x)), file);
1585 fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1588 gcc_assert (GET_CODE (x) == CONST_INT);
1608 print_operand_address (file, x);
1611 if (GET_CODE (x) == CONST_INT)
1612 fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1614 fprintf (file, "%s", byte_reg (x, 0));
1617 if (GET_CODE (x) == CONST_INT)
1618 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1620 fprintf (file, "%s", byte_reg (x, 1));
1623 if (GET_CODE (x) == CONST_INT)
1624 fprintf (file, "#%ld", INTVAL (x) & 0xff);
1626 fprintf (file, "%s",
1627 byte_reg (x, TARGET_H8300 ? 2 : 0));
1630 if (GET_CODE (x) == CONST_INT)
1631 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1633 fprintf (file, "%s",
1634 byte_reg (x, TARGET_H8300 ? 3 : 1));
1637 if (GET_CODE (x) == CONST_INT)
1638 fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1640 fprintf (file, "%s", byte_reg (x, 0));
1643 if (GET_CODE (x) == CONST_INT)
1644 fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1646 fprintf (file, "%s", byte_reg (x, 1));
1651 switch (GET_CODE (x))
1654 switch (GET_MODE (x))
1657 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1658 fprintf (file, "%s", byte_reg (x, 0));
1659 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1660 fprintf (file, "%s", names_big[REGNO (x)]);
1664 fprintf (file, "%s", names_big[REGNO (x)]);
1668 fprintf (file, "%s", names_extended[REGNO (x)]);
1677 rtx addr = XEXP (x, 0);
1679 fprintf (file, "@");
1680 output_address (addr);
1682 /* Add a length suffix to constant addresses. Although this
1683 is often unnecessary, it helps to avoid ambiguity in the
1684 syntax of mova. If we wrote an insn like:
1686 mova/w.l @(1,@foo.b),er0
1688 then .b would be considered part of the symbol name.
1689 Adding a length after foo will avoid this. */
1690 if (CONSTANT_P (addr))
1694 /* Used for mov.b and bit operations. */
1695 if (h8300_eightbit_constant_address_p (addr))
1697 fprintf (file, ":8");
1701 /* Fall through. We should not get here if we are
1702 processing bit operations on H8/300 or H8/300H
1703 because 'U' constraint does not allow bit
1704 operations on the tiny area on these machines. */
1709 if (h8300_constant_length (addr) == 2)
1710 fprintf (file, ":16");
1712 fprintf (file, ":32");
1724 fprintf (file, "#");
1725 print_operand_address (file, x);
1731 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1732 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1733 fprintf (file, "#%ld", val);
1742 /* Output assembly language output for the address ADDR to FILE. */
1745 print_operand_address (FILE *file, rtx addr)
1750 switch (GET_CODE (addr))
1753 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1757 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1761 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1765 fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1769 fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1773 fprintf (file, "(");
1775 index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1776 if (GET_CODE (index) == REG)
1779 print_operand_address (file, XEXP (addr, 1));
1780 fprintf (file, ",");
1784 print_operand_address (file, index);
1788 print_operand (file, index, 'X');
1793 print_operand (file, index, 'T');
1798 print_operand (file, index, 'S');
1802 /* print_operand_address (file, XEXP (addr, 0)); */
1807 print_operand_address (file, XEXP (addr, 0));
1808 fprintf (file, "+");
1809 print_operand_address (file, XEXP (addr, 1));
1811 fprintf (file, ")");
1816 /* Since the H8/300 only has 16-bit pointers, negative values are also
1817 those >= 32768. This happens for example with pointer minus a
1818 constant. We don't want to turn (char *p - 2) into
1819 (char *p + 65534) because loop unrolling can build upon this
1820 (IE: char *p + 131068). */
1821 int n = INTVAL (addr);
1823 n = (int) (short) n;
1824 fprintf (file, "%d", n);
1829 output_addr_const (file, addr);
1834 /* Output all insn addresses and their sizes into the assembly language
1835 output file. This is helpful for debugging whether the length attributes
1836 in the md file are correct. This is not meant to be a user selectable
1840 final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
1841 int num_operands ATTRIBUTE_UNUSED)
1843 /* This holds the last insn address. */
1844 static int last_insn_address = 0;
1846 const int uid = INSN_UID (insn);
1848 if (TARGET_ADDRESSES)
1850 fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1851 INSN_ADDRESSES (uid) - last_insn_address);
1852 last_insn_address = INSN_ADDRESSES (uid);
1856 /* Prepare for an SI sized move. */
1859 h8300_expand_movsi (rtx operands[])
1861 rtx src = operands[1];
1862 rtx dst = operands[0];
1863 if (!reload_in_progress && !reload_completed)
1865 if (!register_operand (dst, GET_MODE (dst)))
1867 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1868 emit_move_insn (tmp, src);
1875 /* Given FROM and TO register numbers, say whether this elimination is allowed.
1876 Frame pointer elimination is automatically handled.
1878 For the h8300, if frame pointer elimination is being done, we would like to
1879 convert ap and rp into sp, not fp.
1881 All other eliminations are valid. */
1884 h8300_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1886 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
1889 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1890 Define the offset between two registers, one to be eliminated, and
1891 the other its replacement, at the start of a routine. */
1894 h8300_initial_elimination_offset (int from, int to)
1896 /* The number of bytes that the return address takes on the stack. */
1897 int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1899 /* The number of bytes that the saved frame pointer takes on the stack. */
1900 int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1902 /* The number of bytes that the saved registers, excluding the frame
1903 pointer, take on the stack. */
1904 int saved_regs_size = 0;
1906 /* The number of bytes that the locals takes on the stack. */
1907 int frame_size = round_frame_size (get_frame_size ());
1911 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1912 if (WORD_REG_USED (regno))
1913 saved_regs_size += UNITS_PER_WORD;
1915 /* Adjust saved_regs_size because the above loop took the frame
1916 pointer int account. */
1917 saved_regs_size -= fp_size;
1921 case HARD_FRAME_POINTER_REGNUM:
1924 case ARG_POINTER_REGNUM:
1925 return pc_size + fp_size;
1926 case RETURN_ADDRESS_POINTER_REGNUM:
1928 case FRAME_POINTER_REGNUM:
1929 return -saved_regs_size;
1934 case STACK_POINTER_REGNUM:
1937 case ARG_POINTER_REGNUM:
1938 return pc_size + saved_regs_size + frame_size;
1939 case RETURN_ADDRESS_POINTER_REGNUM:
1940 return saved_regs_size + frame_size;
1941 case FRAME_POINTER_REGNUM:
1953 /* Worker function for RETURN_ADDR_RTX. */
1956 h8300_return_addr_rtx (int count, rtx frame)
1961 ret = gen_rtx_MEM (Pmode,
1962 gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
1963 else if (flag_omit_frame_pointer)
1966 ret = gen_rtx_MEM (Pmode,
1967 memory_address (Pmode,
1968 plus_constant (frame, UNITS_PER_WORD)));
1969 set_mem_alias_set (ret, get_frame_alias_set ());
1973 /* Update the condition code from the insn. */
1976 notice_update_cc (rtx body, rtx insn)
1980 switch (get_attr_cc (insn))
1983 /* Insn does not affect CC at all. */
1987 /* Insn does not change CC, but the 0'th operand has been changed. */
1988 if (cc_status.value1 != 0
1989 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1990 cc_status.value1 = 0;
1991 if (cc_status.value2 != 0
1992 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
1993 cc_status.value2 = 0;
1997 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1998 The V flag is unusable. The C flag may or may not be known but
1999 that's ok because alter_cond will change tests to use EQ/NE. */
2001 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2002 set = single_set (insn);
2003 cc_status.value1 = SET_SRC (set);
2004 if (SET_DEST (set) != cc0_rtx)
2005 cc_status.value2 = SET_DEST (set);
2009 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2010 The C flag may or may not be known but that's ok because
2011 alter_cond will change tests to use EQ/NE. */
2013 cc_status.flags |= CC_NO_CARRY;
2014 set = single_set (insn);
2015 cc_status.value1 = SET_SRC (set);
2016 if (SET_DEST (set) != cc0_rtx)
2018 /* If the destination is STRICT_LOW_PART, strip off
2020 if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
2021 cc_status.value2 = XEXP (SET_DEST (set), 0);
2023 cc_status.value2 = SET_DEST (set);
2028 /* The insn is a compare instruction. */
2030 cc_status.value1 = SET_SRC (body);
2034 /* Insn doesn't leave CC in a usable state. */
2040 /* Given that X occurs in an address of the form (plus X constant),
2041 return the part of X that is expected to be a register. There are
2042 four kinds of addressing mode to recognize:
2049 If SIZE is nonnull, and the address is one of the last three forms,
2050 set *SIZE to the index multiplication factor. Set it to 0 for
2051 plain @(dd,Rn) addresses.
2053 MODE is the mode of the value being accessed. It can be VOIDmode
2054 if the address is known to be valid, but its mode is unknown. */
2057 h8300_get_index (rtx x, enum machine_mode mode, int *size)
2064 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
2067 && (mode == VOIDmode
2068 || GET_MODE_CLASS (mode) == MODE_INT
2069 || GET_MODE_CLASS (mode) == MODE_FLOAT))
2071 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
2073 /* When accessing byte-sized values, the index can be
2074 a zero-extended QImode or HImode register. */
2075 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
2080 /* We're looking for addresses of the form:
2083 or (mult (zero_extend X) I)
2085 where I is the size of the operand being accessed.
2086 The canonical form of the second expression is:
2088 (and (mult (subreg X) I) J)
2090 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2093 if (GET_CODE (x) == AND
2094 && GET_CODE (XEXP (x, 1)) == CONST_INT
2096 || INTVAL (XEXP (x, 1)) == 0xff * factor
2097 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2099 index = XEXP (x, 0);
2100 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2108 if (GET_CODE (index) == MULT
2109 && GET_CODE (XEXP (index, 1)) == CONST_INT
2110 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2111 return XEXP (index, 0);
2118 static const h8300_length_table addb_length_table =
2120 /* #xx Rs @aa @Rs @xx */
2121 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2122 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2123 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2124 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2127 static const h8300_length_table addw_length_table =
2129 /* #xx Rs @aa @Rs @xx */
2130 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2131 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2132 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2133 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2136 static const h8300_length_table addl_length_table =
2138 /* #xx Rs @aa @Rs @xx */
2139 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2140 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2141 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2142 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2145 #define logicb_length_table addb_length_table
2146 #define logicw_length_table addw_length_table
2148 static const h8300_length_table logicl_length_table =
2150 /* #xx Rs @aa @Rs @xx */
2151 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2152 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2153 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2154 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2157 static const h8300_length_table movb_length_table =
2159 /* #xx Rs @aa @Rs @xx */
2160 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2161 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2162 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2163 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2166 #define movw_length_table movb_length_table
2168 static const h8300_length_table movl_length_table =
2170 /* #xx Rs @aa @Rs @xx */
2171 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2172 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2173 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2174 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2177 /* Return the size of the given address or displacement constant. */
2180 h8300_constant_length (rtx constant)
2182 /* Check for (@d:16,Reg). */
2183 if (GET_CODE (constant) == CONST_INT
2184 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2187 /* Check for (@d:16,Reg) in cases where the displacement is
2188 an absolute address. */
2189 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2195 /* Return the size of a displacement field in address ADDR, which should
2196 have the form (plus X constant). SIZE is the number of bytes being
2200 h8300_displacement_length (rtx addr, int size)
2204 offset = XEXP (addr, 1);
2206 /* Check for @(d:2,Reg). */
2207 if (register_operand (XEXP (addr, 0), VOIDmode)
2208 && GET_CODE (offset) == CONST_INT
2209 && (INTVAL (offset) == size
2210 || INTVAL (offset) == size * 2
2211 || INTVAL (offset) == size * 3))
2214 return h8300_constant_length (offset);
2217 /* Store the class of operand OP in *OPCLASS and return the length of any
2218 extra operand fields. SIZE is the number of bytes in OP. OPCLASS
2219 can be null if only the length is needed. */
2222 h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
2224 enum h8300_operand_class dummy;
2229 if (CONSTANT_P (op))
2231 *opclass = H8OP_IMMEDIATE;
2233 /* Byte-sized immediates are stored in the opcode fields. */
2237 /* If this is a 32-bit instruction, see whether the constant
2238 will fit into a 16-bit immediate field. */
2241 && GET_CODE (op) == CONST_INT
2242 && IN_RANGE (INTVAL (op), 0, 0xffff))
2247 else if (GET_CODE (op) == MEM)
2250 if (CONSTANT_P (op))
2252 *opclass = H8OP_MEM_ABSOLUTE;
2253 return h8300_constant_length (op);
2255 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2257 *opclass = H8OP_MEM_COMPLEX;
2258 return h8300_displacement_length (op, size);
2260 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2262 *opclass = H8OP_MEM_COMPLEX;
2265 else if (register_operand (op, VOIDmode))
2267 *opclass = H8OP_MEM_BASE;
2271 gcc_assert (register_operand (op, VOIDmode));
2272 *opclass = H8OP_REGISTER;
2276 /* Return the length of the instruction described by TABLE given that
2277 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2278 and OP2 must be an h8300_src_operand. */
2281 h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2283 enum h8300_operand_class op1_class, op2_class;
2284 unsigned int size, immediate_length;
2286 size = GET_MODE_SIZE (GET_MODE (op1));
2287 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2288 + h8300_classify_operand (op2, size, &op2_class));
2289 return immediate_length + (*table)[op1_class - 1][op2_class];
2292 /* Return the length of a unary instruction such as neg or not given that
2293 its operand is OP. */
2296 h8300_unary_length (rtx op)
2298 enum h8300_operand_class opclass;
2299 unsigned int size, operand_length;
2301 size = GET_MODE_SIZE (GET_MODE (op));
2302 operand_length = h8300_classify_operand (op, size, &opclass);
2309 return (size == 4 ? 6 : 4);
2311 case H8OP_MEM_ABSOLUTE:
2312 return operand_length + (size == 4 ? 6 : 4);
2314 case H8OP_MEM_COMPLEX:
2315 return operand_length + 6;
2322 /* Likewise short immediate instructions such as add.w #xx:3,OP. */
2325 h8300_short_immediate_length (rtx op)
2327 enum h8300_operand_class opclass;
2328 unsigned int size, operand_length;
2330 size = GET_MODE_SIZE (GET_MODE (op));
2331 operand_length = h8300_classify_operand (op, size, &opclass);
2339 case H8OP_MEM_ABSOLUTE:
2340 case H8OP_MEM_COMPLEX:
2341 return 4 + operand_length;
2348 /* Likewise bitfield load and store instructions. */
2351 h8300_bitfield_length (rtx op, rtx op2)
2353 enum h8300_operand_class opclass;
2354 unsigned int size, operand_length;
2356 if (GET_CODE (op) == REG)
2358 gcc_assert (GET_CODE (op) != REG);
2360 size = GET_MODE_SIZE (GET_MODE (op));
2361 operand_length = h8300_classify_operand (op, size, &opclass);
2366 case H8OP_MEM_ABSOLUTE:
2367 case H8OP_MEM_COMPLEX:
2368 return 4 + operand_length;
2375 /* Calculate the length of general binary instruction INSN using TABLE. */
2378 h8300_binary_length (rtx insn, const h8300_length_table *table)
2382 set = single_set (insn);
2385 if (BINARY_P (SET_SRC (set)))
2386 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2387 XEXP (SET_SRC (set), 1), table);
2390 gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2391 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2392 XEXP (XEXP (SET_SRC (set), 1), 1),
2397 /* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2398 memory reference and either (1) it has the form @(d:16,Rn) or
2399 (2) its address has the code given by INC_CODE. */
2402 h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
2407 if (GET_CODE (op) != MEM)
2410 addr = XEXP (op, 0);
2411 size = GET_MODE_SIZE (GET_MODE (op));
2412 if (size != 1 && size != 2)
2415 return (GET_CODE (addr) == inc_code
2416 || (GET_CODE (addr) == PLUS
2417 && GET_CODE (XEXP (addr, 0)) == REG
2418 && h8300_displacement_length (addr, size) == 2));
2421 /* Calculate the length of move instruction INSN using the given length
2422 table. Although the tables are correct for most cases, there is some
2423 irregularity in the length of mov.b and mov.w. The following forms:
2430 are two bytes shorter than most other "mov Rs, @complex" or
2431 "mov @complex,Rd" combinations. */
2434 h8300_move_length (rtx *operands, const h8300_length_table *table)
2438 size = h8300_length_from_table (operands[0], operands[1], table);
2439 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2441 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2446 /* Return the length of a mova instruction with the given operands.
2447 DEST is the register destination, SRC is the source address and
2448 OFFSET is the 16-bit or 32-bit displacement. */
2451 h8300_mova_length (rtx dest, rtx src, rtx offset)
2456 + h8300_constant_length (offset)
2457 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2458 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2463 /* Compute the length of INSN based on its length_table attribute.
2464 OPERANDS is the array of its operands. */
2467 h8300_insn_length_from_table (rtx insn, rtx * operands)
2469 switch (get_attr_length_table (insn))
2471 case LENGTH_TABLE_NONE:
2474 case LENGTH_TABLE_ADDB:
2475 return h8300_binary_length (insn, &addb_length_table);
2477 case LENGTH_TABLE_ADDW:
2478 return h8300_binary_length (insn, &addw_length_table);
2480 case LENGTH_TABLE_ADDL:
2481 return h8300_binary_length (insn, &addl_length_table);
2483 case LENGTH_TABLE_LOGICB:
2484 return h8300_binary_length (insn, &logicb_length_table);
2486 case LENGTH_TABLE_MOVB:
2487 return h8300_move_length (operands, &movb_length_table);
2489 case LENGTH_TABLE_MOVW:
2490 return h8300_move_length (operands, &movw_length_table);
2492 case LENGTH_TABLE_MOVL:
2493 return h8300_move_length (operands, &movl_length_table);
2495 case LENGTH_TABLE_MOVA:
2496 return h8300_mova_length (operands[0], operands[1], operands[2]);
2498 case LENGTH_TABLE_MOVA_ZERO:
2499 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2501 case LENGTH_TABLE_UNARY:
2502 return h8300_unary_length (operands[0]);
2504 case LENGTH_TABLE_MOV_IMM4:
2505 return 2 + h8300_classify_operand (operands[0], 0, 0);
2507 case LENGTH_TABLE_SHORT_IMMEDIATE:
2508 return h8300_short_immediate_length (operands[0]);
2510 case LENGTH_TABLE_BITFIELD:
2511 return h8300_bitfield_length (operands[0], operands[1]);
2513 case LENGTH_TABLE_BITBRANCH:
2514 return h8300_bitfield_length (operands[1], operands[2]) - 2;
2521 /* Return true if LHS and RHS are memory references that can be mapped
2522 to the same h8sx assembly operand. LHS appears as the destination of
2523 an instruction and RHS appears as a source.
2525 Three cases are allowed:
2527 - RHS is @+Rn or @-Rn, LHS is @Rn
2528 - RHS is @Rn, LHS is @Rn+ or @Rn-
2529 - RHS and LHS have the same address and neither has side effects. */
2532 h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
2534 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2536 rhs = XEXP (rhs, 0);
2537 lhs = XEXP (lhs, 0);
2539 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2540 return rtx_equal_p (XEXP (rhs, 0), lhs);
2542 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2543 return rtx_equal_p (rhs, XEXP (lhs, 0));
2545 if (rtx_equal_p (rhs, lhs))
2551 /* Return true if OPERANDS[1] can be mapped to the same assembly
2552 operand as OPERANDS[0]. */
2555 h8300_operands_match_p (rtx *operands)
2557 if (register_operand (operands[0], VOIDmode)
2558 && register_operand (operands[1], VOIDmode))
2561 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2567 /* Try using movmd to move LENGTH bytes from memory region SRC to memory
2568 region DEST. The two regions do not overlap and have the common
2569 alignment given by ALIGNMENT. Return true on success.
2571 Using movmd for variable-length moves seems to involve some
2572 complex trade-offs. For instance:
2574 - Preparing for a movmd instruction is similar to preparing
2575 for a memcpy. The main difference is that the arguments
2576 are moved into er4, er5 and er6 rather than er0, er1 and er2.
2578 - Since movmd clobbers the frame pointer, we need to save
2579 and restore it somehow when frame_pointer_needed. This can
2580 sometimes make movmd sequences longer than calls to memcpy().
2582 - The counter register is 16 bits, so the instruction is only
2583 suitable for variable-length moves when sizeof (size_t) == 2.
2584 That's only true in normal mode.
2586 - We will often lack static alignment information. Falling back
2587 on movmd.b would likely be slower than calling memcpy(), at least
2590 This function therefore only uses movmd when the length is a
2591 known constant, and only then if -fomit-frame-pointer is in
2592 effect or if we're not optimizing for size.
2594 At the moment the function uses movmd for all in-range constants,
2595 but it might be better to fall back on memcpy() for large moves
2596 if ALIGNMENT == 1. */
2599 h8sx_emit_movmd (rtx dest, rtx src, rtx length,
2600 HOST_WIDE_INT alignment)
2602 if (!flag_omit_frame_pointer && optimize_size)
2605 if (GET_CODE (length) == CONST_INT)
2607 rtx dest_reg, src_reg, first_dest, first_src;
2611 /* Use movmd.l if the alignment allows it, otherwise fall back
2613 factor = (alignment >= 2 ? 4 : 1);
2615 /* Make sure the length is within range. We can handle counter
2616 values up to 65536, although HImode truncation will make
2617 the count appear negative in rtl dumps. */
2618 n = INTVAL (length);
2619 if (n <= 0 || n / factor > 65536)
2622 /* Create temporary registers for the source and destination
2623 pointers. Initialize them to the start of each region. */
2624 dest_reg = copy_addr_to_reg (XEXP (dest, 0));
2625 src_reg = copy_addr_to_reg (XEXP (src, 0));
2627 /* Create references to the movmd source and destination blocks. */
2628 first_dest = replace_equiv_address (dest, dest_reg);
2629 first_src = replace_equiv_address (src, src_reg);
2631 set_mem_size (first_dest, GEN_INT (n & -factor));
2632 set_mem_size (first_src, GEN_INT (n & -factor));
2634 length = copy_to_mode_reg (HImode, gen_int_mode (n / factor, HImode));
2635 emit_insn (gen_movmd (first_dest, first_src, length, GEN_INT (factor)));
2637 if ((n & -factor) != n)
2639 /* Move SRC and DEST past the region we just copied.
2640 This is done to update the memory attributes. */
2641 dest = adjust_address (dest, BLKmode, n & -factor);
2642 src = adjust_address (src, BLKmode, n & -factor);
2644 /* Replace the addresses with the source and destination
2645 registers, which movmd has left with the right values. */
2646 dest = replace_equiv_address (dest, dest_reg);
2647 src = replace_equiv_address (src, src_reg);
2649 /* Mop up the left-over bytes. */
2651 emit_move_insn (adjust_address (dest, HImode, 0),
2652 adjust_address (src, HImode, 0));
2654 emit_move_insn (adjust_address (dest, QImode, n & 2),
2655 adjust_address (src, QImode, n & 2));
2662 /* Move ADDR into er6 after pushing its old value onto the stack. */
2665 h8300_swap_into_er6 (rtx addr)
2667 push (HARD_FRAME_POINTER_REGNUM);
2668 emit_move_insn (hard_frame_pointer_rtx, addr);
2669 if (REGNO (addr) == SP_REG)
2670 emit_move_insn (hard_frame_pointer_rtx,
2671 plus_constant (hard_frame_pointer_rtx,
2672 GET_MODE_SIZE (word_mode)));
2675 /* Move the current value of er6 into ADDR and pop its old value
2679 h8300_swap_out_of_er6 (rtx addr)
2681 if (REGNO (addr) != SP_REG)
2682 emit_move_insn (addr, hard_frame_pointer_rtx);
2683 pop (HARD_FRAME_POINTER_REGNUM);
2686 /* Return the length of mov instruction. */
2689 compute_mov_length (rtx *operands)
2691 /* If the mov instruction involves a memory operand, we compute the
2692 length, assuming the largest addressing mode is used, and then
2693 adjust later in the function. Otherwise, we compute and return
2694 the exact length in one step. */
2695 enum machine_mode mode = GET_MODE (operands[0]);
2696 rtx dest = operands[0];
2697 rtx src = operands[1];
2700 if (GET_CODE (src) == MEM)
2701 addr = XEXP (src, 0);
2702 else if (GET_CODE (dest) == MEM)
2703 addr = XEXP (dest, 0);
2709 unsigned int base_length;
2714 if (addr == NULL_RTX)
2717 /* The eightbit addressing is available only in QImode, so
2718 go ahead and take care of it. */
2719 if (h8300_eightbit_constant_address_p (addr))
2726 if (addr == NULL_RTX)
2731 if (src == const0_rtx)
2741 if (addr == NULL_RTX)
2746 if (GET_CODE (src) == CONST_INT)
2748 if (src == const0_rtx)
2751 if ((INTVAL (src) & 0xffff) == 0)
2754 if ((INTVAL (src) & 0xffff) == 0)
2757 if ((INTVAL (src) & 0xffff)
2758 == ((INTVAL (src) >> 16) & 0xffff))
2768 if (addr == NULL_RTX)
2773 if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2786 /* Adjust the length based on the addressing mode used.
2787 Specifically, we subtract the difference between the actual
2788 length and the longest one, which is @(d:16,Rs). For SImode
2789 and SFmode, we double the adjustment because two mov.w are
2790 used to do the job. */
2792 /* @Rs+ and @-Rd are 2 bytes shorter than the longest. */
2793 if (GET_CODE (addr) == PRE_DEC
2794 || GET_CODE (addr) == POST_INC)
2796 if (mode == QImode || mode == HImode)
2797 return base_length - 2;
2799 /* In SImode and SFmode, we use two mov.w instructions, so
2800 double the adjustment. */
2801 return base_length - 4;
2804 /* @Rs and @Rd are 2 bytes shorter than the longest. Note that
2805 in SImode and SFmode, the second mov.w involves an address
2806 with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
2808 if (GET_CODE (addr) == REG)
2809 return base_length - 2;
2815 unsigned int base_length;
2820 if (addr == NULL_RTX)
2823 /* The eightbit addressing is available only in QImode, so
2824 go ahead and take care of it. */
2825 if (h8300_eightbit_constant_address_p (addr))
2832 if (addr == NULL_RTX)
2837 if (src == const0_rtx)
2847 if (addr == NULL_RTX)
2851 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2857 if (GET_CODE (src) == CONST_INT)
2859 int val = INTVAL (src);
2864 if (val == (val & 0x00ff) || val == (val & 0xff00))
2867 switch (val & 0xffffffff)
2888 if (addr == NULL_RTX)
2893 if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2906 /* Adjust the length based on the addressing mode used.
2907 Specifically, we subtract the difference between the actual
2908 length and the longest one, which is @(d:24,ERs). */
2910 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
2911 if (GET_CODE (addr) == PRE_DEC
2912 || GET_CODE (addr) == POST_INC)
2913 return base_length - 6;
2915 /* @ERs and @ERd are 6 bytes shorter than the longest. */
2916 if (GET_CODE (addr) == REG)
2917 return base_length - 6;
2919 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2921 if (GET_CODE (addr) == PLUS
2922 && GET_CODE (XEXP (addr, 0)) == REG
2923 && GET_CODE (XEXP (addr, 1)) == CONST_INT
2924 && INTVAL (XEXP (addr, 1)) > -32768
2925 && INTVAL (XEXP (addr, 1)) < 32767)
2926 return base_length - 4;
2928 /* @aa:16 is 4 bytes shorter than the longest. */
2929 if (h8300_tiny_constant_address_p (addr))
2930 return base_length - 4;
2932 /* @aa:24 is 2 bytes shorter than the longest. */
2933 if (CONSTANT_P (addr))
2934 return base_length - 2;
2940 /* Output an addition insn. */
2943 output_plussi (rtx *operands)
2945 enum machine_mode mode = GET_MODE (operands[0]);
2947 gcc_assert (mode == SImode);
2951 if (GET_CODE (operands[2]) == REG)
2952 return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2954 if (GET_CODE (operands[2]) == CONST_INT)
2956 HOST_WIDE_INT n = INTVAL (operands[2]);
2958 if ((n & 0xffffff) == 0)
2959 return "add\t%z2,%z0";
2960 if ((n & 0xffff) == 0)
2961 return "add\t%y2,%y0\n\taddx\t%z2,%z0";
2962 if ((n & 0xff) == 0)
2963 return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2966 return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2970 if (GET_CODE (operands[2]) == CONST_INT
2971 && register_operand (operands[1], VOIDmode))
2973 HOST_WIDE_INT intval = INTVAL (operands[2]);
2975 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2976 return "add.l\t%S2,%S0";
2977 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2978 return "sub.l\t%G2,%S0";
2980 /* See if we can finish with 2 bytes. */
2982 switch ((unsigned int) intval & 0xffffffff)
2987 return "adds\t%2,%S0";
2992 return "subs\t%G2,%S0";
2996 operands[2] = GEN_INT (intval >> 16);
2997 return "inc.w\t%2,%e0";
3001 operands[2] = GEN_INT (intval >> 16);
3002 return "dec.w\t%G2,%e0";
3005 /* See if we can finish with 4 bytes. */
3006 if ((intval & 0xffff) == 0)
3008 operands[2] = GEN_INT (intval >> 16);
3009 return "add.w\t%2,%e0";
3013 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3015 operands[2] = GEN_INT (-INTVAL (operands[2]));
3016 return "sub.l\t%S2,%S0";
3018 return "add.l\t%S2,%S0";
3022 /* ??? It would be much easier to add the h8sx stuff if a single function
3023 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
3024 /* Compute the length of an addition insn. */
3027 compute_plussi_length (rtx *operands)
3029 enum machine_mode mode = GET_MODE (operands[0]);
3031 gcc_assert (mode == SImode);
3035 if (GET_CODE (operands[2]) == REG)
3038 if (GET_CODE (operands[2]) == CONST_INT)
3040 HOST_WIDE_INT n = INTVAL (operands[2]);
3042 if ((n & 0xffffff) == 0)
3044 if ((n & 0xffff) == 0)
3046 if ((n & 0xff) == 0)
3054 if (GET_CODE (operands[2]) == CONST_INT
3055 && register_operand (operands[1], VOIDmode))
3057 HOST_WIDE_INT intval = INTVAL (operands[2]);
3059 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3061 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3064 /* See if we can finish with 2 bytes. */
3066 switch ((unsigned int) intval & 0xffffffff)
3087 /* See if we can finish with 4 bytes. */
3088 if ((intval & 0xffff) == 0)
3092 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3093 return h8300_length_from_table (operands[0],
3094 GEN_INT (-INTVAL (operands[2])),
3095 &addl_length_table);
3097 return h8300_length_from_table (operands[0], operands[2],
3098 &addl_length_table);
3103 /* Compute which flag bits are valid after an addition insn. */
3106 compute_plussi_cc (rtx *operands)
3108 enum machine_mode mode = GET_MODE (operands[0]);
3110 gcc_assert (mode == SImode);
3118 if (GET_CODE (operands[2]) == CONST_INT
3119 && register_operand (operands[1], VOIDmode))
3121 HOST_WIDE_INT intval = INTVAL (operands[2]);
3123 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3125 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3128 /* See if we can finish with 2 bytes. */
3130 switch ((unsigned int) intval & 0xffffffff)
3135 return CC_NONE_0HIT;
3140 return CC_NONE_0HIT;
3151 /* See if we can finish with 4 bytes. */
3152 if ((intval & 0xffff) == 0)
3160 /* Output a logical insn. */
3163 output_logical_op (enum machine_mode mode, rtx *operands)
3165 /* Figure out the logical op that we need to perform. */
3166 enum rtx_code code = GET_CODE (operands[3]);
3167 /* Pretend that every byte is affected if both operands are registers. */
3168 const unsigned HOST_WIDE_INT intval =
3169 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3170 /* Always use the full instruction if the
3171 first operand is in memory. It is better
3172 to use define_splits to generate the shorter
3173 sequence where valid. */
3174 && register_operand (operands[1], VOIDmode)
3175 ? INTVAL (operands[2]) : 0x55555555);
3176 /* The determinant of the algorithm. If we perform an AND, 0
3177 affects a bit. Otherwise, 1 affects a bit. */
3178 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3179 /* Break up DET into pieces. */
3180 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3181 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3182 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3183 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3184 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3185 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3186 int lower_half_easy_p = 0;
3187 int upper_half_easy_p = 0;
3188 /* The name of an insn. */
3210 /* First, see if we can finish with one insn. */
3211 if ((TARGET_H8300H || TARGET_H8300S)
3215 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
3216 output_asm_insn (insn_buf, operands);
3220 /* Take care of the lower byte. */
3223 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
3224 output_asm_insn (insn_buf, operands);
3226 /* Take care of the upper byte. */
3229 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
3230 output_asm_insn (insn_buf, operands);
3235 if (TARGET_H8300H || TARGET_H8300S)
3237 /* Determine if the lower half can be taken care of in no more
3239 lower_half_easy_p = (b0 == 0
3241 || (code != IOR && w0 == 0xffff));
3243 /* Determine if the upper half can be taken care of in no more
3245 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3246 || (code == AND && w1 == 0xff00));
3249 /* Check if doing everything with one insn is no worse than
3250 using multiple insns. */
3251 if ((TARGET_H8300H || TARGET_H8300S)
3252 && w0 != 0 && w1 != 0
3253 && !(lower_half_easy_p && upper_half_easy_p)
3254 && !(code == IOR && w1 == 0xffff
3255 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3257 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
3258 output_asm_insn (insn_buf, operands);
3262 /* Take care of the lower and upper words individually. For
3263 each word, we try different methods in the order of
3265 1) the special insn (in case of AND or XOR),
3266 2) the word-wise insn, and
3267 3) The byte-wise insn. */
3269 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3270 output_asm_insn ((code == AND)
3271 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
3273 else if ((TARGET_H8300H || TARGET_H8300S)
3277 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
3278 output_asm_insn (insn_buf, operands);
3284 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
3285 output_asm_insn (insn_buf, operands);
3289 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3290 output_asm_insn (insn_buf, operands);
3295 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3296 output_asm_insn ((code == AND)
3297 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
3299 else if ((TARGET_H8300H || TARGET_H8300S)
3302 && (w0 & 0x8000) != 0)
3304 output_asm_insn ("exts.l\t%S0", operands);
3306 else if ((TARGET_H8300H || TARGET_H8300S)
3310 output_asm_insn ("extu.w\t%e0", operands);
3312 else if (TARGET_H8300H || TARGET_H8300S)
3316 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3317 output_asm_insn (insn_buf, operands);
3324 sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
3325 output_asm_insn (insn_buf, operands);
3329 sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
3330 output_asm_insn (insn_buf, operands);
3341 /* Compute the length of a logical insn. */
3344 compute_logical_op_length (enum machine_mode mode, rtx *operands)
3346 /* Figure out the logical op that we need to perform. */
3347 enum rtx_code code = GET_CODE (operands[3]);
3348 /* Pretend that every byte is affected if both operands are registers. */
3349 const unsigned HOST_WIDE_INT intval =
3350 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3351 /* Always use the full instruction if the
3352 first operand is in memory. It is better
3353 to use define_splits to generate the shorter
3354 sequence where valid. */
3355 && register_operand (operands[1], VOIDmode)
3356 ? INTVAL (operands[2]) : 0x55555555);
3357 /* The determinant of the algorithm. If we perform an AND, 0
3358 affects a bit. Otherwise, 1 affects a bit. */
3359 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3360 /* Break up DET into pieces. */
3361 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3362 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3363 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3364 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3365 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3366 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3367 int lower_half_easy_p = 0;
3368 int upper_half_easy_p = 0;
3370 unsigned int length = 0;
3375 /* First, see if we can finish with one insn. */
3376 if ((TARGET_H8300H || TARGET_H8300S)
3380 length = h8300_length_from_table (operands[1], operands[2],
3381 &logicw_length_table);
3385 /* Take care of the lower byte. */
3389 /* Take care of the upper byte. */
3395 if (TARGET_H8300H || TARGET_H8300S)
3397 /* Determine if the lower half can be taken care of in no more
3399 lower_half_easy_p = (b0 == 0
3401 || (code != IOR && w0 == 0xffff));
3403 /* Determine if the upper half can be taken care of in no more
3405 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3406 || (code == AND && w1 == 0xff00));
3409 /* Check if doing everything with one insn is no worse than
3410 using multiple insns. */
3411 if ((TARGET_H8300H || TARGET_H8300S)
3412 && w0 != 0 && w1 != 0
3413 && !(lower_half_easy_p && upper_half_easy_p)
3414 && !(code == IOR && w1 == 0xffff
3415 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3417 length = h8300_length_from_table (operands[1], operands[2],
3418 &logicl_length_table);
3422 /* Take care of the lower and upper words individually. For
3423 each word, we try different methods in the order of
3425 1) the special insn (in case of AND or XOR),
3426 2) the word-wise insn, and
3427 3) The byte-wise insn. */
3429 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3433 else if ((TARGET_H8300H || TARGET_H8300S)