* tree-ssa-phiopt.c, config/arm/arm.c, config/fr30/fr30.md,
[official-gcc.git] / gcc / config / h8300 / h8300.c
blob81e104faf91895329ac12df9ca2c4f4768ff4a39
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 Free Software Foundation, Inc.
4 Contributed by Steve Chamberlain (sac@cygnus.com),
5 Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "conditions.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "recog.h"
39 #include "expr.h"
40 #include "function.h"
41 #include "optabs.h"
42 #include "toplev.h"
43 #include "c-pragma.h"
44 #include "tm_p.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
49 /* Classifies a h8300_src_operand or h8300_dst_operand.
51 H8OP_IMMEDIATE
52 A constant operand of some sort.
54 H8OP_REGISTER
55 An ordinary register.
57 H8OP_MEM_ABSOLUTE
58 A memory reference with a constant address.
60 H8OP_MEM_BASE
61 A memory reference with a register as its address.
63 H8OP_MEM_COMPLEX
64 Some other kind of memory reference. */
65 enum h8300_operand_class
67 H8OP_IMMEDIATE,
68 H8OP_REGISTER,
69 H8OP_MEM_ABSOLUTE,
70 H8OP_MEM_BASE,
71 H8OP_MEM_COMPLEX,
72 NUM_H8OPS
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, unsigned int);
87 static int round_frame_size (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);
98 #endif
99 static int h8300_and_costs (rtx);
100 static int h8300_shift_costs (rtx);
101 static void h8300_push_pop (int, int, int, int);
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 *);
116 /* CPU_TYPE, says what cpu we're compiling for. */
117 int cpu_type;
119 /* True if a #pragma interrupt has been seen for the current function. */
120 static int pragma_interrupt;
122 /* True if a #pragma saveall has been seen for the current function. */
123 static int pragma_saveall;
125 static const char *const names_big[] =
126 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
128 static const char *const names_extended[] =
129 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
131 static const char *const names_upper_extended[] =
132 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
134 /* Points to one of the above. */
135 /* ??? The above could be put in an array indexed by CPU_TYPE. */
136 const char * const *h8_reg_names;
138 /* Various operations needed by the following, indexed by CPU_TYPE. */
140 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
142 /* Value of MOVE_RATIO. */
143 int h8300_move_ratio;
145 /* See below where shifts are handled for explanation of this enum. */
147 enum shift_alg
149 SHIFT_INLINE,
150 SHIFT_ROT_AND,
151 SHIFT_SPECIAL,
152 SHIFT_LOOP
155 /* Symbols of the various shifts which can be used as indices. */
157 enum shift_type
159 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
162 /* Macros to keep the shift algorithm tables small. */
163 #define INL SHIFT_INLINE
164 #define ROT SHIFT_ROT_AND
165 #define LOP SHIFT_LOOP
166 #define SPC SHIFT_SPECIAL
168 /* The shift algorithms for each machine, mode, shift type, and shift
169 count are defined below. The three tables below correspond to
170 QImode, HImode, and SImode, respectively. Each table is organized
171 by, in the order of indices, machine, shift type, and shift count. */
173 static enum shift_alg shift_alg_qi[3][3][8] = {
175 /* TARGET_H8300 */
176 /* 0 1 2 3 4 5 6 7 */
177 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
178 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
179 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
182 /* TARGET_H8300H */
183 /* 0 1 2 3 4 5 6 7 */
184 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
185 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
186 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
189 /* TARGET_H8300S */
190 /* 0 1 2 3 4 5 6 7 */
191 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT */
192 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
193 { INL, INL, INL, INL, INL, INL, INL, SPC } /* SHIFT_ASHIFTRT */
197 static enum shift_alg shift_alg_hi[3][3][16] = {
199 /* TARGET_H8300 */
200 /* 0 1 2 3 4 5 6 7 */
201 /* 8 9 10 11 12 13 14 15 */
202 { INL, INL, INL, INL, INL, INL, INL, SPC,
203 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
204 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
205 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
206 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
207 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
210 /* TARGET_H8300H */
211 /* 0 1 2 3 4 5 6 7 */
212 /* 8 9 10 11 12 13 14 15 */
213 { INL, INL, INL, INL, INL, INL, INL, SPC,
214 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
215 { INL, INL, INL, INL, INL, INL, INL, SPC,
216 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
217 { INL, INL, INL, INL, INL, INL, INL, SPC,
218 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
221 /* TARGET_H8300S */
222 /* 0 1 2 3 4 5 6 7 */
223 /* 8 9 10 11 12 13 14 15 */
224 { INL, INL, INL, INL, INL, INL, INL, INL,
225 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
226 { INL, INL, INL, INL, INL, INL, INL, INL,
227 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
228 { INL, INL, INL, INL, INL, INL, INL, INL,
229 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
233 static enum shift_alg shift_alg_si[3][3][32] = {
235 /* TARGET_H8300 */
236 /* 0 1 2 3 4 5 6 7 */
237 /* 8 9 10 11 12 13 14 15 */
238 /* 16 17 18 19 20 21 22 23 */
239 /* 24 25 26 27 28 29 30 31 */
240 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
241 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
242 SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
243 SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT */
244 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
245 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
246 SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
247 SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
248 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
249 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
250 SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
251 SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
254 /* TARGET_H8300H */
255 /* 0 1 2 3 4 5 6 7 */
256 /* 8 9 10 11 12 13 14 15 */
257 /* 16 17 18 19 20 21 22 23 */
258 /* 24 25 26 27 28 29 30 31 */
259 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
260 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
261 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
262 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
263 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
264 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
265 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
266 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
267 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
268 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
269 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
270 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
273 /* TARGET_H8300S */
274 /* 0 1 2 3 4 5 6 7 */
275 /* 8 9 10 11 12 13 14 15 */
276 /* 16 17 18 19 20 21 22 23 */
277 /* 24 25 26 27 28 29 30 31 */
278 { INL, INL, INL, INL, INL, INL, INL, INL,
279 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
280 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
281 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
282 { INL, INL, INL, INL, INL, INL, INL, INL,
283 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
284 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
285 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
286 { INL, INL, INL, INL, INL, INL, INL, INL,
287 INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
288 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
289 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
293 #undef INL
294 #undef ROT
295 #undef LOP
296 #undef SPC
298 enum h8_cpu
300 H8_300,
301 H8_300H,
302 H8_S
305 /* Initialize various cpu specific globals at start up. */
307 void
308 h8300_init_once (void)
310 static const char *const h8_push_ops[2] = { "push" , "push.l" };
311 static const char *const h8_pop_ops[2] = { "pop" , "pop.l" };
312 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
314 if (TARGET_H8300)
316 cpu_type = (int) CPU_H8300;
317 h8_reg_names = names_big;
319 else
321 /* For this we treat the H8/300H and H8S the same. */
322 cpu_type = (int) CPU_H8300H;
323 h8_reg_names = names_extended;
325 h8_push_op = h8_push_ops[cpu_type];
326 h8_pop_op = h8_pop_ops[cpu_type];
327 h8_mov_op = h8_mov_ops[cpu_type];
329 if (!TARGET_H8300S && TARGET_MAC)
331 error ("-ms2600 is used without -ms");
332 target_flags |= MASK_H8300S_1;
335 if (TARGET_H8300 && TARGET_NORMAL_MODE)
337 error ("-mn is used without -mh or -ms");
338 target_flags ^= MASK_NORMAL_MODE;
341 /* Some of the shifts are optimized for speed by default.
342 See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
343 If optimizing for size, change shift_alg for those shift to
344 SHIFT_LOOP. */
345 if (optimize_size)
347 /* H8/300 */
348 shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
349 shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
350 shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
351 shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
353 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
354 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
356 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
357 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
359 /* H8/300H */
360 shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
361 shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
363 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
364 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
366 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
367 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
368 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
369 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
371 /* H8S */
372 shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
375 /* Work out a value for MOVE_RATIO. */
376 if (!TARGET_H8300SX)
378 /* Memory-memory moves are quite expensive without the
379 h8sx instructions. */
380 h8300_move_ratio = 3;
382 else if (flag_omit_frame_pointer)
384 /* movmd sequences are fairly cheap when er6 isn't fixed. They can
385 sometimes be as short as two individual memory-to-memory moves,
386 but since they use all the call-saved registers, it seems better
387 to allow up to three moves here. */
388 h8300_move_ratio = 4;
390 else if (optimize_size)
392 /* In this case we don't use movmd sequences since they tend
393 to be longer than calls to memcpy(). Memory-to-memory
394 moves are cheaper than for !TARGET_H8300SX, so it makes
395 sense to have a slightly higher threshold. */
396 h8300_move_ratio = 4;
398 else
400 /* We use movmd sequences for some moves since it can be quicker
401 than calling memcpy(). The sequences will need to save and
402 restore er6 though, so bump up the cost. */
403 h8300_move_ratio = 6;
407 /* Implement REG_CLASS_FROM_LETTER.
409 Some patterns need to use er6 as a scratch register. This is
410 difficult to arrange since er6 is the frame pointer and usually
411 can't be spilled.
413 Such patterns should define two alternatives, one which allows only
414 er6 and one which allows any general register. The former alternative
415 should have a 'd' constraint while the latter should be disparaged and
416 use 'D'.
418 Normally, 'd' maps to DESTINATION_REGS and 'D' maps to GENERAL_REGS.
419 However, there are cases where they should be NO_REGS:
421 - 'd' should be NO_REGS when reloading a function that uses the
422 frame pointer. In this case, DESTINATION_REGS won't contain any
423 spillable registers, so the first alternative can't be used.
425 - -fno-omit-frame-pointer means that the frame pointer will
426 always be in use. It's therefore better to map 'd' to NO_REGS
427 before reload so that register allocator will pick the second
428 alternative.
430 - we would like 'D' to be be NO_REGS when the frame pointer isn't
431 live, but we the frame pointer may turn out to be needed after
432 we start reload, and then we may have already decided we don't
433 have a choice, so we can't do that. Forcing the register
434 allocator to use er6 if possible might produce better code for
435 small functions: it's more efficient to save and restore er6 in
436 the prologue & epilogue than to do it in a define_split.
437 Hopefully disparaging 'D' will have a similar effect, without
438 forcing a reload failure if the frame pointer is found to be
439 needed too late. */
441 enum reg_class
442 h8300_reg_class_from_letter (int c)
444 switch (c)
446 case 'a':
447 return MAC_REGS;
449 case 'c':
450 return COUNTER_REGS;
452 case 'd':
453 if (!flag_omit_frame_pointer && !reload_completed)
454 return NO_REGS;
455 if (frame_pointer_needed && reload_in_progress)
456 return NO_REGS;
457 return DESTINATION_REGS;
459 case 'D':
460 /* The meaning of a constraint shouldn't change dynamically, so
461 we can't make this NO_REGS. */
462 return GENERAL_REGS;
464 case 'f':
465 return SOURCE_REGS;
467 default:
468 return NO_REGS;
472 /* Return the byte register name for a register rtx X. B should be 0
473 if you want a lower byte register. B should be 1 if you want an
474 upper byte register. */
476 static const char *
477 byte_reg (rtx x, int b)
479 static const char *const names_small[] = {
480 "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
481 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
484 if (!REG_P (x))
485 abort ();
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) \
493 (regno < SP_REG \
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 || (regs_ever_live[regno] && !call_used_regs[regno]) \
499 /* Save the frame pointer if it was used. */ \
500 || (regno == HARD_FRAME_POINTER_REGNUM && regs_ever_live[regno]) \
501 /* Save any register used in an interrupt handler. */ \
502 || (h8300_current_function_interrupt_function_p () \
503 && regs_ever_live[regno]) \
504 /* Save call clobbered registers in non-leaf interrupt \
505 handlers. */ \
506 || (h8300_current_function_interrupt_function_p () \
507 && call_used_regs[regno] \
508 && !current_function_is_leaf)))
510 /* Output assembly language to FILE for the operation OP with operand size
511 SIZE to adjust the stack pointer. */
513 static void
514 h8300_emit_stack_adjustment (int sign, unsigned int size)
516 /* If the frame size is 0, we don't have anything to do. */
517 if (size == 0)
518 return;
520 /* H8/300 cannot add/subtract a large constant with a single
521 instruction. If a temporary register is available, load the
522 constant to it and then do the addition. */
523 if (TARGET_H8300
524 && size > 4
525 && !h8300_current_function_interrupt_function_p ()
526 && !(cfun->static_chain_decl != NULL && sign < 0))
528 rtx r3 = gen_rtx_REG (Pmode, 3);
529 emit_insn (gen_movhi (r3, GEN_INT (sign * size)));
530 emit_insn (gen_addhi3 (stack_pointer_rtx,
531 stack_pointer_rtx, r3));
533 else
535 /* The stack adjustment made here is further optimized by the
536 splitter. In case of H8/300, the splitter always splits the
537 addition emitted here to make the adjustment
538 interrupt-safe. */
539 if (Pmode == HImode)
540 emit_insn (gen_addhi3 (stack_pointer_rtx,
541 stack_pointer_rtx, GEN_INT (sign * size)));
542 else
543 emit_insn (gen_addsi3 (stack_pointer_rtx,
544 stack_pointer_rtx, GEN_INT (sign * size)));
548 /* Round up frame size SIZE. */
550 static int
551 round_frame_size (int size)
553 return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
554 & -STACK_BOUNDARY / BITS_PER_UNIT);
557 /* Compute which registers to push/pop.
558 Return a bit vector of registers. */
560 static unsigned int
561 compute_saved_regs (void)
563 unsigned int saved_regs = 0;
564 int regno;
566 /* Construct a bit vector of registers to be pushed/popped. */
567 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
569 if (WORD_REG_USED (regno))
570 saved_regs |= 1 << regno;
573 /* Don't push/pop the frame pointer as it is treated separately. */
574 if (frame_pointer_needed)
575 saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
577 return saved_regs;
580 /* Emit an insn to push register RN. */
582 static void
583 push (int rn)
585 rtx reg = gen_rtx_REG (word_mode, rn);
586 rtx x;
588 if (TARGET_H8300)
589 x = gen_push_h8300 (reg);
590 else if (!TARGET_NORMAL_MODE)
591 x = gen_push_h8300hs_advanced (reg);
592 else
593 x = gen_push_h8300hs_normal (reg);
594 x = emit_insn (x);
595 REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
598 /* Emit an insn to pop register RN. */
600 static void
601 pop (int rn)
603 rtx reg = gen_rtx_REG (word_mode, rn);
604 rtx x;
606 if (TARGET_H8300)
607 x = gen_pop_h8300 (reg);
608 else if (!TARGET_NORMAL_MODE)
609 x = gen_pop_h8300hs_advanced (reg);
610 else
611 x = gen_pop_h8300hs_normal (reg);
612 x = emit_insn (x);
613 REG_NOTES (x) = gen_rtx_EXPR_LIST (REG_INC, stack_pointer_rtx, 0);
616 /* Emit an instruction to push or pop NREGS consecutive registers
617 starting at register REGNO. POP_P selects a pop rather than a
618 push and RETURN_P is true if the instruction should return.
620 It must be possible to do the requested operation in a single
621 instruction. If NREGS == 1 && !RETURN_P, use a normal push
622 or pop insn. Otherwise emit a parallel of the form:
624 (parallel
625 [(return) ;; if RETURN_P
626 (save or restore REGNO)
627 (save or restore REGNO + 1)
629 (save or restore REGNO + NREGS - 1)
630 (set sp (plus sp (const_int adjust)))] */
632 static void
633 h8300_push_pop (int regno, int nregs, int pop_p, int return_p)
635 int i, j;
636 rtvec vec;
637 rtx sp, offset;
639 /* See whether we can use a simple push or pop. */
640 if (!return_p && nregs == 1)
642 if (pop_p)
643 pop (regno);
644 else
645 push (regno);
646 return;
649 /* We need one element for the return insn, if present, one for each
650 register, and one for stack adjustment. */
651 vec = rtvec_alloc ((return_p != 0) + nregs + 1);
652 sp = stack_pointer_rtx;
653 i = 0;
655 /* Add the return instruction. */
656 if (return_p)
658 RTVEC_ELT (vec, i) = gen_rtx_RETURN (VOIDmode);
659 i++;
662 /* Add the register moves. */
663 for (j = 0; j < nregs; j++)
665 rtx lhs, rhs;
667 if (pop_p)
669 /* Register REGNO + NREGS - 1 is popped first. Before the
670 stack adjustment, its slot is at address @sp. */
671 lhs = gen_rtx_REG (SImode, regno + j);
672 rhs = gen_rtx_MEM (SImode, plus_constant (sp, (nregs - j - 1) * 4));
674 else
676 /* Register REGNO is pushed first and will be stored at @(-4,sp). */
677 lhs = gen_rtx_MEM (SImode, plus_constant (sp, (j + 1) * -4));
678 rhs = gen_rtx_REG (SImode, regno + j);
680 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, lhs, rhs);
683 /* Add the stack adjustment. */
684 offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
685 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, sp,
686 gen_rtx_PLUS (Pmode, sp, offset));
688 emit_insn (gen_rtx_PARALLEL (VOIDmode, vec));
691 /* Return true if X has the value sp + OFFSET. */
693 static int
694 h8300_stack_offset_p (rtx x, int offset)
696 if (offset == 0)
697 return x == stack_pointer_rtx;
699 return (GET_CODE (x) == PLUS
700 && XEXP (x, 0) == stack_pointer_rtx
701 && GET_CODE (XEXP (x, 1)) == CONST_INT
702 && INTVAL (XEXP (x, 1)) == offset);
705 /* A subroutine of h8300_ldm_stm_parallel. X is one pattern in
706 something that may be an ldm or stm instruction. If it fits
707 the required template, return the register it loads or stores,
708 otherwise return -1.
710 LOAD_P is true if X should be a load, false if it should be a store.
711 NREGS is the number of registers that the whole instruction is expected
712 to load or store. INDEX is the index of the register that X should
713 load or store, relative to the lowest-numbered register. */
715 static int
716 h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
718 int regindex, memindex, offset;
720 if (load_p)
721 regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
722 else
723 memindex = 0, regindex = 1, offset = (index + 1) * -4;
725 if (GET_CODE (x) == SET
726 && GET_CODE (XEXP (x, regindex)) == REG
727 && GET_CODE (XEXP (x, memindex)) == MEM
728 && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
729 return REGNO (XEXP (x, regindex));
731 return -1;
734 /* Return true if the elements of VEC starting at FIRST describe an
735 ldm or stm instruction (LOAD_P says which). */
738 h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
740 rtx last;
741 int nregs, i, regno, adjust;
743 /* There must be a stack adjustment, a register move, and at least one
744 other operation (a return or another register move). */
745 if (GET_NUM_ELEM (vec) < 3)
746 return false;
748 /* Get the range of registers to be pushed or popped. */
749 nregs = GET_NUM_ELEM (vec) - first - 1;
750 regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
752 /* Check that the call to h8300_ldm_stm_regno succeeded and
753 that we're only dealing with GPRs. */
754 if (regno < 0 || regno + nregs > 8)
755 return false;
757 /* 2-register h8s instructions must start with an even-numbered register.
758 3- and 4-register instructions must start with er0 or er4. */
759 if (!TARGET_H8300SX)
761 if ((regno & 1) != 0)
762 return false;
763 if (nregs > 2 && (regno & 3) != 0)
764 return false;
767 /* Check the other loads or stores. */
768 for (i = 1; i < nregs; i++)
769 if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
770 != regno + i)
771 return false;
773 /* Check the stack adjustment. */
774 last = RTVEC_ELT (vec, first + nregs);
775 adjust = (load_p ? nregs : -nregs) * 4;
776 return (GET_CODE (last) == SET
777 && SET_DEST (last) == stack_pointer_rtx
778 && h8300_stack_offset_p (SET_SRC (last), adjust));
781 /* This is what the stack looks like after the prolog of
782 a function with a frame has been set up:
784 <args>
786 FP <- fp
787 <locals>
788 <saved registers> <- sp
790 This is what the stack looks like after the prolog of
791 a function which doesn't have a frame:
793 <args>
795 <locals>
796 <saved registers> <- sp
799 /* Generate RTL code for the function prologue. */
801 void
802 h8300_expand_prologue (void)
804 int regno;
805 int saved_regs;
806 int n_regs;
808 /* If the current function has the OS_Task attribute set, then
809 we have a naked prologue. */
810 if (h8300_os_task_function_p (current_function_decl))
811 return;
813 if (h8300_monitor_function_p (current_function_decl))
814 /* My understanding of monitor functions is they act just like
815 interrupt functions, except the prologue must mask
816 interrupts. */
817 emit_insn (gen_monitor_prologue ());
819 if (frame_pointer_needed)
821 /* Push fp. */
822 push (HARD_FRAME_POINTER_REGNUM);
823 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
826 /* Push the rest of the registers in ascending order. */
827 saved_regs = compute_saved_regs ();
828 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
830 n_regs = 1;
831 if (saved_regs & (1 << regno))
833 if (TARGET_H8300S)
835 /* See how many registers we can push at the same time. */
836 if ((!TARGET_H8300SX || (regno & 3) == 0)
837 && ((saved_regs >> regno) & 0x0f) == 0x0f)
838 n_regs = 4;
840 else if ((!TARGET_H8300SX || (regno & 3) == 0)
841 && ((saved_regs >> regno) & 0x07) == 0x07)
842 n_regs = 3;
844 else if ((!TARGET_H8300SX || (regno & 1) == 0)
845 && ((saved_regs >> regno) & 0x03) == 0x03)
846 n_regs = 2;
849 h8300_push_pop (regno, n_regs, 0, 0);
853 /* Leave room for locals. */
854 h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()));
857 /* Return nonzero if we can use "rts" for the function currently being
858 compiled. */
861 h8300_can_use_return_insn_p (void)
863 return (reload_completed
864 && !frame_pointer_needed
865 && get_frame_size () == 0
866 && compute_saved_regs () == 0);
869 /* Generate RTL code for the function epilogue. */
871 void
872 h8300_expand_epilogue (void)
874 int regno;
875 int saved_regs;
876 int n_regs;
877 HOST_WIDE_INT frame_size;
878 bool returned_p;
880 if (h8300_os_task_function_p (current_function_decl))
881 /* OS_Task epilogues are nearly naked -- they just have an
882 rts instruction. */
883 return;
885 frame_size = round_frame_size (get_frame_size ());
886 returned_p = false;
888 /* Deallocate locals. */
889 h8300_emit_stack_adjustment (1, frame_size);
891 /* Pop the saved registers in descending order. */
892 saved_regs = compute_saved_regs ();
893 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
895 n_regs = 1;
896 if (saved_regs & (1 << regno))
898 if (TARGET_H8300S)
900 /* See how many registers we can pop at the same time. */
901 if ((TARGET_H8300SX || (regno & 3) == 3)
902 && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
903 n_regs = 4;
905 else if ((TARGET_H8300SX || (regno & 3) == 2)
906 && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
907 n_regs = 3;
909 else if ((TARGET_H8300SX || (regno & 1) == 1)
910 && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
911 n_regs = 2;
914 /* See if this pop would be the last insn before the return.
915 If so, use rte/l or rts/l instead of pop or ldm.l. */
916 if (TARGET_H8300SX
917 && !frame_pointer_needed
918 && frame_size == 0
919 && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
920 returned_p = true;
922 h8300_push_pop (regno - n_regs + 1, n_regs, 1, returned_p);
926 /* Pop frame pointer if we had one. */
927 if (frame_pointer_needed)
929 if (TARGET_H8300SX)
930 returned_p = true;
931 h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, 1, returned_p);
934 if (!returned_p)
935 emit_insn (gen_rtx_RETURN (VOIDmode));
938 /* Return nonzero if the current function is an interrupt
939 function. */
942 h8300_current_function_interrupt_function_p (void)
944 return (h8300_interrupt_function_p (current_function_decl)
945 || h8300_monitor_function_p (current_function_decl));
948 /* Output assembly code for the start of the file. */
950 static void
951 h8300_file_start (void)
953 default_file_start ();
955 if (TARGET_H8300H)
956 fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
957 else if (TARGET_H8300SX)
958 fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
959 else if (TARGET_H8300S)
960 fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
963 /* Output assembly language code for the end of file. */
965 static void
966 h8300_file_end (void)
968 fputs ("\t.end\n", asm_out_file);
971 /* Split an add of a small constant into two adds/subs insns.
973 If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
974 instead of adds/subs. */
976 void
977 split_adds_subs (enum machine_mode mode, rtx *operands)
979 HOST_WIDE_INT val = INTVAL (operands[1]);
980 rtx reg = operands[0];
981 HOST_WIDE_INT sign = 1;
982 HOST_WIDE_INT amount;
983 rtx (*gen_add) (rtx, rtx, rtx);
985 /* Force VAL to be positive so that we do not have to consider the
986 sign. */
987 if (val < 0)
989 val = -val;
990 sign = -1;
993 switch (mode)
995 case HImode:
996 gen_add = gen_addhi3;
997 break;
999 case SImode:
1000 gen_add = gen_addsi3;
1001 break;
1003 default:
1004 abort ();
1007 /* Try different amounts in descending order. */
1008 for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
1009 amount > 0;
1010 amount /= 2)
1012 for (; val >= amount; val -= amount)
1013 emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
1016 return;
1019 /* Handle machine specific pragmas for compatibility with existing
1020 compilers for the H8/300.
1022 pragma saveall generates prologue/epilogue code which saves and
1023 restores all the registers on function entry.
1025 pragma interrupt saves and restores all registers, and exits with
1026 an rte instruction rather than an rts. A pointer to a function
1027 with this attribute may be safely used in an interrupt vector. */
1029 void
1030 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1032 pragma_interrupt = 1;
1035 void
1036 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1038 pragma_saveall = 1;
1041 /* If the next function argument with MODE and TYPE is to be passed in
1042 a register, return a reg RTX for the hard register in which to pass
1043 the argument. CUM represents the state after the last argument.
1044 If the argument is to be pushed, NULL_RTX is returned. */
1047 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
1048 tree type, int named)
1050 static const char *const hand_list[] = {
1051 "__main",
1052 "__cmpsi2",
1053 "__divhi3",
1054 "__modhi3",
1055 "__udivhi3",
1056 "__umodhi3",
1057 "__divsi3",
1058 "__modsi3",
1059 "__udivsi3",
1060 "__umodsi3",
1061 "__mulhi3",
1062 "__mulsi3",
1063 "__reg_memcpy",
1064 "__reg_memset",
1065 "__ucmpsi2",
1069 rtx result = NULL_RTX;
1070 const char *fname;
1071 int regpass = 0;
1073 /* Never pass unnamed arguments in registers. */
1074 if (!named)
1075 return NULL_RTX;
1077 /* Pass 3 regs worth of data in regs when user asked on the command line. */
1078 if (TARGET_QUICKCALL)
1079 regpass = 3;
1081 /* If calling hand written assembler, use 4 regs of args. */
1082 if (cum->libcall)
1084 const char * const *p;
1086 fname = XSTR (cum->libcall, 0);
1088 /* See if this libcall is one of the hand coded ones. */
1089 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1092 if (*p)
1093 regpass = 4;
1096 if (regpass)
1098 int size;
1100 if (mode == BLKmode)
1101 size = int_size_in_bytes (type);
1102 else
1103 size = GET_MODE_SIZE (mode);
1105 if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1106 && cum->nbytes / UNITS_PER_WORD <= 3)
1107 result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1110 return result;
1113 /* Compute the cost of an and insn. */
1115 static int
1116 h8300_and_costs (rtx x)
1118 rtx operands[4];
1120 if (GET_MODE (x) == QImode)
1121 return 1;
1123 if (GET_MODE (x) != HImode
1124 && GET_MODE (x) != SImode)
1125 return 100;
1127 operands[0] = NULL;
1128 operands[1] = XEXP (x, 0);
1129 operands[2] = XEXP (x, 1);
1130 operands[3] = x;
1131 return compute_logical_op_length (GET_MODE (x), operands) / 2;
1134 /* Compute the cost of a shift insn. */
1136 static int
1137 h8300_shift_costs (rtx x)
1139 rtx operands[4];
1141 if (GET_MODE (x) != QImode
1142 && GET_MODE (x) != HImode
1143 && GET_MODE (x) != SImode)
1144 return 100;
1146 operands[0] = NULL;
1147 operands[1] = NULL;
1148 operands[2] = XEXP (x, 1);
1149 operands[3] = x;
1150 return compute_a_shift_length (NULL, operands) / 2;
1153 /* Worker function for TARGET_RTX_COSTS. */
1155 static bool
1156 h8300_rtx_costs (rtx x, int code, int outer_code, int *total)
1158 if (TARGET_H8300SX && outer_code == MEM)
1160 /* Estimate the number of execution states needed to calculate
1161 the address. */
1162 if (register_operand (x, VOIDmode)
1163 || GET_CODE (x) == POST_INC
1164 || GET_CODE (x) == POST_DEC
1165 || CONSTANT_P (x))
1166 *total = 0;
1167 else
1168 *total = COSTS_N_INSNS (1);
1169 return true;
1172 switch (code)
1174 case CONST_INT:
1176 HOST_WIDE_INT n = INTVAL (x);
1178 if (TARGET_H8300SX)
1180 /* Constant operands need the same number of processor
1181 states as register operands. Although we could try to
1182 use a size-based cost for optimize_size, the lack of
1183 of a mode makes the results very unpredictable. */
1184 *total = 0;
1185 return true;
1187 if (-4 <= n || n <= 4)
1189 switch ((int) n)
1191 case 0:
1192 *total = 0;
1193 return true;
1194 case 1:
1195 case 2:
1196 case -1:
1197 case -2:
1198 *total = 0 + (outer_code == SET);
1199 return true;
1200 case 4:
1201 case -4:
1202 if (TARGET_H8300H || TARGET_H8300S)
1203 *total = 0 + (outer_code == SET);
1204 else
1205 *total = 1;
1206 return true;
1209 *total = 1;
1210 return true;
1213 case CONST:
1214 case LABEL_REF:
1215 case SYMBOL_REF:
1216 if (TARGET_H8300SX)
1218 /* See comment for CONST_INT. */
1219 *total = 0;
1220 return true;
1222 *total = 3;
1223 return true;
1225 case CONST_DOUBLE:
1226 *total = 20;
1227 return true;
1229 case AND:
1230 if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1231 || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1232 return false;
1233 *total = COSTS_N_INSNS (h8300_and_costs (x));
1234 return true;
1236 /* We say that MOD and DIV are so expensive because otherwise we'll
1237 generate some really horrible code for division of a power of two. */
1238 case MOD:
1239 case DIV:
1240 case UMOD:
1241 case UDIV:
1242 if (TARGET_H8300SX)
1243 switch (GET_MODE (x))
1245 case QImode:
1246 case HImode:
1247 *total = COSTS_N_INSNS (optimize_size ? 4 : 10);
1248 return false;
1250 case SImode:
1251 *total = COSTS_N_INSNS (optimize_size ? 4 : 18);
1252 return false;
1254 default:
1255 break;
1257 *total = COSTS_N_INSNS (12);
1258 return true;
1260 case MULT:
1261 if (TARGET_H8300SX)
1262 switch (GET_MODE (x))
1264 case QImode:
1265 case HImode:
1266 *total = COSTS_N_INSNS (2);
1267 return false;
1269 case SImode:
1270 *total = COSTS_N_INSNS (5);
1271 return false;
1273 default:
1274 break;
1276 *total = COSTS_N_INSNS (4);
1277 return true;
1279 case ASHIFT:
1280 case ASHIFTRT:
1281 case LSHIFTRT:
1282 if (h8sx_binary_shift_operator (x, VOIDmode))
1284 *total = COSTS_N_INSNS (2);
1285 return false;
1287 else if (h8sx_unary_shift_operator (x, VOIDmode))
1289 *total = COSTS_N_INSNS (1);
1290 return false;
1292 *total = COSTS_N_INSNS (h8300_shift_costs (x));
1293 return true;
1295 case ROTATE:
1296 case ROTATERT:
1297 if (GET_MODE (x) == HImode)
1298 *total = 2;
1299 else
1300 *total = 8;
1301 return true;
1303 default:
1304 *total = COSTS_N_INSNS (1);
1305 return false;
1309 /* Documentation for the machine specific operand escapes:
1311 'E' like s but negative.
1312 'F' like t but negative.
1313 'G' constant just the negative
1314 'R' print operand as a byte:8 address if appropriate, else fall back to
1315 'X' handling.
1316 'S' print operand as a long word
1317 'T' print operand as a word
1318 'V' find the set bit, and print its number.
1319 'W' find the clear bit, and print its number.
1320 'X' print operand as a byte
1321 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1322 If this operand isn't a register, fall back to 'R' handling.
1323 'Z' print int & 7.
1324 'c' print the opcode corresponding to rtl
1325 'e' first word of 32 bit value - if reg, then least reg. if mem
1326 then least. if const then most sig word
1327 'f' second word of 32 bit value - if reg, then biggest reg. if mem
1328 then +2. if const then least sig word
1329 'j' print operand as condition code.
1330 'k' print operand as reverse condition code.
1331 'm' convert an integer operand to a size suffix (.b, .w or .l)
1332 'o' print an integer without a leading '#'
1333 's' print as low byte of 16 bit value
1334 't' print as high byte of 16 bit value
1335 'w' print as low byte of 32 bit value
1336 'x' print as 2nd byte of 32 bit value
1337 'y' print as 3rd byte of 32 bit value
1338 'z' print as msb of 32 bit value
1341 /* Return assembly language string which identifies a comparison type. */
1343 static const char *
1344 cond_string (enum rtx_code code)
1346 switch (code)
1348 case NE:
1349 return "ne";
1350 case EQ:
1351 return "eq";
1352 case GE:
1353 return "ge";
1354 case GT:
1355 return "gt";
1356 case LE:
1357 return "le";
1358 case LT:
1359 return "lt";
1360 case GEU:
1361 return "hs";
1362 case GTU:
1363 return "hi";
1364 case LEU:
1365 return "ls";
1366 case LTU:
1367 return "lo";
1368 default:
1369 abort ();
1373 /* Print operand X using operand code CODE to assembly language output file
1374 FILE. */
1376 void
1377 print_operand (FILE *file, rtx x, int code)
1379 /* This is used for communication between codes V,W,Z and Y. */
1380 static int bitint;
1382 switch (code)
1384 case 'E':
1385 switch (GET_CODE (x))
1387 case REG:
1388 fprintf (file, "%sl", names_big[REGNO (x)]);
1389 break;
1390 case CONST_INT:
1391 fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1392 break;
1393 default:
1394 abort ();
1396 break;
1397 case 'F':
1398 switch (GET_CODE (x))
1400 case REG:
1401 fprintf (file, "%sh", names_big[REGNO (x)]);
1402 break;
1403 case CONST_INT:
1404 fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1405 break;
1406 default:
1407 abort ();
1409 break;
1410 case 'G':
1411 if (GET_CODE (x) != CONST_INT)
1412 abort ();
1413 fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1414 break;
1415 case 'S':
1416 if (GET_CODE (x) == REG)
1417 fprintf (file, "%s", names_extended[REGNO (x)]);
1418 else
1419 goto def;
1420 break;
1421 case 'T':
1422 if (GET_CODE (x) == REG)
1423 fprintf (file, "%s", names_big[REGNO (x)]);
1424 else
1425 goto def;
1426 break;
1427 case 'V':
1428 bitint = exact_log2 (INTVAL (x) & 0xff);
1429 if (bitint == -1)
1430 abort ();
1431 fprintf (file, "#%d", bitint);
1432 break;
1433 case 'W':
1434 bitint = exact_log2 ((~INTVAL (x)) & 0xff);
1435 if (bitint == -1)
1436 abort ();
1437 fprintf (file, "#%d", bitint);
1438 break;
1439 case 'R':
1440 case 'X':
1441 if (GET_CODE (x) == REG)
1442 fprintf (file, "%s", byte_reg (x, 0));
1443 else
1444 goto def;
1445 break;
1446 case 'Y':
1447 if (bitint == -1)
1448 abort ();
1449 if (GET_CODE (x) == REG)
1450 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1451 else
1452 print_operand (file, x, 'R');
1453 bitint = -1;
1454 break;
1455 case 'Z':
1456 bitint = INTVAL (x);
1457 fprintf (file, "#%d", bitint & 7);
1458 break;
1459 case 'c':
1460 switch (GET_CODE (x))
1462 case IOR:
1463 fprintf (file, "or");
1464 break;
1465 case XOR:
1466 fprintf (file, "xor");
1467 break;
1468 case AND:
1469 fprintf (file, "and");
1470 break;
1471 default:
1472 break;
1474 break;
1475 case 'e':
1476 switch (GET_CODE (x))
1478 case REG:
1479 if (TARGET_H8300)
1480 fprintf (file, "%s", names_big[REGNO (x)]);
1481 else
1482 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1483 break;
1484 case MEM:
1485 print_operand (file, x, 0);
1486 break;
1487 case CONST_INT:
1488 fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1489 break;
1490 case CONST_DOUBLE:
1492 long val;
1493 REAL_VALUE_TYPE rv;
1494 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1495 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1496 fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1497 break;
1499 default:
1500 abort ();
1501 break;
1503 break;
1504 case 'f':
1505 switch (GET_CODE (x))
1507 case REG:
1508 if (TARGET_H8300)
1509 fprintf (file, "%s", names_big[REGNO (x) + 1]);
1510 else
1511 fprintf (file, "%s", names_big[REGNO (x)]);
1512 break;
1513 case MEM:
1514 x = adjust_address (x, HImode, 2);
1515 print_operand (file, x, 0);
1516 break;
1517 case CONST_INT:
1518 fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1519 break;
1520 case CONST_DOUBLE:
1522 long val;
1523 REAL_VALUE_TYPE rv;
1524 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1525 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1526 fprintf (file, "#%ld", (val & 0xffff));
1527 break;
1529 default:
1530 abort ();
1532 break;
1533 case 'j':
1534 fputs (cond_string (GET_CODE (x)), file);
1535 break;
1536 case 'k':
1537 fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1538 break;
1539 case 'm':
1540 if (GET_CODE (x) != CONST_INT)
1541 abort ();
1542 if (INTVAL (x) == 1)
1543 fputs (".b", file);
1544 else if (INTVAL (x) == 2)
1545 fputs (".w", file);
1546 else if (INTVAL (x) == 4)
1547 fputs (".l", file);
1548 else
1549 abort ();
1550 break;
1551 case 'o':
1552 print_operand_address (file, x);
1553 break;
1554 case 's':
1555 if (GET_CODE (x) == CONST_INT)
1556 fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1557 else
1558 fprintf (file, "%s", byte_reg (x, 0));
1559 break;
1560 case 't':
1561 if (GET_CODE (x) == CONST_INT)
1562 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1563 else
1564 fprintf (file, "%s", byte_reg (x, 1));
1565 break;
1566 case 'w':
1567 if (GET_CODE (x) == CONST_INT)
1568 fprintf (file, "#%ld", INTVAL (x) & 0xff);
1569 else
1570 fprintf (file, "%s",
1571 byte_reg (x, TARGET_H8300 ? 2 : 0));
1572 break;
1573 case 'x':
1574 if (GET_CODE (x) == CONST_INT)
1575 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1576 else
1577 fprintf (file, "%s",
1578 byte_reg (x, TARGET_H8300 ? 3 : 1));
1579 break;
1580 case 'y':
1581 if (GET_CODE (x) == CONST_INT)
1582 fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1583 else
1584 fprintf (file, "%s", byte_reg (x, 0));
1585 break;
1586 case 'z':
1587 if (GET_CODE (x) == CONST_INT)
1588 fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1589 else
1590 fprintf (file, "%s", byte_reg (x, 1));
1591 break;
1593 default:
1594 def:
1595 switch (GET_CODE (x))
1597 case REG:
1598 switch (GET_MODE (x))
1600 case QImode:
1601 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1602 fprintf (file, "%s", byte_reg (x, 0));
1603 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1604 fprintf (file, "%s", names_big[REGNO (x)]);
1605 #endif
1606 break;
1607 case HImode:
1608 fprintf (file, "%s", names_big[REGNO (x)]);
1609 break;
1610 case SImode:
1611 case SFmode:
1612 fprintf (file, "%s", names_extended[REGNO (x)]);
1613 break;
1614 default:
1615 abort ();
1617 break;
1619 case MEM:
1621 rtx addr = XEXP (x, 0);
1623 fprintf (file, "@");
1624 output_address (addr);
1626 /* Add a length suffix to constant addresses. Although this
1627 is often unnecessary, it helps to avoid ambiguity in the
1628 syntax of mova. If we wrote an insn like:
1630 mova/w.l @(1,@foo.b),er0
1632 then .b would be considered part of the symbol name.
1633 Adding a length after foo will avoid this. */
1634 if (CONSTANT_P (addr))
1635 switch (code)
1637 case 'R':
1638 /* Used for mov.b and bit operations. */
1639 if (h8300_eightbit_constant_address_p (addr))
1641 fprintf (file, ":8");
1642 break;
1645 /* Fall through. We should not get here if we are
1646 processing bit operations on H8/300 or H8/300H
1647 because 'U' constraint does not allow bit
1648 operations on the tiny area on these machines. */
1650 case 'X':
1651 case 'T':
1652 case 'S':
1653 if (h8300_constant_length (addr) == 2)
1654 fprintf (file, ":16");
1655 else
1656 fprintf (file, ":32");
1657 break;
1658 default:
1659 break;
1662 break;
1664 case CONST_INT:
1665 case SYMBOL_REF:
1666 case CONST:
1667 case LABEL_REF:
1668 fprintf (file, "#");
1669 print_operand_address (file, x);
1670 break;
1671 case CONST_DOUBLE:
1673 long val;
1674 REAL_VALUE_TYPE rv;
1675 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1676 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1677 fprintf (file, "#%ld", val);
1678 break;
1680 default:
1681 break;
1686 /* Output assembly language output for the address ADDR to FILE. */
1688 void
1689 print_operand_address (FILE *file, rtx addr)
1691 rtx index;
1692 int size;
1694 switch (GET_CODE (addr))
1696 case REG:
1697 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1698 break;
1700 case PRE_DEC:
1701 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1702 break;
1704 case POST_INC:
1705 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1706 break;
1708 case PRE_INC:
1709 fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1710 break;
1712 case POST_DEC:
1713 fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1714 break;
1716 case PLUS:
1717 fprintf (file, "(");
1719 index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1720 if (GET_CODE (index) == REG)
1722 /* reg,foo */
1723 print_operand_address (file, XEXP (addr, 1));
1724 fprintf (file, ",");
1725 switch (size)
1727 case 0:
1728 print_operand_address (file, index);
1729 break;
1731 case 1:
1732 print_operand (file, index, 'X');
1733 fputs (".b", file);
1734 break;
1736 case 2:
1737 print_operand (file, index, 'T');
1738 fputs (".w", file);
1739 break;
1741 case 4:
1742 print_operand (file, index, 'S');
1743 fputs (".l", file);
1744 break;
1746 /* print_operand_address (file, XEXP (addr, 0)); */
1748 else
1750 /* foo+k */
1751 print_operand_address (file, XEXP (addr, 0));
1752 fprintf (file, "+");
1753 print_operand_address (file, XEXP (addr, 1));
1755 fprintf (file, ")");
1756 break;
1758 case CONST_INT:
1760 /* Since the H8/300 only has 16 bit pointers, negative values are also
1761 those >= 32768. This happens for example with pointer minus a
1762 constant. We don't want to turn (char *p - 2) into
1763 (char *p + 65534) because loop unrolling can build upon this
1764 (IE: char *p + 131068). */
1765 int n = INTVAL (addr);
1766 if (TARGET_H8300)
1767 n = (int) (short) n;
1768 fprintf (file, "%d", n);
1769 break;
1772 default:
1773 output_addr_const (file, addr);
1774 break;
1778 /* Output all insn addresses and their sizes into the assembly language
1779 output file. This is helpful for debugging whether the length attributes
1780 in the md file are correct. This is not meant to be a user selectable
1781 option. */
1783 void
1784 final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
1785 int num_operands ATTRIBUTE_UNUSED)
1787 /* This holds the last insn address. */
1788 static int last_insn_address = 0;
1790 const int uid = INSN_UID (insn);
1792 if (TARGET_ADDRESSES)
1794 fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1795 INSN_ADDRESSES (uid) - last_insn_address);
1796 last_insn_address = INSN_ADDRESSES (uid);
1800 /* Prepare for an SI sized move. */
1803 h8300_expand_movsi (rtx operands[])
1805 rtx src = operands[1];
1806 rtx dst = operands[0];
1807 if (!reload_in_progress && !reload_completed)
1809 if (!register_operand (dst, GET_MODE (dst)))
1811 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1812 emit_move_insn (tmp, src);
1813 operands[1] = tmp;
1816 return 0;
1819 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1820 Define the offset between two registers, one to be eliminated, and
1821 the other its replacement, at the start of a routine. */
1824 h8300_initial_elimination_offset (int from, int to)
1826 /* The number of bytes that the return address takes on the stack. */
1827 int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1829 /* The number of bytes that the saved frame pointer takes on the stack. */
1830 int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1832 /* The number of bytes that the saved registers, excluding the frame
1833 pointer, take on the stack. */
1834 int saved_regs_size = 0;
1836 /* The number of bytes that the locals takes on the stack. */
1837 int frame_size = round_frame_size (get_frame_size ());
1839 int regno;
1841 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1842 if (WORD_REG_USED (regno))
1843 saved_regs_size += UNITS_PER_WORD;
1845 /* Adjust saved_regs_size because the above loop took the frame
1846 pointer int account. */
1847 saved_regs_size -= fp_size;
1849 if (to == HARD_FRAME_POINTER_REGNUM)
1851 switch (from)
1853 case ARG_POINTER_REGNUM:
1854 return pc_size + fp_size;
1855 case RETURN_ADDRESS_POINTER_REGNUM:
1856 return fp_size;
1857 case FRAME_POINTER_REGNUM:
1858 return -saved_regs_size;
1859 default:
1860 abort ();
1863 else if (to == STACK_POINTER_REGNUM)
1865 switch (from)
1867 case ARG_POINTER_REGNUM:
1868 return pc_size + saved_regs_size + frame_size;
1869 case RETURN_ADDRESS_POINTER_REGNUM:
1870 return saved_regs_size + frame_size;
1871 case FRAME_POINTER_REGNUM:
1872 return frame_size;
1873 default:
1874 abort ();
1877 else
1878 abort ();
1881 /* Worker function for RETURN_ADDR_RTX. */
1884 h8300_return_addr_rtx (int count, rtx frame)
1886 rtx ret;
1888 if (count == 0)
1889 ret = gen_rtx_MEM (Pmode,
1890 gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
1891 else if (flag_omit_frame_pointer)
1892 return (rtx) 0;
1893 else
1894 ret = gen_rtx_MEM (Pmode,
1895 memory_address (Pmode,
1896 plus_constant (frame, UNITS_PER_WORD)));
1897 set_mem_alias_set (ret, get_frame_alias_set ());
1898 return ret;
1901 /* Update the condition code from the insn. */
1903 void
1904 notice_update_cc (rtx body, rtx insn)
1906 rtx set;
1908 switch (get_attr_cc (insn))
1910 case CC_NONE:
1911 /* Insn does not affect CC at all. */
1912 break;
1914 case CC_NONE_0HIT:
1915 /* Insn does not change CC, but the 0'th operand has been changed. */
1916 if (cc_status.value1 != 0
1917 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
1918 cc_status.value1 = 0;
1919 if (cc_status.value2 != 0
1920 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
1921 cc_status.value2 = 0;
1922 break;
1924 case CC_SET_ZN:
1925 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
1926 The V flag is unusable. The C flag may or may not be known but
1927 that's ok because alter_cond will change tests to use EQ/NE. */
1928 CC_STATUS_INIT;
1929 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
1930 set = single_set (insn);
1931 cc_status.value1 = SET_SRC (set);
1932 if (SET_DEST (set) != cc0_rtx)
1933 cc_status.value2 = SET_DEST (set);
1934 break;
1936 case CC_SET_ZNV:
1937 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
1938 The C flag may or may not be known but that's ok because
1939 alter_cond will change tests to use EQ/NE. */
1940 CC_STATUS_INIT;
1941 cc_status.flags |= CC_NO_CARRY;
1942 set = single_set (insn);
1943 cc_status.value1 = SET_SRC (set);
1944 if (SET_DEST (set) != cc0_rtx)
1946 /* If the destination is STRICT_LOW_PART, strip off
1947 STRICT_LOW_PART. */
1948 if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
1949 cc_status.value2 = XEXP (SET_DEST (set), 0);
1950 else
1951 cc_status.value2 = SET_DEST (set);
1953 break;
1955 case CC_COMPARE:
1956 /* The insn is a compare instruction. */
1957 CC_STATUS_INIT;
1958 cc_status.value1 = SET_SRC (body);
1959 break;
1961 case CC_CLOBBER:
1962 /* Insn doesn't leave CC in a usable state. */
1963 CC_STATUS_INIT;
1964 break;
1968 /* Given that X occurs in an address of the form (plus X constant),
1969 return the part of X that is expected to be a register. There are
1970 four kinds of addressing mode to recognize:
1972 @(dd,Rn)
1973 @(dd,RnL.b)
1974 @(dd,Rn.w)
1975 @(dd,ERn.l)
1977 If SIZE is nonnull, and the address is one of the last three forms,
1978 set *SIZE to the index multiplication factor. Set it to 0 for
1979 plain @(dd,Rn) addresses.
1981 MODE is the mode of the value being accessed. It can be VOIDmode
1982 if the address is known to be valid, but its mode is unknown. */
1985 h8300_get_index (rtx x, enum machine_mode mode, int *size)
1987 int dummy, factor;
1989 if (size == 0)
1990 size = &dummy;
1992 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
1993 if (TARGET_H8300SX
1994 && factor <= 4
1995 && (mode == VOIDmode
1996 || GET_MODE_CLASS (mode) == MODE_INT
1997 || GET_MODE_CLASS (mode) == MODE_FLOAT))
1999 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
2001 /* When accessing byte-sized values, the index can be
2002 a zero-extended QImode or HImode register. */
2003 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
2004 return XEXP (x, 0);
2006 else
2008 /* We're looking for addresses of the form:
2010 (mult X I)
2011 or (mult (zero_extend X) I)
2013 where I is the size of the operand being accessed.
2014 The canonical form of the second expression is:
2016 (and (mult (subreg X) I) J)
2018 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2019 rtx index;
2021 if (GET_CODE (x) == AND
2022 && GET_CODE (XEXP (x, 1)) == CONST_INT
2023 && (factor == 0
2024 || INTVAL (XEXP (x, 1)) == 0xff * factor
2025 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2027 index = XEXP (x, 0);
2028 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2030 else
2032 index = x;
2033 *size = 4;
2036 if (GET_CODE (index) == MULT
2037 && GET_CODE (XEXP (index, 1)) == CONST_INT
2038 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2039 return XEXP (index, 0);
2042 *size = 0;
2043 return x;
2046 static const h8300_length_table addb_length_table =
2048 /* #xx Rs @aa @Rs @xx */
2049 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2050 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2051 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2052 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2055 static const h8300_length_table addw_length_table =
2057 /* #xx Rs @aa @Rs @xx */
2058 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2059 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2060 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2061 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2064 static const h8300_length_table addl_length_table =
2066 /* #xx Rs @aa @Rs @xx */
2067 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2068 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2069 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2070 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2073 #define logicb_length_table addb_length_table
2074 #define logicw_length_table addw_length_table
2076 static const h8300_length_table logicl_length_table =
2078 /* #xx Rs @aa @Rs @xx */
2079 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2080 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2081 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2082 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2085 static const h8300_length_table movb_length_table =
2087 /* #xx Rs @aa @Rs @xx */
2088 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2089 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2090 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2091 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2094 #define movw_length_table movb_length_table
2096 static const h8300_length_table movl_length_table =
2098 /* #xx Rs @aa @Rs @xx */
2099 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2100 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2101 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2102 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2105 /* Return the size of the given address or displacement constant. */
2107 static unsigned int
2108 h8300_constant_length (rtx constant)
2110 /* Check for (@d:16,Reg). */
2111 if (GET_CODE (constant) == CONST_INT
2112 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2113 return 2;
2115 /* Check for (@d:16,Reg) in cases where the displacement is
2116 an absolute address. */
2117 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2118 return 2;
2120 return 4;
2123 /* Return the size of a displacement field in address ADDR, which should
2124 have the form (plus X constant). SIZE is the number of bytes being
2125 accessed. */
2127 static unsigned int
2128 h8300_displacement_length (rtx addr, int size)
2130 rtx offset;
2132 offset = XEXP (addr, 1);
2134 /* Check for @(d:2,Reg). */
2135 if (register_operand (XEXP (addr, 0), VOIDmode)
2136 && GET_CODE (offset) == CONST_INT
2137 && (INTVAL (offset) == size
2138 || INTVAL (offset) == size * 2
2139 || INTVAL (offset) == size * 3))
2140 return 0;
2142 return h8300_constant_length (offset);
2145 /* Store the class of operand OP in *CLASS and return the length of any
2146 extra operand fields. SIZE is the number of bytes in OP. CLASS
2147 can be null if only the length is needed. */
2149 static unsigned int
2150 h8300_classify_operand (rtx op, int size, enum h8300_operand_class *class)
2152 enum h8300_operand_class dummy;
2154 if (class == 0)
2155 class = &dummy;
2157 if (CONSTANT_P (op))
2159 *class = H8OP_IMMEDIATE;
2161 /* Byte-sized immediates are stored in the opcode fields. */
2162 if (size == 1)
2163 return 0;
2165 /* If this is a 32-bit instruction, see whether the constant
2166 will fit into a 16-bit immediate field. */
2167 if (TARGET_H8300SX
2168 && size == 4
2169 && GET_CODE (op) == CONST_INT
2170 && IN_RANGE (INTVAL (op), 0, 0xffff))
2171 return 2;
2173 return size;
2175 else if (GET_CODE (op) == MEM)
2177 op = XEXP (op, 0);
2178 if (CONSTANT_P (op))
2180 *class = H8OP_MEM_ABSOLUTE;
2181 return h8300_constant_length (op);
2183 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2185 *class = H8OP_MEM_COMPLEX;
2186 return h8300_displacement_length (op, size);
2188 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2190 *class = H8OP_MEM_COMPLEX;
2191 return 0;
2193 else if (register_operand (op, VOIDmode))
2195 *class = H8OP_MEM_BASE;
2196 return 0;
2199 else if (register_operand (op, VOIDmode))
2201 *class = H8OP_REGISTER;
2202 return 0;
2204 abort ();
2207 /* Return the length of the instruction described by TABLE given that
2208 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2209 and OP2 must be an h8300_src_operand. */
2211 static unsigned int
2212 h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2214 enum h8300_operand_class op1_class, op2_class;
2215 unsigned int size, immediate_length;
2217 size = GET_MODE_SIZE (GET_MODE (op1));
2218 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2219 + h8300_classify_operand (op2, size, &op2_class));
2220 return immediate_length + (*table)[op1_class - 1][op2_class];
2223 /* Return the length of a unary instruction such as neg or not given that
2224 its operand is OP. */
2226 unsigned int
2227 h8300_unary_length (rtx op)
2229 enum h8300_operand_class class;
2230 unsigned int size, operand_length;
2232 size = GET_MODE_SIZE (GET_MODE (op));
2233 operand_length = h8300_classify_operand (op, size, &class);
2234 switch (class)
2236 case H8OP_REGISTER:
2237 return 2;
2239 case H8OP_MEM_BASE:
2240 return (size == 4 ? 6 : 4);
2242 case H8OP_MEM_ABSOLUTE:
2243 return operand_length + (size == 4 ? 6 : 4);
2245 case H8OP_MEM_COMPLEX:
2246 return operand_length + 6;
2248 default:
2249 abort ();
2253 /* Likewise short immediate instructions such as add.w #xx:3,OP. */
2255 static unsigned int
2256 h8300_short_immediate_length (rtx op)
2258 enum h8300_operand_class class;
2259 unsigned int size, operand_length;
2261 size = GET_MODE_SIZE (GET_MODE (op));
2262 operand_length = h8300_classify_operand (op, size, &class);
2264 switch (class)
2266 case H8OP_REGISTER:
2267 return 2;
2269 case H8OP_MEM_BASE:
2270 case H8OP_MEM_ABSOLUTE:
2271 case H8OP_MEM_COMPLEX:
2272 return 4 + operand_length;
2274 default:
2275 abort ();
2279 /* Likewise bitfield load and store instructions. */
2281 static unsigned int
2282 h8300_bitfield_length (rtx op, rtx op2)
2284 enum h8300_operand_class class;
2285 unsigned int size, operand_length;
2287 if (GET_CODE (op) == REG)
2288 op = op2;
2289 if (GET_CODE (op) == REG)
2290 abort ();
2292 size = GET_MODE_SIZE (GET_MODE (op));
2293 operand_length = h8300_classify_operand (op, size, &class);
2295 switch (class)
2297 case H8OP_MEM_BASE:
2298 case H8OP_MEM_ABSOLUTE:
2299 case H8OP_MEM_COMPLEX:
2300 return 4 + operand_length;
2302 default:
2303 abort ();
2307 /* Calculate the length of general binary instruction INSN using TABLE. */
2309 static unsigned int
2310 h8300_binary_length (rtx insn, const h8300_length_table *table)
2312 rtx set;
2314 set = single_set (insn);
2315 if (set == 0)
2316 abort ();
2318 if (BINARY_P (SET_SRC (set)))
2319 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2320 XEXP (SET_SRC (set), 1), table);
2321 else if (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY)
2322 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2323 XEXP (XEXP (SET_SRC (set), 1), 1),
2324 table);
2325 else
2326 abort ();
2329 /* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2330 memory reference and either (1) it has the form @(d:16,Rn) or
2331 (2) its address has the code given by INC_CODE. */
2333 static bool
2334 h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
2336 rtx addr;
2337 unsigned int size;
2339 if (GET_CODE (op) != MEM)
2340 return false;
2342 addr = XEXP (op, 0);
2343 size = GET_MODE_SIZE (GET_MODE (op));
2344 if (size != 1 && size != 2)
2345 return false;
2347 return (GET_CODE (addr) == inc_code
2348 || (GET_CODE (addr) == PLUS
2349 && GET_CODE (XEXP (addr, 0)) == REG
2350 && h8300_displacement_length (addr, size) == 2));
2353 /* Calculate the length of move instruction INSN using the given length
2354 table. Although the tables are correct for most cases, there is some
2355 irregularity in the length of mov.b and mov.w. The following forms:
2357 mov @ERs+, Rd
2358 mov @(d:16,ERs), Rd
2359 mov Rs, @-ERd
2360 mov Rs, @(d:16,ERd)
2362 are two bytes shorter than most other "mov Rs, @complex" or
2363 "mov @complex,Rd" combinations. */
2365 static unsigned int
2366 h8300_move_length (rtx *operands, const h8300_length_table *table)
2368 unsigned int size;
2370 size = h8300_length_from_table (operands[0], operands[1], table);
2371 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2372 size -= 2;
2373 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2374 size -= 2;
2375 return size;
2378 /* Return the length of a mova instruction with the given operands.
2379 DEST is the register destination, SRC is the source address and
2380 OFFSET is the 16-bit or 32-bit displacement. */
2382 static unsigned int
2383 h8300_mova_length (rtx dest, rtx src, rtx offset)
2385 unsigned int size;
2387 size = (2
2388 + h8300_constant_length (offset)
2389 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2390 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2391 size += 2;
2392 return size;
2395 /* Compute the length of INSN based on its length_table attribute.
2396 OPERANDS is the array of its operands. */
2398 unsigned int
2399 h8300_insn_length_from_table (rtx insn, rtx * operands)
2401 switch (get_attr_length_table (insn))
2403 case LENGTH_TABLE_NONE:
2404 abort ();
2406 case LENGTH_TABLE_ADDB:
2407 return h8300_binary_length (insn, &addb_length_table);
2409 case LENGTH_TABLE_ADDW:
2410 return h8300_binary_length (insn, &addw_length_table);
2412 case LENGTH_TABLE_ADDL:
2413 return h8300_binary_length (insn, &addl_length_table);
2415 case LENGTH_TABLE_LOGICB:
2416 return h8300_binary_length (insn, &logicb_length_table);
2418 case LENGTH_TABLE_MOVB:
2419 return h8300_move_length (operands, &movb_length_table);
2421 case LENGTH_TABLE_MOVW:
2422 return h8300_move_length (operands, &movw_length_table);
2424 case LENGTH_TABLE_MOVL:
2425 return h8300_move_length (operands, &movl_length_table);
2427 case LENGTH_TABLE_MOVA:
2428 return h8300_mova_length (operands[0], operands[1], operands[2]);
2430 case LENGTH_TABLE_MOVA_ZERO:
2431 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2433 case LENGTH_TABLE_UNARY:
2434 return h8300_unary_length (operands[0]);
2436 case LENGTH_TABLE_MOV_IMM4:
2437 return 2 + h8300_classify_operand (operands[0], 0, 0);
2439 case LENGTH_TABLE_SHORT_IMMEDIATE:
2440 return h8300_short_immediate_length (operands[0]);
2442 case LENGTH_TABLE_BITFIELD:
2443 return h8300_bitfield_length (operands[0], operands[1]);
2445 case LENGTH_TABLE_BITBRANCH:
2446 return h8300_bitfield_length (operands[1], operands[2]) - 2;
2449 abort ();
2452 /* Return true if LHS and RHS are memory references that can be mapped
2453 to the same h8sx assembly operand. LHS appears as the destination of
2454 an instruction and RHS appears as a source.
2456 Three cases are allowed:
2458 - RHS is @+Rn or @-Rn, LHS is @Rn
2459 - RHS is @Rn, LHS is @Rn+ or @Rn-
2460 - RHS and LHS have the same address and neither has side effects. */
2462 bool
2463 h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
2465 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2467 rhs = XEXP (rhs, 0);
2468 lhs = XEXP (lhs, 0);
2470 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2471 return rtx_equal_p (XEXP (rhs, 0), lhs);
2473 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2474 return rtx_equal_p (rhs, XEXP (lhs, 0));
2476 if (rtx_equal_p (rhs, lhs))
2477 return true;
2479 return false;
2482 /* Return true if OPERANDS[1] can be mapped to the same assembly
2483 operand as OPERANDS[0]. */
2485 bool
2486 h8300_operands_match_p (rtx *operands)
2488 if (register_operand (operands[0], VOIDmode)
2489 && register_operand (operands[1], VOIDmode))
2490 return true;
2492 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2493 return true;
2495 return false;
2498 /* Try using movmd to move LENGTH bytes from memory region SRC to memory
2499 region DEST. The two regions do not overlap and have the common
2500 alignment given by ALIGNMENT. Return true on success.
2502 Using movmd for variable-length moves seems to involve some
2503 complex trade-offs. For instance:
2505 - Preparing for a movmd instruction is similar to preparing
2506 for a memcpy. The main difference is that the arguments
2507 are moved into er4, er5 and er6 rather than er0, er1 and er2.
2509 - Since movmd clobbers the frame pointer, we need to save
2510 and restore it somehow when frame_pointer_needed. This can
2511 sometimes make movmd sequences longer than calls to memcpy().
2513 - The counter register is 16 bits, so the instruction is only
2514 suitable for variable-length moves when sizeof (size_t) == 2.
2515 That's only true in normal mode.
2517 - We will often lack static alignment information. Falling back
2518 on movmd.b would likely be slower than calling memcpy(), at least
2519 for big moves.
2521 This function therefore only uses movmd when the length is a
2522 known constant, and only then if -fomit-frame-pointer is in
2523 effect or if we're not optimizing for size.
2525 At the moment the function uses movmd for all in-range constants,
2526 but it might be better to fall back on memcpy() for large moves
2527 if ALIGNMENT == 1. */
2529 bool
2530 h8sx_emit_movmd (rtx dest, rtx src, rtx length,
2531 HOST_WIDE_INT alignment)
2533 if (!flag_omit_frame_pointer && optimize_size)
2534 return false;
2536 if (GET_CODE (length) == CONST_INT)
2538 rtx dest_reg, src_reg, first_dest, first_src;
2539 HOST_WIDE_INT n;
2540 int factor;
2542 /* Use movmd.l if the alignment allows it, otherwise fall back
2543 on movmd.b. */
2544 factor = (alignment >= 2 ? 4 : 1);
2546 /* Make sure the length is within range. We can handle counter
2547 values up to 65536, although HImode truncation will make
2548 the count appear negative in rtl dumps. */
2549 n = INTVAL (length);
2550 if (n <= 0 || n / factor > 65536)
2551 return false;
2553 /* Create temporary registers for the source and destination
2554 pointers. Initialize them to the start of each region. */
2555 dest_reg = copy_addr_to_reg (XEXP (dest, 0));
2556 src_reg = copy_addr_to_reg (XEXP (src, 0));
2558 /* Create references to the movmd source and destination blocks. */
2559 first_dest = replace_equiv_address (dest, dest_reg);
2560 first_src = replace_equiv_address (src, src_reg);
2562 set_mem_size (first_dest, GEN_INT (n & -factor));
2563 set_mem_size (first_src, GEN_INT (n & -factor));
2565 length = copy_to_mode_reg (HImode, gen_int_mode (n / factor, HImode));
2566 emit_insn (gen_movmd (first_dest, first_src, length, GEN_INT (factor)));
2568 if ((n & -factor) != n)
2570 /* Move SRC and DEST past the region we just copied.
2571 This is done to update the memory attributes. */
2572 dest = adjust_address (dest, BLKmode, n & -factor);
2573 src = adjust_address (src, BLKmode, n & -factor);
2575 /* Replace the addresses with the source and destination
2576 registers, which movmd has left with the right values. */
2577 dest = replace_equiv_address (dest, dest_reg);
2578 src = replace_equiv_address (src, src_reg);
2580 /* Mop up the left-over bytes. */
2581 if (n & 2)
2582 emit_move_insn (adjust_address (dest, HImode, 0),
2583 adjust_address (src, HImode, 0));
2584 if (n & 1)
2585 emit_move_insn (adjust_address (dest, QImode, n & 2),
2586 adjust_address (src, QImode, n & 2));
2588 return true;
2590 return false;
2593 /* Move ADDR into er6 after pushing its old value onto the stack. */
2595 void
2596 h8300_swap_into_er6 (rtx addr)
2598 push (HARD_FRAME_POINTER_REGNUM);
2599 emit_move_insn (hard_frame_pointer_rtx, addr);
2600 if (REGNO (addr) == SP_REG)
2601 emit_move_insn (hard_frame_pointer_rtx,
2602 plus_constant (hard_frame_pointer_rtx,
2603 GET_MODE_SIZE (word_mode)));
2606 /* Move the current value of er6 into ADDR and pop its old value
2607 from the stack. */
2609 void
2610 h8300_swap_out_of_er6 (rtx addr)
2612 if (REGNO (addr) != SP_REG)
2613 emit_move_insn (addr, hard_frame_pointer_rtx);
2614 pop (HARD_FRAME_POINTER_REGNUM);
2617 /* Return the length of mov instruction. */
2619 unsigned int
2620 compute_mov_length (rtx *operands)
2622 /* If the mov instruction involves a memory operand, we compute the
2623 length, assuming the largest addressing mode is used, and then
2624 adjust later in the function. Otherwise, we compute and return
2625 the exact length in one step. */
2626 enum machine_mode mode = GET_MODE (operands[0]);
2627 rtx dest = operands[0];
2628 rtx src = operands[1];
2629 rtx addr;
2631 if (GET_CODE (src) == MEM)
2632 addr = XEXP (src, 0);
2633 else if (GET_CODE (dest) == MEM)
2634 addr = XEXP (dest, 0);
2635 else
2636 addr = NULL_RTX;
2638 if (TARGET_H8300)
2640 unsigned int base_length;
2642 switch (mode)
2644 case QImode:
2645 if (addr == NULL_RTX)
2646 return 2;
2648 /* The eightbit addressing is available only in QImode, so
2649 go ahead and take care of it. */
2650 if (h8300_eightbit_constant_address_p (addr))
2651 return 2;
2653 base_length = 4;
2654 break;
2656 case HImode:
2657 if (addr == NULL_RTX)
2659 if (REG_P (src))
2660 return 2;
2662 if (src == const0_rtx)
2663 return 2;
2665 return 4;
2668 base_length = 4;
2669 break;
2671 case SImode:
2672 if (addr == NULL_RTX)
2674 if (REG_P (src))
2675 return 4;
2677 if (GET_CODE (src) == CONST_INT)
2679 if (src == const0_rtx)
2680 return 4;
2682 if ((INTVAL (src) & 0xffff) == 0)
2683 return 6;
2685 if ((INTVAL (src) & 0xffff) == 0)
2686 return 6;
2688 if ((INTVAL (src) & 0xffff)
2689 == ((INTVAL (src) >> 16) & 0xffff))
2690 return 6;
2692 return 8;
2695 base_length = 8;
2696 break;
2698 case SFmode:
2699 if (addr == NULL_RTX)
2701 if (REG_P (src))
2702 return 4;
2704 if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2705 return 4;
2707 return 8;
2710 base_length = 8;
2711 break;
2713 default:
2714 abort ();
2717 /* Adjust the length based on the addressing mode used.
2718 Specifically, we subtract the difference between the actual
2719 length and the longest one, which is @(d:16,Rs). For SImode
2720 and SFmode, we double the adjustment because two mov.w are
2721 used to do the job. */
2723 /* @Rs+ and @-Rd are 2 bytes shorter than the longest. */
2724 if (GET_CODE (addr) == PRE_DEC
2725 || GET_CODE (addr) == POST_INC)
2727 if (mode == QImode || mode == HImode)
2728 return base_length - 2;
2729 else
2730 /* In SImode and SFmode, we use two mov.w instructions, so
2731 double the adjustment. */
2732 return base_length - 4;
2735 /* @Rs and @Rd are 2 bytes shorter than the longest. Note that
2736 in SImode and SFmode, the second mov.w involves an address
2737 with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
2738 only 2 bytes. */
2739 if (GET_CODE (addr) == REG)
2740 return base_length - 2;
2742 return base_length;
2744 else
2746 unsigned int base_length;
2748 switch (mode)
2750 case QImode:
2751 if (addr == NULL_RTX)
2752 return 2;
2754 /* The eightbit addressing is available only in QImode, so
2755 go ahead and take care of it. */
2756 if (h8300_eightbit_constant_address_p (addr))
2757 return 2;
2759 base_length = 8;
2760 break;
2762 case HImode:
2763 if (addr == NULL_RTX)
2765 if (REG_P (src))
2766 return 2;
2768 if (src == const0_rtx)
2769 return 2;
2771 return 4;
2774 base_length = 8;
2775 break;
2777 case SImode:
2778 if (addr == NULL_RTX)
2780 if (REG_P (src))
2782 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2783 return 4;
2784 else
2785 return 2;
2788 if (GET_CODE (src) == CONST_INT)
2790 int val = INTVAL (src);
2792 if (val == 0)
2793 return 2;
2795 if (val == (val & 0x00ff) || val == (val & 0xff00))
2796 return 4;
2798 switch (val & 0xffffffff)
2800 case 0xffffffff:
2801 case 0xfffffffe:
2802 case 0xfffffffc:
2803 case 0x0000ffff:
2804 case 0x0000fffe:
2805 case 0xffff0000:
2806 case 0xfffe0000:
2807 case 0x00010000:
2808 case 0x00020000:
2809 return 4;
2812 return 6;
2815 base_length = 10;
2816 break;
2818 case SFmode:
2819 if (addr == NULL_RTX)
2821 if (REG_P (src))
2822 return 2;
2824 if (CONST_DOUBLE_OK_FOR_LETTER_P (src, 'G'))
2825 return 2;
2827 return 6;
2830 base_length = 10;
2831 break;
2833 default:
2834 abort ();
2837 /* Adjust the length based on the addressing mode used.
2838 Specifically, we subtract the difference between the actual
2839 length and the longest one, which is @(d:24,ERs). */
2841 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
2842 if (GET_CODE (addr) == PRE_DEC
2843 || GET_CODE (addr) == POST_INC)
2844 return base_length - 6;
2846 /* @ERs and @ERd are 6 bytes shorter than the longest. */
2847 if (GET_CODE (addr) == REG)
2848 return base_length - 6;
2850 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2851 longest. */
2852 if (GET_CODE (addr) == PLUS
2853 && GET_CODE (XEXP (addr, 0)) == REG
2854 && GET_CODE (XEXP (addr, 1)) == CONST_INT
2855 && INTVAL (XEXP (addr, 1)) > -32768
2856 && INTVAL (XEXP (addr, 1)) < 32767)
2857 return base_length - 4;
2859 /* @aa:16 is 4 bytes shorter than the longest. */
2860 if (h8300_tiny_constant_address_p (addr))
2861 return base_length - 4;
2863 /* @aa:24 is 2 bytes shorter than the longest. */
2864 if (CONSTANT_P (addr))
2865 return base_length - 2;
2867 return base_length;
2871 /* Output an addition insn. */
2873 const char *
2874 output_plussi (rtx *operands)
2876 enum machine_mode mode = GET_MODE (operands[0]);
2878 if (mode != SImode)
2879 abort ();
2881 if (TARGET_H8300)
2883 if (GET_CODE (operands[2]) == REG)
2884 return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2886 if (GET_CODE (operands[2]) == CONST_INT)
2888 HOST_WIDE_INT n = INTVAL (operands[2]);
2890 if ((n & 0xffffff) == 0)
2891 return "add\t%z2,%z0";
2892 if ((n & 0xffff) == 0)
2893 return "add\t%y2,%y0\n\taddx\t%z2,%z0";
2894 if ((n & 0xff) == 0)
2895 return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2898 return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
2900 else
2902 if (GET_CODE (operands[2]) == CONST_INT
2903 && register_operand (operands[1], VOIDmode))
2905 HOST_WIDE_INT intval = INTVAL (operands[2]);
2907 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2908 return "add.l\t%S2,%S0";
2909 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2910 return "sub.l\t%G2,%S0";
2912 /* See if we can finish with 2 bytes. */
2914 switch ((unsigned int) intval & 0xffffffff)
2916 case 0x00000001:
2917 case 0x00000002:
2918 case 0x00000004:
2919 return "adds\t%2,%S0";
2921 case 0xffffffff:
2922 case 0xfffffffe:
2923 case 0xfffffffc:
2924 return "subs\t%G2,%S0";
2926 case 0x00010000:
2927 case 0x00020000:
2928 operands[2] = GEN_INT (intval >> 16);
2929 return "inc.w\t%2,%e0";
2931 case 0xffff0000:
2932 case 0xfffe0000:
2933 operands[2] = GEN_INT (intval >> 16);
2934 return "dec.w\t%G2,%e0";
2937 /* See if we can finish with 4 bytes. */
2938 if ((intval & 0xffff) == 0)
2940 operands[2] = GEN_INT (intval >> 16);
2941 return "add.w\t%2,%e0";
2945 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
2947 operands[2] = GEN_INT (-INTVAL (operands[2]));
2948 return "sub.l\t%S2,%S0";
2950 return "add.l\t%S2,%S0";
2954 /* ??? It would be much easier to add the h8sx stuff if a single function
2955 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
2956 /* Compute the length of an addition insn. */
2958 unsigned int
2959 compute_plussi_length (rtx *operands)
2961 enum machine_mode mode = GET_MODE (operands[0]);
2963 if (mode != SImode)
2964 abort ();
2966 if (TARGET_H8300)
2968 if (GET_CODE (operands[2]) == REG)
2969 return 6;
2971 if (GET_CODE (operands[2]) == CONST_INT)
2973 HOST_WIDE_INT n = INTVAL (operands[2]);
2975 if ((n & 0xffffff) == 0)
2976 return 2;
2977 if ((n & 0xffff) == 0)
2978 return 4;
2979 if ((n & 0xff) == 0)
2980 return 6;
2983 return 8;
2985 else
2987 if (GET_CODE (operands[2]) == CONST_INT
2988 && register_operand (operands[1], VOIDmode))
2990 HOST_WIDE_INT intval = INTVAL (operands[2]);
2992 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2993 return 2;
2994 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2995 return 2;
2997 /* See if we can finish with 2 bytes. */
2999 switch ((unsigned int) intval & 0xffffffff)
3001 case 0x00000001:
3002 case 0x00000002:
3003 case 0x00000004:
3004 return 2;
3006 case 0xffffffff:
3007 case 0xfffffffe:
3008 case 0xfffffffc:
3009 return 2;
3011 case 0x00010000:
3012 case 0x00020000:
3013 return 2;
3015 case 0xffff0000:
3016 case 0xfffe0000:
3017 return 2;
3020 /* See if we can finish with 4 bytes. */
3021 if ((intval & 0xffff) == 0)
3022 return 4;
3025 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3026 return h8300_length_from_table (operands[0],
3027 GEN_INT (-INTVAL (operands[2])),
3028 &addl_length_table);
3029 else
3030 return h8300_length_from_table (operands[0], operands[2],
3031 &addl_length_table);
3032 return 6;
3036 /* Compute which flag bits are valid after an addition insn. */
3039 compute_plussi_cc (rtx *operands)
3041 enum machine_mode mode = GET_MODE (operands[0]);
3043 if (mode != SImode)
3044 abort ();
3046 if (TARGET_H8300)
3048 return CC_CLOBBER;
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 CC_SET_ZN;
3059 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3060 return CC_SET_ZN;
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 CC_NONE_0HIT;
3071 case 0xffffffff:
3072 case 0xfffffffe:
3073 case 0xfffffffc:
3074 return CC_NONE_0HIT;
3076 case 0x00010000:
3077 case 0x00020000:
3078 return CC_CLOBBER;
3080 case 0xffff0000:
3081 case 0xfffe0000:
3082 return CC_CLOBBER;
3085 /* See if we can finish with 4 bytes. */
3086 if ((intval & 0xffff) == 0)
3087 return CC_CLOBBER;
3090 return CC_SET_ZN;
3094 /* Output a logical insn. */
3096 const char *
3097 output_logical_op (enum machine_mode mode, rtx *operands)
3099 /* Figure out the logical op that we need to perform. */
3100 enum rtx_code code = GET_CODE (operands[3]);
3101 /* Pretend that every byte is affected if both operands are registers. */
3102 const unsigned HOST_WIDE_INT intval =
3103 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3104 /* Always use the full instruction if the
3105 first operand is in memory. It is better
3106 to use define_splits to generate the shorter
3107 sequence where valid. */
3108 && register_operand (operands[1], VOIDmode)
3109 ? INTVAL (operands[2]) : 0x55555555);
3110 /* The determinant of the algorithm. If we perform an AND, 0
3111 affects a bit. Otherwise, 1 affects a bit. */
3112 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3113 /* Break up DET into pieces. */
3114 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3115 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3116 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3117 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3118 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3119 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3120 int lower_half_easy_p = 0;
3121 int upper_half_easy_p = 0;
3122 /* The name of an insn. */
3123 const char *opname;
3124 char insn_buf[100];
3126 switch (code)
3128 case AND:
3129 opname = "and";
3130 break;
3131 case IOR:
3132 opname = "or";
3133 break;
3134 case XOR:
3135 opname = "xor";
3136 break;
3137 default:
3138 abort ();
3141 switch (mode)
3143 case HImode:
3144 /* First, see if we can finish with one insn. */
3145 if ((TARGET_H8300H || TARGET_H8300S)
3146 && b0 != 0
3147 && b1 != 0)
3149 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
3150 output_asm_insn (insn_buf, operands);
3152 else
3154 /* Take care of the lower byte. */
3155 if (b0 != 0)
3157 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
3158 output_asm_insn (insn_buf, operands);
3160 /* Take care of the upper byte. */
3161 if (b1 != 0)
3163 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
3164 output_asm_insn (insn_buf, operands);
3167 break;
3168 case SImode:
3169 if (TARGET_H8300H || TARGET_H8300S)
3171 /* Determine if the lower half can be taken care of in no more
3172 than two bytes. */
3173 lower_half_easy_p = (b0 == 0
3174 || b1 == 0
3175 || (code != IOR && w0 == 0xffff));
3177 /* Determine if the upper half can be taken care of in no more
3178 than two bytes. */
3179 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3180 || (code == AND && w1 == 0xff00));
3183 /* Check if doing everything with one insn is no worse than
3184 using multiple insns. */
3185 if ((TARGET_H8300H || TARGET_H8300S)
3186 && w0 != 0 && w1 != 0
3187 && !(lower_half_easy_p && upper_half_easy_p)
3188 && !(code == IOR && w1 == 0xffff
3189 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3191 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
3192 output_asm_insn (insn_buf, operands);
3194 else
3196 /* Take care of the lower and upper words individually. For
3197 each word, we try different methods in the order of
3199 1) the special insn (in case of AND or XOR),
3200 2) the word-wise insn, and
3201 3) The byte-wise insn. */
3202 if (w0 == 0xffff
3203 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3204 output_asm_insn ((code == AND)
3205 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
3206 operands);
3207 else if ((TARGET_H8300H || TARGET_H8300S)
3208 && (b0 != 0)
3209 && (b1 != 0))
3211 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
3212 output_asm_insn (insn_buf, operands);
3214 else
3216 if (b0 != 0)
3218 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
3219 output_asm_insn (insn_buf, operands);
3221 if (b1 != 0)
3223 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3224 output_asm_insn (insn_buf, operands);
3228 if ((w1 == 0xffff)
3229 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3230 output_asm_insn ((code == AND)
3231 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
3232 operands);
3233 else if ((TARGET_H8300H || TARGET_H8300S)
3234 && code == IOR
3235 && w1 == 0xffff
3236 && (w0 & 0x8000) != 0)
3238 output_asm_insn ("exts.l\t%S0", operands);
3240 else if ((TARGET_H8300H || TARGET_H8300S)
3241 && code == AND
3242 && w1 == 0xff00)
3244 output_asm_insn ("extu.w\t%e0", operands);
3246 else if (TARGET_H8300H || TARGET_H8300S)
3248 if (w1 != 0)
3250 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3251 output_asm_insn (insn_buf, operands);
3254 else
3256 if (b2 != 0)
3258 sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
3259 output_asm_insn (insn_buf, operands);
3261 if (b3 != 0)
3263 sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
3264 output_asm_insn (insn_buf, operands);
3268 break;
3269 default:
3270 abort ();
3272 return "";
3275 /* Compute the length of a logical insn. */
3277 unsigned int
3278 compute_logical_op_length (enum machine_mode mode, rtx *operands)
3280 /* Figure out the logical op that we need to perform. */
3281 enum rtx_code code = GET_CODE (operands[3]);
3282 /* Pretend that every byte is affected if both operands are registers. */
3283 const unsigned HOST_WIDE_INT intval =
3284 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3285 /* Always use the full instruction if the
3286 first operand is in memory. It is better
3287 to use define_splits to generate the shorter
3288 sequence where valid. */
3289 && register_operand (operands[1], VOIDmode)
3290 ? INTVAL (operands[2]) : 0x55555555);
3291 /* The determinant of the algorithm. If we perform an AND, 0
3292 affects a bit. Otherwise, 1 affects a bit. */
3293 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3294 /* Break up DET into pieces. */
3295 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3296 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3297 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3298 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3299 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3300 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3301 int lower_half_easy_p = 0;
3302 int upper_half_easy_p = 0;
3303 /* Insn length. */
3304 unsigned int length = 0;
3306 switch (mode)
3308 case HImode:
3309 /* First, see if we can finish with one insn. */
3310 if ((TARGET_H8300H || TARGET_H8300S)
3311 && b0 != 0
3312 && b1 != 0)
3314 length = h8300_length_from_table (operands[1], operands[2],
3315 &logicw_length_table);
3317 else
3319 /* Take care of the lower byte. */
3320 if (b0 != 0)
3321 length += 2;
3323 /* Take care of the upper byte. */
3324 if (b1 != 0)
3325 length += 2;
3327 break;
3328 case SImode:
3329 if (TARGET_H8300H || TARGET_H8300S)
3331 /* Determine if the lower half can be taken care of in no more
3332 than two bytes. */
3333 lower_half_easy_p = (b0 == 0
3334 || b1 == 0
3335 || (code != IOR && w0 == 0xffff));
3337 /* Determine if the upper half can be taken care of in no more
3338 than two bytes. */
3339 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3340 || (code == AND && w1 == 0xff00));
3343 /* Check if doing everything with one insn is no worse than
3344 using multiple insns. */
3345 if ((TARGET_H8300H || TARGET_H8300S)
3346 && w0 != 0 && w1 != 0
3347 && !(lower_half_easy_p && upper_half_easy_p)
3348 && !(code == IOR && w1 == 0xffff
3349 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3351 length = h8300_length_from_table (operands[1], operands[2],
3352 &logicl_length_table);
3354 else
3356 /* Take care of the lower and upper words individually. For
3357 each word, we try different methods in the order of
3359 1) the special insn (in case of AND or XOR),
3360 2) the word-wise insn, and
3361 3) The byte-wise insn. */
3362 if (w0 == 0xffff
3363 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3365 length += 2;
3367 else if ((TARGET_H8300H || TARGET_H8300S)
3368 && (b0 != 0)
3369 && (b1 != 0))
3371 length += 4;
3373 else
3375 if (b0 != 0)
3376 length += 2;
3378 if (b1 != 0)
3379 length += 2;
3382 if (w1 == 0xffff
3383 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3385 length += 2;
3387 else if ((TARGET_H8300H || TARGET_H8300S)
3388 && code == IOR
3389 && w1 == 0xffff
3390 && (w0 & 0x8000) != 0)
3392 length += 2;
3394 else if ((TARGET_H8300H || TARGET_H8300S)
3395 && code == AND
3396 && w1 == 0xff00)
3398 length += 2;
3400 else if (TARGET_H8300H || TARGET_H8300S)
3402 if (w1 != 0)
3403 length += 4;
3405 else
3407 if (b2 != 0)
3408 length += 2;
3410 if (b3 != 0)
3411 length += 2;
3414 break;
3415 default:
3416 abort ();
3418 return length;
3421 /* Compute which flag bits are valid after a logical insn. */
3424 compute_logical_op_cc (enum machine_mode mode, rtx *operands)
3426 /* Figure out the logical op that we need to perform. */
3427 enum rtx_code code = GET_CODE (operands[3]);
3428 /* Pretend that every byte is affected if both operands are registers. */
3429 const unsigned HOST_WIDE_INT intval =
3430 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3431 /* Always use the full instruction if the
3432 first operand is in memory. It is better
3433 to use define_splits to generate the shorter
3434 sequence where valid. */
3435 && register_operand (operands[1], VOIDmode)
3436 ? INTVAL (operands[2]) : 0x55555555);
3437 /* The determinant of the algorithm. If we perform an AND, 0
3438 affects a bit. Otherwise, 1 affects a bit. */
3439 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3440 /* Break up DET into pieces. */
3441 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3442 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3443 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3444 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3445 int lower_half_easy_p = 0;
3446 int upper_half_easy_p = 0;
3447 /* Condition code. */
3448 enum attr_cc cc = CC_CLOBBER;
3450 switch (mode)
3452 case HImode:
3453 /* First, see if we can finish with one insn. */
3454 if ((TARGET_H8300H || TARGET_H8300S)
3455 && b0 != 0
3456 && b1 != 0)
3458 cc = CC_SET_ZNV;
3460 break;
3461 case SImode:
3462 if (TARGET_H8300H || TARGET_H8300S)
3464 /* Determine if the lower half can be taken care of in no more
3465 than two bytes. */
3466 lower_half_easy_p = (b0 == 0
3467 || b1 == 0
3468 || (code != IOR && w0 == 0xffff));
3470 /* Determine if the upper half can be taken care of in no more
3471 than two bytes. */
3472 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3473 || (code == AND && w1 == 0xff00));
3476 /* Check if doing everything with one insn is no worse than
3477 using multiple insns. */
3478 if ((TARGET_H8300H || TARGET_H8300S)
3479 && w0 != 0 && w1 != 0
3480 && !(lower_half_easy_p && upper_half_easy_p)
3481 && !(code == IOR && w1 == 0xffff
3482 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3484 cc = CC_SET_ZNV;
3486 else
3488 if ((TARGET_H8300H || TARGET_H8300S)
3489 && code == IOR
3490 && w1 == 0xffff
3491 && (w0 & 0x8000) != 0)
3493 cc = CC_SET_ZNV;
3496 break;
3497 default:
3498 abort ();
3500 return cc;
3503 /* Expand a conditional branch. */
3505 void
3506 h8300_expand_branch (enum rtx_code code, rtx label)
3508 rtx tmp;
3510 tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3511 tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3512 gen_rtx_LABEL_REF (VOIDmode, label),
3513 pc_rtx);
3514 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
3517 /* Shifts.
3519 We devote a fair bit of code to getting efficient shifts since we
3520 can only shift one bit at a time on the H8/300 and H8/300H and only
3521 one or two bits at a time on the H8S.
3523 All shift code falls into one of the following ways of
3524 implementation:
3526 o SHIFT_INLINE: Emit straight line code for the shift; this is used
3527 when a straight line shift is about the same size or smaller than
3528 a loop.
3530 o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3531 off the bits we don't need. This is used when only a few of the
3532 bits in the original value will survive in the shifted value.
3534 o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3535 simulate a shift by 8, 16, or 24 bits. Once moved, a few inline
3536 shifts can be added if the shift count is slightly more than 8 or
3537 16. This case also includes other oddballs that are not worth
3538 explaining here.
3540 o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
3542 For each shift count, we try to use code that has no trade-off
3543 between code size and speed whenever possible.
3545 If the trade-off is unavoidable, we try to be reasonable.
3546 Specifically, the fastest version is one instruction longer than
3547 the shortest version, we take the fastest version. We also provide
3548 the use a way to switch back to the shortest version with -Os.
3550 For the details of the shift algorithms for various shift counts,
3551 refer to shift_alg_[qhs]i. */
3553 /* Classify a shift with the given mode and code. OP is the shift amount. */
3555 enum h8sx_shift_type
3556 h8sx_classify_shift (enum machine_mode mode, enum rtx_code code, rtx op)
3558 if (!TARGET_H8300SX)
3559 return H8SX_SHIFT_NONE;
3561 switch (code)
3563 case ASHIFT:
3564 case LSHIFTRT:
3565 /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd). */
3566 if (GET_CODE (op) != CONST_INT)
3567 return H8SX_SHIFT_BINARY;
3569 /* Reject out-of-range shift amounts. */
3570 if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3571 return H8SX_SHIFT_NONE;
3573 /* Power-of-2 shifts are effectively unary operations. */
3574 if (exact_log2 (INTVAL (op)) >= 0)
3575 return H8SX_SHIFT_UNARY;
3577 return H8SX_SHIFT_BINARY;
3579 case ASHIFTRT:
3580 if (op == const1_rtx || op == const2_rtx)
3581 return H8SX_SHIFT_UNARY;
3582 return H8SX_SHIFT_NONE;
3584 case ROTATE:
3585 if (GET_CODE (op) == CONST_INT
3586 && (INTVAL (op) == 1
3587 || INTVAL (op) == 2
3588 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3589 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3590 return H8SX_SHIFT_UNARY;
3591 return H8SX_SHIFT_NONE;
3593 default:
3594 return H8SX_SHIFT_NONE;
3598 /* Return the asm template for a single h8sx shift instruction.
3599 OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3600 is the source and OPERANDS[3] is the shift. SUFFIX is the
3601 size suffix ('b', 'w' or 'l') and OPTYPE is the print_operand
3602 prefix for the destination operand. */
3604 const char *
3605 output_h8sx_shift (rtx *operands, int suffix, int optype)
3607 static char buffer[16];
3608 const char *stem;
3610 switch (GET_CODE (operands[3]))
3612 case ASHIFT:
3613 stem = "shll";
3614 break;
3616 case ASHIFTRT:
3617 stem = "shar";
3618 break;
3620 case LSHIFTRT:
3621 stem = "shlr";
3622 break;
3624 case ROTATE:
3625 stem = "rotl";
3626 if (INTVAL (operands[2]) > 2)
3628 /* This is really a right rotate. */
3629 operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3630 - INTVAL (operands[2]));
3631 stem = "rotr";
3633 break;
3635 default:
3636 abort ();
3638 if (operands[2] == const1_rtx)
3639 sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3640 else
3641 sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3642 return buffer;
3645 /* Emit code to do shifts. */
3647 bool
3648 expand_a_shift (enum machine_mode mode, int code, rtx operands[])
3650 switch (h8sx_classify_shift (mode, code, operands[2]))
3652 case H8SX_SHIFT_BINARY:
3653 operands[1] = force_reg (mode, operands[1]);
3654 return false;
3656 case H8SX_SHIFT_UNARY:
3657 return false;
3659 case H8SX_SHIFT_NONE:
3660 break;
3663 emit_move_insn (operands[0], operands[1]);
3665 /* Need a loop to get all the bits we want - we generate the
3666 code at emit time, but need to allocate a scratch reg now. */
3668 emit_insn (gen_rtx_PARALLEL
3669 (VOIDmode,
3670 gen_rtvec (2,
3671 gen_rtx_SET (VOIDmode, operands[0],
3672 gen_rtx_fmt_ee (code, mode,
3673 operands[0], operands[2])),
3674 gen_rtx_CLOBBER (VOIDmode,
3675 gen_rtx_SCRATCH (QImode)))));
3676 return true;
3679 /* Symbols of the various modes which can be used as indices. */
3681 enum shift_mode
3683 QIshift, HIshift, SIshift
3686 /* For single bit shift insns, record assembler and what bits of the
3687 condition code are valid afterwards (represented as various CC_FOO
3688 bits, 0 means CC isn't left in a usable state). */
3690 struct shift_insn
3692 const char *const assembler;
3693 const int cc_valid;
3696 /* Assembler instruction shift table.
3698 These tables are used to look up the basic shifts.
3699 They are indexed by cpu, shift_type, and mode. */
3701 static const struct shift_insn shift_one[2][3][3] =
3703 /* H8/300 */
3705 /* SHIFT_ASHIFT */
3707 { "shll\t%X0", CC_SET_ZNV },
3708 { "add.w\t%T0,%T0", CC_SET_ZN },
3709 { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", CC_CLOBBER }
3711 /* SHIFT_LSHIFTRT */
3713 { "shlr\t%X0", CC_SET_ZNV },
3714 { "shlr\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3715 { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3717 /* SHIFT_ASHIFTRT */
3719 { "shar\t%X0", CC_SET_ZNV },
3720 { "shar\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3721 { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3724 /* H8/300H */
3726 /* SHIFT_ASHIFT */
3728 { "shll.b\t%X0", CC_SET_ZNV },
3729 { "shll.w\t%T0", CC_SET_ZNV },
3730 { "shll.l\t%S0", CC_SET_ZNV }
3732 /* SHIFT_LSHIFTRT */
3734 { "shlr.b\t%X0", CC_SET_ZNV },
3735 { "shlr.w\t%T0", CC_SET_ZNV },
3736 { "shlr.l\t%S0", CC_SET_ZNV }
3738 /* SHIFT_ASHIFTRT */
3740 { "shar.b\t%X0", CC_SET_ZNV },
3741 { "shar.w\t%T0", CC_SET_ZNV },
3742 { "shar.l\t%S0", CC_SET_ZNV }
3747 static const struct shift_insn shift_two[3][3] =
3749 /* SHIFT_ASHIFT */
3751 { "shll.b\t#2,%X0", CC_SET_ZNV },
3752 { "shll.w\t#2,%T0", CC_SET_ZNV },
3753 { "shll.l\t#2,%S0", CC_SET_ZNV }
3755 /* SHIFT_LSHIFTRT */
3757 { "shlr.b\t#2,%X0", CC_SET_ZNV },
3758 { "shlr.w\t#2,%T0", CC_SET_ZNV },
3759 { "shlr.l\t#2,%S0", CC_SET_ZNV }
3761 /* SHIFT_ASHIFTRT */
3763 { "shar.b\t#2,%X0", CC_SET_ZNV },
3764 { "shar.w\t#2,%T0", CC_SET_ZNV },
3765 { "shar.l\t#2,%S0", CC_SET_ZNV }
3769 /* Rotates are organized by which shift they'll be used in implementing.
3770 There's no need to record whether the cc is valid afterwards because
3771 it is the AND insn that will decide this. */
3773 static const char *const rotate_one[2][3][3] =
3775 /* H8/300 */
3777 /* SHIFT_ASHIFT */
3779 "rotr\t%X0",
3780 "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
3783 /* SHIFT_LSHIFTRT */
3785 "rotl\t%X0",
3786 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3789 /* SHIFT_ASHIFTRT */
3791 "rotl\t%X0",
3792 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3796 /* H8/300H */
3798 /* SHIFT_ASHIFT */
3800 "rotr.b\t%X0",
3801 "rotr.w\t%T0",
3802 "rotr.l\t%S0"
3804 /* SHIFT_LSHIFTRT */
3806 "rotl.b\t%X0",
3807 "rotl.w\t%T0",
3808 "rotl.l\t%S0"
3810 /* SHIFT_ASHIFTRT */
3812 "rotl.b\t%X0",
3813 "rotl.w\t%T0",
3814 "rotl.l\t%S0"
3819 static const char *const rotate_two[3][3] =
3821 /* SHIFT_ASHIFT */
3823 "rotr.b\t#2,%X0",
3824 "rotr.w\t#2,%T0",
3825 "rotr.l\t#2,%S0"
3827 /* SHIFT_LSHIFTRT */
3829 "rotl.b\t#2,%X0",
3830 "rotl.w\t#2,%T0",
3831 "rotl.l\t#2,%S0"
3833 /* SHIFT_ASHIFTRT */
3835 "rotl.b\t#2,%X0",
3836 "rotl.w\t#2,%T0",
3837 "rotl.l\t#2,%S0"
3841 struct shift_info {
3842 /* Shift algorithm. */
3843 enum shift_alg alg;
3845 /* The number of bits to be shifted by shift1 and shift2. Valid
3846 when ALG is SHIFT_SPECIAL. */
3847 unsigned int remainder;
3849 /* Special insn for a shift. Valid when ALG is SHIFT_SPECIAL. */
3850 const char *special;
3852 /* Insn for a one-bit shift. Valid when ALG is either SHIFT_INLINE
3853 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
3854 const char *shift1;
3856 /* Insn for a two-bit shift. Valid when ALG is either SHIFT_INLINE
3857 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
3858 const char *shift2;
3860 /* CC status for SHIFT_INLINE. */
3861 int cc_inline;
3863 /* CC status for SHIFT_SPECIAL. */
3864 int cc_special;
3867 static void get_shift_alg (enum shift_type,
3868 enum shift_mode, unsigned int,
3869 struct shift_info *);
3871 /* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
3872 best algorithm for doing the shift. The assembler code is stored
3873 in the pointers in INFO. We achieve the maximum efficiency in most
3874 cases when !TARGET_H8300. In case of TARGET_H8300, shifts in
3875 SImode in particular have a lot of room to optimize.
3877 We first determine the strategy of the shift algorithm by a table
3878 lookup. If that tells us to use a hand crafted assembly code, we
3879 go into the big switch statement to find what that is. Otherwise,
3880 we resort to a generic way, such as inlining. In either case, the
3881 result is returned through INFO. */
3883 static void
3884 get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
3885 unsigned int count, struct shift_info *info)
3887 enum h8_cpu cpu;
3889 /* Find the target CPU. */
3890 if (TARGET_H8300)
3891 cpu = H8_300;
3892 else if (TARGET_H8300H)
3893 cpu = H8_300H;
3894 else
3895 cpu = H8_S;
3897 /* Find the shift algorithm. */
3898 info->alg = SHIFT_LOOP;
3899 switch (shift_mode)
3901 case QIshift:
3902 if (count < GET_MODE_BITSIZE (QImode))
3903 info->alg = shift_alg_qi[cpu][shift_type][count];
3904 break;
3906 case HIshift:
3907 if (count < GET_MODE_BITSIZE (HImode))
3908 info->alg = shift_alg_hi[cpu][shift_type][count];
3909 break;
3911 case SIshift:
3912 if (count < GET_MODE_BITSIZE (SImode))
3913 info->alg = shift_alg_si[cpu][shift_type][count];
3914 break;
3916 default:
3917 abort ();
3920 /* Fill in INFO. Return unless we have SHIFT_SPECIAL. */
3921 switch (info->alg)
3923 case SHIFT_INLINE:
3924 info->remainder = count;
3925 /* Fall through. */
3927 case SHIFT_LOOP:
3928 /* It is up to the caller to know that looping clobbers cc. */
3929 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3930 info->shift2 = shift_two[shift_type][shift_mode].assembler;
3931 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
3932 goto end;
3934 case SHIFT_ROT_AND:
3935 info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
3936 info->shift2 = rotate_two[shift_type][shift_mode];
3937 info->cc_inline = CC_CLOBBER;
3938 goto end;
3940 case SHIFT_SPECIAL:
3941 /* REMAINDER is 0 for most cases, so initialize it to 0. */
3942 info->remainder = 0;
3943 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3944 info->shift2 = shift_two[shift_type][shift_mode].assembler;
3945 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
3946 info->cc_special = CC_CLOBBER;
3947 break;
3950 /* Here we only deal with SHIFT_SPECIAL. */
3951 switch (shift_mode)
3953 case QIshift:
3954 /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
3955 through the entire value. */
3956 if (shift_type == SHIFT_ASHIFTRT && count == 7)
3958 info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
3959 goto end;
3961 abort ();
3963 case HIshift:
3964 if (count == 7)
3966 switch (shift_type)
3968 case SHIFT_ASHIFT:
3969 if (TARGET_H8300)
3970 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";
3971 else
3972 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
3973 goto end;
3974 case SHIFT_LSHIFTRT:
3975 if (TARGET_H8300)
3976 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";
3977 else
3978 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
3979 goto end;
3980 case SHIFT_ASHIFTRT:
3981 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
3982 goto end;
3985 else if ((8 <= count && count <= 13)
3986 || (TARGET_H8300S && count == 14))
3988 info->remainder = count - 8;
3990 switch (shift_type)
3992 case SHIFT_ASHIFT:
3993 info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
3994 goto end;
3995 case SHIFT_LSHIFTRT:
3996 if (TARGET_H8300)
3998 info->special = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
3999 info->shift1 = "shlr.b\t%s0";
4000 info->cc_inline = CC_SET_ZNV;
4002 else
4004 info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
4005 info->cc_special = CC_SET_ZNV;
4007 goto end;
4008 case SHIFT_ASHIFTRT:
4009 if (TARGET_H8300)
4011 info->special = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0";
4012 info->shift1 = "shar.b\t%s0";
4014 else
4016 info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
4017 info->cc_special = CC_SET_ZNV;
4019 goto end;
4022 else if (count == 14)
4024 switch (shift_type)
4026 case SHIFT_ASHIFT:
4027 if (TARGET_H8300)
4028 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";
4029 goto end;
4030 case SHIFT_LSHIFTRT:
4031 if (TARGET_H8300)
4032 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";
4033 goto end;
4034 case SHIFT_ASHIFTRT:
4035 if (TARGET_H8300)
4036 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";
4037 else if (TARGET_H8300H)
4039 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";
4040 info->cc_special = CC_SET_ZNV;
4042 else /* TARGET_H8300S */
4043 abort ();
4044 goto end;
4047 else if (count == 15)
4049 switch (shift_type)
4051 case SHIFT_ASHIFT:
4052 info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
4053 goto end;
4054 case SHIFT_LSHIFTRT:
4055 info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
4056 goto end;
4057 case SHIFT_ASHIFTRT:
4058 info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
4059 goto end;
4062 abort ();
4064 case SIshift:
4065 if (TARGET_H8300 && 8 <= count && count <= 9)
4067 info->remainder = count - 8;
4069 switch (shift_type)
4071 case SHIFT_ASHIFT:
4072 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";
4073 goto end;
4074 case SHIFT_LSHIFTRT:
4075 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";
4076 info->shift1 = "shlr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0";
4077 goto end;
4078 case SHIFT_ASHIFTRT:
4079 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";
4080 goto end;
4083 else if (count == 8 && !TARGET_H8300)
4085 switch (shift_type)
4087 case SHIFT_ASHIFT:
4088 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";
4089 goto end;
4090 case SHIFT_LSHIFTRT:
4091 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";
4092 goto end;
4093 case SHIFT_ASHIFTRT:
4094 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";
4095 goto end;
4098 else if (count == 15 && TARGET_H8300)
4100 switch (shift_type)
4102 case SHIFT_ASHIFT:
4103 abort ();
4104 case SHIFT_LSHIFTRT:
4105 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";
4106 goto end;
4107 case SHIFT_ASHIFTRT:
4108 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";
4109 goto end;
4112 else if (count == 15 && !TARGET_H8300)
4114 switch (shift_type)
4116 case SHIFT_ASHIFT:
4117 info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
4118 info->cc_special = CC_SET_ZNV;
4119 goto end;
4120 case SHIFT_LSHIFTRT:
4121 info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
4122 info->cc_special = CC_SET_ZNV;
4123 goto end;
4124 case SHIFT_ASHIFTRT:
4125 abort ();
4128 else if ((TARGET_H8300 && 16 <= count && count <= 20)
4129 || (TARGET_H8300H && 16 <= count && count <= 19)
4130 || (TARGET_H8300S && 16 <= count && count <= 21))
4132 info->remainder = count - 16;
4134 switch (shift_type)
4136 case SHIFT_ASHIFT:
4137 info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
4138 if (TARGET_H8300)
4139 info->shift1 = "add.w\t%e0,%e0";
4140 goto end;
4141 case SHIFT_LSHIFTRT:
4142 if (TARGET_H8300)
4144 info->special = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
4145 info->shift1 = "shlr\t%x0\n\trotxr\t%w0";
4147 else
4149 info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
4150 info->cc_special = CC_SET_ZNV;
4152 goto end;
4153 case SHIFT_ASHIFTRT:
4154 if (TARGET_H8300)
4156 info->special = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
4157 info->shift1 = "shar\t%x0\n\trotxr\t%w0";
4159 else
4161 info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
4162 info->cc_special = CC_SET_ZNV;
4164 goto end;
4167 else if (TARGET_H8300 && 24 <= count && count <= 28)
4169 info->remainder = count - 24;
4171 switch (shift_type)
4173 case SHIFT_ASHIFT:
4174 info->special = "mov.b\t%w0,%z0\n\tsub.b\t%y0,%y0\n\tsub.w\t%f0,%f0";
4175 info->shift1 = "shll.b\t%z0";
4176 info->cc_inline = CC_SET_ZNV;
4177 goto end;
4178 case SHIFT_LSHIFTRT:
4179 info->special = "mov.b\t%z0,%w0\n\tsub.b\t%x0,%x0\n\tsub.w\t%e0,%e0";
4180 info->shift1 = "shlr.b\t%w0";
4181 info->cc_inline = CC_SET_ZNV;
4182 goto end;
4183 case SHIFT_ASHIFTRT:
4184 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";
4185 info->shift1 = "shar.b\t%w0";
4186 info->cc_inline = CC_SET_ZNV;
4187 goto end;
4190 else if ((TARGET_H8300H && count == 24)
4191 || (TARGET_H8300S && 24 <= count && count <= 25))
4193 info->remainder = count - 24;
4195 switch (shift_type)
4197 case SHIFT_ASHIFT:
4198 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";
4199 goto end;
4200 case SHIFT_LSHIFTRT:
4201 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
4202 info->cc_special = CC_SET_ZNV;
4203 goto end;
4204 case SHIFT_ASHIFTRT:
4205 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
4206 info->cc_special = CC_SET_ZNV;
4207 goto end;
4210 else if (!TARGET_H8300 && count == 28)
4212 switch (shift_type)
4214 case SHIFT_ASHIFT:
4215 if (TARGET_H8300H)
4216 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";
4217 else
4218 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";
4219 goto end;
4220 case SHIFT_LSHIFTRT:
4221 if (TARGET_H8300H)
4223 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";
4224 info->cc_special = CC_SET_ZNV;
4226 else
4227 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4228 goto end;
4229 case SHIFT_ASHIFTRT:
4230 abort ();
4233 else if (!TARGET_H8300 && count == 29)
4235 switch (shift_type)
4237 case SHIFT_ASHIFT:
4238 if (TARGET_H8300H)
4239 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";
4240 else
4241 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4242 goto end;
4243 case SHIFT_LSHIFTRT:
4244 if (TARGET_H8300H)
4246 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";
4247 info->cc_special = CC_SET_ZNV;
4249 else
4251 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4252 info->cc_special = CC_SET_ZNV;
4254 goto end;
4255 case SHIFT_ASHIFTRT:
4256 abort ();
4259 else if (!TARGET_H8300 && count == 30)
4261 switch (shift_type)
4263 case SHIFT_ASHIFT:
4264 if (TARGET_H8300H)
4265 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4266 else
4267 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
4268 goto end;
4269 case SHIFT_LSHIFTRT:
4270 if (TARGET_H8300H)
4271 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4272 else
4273 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4274 goto end;
4275 case SHIFT_ASHIFTRT:
4276 abort ();
4279 else if (count == 31)
4281 if (TARGET_H8300)
4283 switch (shift_type)
4285 case SHIFT_ASHIFT:
4286 info->special = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
4287 goto end;
4288 case SHIFT_LSHIFTRT:
4289 info->special = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
4290 goto end;
4291 case SHIFT_ASHIFTRT:
4292 info->special = "shll\t%z0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
4293 goto end;
4296 else
4298 switch (shift_type)
4300 case SHIFT_ASHIFT:
4301 info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
4302 info->cc_special = CC_SET_ZNV;
4303 goto end;
4304 case SHIFT_LSHIFTRT:
4305 info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
4306 info->cc_special = CC_SET_ZNV;
4307 goto end;
4308 case SHIFT_ASHIFTRT:
4309 info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
4310 info->cc_special = CC_SET_ZNV;
4311 goto end;
4315 abort ();
4317 default:
4318 abort ();
4321 end:
4322 if (!TARGET_H8300S)
4323 info->shift2 = NULL;
4326 /* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
4327 needed for some shift with COUNT and MODE. Return 0 otherwise. */
4330 h8300_shift_needs_scratch_p (int count, enum machine_mode mode)
4332 enum h8_cpu cpu;
4333 int a, lr, ar;
4335 if (GET_MODE_BITSIZE (mode) <= count)
4336 return 1;
4338 /* Find out the target CPU. */
4339 if (TARGET_H8300)
4340 cpu = H8_300;
4341 else if (TARGET_H8300H)
4342 cpu = H8_300H;
4343 else
4344 cpu = H8_S;
4346 /* Find the shift algorithm. */
4347 switch (mode)
4349 case QImode:
4350 a = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
4351 lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
4352 ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
4353 break;
4355 case HImode:
4356 a = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
4357 lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
4358 ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
4359 break;
4361 case SImode:
4362 a = shift_alg_si[cpu][SHIFT_ASHIFT][count];
4363 lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
4364 ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4365 break;
4367 default:
4368 abort ();
4371 /* On H8/300H, count == 8 uses a scratch register. */
4372 return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4373 || (TARGET_H8300H && mode == SImode && count == 8));
4376 /* Output the assembler code for doing shifts. */
4378 const char *
4379 output_a_shift (rtx *operands)
4381 static int loopend_lab;
4382 rtx shift = operands[3];
4383 enum machine_mode mode = GET_MODE (shift);
4384 enum rtx_code code = GET_CODE (shift);
4385 enum shift_type shift_type;
4386 enum shift_mode shift_mode;
4387 struct shift_info info;
4389 loopend_lab++;
4391 switch (mode)
4393 case QImode:
4394 shift_mode = QIshift;
4395 break;
4396 case HImode:
4397 shift_mode = HIshift;
4398 break;
4399 case SImode:
4400 shift_mode = SIshift;
4401 break;
4402 default:
4403 abort ();
4406 switch (code)
4408 case ASHIFTRT:
4409 shift_type = SHIFT_ASHIFTRT;
4410 break;
4411 case LSHIFTRT:
4412 shift_type = SHIFT_LSHIFTRT;
4413 break;
4414 case ASHIFT:
4415 shift_type = SHIFT_ASHIFT;
4416 break;
4417 default:
4418 abort ();
4421 if (GET_CODE (operands[2]) != CONST_INT)
4423 /* This case must be taken care of by one of the two splitters
4424 that convert a variable shift into a loop. */
4425 abort ();
4427 else
4429 int n = INTVAL (operands[2]);
4431 /* If the count is negative, make it 0. */
4432 if (n < 0)
4433 n = 0;
4434 /* If the count is too big, truncate it.
4435 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4436 do the intuitive thing. */
4437 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4438 n = GET_MODE_BITSIZE (mode);
4440 get_shift_alg (shift_type, shift_mode, n, &info);
4442 switch (info.alg)
4444 case SHIFT_SPECIAL:
4445 output_asm_insn (info.special, operands);
4446 /* Fall through. */
4448 case SHIFT_INLINE:
4449 n = info.remainder;
4451 /* Emit two bit shifts first. */
4452 if (info.shift2 != NULL)
4454 for (; n > 1; n -= 2)
4455 output_asm_insn (info.shift2, operands);
4458 /* Now emit one bit shifts for any residual. */
4459 for (; n > 0; n--)
4460 output_asm_insn (info.shift1, operands);
4461 return "";
4463 case SHIFT_ROT_AND:
4465 int m = GET_MODE_BITSIZE (mode) - n;
4466 const int mask = (shift_type == SHIFT_ASHIFT
4467 ? ((1 << m) - 1) << n
4468 : (1 << m) - 1);
4469 char insn_buf[200];
4471 /* Not all possibilities of rotate are supported. They shouldn't
4472 be generated, but let's watch for 'em. */
4473 if (info.shift1 == 0)
4474 abort ();
4476 /* Emit two bit rotates first. */
4477 if (info.shift2 != NULL)
4479 for (; m > 1; m -= 2)
4480 output_asm_insn (info.shift2, operands);
4483 /* Now single bit rotates for any residual. */
4484 for (; m > 0; m--)
4485 output_asm_insn (info.shift1, operands);
4487 /* Now mask off the high bits. */
4488 if (mode == QImode)
4489 sprintf (insn_buf, "and\t#%d,%%X0", mask);
4490 else if (mode == HImode && (TARGET_H8300H || TARGET_H8300S))
4491 sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4492 else
4493 abort ();
4495 output_asm_insn (insn_buf, operands);
4496 return "";
4499 case SHIFT_LOOP:
4500 /* A loop to shift by a "large" constant value.
4501 If we have shift-by-2 insns, use them. */
4502 if (info.shift2 != NULL)
4504 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n / 2,
4505 names_big[REGNO (operands[4])]);
4506 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4507 output_asm_insn (info.shift2, operands);
4508 output_asm_insn ("add #0xff,%X4", operands);
4509 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4510 if (n % 2)
4511 output_asm_insn (info.shift1, operands);
4513 else
4515 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n,
4516 names_big[REGNO (operands[4])]);
4517 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4518 output_asm_insn (info.shift1, operands);
4519 output_asm_insn ("add #0xff,%X4", operands);
4520 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4522 return "";
4524 default:
4525 abort ();
4530 /* Count the number of assembly instructions in a string TEMPLATE. */
4532 static unsigned int
4533 h8300_asm_insn_count (const char *template)
4535 unsigned int count = 1;
4537 for (; *template; template++)
4538 if (*template == '\n')
4539 count++;
4541 return count;
4544 /* Compute the length of a shift insn. */
4546 unsigned int
4547 compute_a_shift_length (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4549 rtx shift = operands[3];
4550 enum machine_mode mode = GET_MODE (shift);
4551 enum rtx_code code = GET_CODE (shift);
4552 enum shift_type shift_type;
4553 enum shift_mode shift_mode;
4554 struct shift_info info;
4555 unsigned int wlength = 0;
4557 switch (mode)
4559 case QImode:
4560 shift_mode = QIshift;
4561 break;
4562 case HImode:
4563 shift_mode = HIshift;
4564 break;
4565 case SImode:
4566 shift_mode = SIshift;
4567 break;
4568 default:
4569 abort ();
4572 switch (code)
4574 case ASHIFTRT:
4575 shift_type = SHIFT_ASHIFTRT;
4576 break;
4577 case LSHIFTRT:
4578 shift_type = SHIFT_LSHIFTRT;
4579 break;
4580 case ASHIFT:
4581 shift_type = SHIFT_ASHIFT;
4582 break;
4583 default:
4584 abort ();
4587 if (GET_CODE (operands[2]) != CONST_INT)
4589 /* Get the assembler code to do one shift. */
4590 get_shift_alg (shift_type, shift_mode, 1, &info);
4592 return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4594 else
4596 int n = INTVAL (operands[2]);
4598 /* If the count is negative, make it 0. */
4599 if (n < 0)
4600 n = 0;
4601 /* If the count is too big, truncate it.
4602 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4603 do the intuitive thing. */
4604 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4605 n = GET_MODE_BITSIZE (mode);
4607 get_shift_alg (shift_type, shift_mode, n, &info);
4609 switch (info.alg)
4611 case SHIFT_SPECIAL:
4612 wlength += h8300_asm_insn_count (info.special);
4614 /* Every assembly instruction used in SHIFT_SPECIAL case
4615 takes 2 bytes except xor.l, which takes 4 bytes, so if we
4616 see xor.l, we just pretend that xor.l counts as two insns
4617 so that the insn length will be computed correctly. */
4618 if (strstr (info.special, "xor.l") != NULL)
4619 wlength++;
4621 /* Fall through. */
4623 case SHIFT_INLINE:
4624 n = info.remainder;
4626 if (info.shift2 != NULL)
4628 wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4629 n = n % 2;
4632 wlength += h8300_asm_insn_count (info.shift1) * n;
4634 return 2 * wlength;
4636 case SHIFT_ROT_AND:
4638 int m = GET_MODE_BITSIZE (mode) - n;
4640 /* Not all possibilities of rotate are supported. They shouldn't
4641 be generated, but let's watch for 'em. */
4642 if (info.shift1 == 0)
4643 abort ();
4645 if (info.shift2 != NULL)
4647 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4648 m = m % 2;
4651 wlength += h8300_asm_insn_count (info.shift1) * m;
4653 /* Now mask off the high bits. */
4654 switch (mode)
4656 case QImode:
4657 wlength += 1;
4658 break;
4659 case HImode:
4660 wlength += 2;
4661 break;
4662 case SImode:
4663 if (TARGET_H8300)
4664 abort ();
4665 wlength += 3;
4666 break;
4667 default:
4668 abort ();
4670 return 2 * wlength;
4673 case SHIFT_LOOP:
4674 /* A loop to shift by a "large" constant value.
4675 If we have shift-by-2 insns, use them. */
4676 if (info.shift2 != NULL)
4678 wlength += 3 + h8300_asm_insn_count (info.shift2);
4679 if (n % 2)
4680 wlength += h8300_asm_insn_count (info.shift1);
4682 else
4684 wlength += 3 + h8300_asm_insn_count (info.shift1);
4686 return 2 * wlength;
4688 default:
4689 abort ();
4694 /* Compute which flag bits are valid after a shift insn. */
4697 compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4699 rtx shift = operands[3];
4700 enum machine_mode mode = GET_MODE (shift);
4701 enum rtx_code code = GET_CODE (shift);
4702 enum shift_type shift_type;
4703 enum shift_mode shift_mode;
4704 struct shift_info info;
4706 switch (mode)
4708 case QImode:
4709 shift_mode = QIshift;
4710 break;
4711 case HImode:
4712 shift_mode = HIshift;
4713 break;
4714 case SImode:
4715 shift_mode = SIshift;
4716 break;
4717 default:
4718 abort ();
4721 switch (code)
4723 case ASHIFTRT:
4724 shift_type = SHIFT_ASHIFTRT;
4725 break;
4726 case LSHIFTRT:
4727 shift_type = SHIFT_LSHIFTRT;
4728 break;
4729 case ASHIFT:
4730 shift_type = SHIFT_ASHIFT;
4731 break;
4732 default:
4733 abort ();
4736 if (GET_CODE (operands[2]) != CONST_INT)
4738 /* This case must be taken care of by one of the two splitters
4739 that convert a variable shift into a loop. */
4740 abort ();
4742 else
4744 int n = INTVAL (operands[2]);
4746 /* If the count is negative, make it 0. */
4747 if (n < 0)
4748 n = 0;
4749 /* If the count is too big, truncate it.
4750 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4751 do the intuitive thing. */
4752 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4753 n = GET_MODE_BITSIZE (mode);
4755 get_shift_alg (shift_type, shift_mode, n, &info);
4757 switch (info.alg)
4759 case SHIFT_SPECIAL:
4760 if (info.remainder == 0)
4761 return info.cc_special;
4763 /* Fall through. */
4765 case SHIFT_INLINE:
4766 return info.cc_inline;
4768 case SHIFT_ROT_AND:
4769 /* This case always ends with an and instruction. */
4770 return CC_SET_ZNV;
4772 case SHIFT_LOOP:
4773 /* A loop to shift by a "large" constant value.
4774 If we have shift-by-2 insns, use them. */
4775 if (info.shift2 != NULL)
4777 if (n % 2)
4778 return info.cc_inline;
4780 return CC_CLOBBER;
4782 default:
4783 abort ();
4788 /* A rotation by a non-constant will cause a loop to be generated, in
4789 which a rotation by one bit is used. A rotation by a constant,
4790 including the one in the loop, will be taken care of by
4791 output_a_rotate () at the insn emit time. */
4794 expand_a_rotate (rtx operands[])
4796 rtx dst = operands[0];
4797 rtx src = operands[1];
4798 rtx rotate_amount = operands[2];
4799 enum machine_mode mode = GET_MODE (dst);
4801 if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4802 return false;
4804 /* We rotate in place. */
4805 emit_move_insn (dst, src);
4807 if (GET_CODE (rotate_amount) != CONST_INT)
4809 rtx counter = gen_reg_rtx (QImode);
4810 rtx start_label = gen_label_rtx ();
4811 rtx end_label = gen_label_rtx ();
4813 /* If the rotate amount is less than or equal to 0,
4814 we go out of the loop. */
4815 emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
4816 QImode, 0, end_label);
4818 /* Initialize the loop counter. */
4819 emit_move_insn (counter, rotate_amount);
4821 emit_label (start_label);
4823 /* Rotate by one bit. */
4824 switch (mode)
4826 case QImode:
4827 emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
4828 break;
4829 case HImode:
4830 emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
4831 break;
4832 case SImode:
4833 emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
4834 break;
4835 default:
4836 abort ();
4839 /* Decrement the counter by 1. */
4840 emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
4842 /* If the loop counter is nonzero, we go back to the beginning
4843 of the loop. */
4844 emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
4845 start_label);
4847 emit_label (end_label);
4849 else
4851 /* Rotate by AMOUNT bits. */
4852 switch (mode)
4854 case QImode:
4855 emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
4856 break;
4857 case HImode:
4858 emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
4859 break;
4860 case SImode:
4861 emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
4862 break;
4863 default:
4864 abort ();
4868 return 1;
4871 /* Output a rotate insn. */
4873 const char *
4874 output_a_rotate (enum rtx_code code, rtx *operands)
4876 rtx dst = operands[0];
4877 rtx rotate_amount = operands[2];
4878 enum shift_mode rotate_mode;
4879 enum shift_type rotate_type;
4880 const char *insn_buf;
4881 int bits;
4882 int amount;
4883 enum machine_mode mode = GET_MODE (dst);
4885 if (GET_CODE (rotate_amount) != CONST_INT)
4886 abort ();
4888 switch (mode)
4890 case QImode:
4891 rotate_mode = QIshift;
4892 break;
4893 case HImode:
4894 rotate_mode = HIshift;
4895 break;
4896 case SImode:
4897 rotate_mode = SIshift;
4898 break;
4899 default:
4900 abort ();
4903 switch (code)
4905 case ROTATERT:
4906 rotate_type = SHIFT_ASHIFT;
4907 break;
4908 case ROTATE:
4909 rotate_type = SHIFT_LSHIFTRT;
4910 break;
4911 default:
4912 abort ();
4915 amount = INTVAL (rotate_amount);
4917 /* Clean up AMOUNT. */
4918 if (amount < 0)
4919 amount = 0;
4920 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4921 amount = GET_MODE_BITSIZE (mode);
4923 /* Determine the faster direction. After this phase, amount will be
4924 at most a half of GET_MODE_BITSIZE (mode). */
4925 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
4927 /* Flip the direction. */
4928 amount = GET_MODE_BITSIZE (mode) - amount;
4929 rotate_type =
4930 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4933 /* See if a byte swap (in HImode) or a word swap (in SImode) can
4934 boost up the rotation. */
4935 if ((mode == HImode && TARGET_H8300 && amount >= 5)
4936 || (mode == HImode && TARGET_H8300H && amount >= 6)
4937 || (mode == HImode && TARGET_H8300S && amount == 8)
4938 || (mode == SImode && TARGET_H8300H && amount >= 10)
4939 || (mode == SImode && TARGET_H8300S && amount >= 13))
4941 switch (mode)
4943 case HImode:
4944 /* This code works on any family. */
4945 insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
4946 output_asm_insn (insn_buf, operands);
4947 break;
4949 case SImode:
4950 /* This code works on the H8/300H and H8S. */
4951 insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
4952 output_asm_insn (insn_buf, operands);
4953 break;
4955 default:
4956 abort ();
4959 /* Adjust AMOUNT and flip the direction. */
4960 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4961 rotate_type =
4962 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4965 /* Output rotate insns. */
4966 for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
4968 if (bits == 2)
4969 insn_buf = rotate_two[rotate_type][rotate_mode];
4970 else
4971 insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
4973 for (; amount >= bits; amount -= bits)
4974 output_asm_insn (insn_buf, operands);
4977 return "";
4980 /* Compute the length of a rotate insn. */
4982 unsigned int
4983 compute_a_rotate_length (rtx *operands)
4985 rtx src = operands[1];
4986 rtx amount_rtx = operands[2];
4987 enum machine_mode mode = GET_MODE (src);
4988 int amount;
4989 unsigned int length = 0;
4991 if (GET_CODE (amount_rtx) != CONST_INT)
4992 abort ();
4994 amount = INTVAL (amount_rtx);
4996 /* Clean up AMOUNT. */
4997 if (amount < 0)
4998 amount = 0;
4999 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5000 amount = GET_MODE_BITSIZE (mode);
5002 /* Determine the faster direction. After this phase, amount
5003 will be at most a half of GET_MODE_BITSIZE (mode). */
5004 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5005 /* Flip the direction. */
5006 amount = GET_MODE_BITSIZE (mode) - amount;
5008 /* See if a byte swap (in HImode) or a word swap (in SImode) can
5009 boost up the rotation. */
5010 if ((mode == HImode && TARGET_H8300 && amount >= 5)
5011 || (mode == HImode && TARGET_H8300H && amount >= 6)
5012 || (mode == HImode && TARGET_H8300S && amount == 8)
5013 || (mode == SImode && TARGET_H8300H && amount >= 10)
5014 || (mode == SImode && TARGET_H8300S && amount >= 13))
5016 /* Adjust AMOUNT and flip the direction. */
5017 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5018 length += 6;
5021 /* We use 2-bit rotations on the H8S. */
5022 if (TARGET_H8300S)
5023 amount = amount / 2 + amount % 2;
5025 /* The H8/300 uses three insns to rotate one bit, taking 6
5026 length. */
5027 length += amount * ((TARGET_H8300 && mode == HImode) ? 6 : 2);
5029 return length;
5032 /* Fix the operands of a gen_xxx so that it could become a bit
5033 operating insn. */
5036 fix_bit_operand (rtx *operands, enum rtx_code code)
5038 /* The bit_operand predicate accepts any memory during RTL generation, but
5039 only 'U' memory afterwards, so if this is a MEM operand, we must force
5040 it to be valid for 'U' by reloading the address. */
5042 if (code == AND
5043 ? single_zero_operand (operands[2], QImode)
5044 : single_one_operand (operands[2], QImode))
5046 /* OK to have a memory dest. */
5047 if (GET_CODE (operands[0]) == MEM
5048 && !OK_FOR_U (operands[0]))
5050 rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
5051 copy_to_mode_reg (Pmode,
5052 XEXP (operands[0], 0)));
5053 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5054 operands[0] = mem;
5057 if (GET_CODE (operands[1]) == MEM
5058 && !OK_FOR_U (operands[1]))
5060 rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
5061 copy_to_mode_reg (Pmode,
5062 XEXP (operands[1], 0)));
5063 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5064 operands[1] = mem;
5066 return 0;
5069 /* Dest and src op must be register. */
5071 operands[1] = force_reg (QImode, operands[1]);
5073 rtx res = gen_reg_rtx (QImode);
5074 switch (code)
5076 case AND:
5077 emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
5078 break;
5079 case IOR:
5080 emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
5081 break;
5082 case XOR:
5083 emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
5084 break;
5085 default:
5086 abort ();
5088 emit_insn (gen_movqi (operands[0], res));
5090 return 1;
5093 /* Return nonzero if FUNC is an interrupt function as specified
5094 by the "interrupt" attribute. */
5096 static int
5097 h8300_interrupt_function_p (tree func)
5099 tree a;
5101 if (TREE_CODE (func) != FUNCTION_DECL)
5102 return 0;
5104 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
5105 return a != NULL_TREE;
5108 /* Return nonzero if FUNC is a saveall function as specified by the
5109 "saveall" attribute. */
5111 static int
5112 h8300_saveall_function_p (tree func)
5114 tree a;
5116 if (TREE_CODE (func) != FUNCTION_DECL)
5117 return 0;
5119 a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
5120 return a != NULL_TREE;
5123 /* Return nonzero if FUNC is an OS_Task function as specified
5124 by the "OS_Task" attribute. */
5126 static int
5127 h8300_os_task_function_p (tree func)
5129 tree a;
5131 if (TREE_CODE (func) != FUNCTION_DECL)
5132 return 0;
5134 a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
5135 return a != NULL_TREE;
5138 /* Return nonzero if FUNC is a monitor function as specified
5139 by the "monitor" attribute. */
5141 static int
5142 h8300_monitor_function_p (tree func)
5144 tree a;
5146 if (TREE_CODE (func) != FUNCTION_DECL)
5147 return 0;
5149 a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
5150 return a != NULL_TREE;
5153 /* Return nonzero if FUNC is a function that should be called
5154 through the function vector. */
5157 h8300_funcvec_function_p (tree func)
5159 tree a;
5161 if (TREE_CODE (func) != FUNCTION_DECL)
5162 return 0;
5164 a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
5165 return a != NULL_TREE;
5168 /* Return nonzero if DECL is a variable that's in the eight bit
5169 data area. */
5172 h8300_eightbit_data_p (tree decl)
5174 tree a;
5176 if (TREE_CODE (decl) != VAR_DECL)
5177 return 0;
5179 a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
5180 return a != NULL_TREE;
5183 /* Return nonzero if DECL is a variable that's in the tiny
5184 data area. */
5187 h8300_tiny_data_p (tree decl)
5189 tree a;
5191 if (TREE_CODE (decl) != VAR_DECL)
5192 return 0;
5194 a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
5195 return a != NULL_TREE;
5198 /* Generate an 'interrupt_handler' attribute for decls. We convert
5199 all the pragmas to corresponding attributes. */
5201 static void
5202 h8300_insert_attributes (tree node, tree *attributes)
5204 if (TREE_CODE (node) == FUNCTION_DECL)
5206 if (pragma_interrupt)
5208 pragma_interrupt = 0;
5210 /* Add an 'interrupt_handler' attribute. */
5211 *attributes = tree_cons (get_identifier ("interrupt_handler"),
5212 NULL, *attributes);
5215 if (pragma_saveall)
5217 pragma_saveall = 0;
5219 /* Add an 'saveall' attribute. */
5220 *attributes = tree_cons (get_identifier ("saveall"),
5221 NULL, *attributes);
5226 /* Supported attributes:
5228 interrupt_handler: output a prologue and epilogue suitable for an
5229 interrupt handler.
5231 saveall: output a prologue and epilogue that saves and restores
5232 all registers except the stack pointer.
5234 function_vector: This function should be called through the
5235 function vector.
5237 eightbit_data: This variable lives in the 8-bit data area and can
5238 be referenced with 8-bit absolute memory addresses.
5240 tiny_data: This variable lives in the tiny data area and can be
5241 referenced with 16-bit absolute memory references. */
5243 const struct attribute_spec h8300_attribute_table[] =
5245 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
5246 { "interrupt_handler", 0, 0, true, false, false, h8300_handle_fndecl_attribute },
5247 { "saveall", 0, 0, true, false, false, h8300_handle_fndecl_attribute },
5248 { "OS_Task", 0, 0, true, false, false, h8300_handle_fndecl_attribute },
5249 { "monitor", 0, 0, true, false, false, h8300_handle_fndecl_attribute },
5250 { "function_vector", 0, 0, true, false, false, h8300_handle_fndecl_attribute },
5251 { "eightbit_data", 0, 0, true, false, false, h8300_handle_eightbit_data_attribute },
5252 { "tiny_data", 0, 0, true, false, false, h8300_handle_tiny_data_attribute },
5253 { NULL, 0, 0, false, false, false, NULL }
5257 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
5258 struct attribute_spec.handler. */
5259 static tree
5260 h8300_handle_fndecl_attribute (tree *node, tree name,
5261 tree args ATTRIBUTE_UNUSED,
5262 int flags ATTRIBUTE_UNUSED,
5263 bool *no_add_attrs)
5265 if (TREE_CODE (*node) != FUNCTION_DECL)
5267 warning ("%qs attribute only applies to functions",
5268 IDENTIFIER_POINTER (name));
5269 *no_add_attrs = true;
5272 return NULL_TREE;
5275 /* Handle an "eightbit_data" attribute; arguments as in
5276 struct attribute_spec.handler. */
5277 static tree
5278 h8300_handle_eightbit_data_attribute (tree *node, tree name,
5279 tree args ATTRIBUTE_UNUSED,
5280 int flags ATTRIBUTE_UNUSED,
5281 bool *no_add_attrs)
5283 tree decl = *node;
5285 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5287 DECL_SECTION_NAME (decl) = build_string (7, ".eight");
5289 else
5291 warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
5292 *no_add_attrs = true;
5295 return NULL_TREE;
5298 /* Handle an "tiny_data" attribute; arguments as in
5299 struct attribute_spec.handler. */
5300 static tree
5301 h8300_handle_tiny_data_attribute (tree *node, tree name,
5302 tree args ATTRIBUTE_UNUSED,
5303 int flags ATTRIBUTE_UNUSED,
5304 bool *no_add_attrs)
5306 tree decl = *node;
5308 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5310 DECL_SECTION_NAME (decl) = build_string (6, ".tiny");
5312 else
5314 warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
5315 *no_add_attrs = true;
5318 return NULL_TREE;
5321 /* Mark function vectors, and various small data objects. */
5323 static void
5324 h8300_encode_section_info (tree decl, rtx rtl, int first)
5326 int extra_flags = 0;
5328 default_encode_section_info (decl, rtl, first);
5330 if (TREE_CODE (decl) == FUNCTION_DECL
5331 && h8300_funcvec_function_p (decl))
5332 extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
5333 else if (TREE_CODE (decl) == VAR_DECL
5334 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
5336 if (h8300_eightbit_data_p (decl))
5337 extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
5338 else if (first && h8300_tiny_data_p (decl))
5339 extra_flags = SYMBOL_FLAG_TINY_DATA;
5342 if (extra_flags)
5343 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
5346 /* Output a single-bit extraction. */
5348 const char *
5349 output_simode_bld (int bild, rtx operands[])
5351 if (TARGET_H8300)
5353 /* Clear the destination register. */
5354 output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
5356 /* Now output the bit load or bit inverse load, and store it in
5357 the destination. */
5358 if (bild)
5359 output_asm_insn ("bild\t%Z2,%Y1", operands);
5360 else
5361 output_asm_insn ("bld\t%Z2,%Y1", operands);
5363 output_asm_insn ("bst\t#0,%w0", operands);
5365 else
5367 /* Determine if we can clear the destination first. */
5368 int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5369 && REGNO (operands[0]) != REGNO (operands[1]));
5371 if (clear_first)
5372 output_asm_insn ("sub.l\t%S0,%S0", operands);
5374 /* Output the bit load or bit inverse load. */
5375 if (bild)
5376 output_asm_insn ("bild\t%Z2,%Y1", operands);
5377 else
5378 output_asm_insn ("bld\t%Z2,%Y1", operands);
5380 if (!clear_first)
5381 output_asm_insn ("xor.l\t%S0,%S0", operands);
5383 /* Perform the bit store. */
5384 output_asm_insn ("rotxl.l\t%S0", operands);
5387 /* All done. */
5388 return "";
5391 /* Delayed-branch scheduling is more effective if we have some idea
5392 how long each instruction will be. Use a shorten_branches pass
5393 to get an initial estimate. */
5395 static void
5396 h8300_reorg (void)
5398 if (flag_delayed_branch)
5399 shorten_branches (get_insns ());
5402 #ifndef OBJECT_FORMAT_ELF
5403 static void
5404 h8300_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED,
5405 tree decl)
5407 /* ??? Perhaps we should be using default_coff_asm_named_section. */
5408 fprintf (asm_out_file, "\t.section %s\n", name);
5410 #endif /* ! OBJECT_FORMAT_ELF */
5412 /* Nonzero if X is a constant address suitable as an 8-bit absolute,
5413 which is a special case of the 'R' operand. */
5416 h8300_eightbit_constant_address_p (rtx x)
5418 /* The ranges of the 8-bit area. */
5419 const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5420 const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
5421 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5422 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5423 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5424 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5426 unsigned HOST_WIDE_INT addr;
5428 /* We accept symbols declared with eightbit_data. */
5429 if (GET_CODE (x) == SYMBOL_REF)
5430 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
5432 if (GET_CODE (x) != CONST_INT)
5433 return 0;
5435 addr = INTVAL (x);
5437 return (0
5438 || ((TARGET_H8300 || TARGET_NORMAL_MODE) && IN_RANGE (addr, n1, n2))
5439 || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5440 || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5443 /* Nonzero if X is a constant address suitable as an 16-bit absolute
5444 on H8/300H and H8S. */
5447 h8300_tiny_constant_address_p (rtx x)
5449 /* The ranges of the 16-bit area. */
5450 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5451 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5452 const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5453 const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5454 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5455 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5456 const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5457 const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5459 unsigned HOST_WIDE_INT addr;
5461 switch (GET_CODE (x))
5463 case SYMBOL_REF:
5464 /* In the normal mode, any symbol fits in the 16-bit absolute
5465 address range. We also accept symbols declared with
5466 tiny_data. */
5467 return (TARGET_NORMAL_MODE
5468 || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
5470 case CONST_INT:
5471 addr = INTVAL (x);
5472 return (TARGET_NORMAL_MODE
5473 || (TARGET_H8300H
5474 && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5475 || (TARGET_H8300S
5476 && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
5478 case CONST:
5479 return TARGET_NORMAL_MODE;
5481 default:
5482 return 0;
5487 /* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5488 locations that can be accessed as a 16-bit word. */
5491 byte_accesses_mergeable_p (rtx addr1, rtx addr2)
5493 HOST_WIDE_INT offset1, offset2;
5494 rtx reg1, reg2;
5496 if (REG_P (addr1))
5498 reg1 = addr1;
5499 offset1 = 0;
5501 else if (GET_CODE (addr1) == PLUS
5502 && REG_P (XEXP (addr1, 0))
5503 && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5505 reg1 = XEXP (addr1, 0);
5506 offset1 = INTVAL (XEXP (addr1, 1));
5508 else
5509 return 0;
5511 if (REG_P (addr2))
5513 reg2 = addr2;
5514 offset2 = 0;
5516 else if (GET_CODE (addr2) == PLUS
5517 && REG_P (XEXP (addr2, 0))
5518 && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5520 reg2 = XEXP (addr2, 0);
5521 offset2 = INTVAL (XEXP (addr2, 1));
5523 else
5524 return 0;
5526 if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5527 || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5528 && offset1 % 2 == 0
5529 && offset1 + 1 == offset2)
5530 return 1;
5532 return 0;
5535 /* Return nonzero if we have the same comparison insn as I3 two insns
5536 before I3. I3 is assumed to be a comparison insn. */
5539 same_cmp_preceding_p (rtx i3)
5541 rtx i1, i2;
5543 /* Make sure we have a sequence of three insns. */
5544 i2 = prev_nonnote_insn (i3);
5545 if (i2 == NULL_RTX)
5546 return 0;
5547 i1 = prev_nonnote_insn (i2);
5548 if (i1 == NULL_RTX)
5549 return 0;
5551 return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5552 && any_condjump_p (i2) && onlyjump_p (i2));
5555 /* Return nonzero if we have the same comparison insn as I1 two insns
5556 after I1. I1 is assumed to be a comparison insn. */
5559 same_cmp_following_p (rtx i1)
5561 rtx i2, i3;
5563 /* Make sure we have a sequence of three insns. */
5564 i2 = next_nonnote_insn (i1);
5565 if (i2 == NULL_RTX)
5566 return 0;
5567 i3 = next_nonnote_insn (i2);
5568 if (i3 == NULL_RTX)
5569 return 0;
5571 return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5572 && any_condjump_p (i2) && onlyjump_p (i2));
5575 /* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
5576 (or pops) N registers. OPERANDS are assumed to be an array of
5577 registers. */
5580 h8300_regs_ok_for_stm (int n, rtx operands[])
5582 switch (n)
5584 case 2:
5585 return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5586 || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5587 || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5588 case 3:
5589 return ((REGNO (operands[0]) == 0
5590 && REGNO (operands[1]) == 1
5591 && REGNO (operands[2]) == 2)
5592 || (REGNO (operands[0]) == 4
5593 && REGNO (operands[1]) == 5
5594 && REGNO (operands[2]) == 6));
5596 case 4:
5597 return (REGNO (operands[0]) == 0
5598 && REGNO (operands[1]) == 1
5599 && REGNO (operands[2]) == 2
5600 && REGNO (operands[3]) == 3);
5603 abort ();
5606 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
5609 h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5610 unsigned int new_reg)
5612 /* Interrupt functions can only use registers that have already been
5613 saved by the prologue, even if they would normally be
5614 call-clobbered. */
5616 if (h8300_current_function_interrupt_function_p ()
5617 && !regs_ever_live[new_reg])
5618 return 0;
5620 return 1;
5623 /* Return nonzero if X is a legitimate constant. */
5626 h8300_legitimate_constant_p (rtx x ATTRIBUTE_UNUSED)
5628 return 1;
5631 /* Return nonzero if X is a REG or SUBREG suitable as a base register. */
5633 static int
5634 h8300_rtx_ok_for_base_p (rtx x, int strict)
5636 /* Strip off SUBREG if any. */
5637 if (GET_CODE (x) == SUBREG)
5638 x = SUBREG_REG (x);
5640 return (REG_P (x)
5641 && (strict
5642 ? REG_OK_FOR_BASE_STRICT_P (x)
5643 : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5646 /* Return nozero if X is a legitimate address. On the H8/300, a
5647 legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5648 CONSTANT_ADDRESS. */
5651 h8300_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
5653 /* The register indirect addresses like @er0 is always valid. */
5654 if (h8300_rtx_ok_for_base_p (x, strict))
5655 return 1;
5657 if (CONSTANT_ADDRESS_P (x))
5658 return 1;
5660 if (TARGET_H8300SX
5661 && ( GET_CODE (x) == PRE_INC
5662 || GET_CODE (x) == PRE_DEC
5663 || GET_CODE (x) == POST_INC
5664 || GET_CODE (x) == POST_DEC)
5665 && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5666 return 1;
5668 if (GET_CODE (x) == PLUS
5669 && CONSTANT_ADDRESS_P (XEXP (x, 1))
5670 && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5671 mode, 0), strict))
5672 return 1;
5674 return 0;
5677 /* Worker function for HARD_REGNO_NREGS.
5679 We pretend the MAC register is 32bits -- we don't have any data
5680 types on the H8 series to handle more than 32bits. */
5683 h8300_hard_regno_nregs (int regno ATTRIBUTE_UNUSED, enum machine_mode mode)
5685 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5688 /* Worker function for HARD_REGNO_MODE_OK. */
5691 h8300_hard_regno_mode_ok (int regno, enum machine_mode mode)
5693 if (TARGET_H8300)
5694 /* If an even reg, then anything goes. Otherwise the mode must be
5695 QI or HI. */
5696 return ((regno & 1) == 0) || (mode == HImode) || (mode == QImode);
5697 else
5698 /* MAC register can only be of SImode. Otherwise, anything
5699 goes. */
5700 return regno == MAC_REG ? mode == SImode : 1;
5703 /* Perform target dependent optabs initialization. */
5704 static void
5705 h8300_init_libfuncs (void)
5707 set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5708 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5709 set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5710 set_optab_libfunc (smod_optab, HImode, "__modhi3");
5711 set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5714 /* Worker function for TARGET_RETURN_IN_MEMORY. */
5716 static bool
5717 h8300_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
5719 return (TYPE_MODE (type) == BLKmode
5720 || GET_MODE_SIZE (TYPE_MODE (type)) > (TARGET_H8300 ? 4 : 8));
5723 /* Initialize the GCC target structure. */
5724 #undef TARGET_ATTRIBUTE_TABLE
5725 #define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
5727 #undef TARGET_ASM_ALIGNED_HI_OP
5728 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
5730 #undef TARGET_ASM_FILE_START
5731 #define TARGET_ASM_FILE_START h8300_file_start
5732 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
5733 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
5735 #undef TARGET_ASM_FILE_END
5736 #define TARGET_ASM_FILE_END h8300_file_end
5738 #undef TARGET_ENCODE_SECTION_INFO
5739 #define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
5741 #undef TARGET_INSERT_ATTRIBUTES
5742 #define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
5744 #undef TARGET_RTX_COSTS
5745 #define TARGET_RTX_COSTS h8300_rtx_costs
5747 #undef TARGET_INIT_LIBFUNCS
5748 #define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
5750 #undef TARGET_RETURN_IN_MEMORY
5751 #define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
5753 #undef TARGET_MACHINE_DEPENDENT_REORG
5754 #define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
5756 struct gcc_target targetm = TARGET_INITIALIZER;