2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / config / h8300 / h8300.c
bloba83f3def80afcc0508ec0743b6e38d2a588c2850
1 /* Subroutines for insn-output.c for Renesas H8/300.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Steve Chamberlain (sac@cygnus.com),
4 Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "varasm.h"
30 #include "calls.h"
31 #include "stringpool.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "optabs.h"
48 #include "diagnostic-core.h"
49 #include "c-family/c-pragma.h" /* ??? */
50 #include "tm_p.h"
51 #include "tm-constrs.h"
52 #include "ggc.h"
53 #include "target.h"
54 #include "target-def.h"
55 #include "df.h"
56 #include "builtins.h"
58 /* Classifies a h8300_src_operand or h8300_dst_operand.
60 H8OP_IMMEDIATE
61 A constant operand of some sort.
63 H8OP_REGISTER
64 An ordinary register.
66 H8OP_MEM_ABSOLUTE
67 A memory reference with a constant address.
69 H8OP_MEM_BASE
70 A memory reference with a register as its address.
72 H8OP_MEM_COMPLEX
73 Some other kind of memory reference. */
74 enum h8300_operand_class
76 H8OP_IMMEDIATE,
77 H8OP_REGISTER,
78 H8OP_MEM_ABSOLUTE,
79 H8OP_MEM_BASE,
80 H8OP_MEM_COMPLEX,
81 NUM_H8OPS
84 /* For a general two-operand instruction, element [X][Y] gives
85 the length of the opcode fields when the first operand has class
86 (X + 1) and the second has class Y. */
87 typedef unsigned char h8300_length_table[NUM_H8OPS - 1][NUM_H8OPS];
89 /* Forward declarations. */
90 static const char *byte_reg (rtx, int);
91 static int h8300_interrupt_function_p (tree);
92 static int h8300_saveall_function_p (tree);
93 static int h8300_monitor_function_p (tree);
94 static int h8300_os_task_function_p (tree);
95 static void h8300_emit_stack_adjustment (int, HOST_WIDE_INT, bool);
96 static HOST_WIDE_INT round_frame_size (HOST_WIDE_INT);
97 static unsigned int compute_saved_regs (void);
98 static const char *cond_string (enum rtx_code);
99 static unsigned int h8300_asm_insn_count (const char *);
100 static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
101 static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
102 static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
103 static void h8300_print_operand_address (FILE *, rtx);
104 static void h8300_print_operand (FILE *, rtx, int);
105 static bool h8300_print_operand_punct_valid_p (unsigned char code);
106 #ifndef OBJECT_FORMAT_ELF
107 static void h8300_asm_named_section (const char *, unsigned int, tree);
108 #endif
109 static int h8300_register_move_cost (enum machine_mode, reg_class_t, reg_class_t);
110 static int h8300_and_costs (rtx);
111 static int h8300_shift_costs (rtx);
112 static void h8300_push_pop (int, int, bool, bool);
113 static int h8300_stack_offset_p (rtx, int);
114 static int h8300_ldm_stm_regno (rtx, int, int, int);
115 static void h8300_reorg (void);
116 static unsigned int h8300_constant_length (rtx);
117 static unsigned int h8300_displacement_length (rtx, int);
118 static unsigned int h8300_classify_operand (rtx, int, enum h8300_operand_class *);
119 static unsigned int h8300_length_from_table (rtx, rtx, const h8300_length_table *);
120 static unsigned int h8300_unary_length (rtx);
121 static unsigned int h8300_short_immediate_length (rtx);
122 static unsigned int h8300_bitfield_length (rtx, rtx);
123 static unsigned int h8300_binary_length (rtx_insn *, const h8300_length_table *);
124 static bool h8300_short_move_mem_p (rtx, enum rtx_code);
125 static unsigned int h8300_move_length (rtx *, const h8300_length_table *);
126 static bool h8300_hard_regno_scratch_ok (unsigned int);
127 static rtx h8300_get_index (rtx, enum machine_mode mode, int *);
129 /* CPU_TYPE, says what cpu we're compiling for. */
130 int cpu_type;
132 /* True if a #pragma interrupt has been seen for the current function. */
133 static int pragma_interrupt;
135 /* True if a #pragma saveall has been seen for the current function. */
136 static int pragma_saveall;
138 static const char *const names_big[] =
139 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" };
141 static const char *const names_extended[] =
142 { "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" };
144 static const char *const names_upper_extended[] =
145 { "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
147 /* Points to one of the above. */
148 /* ??? The above could be put in an array indexed by CPU_TYPE. */
149 const char * const *h8_reg_names;
151 /* Various operations needed by the following, indexed by CPU_TYPE. */
153 const char *h8_push_op, *h8_pop_op, *h8_mov_op;
155 /* Value of MOVE_RATIO. */
156 int h8300_move_ratio;
158 /* See below where shifts are handled for explanation of this enum. */
160 enum shift_alg
162 SHIFT_INLINE,
163 SHIFT_ROT_AND,
164 SHIFT_SPECIAL,
165 SHIFT_LOOP
168 /* Symbols of the various shifts which can be used as indices. */
170 enum shift_type
172 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
175 /* Macros to keep the shift algorithm tables small. */
176 #define INL SHIFT_INLINE
177 #define ROT SHIFT_ROT_AND
178 #define LOP SHIFT_LOOP
179 #define SPC SHIFT_SPECIAL
181 /* The shift algorithms for each machine, mode, shift type, and shift
182 count are defined below. The three tables below correspond to
183 QImode, HImode, and SImode, respectively. Each table is organized
184 by, in the order of indices, machine, shift type, and shift count. */
186 static enum shift_alg shift_alg_qi[3][3][8] = {
188 /* TARGET_H8300 */
189 /* 0 1 2 3 4 5 6 7 */
190 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
191 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
192 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
195 /* TARGET_H8300H */
196 /* 0 1 2 3 4 5 6 7 */
197 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
198 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
199 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
202 /* TARGET_H8300S */
203 /* 0 1 2 3 4 5 6 7 */
204 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT */
205 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
206 { INL, INL, INL, INL, INL, INL, INL, SPC } /* SHIFT_ASHIFTRT */
210 static enum shift_alg shift_alg_hi[3][3][16] = {
212 /* TARGET_H8300 */
213 /* 0 1 2 3 4 5 6 7 */
214 /* 8 9 10 11 12 13 14 15 */
215 { INL, INL, INL, INL, INL, INL, INL, SPC,
216 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
217 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
218 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
219 { INL, INL, INL, INL, INL, LOP, LOP, SPC,
220 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
223 /* TARGET_H8300H */
224 /* 0 1 2 3 4 5 6 7 */
225 /* 8 9 10 11 12 13 14 15 */
226 { INL, INL, INL, INL, INL, INL, INL, SPC,
227 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
228 { INL, INL, INL, INL, INL, INL, INL, SPC,
229 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
230 { INL, INL, INL, INL, INL, INL, INL, SPC,
231 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
234 /* TARGET_H8300S */
235 /* 0 1 2 3 4 5 6 7 */
236 /* 8 9 10 11 12 13 14 15 */
237 { INL, INL, INL, INL, INL, INL, INL, INL,
238 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
239 { INL, INL, INL, INL, INL, INL, INL, INL,
240 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
241 { INL, INL, INL, INL, INL, INL, INL, INL,
242 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
246 static enum shift_alg shift_alg_si[3][3][32] = {
248 /* TARGET_H8300 */
249 /* 0 1 2 3 4 5 6 7 */
250 /* 8 9 10 11 12 13 14 15 */
251 /* 16 17 18 19 20 21 22 23 */
252 /* 24 25 26 27 28 29 30 31 */
253 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
254 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
255 SPC, SPC, SPC, SPC, SPC, LOP, LOP, LOP,
256 SPC, SPC, SPC, SPC, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFT */
257 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
258 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC,
259 SPC, SPC, SPC, LOP, LOP, LOP, LOP, LOP,
260 SPC, SPC, SPC, SPC, SPC, LOP, LOP, SPC }, /* SHIFT_LSHIFTRT */
261 { INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
262 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
263 SPC, SPC, LOP, LOP, LOP, LOP, LOP, LOP,
264 SPC, SPC, SPC, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
267 /* TARGET_H8300H */
268 /* 0 1 2 3 4 5 6 7 */
269 /* 8 9 10 11 12 13 14 15 */
270 /* 16 17 18 19 20 21 22 23 */
271 /* 24 25 26 27 28 29 30 31 */
272 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
273 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
274 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
275 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
276 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
277 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
278 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
279 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
280 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
281 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
282 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
283 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
286 /* TARGET_H8300S */
287 /* 0 1 2 3 4 5 6 7 */
288 /* 8 9 10 11 12 13 14 15 */
289 /* 16 17 18 19 20 21 22 23 */
290 /* 24 25 26 27 28 29 30 31 */
291 { INL, INL, INL, INL, INL, INL, INL, INL,
292 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
293 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
294 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
295 { INL, INL, INL, INL, INL, INL, INL, INL,
296 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
297 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
298 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
299 { INL, INL, INL, INL, INL, INL, INL, INL,
300 INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
301 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
302 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
306 #undef INL
307 #undef ROT
308 #undef LOP
309 #undef SPC
311 enum h8_cpu
313 H8_300,
314 H8_300H,
315 H8_S
318 /* Initialize various cpu specific globals at start up. */
320 static void
321 h8300_option_override (void)
323 static const char *const h8_push_ops[2] = { "push" , "push.l" };
324 static const char *const h8_pop_ops[2] = { "pop" , "pop.l" };
325 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
327 #ifndef OBJECT_FORMAT_ELF
328 if (TARGET_H8300SX)
330 error ("-msx is not supported in coff");
331 target_flags |= MASK_H8300S;
333 #endif
335 if (TARGET_H8300)
337 cpu_type = (int) CPU_H8300;
338 h8_reg_names = names_big;
340 else
342 /* For this we treat the H8/300H and H8S the same. */
343 cpu_type = (int) CPU_H8300H;
344 h8_reg_names = names_extended;
346 h8_push_op = h8_push_ops[cpu_type];
347 h8_pop_op = h8_pop_ops[cpu_type];
348 h8_mov_op = h8_mov_ops[cpu_type];
350 if (!TARGET_H8300S && TARGET_MAC)
352 error ("-ms2600 is used without -ms");
353 target_flags |= MASK_H8300S_1;
356 if (TARGET_H8300 && TARGET_NORMAL_MODE)
358 error ("-mn is used without -mh or -ms or -msx");
359 target_flags ^= MASK_NORMAL_MODE;
362 if (! TARGET_H8300S && TARGET_EXR)
364 error ("-mexr is used without -ms");
365 target_flags |= MASK_H8300S_1;
368 if (TARGET_H8300 && TARGET_INT32)
370 error ("-mint32 is not supported for H8300 and H8300L targets");
371 target_flags ^= MASK_INT32;
374 if ((!TARGET_H8300S && TARGET_EXR) && (!TARGET_H8300SX && TARGET_EXR))
376 error ("-mexr is used without -ms or -msx");
377 target_flags |= MASK_H8300S_1;
380 if ((!TARGET_H8300S && TARGET_NEXR) && (!TARGET_H8300SX && TARGET_NEXR))
382 warning (OPT_mno_exr, "-mno-exr valid only with -ms or -msx \
383 - Option ignored!");
386 /* Some of the shifts are optimized for speed by default.
387 See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
388 If optimizing for size, change shift_alg for those shift to
389 SHIFT_LOOP. */
390 if (optimize_size)
392 /* H8/300 */
393 shift_alg_hi[H8_300][SHIFT_ASHIFT][5] = SHIFT_LOOP;
394 shift_alg_hi[H8_300][SHIFT_ASHIFT][6] = SHIFT_LOOP;
395 shift_alg_hi[H8_300][SHIFT_ASHIFT][13] = SHIFT_LOOP;
396 shift_alg_hi[H8_300][SHIFT_ASHIFT][14] = SHIFT_LOOP;
398 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][13] = SHIFT_LOOP;
399 shift_alg_hi[H8_300][SHIFT_LSHIFTRT][14] = SHIFT_LOOP;
401 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
402 shift_alg_hi[H8_300][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
404 /* H8/300H */
405 shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
406 shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
408 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
409 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
411 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
412 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
413 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
414 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
416 /* H8S */
417 shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
420 /* Work out a value for MOVE_RATIO. */
421 if (!TARGET_H8300SX)
423 /* Memory-memory moves are quite expensive without the
424 h8sx instructions. */
425 h8300_move_ratio = 3;
427 else if (flag_omit_frame_pointer)
429 /* movmd sequences are fairly cheap when er6 isn't fixed. They can
430 sometimes be as short as two individual memory-to-memory moves,
431 but since they use all the call-saved registers, it seems better
432 to allow up to three moves here. */
433 h8300_move_ratio = 4;
435 else if (optimize_size)
437 /* In this case we don't use movmd sequences since they tend
438 to be longer than calls to memcpy(). Memory-to-memory
439 moves are cheaper than for !TARGET_H8300SX, so it makes
440 sense to have a slightly higher threshold. */
441 h8300_move_ratio = 4;
443 else
445 /* We use movmd sequences for some moves since it can be quicker
446 than calling memcpy(). The sequences will need to save and
447 restore er6 though, so bump up the cost. */
448 h8300_move_ratio = 6;
451 /* This target defaults to strict volatile bitfields. */
452 if (flag_strict_volatile_bitfields < 0 && abi_version_at_least(2))
453 flag_strict_volatile_bitfields = 1;
456 /* Return the byte register name for a register rtx X. B should be 0
457 if you want a lower byte register. B should be 1 if you want an
458 upper byte register. */
460 static const char *
461 byte_reg (rtx x, int b)
463 static const char *const names_small[] = {
464 "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
465 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
468 gcc_assert (REG_P (x));
470 return names_small[REGNO (x) * 2 + b];
473 /* REGNO must be saved/restored across calls if this macro is true. */
475 #define WORD_REG_USED(regno) \
476 (regno < SP_REG \
477 /* No need to save registers if this function will not return. */ \
478 && ! TREE_THIS_VOLATILE (current_function_decl) \
479 && (h8300_saveall_function_p (current_function_decl) \
480 /* Save any call saved register that was used. */ \
481 || (df_regs_ever_live_p (regno) && !call_used_regs[regno]) \
482 /* Save the frame pointer if it was used. */ \
483 || (regno == HARD_FRAME_POINTER_REGNUM && df_regs_ever_live_p (regno)) \
484 /* Save any register used in an interrupt handler. */ \
485 || (h8300_current_function_interrupt_function_p () \
486 && df_regs_ever_live_p (regno)) \
487 /* Save call clobbered registers in non-leaf interrupt \
488 handlers. */ \
489 || (h8300_current_function_interrupt_function_p () \
490 && call_used_regs[regno] \
491 && !crtl->is_leaf)))
493 /* We use this to wrap all emitted insns in the prologue. */
494 static rtx_insn *
495 F (rtx_insn *x, bool set_it)
497 if (set_it)
498 RTX_FRAME_RELATED_P (x) = 1;
499 return x;
502 /* Mark all the subexpressions of the PARALLEL rtx PAR as
503 frame-related. Return PAR.
505 dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
506 PARALLEL rtx other than the first if they do not have the
507 FRAME_RELATED flag set on them. */
508 static rtx
509 Fpa (rtx par)
511 int len = XVECLEN (par, 0);
512 int i;
514 for (i = 0; i < len; i++)
515 F (as_a <rtx_insn *> (XVECEXP (par, 0, i)), true);
517 return par;
520 /* Output assembly language to FILE for the operation OP with operand size
521 SIZE to adjust the stack pointer. */
523 static void
524 h8300_emit_stack_adjustment (int sign, HOST_WIDE_INT size, bool in_prologue)
526 /* If the frame size is 0, we don't have anything to do. */
527 if (size == 0)
528 return;
530 /* H8/300 cannot add/subtract a large constant with a single
531 instruction. If a temporary register is available, load the
532 constant to it and then do the addition. */
533 if (TARGET_H8300
534 && size > 4
535 && !h8300_current_function_interrupt_function_p ()
536 && !(cfun->static_chain_decl != NULL && sign < 0))
538 rtx r3 = gen_rtx_REG (Pmode, 3);
539 F (emit_insn (gen_movhi (r3, GEN_INT (sign * size))), in_prologue);
540 F (emit_insn (gen_addhi3 (stack_pointer_rtx,
541 stack_pointer_rtx, r3)), in_prologue);
543 else
545 /* The stack adjustment made here is further optimized by the
546 splitter. In case of H8/300, the splitter always splits the
547 addition emitted here to make the adjustment interrupt-safe.
548 FIXME: We don't always tag those, because we don't know what
549 the splitter will do. */
550 if (Pmode == HImode)
552 rtx_insn *x = emit_insn (gen_addhi3 (stack_pointer_rtx,
553 stack_pointer_rtx,
554 GEN_INT (sign * size)));
555 if (size < 4)
556 F (x, in_prologue);
558 else
559 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
560 stack_pointer_rtx, GEN_INT (sign * size))), in_prologue);
564 /* Round up frame size SIZE. */
566 static HOST_WIDE_INT
567 round_frame_size (HOST_WIDE_INT size)
569 return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
570 & -STACK_BOUNDARY / BITS_PER_UNIT);
573 /* Compute which registers to push/pop.
574 Return a bit vector of registers. */
576 static unsigned int
577 compute_saved_regs (void)
579 unsigned int saved_regs = 0;
580 int regno;
582 /* Construct a bit vector of registers to be pushed/popped. */
583 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
585 if (WORD_REG_USED (regno))
586 saved_regs |= 1 << regno;
589 /* Don't push/pop the frame pointer as it is treated separately. */
590 if (frame_pointer_needed)
591 saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
593 return saved_regs;
596 /* Emit an insn to push register RN. */
598 static rtx
599 push (int rn)
601 rtx reg = gen_rtx_REG (word_mode, rn);
602 rtx x;
604 if (TARGET_H8300)
605 x = gen_push_h8300 (reg);
606 else if (!TARGET_NORMAL_MODE)
607 x = gen_push_h8300hs_advanced (reg);
608 else
609 x = gen_push_h8300hs_normal (reg);
610 x = F (emit_insn (x), true);
611 add_reg_note (x, REG_INC, stack_pointer_rtx);
612 return x;
615 /* Emit an insn to pop register RN. */
617 static rtx
618 pop (int rn)
620 rtx reg = gen_rtx_REG (word_mode, rn);
621 rtx x;
623 if (TARGET_H8300)
624 x = gen_pop_h8300 (reg);
625 else if (!TARGET_NORMAL_MODE)
626 x = gen_pop_h8300hs_advanced (reg);
627 else
628 x = gen_pop_h8300hs_normal (reg);
629 x = emit_insn (x);
630 add_reg_note (x, REG_INC, stack_pointer_rtx);
631 return x;
634 /* Emit an instruction to push or pop NREGS consecutive registers
635 starting at register REGNO. POP_P selects a pop rather than a
636 push and RETURN_P is true if the instruction should return.
638 It must be possible to do the requested operation in a single
639 instruction. If NREGS == 1 && !RETURN_P, use a normal push
640 or pop insn. Otherwise emit a parallel of the form:
642 (parallel
643 [(return) ;; if RETURN_P
644 (save or restore REGNO)
645 (save or restore REGNO + 1)
647 (save or restore REGNO + NREGS - 1)
648 (set sp (plus sp (const_int adjust)))] */
650 static void
651 h8300_push_pop (int regno, int nregs, bool pop_p, bool return_p)
653 int i, j;
654 rtvec vec;
655 rtx sp, offset, x;
657 /* See whether we can use a simple push or pop. */
658 if (!return_p && nregs == 1)
660 if (pop_p)
661 pop (regno);
662 else
663 push (regno);
664 return;
667 /* We need one element for the return insn, if present, one for each
668 register, and one for stack adjustment. */
669 vec = rtvec_alloc ((return_p ? 1 : 0) + nregs + 1);
670 sp = stack_pointer_rtx;
671 i = 0;
673 /* Add the return instruction. */
674 if (return_p)
676 RTVEC_ELT (vec, i) = ret_rtx;
677 i++;
680 /* Add the register moves. */
681 for (j = 0; j < nregs; j++)
683 rtx lhs, rhs;
685 if (pop_p)
687 /* Register REGNO + NREGS - 1 is popped first. Before the
688 stack adjustment, its slot is at address @sp. */
689 lhs = gen_rtx_REG (SImode, regno + j);
690 rhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp,
691 (nregs - j - 1) * 4));
693 else
695 /* Register REGNO is pushed first and will be stored at @(-4,sp). */
696 lhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp, (j + 1) * -4));
697 rhs = gen_rtx_REG (SImode, regno + j);
699 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, lhs, rhs);
702 /* Add the stack adjustment. */
703 offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
704 RTVEC_ELT (vec, i + j) = gen_rtx_SET (VOIDmode, sp,
705 gen_rtx_PLUS (Pmode, sp, offset));
707 x = gen_rtx_PARALLEL (VOIDmode, vec);
708 if (!pop_p)
709 x = Fpa (x);
711 if (return_p)
712 emit_jump_insn (x);
713 else
714 emit_insn (x);
717 /* Return true if X has the value sp + OFFSET. */
719 static int
720 h8300_stack_offset_p (rtx x, int offset)
722 if (offset == 0)
723 return x == stack_pointer_rtx;
725 return (GET_CODE (x) == PLUS
726 && XEXP (x, 0) == stack_pointer_rtx
727 && GET_CODE (XEXP (x, 1)) == CONST_INT
728 && INTVAL (XEXP (x, 1)) == offset);
731 /* A subroutine of h8300_ldm_stm_parallel. X is one pattern in
732 something that may be an ldm or stm instruction. If it fits
733 the required template, return the register it loads or stores,
734 otherwise return -1.
736 LOAD_P is true if X should be a load, false if it should be a store.
737 NREGS is the number of registers that the whole instruction is expected
738 to load or store. INDEX is the index of the register that X should
739 load or store, relative to the lowest-numbered register. */
741 static int
742 h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
744 int regindex, memindex, offset;
746 if (load_p)
747 regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
748 else
749 memindex = 0, regindex = 1, offset = (index + 1) * -4;
751 if (GET_CODE (x) == SET
752 && GET_CODE (XEXP (x, regindex)) == REG
753 && GET_CODE (XEXP (x, memindex)) == MEM
754 && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
755 return REGNO (XEXP (x, regindex));
757 return -1;
760 /* Return true if the elements of VEC starting at FIRST describe an
761 ldm or stm instruction (LOAD_P says which). */
764 h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
766 rtx last;
767 int nregs, i, regno, adjust;
769 /* There must be a stack adjustment, a register move, and at least one
770 other operation (a return or another register move). */
771 if (GET_NUM_ELEM (vec) < 3)
772 return false;
774 /* Get the range of registers to be pushed or popped. */
775 nregs = GET_NUM_ELEM (vec) - first - 1;
776 regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
778 /* Check that the call to h8300_ldm_stm_regno succeeded and
779 that we're only dealing with GPRs. */
780 if (regno < 0 || regno + nregs > 8)
781 return false;
783 /* 2-register h8s instructions must start with an even-numbered register.
784 3- and 4-register instructions must start with er0 or er4. */
785 if (!TARGET_H8300SX)
787 if ((regno & 1) != 0)
788 return false;
789 if (nregs > 2 && (regno & 3) != 0)
790 return false;
793 /* Check the other loads or stores. */
794 for (i = 1; i < nregs; i++)
795 if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
796 != regno + i)
797 return false;
799 /* Check the stack adjustment. */
800 last = RTVEC_ELT (vec, first + nregs);
801 adjust = (load_p ? nregs : -nregs) * 4;
802 return (GET_CODE (last) == SET
803 && SET_DEST (last) == stack_pointer_rtx
804 && h8300_stack_offset_p (SET_SRC (last), adjust));
807 /* This is what the stack looks like after the prolog of
808 a function with a frame has been set up:
810 <args>
812 FP <- fp
813 <locals>
814 <saved registers> <- sp
816 This is what the stack looks like after the prolog of
817 a function which doesn't have a frame:
819 <args>
821 <locals>
822 <saved registers> <- sp
825 /* Generate RTL code for the function prologue. */
827 void
828 h8300_expand_prologue (void)
830 int regno;
831 int saved_regs;
832 int n_regs;
834 /* If the current function has the OS_Task attribute set, then
835 we have a naked prologue. */
836 if (h8300_os_task_function_p (current_function_decl))
837 return;
839 if (h8300_monitor_function_p (current_function_decl))
840 /* The monitor function act as normal functions, which means it
841 can accept parameters and return values. In addition to this,
842 interrupts are masked in prologue and return with "rte" in epilogue. */
843 emit_insn (gen_monitor_prologue ());
845 if (frame_pointer_needed)
847 /* Push fp. */
848 push (HARD_FRAME_POINTER_REGNUM);
849 F (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx), true);
852 /* Push the rest of the registers in ascending order. */
853 saved_regs = compute_saved_regs ();
854 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
856 n_regs = 1;
857 if (saved_regs & (1 << regno))
859 if (TARGET_H8300S)
861 /* See how many registers we can push at the same time. */
862 if ((!TARGET_H8300SX || (regno & 3) == 0)
863 && ((saved_regs >> regno) & 0x0f) == 0x0f)
864 n_regs = 4;
866 else if ((!TARGET_H8300SX || (regno & 3) == 0)
867 && ((saved_regs >> regno) & 0x07) == 0x07)
868 n_regs = 3;
870 else if ((!TARGET_H8300SX || (regno & 1) == 0)
871 && ((saved_regs >> regno) & 0x03) == 0x03)
872 n_regs = 2;
875 h8300_push_pop (regno, n_regs, false, false);
879 /* Leave room for locals. */
880 h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()), true);
883 /* Return nonzero if we can use "rts" for the function currently being
884 compiled. */
887 h8300_can_use_return_insn_p (void)
889 return (reload_completed
890 && !frame_pointer_needed
891 && get_frame_size () == 0
892 && compute_saved_regs () == 0);
895 /* Generate RTL code for the function epilogue. */
897 void
898 h8300_expand_epilogue (void)
900 int regno;
901 int saved_regs;
902 int n_regs;
903 HOST_WIDE_INT frame_size;
904 bool returned_p;
906 if (h8300_os_task_function_p (current_function_decl))
907 /* OS_Task epilogues are nearly naked -- they just have an
908 rts instruction. */
909 return;
911 frame_size = round_frame_size (get_frame_size ());
912 returned_p = false;
914 /* Deallocate locals. */
915 h8300_emit_stack_adjustment (1, frame_size, false);
917 /* Pop the saved registers in descending order. */
918 saved_regs = compute_saved_regs ();
919 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
921 n_regs = 1;
922 if (saved_regs & (1 << regno))
924 if (TARGET_H8300S)
926 /* See how many registers we can pop at the same time. */
927 if ((TARGET_H8300SX || (regno & 3) == 3)
928 && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
929 n_regs = 4;
931 else if ((TARGET_H8300SX || (regno & 3) == 2)
932 && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
933 n_regs = 3;
935 else if ((TARGET_H8300SX || (regno & 1) == 1)
936 && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
937 n_regs = 2;
940 /* See if this pop would be the last insn before the return.
941 If so, use rte/l or rts/l instead of pop or ldm.l. */
942 if (TARGET_H8300SX
943 && !frame_pointer_needed
944 && frame_size == 0
945 && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
946 returned_p = true;
948 h8300_push_pop (regno - n_regs + 1, n_regs, true, returned_p);
952 /* Pop frame pointer if we had one. */
953 if (frame_pointer_needed)
955 if (TARGET_H8300SX)
956 returned_p = true;
957 h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, true, returned_p);
960 if (!returned_p)
961 emit_jump_insn (ret_rtx);
964 /* Return nonzero if the current function is an interrupt
965 function. */
968 h8300_current_function_interrupt_function_p (void)
970 return (h8300_interrupt_function_p (current_function_decl));
974 h8300_current_function_monitor_function_p ()
976 return (h8300_monitor_function_p (current_function_decl));
979 /* Output assembly code for the start of the file. */
981 static void
982 h8300_file_start (void)
984 default_file_start ();
986 if (TARGET_H8300H)
987 fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
988 else if (TARGET_H8300SX)
989 fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
990 else if (TARGET_H8300S)
991 fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
994 /* Output assembly language code for the end of file. */
996 static void
997 h8300_file_end (void)
999 fputs ("\t.end\n", asm_out_file);
1002 /* Split an add of a small constant into two adds/subs insns.
1004 If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
1005 instead of adds/subs. */
1007 void
1008 split_adds_subs (enum machine_mode mode, rtx *operands)
1010 HOST_WIDE_INT val = INTVAL (operands[1]);
1011 rtx reg = operands[0];
1012 HOST_WIDE_INT sign = 1;
1013 HOST_WIDE_INT amount;
1014 rtx (*gen_add) (rtx, rtx, rtx);
1016 /* Force VAL to be positive so that we do not have to consider the
1017 sign. */
1018 if (val < 0)
1020 val = -val;
1021 sign = -1;
1024 switch (mode)
1026 case HImode:
1027 gen_add = gen_addhi3;
1028 break;
1030 case SImode:
1031 gen_add = gen_addsi3;
1032 break;
1034 default:
1035 gcc_unreachable ();
1038 /* Try different amounts in descending order. */
1039 for (amount = (TARGET_H8300H || TARGET_H8300S) ? 4 : 2;
1040 amount > 0;
1041 amount /= 2)
1043 for (; val >= amount; val -= amount)
1044 emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
1047 return;
1050 /* Handle machine specific pragmas for compatibility with existing
1051 compilers for the H8/300.
1053 pragma saveall generates prologue/epilogue code which saves and
1054 restores all the registers on function entry.
1056 pragma interrupt saves and restores all registers, and exits with
1057 an rte instruction rather than an rts. A pointer to a function
1058 with this attribute may be safely used in an interrupt vector. */
1060 void
1061 h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1063 pragma_interrupt = 1;
1066 void
1067 h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
1069 pragma_saveall = 1;
1072 /* If the next function argument with MODE and TYPE is to be passed in
1073 a register, return a reg RTX for the hard register in which to pass
1074 the argument. CUM represents the state after the last argument.
1075 If the argument is to be pushed, NULL_RTX is returned.
1077 On the H8/300 all normal args are pushed, unless -mquickcall in which
1078 case the first 3 arguments are passed in registers. */
1080 static rtx
1081 h8300_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
1082 const_tree type, bool named)
1084 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1086 static const char *const hand_list[] = {
1087 "__main",
1088 "__cmpsi2",
1089 "__divhi3",
1090 "__modhi3",
1091 "__udivhi3",
1092 "__umodhi3",
1093 "__divsi3",
1094 "__modsi3",
1095 "__udivsi3",
1096 "__umodsi3",
1097 "__mulhi3",
1098 "__mulsi3",
1099 "__reg_memcpy",
1100 "__reg_memset",
1101 "__ucmpsi2",
1105 rtx result = NULL_RTX;
1106 const char *fname;
1107 int regpass = 0;
1109 /* Never pass unnamed arguments in registers. */
1110 if (!named)
1111 return NULL_RTX;
1113 /* Pass 3 regs worth of data in regs when user asked on the command line. */
1114 if (TARGET_QUICKCALL)
1115 regpass = 3;
1117 /* If calling hand written assembler, use 4 regs of args. */
1118 if (cum->libcall)
1120 const char * const *p;
1122 fname = XSTR (cum->libcall, 0);
1124 /* See if this libcall is one of the hand coded ones. */
1125 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1128 if (*p)
1129 regpass = 4;
1132 if (regpass)
1134 int size;
1136 if (mode == BLKmode)
1137 size = int_size_in_bytes (type);
1138 else
1139 size = GET_MODE_SIZE (mode);
1141 if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1142 && cum->nbytes / UNITS_PER_WORD <= 3)
1143 result = gen_rtx_REG (mode, cum->nbytes / UNITS_PER_WORD);
1146 return result;
1149 /* Update the data in CUM to advance over an argument
1150 of mode MODE and data type TYPE.
1151 (TYPE is null for libcalls where that information may not be available.) */
1153 static void
1154 h8300_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
1155 const_tree type, bool named ATTRIBUTE_UNUSED)
1157 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1159 cum->nbytes += (mode != BLKmode
1160 ? (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD
1161 : (int_size_in_bytes (type) + UNITS_PER_WORD - 1) & -UNITS_PER_WORD);
1165 /* Implements TARGET_REGISTER_MOVE_COST.
1167 Any SI register-to-register move may need to be reloaded,
1168 so inmplement h8300_register_move_cost to return > 2 so that reload never
1169 shortcuts. */
1171 static int
1172 h8300_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
1173 reg_class_t from, reg_class_t to)
1175 if (from == MAC_REGS || to == MAC_REG)
1176 return 6;
1177 else
1178 return 3;
1181 /* Compute the cost of an and insn. */
1183 static int
1184 h8300_and_costs (rtx x)
1186 rtx operands[4];
1188 if (GET_MODE (x) == QImode)
1189 return 1;
1191 if (GET_MODE (x) != HImode
1192 && GET_MODE (x) != SImode)
1193 return 100;
1195 operands[0] = NULL;
1196 operands[1] = XEXP (x, 0);
1197 operands[2] = XEXP (x, 1);
1198 operands[3] = x;
1199 return compute_logical_op_length (GET_MODE (x), operands) / 2;
1202 /* Compute the cost of a shift insn. */
1204 static int
1205 h8300_shift_costs (rtx x)
1207 rtx operands[4];
1209 if (GET_MODE (x) != QImode
1210 && GET_MODE (x) != HImode
1211 && GET_MODE (x) != SImode)
1212 return 100;
1214 operands[0] = NULL;
1215 operands[1] = NULL;
1216 operands[2] = XEXP (x, 1);
1217 operands[3] = x;
1218 return compute_a_shift_length (NULL, operands) / 2;
1221 /* Worker function for TARGET_RTX_COSTS. */
1223 static bool
1224 h8300_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
1225 int *total, bool speed)
1227 if (TARGET_H8300SX && outer_code == MEM)
1229 /* Estimate the number of execution states needed to calculate
1230 the address. */
1231 if (register_operand (x, VOIDmode)
1232 || GET_CODE (x) == POST_INC
1233 || GET_CODE (x) == POST_DEC
1234 || CONSTANT_P (x))
1235 *total = 0;
1236 else
1237 *total = COSTS_N_INSNS (1);
1238 return true;
1241 switch (code)
1243 case CONST_INT:
1245 HOST_WIDE_INT n = INTVAL (x);
1247 if (TARGET_H8300SX)
1249 /* Constant operands need the same number of processor
1250 states as register operands. Although we could try to
1251 use a size-based cost for !speed, the lack of
1252 of a mode makes the results very unpredictable. */
1253 *total = 0;
1254 return true;
1256 if (-4 <= n && n <= 4)
1258 switch ((int) n)
1260 case 0:
1261 *total = 0;
1262 return true;
1263 case 1:
1264 case 2:
1265 case -1:
1266 case -2:
1267 *total = 0 + (outer_code == SET);
1268 return true;
1269 case 4:
1270 case -4:
1271 if (TARGET_H8300H || TARGET_H8300S)
1272 *total = 0 + (outer_code == SET);
1273 else
1274 *total = 1;
1275 return true;
1278 *total = 1;
1279 return true;
1282 case CONST:
1283 case LABEL_REF:
1284 case SYMBOL_REF:
1285 if (TARGET_H8300SX)
1287 /* See comment for CONST_INT. */
1288 *total = 0;
1289 return true;
1291 *total = 3;
1292 return true;
1294 case CONST_DOUBLE:
1295 *total = 20;
1296 return true;
1298 case COMPARE:
1299 if (XEXP (x, 1) == const0_rtx)
1300 *total = 0;
1301 return false;
1303 case AND:
1304 if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1305 || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1306 return false;
1307 *total = COSTS_N_INSNS (h8300_and_costs (x));
1308 return true;
1310 /* We say that MOD and DIV are so expensive because otherwise we'll
1311 generate some really horrible code for division of a power of two. */
1312 case MOD:
1313 case DIV:
1314 case UMOD:
1315 case UDIV:
1316 if (TARGET_H8300SX)
1317 switch (GET_MODE (x))
1319 case QImode:
1320 case HImode:
1321 *total = COSTS_N_INSNS (!speed ? 4 : 10);
1322 return false;
1324 case SImode:
1325 *total = COSTS_N_INSNS (!speed ? 4 : 18);
1326 return false;
1328 default:
1329 break;
1331 *total = COSTS_N_INSNS (12);
1332 return true;
1334 case MULT:
1335 if (TARGET_H8300SX)
1336 switch (GET_MODE (x))
1338 case QImode:
1339 case HImode:
1340 *total = COSTS_N_INSNS (2);
1341 return false;
1343 case SImode:
1344 *total = COSTS_N_INSNS (5);
1345 return false;
1347 default:
1348 break;
1350 *total = COSTS_N_INSNS (4);
1351 return true;
1353 case ASHIFT:
1354 case ASHIFTRT:
1355 case LSHIFTRT:
1356 if (h8sx_binary_shift_operator (x, VOIDmode))
1358 *total = COSTS_N_INSNS (2);
1359 return false;
1361 else if (h8sx_unary_shift_operator (x, VOIDmode))
1363 *total = COSTS_N_INSNS (1);
1364 return false;
1366 *total = COSTS_N_INSNS (h8300_shift_costs (x));
1367 return true;
1369 case ROTATE:
1370 case ROTATERT:
1371 if (GET_MODE (x) == HImode)
1372 *total = 2;
1373 else
1374 *total = 8;
1375 return true;
1377 default:
1378 *total = COSTS_N_INSNS (1);
1379 return false;
1383 /* Documentation for the machine specific operand escapes:
1385 'E' like s but negative.
1386 'F' like t but negative.
1387 'G' constant just the negative
1388 'R' print operand as a byte:8 address if appropriate, else fall back to
1389 'X' handling.
1390 'S' print operand as a long word
1391 'T' print operand as a word
1392 'V' find the set bit, and print its number.
1393 'W' find the clear bit, and print its number.
1394 'X' print operand as a byte
1395 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
1396 If this operand isn't a register, fall back to 'R' handling.
1397 'Z' print int & 7.
1398 'c' print the opcode corresponding to rtl
1399 'e' first word of 32-bit value - if reg, then least reg. if mem
1400 then least. if const then most sig word
1401 'f' second word of 32-bit value - if reg, then biggest reg. if mem
1402 then +2. if const then least sig word
1403 'j' print operand as condition code.
1404 'k' print operand as reverse condition code.
1405 'm' convert an integer operand to a size suffix (.b, .w or .l)
1406 'o' print an integer without a leading '#'
1407 's' print as low byte of 16-bit value
1408 't' print as high byte of 16-bit value
1409 'w' print as low byte of 32-bit value
1410 'x' print as 2nd byte of 32-bit value
1411 'y' print as 3rd byte of 32-bit value
1412 'z' print as msb of 32-bit value
1415 /* Return assembly language string which identifies a comparison type. */
1417 static const char *
1418 cond_string (enum rtx_code code)
1420 switch (code)
1422 case NE:
1423 return "ne";
1424 case EQ:
1425 return "eq";
1426 case GE:
1427 return "ge";
1428 case GT:
1429 return "gt";
1430 case LE:
1431 return "le";
1432 case LT:
1433 return "lt";
1434 case GEU:
1435 return "hs";
1436 case GTU:
1437 return "hi";
1438 case LEU:
1439 return "ls";
1440 case LTU:
1441 return "lo";
1442 default:
1443 gcc_unreachable ();
1447 /* Print operand X using operand code CODE to assembly language output file
1448 FILE. */
1450 static void
1451 h8300_print_operand (FILE *file, rtx x, int code)
1453 /* This is used for communication between codes V,W,Z and Y. */
1454 static int bitint;
1456 switch (code)
1458 case 'C':
1459 if (h8300_constant_length (x) == 2)
1460 fprintf (file, ":16");
1461 else
1462 fprintf (file, ":32");
1463 return;
1464 case 'E':
1465 switch (GET_CODE (x))
1467 case REG:
1468 fprintf (file, "%sl", names_big[REGNO (x)]);
1469 break;
1470 case CONST_INT:
1471 fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
1472 break;
1473 default:
1474 gcc_unreachable ();
1476 break;
1477 case 'F':
1478 switch (GET_CODE (x))
1480 case REG:
1481 fprintf (file, "%sh", names_big[REGNO (x)]);
1482 break;
1483 case CONST_INT:
1484 fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
1485 break;
1486 default:
1487 gcc_unreachable ();
1489 break;
1490 case 'G':
1491 gcc_assert (GET_CODE (x) == CONST_INT);
1492 fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
1493 break;
1494 case 'S':
1495 if (GET_CODE (x) == REG)
1496 fprintf (file, "%s", names_extended[REGNO (x)]);
1497 else
1498 goto def;
1499 break;
1500 case 'T':
1501 if (GET_CODE (x) == REG)
1502 fprintf (file, "%s", names_big[REGNO (x)]);
1503 else
1504 goto def;
1505 break;
1506 case 'V':
1507 bitint = (INTVAL (x) & 0xffff);
1508 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1)
1509 bitint = exact_log2 (bitint & 0xff);
1510 else
1511 bitint = exact_log2 ((bitint >> 8) & 0xff);
1512 gcc_assert (bitint >= 0);
1513 fprintf (file, "#%d", bitint);
1514 break;
1515 case 'W':
1516 bitint = ((~INTVAL (x)) & 0xffff);
1517 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1 )
1518 bitint = exact_log2 (bitint & 0xff);
1519 else
1520 bitint = (exact_log2 ((bitint >> 8) & 0xff));
1521 gcc_assert (bitint >= 0);
1522 fprintf (file, "#%d", bitint);
1523 break;
1524 case 'R':
1525 case 'X':
1526 if (GET_CODE (x) == REG)
1527 fprintf (file, "%s", byte_reg (x, 0));
1528 else
1529 goto def;
1530 break;
1531 case 'Y':
1532 gcc_assert (bitint >= 0);
1533 if (GET_CODE (x) == REG)
1534 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1535 else
1536 h8300_print_operand (file, x, 'R');
1537 bitint = -1;
1538 break;
1539 case 'Z':
1540 bitint = INTVAL (x);
1541 fprintf (file, "#%d", bitint & 7);
1542 break;
1543 case 'c':
1544 switch (GET_CODE (x))
1546 case IOR:
1547 fprintf (file, "or");
1548 break;
1549 case XOR:
1550 fprintf (file, "xor");
1551 break;
1552 case AND:
1553 fprintf (file, "and");
1554 break;
1555 default:
1556 break;
1558 break;
1559 case 'e':
1560 switch (GET_CODE (x))
1562 case REG:
1563 if (TARGET_H8300)
1564 fprintf (file, "%s", names_big[REGNO (x)]);
1565 else
1566 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
1567 break;
1568 case MEM:
1569 h8300_print_operand (file, x, 0);
1570 break;
1571 case CONST_INT:
1572 fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
1573 break;
1574 case CONST_DOUBLE:
1576 long val;
1577 REAL_VALUE_TYPE rv;
1578 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1579 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1580 fprintf (file, "#%ld", ((val >> 16) & 0xffff));
1581 break;
1583 default:
1584 gcc_unreachable ();
1585 break;
1587 break;
1588 case 'f':
1589 switch (GET_CODE (x))
1591 case REG:
1592 if (TARGET_H8300)
1593 fprintf (file, "%s", names_big[REGNO (x) + 1]);
1594 else
1595 fprintf (file, "%s", names_big[REGNO (x)]);
1596 break;
1597 case MEM:
1598 x = adjust_address (x, HImode, 2);
1599 h8300_print_operand (file, x, 0);
1600 break;
1601 case CONST_INT:
1602 fprintf (file, "#%ld", INTVAL (x) & 0xffff);
1603 break;
1604 case CONST_DOUBLE:
1606 long val;
1607 REAL_VALUE_TYPE rv;
1608 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1609 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1610 fprintf (file, "#%ld", (val & 0xffff));
1611 break;
1613 default:
1614 gcc_unreachable ();
1616 break;
1617 case 'j':
1618 fputs (cond_string (GET_CODE (x)), file);
1619 break;
1620 case 'k':
1621 fputs (cond_string (reverse_condition (GET_CODE (x))), file);
1622 break;
1623 case 'm':
1624 gcc_assert (GET_CODE (x) == CONST_INT);
1625 switch (INTVAL (x))
1627 case 1:
1628 fputs (".b", file);
1629 break;
1631 case 2:
1632 fputs (".w", file);
1633 break;
1635 case 4:
1636 fputs (".l", file);
1637 break;
1639 default:
1640 gcc_unreachable ();
1642 break;
1643 case 'o':
1644 h8300_print_operand_address (file, x);
1645 break;
1646 case 's':
1647 if (GET_CODE (x) == CONST_INT)
1648 fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
1649 else
1650 fprintf (file, "%s", byte_reg (x, 0));
1651 break;
1652 case 't':
1653 if (GET_CODE (x) == CONST_INT)
1654 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1655 else
1656 fprintf (file, "%s", byte_reg (x, 1));
1657 break;
1658 case 'w':
1659 if (GET_CODE (x) == CONST_INT)
1660 fprintf (file, "#%ld", INTVAL (x) & 0xff);
1661 else
1662 fprintf (file, "%s",
1663 byte_reg (x, TARGET_H8300 ? 2 : 0));
1664 break;
1665 case 'x':
1666 if (GET_CODE (x) == CONST_INT)
1667 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
1668 else
1669 fprintf (file, "%s",
1670 byte_reg (x, TARGET_H8300 ? 3 : 1));
1671 break;
1672 case 'y':
1673 if (GET_CODE (x) == CONST_INT)
1674 fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
1675 else
1676 fprintf (file, "%s", byte_reg (x, 0));
1677 break;
1678 case 'z':
1679 if (GET_CODE (x) == CONST_INT)
1680 fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
1681 else
1682 fprintf (file, "%s", byte_reg (x, 1));
1683 break;
1685 default:
1686 def:
1687 switch (GET_CODE (x))
1689 case REG:
1690 switch (GET_MODE (x))
1692 case QImode:
1693 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1694 fprintf (file, "%s", byte_reg (x, 0));
1695 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1696 fprintf (file, "%s", names_big[REGNO (x)]);
1697 #endif
1698 break;
1699 case HImode:
1700 fprintf (file, "%s", names_big[REGNO (x)]);
1701 break;
1702 case SImode:
1703 case SFmode:
1704 fprintf (file, "%s", names_extended[REGNO (x)]);
1705 break;
1706 default:
1707 gcc_unreachable ();
1709 break;
1711 case MEM:
1713 rtx addr = XEXP (x, 0);
1715 fprintf (file, "@");
1716 output_address (addr);
1718 /* Add a length suffix to constant addresses. Although this
1719 is often unnecessary, it helps to avoid ambiguity in the
1720 syntax of mova. If we wrote an insn like:
1722 mova/w.l @(1,@foo.b),er0
1724 then .b would be considered part of the symbol name.
1725 Adding a length after foo will avoid this. */
1726 if (CONSTANT_P (addr))
1727 switch (code)
1729 case 'R':
1730 /* Used for mov.b and bit operations. */
1731 if (h8300_eightbit_constant_address_p (addr))
1733 fprintf (file, ":8");
1734 break;
1737 /* Fall through. We should not get here if we are
1738 processing bit operations on H8/300 or H8/300H
1739 because 'U' constraint does not allow bit
1740 operations on the tiny area on these machines. */
1742 case 'X':
1743 case 'T':
1744 case 'S':
1745 if (h8300_constant_length (addr) == 2)
1746 fprintf (file, ":16");
1747 else
1748 fprintf (file, ":32");
1749 break;
1750 default:
1751 break;
1754 break;
1756 case CONST_INT:
1757 case SYMBOL_REF:
1758 case CONST:
1759 case LABEL_REF:
1760 fprintf (file, "#");
1761 h8300_print_operand_address (file, x);
1762 break;
1763 case CONST_DOUBLE:
1765 long val;
1766 REAL_VALUE_TYPE rv;
1767 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
1768 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
1769 fprintf (file, "#%ld", val);
1770 break;
1772 default:
1773 break;
1778 /* Implements TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
1780 static bool
1781 h8300_print_operand_punct_valid_p (unsigned char code)
1783 return (code == '#');
1786 /* Output assembly language output for the address ADDR to FILE. */
1788 static void
1789 h8300_print_operand_address (FILE *file, rtx addr)
1791 rtx index;
1792 int size;
1794 switch (GET_CODE (addr))
1796 case REG:
1797 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1798 break;
1800 case PRE_DEC:
1801 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1802 break;
1804 case POST_INC:
1805 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1806 break;
1808 case PRE_INC:
1809 fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1810 break;
1812 case POST_DEC:
1813 fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1814 break;
1816 case PLUS:
1817 fprintf (file, "(");
1819 index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1820 if (GET_CODE (index) == REG)
1822 /* reg,foo */
1823 h8300_print_operand_address (file, XEXP (addr, 1));
1824 fprintf (file, ",");
1825 switch (size)
1827 case 0:
1828 h8300_print_operand_address (file, index);
1829 break;
1831 case 1:
1832 h8300_print_operand (file, index, 'X');
1833 fputs (".b", file);
1834 break;
1836 case 2:
1837 h8300_print_operand (file, index, 'T');
1838 fputs (".w", file);
1839 break;
1841 case 4:
1842 h8300_print_operand (file, index, 'S');
1843 fputs (".l", file);
1844 break;
1846 /* h8300_print_operand_address (file, XEXP (addr, 0)); */
1848 else
1850 /* foo+k */
1851 h8300_print_operand_address (file, XEXP (addr, 0));
1852 fprintf (file, "+");
1853 h8300_print_operand_address (file, XEXP (addr, 1));
1855 fprintf (file, ")");
1856 break;
1858 case CONST_INT:
1860 /* Since the H8/300 only has 16-bit pointers, negative values are also
1861 those >= 32768. This happens for example with pointer minus a
1862 constant. We don't want to turn (char *p - 2) into
1863 (char *p + 65534) because loop unrolling can build upon this
1864 (IE: char *p + 131068). */
1865 int n = INTVAL (addr);
1866 if (TARGET_H8300)
1867 n = (int) (short) n;
1868 fprintf (file, "%d", n);
1869 break;
1872 default:
1873 output_addr_const (file, addr);
1874 break;
1878 /* Output all insn addresses and their sizes into the assembly language
1879 output file. This is helpful for debugging whether the length attributes
1880 in the md file are correct. This is not meant to be a user selectable
1881 option. */
1883 void
1884 final_prescan_insn (rtx_insn *insn, rtx *operand ATTRIBUTE_UNUSED,
1885 int num_operands ATTRIBUTE_UNUSED)
1887 /* This holds the last insn address. */
1888 static int last_insn_address = 0;
1890 const int uid = INSN_UID (insn);
1892 if (TARGET_ADDRESSES)
1894 fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1895 INSN_ADDRESSES (uid) - last_insn_address);
1896 last_insn_address = INSN_ADDRESSES (uid);
1900 /* Prepare for an SI sized move. */
1903 h8300_expand_movsi (rtx operands[])
1905 rtx src = operands[1];
1906 rtx dst = operands[0];
1907 if (!reload_in_progress && !reload_completed)
1909 if (!register_operand (dst, GET_MODE (dst)))
1911 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1912 emit_move_insn (tmp, src);
1913 operands[1] = tmp;
1916 return 0;
1919 /* Given FROM and TO register numbers, say whether this elimination is allowed.
1920 Frame pointer elimination is automatically handled.
1922 For the h8300, if frame pointer elimination is being done, we would like to
1923 convert ap and rp into sp, not fp.
1925 All other eliminations are valid. */
1927 static bool
1928 h8300_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1930 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
1933 /* Conditionally modify register usage based on target flags. */
1935 static void
1936 h8300_conditional_register_usage (void)
1938 if (!TARGET_MAC)
1939 fixed_regs[MAC_REG] = call_used_regs[MAC_REG] = 1;
1942 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1943 Define the offset between two registers, one to be eliminated, and
1944 the other its replacement, at the start of a routine. */
1947 h8300_initial_elimination_offset (int from, int to)
1949 /* The number of bytes that the return address takes on the stack. */
1950 int pc_size = POINTER_SIZE / BITS_PER_UNIT;
1952 /* The number of bytes that the saved frame pointer takes on the stack. */
1953 int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1955 /* The number of bytes that the saved registers, excluding the frame
1956 pointer, take on the stack. */
1957 int saved_regs_size = 0;
1959 /* The number of bytes that the locals takes on the stack. */
1960 int frame_size = round_frame_size (get_frame_size ());
1962 int regno;
1964 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1965 if (WORD_REG_USED (regno))
1966 saved_regs_size += UNITS_PER_WORD;
1968 /* Adjust saved_regs_size because the above loop took the frame
1969 pointer int account. */
1970 saved_regs_size -= fp_size;
1972 switch (to)
1974 case HARD_FRAME_POINTER_REGNUM:
1975 switch (from)
1977 case ARG_POINTER_REGNUM:
1978 return pc_size + fp_size;
1979 case RETURN_ADDRESS_POINTER_REGNUM:
1980 return fp_size;
1981 case FRAME_POINTER_REGNUM:
1982 return -saved_regs_size;
1983 default:
1984 gcc_unreachable ();
1986 break;
1987 case STACK_POINTER_REGNUM:
1988 switch (from)
1990 case ARG_POINTER_REGNUM:
1991 return pc_size + saved_regs_size + frame_size;
1992 case RETURN_ADDRESS_POINTER_REGNUM:
1993 return saved_regs_size + frame_size;
1994 case FRAME_POINTER_REGNUM:
1995 return frame_size;
1996 default:
1997 gcc_unreachable ();
1999 break;
2000 default:
2001 gcc_unreachable ();
2003 gcc_unreachable ();
2006 /* Worker function for RETURN_ADDR_RTX. */
2009 h8300_return_addr_rtx (int count, rtx frame)
2011 rtx ret;
2013 if (count == 0)
2014 ret = gen_rtx_MEM (Pmode,
2015 gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
2016 else if (flag_omit_frame_pointer)
2017 return (rtx) 0;
2018 else
2019 ret = gen_rtx_MEM (Pmode,
2020 memory_address (Pmode,
2021 plus_constant (Pmode, frame,
2022 UNITS_PER_WORD)));
2023 set_mem_alias_set (ret, get_frame_alias_set ());
2024 return ret;
2027 /* Update the condition code from the insn. */
2029 void
2030 notice_update_cc (rtx body, rtx_insn *insn)
2032 rtx set;
2034 switch (get_attr_cc (insn))
2036 case CC_NONE:
2037 /* Insn does not affect CC at all. */
2038 break;
2040 case CC_NONE_0HIT:
2041 /* Insn does not change CC, but the 0'th operand has been changed. */
2042 if (cc_status.value1 != 0
2043 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value1))
2044 cc_status.value1 = 0;
2045 if (cc_status.value2 != 0
2046 && reg_overlap_mentioned_p (recog_data.operand[0], cc_status.value2))
2047 cc_status.value2 = 0;
2048 break;
2050 case CC_SET_ZN:
2051 /* Insn sets the Z,N flags of CC to recog_data.operand[0].
2052 The V flag is unusable. The C flag may or may not be known but
2053 that's ok because alter_cond will change tests to use EQ/NE. */
2054 CC_STATUS_INIT;
2055 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
2056 set = single_set (insn);
2057 cc_status.value1 = SET_SRC (set);
2058 if (SET_DEST (set) != cc0_rtx)
2059 cc_status.value2 = SET_DEST (set);
2060 break;
2062 case CC_SET_ZNV:
2063 /* Insn sets the Z,N,V flags of CC to recog_data.operand[0].
2064 The C flag may or may not be known but that's ok because
2065 alter_cond will change tests to use EQ/NE. */
2066 CC_STATUS_INIT;
2067 cc_status.flags |= CC_NO_CARRY;
2068 set = single_set (insn);
2069 cc_status.value1 = SET_SRC (set);
2070 if (SET_DEST (set) != cc0_rtx)
2072 /* If the destination is STRICT_LOW_PART, strip off
2073 STRICT_LOW_PART. */
2074 if (GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
2075 cc_status.value2 = XEXP (SET_DEST (set), 0);
2076 else
2077 cc_status.value2 = SET_DEST (set);
2079 break;
2081 case CC_COMPARE:
2082 /* The insn is a compare instruction. */
2083 CC_STATUS_INIT;
2084 cc_status.value1 = SET_SRC (body);
2085 break;
2087 case CC_CLOBBER:
2088 /* Insn doesn't leave CC in a usable state. */
2089 CC_STATUS_INIT;
2090 break;
2094 /* Given that X occurs in an address of the form (plus X constant),
2095 return the part of X that is expected to be a register. There are
2096 four kinds of addressing mode to recognize:
2098 @(dd,Rn)
2099 @(dd,RnL.b)
2100 @(dd,Rn.w)
2101 @(dd,ERn.l)
2103 If SIZE is nonnull, and the address is one of the last three forms,
2104 set *SIZE to the index multiplication factor. Set it to 0 for
2105 plain @(dd,Rn) addresses.
2107 MODE is the mode of the value being accessed. It can be VOIDmode
2108 if the address is known to be valid, but its mode is unknown. */
2110 static rtx
2111 h8300_get_index (rtx x, enum machine_mode mode, int *size)
2113 int dummy, factor;
2115 if (size == 0)
2116 size = &dummy;
2118 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
2119 if (TARGET_H8300SX
2120 && factor <= 4
2121 && (mode == VOIDmode
2122 || GET_MODE_CLASS (mode) == MODE_INT
2123 || GET_MODE_CLASS (mode) == MODE_FLOAT))
2125 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
2127 /* When accessing byte-sized values, the index can be
2128 a zero-extended QImode or HImode register. */
2129 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
2130 return XEXP (x, 0);
2132 else
2134 /* We're looking for addresses of the form:
2136 (mult X I)
2137 or (mult (zero_extend X) I)
2139 where I is the size of the operand being accessed.
2140 The canonical form of the second expression is:
2142 (and (mult (subreg X) I) J)
2144 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2145 rtx index;
2147 if (GET_CODE (x) == AND
2148 && GET_CODE (XEXP (x, 1)) == CONST_INT
2149 && (factor == 0
2150 || INTVAL (XEXP (x, 1)) == 0xff * factor
2151 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2153 index = XEXP (x, 0);
2154 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2156 else
2158 index = x;
2159 *size = 4;
2162 if (GET_CODE (index) == MULT
2163 && GET_CODE (XEXP (index, 1)) == CONST_INT
2164 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2165 return XEXP (index, 0);
2168 *size = 0;
2169 return x;
2172 /* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
2174 On the H8/300, the predecrement and postincrement address depend thus
2175 (the amount of decrement or increment being the length of the operand). */
2177 static bool
2178 h8300_mode_dependent_address_p (const_rtx addr,
2179 addr_space_t as ATTRIBUTE_UNUSED)
2181 if (GET_CODE (addr) == PLUS
2182 && h8300_get_index (XEXP (addr, 0), VOIDmode, 0) != XEXP (addr, 0))
2183 return true;
2185 return false;
2188 static const h8300_length_table addb_length_table =
2190 /* #xx Rs @aa @Rs @xx */
2191 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2192 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2193 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2194 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2197 static const h8300_length_table addw_length_table =
2199 /* #xx Rs @aa @Rs @xx */
2200 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2201 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2202 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2203 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2206 static const h8300_length_table addl_length_table =
2208 /* #xx Rs @aa @Rs @xx */
2209 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2210 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2211 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2212 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2215 #define logicb_length_table addb_length_table
2216 #define logicw_length_table addw_length_table
2218 static const h8300_length_table logicl_length_table =
2220 /* #xx Rs @aa @Rs @xx */
2221 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2222 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2223 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2224 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2227 static const h8300_length_table movb_length_table =
2229 /* #xx Rs @aa @Rs @xx */
2230 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2231 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2232 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2233 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2236 #define movw_length_table movb_length_table
2238 static const h8300_length_table movl_length_table =
2240 /* #xx Rs @aa @Rs @xx */
2241 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2242 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2243 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2244 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2247 /* Return the size of the given address or displacement constant. */
2249 static unsigned int
2250 h8300_constant_length (rtx constant)
2252 /* Check for (@d:16,Reg). */
2253 if (GET_CODE (constant) == CONST_INT
2254 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2255 return 2;
2257 /* Check for (@d:16,Reg) in cases where the displacement is
2258 an absolute address. */
2259 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2260 return 2;
2262 return 4;
2265 /* Return the size of a displacement field in address ADDR, which should
2266 have the form (plus X constant). SIZE is the number of bytes being
2267 accessed. */
2269 static unsigned int
2270 h8300_displacement_length (rtx addr, int size)
2272 rtx offset;
2274 offset = XEXP (addr, 1);
2276 /* Check for @(d:2,Reg). */
2277 if (register_operand (XEXP (addr, 0), VOIDmode)
2278 && GET_CODE (offset) == CONST_INT
2279 && (INTVAL (offset) == size
2280 || INTVAL (offset) == size * 2
2281 || INTVAL (offset) == size * 3))
2282 return 0;
2284 return h8300_constant_length (offset);
2287 /* Store the class of operand OP in *OPCLASS and return the length of any
2288 extra operand fields. SIZE is the number of bytes in OP. OPCLASS
2289 can be null if only the length is needed. */
2291 static unsigned int
2292 h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
2294 enum h8300_operand_class dummy;
2296 if (opclass == 0)
2297 opclass = &dummy;
2299 if (CONSTANT_P (op))
2301 *opclass = H8OP_IMMEDIATE;
2303 /* Byte-sized immediates are stored in the opcode fields. */
2304 if (size == 1)
2305 return 0;
2307 /* If this is a 32-bit instruction, see whether the constant
2308 will fit into a 16-bit immediate field. */
2309 if (TARGET_H8300SX
2310 && size == 4
2311 && GET_CODE (op) == CONST_INT
2312 && IN_RANGE (INTVAL (op), 0, 0xffff))
2313 return 2;
2315 return size;
2317 else if (GET_CODE (op) == MEM)
2319 op = XEXP (op, 0);
2320 if (CONSTANT_P (op))
2322 *opclass = H8OP_MEM_ABSOLUTE;
2323 return h8300_constant_length (op);
2325 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2327 *opclass = H8OP_MEM_COMPLEX;
2328 return h8300_displacement_length (op, size);
2330 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2332 *opclass = H8OP_MEM_COMPLEX;
2333 return 0;
2335 else if (register_operand (op, VOIDmode))
2337 *opclass = H8OP_MEM_BASE;
2338 return 0;
2341 gcc_assert (register_operand (op, VOIDmode));
2342 *opclass = H8OP_REGISTER;
2343 return 0;
2346 /* Return the length of the instruction described by TABLE given that
2347 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2348 and OP2 must be an h8300_src_operand. */
2350 static unsigned int
2351 h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2353 enum h8300_operand_class op1_class, op2_class;
2354 unsigned int size, immediate_length;
2356 size = GET_MODE_SIZE (GET_MODE (op1));
2357 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2358 + h8300_classify_operand (op2, size, &op2_class));
2359 return immediate_length + (*table)[op1_class - 1][op2_class];
2362 /* Return the length of a unary instruction such as neg or not given that
2363 its operand is OP. */
2365 unsigned int
2366 h8300_unary_length (rtx op)
2368 enum h8300_operand_class opclass;
2369 unsigned int size, operand_length;
2371 size = GET_MODE_SIZE (GET_MODE (op));
2372 operand_length = h8300_classify_operand (op, size, &opclass);
2373 switch (opclass)
2375 case H8OP_REGISTER:
2376 return 2;
2378 case H8OP_MEM_BASE:
2379 return (size == 4 ? 6 : 4);
2381 case H8OP_MEM_ABSOLUTE:
2382 return operand_length + (size == 4 ? 6 : 4);
2384 case H8OP_MEM_COMPLEX:
2385 return operand_length + 6;
2387 default:
2388 gcc_unreachable ();
2392 /* Likewise short immediate instructions such as add.w #xx:3,OP. */
2394 static unsigned int
2395 h8300_short_immediate_length (rtx op)
2397 enum h8300_operand_class opclass;
2398 unsigned int size, operand_length;
2400 size = GET_MODE_SIZE (GET_MODE (op));
2401 operand_length = h8300_classify_operand (op, size, &opclass);
2403 switch (opclass)
2405 case H8OP_REGISTER:
2406 return 2;
2408 case H8OP_MEM_BASE:
2409 case H8OP_MEM_ABSOLUTE:
2410 case H8OP_MEM_COMPLEX:
2411 return 4 + operand_length;
2413 default:
2414 gcc_unreachable ();
2418 /* Likewise bitfield load and store instructions. */
2420 static unsigned int
2421 h8300_bitfield_length (rtx op, rtx op2)
2423 enum h8300_operand_class opclass;
2424 unsigned int size, operand_length;
2426 if (GET_CODE (op) == REG)
2427 op = op2;
2428 gcc_assert (GET_CODE (op) != REG);
2430 size = GET_MODE_SIZE (GET_MODE (op));
2431 operand_length = h8300_classify_operand (op, size, &opclass);
2433 switch (opclass)
2435 case H8OP_MEM_BASE:
2436 case H8OP_MEM_ABSOLUTE:
2437 case H8OP_MEM_COMPLEX:
2438 return 4 + operand_length;
2440 default:
2441 gcc_unreachable ();
2445 /* Calculate the length of general binary instruction INSN using TABLE. */
2447 static unsigned int
2448 h8300_binary_length (rtx_insn *insn, const h8300_length_table *table)
2450 rtx set;
2452 set = single_set (insn);
2453 gcc_assert (set);
2455 if (BINARY_P (SET_SRC (set)))
2456 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2457 XEXP (SET_SRC (set), 1), table);
2458 else
2460 gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2461 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2462 XEXP (XEXP (SET_SRC (set), 1), 1),
2463 table);
2467 /* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2468 memory reference and either (1) it has the form @(d:16,Rn) or
2469 (2) its address has the code given by INC_CODE. */
2471 static bool
2472 h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
2474 rtx addr;
2475 unsigned int size;
2477 if (GET_CODE (op) != MEM)
2478 return false;
2480 addr = XEXP (op, 0);
2481 size = GET_MODE_SIZE (GET_MODE (op));
2482 if (size != 1 && size != 2)
2483 return false;
2485 return (GET_CODE (addr) == inc_code
2486 || (GET_CODE (addr) == PLUS
2487 && GET_CODE (XEXP (addr, 0)) == REG
2488 && h8300_displacement_length (addr, size) == 2));
2491 /* Calculate the length of move instruction INSN using the given length
2492 table. Although the tables are correct for most cases, there is some
2493 irregularity in the length of mov.b and mov.w. The following forms:
2495 mov @ERs+, Rd
2496 mov @(d:16,ERs), Rd
2497 mov Rs, @-ERd
2498 mov Rs, @(d:16,ERd)
2500 are two bytes shorter than most other "mov Rs, @complex" or
2501 "mov @complex,Rd" combinations. */
2503 static unsigned int
2504 h8300_move_length (rtx *operands, const h8300_length_table *table)
2506 unsigned int size;
2508 size = h8300_length_from_table (operands[0], operands[1], table);
2509 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2510 size -= 2;
2511 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2512 size -= 2;
2513 return size;
2516 /* Return the length of a mova instruction with the given operands.
2517 DEST is the register destination, SRC is the source address and
2518 OFFSET is the 16-bit or 32-bit displacement. */
2520 static unsigned int
2521 h8300_mova_length (rtx dest, rtx src, rtx offset)
2523 unsigned int size;
2525 size = (2
2526 + h8300_constant_length (offset)
2527 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2528 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2529 size += 2;
2530 return size;
2533 /* Compute the length of INSN based on its length_table attribute.
2534 OPERANDS is the array of its operands. */
2536 unsigned int
2537 h8300_insn_length_from_table (rtx_insn *insn, rtx * operands)
2539 switch (get_attr_length_table (insn))
2541 case LENGTH_TABLE_NONE:
2542 gcc_unreachable ();
2544 case LENGTH_TABLE_ADDB:
2545 return h8300_binary_length (insn, &addb_length_table);
2547 case LENGTH_TABLE_ADDW:
2548 return h8300_binary_length (insn, &addw_length_table);
2550 case LENGTH_TABLE_ADDL:
2551 return h8300_binary_length (insn, &addl_length_table);
2553 case LENGTH_TABLE_LOGICB:
2554 return h8300_binary_length (insn, &logicb_length_table);
2556 case LENGTH_TABLE_MOVB:
2557 return h8300_move_length (operands, &movb_length_table);
2559 case LENGTH_TABLE_MOVW:
2560 return h8300_move_length (operands, &movw_length_table);
2562 case LENGTH_TABLE_MOVL:
2563 return h8300_move_length (operands, &movl_length_table);
2565 case LENGTH_TABLE_MOVA:
2566 return h8300_mova_length (operands[0], operands[1], operands[2]);
2568 case LENGTH_TABLE_MOVA_ZERO:
2569 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2571 case LENGTH_TABLE_UNARY:
2572 return h8300_unary_length (operands[0]);
2574 case LENGTH_TABLE_MOV_IMM4:
2575 return 2 + h8300_classify_operand (operands[0], 0, 0);
2577 case LENGTH_TABLE_SHORT_IMMEDIATE:
2578 return h8300_short_immediate_length (operands[0]);
2580 case LENGTH_TABLE_BITFIELD:
2581 return h8300_bitfield_length (operands[0], operands[1]);
2583 case LENGTH_TABLE_BITBRANCH:
2584 return h8300_bitfield_length (operands[1], operands[2]) - 2;
2586 default:
2587 gcc_unreachable ();
2591 /* Return true if LHS and RHS are memory references that can be mapped
2592 to the same h8sx assembly operand. LHS appears as the destination of
2593 an instruction and RHS appears as a source.
2595 Three cases are allowed:
2597 - RHS is @+Rn or @-Rn, LHS is @Rn
2598 - RHS is @Rn, LHS is @Rn+ or @Rn-
2599 - RHS and LHS have the same address and neither has side effects. */
2601 bool
2602 h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
2604 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2606 rhs = XEXP (rhs, 0);
2607 lhs = XEXP (lhs, 0);
2609 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2610 return rtx_equal_p (XEXP (rhs, 0), lhs);
2612 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2613 return rtx_equal_p (rhs, XEXP (lhs, 0));
2615 if (rtx_equal_p (rhs, lhs))
2616 return true;
2618 return false;
2621 /* Return true if OPERANDS[1] can be mapped to the same assembly
2622 operand as OPERANDS[0]. */
2624 bool
2625 h8300_operands_match_p (rtx *operands)
2627 if (register_operand (operands[0], VOIDmode)
2628 && register_operand (operands[1], VOIDmode))
2629 return true;
2631 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2632 return true;
2634 return false;
2637 /* Try using movmd to move LENGTH bytes from memory region SRC to memory
2638 region DEST. The two regions do not overlap and have the common
2639 alignment given by ALIGNMENT. Return true on success.
2641 Using movmd for variable-length moves seems to involve some
2642 complex trade-offs. For instance:
2644 - Preparing for a movmd instruction is similar to preparing
2645 for a memcpy. The main difference is that the arguments
2646 are moved into er4, er5 and er6 rather than er0, er1 and er2.
2648 - Since movmd clobbers the frame pointer, we need to save
2649 and restore it somehow when frame_pointer_needed. This can
2650 sometimes make movmd sequences longer than calls to memcpy().
2652 - The counter register is 16 bits, so the instruction is only
2653 suitable for variable-length moves when sizeof (size_t) == 2.
2654 That's only true in normal mode.
2656 - We will often lack static alignment information. Falling back
2657 on movmd.b would likely be slower than calling memcpy(), at least
2658 for big moves.
2660 This function therefore only uses movmd when the length is a
2661 known constant, and only then if -fomit-frame-pointer is in
2662 effect or if we're not optimizing for size.
2664 At the moment the function uses movmd for all in-range constants,
2665 but it might be better to fall back on memcpy() for large moves
2666 if ALIGNMENT == 1. */
2668 bool
2669 h8sx_emit_movmd (rtx dest, rtx src, rtx length,
2670 HOST_WIDE_INT alignment)
2672 if (!flag_omit_frame_pointer && optimize_size)
2673 return false;
2675 if (GET_CODE (length) == CONST_INT)
2677 rtx dest_reg, src_reg, first_dest, first_src;
2678 HOST_WIDE_INT n;
2679 int factor;
2681 /* Use movmd.l if the alignment allows it, otherwise fall back
2682 on movmd.b. */
2683 factor = (alignment >= 2 ? 4 : 1);
2685 /* Make sure the length is within range. We can handle counter
2686 values up to 65536, although HImode truncation will make
2687 the count appear negative in rtl dumps. */
2688 n = INTVAL (length);
2689 if (n <= 0 || n / factor > 65536)
2690 return false;
2692 /* Create temporary registers for the source and destination
2693 pointers. Initialize them to the start of each region. */
2694 dest_reg = copy_addr_to_reg (XEXP (dest, 0));
2695 src_reg = copy_addr_to_reg (XEXP (src, 0));
2697 /* Create references to the movmd source and destination blocks. */
2698 first_dest = replace_equiv_address (dest, dest_reg);
2699 first_src = replace_equiv_address (src, src_reg);
2701 set_mem_size (first_dest, n & -factor);
2702 set_mem_size (first_src, n & -factor);
2704 length = copy_to_mode_reg (HImode, gen_int_mode (n / factor, HImode));
2705 emit_insn (gen_movmd (first_dest, first_src, length, GEN_INT (factor)));
2707 if ((n & -factor) != n)
2709 /* Move SRC and DEST past the region we just copied.
2710 This is done to update the memory attributes. */
2711 dest = adjust_address (dest, BLKmode, n & -factor);
2712 src = adjust_address (src, BLKmode, n & -factor);
2714 /* Replace the addresses with the source and destination
2715 registers, which movmd has left with the right values. */
2716 dest = replace_equiv_address (dest, dest_reg);
2717 src = replace_equiv_address (src, src_reg);
2719 /* Mop up the left-over bytes. */
2720 if (n & 2)
2721 emit_move_insn (adjust_address (dest, HImode, 0),
2722 adjust_address (src, HImode, 0));
2723 if (n & 1)
2724 emit_move_insn (adjust_address (dest, QImode, n & 2),
2725 adjust_address (src, QImode, n & 2));
2727 return true;
2729 return false;
2732 /* Move ADDR into er6 after pushing its old value onto the stack. */
2734 void
2735 h8300_swap_into_er6 (rtx addr)
2737 rtx insn = push (HARD_FRAME_POINTER_REGNUM);
2738 if (frame_pointer_needed)
2739 add_reg_note (insn, REG_CFA_DEF_CFA,
2740 plus_constant (Pmode, gen_rtx_MEM (Pmode, stack_pointer_rtx),
2741 2 * UNITS_PER_WORD));
2742 else
2743 add_reg_note (insn, REG_CFA_ADJUST_CFA,
2744 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
2745 plus_constant (Pmode, stack_pointer_rtx, 4)));
2747 emit_move_insn (hard_frame_pointer_rtx, addr);
2748 if (REGNO (addr) == SP_REG)
2749 emit_move_insn (hard_frame_pointer_rtx,
2750 plus_constant (Pmode, hard_frame_pointer_rtx,
2751 GET_MODE_SIZE (word_mode)));
2754 /* Move the current value of er6 into ADDR and pop its old value
2755 from the stack. */
2757 void
2758 h8300_swap_out_of_er6 (rtx addr)
2760 rtx insn;
2762 if (REGNO (addr) != SP_REG)
2763 emit_move_insn (addr, hard_frame_pointer_rtx);
2765 insn = pop (HARD_FRAME_POINTER_REGNUM);
2766 RTX_FRAME_RELATED_P (insn) = 1;
2767 if (frame_pointer_needed)
2768 add_reg_note (insn, REG_CFA_DEF_CFA,
2769 plus_constant (Pmode, hard_frame_pointer_rtx,
2770 2 * UNITS_PER_WORD));
2771 else
2772 add_reg_note (insn, REG_CFA_ADJUST_CFA,
2773 gen_rtx_SET (VOIDmode, stack_pointer_rtx,
2774 plus_constant (Pmode, stack_pointer_rtx, -4)));
2777 /* Return the length of mov instruction. */
2779 unsigned int
2780 compute_mov_length (rtx *operands)
2782 /* If the mov instruction involves a memory operand, we compute the
2783 length, assuming the largest addressing mode is used, and then
2784 adjust later in the function. Otherwise, we compute and return
2785 the exact length in one step. */
2786 enum machine_mode mode = GET_MODE (operands[0]);
2787 rtx dest = operands[0];
2788 rtx src = operands[1];
2789 rtx addr;
2791 if (GET_CODE (src) == MEM)
2792 addr = XEXP (src, 0);
2793 else if (GET_CODE (dest) == MEM)
2794 addr = XEXP (dest, 0);
2795 else
2796 addr = NULL_RTX;
2798 if (TARGET_H8300)
2800 unsigned int base_length;
2802 switch (mode)
2804 case QImode:
2805 if (addr == NULL_RTX)
2806 return 2;
2808 /* The eightbit addressing is available only in QImode, so
2809 go ahead and take care of it. */
2810 if (h8300_eightbit_constant_address_p (addr))
2811 return 2;
2813 base_length = 4;
2814 break;
2816 case HImode:
2817 if (addr == NULL_RTX)
2819 if (REG_P (src))
2820 return 2;
2822 if (src == const0_rtx)
2823 return 2;
2825 return 4;
2828 base_length = 4;
2829 break;
2831 case SImode:
2832 if (addr == NULL_RTX)
2834 if (REG_P (src))
2835 return 4;
2837 if (GET_CODE (src) == CONST_INT)
2839 if (src == const0_rtx)
2840 return 4;
2842 if ((INTVAL (src) & 0xffff) == 0)
2843 return 6;
2845 if ((INTVAL (src) & 0xffff) == 0)
2846 return 6;
2848 if ((INTVAL (src) & 0xffff)
2849 == ((INTVAL (src) >> 16) & 0xffff))
2850 return 6;
2852 return 8;
2855 base_length = 8;
2856 break;
2858 case SFmode:
2859 if (addr == NULL_RTX)
2861 if (REG_P (src))
2862 return 4;
2864 if (satisfies_constraint_G (src))
2865 return 4;
2867 return 8;
2870 base_length = 8;
2871 break;
2873 default:
2874 gcc_unreachable ();
2877 /* Adjust the length based on the addressing mode used.
2878 Specifically, we subtract the difference between the actual
2879 length and the longest one, which is @(d:16,Rs). For SImode
2880 and SFmode, we double the adjustment because two mov.w are
2881 used to do the job. */
2883 /* @Rs+ and @-Rd are 2 bytes shorter than the longest. */
2884 if (GET_CODE (addr) == PRE_DEC
2885 || GET_CODE (addr) == POST_INC)
2887 if (mode == QImode || mode == HImode)
2888 return base_length - 2;
2889 else
2890 /* In SImode and SFmode, we use two mov.w instructions, so
2891 double the adjustment. */
2892 return base_length - 4;
2895 /* @Rs and @Rd are 2 bytes shorter than the longest. Note that
2896 in SImode and SFmode, the second mov.w involves an address
2897 with displacement, namely @(2,Rs) or @(2,Rd), so we subtract
2898 only 2 bytes. */
2899 if (GET_CODE (addr) == REG)
2900 return base_length - 2;
2902 return base_length;
2904 else
2906 unsigned int base_length;
2908 switch (mode)
2910 case QImode:
2911 if (addr == NULL_RTX)
2912 return 2;
2914 /* The eightbit addressing is available only in QImode, so
2915 go ahead and take care of it. */
2916 if (h8300_eightbit_constant_address_p (addr))
2917 return 2;
2919 base_length = 8;
2920 break;
2922 case HImode:
2923 if (addr == NULL_RTX)
2925 if (REG_P (src))
2926 return 2;
2928 if (src == const0_rtx)
2929 return 2;
2931 return 4;
2934 base_length = 8;
2935 break;
2937 case SImode:
2938 if (addr == NULL_RTX)
2940 if (REG_P (src))
2942 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
2943 return 4;
2944 else
2945 return 2;
2948 if (GET_CODE (src) == CONST_INT)
2950 int val = INTVAL (src);
2952 if (val == 0)
2953 return 2;
2955 if (val == (val & 0x00ff) || val == (val & 0xff00))
2956 return 4;
2958 switch (val & 0xffffffff)
2960 case 0xffffffff:
2961 case 0xfffffffe:
2962 case 0xfffffffc:
2963 case 0x0000ffff:
2964 case 0x0000fffe:
2965 case 0xffff0000:
2966 case 0xfffe0000:
2967 case 0x00010000:
2968 case 0x00020000:
2969 return 4;
2972 return 6;
2975 base_length = 10;
2976 break;
2978 case SFmode:
2979 if (addr == NULL_RTX)
2981 if (REG_P (src))
2982 return 2;
2984 if (satisfies_constraint_G (src))
2985 return 2;
2987 return 6;
2990 base_length = 10;
2991 break;
2993 default:
2994 gcc_unreachable ();
2997 /* Adjust the length based on the addressing mode used.
2998 Specifically, we subtract the difference between the actual
2999 length and the longest one, which is @(d:24,ERs). */
3001 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
3002 if (GET_CODE (addr) == PRE_DEC
3003 || GET_CODE (addr) == POST_INC)
3004 return base_length - 6;
3006 /* @ERs and @ERd are 6 bytes shorter than the longest. */
3007 if (GET_CODE (addr) == REG)
3008 return base_length - 6;
3010 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
3011 longest. */
3012 if (GET_CODE (addr) == PLUS
3013 && GET_CODE (XEXP (addr, 0)) == REG
3014 && GET_CODE (XEXP (addr, 1)) == CONST_INT
3015 && INTVAL (XEXP (addr, 1)) > -32768
3016 && INTVAL (XEXP (addr, 1)) < 32767)
3017 return base_length - 4;
3019 /* @aa:16 is 4 bytes shorter than the longest. */
3020 if (h8300_tiny_constant_address_p (addr))
3021 return base_length - 4;
3023 /* @aa:24 is 2 bytes shorter than the longest. */
3024 if (CONSTANT_P (addr))
3025 return base_length - 2;
3027 return base_length;
3031 /* Output an addition insn. */
3033 const char *
3034 output_plussi (rtx *operands)
3036 enum machine_mode mode = GET_MODE (operands[0]);
3038 gcc_assert (mode == SImode);
3040 if (TARGET_H8300)
3042 if (GET_CODE (operands[2]) == REG)
3043 return "add.w\t%f2,%f0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3045 if (GET_CODE (operands[2]) == CONST_INT)
3047 HOST_WIDE_INT n = INTVAL (operands[2]);
3049 if ((n & 0xffffff) == 0)
3050 return "add\t%z2,%z0";
3051 if ((n & 0xffff) == 0)
3052 return "add\t%y2,%y0\n\taddx\t%z2,%z0";
3053 if ((n & 0xff) == 0)
3054 return "add\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3057 return "add\t%w2,%w0\n\taddx\t%x2,%x0\n\taddx\t%y2,%y0\n\taddx\t%z2,%z0";
3059 else
3061 if (GET_CODE (operands[2]) == CONST_INT
3062 && register_operand (operands[1], VOIDmode))
3064 HOST_WIDE_INT intval = INTVAL (operands[2]);
3066 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3067 return "add.l\t%S2,%S0";
3068 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3069 return "sub.l\t%G2,%S0";
3071 /* See if we can finish with 2 bytes. */
3073 switch ((unsigned int) intval & 0xffffffff)
3075 case 0x00000001:
3076 case 0x00000002:
3077 case 0x00000004:
3078 return "adds\t%2,%S0";
3080 case 0xffffffff:
3081 case 0xfffffffe:
3082 case 0xfffffffc:
3083 return "subs\t%G2,%S0";
3085 case 0x00010000:
3086 case 0x00020000:
3087 operands[2] = GEN_INT (intval >> 16);
3088 return "inc.w\t%2,%e0";
3090 case 0xffff0000:
3091 case 0xfffe0000:
3092 operands[2] = GEN_INT (intval >> 16);
3093 return "dec.w\t%G2,%e0";
3096 /* See if we can finish with 4 bytes. */
3097 if ((intval & 0xffff) == 0)
3099 operands[2] = GEN_INT (intval >> 16);
3100 return "add.w\t%2,%e0";
3104 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3106 operands[2] = GEN_INT (-INTVAL (operands[2]));
3107 return "sub.l\t%S2,%S0";
3109 return "add.l\t%S2,%S0";
3113 /* ??? It would be much easier to add the h8sx stuff if a single function
3114 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
3115 /* Compute the length of an addition insn. */
3117 unsigned int
3118 compute_plussi_length (rtx *operands)
3120 enum machine_mode mode = GET_MODE (operands[0]);
3122 gcc_assert (mode == SImode);
3124 if (TARGET_H8300)
3126 if (GET_CODE (operands[2]) == REG)
3127 return 6;
3129 if (GET_CODE (operands[2]) == CONST_INT)
3131 HOST_WIDE_INT n = INTVAL (operands[2]);
3133 if ((n & 0xffffff) == 0)
3134 return 2;
3135 if ((n & 0xffff) == 0)
3136 return 4;
3137 if ((n & 0xff) == 0)
3138 return 6;
3141 return 8;
3143 else
3145 if (GET_CODE (operands[2]) == CONST_INT
3146 && register_operand (operands[1], VOIDmode))
3148 HOST_WIDE_INT intval = INTVAL (operands[2]);
3150 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3151 return 2;
3152 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3153 return 2;
3155 /* See if we can finish with 2 bytes. */
3157 switch ((unsigned int) intval & 0xffffffff)
3159 case 0x00000001:
3160 case 0x00000002:
3161 case 0x00000004:
3162 return 2;
3164 case 0xffffffff:
3165 case 0xfffffffe:
3166 case 0xfffffffc:
3167 return 2;
3169 case 0x00010000:
3170 case 0x00020000:
3171 return 2;
3173 case 0xffff0000:
3174 case 0xfffe0000:
3175 return 2;
3178 /* See if we can finish with 4 bytes. */
3179 if ((intval & 0xffff) == 0)
3180 return 4;
3183 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
3184 return h8300_length_from_table (operands[0],
3185 GEN_INT (-INTVAL (operands[2])),
3186 &addl_length_table);
3187 else
3188 return h8300_length_from_table (operands[0], operands[2],
3189 &addl_length_table);
3190 return 6;
3194 /* Compute which flag bits are valid after an addition insn. */
3196 enum attr_cc
3197 compute_plussi_cc (rtx *operands)
3199 enum machine_mode mode = GET_MODE (operands[0]);
3201 gcc_assert (mode == SImode);
3203 if (TARGET_H8300)
3205 return CC_CLOBBER;
3207 else
3209 if (GET_CODE (operands[2]) == CONST_INT
3210 && register_operand (operands[1], VOIDmode))
3212 HOST_WIDE_INT intval = INTVAL (operands[2]);
3214 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
3215 return CC_SET_ZN;
3216 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
3217 return CC_SET_ZN;
3219 /* See if we can finish with 2 bytes. */
3221 switch ((unsigned int) intval & 0xffffffff)
3223 case 0x00000001:
3224 case 0x00000002:
3225 case 0x00000004:
3226 return CC_NONE_0HIT;
3228 case 0xffffffff:
3229 case 0xfffffffe:
3230 case 0xfffffffc:
3231 return CC_NONE_0HIT;
3233 case 0x00010000:
3234 case 0x00020000:
3235 return CC_CLOBBER;
3237 case 0xffff0000:
3238 case 0xfffe0000:
3239 return CC_CLOBBER;
3242 /* See if we can finish with 4 bytes. */
3243 if ((intval & 0xffff) == 0)
3244 return CC_CLOBBER;
3247 return CC_SET_ZN;
3251 /* Output a logical insn. */
3253 const char *
3254 output_logical_op (enum machine_mode mode, rtx *operands)
3256 /* Figure out the logical op that we need to perform. */
3257 enum rtx_code code = GET_CODE (operands[3]);
3258 /* Pretend that every byte is affected if both operands are registers. */
3259 const unsigned HOST_WIDE_INT intval =
3260 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3261 /* Always use the full instruction if the
3262 first operand is in memory. It is better
3263 to use define_splits to generate the shorter
3264 sequence where valid. */
3265 && register_operand (operands[1], VOIDmode)
3266 ? INTVAL (operands[2]) : 0x55555555);
3267 /* The determinant of the algorithm. If we perform an AND, 0
3268 affects a bit. Otherwise, 1 affects a bit. */
3269 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3270 /* Break up DET into pieces. */
3271 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3272 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3273 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3274 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3275 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3276 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3277 int lower_half_easy_p = 0;
3278 int upper_half_easy_p = 0;
3279 /* The name of an insn. */
3280 const char *opname;
3281 char insn_buf[100];
3283 switch (code)
3285 case AND:
3286 opname = "and";
3287 break;
3288 case IOR:
3289 opname = "or";
3290 break;
3291 case XOR:
3292 opname = "xor";
3293 break;
3294 default:
3295 gcc_unreachable ();
3298 switch (mode)
3300 case HImode:
3301 /* First, see if we can finish with one insn. */
3302 if ((TARGET_H8300H || TARGET_H8300S)
3303 && b0 != 0
3304 && b1 != 0)
3306 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
3307 output_asm_insn (insn_buf, operands);
3309 else
3311 /* Take care of the lower byte. */
3312 if (b0 != 0)
3314 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
3315 output_asm_insn (insn_buf, operands);
3317 /* Take care of the upper byte. */
3318 if (b1 != 0)
3320 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
3321 output_asm_insn (insn_buf, operands);
3324 break;
3325 case SImode:
3326 if (TARGET_H8300H || TARGET_H8300S)
3328 /* Determine if the lower half can be taken care of in no more
3329 than two bytes. */
3330 lower_half_easy_p = (b0 == 0
3331 || b1 == 0
3332 || (code != IOR && w0 == 0xffff));
3334 /* Determine if the upper half can be taken care of in no more
3335 than two bytes. */
3336 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3337 || (code == AND && w1 == 0xff00));
3340 /* Check if doing everything with one insn is no worse than
3341 using multiple insns. */
3342 if ((TARGET_H8300H || TARGET_H8300S)
3343 && w0 != 0 && w1 != 0
3344 && !(lower_half_easy_p && upper_half_easy_p)
3345 && !(code == IOR && w1 == 0xffff
3346 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3348 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
3349 output_asm_insn (insn_buf, operands);
3351 else
3353 /* Take care of the lower and upper words individually. For
3354 each word, we try different methods in the order of
3356 1) the special insn (in case of AND or XOR),
3357 2) the word-wise insn, and
3358 3) The byte-wise insn. */
3359 if (w0 == 0xffff
3360 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3361 output_asm_insn ((code == AND)
3362 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
3363 operands);
3364 else if ((TARGET_H8300H || TARGET_H8300S)
3365 && (b0 != 0)
3366 && (b1 != 0))
3368 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
3369 output_asm_insn (insn_buf, operands);
3371 else
3373 if (b0 != 0)
3375 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
3376 output_asm_insn (insn_buf, operands);
3378 if (b1 != 0)
3380 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3381 output_asm_insn (insn_buf, operands);
3385 if ((w1 == 0xffff)
3386 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3387 output_asm_insn ((code == AND)
3388 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
3389 operands);
3390 else if ((TARGET_H8300H || TARGET_H8300S)
3391 && code == IOR
3392 && w1 == 0xffff
3393 && (w0 & 0x8000) != 0)
3395 output_asm_insn ("exts.l\t%S0", operands);
3397 else if ((TARGET_H8300H || TARGET_H8300S)
3398 && code == AND
3399 && w1 == 0xff00)
3401 output_asm_insn ("extu.w\t%e0", operands);
3403 else if (TARGET_H8300H || TARGET_H8300S)
3405 if (w1 != 0)
3407 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3408 output_asm_insn (insn_buf, operands);
3411 else
3413 if (b2 != 0)
3415 sprintf (insn_buf, "%s\t%%y2,%%y0", opname);
3416 output_asm_insn (insn_buf, operands);
3418 if (b3 != 0)
3420 sprintf (insn_buf, "%s\t%%z2,%%z0", opname);
3421 output_asm_insn (insn_buf, operands);
3425 break;
3426 default:
3427 gcc_unreachable ();
3429 return "";
3432 /* Compute the length of a logical insn. */
3434 unsigned int
3435 compute_logical_op_length (enum machine_mode mode, rtx *operands)
3437 /* Figure out the logical op that we need to perform. */
3438 enum rtx_code code = GET_CODE (operands[3]);
3439 /* Pretend that every byte is affected if both operands are registers. */
3440 const unsigned HOST_WIDE_INT intval =
3441 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3442 /* Always use the full instruction if the
3443 first operand is in memory. It is better
3444 to use define_splits to generate the shorter
3445 sequence where valid. */
3446 && register_operand (operands[1], VOIDmode)
3447 ? INTVAL (operands[2]) : 0x55555555);
3448 /* The determinant of the algorithm. If we perform an AND, 0
3449 affects a bit. Otherwise, 1 affects a bit. */
3450 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3451 /* Break up DET into pieces. */
3452 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3453 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3454 const unsigned HOST_WIDE_INT b2 = (det >> 16) & 0xff;
3455 const unsigned HOST_WIDE_INT b3 = (det >> 24) & 0xff;
3456 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3457 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3458 int lower_half_easy_p = 0;
3459 int upper_half_easy_p = 0;
3460 /* Insn length. */
3461 unsigned int length = 0;
3463 switch (mode)
3465 case HImode:
3466 /* First, see if we can finish with one insn. */
3467 if ((TARGET_H8300H || TARGET_H8300S)
3468 && b0 != 0
3469 && b1 != 0)
3471 length = h8300_length_from_table (operands[1], operands[2],
3472 &logicw_length_table);
3474 else
3476 /* Take care of the lower byte. */
3477 if (b0 != 0)
3478 length += 2;
3480 /* Take care of the upper byte. */
3481 if (b1 != 0)
3482 length += 2;
3484 break;
3485 case SImode:
3486 if (TARGET_H8300H || TARGET_H8300S)
3488 /* Determine if the lower half can be taken care of in no more
3489 than two bytes. */
3490 lower_half_easy_p = (b0 == 0
3491 || b1 == 0
3492 || (code != IOR && w0 == 0xffff));
3494 /* Determine if the upper half can be taken care of in no more
3495 than two bytes. */
3496 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3497 || (code == AND && w1 == 0xff00));
3500 /* Check if doing everything with one insn is no worse than
3501 using multiple insns. */
3502 if ((TARGET_H8300H || TARGET_H8300S)
3503 && w0 != 0 && w1 != 0
3504 && !(lower_half_easy_p && upper_half_easy_p)
3505 && !(code == IOR && w1 == 0xffff
3506 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3508 length = h8300_length_from_table (operands[1], operands[2],
3509 &logicl_length_table);
3511 else
3513 /* Take care of the lower and upper words individually. For
3514 each word, we try different methods in the order of
3516 1) the special insn (in case of AND or XOR),
3517 2) the word-wise insn, and
3518 3) The byte-wise insn. */
3519 if (w0 == 0xffff
3520 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3522 length += 2;
3524 else if ((TARGET_H8300H || TARGET_H8300S)
3525 && (b0 != 0)
3526 && (b1 != 0))
3528 length += 4;
3530 else
3532 if (b0 != 0)
3533 length += 2;
3535 if (b1 != 0)
3536 length += 2;
3539 if (w1 == 0xffff
3540 && (TARGET_H8300 ? (code == AND) : (code != IOR)))
3542 length += 2;
3544 else if ((TARGET_H8300H || TARGET_H8300S)
3545 && code == IOR
3546 && w1 == 0xffff
3547 && (w0 & 0x8000) != 0)
3549 length += 2;
3551 else if ((TARGET_H8300H || TARGET_H8300S)
3552 && code == AND
3553 && w1 == 0xff00)
3555 length += 2;
3557 else if (TARGET_H8300H || TARGET_H8300S)
3559 if (w1 != 0)
3560 length += 4;
3562 else
3564 if (b2 != 0)
3565 length += 2;
3567 if (b3 != 0)
3568 length += 2;
3571 break;
3572 default:
3573 gcc_unreachable ();
3575 return length;
3578 /* Compute which flag bits are valid after a logical insn. */
3580 enum attr_cc
3581 compute_logical_op_cc (enum machine_mode mode, rtx *operands)
3583 /* Figure out the logical op that we need to perform. */
3584 enum rtx_code code = GET_CODE (operands[3]);
3585 /* Pretend that every byte is affected if both operands are registers. */
3586 const unsigned HOST_WIDE_INT intval =
3587 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
3588 /* Always use the full instruction if the
3589 first operand is in memory. It is better
3590 to use define_splits to generate the shorter
3591 sequence where valid. */
3592 && register_operand (operands[1], VOIDmode)
3593 ? INTVAL (operands[2]) : 0x55555555);
3594 /* The determinant of the algorithm. If we perform an AND, 0
3595 affects a bit. Otherwise, 1 affects a bit. */
3596 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
3597 /* Break up DET into pieces. */
3598 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3599 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3600 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3601 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3602 int lower_half_easy_p = 0;
3603 int upper_half_easy_p = 0;
3604 /* Condition code. */
3605 enum attr_cc cc = CC_CLOBBER;
3607 switch (mode)
3609 case HImode:
3610 /* First, see if we can finish with one insn. */
3611 if ((TARGET_H8300H || TARGET_H8300S)
3612 && b0 != 0
3613 && b1 != 0)
3615 cc = CC_SET_ZNV;
3617 break;
3618 case SImode:
3619 if (TARGET_H8300H || TARGET_H8300S)
3621 /* Determine if the lower half can be taken care of in no more
3622 than two bytes. */
3623 lower_half_easy_p = (b0 == 0
3624 || b1 == 0
3625 || (code != IOR && w0 == 0xffff));
3627 /* Determine if the upper half can be taken care of in no more
3628 than two bytes. */
3629 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3630 || (code == AND && w1 == 0xff00));
3633 /* Check if doing everything with one insn is no worse than
3634 using multiple insns. */
3635 if ((TARGET_H8300H || TARGET_H8300S)
3636 && w0 != 0 && w1 != 0
3637 && !(lower_half_easy_p && upper_half_easy_p)
3638 && !(code == IOR && w1 == 0xffff
3639 && (w0 & 0x8000) != 0 && lower_half_easy_p))
3641 cc = CC_SET_ZNV;
3643 else
3645 if ((TARGET_H8300H || TARGET_H8300S)
3646 && code == IOR
3647 && w1 == 0xffff
3648 && (w0 & 0x8000) != 0)
3650 cc = CC_SET_ZNV;
3653 break;
3654 default:
3655 gcc_unreachable ();
3657 return cc;
3660 /* Expand a conditional branch. */
3662 void
3663 h8300_expand_branch (rtx operands[])
3665 enum rtx_code code = GET_CODE (operands[0]);
3666 rtx op0 = operands[1];
3667 rtx op1 = operands[2];
3668 rtx label = operands[3];
3669 rtx tmp;
3671 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3672 emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3674 tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3675 tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3676 gen_rtx_LABEL_REF (VOIDmode, label),
3677 pc_rtx);
3678 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
3682 /* Expand a conditional store. */
3684 void
3685 h8300_expand_store (rtx operands[])
3687 rtx dest = operands[0];
3688 enum rtx_code code = GET_CODE (operands[1]);
3689 rtx op0 = operands[2];
3690 rtx op1 = operands[3];
3691 rtx tmp;
3693 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
3694 emit_insn (gen_rtx_SET (VOIDmode, cc0_rtx, tmp));
3696 tmp = gen_rtx_fmt_ee (code, GET_MODE (dest), cc0_rtx, const0_rtx);
3697 emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
3700 /* Shifts.
3702 We devote a fair bit of code to getting efficient shifts since we
3703 can only shift one bit at a time on the H8/300 and H8/300H and only
3704 one or two bits at a time on the H8S.
3706 All shift code falls into one of the following ways of
3707 implementation:
3709 o SHIFT_INLINE: Emit straight line code for the shift; this is used
3710 when a straight line shift is about the same size or smaller than
3711 a loop.
3713 o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3714 off the bits we don't need. This is used when only a few of the
3715 bits in the original value will survive in the shifted value.
3717 o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3718 simulate a shift by 8, 16, or 24 bits. Once moved, a few inline
3719 shifts can be added if the shift count is slightly more than 8 or
3720 16. This case also includes other oddballs that are not worth
3721 explaining here.
3723 o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
3725 For each shift count, we try to use code that has no trade-off
3726 between code size and speed whenever possible.
3728 If the trade-off is unavoidable, we try to be reasonable.
3729 Specifically, the fastest version is one instruction longer than
3730 the shortest version, we take the fastest version. We also provide
3731 the use a way to switch back to the shortest version with -Os.
3733 For the details of the shift algorithms for various shift counts,
3734 refer to shift_alg_[qhs]i. */
3736 /* Classify a shift with the given mode and code. OP is the shift amount. */
3738 enum h8sx_shift_type
3739 h8sx_classify_shift (enum machine_mode mode, enum rtx_code code, rtx op)
3741 if (!TARGET_H8300SX)
3742 return H8SX_SHIFT_NONE;
3744 switch (code)
3746 case ASHIFT:
3747 case LSHIFTRT:
3748 /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd). */
3749 if (GET_CODE (op) != CONST_INT)
3750 return H8SX_SHIFT_BINARY;
3752 /* Reject out-of-range shift amounts. */
3753 if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3754 return H8SX_SHIFT_NONE;
3756 /* Power-of-2 shifts are effectively unary operations. */
3757 if (exact_log2 (INTVAL (op)) >= 0)
3758 return H8SX_SHIFT_UNARY;
3760 return H8SX_SHIFT_BINARY;
3762 case ASHIFTRT:
3763 if (op == const1_rtx || op == const2_rtx)
3764 return H8SX_SHIFT_UNARY;
3765 return H8SX_SHIFT_NONE;
3767 case ROTATE:
3768 if (GET_CODE (op) == CONST_INT
3769 && (INTVAL (op) == 1
3770 || INTVAL (op) == 2
3771 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3772 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3773 return H8SX_SHIFT_UNARY;
3774 return H8SX_SHIFT_NONE;
3776 default:
3777 return H8SX_SHIFT_NONE;
3781 /* Return the asm template for a single h8sx shift instruction.
3782 OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3783 is the source and OPERANDS[3] is the shift. SUFFIX is the
3784 size suffix ('b', 'w' or 'l') and OPTYPE is the h8300_print_operand
3785 prefix for the destination operand. */
3787 const char *
3788 output_h8sx_shift (rtx *operands, int suffix, int optype)
3790 static char buffer[16];
3791 const char *stem;
3793 switch (GET_CODE (operands[3]))
3795 case ASHIFT:
3796 stem = "shll";
3797 break;
3799 case ASHIFTRT:
3800 stem = "shar";
3801 break;
3803 case LSHIFTRT:
3804 stem = "shlr";
3805 break;
3807 case ROTATE:
3808 stem = "rotl";
3809 if (INTVAL (operands[2]) > 2)
3811 /* This is really a right rotate. */
3812 operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3813 - INTVAL (operands[2]));
3814 stem = "rotr";
3816 break;
3818 default:
3819 gcc_unreachable ();
3821 if (operands[2] == const1_rtx)
3822 sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3823 else
3824 sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3825 return buffer;
3828 /* Emit code to do shifts. */
3830 bool
3831 expand_a_shift (enum machine_mode mode, enum rtx_code code, rtx operands[])
3833 switch (h8sx_classify_shift (mode, code, operands[2]))
3835 case H8SX_SHIFT_BINARY:
3836 operands[1] = force_reg (mode, operands[1]);
3837 return false;
3839 case H8SX_SHIFT_UNARY:
3840 return false;
3842 case H8SX_SHIFT_NONE:
3843 break;
3846 emit_move_insn (copy_rtx (operands[0]), operands[1]);
3848 /* Need a loop to get all the bits we want - we generate the
3849 code at emit time, but need to allocate a scratch reg now. */
3851 emit_insn (gen_rtx_PARALLEL
3852 (VOIDmode,
3853 gen_rtvec (2,
3854 gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
3855 gen_rtx_fmt_ee (code, mode,
3856 copy_rtx (operands[0]), operands[2])),
3857 gen_rtx_CLOBBER (VOIDmode,
3858 gen_rtx_SCRATCH (QImode)))));
3859 return true;
3862 /* Symbols of the various modes which can be used as indices. */
3864 enum shift_mode
3866 QIshift, HIshift, SIshift
3869 /* For single bit shift insns, record assembler and what bits of the
3870 condition code are valid afterwards (represented as various CC_FOO
3871 bits, 0 means CC isn't left in a usable state). */
3873 struct shift_insn
3875 const char *const assembler;
3876 const enum attr_cc cc_valid;
3879 /* Assembler instruction shift table.
3881 These tables are used to look up the basic shifts.
3882 They are indexed by cpu, shift_type, and mode. */
3884 static const struct shift_insn shift_one[2][3][3] =
3886 /* H8/300 */
3888 /* SHIFT_ASHIFT */
3890 { "shll\t%X0", CC_SET_ZNV },
3891 { "add.w\t%T0,%T0", CC_SET_ZN },
3892 { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", CC_CLOBBER }
3894 /* SHIFT_LSHIFTRT */
3896 { "shlr\t%X0", CC_SET_ZNV },
3897 { "shlr\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3898 { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3900 /* SHIFT_ASHIFTRT */
3902 { "shar\t%X0", CC_SET_ZNV },
3903 { "shar\t%t0\n\trotxr\t%s0", CC_CLOBBER },
3904 { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", CC_CLOBBER }
3907 /* H8/300H */
3909 /* SHIFT_ASHIFT */
3911 { "shll.b\t%X0", CC_SET_ZNV },
3912 { "shll.w\t%T0", CC_SET_ZNV },
3913 { "shll.l\t%S0", CC_SET_ZNV }
3915 /* SHIFT_LSHIFTRT */
3917 { "shlr.b\t%X0", CC_SET_ZNV },
3918 { "shlr.w\t%T0", CC_SET_ZNV },
3919 { "shlr.l\t%S0", CC_SET_ZNV }
3921 /* SHIFT_ASHIFTRT */
3923 { "shar.b\t%X0", CC_SET_ZNV },
3924 { "shar.w\t%T0", CC_SET_ZNV },
3925 { "shar.l\t%S0", CC_SET_ZNV }
3930 static const struct shift_insn shift_two[3][3] =
3932 /* SHIFT_ASHIFT */
3934 { "shll.b\t#2,%X0", CC_SET_ZNV },
3935 { "shll.w\t#2,%T0", CC_SET_ZNV },
3936 { "shll.l\t#2,%S0", CC_SET_ZNV }
3938 /* SHIFT_LSHIFTRT */
3940 { "shlr.b\t#2,%X0", CC_SET_ZNV },
3941 { "shlr.w\t#2,%T0", CC_SET_ZNV },
3942 { "shlr.l\t#2,%S0", CC_SET_ZNV }
3944 /* SHIFT_ASHIFTRT */
3946 { "shar.b\t#2,%X0", CC_SET_ZNV },
3947 { "shar.w\t#2,%T0", CC_SET_ZNV },
3948 { "shar.l\t#2,%S0", CC_SET_ZNV }
3952 /* Rotates are organized by which shift they'll be used in implementing.
3953 There's no need to record whether the cc is valid afterwards because
3954 it is the AND insn that will decide this. */
3956 static const char *const rotate_one[2][3][3] =
3958 /* H8/300 */
3960 /* SHIFT_ASHIFT */
3962 "rotr\t%X0",
3963 "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
3966 /* SHIFT_LSHIFTRT */
3968 "rotl\t%X0",
3969 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3972 /* SHIFT_ASHIFTRT */
3974 "rotl\t%X0",
3975 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
3979 /* H8/300H */
3981 /* SHIFT_ASHIFT */
3983 "rotr.b\t%X0",
3984 "rotr.w\t%T0",
3985 "rotr.l\t%S0"
3987 /* SHIFT_LSHIFTRT */
3989 "rotl.b\t%X0",
3990 "rotl.w\t%T0",
3991 "rotl.l\t%S0"
3993 /* SHIFT_ASHIFTRT */
3995 "rotl.b\t%X0",
3996 "rotl.w\t%T0",
3997 "rotl.l\t%S0"
4002 static const char *const rotate_two[3][3] =
4004 /* SHIFT_ASHIFT */
4006 "rotr.b\t#2,%X0",
4007 "rotr.w\t#2,%T0",
4008 "rotr.l\t#2,%S0"
4010 /* SHIFT_LSHIFTRT */
4012 "rotl.b\t#2,%X0",
4013 "rotl.w\t#2,%T0",
4014 "rotl.l\t#2,%S0"
4016 /* SHIFT_ASHIFTRT */
4018 "rotl.b\t#2,%X0",
4019 "rotl.w\t#2,%T0",
4020 "rotl.l\t#2,%S0"
4024 struct shift_info {
4025 /* Shift algorithm. */
4026 enum shift_alg alg;
4028 /* The number of bits to be shifted by shift1 and shift2. Valid
4029 when ALG is SHIFT_SPECIAL. */
4030 unsigned int remainder;
4032 /* Special insn for a shift. Valid when ALG is SHIFT_SPECIAL. */
4033 const char *special;
4035 /* Insn for a one-bit shift. Valid when ALG is either SHIFT_INLINE
4036 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
4037 const char *shift1;
4039 /* Insn for a two-bit shift. Valid when ALG is either SHIFT_INLINE
4040 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
4041 const char *shift2;
4043 /* CC status for SHIFT_INLINE. */
4044 enum attr_cc cc_inline;
4046 /* CC status for SHIFT_SPECIAL. */
4047 enum attr_cc cc_special;
4050 static void get_shift_alg (enum shift_type,
4051 enum shift_mode, unsigned int,
4052 struct shift_info *);
4054 /* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
4055 best algorithm for doing the shift. The assembler code is stored
4056 in the pointers in INFO. We achieve the maximum efficiency in most
4057 cases when !TARGET_H8300. In case of TARGET_H8300, shifts in
4058 SImode in particular have a lot of room to optimize.
4060 We first determine the strategy of the shift algorithm by a table
4061 lookup. If that tells us to use a hand crafted assembly code, we
4062 go into the big switch statement to find what that is. Otherwise,
4063 we resort to a generic way, such as inlining. In either case, the
4064 result is returned through INFO. */
4066 static void
4067 get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
4068 unsigned int count, struct shift_info *info)
4070 enum h8_cpu cpu;
4072 /* Find the target CPU. */
4073 if (TARGET_H8300)
4074 cpu = H8_300;
4075 else if (TARGET_H8300H)
4076 cpu = H8_300H;
4077 else
4078 cpu = H8_S;
4080 /* Find the shift algorithm. */
4081 info->alg = SHIFT_LOOP;
4082 switch (shift_mode)
4084 case QIshift:
4085 if (count < GET_MODE_BITSIZE (QImode))
4086 info->alg = shift_alg_qi[cpu][shift_type][count];
4087 break;
4089 case HIshift:
4090 if (count < GET_MODE_BITSIZE (HImode))
4091 info->alg = shift_alg_hi[cpu][shift_type][count];
4092 break;
4094 case SIshift:
4095 if (count < GET_MODE_BITSIZE (SImode))
4096 info->alg = shift_alg_si[cpu][shift_type][count];
4097 break;
4099 default:
4100 gcc_unreachable ();
4103 /* Fill in INFO. Return unless we have SHIFT_SPECIAL. */
4104 switch (info->alg)
4106 case SHIFT_INLINE:
4107 info->remainder = count;
4108 /* Fall through. */
4110 case SHIFT_LOOP:
4111 /* It is up to the caller to know that looping clobbers cc. */
4112 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4113 info->shift2 = shift_two[shift_type][shift_mode].assembler;
4114 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4115 goto end;
4117 case SHIFT_ROT_AND:
4118 info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
4119 info->shift2 = rotate_two[shift_type][shift_mode];
4120 info->cc_inline = CC_CLOBBER;
4121 goto end;
4123 case SHIFT_SPECIAL:
4124 /* REMAINDER is 0 for most cases, so initialize it to 0. */
4125 info->remainder = 0;
4126 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
4127 info->shift2 = shift_two[shift_type][shift_mode].assembler;
4128 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
4129 info->cc_special = CC_CLOBBER;
4130 break;
4133 /* Here we only deal with SHIFT_SPECIAL. */
4134 switch (shift_mode)
4136 case QIshift:
4137 /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
4138 through the entire value. */
4139 gcc_assert (shift_type == SHIFT_ASHIFTRT && count == 7);
4140 info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
4141 goto end;
4143 case HIshift:
4144 if (count == 7)
4146 switch (shift_type)
4148 case SHIFT_ASHIFT:
4149 if (TARGET_H8300)
4150 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";
4151 else
4152 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
4153 goto end;
4154 case SHIFT_LSHIFTRT:
4155 if (TARGET_H8300)
4156 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";
4157 else
4158 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
4159 goto end;
4160 case SHIFT_ASHIFTRT:
4161 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
4162 goto end;
4165 else if ((8 <= count && count <= 13)
4166 || (TARGET_H8300S && count == 14))
4168 info->remainder = count - 8;
4170 switch (shift_type)
4172 case SHIFT_ASHIFT:
4173 info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
4174 goto end;
4175 case SHIFT_LSHIFTRT:
4176 if (TARGET_H8300)
4178 info->special = "mov.b\t%t0,%s0\n\tsub.b\t%t0,%t0";
4179 info->shift1 = "shlr.b\t%s0";
4180 info->cc_inline = CC_SET_ZNV;
4182 else
4184 info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
4185 info->cc_special = CC_SET_ZNV;
4187 goto end;
4188 case SHIFT_ASHIFTRT:
4189 if (TARGET_H8300)
4191 info->special = "mov.b\t%t0,%s0\n\tbld\t#7,%s0\n\tsubx\t%t0,%t0";
4192 info->shift1 = "shar.b\t%s0";
4194 else
4196 info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
4197 info->cc_special = CC_SET_ZNV;
4199 goto end;
4202 else if (count == 14)
4204 switch (shift_type)
4206 case SHIFT_ASHIFT:
4207 if (TARGET_H8300)
4208 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";
4209 goto end;
4210 case SHIFT_LSHIFTRT:
4211 if (TARGET_H8300)
4212 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";
4213 goto end;
4214 case SHIFT_ASHIFTRT:
4215 if (TARGET_H8300)
4216 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";
4217 else if (TARGET_H8300H)
4219 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";
4220 info->cc_special = CC_SET_ZNV;
4222 else /* TARGET_H8300S */
4223 gcc_unreachable ();
4224 goto end;
4227 else if (count == 15)
4229 switch (shift_type)
4231 case SHIFT_ASHIFT:
4232 info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
4233 goto end;
4234 case SHIFT_LSHIFTRT:
4235 info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
4236 goto end;
4237 case SHIFT_ASHIFTRT:
4238 info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
4239 goto end;
4242 gcc_unreachable ();
4244 case SIshift:
4245 if (TARGET_H8300 && 8 <= count && count <= 9)
4247 info->remainder = count - 8;
4249 switch (shift_type)
4251 case SHIFT_ASHIFT:
4252 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";
4253 goto end;
4254 case SHIFT_LSHIFTRT:
4255 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";
4256 info->shift1 = "shlr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0";
4257 goto end;
4258 case SHIFT_ASHIFTRT:
4259 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";
4260 goto end;
4263 else if (count == 8 && !TARGET_H8300)
4265 switch (shift_type)
4267 case SHIFT_ASHIFT:
4268 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";
4269 goto end;
4270 case SHIFT_LSHIFTRT:
4271 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";
4272 goto end;
4273 case SHIFT_ASHIFTRT:
4274 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";
4275 goto end;
4278 else if (count == 15 && TARGET_H8300)
4280 switch (shift_type)
4282 case SHIFT_ASHIFT:
4283 gcc_unreachable ();
4284 case SHIFT_LSHIFTRT:
4285 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";
4286 goto end;
4287 case SHIFT_ASHIFTRT:
4288 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";
4289 goto end;
4292 else if (count == 15 && !TARGET_H8300)
4294 switch (shift_type)
4296 case SHIFT_ASHIFT:
4297 info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
4298 info->cc_special = CC_SET_ZNV;
4299 goto end;
4300 case SHIFT_LSHIFTRT:
4301 info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
4302 info->cc_special = CC_SET_ZNV;
4303 goto end;
4304 case SHIFT_ASHIFTRT:
4305 gcc_unreachable ();
4308 else if ((TARGET_H8300 && 16 <= count && count <= 20)
4309 || (TARGET_H8300H && 16 <= count && count <= 19)
4310 || (TARGET_H8300S && 16 <= count && count <= 21))
4312 info->remainder = count - 16;
4314 switch (shift_type)
4316 case SHIFT_ASHIFT:
4317 info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
4318 if (TARGET_H8300)
4319 info->shift1 = "add.w\t%e0,%e0";
4320 goto end;
4321 case SHIFT_LSHIFTRT:
4322 if (TARGET_H8300)
4324 info->special = "mov.w\t%e0,%f0\n\tsub.w\t%e0,%e0";
4325 info->shift1 = "shlr\t%x0\n\trotxr\t%w0";
4327 else
4329 info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
4330 info->cc_special = CC_SET_ZNV;
4332 goto end;
4333 case SHIFT_ASHIFTRT:
4334 if (TARGET_H8300)
4336 info->special = "mov.w\t%e0,%f0\n\tshll\t%z0\n\tsubx\t%z0,%z0\n\tmov.b\t%z0,%y0";
4337 info->shift1 = "shar\t%x0\n\trotxr\t%w0";
4339 else
4341 info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
4342 info->cc_special = CC_SET_ZNV;
4344 goto end;
4347 else if (TARGET_H8300 && 24 <= count && count <= 28)
4349 info->remainder = count - 24;
4351 switch (shift_type)
4353 case SHIFT_ASHIFT:
4354 info->special = "mov.b\t%w0,%z0\n\tsub.b\t%y0,%y0\n\tsub.w\t%f0,%f0";
4355 info->shift1 = "shll.b\t%z0";
4356 info->cc_inline = CC_SET_ZNV;
4357 goto end;
4358 case SHIFT_LSHIFTRT:
4359 info->special = "mov.b\t%z0,%w0\n\tsub.b\t%x0,%x0\n\tsub.w\t%e0,%e0";
4360 info->shift1 = "shlr.b\t%w0";
4361 info->cc_inline = CC_SET_ZNV;
4362 goto end;
4363 case SHIFT_ASHIFTRT:
4364 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";
4365 info->shift1 = "shar.b\t%w0";
4366 info->cc_inline = CC_SET_ZNV;
4367 goto end;
4370 else if ((TARGET_H8300H && count == 24)
4371 || (TARGET_H8300S && 24 <= count && count <= 25))
4373 info->remainder = count - 24;
4375 switch (shift_type)
4377 case SHIFT_ASHIFT:
4378 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";
4379 goto end;
4380 case SHIFT_LSHIFTRT:
4381 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
4382 info->cc_special = CC_SET_ZNV;
4383 goto end;
4384 case SHIFT_ASHIFTRT:
4385 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
4386 info->cc_special = CC_SET_ZNV;
4387 goto end;
4390 else if (!TARGET_H8300 && count == 28)
4392 switch (shift_type)
4394 case SHIFT_ASHIFT:
4395 if (TARGET_H8300H)
4396 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";
4397 else
4398 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";
4399 goto end;
4400 case SHIFT_LSHIFTRT:
4401 if (TARGET_H8300H)
4403 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";
4404 info->cc_special = CC_SET_ZNV;
4406 else
4407 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4408 goto end;
4409 case SHIFT_ASHIFTRT:
4410 gcc_unreachable ();
4413 else if (!TARGET_H8300 && count == 29)
4415 switch (shift_type)
4417 case SHIFT_ASHIFT:
4418 if (TARGET_H8300H)
4419 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";
4420 else
4421 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4422 goto end;
4423 case SHIFT_LSHIFTRT:
4424 if (TARGET_H8300H)
4426 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";
4427 info->cc_special = CC_SET_ZNV;
4429 else
4431 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4432 info->cc_special = CC_SET_ZNV;
4434 goto end;
4435 case SHIFT_ASHIFTRT:
4436 gcc_unreachable ();
4439 else if (!TARGET_H8300 && count == 30)
4441 switch (shift_type)
4443 case SHIFT_ASHIFT:
4444 if (TARGET_H8300H)
4445 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
4446 else
4447 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
4448 goto end;
4449 case SHIFT_LSHIFTRT:
4450 if (TARGET_H8300H)
4451 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
4452 else
4453 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
4454 goto end;
4455 case SHIFT_ASHIFTRT:
4456 gcc_unreachable ();
4459 else if (count == 31)
4461 if (TARGET_H8300)
4463 switch (shift_type)
4465 case SHIFT_ASHIFT:
4466 info->special = "sub.w\t%e0,%e0\n\tshlr\t%w0\n\tmov.w\t%e0,%f0\n\trotxr\t%z0";
4467 goto end;
4468 case SHIFT_LSHIFTRT:
4469 info->special = "sub.w\t%f0,%f0\n\tshll\t%z0\n\tmov.w\t%f0,%e0\n\trotxl\t%w0";
4470 goto end;
4471 case SHIFT_ASHIFTRT:
4472 info->special = "shll\t%z0\n\tsubx\t%w0,%w0\n\tmov.b\t%w0,%x0\n\tmov.w\t%f0,%e0";
4473 goto end;
4476 else
4478 switch (shift_type)
4480 case SHIFT_ASHIFT:
4481 info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
4482 info->cc_special = CC_SET_ZNV;
4483 goto end;
4484 case SHIFT_LSHIFTRT:
4485 info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
4486 info->cc_special = CC_SET_ZNV;
4487 goto end;
4488 case SHIFT_ASHIFTRT:
4489 info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
4490 info->cc_special = CC_SET_ZNV;
4491 goto end;
4495 gcc_unreachable ();
4497 default:
4498 gcc_unreachable ();
4501 end:
4502 if (!TARGET_H8300S)
4503 info->shift2 = NULL;
4506 /* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
4507 needed for some shift with COUNT and MODE. Return 0 otherwise. */
4510 h8300_shift_needs_scratch_p (int count, enum machine_mode mode)
4512 enum h8_cpu cpu;
4513 int a, lr, ar;
4515 if (GET_MODE_BITSIZE (mode) <= count)
4516 return 1;
4518 /* Find out the target CPU. */
4519 if (TARGET_H8300)
4520 cpu = H8_300;
4521 else if (TARGET_H8300H)
4522 cpu = H8_300H;
4523 else
4524 cpu = H8_S;
4526 /* Find the shift algorithm. */
4527 switch (mode)
4529 case QImode:
4530 a = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
4531 lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
4532 ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
4533 break;
4535 case HImode:
4536 a = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
4537 lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
4538 ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
4539 break;
4541 case SImode:
4542 a = shift_alg_si[cpu][SHIFT_ASHIFT][count];
4543 lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
4544 ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4545 break;
4547 default:
4548 gcc_unreachable ();
4551 /* On H8/300H, count == 8 uses a scratch register. */
4552 return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4553 || (TARGET_H8300H && mode == SImode && count == 8));
4556 /* Output the assembler code for doing shifts. */
4558 const char *
4559 output_a_shift (rtx *operands)
4561 static int loopend_lab;
4562 rtx shift = operands[3];
4563 enum machine_mode mode = GET_MODE (shift);
4564 enum rtx_code code = GET_CODE (shift);
4565 enum shift_type shift_type;
4566 enum shift_mode shift_mode;
4567 struct shift_info info;
4568 int n;
4570 loopend_lab++;
4572 switch (mode)
4574 case QImode:
4575 shift_mode = QIshift;
4576 break;
4577 case HImode:
4578 shift_mode = HIshift;
4579 break;
4580 case SImode:
4581 shift_mode = SIshift;
4582 break;
4583 default:
4584 gcc_unreachable ();
4587 switch (code)
4589 case ASHIFTRT:
4590 shift_type = SHIFT_ASHIFTRT;
4591 break;
4592 case LSHIFTRT:
4593 shift_type = SHIFT_LSHIFTRT;
4594 break;
4595 case ASHIFT:
4596 shift_type = SHIFT_ASHIFT;
4597 break;
4598 default:
4599 gcc_unreachable ();
4602 /* This case must be taken care of by one of the two splitters
4603 that convert a variable shift into a loop. */
4604 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4606 n = INTVAL (operands[2]);
4608 /* If the count is negative, make it 0. */
4609 if (n < 0)
4610 n = 0;
4611 /* If the count is too big, truncate it.
4612 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4613 do the intuitive thing. */
4614 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4615 n = GET_MODE_BITSIZE (mode);
4617 get_shift_alg (shift_type, shift_mode, n, &info);
4619 switch (info.alg)
4621 case SHIFT_SPECIAL:
4622 output_asm_insn (info.special, operands);
4623 /* Fall through. */
4625 case SHIFT_INLINE:
4626 n = info.remainder;
4628 /* Emit two bit shifts first. */
4629 if (info.shift2 != NULL)
4631 for (; n > 1; n -= 2)
4632 output_asm_insn (info.shift2, operands);
4635 /* Now emit one bit shifts for any residual. */
4636 for (; n > 0; n--)
4637 output_asm_insn (info.shift1, operands);
4638 return "";
4640 case SHIFT_ROT_AND:
4642 int m = GET_MODE_BITSIZE (mode) - n;
4643 const int mask = (shift_type == SHIFT_ASHIFT
4644 ? ((1 << m) - 1) << n
4645 : (1 << m) - 1);
4646 char insn_buf[200];
4648 /* Not all possibilities of rotate are supported. They shouldn't
4649 be generated, but let's watch for 'em. */
4650 gcc_assert (info.shift1);
4652 /* Emit two bit rotates first. */
4653 if (info.shift2 != NULL)
4655 for (; m > 1; m -= 2)
4656 output_asm_insn (info.shift2, operands);
4659 /* Now single bit rotates for any residual. */
4660 for (; m > 0; m--)
4661 output_asm_insn (info.shift1, operands);
4663 /* Now mask off the high bits. */
4664 switch (mode)
4666 case QImode:
4667 sprintf (insn_buf, "and\t#%d,%%X0", mask);
4668 break;
4670 case HImode:
4671 gcc_assert (TARGET_H8300H || TARGET_H8300S);
4672 sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4673 break;
4675 default:
4676 gcc_unreachable ();
4679 output_asm_insn (insn_buf, operands);
4680 return "";
4683 case SHIFT_LOOP:
4684 /* A loop to shift by a "large" constant value.
4685 If we have shift-by-2 insns, use them. */
4686 if (info.shift2 != NULL)
4688 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n / 2,
4689 names_big[REGNO (operands[4])]);
4690 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4691 output_asm_insn (info.shift2, operands);
4692 output_asm_insn ("add #0xff,%X4", operands);
4693 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4694 if (n % 2)
4695 output_asm_insn (info.shift1, operands);
4697 else
4699 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n,
4700 names_big[REGNO (operands[4])]);
4701 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4702 output_asm_insn (info.shift1, operands);
4703 output_asm_insn ("add #0xff,%X4", operands);
4704 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4706 return "";
4708 default:
4709 gcc_unreachable ();
4713 /* Count the number of assembly instructions in a string TEMPL. */
4715 static unsigned int
4716 h8300_asm_insn_count (const char *templ)
4718 unsigned int count = 1;
4720 for (; *templ; templ++)
4721 if (*templ == '\n')
4722 count++;
4724 return count;
4727 /* Compute the length of a shift insn. */
4729 unsigned int
4730 compute_a_shift_length (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4732 rtx shift = operands[3];
4733 enum machine_mode mode = GET_MODE (shift);
4734 enum rtx_code code = GET_CODE (shift);
4735 enum shift_type shift_type;
4736 enum shift_mode shift_mode;
4737 struct shift_info info;
4738 unsigned int wlength = 0;
4740 switch (mode)
4742 case QImode:
4743 shift_mode = QIshift;
4744 break;
4745 case HImode:
4746 shift_mode = HIshift;
4747 break;
4748 case SImode:
4749 shift_mode = SIshift;
4750 break;
4751 default:
4752 gcc_unreachable ();
4755 switch (code)
4757 case ASHIFTRT:
4758 shift_type = SHIFT_ASHIFTRT;
4759 break;
4760 case LSHIFTRT:
4761 shift_type = SHIFT_LSHIFTRT;
4762 break;
4763 case ASHIFT:
4764 shift_type = SHIFT_ASHIFT;
4765 break;
4766 default:
4767 gcc_unreachable ();
4770 if (GET_CODE (operands[2]) != CONST_INT)
4772 /* Get the assembler code to do one shift. */
4773 get_shift_alg (shift_type, shift_mode, 1, &info);
4775 return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4777 else
4779 int n = INTVAL (operands[2]);
4781 /* If the count is negative, make it 0. */
4782 if (n < 0)
4783 n = 0;
4784 /* If the count is too big, truncate it.
4785 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4786 do the intuitive thing. */
4787 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4788 n = GET_MODE_BITSIZE (mode);
4790 get_shift_alg (shift_type, shift_mode, n, &info);
4792 switch (info.alg)
4794 case SHIFT_SPECIAL:
4795 wlength += h8300_asm_insn_count (info.special);
4797 /* Every assembly instruction used in SHIFT_SPECIAL case
4798 takes 2 bytes except xor.l, which takes 4 bytes, so if we
4799 see xor.l, we just pretend that xor.l counts as two insns
4800 so that the insn length will be computed correctly. */
4801 if (strstr (info.special, "xor.l") != NULL)
4802 wlength++;
4804 /* Fall through. */
4806 case SHIFT_INLINE:
4807 n = info.remainder;
4809 if (info.shift2 != NULL)
4811 wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4812 n = n % 2;
4815 wlength += h8300_asm_insn_count (info.shift1) * n;
4817 return 2 * wlength;
4819 case SHIFT_ROT_AND:
4821 int m = GET_MODE_BITSIZE (mode) - n;
4823 /* Not all possibilities of rotate are supported. They shouldn't
4824 be generated, but let's watch for 'em. */
4825 gcc_assert (info.shift1);
4827 if (info.shift2 != NULL)
4829 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4830 m = m % 2;
4833 wlength += h8300_asm_insn_count (info.shift1) * m;
4835 /* Now mask off the high bits. */
4836 switch (mode)
4838 case QImode:
4839 wlength += 1;
4840 break;
4841 case HImode:
4842 wlength += 2;
4843 break;
4844 case SImode:
4845 gcc_assert (!TARGET_H8300);
4846 wlength += 3;
4847 break;
4848 default:
4849 gcc_unreachable ();
4851 return 2 * wlength;
4854 case SHIFT_LOOP:
4855 /* A loop to shift by a "large" constant value.
4856 If we have shift-by-2 insns, use them. */
4857 if (info.shift2 != NULL)
4859 wlength += 3 + h8300_asm_insn_count (info.shift2);
4860 if (n % 2)
4861 wlength += h8300_asm_insn_count (info.shift1);
4863 else
4865 wlength += 3 + h8300_asm_insn_count (info.shift1);
4867 return 2 * wlength;
4869 default:
4870 gcc_unreachable ();
4875 /* Compute which flag bits are valid after a shift insn. */
4877 enum attr_cc
4878 compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
4880 rtx shift = operands[3];
4881 enum machine_mode mode = GET_MODE (shift);
4882 enum rtx_code code = GET_CODE (shift);
4883 enum shift_type shift_type;
4884 enum shift_mode shift_mode;
4885 struct shift_info info;
4886 int n;
4888 switch (mode)
4890 case QImode:
4891 shift_mode = QIshift;
4892 break;
4893 case HImode:
4894 shift_mode = HIshift;
4895 break;
4896 case SImode:
4897 shift_mode = SIshift;
4898 break;
4899 default:
4900 gcc_unreachable ();
4903 switch (code)
4905 case ASHIFTRT:
4906 shift_type = SHIFT_ASHIFTRT;
4907 break;
4908 case LSHIFTRT:
4909 shift_type = SHIFT_LSHIFTRT;
4910 break;
4911 case ASHIFT:
4912 shift_type = SHIFT_ASHIFT;
4913 break;
4914 default:
4915 gcc_unreachable ();
4918 /* This case must be taken care of by one of the two splitters
4919 that convert a variable shift into a loop. */
4920 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4922 n = INTVAL (operands[2]);
4924 /* If the count is negative, make it 0. */
4925 if (n < 0)
4926 n = 0;
4927 /* If the count is too big, truncate it.
4928 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4929 do the intuitive thing. */
4930 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4931 n = GET_MODE_BITSIZE (mode);
4933 get_shift_alg (shift_type, shift_mode, n, &info);
4935 switch (info.alg)
4937 case SHIFT_SPECIAL:
4938 if (info.remainder == 0)
4939 return info.cc_special;
4941 /* Fall through. */
4943 case SHIFT_INLINE:
4944 return info.cc_inline;
4946 case SHIFT_ROT_AND:
4947 /* This case always ends with an and instruction. */
4948 return CC_SET_ZNV;
4950 case SHIFT_LOOP:
4951 /* A loop to shift by a "large" constant value.
4952 If we have shift-by-2 insns, use them. */
4953 if (info.shift2 != NULL)
4955 if (n % 2)
4956 return info.cc_inline;
4958 return CC_CLOBBER;
4960 default:
4961 gcc_unreachable ();
4965 /* A rotation by a non-constant will cause a loop to be generated, in
4966 which a rotation by one bit is used. A rotation by a constant,
4967 including the one in the loop, will be taken care of by
4968 output_a_rotate () at the insn emit time. */
4971 expand_a_rotate (rtx operands[])
4973 rtx dst = operands[0];
4974 rtx src = operands[1];
4975 rtx rotate_amount = operands[2];
4976 enum machine_mode mode = GET_MODE (dst);
4978 if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4979 return false;
4981 /* We rotate in place. */
4982 emit_move_insn (dst, src);
4984 if (GET_CODE (rotate_amount) != CONST_INT)
4986 rtx counter = gen_reg_rtx (QImode);
4987 rtx_code_label *start_label = gen_label_rtx ();
4988 rtx_code_label *end_label = gen_label_rtx ();
4990 /* If the rotate amount is less than or equal to 0,
4991 we go out of the loop. */
4992 emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
4993 QImode, 0, end_label);
4995 /* Initialize the loop counter. */
4996 emit_move_insn (counter, rotate_amount);
4998 emit_label (start_label);
5000 /* Rotate by one bit. */
5001 switch (mode)
5003 case QImode:
5004 emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
5005 break;
5006 case HImode:
5007 emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
5008 break;
5009 case SImode:
5010 emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
5011 break;
5012 default:
5013 gcc_unreachable ();
5016 /* Decrement the counter by 1. */
5017 emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
5019 /* If the loop counter is nonzero, we go back to the beginning
5020 of the loop. */
5021 emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
5022 start_label);
5024 emit_label (end_label);
5026 else
5028 /* Rotate by AMOUNT bits. */
5029 switch (mode)
5031 case QImode:
5032 emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
5033 break;
5034 case HImode:
5035 emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
5036 break;
5037 case SImode:
5038 emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
5039 break;
5040 default:
5041 gcc_unreachable ();
5045 return 1;
5048 /* Output a rotate insn. */
5050 const char *
5051 output_a_rotate (enum rtx_code code, rtx *operands)
5053 rtx dst = operands[0];
5054 rtx rotate_amount = operands[2];
5055 enum shift_mode rotate_mode;
5056 enum shift_type rotate_type;
5057 const char *insn_buf;
5058 int bits;
5059 int amount;
5060 enum machine_mode mode = GET_MODE (dst);
5062 gcc_assert (GET_CODE (rotate_amount) == CONST_INT);
5064 switch (mode)
5066 case QImode:
5067 rotate_mode = QIshift;
5068 break;
5069 case HImode:
5070 rotate_mode = HIshift;
5071 break;
5072 case SImode:
5073 rotate_mode = SIshift;
5074 break;
5075 default:
5076 gcc_unreachable ();
5079 switch (code)
5081 case ROTATERT:
5082 rotate_type = SHIFT_ASHIFT;
5083 break;
5084 case ROTATE:
5085 rotate_type = SHIFT_LSHIFTRT;
5086 break;
5087 default:
5088 gcc_unreachable ();
5091 amount = INTVAL (rotate_amount);
5093 /* Clean up AMOUNT. */
5094 if (amount < 0)
5095 amount = 0;
5096 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5097 amount = GET_MODE_BITSIZE (mode);
5099 /* Determine the faster direction. After this phase, amount will be
5100 at most a half of GET_MODE_BITSIZE (mode). */
5101 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5103 /* Flip the direction. */
5104 amount = GET_MODE_BITSIZE (mode) - amount;
5105 rotate_type =
5106 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5109 /* See if a byte swap (in HImode) or a word swap (in SImode) can
5110 boost up the rotation. */
5111 if ((mode == HImode && TARGET_H8300 && amount >= 5)
5112 || (mode == HImode && TARGET_H8300H && amount >= 6)
5113 || (mode == HImode && TARGET_H8300S && amount == 8)
5114 || (mode == SImode && TARGET_H8300H && amount >= 10)
5115 || (mode == SImode && TARGET_H8300S && amount >= 13))
5117 switch (mode)
5119 case HImode:
5120 /* This code works on any family. */
5121 insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
5122 output_asm_insn (insn_buf, operands);
5123 break;
5125 case SImode:
5126 /* This code works on the H8/300H and H8S. */
5127 insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
5128 output_asm_insn (insn_buf, operands);
5129 break;
5131 default:
5132 gcc_unreachable ();
5135 /* Adjust AMOUNT and flip the direction. */
5136 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5137 rotate_type =
5138 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
5141 /* Output rotate insns. */
5142 for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
5144 if (bits == 2)
5145 insn_buf = rotate_two[rotate_type][rotate_mode];
5146 else
5147 insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
5149 for (; amount >= bits; amount -= bits)
5150 output_asm_insn (insn_buf, operands);
5153 return "";
5156 /* Compute the length of a rotate insn. */
5158 unsigned int
5159 compute_a_rotate_length (rtx *operands)
5161 rtx src = operands[1];
5162 rtx amount_rtx = operands[2];
5163 enum machine_mode mode = GET_MODE (src);
5164 int amount;
5165 unsigned int length = 0;
5167 gcc_assert (GET_CODE (amount_rtx) == CONST_INT);
5169 amount = INTVAL (amount_rtx);
5171 /* Clean up AMOUNT. */
5172 if (amount < 0)
5173 amount = 0;
5174 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
5175 amount = GET_MODE_BITSIZE (mode);
5177 /* Determine the faster direction. After this phase, amount
5178 will be at most a half of GET_MODE_BITSIZE (mode). */
5179 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
5180 /* Flip the direction. */
5181 amount = GET_MODE_BITSIZE (mode) - amount;
5183 /* See if a byte swap (in HImode) or a word swap (in SImode) can
5184 boost up the rotation. */
5185 if ((mode == HImode && TARGET_H8300 && amount >= 5)
5186 || (mode == HImode && TARGET_H8300H && amount >= 6)
5187 || (mode == HImode && TARGET_H8300S && amount == 8)
5188 || (mode == SImode && TARGET_H8300H && amount >= 10)
5189 || (mode == SImode && TARGET_H8300S && amount >= 13))
5191 /* Adjust AMOUNT and flip the direction. */
5192 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
5193 length += 6;
5196 /* We use 2-bit rotations on the H8S. */
5197 if (TARGET_H8300S)
5198 amount = amount / 2 + amount % 2;
5200 /* The H8/300 uses three insns to rotate one bit, taking 6
5201 length. */
5202 length += amount * ((TARGET_H8300 && mode == HImode) ? 6 : 2);
5204 return length;
5207 /* Fix the operands of a gen_xxx so that it could become a bit
5208 operating insn. */
5211 fix_bit_operand (rtx *operands, enum rtx_code code)
5213 /* The bit_operand predicate accepts any memory during RTL generation, but
5214 only 'U' memory afterwards, so if this is a MEM operand, we must force
5215 it to be valid for 'U' by reloading the address. */
5217 if (code == AND
5218 ? single_zero_operand (operands[2], QImode)
5219 : single_one_operand (operands[2], QImode))
5221 /* OK to have a memory dest. */
5222 if (GET_CODE (operands[0]) == MEM
5223 && !satisfies_constraint_U (operands[0]))
5225 rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
5226 copy_to_mode_reg (Pmode,
5227 XEXP (operands[0], 0)));
5228 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5229 operands[0] = mem;
5232 if (GET_CODE (operands[1]) == MEM
5233 && !satisfies_constraint_U (operands[1]))
5235 rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
5236 copy_to_mode_reg (Pmode,
5237 XEXP (operands[1], 0)));
5238 MEM_COPY_ATTRIBUTES (mem, operands[0]);
5239 operands[1] = mem;
5241 return 0;
5244 /* Dest and src op must be register. */
5246 operands[1] = force_reg (QImode, operands[1]);
5248 rtx res = gen_reg_rtx (QImode);
5249 switch (code)
5251 case AND:
5252 emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
5253 break;
5254 case IOR:
5255 emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
5256 break;
5257 case XOR:
5258 emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
5259 break;
5260 default:
5261 gcc_unreachable ();
5263 emit_insn (gen_movqi (operands[0], res));
5265 return 1;
5268 /* Return nonzero if FUNC is an interrupt function as specified
5269 by the "interrupt" attribute. */
5271 static int
5272 h8300_interrupt_function_p (tree func)
5274 tree a;
5276 if (TREE_CODE (func) != FUNCTION_DECL)
5277 return 0;
5279 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
5280 return a != NULL_TREE;
5283 /* Return nonzero if FUNC is a saveall function as specified by the
5284 "saveall" attribute. */
5286 static int
5287 h8300_saveall_function_p (tree func)
5289 tree a;
5291 if (TREE_CODE (func) != FUNCTION_DECL)
5292 return 0;
5294 a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
5295 return a != NULL_TREE;
5298 /* Return nonzero if FUNC is an OS_Task function as specified
5299 by the "OS_Task" attribute. */
5301 static int
5302 h8300_os_task_function_p (tree func)
5304 tree a;
5306 if (TREE_CODE (func) != FUNCTION_DECL)
5307 return 0;
5309 a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
5310 return a != NULL_TREE;
5313 /* Return nonzero if FUNC is a monitor function as specified
5314 by the "monitor" attribute. */
5316 static int
5317 h8300_monitor_function_p (tree func)
5319 tree a;
5321 if (TREE_CODE (func) != FUNCTION_DECL)
5322 return 0;
5324 a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
5325 return a != NULL_TREE;
5328 /* Return nonzero if FUNC is a function that should be called
5329 through the function vector. */
5332 h8300_funcvec_function_p (tree func)
5334 tree a;
5336 if (TREE_CODE (func) != FUNCTION_DECL)
5337 return 0;
5339 a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
5340 return a != NULL_TREE;
5343 /* Return nonzero if DECL is a variable that's in the eight bit
5344 data area. */
5347 h8300_eightbit_data_p (tree decl)
5349 tree a;
5351 if (TREE_CODE (decl) != VAR_DECL)
5352 return 0;
5354 a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
5355 return a != NULL_TREE;
5358 /* Return nonzero if DECL is a variable that's in the tiny
5359 data area. */
5362 h8300_tiny_data_p (tree decl)
5364 tree a;
5366 if (TREE_CODE (decl) != VAR_DECL)
5367 return 0;
5369 a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
5370 return a != NULL_TREE;
5373 /* Generate an 'interrupt_handler' attribute for decls. We convert
5374 all the pragmas to corresponding attributes. */
5376 static void
5377 h8300_insert_attributes (tree node, tree *attributes)
5379 if (TREE_CODE (node) == FUNCTION_DECL)
5381 if (pragma_interrupt)
5383 pragma_interrupt = 0;
5385 /* Add an 'interrupt_handler' attribute. */
5386 *attributes = tree_cons (get_identifier ("interrupt_handler"),
5387 NULL, *attributes);
5390 if (pragma_saveall)
5392 pragma_saveall = 0;
5394 /* Add an 'saveall' attribute. */
5395 *attributes = tree_cons (get_identifier ("saveall"),
5396 NULL, *attributes);
5401 /* Supported attributes:
5403 interrupt_handler: output a prologue and epilogue suitable for an
5404 interrupt handler.
5406 saveall: output a prologue and epilogue that saves and restores
5407 all registers except the stack pointer.
5409 function_vector: This function should be called through the
5410 function vector.
5412 eightbit_data: This variable lives in the 8-bit data area and can
5413 be referenced with 8-bit absolute memory addresses.
5415 tiny_data: This variable lives in the tiny data area and can be
5416 referenced with 16-bit absolute memory references. */
5418 static const struct attribute_spec h8300_attribute_table[] =
5420 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
5421 affects_type_identity } */
5422 { "interrupt_handler", 0, 0, true, false, false,
5423 h8300_handle_fndecl_attribute, false },
5424 { "saveall", 0, 0, true, false, false,
5425 h8300_handle_fndecl_attribute, false },
5426 { "OS_Task", 0, 0, true, false, false,
5427 h8300_handle_fndecl_attribute, false },
5428 { "monitor", 0, 0, true, false, false,
5429 h8300_handle_fndecl_attribute, false },
5430 { "function_vector", 0, 0, true, false, false,
5431 h8300_handle_fndecl_attribute, false },
5432 { "eightbit_data", 0, 0, true, false, false,
5433 h8300_handle_eightbit_data_attribute, false },
5434 { "tiny_data", 0, 0, true, false, false,
5435 h8300_handle_tiny_data_attribute, false },
5436 { NULL, 0, 0, false, false, false, NULL, false }
5440 /* Handle an attribute requiring a FUNCTION_DECL; arguments as in
5441 struct attribute_spec.handler. */
5442 static tree
5443 h8300_handle_fndecl_attribute (tree *node, tree name,
5444 tree args ATTRIBUTE_UNUSED,
5445 int flags ATTRIBUTE_UNUSED,
5446 bool *no_add_attrs)
5448 if (TREE_CODE (*node) != FUNCTION_DECL)
5450 warning (OPT_Wattributes, "%qE attribute only applies to functions",
5451 name);
5452 *no_add_attrs = true;
5455 return NULL_TREE;
5458 /* Handle an "eightbit_data" attribute; arguments as in
5459 struct attribute_spec.handler. */
5460 static tree
5461 h8300_handle_eightbit_data_attribute (tree *node, tree name,
5462 tree args ATTRIBUTE_UNUSED,
5463 int flags ATTRIBUTE_UNUSED,
5464 bool *no_add_attrs)
5466 tree decl = *node;
5468 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5470 set_decl_section_name (decl, ".eight");
5472 else
5474 warning (OPT_Wattributes, "%qE attribute ignored",
5475 name);
5476 *no_add_attrs = true;
5479 return NULL_TREE;
5482 /* Handle an "tiny_data" attribute; arguments as in
5483 struct attribute_spec.handler. */
5484 static tree
5485 h8300_handle_tiny_data_attribute (tree *node, tree name,
5486 tree args ATTRIBUTE_UNUSED,
5487 int flags ATTRIBUTE_UNUSED,
5488 bool *no_add_attrs)
5490 tree decl = *node;
5492 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
5494 set_decl_section_name (decl, ".tiny");
5496 else
5498 warning (OPT_Wattributes, "%qE attribute ignored",
5499 name);
5500 *no_add_attrs = true;
5503 return NULL_TREE;
5506 /* Mark function vectors, and various small data objects. */
5508 static void
5509 h8300_encode_section_info (tree decl, rtx rtl, int first)
5511 int extra_flags = 0;
5513 default_encode_section_info (decl, rtl, first);
5515 if (TREE_CODE (decl) == FUNCTION_DECL
5516 && h8300_funcvec_function_p (decl))
5517 extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
5518 else if (TREE_CODE (decl) == VAR_DECL
5519 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
5521 if (h8300_eightbit_data_p (decl))
5522 extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
5523 else if (first && h8300_tiny_data_p (decl))
5524 extra_flags = SYMBOL_FLAG_TINY_DATA;
5527 if (extra_flags)
5528 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
5531 /* Output a single-bit extraction. */
5533 const char *
5534 output_simode_bld (int bild, rtx operands[])
5536 if (TARGET_H8300)
5538 /* Clear the destination register. */
5539 output_asm_insn ("sub.w\t%e0,%e0\n\tsub.w\t%f0,%f0", operands);
5541 /* Now output the bit load or bit inverse load, and store it in
5542 the destination. */
5543 if (bild)
5544 output_asm_insn ("bild\t%Z2,%Y1", operands);
5545 else
5546 output_asm_insn ("bld\t%Z2,%Y1", operands);
5548 output_asm_insn ("bst\t#0,%w0", operands);
5550 else
5552 /* Determine if we can clear the destination first. */
5553 int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5554 && REGNO (operands[0]) != REGNO (operands[1]));
5556 if (clear_first)
5557 output_asm_insn ("sub.l\t%S0,%S0", operands);
5559 /* Output the bit load or bit inverse load. */
5560 if (bild)
5561 output_asm_insn ("bild\t%Z2,%Y1", operands);
5562 else
5563 output_asm_insn ("bld\t%Z2,%Y1", operands);
5565 if (!clear_first)
5566 output_asm_insn ("xor.l\t%S0,%S0", operands);
5568 /* Perform the bit store. */
5569 output_asm_insn ("rotxl.l\t%S0", operands);
5572 /* All done. */
5573 return "";
5576 /* Delayed-branch scheduling is more effective if we have some idea
5577 how long each instruction will be. Use a shorten_branches pass
5578 to get an initial estimate. */
5580 static void
5581 h8300_reorg (void)
5583 if (flag_delayed_branch)
5584 shorten_branches (get_insns ());
5587 #ifndef OBJECT_FORMAT_ELF
5588 static void
5589 h8300_asm_named_section (const char *name, unsigned int flags ATTRIBUTE_UNUSED,
5590 tree decl)
5592 /* ??? Perhaps we should be using default_coff_asm_named_section. */
5593 fprintf (asm_out_file, "\t.section %s\n", name);
5595 #endif /* ! OBJECT_FORMAT_ELF */
5597 /* Nonzero if X is a constant address suitable as an 8-bit absolute,
5598 which is a special case of the 'R' operand. */
5601 h8300_eightbit_constant_address_p (rtx x)
5603 /* The ranges of the 8-bit area. */
5604 const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5605 const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
5606 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5607 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5608 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5609 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5611 unsigned HOST_WIDE_INT addr;
5613 /* We accept symbols declared with eightbit_data. */
5614 if (GET_CODE (x) == SYMBOL_REF)
5615 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
5617 if (GET_CODE (x) != CONST_INT)
5618 return 0;
5620 addr = INTVAL (x);
5622 return (0
5623 || ((TARGET_H8300 || TARGET_NORMAL_MODE) && IN_RANGE (addr, n1, n2))
5624 || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5625 || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5628 /* Nonzero if X is a constant address suitable as an 16-bit absolute
5629 on H8/300H and H8S. */
5632 h8300_tiny_constant_address_p (rtx x)
5634 /* The ranges of the 16-bit area. */
5635 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5636 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5637 const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5638 const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5639 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5640 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5641 const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5642 const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5644 unsigned HOST_WIDE_INT addr;
5646 switch (GET_CODE (x))
5648 case SYMBOL_REF:
5649 /* In the normal mode, any symbol fits in the 16-bit absolute
5650 address range. We also accept symbols declared with
5651 tiny_data. */
5652 return (TARGET_NORMAL_MODE
5653 || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
5655 case CONST_INT:
5656 addr = INTVAL (x);
5657 return (TARGET_NORMAL_MODE
5658 || (TARGET_H8300H
5659 && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5660 || (TARGET_H8300S
5661 && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
5663 case CONST:
5664 return TARGET_NORMAL_MODE;
5666 default:
5667 return 0;
5672 /* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5673 locations that can be accessed as a 16-bit word. */
5676 byte_accesses_mergeable_p (rtx addr1, rtx addr2)
5678 HOST_WIDE_INT offset1, offset2;
5679 rtx reg1, reg2;
5681 if (REG_P (addr1))
5683 reg1 = addr1;
5684 offset1 = 0;
5686 else if (GET_CODE (addr1) == PLUS
5687 && REG_P (XEXP (addr1, 0))
5688 && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5690 reg1 = XEXP (addr1, 0);
5691 offset1 = INTVAL (XEXP (addr1, 1));
5693 else
5694 return 0;
5696 if (REG_P (addr2))
5698 reg2 = addr2;
5699 offset2 = 0;
5701 else if (GET_CODE (addr2) == PLUS
5702 && REG_P (XEXP (addr2, 0))
5703 && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5705 reg2 = XEXP (addr2, 0);
5706 offset2 = INTVAL (XEXP (addr2, 1));
5708 else
5709 return 0;
5711 if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5712 || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5713 && offset1 % 2 == 0
5714 && offset1 + 1 == offset2)
5715 return 1;
5717 return 0;
5720 /* Return nonzero if we have the same comparison insn as I3 two insns
5721 before I3. I3 is assumed to be a comparison insn. */
5724 same_cmp_preceding_p (rtx i3)
5726 rtx_insn *i1, *i2;
5728 /* Make sure we have a sequence of three insns. */
5729 i2 = prev_nonnote_insn (i3);
5730 if (i2 == NULL)
5731 return 0;
5732 i1 = prev_nonnote_insn (i2);
5733 if (i1 == NULL)
5734 return 0;
5736 return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5737 && any_condjump_p (i2) && onlyjump_p (i2));
5740 /* Return nonzero if we have the same comparison insn as I1 two insns
5741 after I1. I1 is assumed to be a comparison insn. */
5744 same_cmp_following_p (rtx i1)
5746 rtx_insn *i2, *i3;
5748 /* Make sure we have a sequence of three insns. */
5749 i2 = next_nonnote_insn (i1);
5750 if (i2 == NULL)
5751 return 0;
5752 i3 = next_nonnote_insn (i2);
5753 if (i3 == NULL)
5754 return 0;
5756 return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5757 && any_condjump_p (i2) && onlyjump_p (i2));
5760 /* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
5761 (or pops) N registers. OPERANDS are assumed to be an array of
5762 registers. */
5765 h8300_regs_ok_for_stm (int n, rtx operands[])
5767 switch (n)
5769 case 2:
5770 return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5771 || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5772 || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5773 case 3:
5774 return ((REGNO (operands[0]) == 0
5775 && REGNO (operands[1]) == 1
5776 && REGNO (operands[2]) == 2)
5777 || (REGNO (operands[0]) == 4
5778 && REGNO (operands[1]) == 5
5779 && REGNO (operands[2]) == 6));
5781 case 4:
5782 return (REGNO (operands[0]) == 0
5783 && REGNO (operands[1]) == 1
5784 && REGNO (operands[2]) == 2
5785 && REGNO (operands[3]) == 3);
5786 default:
5787 gcc_unreachable ();
5791 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
5794 h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5795 unsigned int new_reg)
5797 /* Interrupt functions can only use registers that have already been
5798 saved by the prologue, even if they would normally be
5799 call-clobbered. */
5801 if (h8300_current_function_interrupt_function_p ()
5802 && !df_regs_ever_live_p (new_reg))
5803 return 0;
5805 return 1;
5808 /* Returns true if register REGNO is safe to be allocated as a scratch
5809 register in the current function. */
5811 static bool
5812 h8300_hard_regno_scratch_ok (unsigned int regno)
5814 if (h8300_current_function_interrupt_function_p ()
5815 && ! WORD_REG_USED (regno))
5816 return false;
5818 return true;
5822 /* Return nonzero if X is a REG or SUBREG suitable as a base register. */
5824 static int
5825 h8300_rtx_ok_for_base_p (rtx x, int strict)
5827 /* Strip off SUBREG if any. */
5828 if (GET_CODE (x) == SUBREG)
5829 x = SUBREG_REG (x);
5831 return (REG_P (x)
5832 && (strict
5833 ? REG_OK_FOR_BASE_STRICT_P (x)
5834 : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5837 /* Return nozero if X is a legitimate address. On the H8/300, a
5838 legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5839 CONSTANT_ADDRESS. */
5841 static bool
5842 h8300_legitimate_address_p (enum machine_mode mode, rtx x, bool strict)
5844 /* The register indirect addresses like @er0 is always valid. */
5845 if (h8300_rtx_ok_for_base_p (x, strict))
5846 return 1;
5848 if (CONSTANT_ADDRESS_P (x))
5849 return 1;
5851 if (TARGET_H8300SX
5852 && ( GET_CODE (x) == PRE_INC
5853 || GET_CODE (x) == PRE_DEC
5854 || GET_CODE (x) == POST_INC
5855 || GET_CODE (x) == POST_DEC)
5856 && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5857 return 1;
5859 if (GET_CODE (x) == PLUS
5860 && CONSTANT_ADDRESS_P (XEXP (x, 1))
5861 && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5862 mode, 0), strict))
5863 return 1;
5865 return 0;
5868 /* Worker function for HARD_REGNO_NREGS.
5870 We pretend the MAC register is 32bits -- we don't have any data
5871 types on the H8 series to handle more than 32bits. */
5874 h8300_hard_regno_nregs (int regno ATTRIBUTE_UNUSED, enum machine_mode mode)
5876 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5879 /* Worker function for HARD_REGNO_MODE_OK. */
5882 h8300_hard_regno_mode_ok (int regno, enum machine_mode mode)
5884 if (TARGET_H8300)
5885 /* If an even reg, then anything goes. Otherwise the mode must be
5886 QI or HI. */
5887 return ((regno & 1) == 0) || (mode == HImode) || (mode == QImode);
5888 else
5889 /* MAC register can only be of SImode. Otherwise, anything
5890 goes. */
5891 return regno == MAC_REG ? mode == SImode : 1;
5894 /* Helper function for the move patterns. Make sure a move is legitimate. */
5896 bool
5897 h8300_move_ok (rtx dest, rtx src)
5899 rtx addr, other;
5901 /* Validate that at least one operand is a register. */
5902 if (MEM_P (dest))
5904 if (MEM_P (src) || CONSTANT_P (src))
5905 return false;
5906 addr = XEXP (dest, 0);
5907 other = src;
5909 else if (MEM_P (src))
5911 addr = XEXP (src, 0);
5912 other = dest;
5914 else
5915 return true;
5917 /* Validate that auto-inc doesn't affect OTHER. */
5918 if (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC)
5919 return true;
5920 addr = XEXP (addr, 0);
5922 if (addr == stack_pointer_rtx)
5923 return register_no_sp_elim_operand (other, VOIDmode);
5924 else
5925 return !reg_overlap_mentioned_p(other, addr);
5928 /* Perform target dependent optabs initialization. */
5929 static void
5930 h8300_init_libfuncs (void)
5932 set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5933 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5934 set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5935 set_optab_libfunc (smod_optab, HImode, "__modhi3");
5936 set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5939 /* Worker function for TARGET_FUNCTION_VALUE.
5941 On the H8 the return value is in R0/R1. */
5943 static rtx
5944 h8300_function_value (const_tree ret_type,
5945 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
5946 bool outgoing ATTRIBUTE_UNUSED)
5948 return gen_rtx_REG (TYPE_MODE (ret_type), R0_REG);
5951 /* Worker function for TARGET_LIBCALL_VALUE.
5953 On the H8 the return value is in R0/R1. */
5955 static rtx
5956 h8300_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5958 return gen_rtx_REG (mode, R0_REG);
5961 /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P.
5963 On the H8, R0 is the only register thus used. */
5965 static bool
5966 h8300_function_value_regno_p (const unsigned int regno)
5968 return (regno == R0_REG);
5971 /* Worker function for TARGET_RETURN_IN_MEMORY. */
5973 static bool
5974 h8300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
5976 return (TYPE_MODE (type) == BLKmode
5977 || GET_MODE_SIZE (TYPE_MODE (type)) > (TARGET_H8300 ? 4 : 8));
5980 /* We emit the entire trampoline here. Depending on the pointer size,
5981 we use a different trampoline.
5983 Pmode == HImode
5984 vvvv context
5985 1 0000 7903xxxx mov.w #0x1234,r3
5986 2 0004 5A00xxxx jmp @0x1234
5987 ^^^^ function
5989 Pmode == SImode
5990 vvvvvvvv context
5991 2 0000 7A03xxxxxxxx mov.l #0x12345678,er3
5992 3 0006 5Axxxxxx jmp @0x123456
5993 ^^^^^^ function
5996 static void
5997 h8300_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
5999 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
6000 rtx mem;
6002 if (Pmode == HImode)
6004 mem = adjust_address (m_tramp, HImode, 0);
6005 emit_move_insn (mem, GEN_INT (0x7903));
6006 mem = adjust_address (m_tramp, Pmode, 2);
6007 emit_move_insn (mem, cxt);
6008 mem = adjust_address (m_tramp, HImode, 4);
6009 emit_move_insn (mem, GEN_INT (0x5a00));
6010 mem = adjust_address (m_tramp, Pmode, 6);
6011 emit_move_insn (mem, fnaddr);
6013 else
6015 rtx tem;
6017 mem = adjust_address (m_tramp, HImode, 0);
6018 emit_move_insn (mem, GEN_INT (0x7a03));
6019 mem = adjust_address (m_tramp, Pmode, 2);
6020 emit_move_insn (mem, cxt);
6022 tem = copy_to_reg (fnaddr);
6023 emit_insn (gen_andsi3 (tem, tem, GEN_INT (0x00ffffff)));
6024 emit_insn (gen_iorsi3 (tem, tem, GEN_INT (0x5a000000)));
6025 mem = adjust_address (m_tramp, SImode, 6);
6026 emit_move_insn (mem, tem);
6030 /* Initialize the GCC target structure. */
6031 #undef TARGET_ATTRIBUTE_TABLE
6032 #define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
6034 #undef TARGET_ASM_ALIGNED_HI_OP
6035 #define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
6037 #undef TARGET_ASM_FILE_START
6038 #define TARGET_ASM_FILE_START h8300_file_start
6039 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
6040 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
6042 #undef TARGET_ASM_FILE_END
6043 #define TARGET_ASM_FILE_END h8300_file_end
6045 #undef TARGET_PRINT_OPERAND
6046 #define TARGET_PRINT_OPERAND h8300_print_operand
6047 #undef TARGET_PRINT_OPERAND_ADDRESS
6048 #define TARGET_PRINT_OPERAND_ADDRESS h8300_print_operand_address
6049 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
6050 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P h8300_print_operand_punct_valid_p
6052 #undef TARGET_ENCODE_SECTION_INFO
6053 #define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
6055 #undef TARGET_INSERT_ATTRIBUTES
6056 #define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
6058 #undef TARGET_REGISTER_MOVE_COST
6059 #define TARGET_REGISTER_MOVE_COST h8300_register_move_cost
6061 #undef TARGET_RTX_COSTS
6062 #define TARGET_RTX_COSTS h8300_rtx_costs
6064 #undef TARGET_INIT_LIBFUNCS
6065 #define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
6067 #undef TARGET_FUNCTION_VALUE
6068 #define TARGET_FUNCTION_VALUE h8300_function_value
6070 #undef TARGET_LIBCALL_VALUE
6071 #define TARGET_LIBCALL_VALUE h8300_libcall_value
6073 #undef TARGET_FUNCTION_VALUE_REGNO_P
6074 #define TARGET_FUNCTION_VALUE_REGNO_P h8300_function_value_regno_p
6076 #undef TARGET_RETURN_IN_MEMORY
6077 #define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
6079 #undef TARGET_FUNCTION_ARG
6080 #define TARGET_FUNCTION_ARG h8300_function_arg
6082 #undef TARGET_FUNCTION_ARG_ADVANCE
6083 #define TARGET_FUNCTION_ARG_ADVANCE h8300_function_arg_advance
6085 #undef TARGET_MACHINE_DEPENDENT_REORG
6086 #define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
6088 #undef TARGET_HARD_REGNO_SCRATCH_OK
6089 #define TARGET_HARD_REGNO_SCRATCH_OK h8300_hard_regno_scratch_ok
6091 #undef TARGET_LEGITIMATE_ADDRESS_P
6092 #define TARGET_LEGITIMATE_ADDRESS_P h8300_legitimate_address_p
6094 #undef TARGET_CAN_ELIMINATE
6095 #define TARGET_CAN_ELIMINATE h8300_can_eliminate
6097 #undef TARGET_CONDITIONAL_REGISTER_USAGE
6098 #define TARGET_CONDITIONAL_REGISTER_USAGE h8300_conditional_register_usage
6100 #undef TARGET_TRAMPOLINE_INIT
6101 #define TARGET_TRAMPOLINE_INIT h8300_trampoline_init
6103 #undef TARGET_OPTION_OVERRIDE
6104 #define TARGET_OPTION_OVERRIDE h8300_option_override
6106 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
6107 #define TARGET_MODE_DEPENDENT_ADDRESS_P h8300_mode_dependent_address_p
6109 struct gcc_target targetm = TARGET_INITIALIZER;