]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/h8300/h8300.c
i386: Add PPERM two-operand 64bit vector permutation [PR89021]
[thirdparty/gcc.git] / gcc / config / h8300 / h8300.c
CommitLineData
340f6494 1/* Subroutines for insn-output.c for Renesas H8/300.
99dee823 2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
48837e29
DE
3 Contributed by Steve Chamberlain (sac@cygnus.com),
4 Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
07aae5c2 5
8aa063fb 6This file is part of GCC.
07aae5c2 7
8aa063fb 8GCC is free software; you can redistribute it and/or modify
07aae5c2 9it under the terms of the GNU General Public License as published by
2f83c7d6 10the Free Software Foundation; either version 3, or (at your option)
07aae5c2
SC
11any later version.
12
8aa063fb 13GCC is distributed in the hope that it will be useful,
07aae5c2
SC
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
2f83c7d6
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
07aae5c2 21
8fcc61f8
RS
22#define IN_TARGET_CODE 1
23
07aae5c2 24#include "config.h"
c5c76735 25#include "system.h"
4977bab6 26#include "coretypes.h"
c7131fb2 27#include "backend.h"
e11c4407 28#include "target.h"
07aae5c2 29#include "rtl.h"
e11c4407 30#include "tree.h"
c7131fb2 31#include "df.h"
4d0cdd0c 32#include "memmodel.h"
e11c4407
AM
33#include "tm_p.h"
34#include "stringpool.h"
314e6352 35#include "attribs.h"
e11c4407
AM
36#include "optabs.h"
37#include "regs.h"
38#include "emit-rtl.h"
39#include "recog.h"
40#include "diagnostic-core.h"
40e23961 41#include "alias.h"
d8a2d370
DN
42#include "stor-layout.h"
43#include "varasm.h"
44#include "calls.h"
07aae5c2 45#include "conditions.h"
07aae5c2
SC
46#include "output.h"
47#include "insn-attr.h"
48#include "flags.h"
36566b39 49#include "explow.h"
36566b39 50#include "expr.h"
ceaaaeab 51#include "tm-constrs.h"
9b2b7279 52#include "builtins.h"
07aae5c2 53
994c5d85 54/* This file should be included last. */
d58627a0
RS
55#include "target-def.h"
56
beed8fc0
AO
57/* Classifies a h8300_src_operand or h8300_dst_operand.
58
59 H8OP_IMMEDIATE
60 A constant operand of some sort.
61
62 H8OP_REGISTER
63 An ordinary register.
64
65 H8OP_MEM_ABSOLUTE
66 A memory reference with a constant address.
67
68 H8OP_MEM_BASE
69 A memory reference with a register as its address.
70
71 H8OP_MEM_COMPLEX
72 Some other kind of memory reference. */
73enum h8300_operand_class
74{
75 H8OP_IMMEDIATE,
76 H8OP_REGISTER,
77 H8OP_MEM_ABSOLUTE,
78 H8OP_MEM_BASE,
79 H8OP_MEM_COMPLEX,
80 NUM_H8OPS
81};
82
beed8fc0
AO
83/* For a general two-operand instruction, element [X][Y] gives
84 the length of the opcode fields when the first operand has class
85 (X + 1) and the second has class Y. */
86typedef unsigned char h8300_length_table[NUM_H8OPS - 1][NUM_H8OPS];
87
07aae5c2 88/* Forward declarations. */
cb713a8d
KH
89static const char *byte_reg (rtx, int);
90static int h8300_interrupt_function_p (tree);
3cfa3702 91static int h8300_saveall_function_p (tree);
cb713a8d
KH
92static int h8300_monitor_function_p (tree);
93static int h8300_os_task_function_p (tree);
12bb62fb 94static void h8300_emit_stack_adjustment (int, HOST_WIDE_INT);
e68d4dd1 95static HOST_WIDE_INT round_frame_size (HOST_WIDE_INT);
cb713a8d 96static unsigned int compute_saved_regs (void);
cb713a8d
KH
97static const char *cond_string (enum rtx_code);
98static unsigned int h8300_asm_insn_count (const char *);
cb713a8d
KH
99static tree h8300_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
100static tree h8300_handle_eightbit_data_attribute (tree *, tree, tree, int, bool *);
101static tree h8300_handle_tiny_data_attribute (tree *, tree, tree, int, bool *);
cc8ca59e 102static void h8300_print_operand_address (FILE *, machine_mode, rtx);
88cb339e
N
103static void h8300_print_operand (FILE *, rtx, int);
104static bool h8300_print_operand_punct_valid_p (unsigned char code);
ef4bddc2 105static int h8300_register_move_cost (machine_mode, reg_class_t, reg_class_t);
cb713a8d
KH
106static int h8300_and_costs (rtx);
107static int h8300_shift_costs (rtx);
ac447f25 108static void h8300_push_pop (int, int, bool, bool);
beed8fc0
AO
109static int h8300_stack_offset_p (rtx, int);
110static int h8300_ldm_stm_regno (rtx, int, int, int);
beed8fc0
AO
111static void h8300_reorg (void);
112static unsigned int h8300_constant_length (rtx);
113static unsigned int h8300_displacement_length (rtx, int);
114static unsigned int h8300_classify_operand (rtx, int, enum h8300_operand_class *);
115static unsigned int h8300_length_from_table (rtx, rtx, const h8300_length_table *);
116static unsigned int h8300_unary_length (rtx);
117static unsigned int h8300_short_immediate_length (rtx);
118static unsigned int h8300_bitfield_length (rtx, rtx);
e7041fcc 119static unsigned int h8300_binary_length (rtx_insn *, const h8300_length_table *);
beed8fc0
AO
120static bool h8300_short_move_mem_p (rtx, enum rtx_code);
121static unsigned int h8300_move_length (rtx *, const h8300_length_table *);
2e762884 122static bool h8300_hard_regno_scratch_ok (unsigned int);
ef4bddc2 123static rtx h8300_get_index (rtx, machine_mode mode, int *);
f5b65a56 124
48837e29
DE
125/* CPU_TYPE, says what cpu we're compiling for. */
126int cpu_type;
127
e392d367
KH
128/* True if a #pragma interrupt has been seen for the current function. */
129static int pragma_interrupt;
07aae5c2
SC
130
131/* True if a #pragma saveall has been seen for the current function. */
0869f126 132static int pragma_saveall;
07aae5c2 133
441d04c6 134static const char *const names_big[] =
f16897cb 135{ "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "cc" };
48837e29 136
441d04c6 137static const char *const names_extended[] =
f16897cb 138{ "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7", "cc" };
48837e29 139
441d04c6 140static const char *const names_upper_extended[] =
f16897cb 141{ "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "cc" };
48837e29
DE
142
143/* Points to one of the above. */
144/* ??? The above could be put in an array indexed by CPU_TYPE. */
441d04c6 145const char * const *h8_reg_names;
48837e29
DE
146
147/* Various operations needed by the following, indexed by CPU_TYPE. */
48837e29 148
441d04c6 149const char *h8_push_op, *h8_pop_op, *h8_mov_op;
dc66a1c4 150
beed8fc0
AO
151/* Value of MOVE_RATIO. */
152int h8300_move_ratio;
672a6f42 153\f
c4dfc70c
DD
154/* See below where shifts are handled for explanation of this enum. */
155
156enum shift_alg
157{
158 SHIFT_INLINE,
159 SHIFT_ROT_AND,
160 SHIFT_SPECIAL,
161 SHIFT_LOOP
162};
163
164/* Symbols of the various shifts which can be used as indices. */
165
166enum shift_type
167{
168 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
169};
170
171/* Macros to keep the shift algorithm tables small. */
172#define INL SHIFT_INLINE
173#define ROT SHIFT_ROT_AND
174#define LOP SHIFT_LOOP
175#define SPC SHIFT_SPECIAL
176
177/* The shift algorithms for each machine, mode, shift type, and shift
178 count are defined below. The three tables below correspond to
179 QImode, HImode, and SImode, respectively. Each table is organized
f411c849 180 by, in the order of indices, machine, shift type, and shift count. */
c4dfc70c 181
27c1afd5 182static enum shift_alg shift_alg_qi[2][3][8] = {
c4dfc70c
DD
183 {
184 /* TARGET_H8300H */
185 /* 0 1 2 3 4 5 6 7 */
186 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
187 { INL, INL, INL, INL, INL, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
188 { INL, INL, INL, INL, INL, LOP, LOP, SPC } /* SHIFT_ASHIFTRT */
189 },
190 {
191 /* TARGET_H8300S */
192 /* 0 1 2 3 4 5 6 7 */
193 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_ASHIFT */
194 { INL, INL, INL, INL, INL, INL, ROT, ROT }, /* SHIFT_LSHIFTRT */
195 { INL, INL, INL, INL, INL, INL, INL, SPC } /* SHIFT_ASHIFTRT */
196 }
197};
198
27c1afd5 199static enum shift_alg shift_alg_hi[2][3][16] = {
c4dfc70c
DD
200 {
201 /* TARGET_H8300H */
202 /* 0 1 2 3 4 5 6 7 */
203 /* 8 9 10 11 12 13 14 15 */
204 { INL, INL, INL, INL, INL, INL, INL, SPC,
205 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
206 { INL, INL, INL, INL, INL, INL, INL, SPC,
207 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
208 { INL, INL, INL, INL, INL, INL, INL, SPC,
209 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
210 },
211 {
212 /* TARGET_H8300S */
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, INL,
216 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_ASHIFT */
217 { INL, INL, INL, INL, INL, INL, INL, INL,
218 SPC, SPC, SPC, SPC, SPC, ROT, ROT, ROT }, /* SHIFT_LSHIFTRT */
219 { INL, INL, INL, INL, INL, INL, INL, INL,
220 SPC, SPC, SPC, SPC, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFTRT */
221 }
222};
223
27c1afd5 224static enum shift_alg shift_alg_si[2][3][32] = {
c4dfc70c
DD
225 {
226 /* TARGET_H8300H */
227 /* 0 1 2 3 4 5 6 7 */
228 /* 8 9 10 11 12 13 14 15 */
229 /* 16 17 18 19 20 21 22 23 */
230 /* 24 25 26 27 28 29 30 31 */
231 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
232 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
233 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
1e5bdc40 234 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
c4dfc70c
DD
235 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
236 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC,
237 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
1e5bdc40 238 SPC, LOP, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
c4dfc70c
DD
239 { INL, INL, INL, INL, INL, LOP, LOP, LOP,
240 SPC, LOP, LOP, LOP, LOP, LOP, LOP, LOP,
241 SPC, SPC, SPC, SPC, LOP, LOP, LOP, LOP,
242 SPC, LOP, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
243 },
244 {
245 /* TARGET_H8300S */
246 /* 0 1 2 3 4 5 6 7 */
247 /* 8 9 10 11 12 13 14 15 */
248 /* 16 17 18 19 20 21 22 23 */
249 /* 24 25 26 27 28 29 30 31 */
250 { INL, INL, INL, INL, INL, INL, INL, INL,
251 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
252 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
1e5bdc40 253 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_ASHIFT */
c4dfc70c
DD
254 { INL, INL, INL, INL, INL, INL, INL, INL,
255 INL, INL, INL, LOP, LOP, LOP, LOP, SPC,
256 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
1e5bdc40 257 SPC, SPC, LOP, LOP, SPC, SPC, SPC, SPC }, /* SHIFT_LSHIFTRT */
c4dfc70c
DD
258 { INL, INL, INL, INL, INL, INL, INL, INL,
259 INL, INL, INL, LOP, LOP, LOP, LOP, LOP,
260 SPC, SPC, SPC, SPC, SPC, SPC, LOP, LOP,
261 SPC, SPC, LOP, LOP, LOP, LOP, LOP, SPC }, /* SHIFT_ASHIFTRT */
262 }
263};
264
265#undef INL
266#undef ROT
267#undef LOP
268#undef SPC
269
270enum h8_cpu
271{
c4dfc70c
DD
272 H8_300H,
273 H8_S
274};
275
48837e29
DE
276/* Initialize various cpu specific globals at start up. */
277
c5387660
JM
278static void
279h8300_option_override (void)
48837e29 280{
cbf1b2da
KH
281 static const char *const h8_push_ops[2] = { "push" , "push.l" };
282 static const char *const h8_pop_ops[2] = { "pop" , "pop.l" };
283 static const char *const h8_mov_ops[2] = { "mov.w", "mov.l" };
284
27c1afd5
JL
285 /* For this we treat the H8/300H and H8S the same. */
286 cpu_type = (int) CPU_H8300H;
287 h8_reg_names = names_extended;
48837e29
DE
288 h8_push_op = h8_push_ops[cpu_type];
289 h8_pop_op = h8_pop_ops[cpu_type];
290 h8_mov_op = h8_mov_ops[cpu_type];
17f0f8fa 291
27c1afd5
JL
292 /* If we're compiling for the H8/S, then turn off H8/300H. */
293 if (TARGET_H8300S)
294 target_flags &= ~MASK_H8300H;
295
17f0f8fa 296 if (!TARGET_H8300S && TARGET_MAC)
400500c4 297 {
a3f9f006 298 error ("%<-ms2600%> is used without %<-ms%>");
5463c726 299 target_flags |= MASK_H8300S_1;
400500c4 300 }
920e86b8 301
f4ea8112
SKS
302 if (! TARGET_H8300S && TARGET_EXR)
303 {
a3f9f006 304 error ("%<-mexr%> is used without %<-ms%>");
f4ea8112
SKS
305 target_flags |= MASK_H8300S_1;
306 }
307
f4ea8112
SKS
308 if ((!TARGET_H8300S && TARGET_EXR) && (!TARGET_H8300SX && TARGET_EXR))
309 {
a3f9f006 310 error ("%<-mexr%> is used without %<-ms%> or %<-msx%>");
f4ea8112
SKS
311 target_flags |= MASK_H8300S_1;
312 }
313
314 if ((!TARGET_H8300S && TARGET_NEXR) && (!TARGET_H8300SX && TARGET_NEXR))
315 {
dfa075d0
JL
316 warning (OPT_mno_exr, "%<-mno-exr%> is valid only with %<-ms%> or "
317 "%<-msx%> - option ignored");
f4ea8112
SKS
318 }
319
2f6bd6eb
YS
320#ifdef H8300_LINUX
321 if ((TARGET_NORMAL_MODE))
322 {
a3f9f006 323 error ("%<-mn%> is not supported for linux targets");
2f6bd6eb
YS
324 target_flags ^= MASK_NORMAL_MODE;
325 }
326#endif
327
8bd06267 328 /* Some of the shifts are optimized for speed by default.
c4dfc70c 329 See http://gcc.gnu.org/ml/gcc-patches/2002-07/msg01858.html
8bd06267 330 If optimizing for size, change shift_alg for those shift to
c4dfc70c 331 SHIFT_LOOP. */
b6894857 332 if (optimize_size)
c4dfc70c 333 {
b6894857
KH
334 /* H8/300H */
335 shift_alg_hi[H8_300H][SHIFT_ASHIFT][5] = SHIFT_LOOP;
336 shift_alg_hi[H8_300H][SHIFT_ASHIFT][6] = SHIFT_LOOP;
c4dfc70c 337
b6894857
KH
338 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][5] = SHIFT_LOOP;
339 shift_alg_hi[H8_300H][SHIFT_LSHIFTRT][6] = SHIFT_LOOP;
c4dfc70c 340
b6894857
KH
341 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][5] = SHIFT_LOOP;
342 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][6] = SHIFT_LOOP;
343 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][13] = SHIFT_LOOP;
344 shift_alg_hi[H8_300H][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
c4dfc70c
DD
345
346 /* H8S */
b6894857 347 shift_alg_hi[H8_S][SHIFT_ASHIFTRT][14] = SHIFT_LOOP;
c4dfc70c 348 }
beed8fc0
AO
349
350 /* Work out a value for MOVE_RATIO. */
351 if (!TARGET_H8300SX)
352 {
353 /* Memory-memory moves are quite expensive without the
354 h8sx instructions. */
355 h8300_move_ratio = 3;
356 }
357 else if (flag_omit_frame_pointer)
358 {
359 /* movmd sequences are fairly cheap when er6 isn't fixed. They can
360 sometimes be as short as two individual memory-to-memory moves,
361 but since they use all the call-saved registers, it seems better
362 to allow up to three moves here. */
363 h8300_move_ratio = 4;
364 }
365 else if (optimize_size)
366 {
367 /* In this case we don't use movmd sequences since they tend
368 to be longer than calls to memcpy(). Memory-to-memory
369 moves are cheaper than for !TARGET_H8300SX, so it makes
370 sense to have a slightly higher threshold. */
371 h8300_move_ratio = 4;
372 }
373 else
374 {
375 /* We use movmd sequences for some moves since it can be quicker
376 than calling memcpy(). The sequences will need to save and
377 restore er6 though, so bump up the cost. */
378 h8300_move_ratio = 6;
379 }
0685e770
DD
380
381 /* This target defaults to strict volatile bitfields. */
36acc1a2 382 if (flag_strict_volatile_bitfields < 0 && abi_version_at_least(2))
0685e770 383 flag_strict_volatile_bitfields = 1;
beed8fc0
AO
384}
385
e9eba255
KH
386/* Return the byte register name for a register rtx X. B should be 0
387 if you want a lower byte register. B should be 1 if you want an
388 upper byte register. */
389
9c188705 390static const char *
cb713a8d 391byte_reg (rtx x, int b)
07aae5c2 392{
9cbcd983
KH
393 static const char *const names_small[] = {
394 "r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
395 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7l", "r7h"
396 };
07aae5c2 397
8c440872 398 gcc_assert (REG_P (x));
500fc80f 399
07aae5c2
SC
400 return names_small[REGNO (x) * 2 + b];
401}
402
403/* REGNO must be saved/restored across calls if this macro is true. */
48837e29 404
9cbcd983 405#define WORD_REG_USED(regno) \
d60004ee 406 (regno < SP_REG \
9cbcd983
KH
407 /* No need to save registers if this function will not return. */ \
408 && ! TREE_THIS_VOLATILE (current_function_decl) \
3cfa3702 409 && (h8300_saveall_function_p (current_function_decl) \
9cbcd983 410 /* Save any call saved register that was used. */ \
a365fa06
RS
411 || (df_regs_ever_live_p (regno) \
412 && !call_used_or_fixed_reg_p (regno)) \
9cbcd983 413 /* Save the frame pointer if it was used. */ \
6fb5fa3c 414 || (regno == HARD_FRAME_POINTER_REGNUM && df_regs_ever_live_p (regno)) \
9cbcd983 415 /* Save any register used in an interrupt handler. */ \
e392d367 416 || (h8300_current_function_interrupt_function_p () \
6fb5fa3c 417 && df_regs_ever_live_p (regno)) \
9cbcd983
KH
418 /* Save call clobbered registers in non-leaf interrupt \
419 handlers. */ \
e392d367 420 || (h8300_current_function_interrupt_function_p () \
a365fa06 421 && call_used_or_fixed_reg_p (regno) \
416ff32e 422 && !crtl->is_leaf)))
07aae5c2 423
18674659 424/* We use this to wrap all emitted insns in the prologue. */
e7041fcc
DM
425static rtx_insn *
426F (rtx_insn *x, bool set_it)
18674659 427{
c72ea086
DD
428 if (set_it)
429 RTX_FRAME_RELATED_P (x) = 1;
18674659
DD
430 return x;
431}
432
433/* Mark all the subexpressions of the PARALLEL rtx PAR as
434 frame-related. Return PAR.
435
436 dwarf2out.c:dwarf2out_frame_debug_expr ignores sub-expressions of a
437 PARALLEL rtx other than the first if they do not have the
438 FRAME_RELATED flag set on them. */
439static rtx
440Fpa (rtx par)
441{
442 int len = XVECLEN (par, 0);
443 int i;
444
445 for (i = 0; i < len; i++)
f698a4b1 446 RTX_FRAME_RELATED_P (XVECEXP (par, 0, i)) = 1;
18674659
DD
447
448 return par;
449}
450
07aae5c2 451/* Output assembly language to FILE for the operation OP with operand size
48837e29 452 SIZE to adjust the stack pointer. */
48837e29 453
07aae5c2 454static void
12bb62fb 455h8300_emit_stack_adjustment (int sign, HOST_WIDE_INT size)
07aae5c2 456{
72b1de44
KH
457 /* If the frame size is 0, we don't have anything to do. */
458 if (size == 0)
f8b58e56 459 return;
72b1de44 460
27c1afd5
JL
461 /* The stack adjustment made here is further optimized by the
462 splitter. In case of H8/300, the splitter always splits the
463 addition emitted here to make the adjustment interrupt-safe.
464 FIXME: We don't always tag those, because we don't know what
465 the splitter will do. */
466 if (Pmode == HImode)
f8f26adc 467 {
27c1afd5
JL
468 rtx_insn *x = emit_insn (gen_addhi3 (stack_pointer_rtx,
469 stack_pointer_rtx,
470 GEN_INT (sign * size)));
471 if (size < 4)
f16897cb 472 F (x, 0);
7b3d4613
KH
473 }
474 else
27c1afd5 475 F (emit_insn (gen_addsi3 (stack_pointer_rtx,
f16897cb 476 stack_pointer_rtx, GEN_INT (sign * size))), 0);
07aae5c2
SC
477}
478
8682223f
KH
479/* Round up frame size SIZE. */
480
e68d4dd1
UB
481static HOST_WIDE_INT
482round_frame_size (HOST_WIDE_INT size)
8682223f 483{
489eda65
KH
484 return ((size + STACK_BOUNDARY / BITS_PER_UNIT - 1)
485 & -STACK_BOUNDARY / BITS_PER_UNIT);
8682223f
KH
486}
487
488/* Compute which registers to push/pop.
489 Return a bit vector of registers. */
490
491static unsigned int
cb713a8d 492compute_saved_regs (void)
8682223f
KH
493{
494 unsigned int saved_regs = 0;
495 int regno;
496
497 /* Construct a bit vector of registers to be pushed/popped. */
1807b726 498 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
8682223f
KH
499 {
500 if (WORD_REG_USED (regno))
501 saved_regs |= 1 << regno;
502 }
503
504 /* Don't push/pop the frame pointer as it is treated separately. */
505 if (frame_pointer_needed)
1807b726 506 saved_regs &= ~(1 << HARD_FRAME_POINTER_REGNUM);
8682223f
KH
507
508 return saved_regs;
509}
510
68ee6df6 511/* Emit an insn to push register RN. */
8682223f 512
8f1594b2 513static rtx
12bb62fb 514push (int rn)
8682223f 515{
68ee6df6
KH
516 rtx reg = gen_rtx_REG (word_mode, rn);
517 rtx x;
518
27c1afd5 519 if (!TARGET_NORMAL_MODE)
32da7865 520 x = gen_push_h8300hs_advanced (reg);
f24f0897
KH
521 else
522 x = gen_push_h8300hs_normal (reg);
f16897cb 523 x = F (emit_insn (x), 0);
9690aa8e 524 add_reg_note (x, REG_INC, stack_pointer_rtx);
8f1594b2 525 return x;
8682223f
KH
526}
527
68ee6df6 528/* Emit an insn to pop register RN. */
8682223f 529
8f1594b2 530static rtx
cb713a8d 531pop (int rn)
8682223f 532{
68ee6df6
KH
533 rtx reg = gen_rtx_REG (word_mode, rn);
534 rtx x;
535
27c1afd5 536 if (!TARGET_NORMAL_MODE)
32da7865 537 x = gen_pop_h8300hs_advanced (reg);
f24f0897
KH
538 else
539 x = gen_pop_h8300hs_normal (reg);
68ee6df6 540 x = emit_insn (x);
9690aa8e 541 add_reg_note (x, REG_INC, stack_pointer_rtx);
8f1594b2 542 return x;
8682223f 543}
07aae5c2 544
beed8fc0
AO
545/* Emit an instruction to push or pop NREGS consecutive registers
546 starting at register REGNO. POP_P selects a pop rather than a
547 push and RETURN_P is true if the instruction should return.
548
549 It must be possible to do the requested operation in a single
550 instruction. If NREGS == 1 && !RETURN_P, use a normal push
551 or pop insn. Otherwise emit a parallel of the form:
552
553 (parallel
554 [(return) ;; if RETURN_P
555 (save or restore REGNO)
556 (save or restore REGNO + 1)
557 ...
558 (save or restore REGNO + NREGS - 1)
559 (set sp (plus sp (const_int adjust)))] */
560
561static void
ac447f25 562h8300_push_pop (int regno, int nregs, bool pop_p, bool return_p)
beed8fc0
AO
563{
564 int i, j;
565 rtvec vec;
18674659 566 rtx sp, offset, x;
beed8fc0
AO
567
568 /* See whether we can use a simple push or pop. */
569 if (!return_p && nregs == 1)
570 {
571 if (pop_p)
572 pop (regno);
573 else
12bb62fb 574 push (regno);
beed8fc0
AO
575 return;
576 }
577
578 /* We need one element for the return insn, if present, one for each
579 register, and one for stack adjustment. */
ac447f25 580 vec = rtvec_alloc ((return_p ? 1 : 0) + nregs + 1);
beed8fc0
AO
581 sp = stack_pointer_rtx;
582 i = 0;
583
584 /* Add the return instruction. */
585 if (return_p)
586 {
3810076b 587 RTVEC_ELT (vec, i) = ret_rtx;
beed8fc0
AO
588 i++;
589 }
590
591 /* Add the register moves. */
592 for (j = 0; j < nregs; j++)
593 {
594 rtx lhs, rhs;
595
596 if (pop_p)
597 {
598 /* Register REGNO + NREGS - 1 is popped first. Before the
599 stack adjustment, its slot is at address @sp. */
600 lhs = gen_rtx_REG (SImode, regno + j);
0a81f074
RS
601 rhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp,
602 (nregs - j - 1) * 4));
beed8fc0
AO
603 }
604 else
605 {
606 /* Register REGNO is pushed first and will be stored at @(-4,sp). */
0a81f074 607 lhs = gen_rtx_MEM (SImode, plus_constant (Pmode, sp, (j + 1) * -4));
beed8fc0
AO
608 rhs = gen_rtx_REG (SImode, regno + j);
609 }
f7df4a84 610 RTVEC_ELT (vec, i + j) = gen_rtx_SET (lhs, rhs);
beed8fc0
AO
611 }
612
613 /* Add the stack adjustment. */
614 offset = GEN_INT ((pop_p ? nregs : -nregs) * 4);
f7df4a84 615 RTVEC_ELT (vec, i + j) = gen_rtx_SET (sp, gen_rtx_PLUS (Pmode, sp, offset));
beed8fc0 616
18674659
DD
617 x = gen_rtx_PARALLEL (VOIDmode, vec);
618 if (!pop_p)
619 x = Fpa (x);
ac447f25
NC
620
621 if (return_p)
622 emit_jump_insn (x);
623 else
624 emit_insn (x);
beed8fc0
AO
625}
626
627/* Return true if X has the value sp + OFFSET. */
628
629static int
630h8300_stack_offset_p (rtx x, int offset)
631{
632 if (offset == 0)
633 return x == stack_pointer_rtx;
634
635 return (GET_CODE (x) == PLUS
636 && XEXP (x, 0) == stack_pointer_rtx
637 && GET_CODE (XEXP (x, 1)) == CONST_INT
638 && INTVAL (XEXP (x, 1)) == offset);
639}
640
641/* A subroutine of h8300_ldm_stm_parallel. X is one pattern in
642 something that may be an ldm or stm instruction. If it fits
643 the required template, return the register it loads or stores,
644 otherwise return -1.
645
646 LOAD_P is true if X should be a load, false if it should be a store.
647 NREGS is the number of registers that the whole instruction is expected
648 to load or store. INDEX is the index of the register that X should
649 load or store, relative to the lowest-numbered register. */
650
651static int
652h8300_ldm_stm_regno (rtx x, int load_p, int index, int nregs)
653{
654 int regindex, memindex, offset;
655
656 if (load_p)
657 regindex = 0, memindex = 1, offset = (nregs - index - 1) * 4;
658 else
659 memindex = 0, regindex = 1, offset = (index + 1) * -4;
660
661 if (GET_CODE (x) == SET
662 && GET_CODE (XEXP (x, regindex)) == REG
663 && GET_CODE (XEXP (x, memindex)) == MEM
664 && h8300_stack_offset_p (XEXP (XEXP (x, memindex), 0), offset))
665 return REGNO (XEXP (x, regindex));
666
667 return -1;
668}
669
670/* Return true if the elements of VEC starting at FIRST describe an
671 ldm or stm instruction (LOAD_P says which). */
672
981c7dce 673int
beed8fc0
AO
674h8300_ldm_stm_parallel (rtvec vec, int load_p, int first)
675{
676 rtx last;
677 int nregs, i, regno, adjust;
678
679 /* There must be a stack adjustment, a register move, and at least one
680 other operation (a return or another register move). */
681 if (GET_NUM_ELEM (vec) < 3)
682 return false;
683
684 /* Get the range of registers to be pushed or popped. */
685 nregs = GET_NUM_ELEM (vec) - first - 1;
686 regno = h8300_ldm_stm_regno (RTVEC_ELT (vec, first), load_p, 0, nregs);
687
688 /* Check that the call to h8300_ldm_stm_regno succeeded and
689 that we're only dealing with GPRs. */
690 if (regno < 0 || regno + nregs > 8)
691 return false;
692
693 /* 2-register h8s instructions must start with an even-numbered register.
694 3- and 4-register instructions must start with er0 or er4. */
695 if (!TARGET_H8300SX)
696 {
697 if ((regno & 1) != 0)
698 return false;
699 if (nregs > 2 && (regno & 3) != 0)
700 return false;
701 }
702
703 /* Check the other loads or stores. */
704 for (i = 1; i < nregs; i++)
705 if (h8300_ldm_stm_regno (RTVEC_ELT (vec, first + i), load_p, i, nregs)
706 != regno + i)
707 return false;
708
709 /* Check the stack adjustment. */
710 last = RTVEC_ELT (vec, first + nregs);
711 adjust = (load_p ? nregs : -nregs) * 4;
712 return (GET_CODE (last) == SET
713 && SET_DEST (last) == stack_pointer_rtx
714 && h8300_stack_offset_p (SET_SRC (last), adjust));
715}
716
f0b6f9a6 717/* This is what the stack looks like after the prolog of
07aae5c2
SC
718 a function with a frame has been set up:
719
48837e29
DE
720 <args>
721 PC
722 FP <- fp
723 <locals>
8bd06267 724 <saved registers> <- sp
07aae5c2
SC
725
726 This is what the stack looks like after the prolog of
727 a function which doesn't have a frame:
728
48837e29
DE
729 <args>
730 PC
731 <locals>
8bd06267 732 <saved registers> <- sp
07aae5c2
SC
733*/
734
68ee6df6 735/* Generate RTL code for the function prologue. */
8682223f 736
68ee6df6 737void
cb713a8d 738h8300_expand_prologue (void)
07aae5c2 739{
e651d484 740 int regno;
8682223f 741 int saved_regs;
cda4bd43 742 int n_regs;
07aae5c2 743
fabe72bb
JL
744 /* If the current function has the OS_Task attribute set, then
745 we have a naked prologue. */
746 if (h8300_os_task_function_p (current_function_decl))
68ee6df6 747 return;
fabe72bb
JL
748
749 if (h8300_monitor_function_p (current_function_decl))
f4ea8112
SKS
750 /* The monitor function act as normal functions, which means it
751 can accept parameters and return values. In addition to this,
752 interrupts are masked in prologue and return with "rte" in epilogue. */
68ee6df6 753 emit_insn (gen_monitor_prologue ());
fabe72bb 754
48837e29
DE
755 if (frame_pointer_needed)
756 {
07e4d94e 757 /* Push fp. */
12bb62fb 758 push (HARD_FRAME_POINTER_REGNUM);
f16897cb 759 F (emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx), 0);
a1616dd9 760 }
48837e29 761
8682223f
KH
762 /* Push the rest of the registers in ascending order. */
763 saved_regs = compute_saved_regs ();
e651d484 764 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno += n_regs)
cda4bd43 765 {
cda4bd43 766 n_regs = 1;
8682223f 767 if (saved_regs & (1 << regno))
a1616dd9
JL
768 {
769 if (TARGET_H8300S)
770 {
cda4bd43 771 /* See how many registers we can push at the same time. */
040e0e44 772 if ((TARGET_H8300SX || (regno & 3) == 0)
8682223f 773 && ((saved_regs >> regno) & 0x0f) == 0x0f)
cda4bd43
KH
774 n_regs = 4;
775
040e0e44 776 else if ((TARGET_H8300SX || (regno & 3) == 0)
8682223f 777 && ((saved_regs >> regno) & 0x07) == 0x07)
cda4bd43
KH
778 n_regs = 3;
779
040e0e44 780 else if ((TARGET_H8300SX || (regno & 1) == 0)
8682223f 781 && ((saved_regs >> regno) & 0x03) == 0x03)
cda4bd43 782 n_regs = 2;
a1616dd9 783 }
cda4bd43 784
ac447f25 785 h8300_push_pop (regno, n_regs, false, false);
07aae5c2
SC
786 }
787 }
1807b726
KH
788
789 /* Leave room for locals. */
12bb62fb 790 h8300_emit_stack_adjustment (-1, round_frame_size (get_frame_size ()));
ed1332ee
NC
791
792 if (flag_stack_usage_info)
793 current_function_static_stack_size
794 = round_frame_size (get_frame_size ())
795 + (__builtin_popcount (saved_regs) * UNITS_PER_WORD)
796 + (frame_pointer_needed ? UNITS_PER_WORD : 0);
07aae5c2
SC
797}
798
e9eba255
KH
799/* Return nonzero if we can use "rts" for the function currently being
800 compiled. */
801
68ee6df6 802int
cb713a8d 803h8300_can_use_return_insn_p (void)
68ee6df6
KH
804{
805 return (reload_completed
806 && !frame_pointer_needed
807 && get_frame_size () == 0
808 && compute_saved_regs () == 0);
809}
07aae5c2 810
68ee6df6
KH
811/* Generate RTL code for the function epilogue. */
812
813void
cb713a8d 814h8300_expand_epilogue (void)
07aae5c2 815{
e651d484 816 int regno;
8682223f 817 int saved_regs;
cda4bd43 818 int n_regs;
beed8fc0
AO
819 HOST_WIDE_INT frame_size;
820 bool returned_p;
07aae5c2 821
e392d367 822 if (h8300_os_task_function_p (current_function_decl))
68ee6df6
KH
823 /* OS_Task epilogues are nearly naked -- they just have an
824 rts instruction. */
825 return;
07aae5c2 826
beed8fc0
AO
827 frame_size = round_frame_size (get_frame_size ());
828 returned_p = false;
829
1807b726 830 /* Deallocate locals. */
12bb62fb 831 h8300_emit_stack_adjustment (1, frame_size);
1807b726 832
8682223f
KH
833 /* Pop the saved registers in descending order. */
834 saved_regs = compute_saved_regs ();
e651d484 835 for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno -= n_regs)
cda4bd43 836 {
cda4bd43 837 n_regs = 1;
8682223f 838 if (saved_regs & (1 << regno))
07aae5c2 839 {
a1616dd9
JL
840 if (TARGET_H8300S)
841 {
cda4bd43 842 /* See how many registers we can pop at the same time. */
beed8fc0
AO
843 if ((TARGET_H8300SX || (regno & 3) == 3)
844 && ((saved_regs << 3 >> regno) & 0x0f) == 0x0f)
cda4bd43
KH
845 n_regs = 4;
846
beed8fc0
AO
847 else if ((TARGET_H8300SX || (regno & 3) == 2)
848 && ((saved_regs << 2 >> regno) & 0x07) == 0x07)
cda4bd43
KH
849 n_regs = 3;
850
beed8fc0
AO
851 else if ((TARGET_H8300SX || (regno & 1) == 1)
852 && ((saved_regs << 1 >> regno) & 0x03) == 0x03)
cda4bd43 853 n_regs = 2;
a1616dd9 854 }
cda4bd43 855
beed8fc0
AO
856 /* See if this pop would be the last insn before the return.
857 If so, use rte/l or rts/l instead of pop or ldm.l. */
858 if (TARGET_H8300SX
859 && !frame_pointer_needed
860 && frame_size == 0
861 && (saved_regs & ((1 << (regno - n_regs + 1)) - 1)) == 0)
862 returned_p = true;
863
ac447f25 864 h8300_push_pop (regno - n_regs + 1, n_regs, true, returned_p);
07aae5c2 865 }
07aae5c2 866 }
48837e29 867
07e4d94e 868 /* Pop frame pointer if we had one. */
a1616dd9 869 if (frame_pointer_needed)
beed8fc0
AO
870 {
871 if (TARGET_H8300SX)
872 returned_p = true;
ac447f25 873 h8300_push_pop (HARD_FRAME_POINTER_REGNUM, 1, true, returned_p);
beed8fc0
AO
874 }
875
876 if (!returned_p)
3810076b 877 emit_jump_insn (ret_rtx);
68ee6df6 878}
a1616dd9 879
e392d367
KH
880/* Return nonzero if the current function is an interrupt
881 function. */
882
883int
cb713a8d 884h8300_current_function_interrupt_function_p (void)
e392d367 885{
f4ea8112
SKS
886 return (h8300_interrupt_function_p (current_function_decl));
887}
888
889int
890h8300_current_function_monitor_function_p ()
891{
892 return (h8300_monitor_function_p (current_function_decl));
e392d367
KH
893}
894
48837e29
DE
895/* Output assembly code for the start of the file. */
896
1bc7c5b6
ZW
897static void
898h8300_file_start (void)
48837e29 899{
1bc7c5b6 900 default_file_start ();
8bd06267 901
2f6bd6eb 902 if (TARGET_H8300SX)
beed8fc0 903 fputs (TARGET_NORMAL_MODE ? "\t.h8300sxn\n" : "\t.h8300sx\n", asm_out_file);
a1616dd9 904 else if (TARGET_H8300S)
1bc7c5b6 905 fputs (TARGET_NORMAL_MODE ? "\t.h8300sn\n" : "\t.h8300s\n", asm_out_file);
2f6bd6eb
YS
906 else if (TARGET_H8300H)
907 fputs (TARGET_NORMAL_MODE ? "\t.h8300hn\n" : "\t.h8300h\n", asm_out_file);
48837e29
DE
908}
909
910/* Output assembly language code for the end of file. */
911
a5fe455b 912static void
cb713a8d 913h8300_file_end (void)
48837e29 914{
a5fe455b 915 fputs ("\t.end\n", asm_out_file);
07aae5c2
SC
916}
917\f
3cee1a78
KH
918/* Split an add of a small constant into two adds/subs insns.
919
920 If USE_INCDEC_P is nonzero, we generate the last insn using inc/dec
921 instead of adds/subs. */
009ac3d3
RH
922
923void
ef4bddc2 924split_adds_subs (machine_mode mode, rtx *operands)
3b7d443c 925{
009ac3d3
RH
926 HOST_WIDE_INT val = INTVAL (operands[1]);
927 rtx reg = operands[0];
9492393e
KH
928 HOST_WIDE_INT sign = 1;
929 HOST_WIDE_INT amount;
590734b6 930 rtx (*gen_add) (rtx, rtx, rtx);
3b7d443c 931
9492393e
KH
932 /* Force VAL to be positive so that we do not have to consider the
933 sign. */
934 if (val < 0)
3b7d443c 935 {
9492393e
KH
936 val = -val;
937 sign = -1;
938 }
3b7d443c 939
3cee1a78
KH
940 switch (mode)
941 {
4e10a5a7 942 case E_HImode:
590734b6 943 gen_add = gen_addhi3;
3cee1a78
KH
944 break;
945
4e10a5a7 946 case E_SImode:
590734b6 947 gen_add = gen_addsi3;
3cee1a78
KH
948 break;
949
950 default:
8c440872 951 gcc_unreachable ();
3cee1a78
KH
952 }
953
9492393e 954 /* Try different amounts in descending order. */
27c1afd5 955 for (amount = 4; amount > 0; amount /= 2)
9492393e 956 {
1a63219b 957 for (; val >= amount; val -= amount)
590734b6 958 emit_insn (gen_add (reg, reg, GEN_INT (sign * amount)));
3b7d443c
JL
959 }
960
9492393e 961 return;
3b7d443c
JL
962}
963
07aae5c2 964/* Handle machine specific pragmas for compatibility with existing
48837e29 965 compilers for the H8/300.
07aae5c2 966
f411c849 967 pragma saveall generates prologue/epilogue code which saves and
07aae5c2 968 restores all the registers on function entry.
48837e29 969
07aae5c2
SC
970 pragma interrupt saves and restores all registers, and exits with
971 an rte instruction rather than an rts. A pointer to a function
972 with this attribute may be safely used in an interrupt vector. */
48837e29 973
8b97c5f8 974void
cb713a8d 975h8300_pr_interrupt (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
07aae5c2 976{
e392d367 977 pragma_interrupt = 1;
8b97c5f8 978}
05a81fe5 979
8b97c5f8 980void
cb713a8d 981h8300_pr_saveall (struct cpp_reader *pfile ATTRIBUTE_UNUSED)
8b97c5f8
ZW
982{
983 pragma_saveall = 1;
07aae5c2 984}
8b97c5f8 985
6783fdb7
RS
986/* If the next function argument ARG is to be passed in a register, return
987 a reg RTX for the hard register in which to pass the argument. CUM
988 represents the state after the last argument. If the argument is to
989 be pushed, NULL_RTX is returned.
48837e29 990
56f9413b
NF
991 On the H8/300 all normal args are pushed, unless -mquickcall in which
992 case the first 3 arguments are passed in registers. */
993
994static rtx
6783fdb7 995h8300_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
07aae5c2 996{
d5cc9181
JR
997 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
998
0ea6f6a0
KH
999 static const char *const hand_list[] = {
1000 "__main",
1001 "__cmpsi2",
1002 "__divhi3",
1003 "__modhi3",
1004 "__udivhi3",
1005 "__umodhi3",
1006 "__divsi3",
1007 "__modsi3",
1008 "__udivsi3",
1009 "__umodsi3",
1010 "__mulhi3",
1011 "__mulsi3",
1012 "__reg_memcpy",
1013 "__reg_memset",
1014 "__ucmpsi2",
1015 0,
1016 };
1017
7192cbf1 1018 rtx result = NULL_RTX;
441d04c6 1019 const char *fname;
48837e29
DE
1020 int regpass = 0;
1021
dd07092e 1022 /* Never pass unnamed arguments in registers. */
6783fdb7 1023 if (!arg.named)
7192cbf1 1024 return NULL_RTX;
dd07092e 1025
48837e29
DE
1026 /* Pass 3 regs worth of data in regs when user asked on the command line. */
1027 if (TARGET_QUICKCALL)
1028 regpass = 3;
1029
1030 /* If calling hand written assembler, use 4 regs of args. */
48837e29
DE
1031 if (cum->libcall)
1032 {
441d04c6 1033 const char * const *p;
48837e29
DE
1034
1035 fname = XSTR (cum->libcall, 0);
1036
1037 /* See if this libcall is one of the hand coded ones. */
48837e29
DE
1038 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
1039 ;
07aae5c2 1040
48837e29
DE
1041 if (*p)
1042 regpass = 4;
1043 }
1044
1045 if (regpass)
1046 {
6783fdb7 1047 int size = arg.promoted_size_in_bytes ();
15e0e275
KH
1048 if (size + cum->nbytes <= regpass * UNITS_PER_WORD
1049 && cum->nbytes / UNITS_PER_WORD <= 3)
6783fdb7 1050 result = gen_rtx_REG (arg.mode, cum->nbytes / UNITS_PER_WORD);
48837e29 1051 }
07aae5c2 1052
48837e29
DE
1053 return result;
1054}
56f9413b 1055
6930c98c 1056/* Update the data in CUM to advance over argument ARG. */
56f9413b
NF
1057
1058static void
6930c98c
RS
1059h8300_function_arg_advance (cumulative_args_t cum_v,
1060 const function_arg_info &arg)
56f9413b 1061{
d5cc9181
JR
1062 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
1063
6930c98c
RS
1064 cum->nbytes += ((arg.promoted_size_in_bytes () + UNITS_PER_WORD - 1)
1065 & -UNITS_PER_WORD);
56f9413b
NF
1066}
1067
48837e29 1068\f
88cb339e
N
1069/* Implements TARGET_REGISTER_MOVE_COST.
1070
1071 Any SI register-to-register move may need to be reloaded,
1072 so inmplement h8300_register_move_cost to return > 2 so that reload never
1073 shortcuts. */
1074
1075static int
ef4bddc2 1076h8300_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
88cb339e
N
1077 reg_class_t from, reg_class_t to)
1078{
1079 if (from == MAC_REGS || to == MAC_REG)
1080 return 6;
1081 else
1082 return 3;
1083}
1084
e9eba255
KH
1085/* Compute the cost of an and insn. */
1086
3c50106f 1087static int
cb713a8d 1088h8300_and_costs (rtx x)
73cc75e9
KH
1089{
1090 rtx operands[4];
1091
1092 if (GET_MODE (x) == QImode)
1093 return 1;
1094
1095 if (GET_MODE (x) != HImode
1096 && GET_MODE (x) != SImode)
1097 return 100;
1098
1099 operands[0] = NULL;
beed8fc0 1100 operands[1] = XEXP (x, 0);
73cc75e9
KH
1101 operands[2] = XEXP (x, 1);
1102 operands[3] = x;
4ea5fe8b 1103 return compute_logical_op_length (GET_MODE (x), AND, operands) / 2;
73cc75e9
KH
1104}
1105
e9eba255
KH
1106/* Compute the cost of a shift insn. */
1107
3c50106f 1108static int
cb713a8d 1109h8300_shift_costs (rtx x)
ae557002
KH
1110{
1111 rtx operands[4];
1112
1113 if (GET_MODE (x) != QImode
1114 && GET_MODE (x) != HImode
1115 && GET_MODE (x) != SImode)
1116 return 100;
1117
1118 operands[0] = NULL;
1119 operands[1] = NULL;
1120 operands[2] = XEXP (x, 1);
1121 operands[3] = x;
4ea5fe8b 1122 return compute_a_shift_length (operands) / 2;
ae557002 1123}
3c50106f 1124
e9eba255
KH
1125/* Worker function for TARGET_RTX_COSTS. */
1126
3c50106f 1127static bool
e548c9df
AM
1128h8300_rtx_costs (rtx x, machine_mode mode ATTRIBUTE_UNUSED, int outer_code,
1129 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
3c50106f 1130{
e548c9df
AM
1131 int code = GET_CODE (x);
1132
beed8fc0
AO
1133 if (TARGET_H8300SX && outer_code == MEM)
1134 {
1135 /* Estimate the number of execution states needed to calculate
1136 the address. */
1137 if (register_operand (x, VOIDmode)
1138 || GET_CODE (x) == POST_INC
1139 || GET_CODE (x) == POST_DEC
1140 || CONSTANT_P (x))
1141 *total = 0;
1142 else
1143 *total = COSTS_N_INSNS (1);
1144 return true;
1145 }
1146
3c50106f
RH
1147 switch (code)
1148 {
08dca707
KH
1149 case CONST_INT:
1150 {
1151 HOST_WIDE_INT n = INTVAL (x);
1152
beed8fc0
AO
1153 if (TARGET_H8300SX)
1154 {
1155 /* Constant operands need the same number of processor
1156 states as register operands. Although we could try to
f40751dd 1157 use a size-based cost for !speed, the lack of
beed8fc0
AO
1158 of a mode makes the results very unpredictable. */
1159 *total = 0;
1160 return true;
1161 }
01512446 1162 if (n >= -4 && n <= 4)
08dca707
KH
1163 {
1164 switch ((int) n)
1165 {
1166 case 0:
1167 *total = 0;
1168 return true;
1169 case 1:
1170 case 2:
1171 case -1:
1172 case -2:
1173 *total = 0 + (outer_code == SET);
1174 return true;
1175 case 4:
1176 case -4:
27c1afd5 1177 *total = 0 + (outer_code == SET);
08dca707
KH
1178 return true;
1179 }
1180 }
1181 *total = 1;
1182 return true;
1183 }
1184
1185 case CONST:
1186 case LABEL_REF:
1187 case SYMBOL_REF:
beed8fc0
AO
1188 if (TARGET_H8300SX)
1189 {
1190 /* See comment for CONST_INT. */
1191 *total = 0;
1192 return true;
1193 }
08dca707
KH
1194 *total = 3;
1195 return true;
1196
1197 case CONST_DOUBLE:
1198 *total = 20;
1199 return true;
1200
f90b7a5a 1201 case COMPARE:
f80565da
JL
1202 case NE:
1203 case EQ:
1204 case GE:
1205 case GT:
1206 case LE:
1207 case LT:
1208 case GEU:
1209 case GTU:
1210 case LEU:
1211 case LTU:
f90b7a5a
PB
1212 if (XEXP (x, 1) == const0_rtx)
1213 *total = 0;
1214 return false;
1215
3c50106f 1216 case AND:
beed8fc0
AO
1217 if (!h8300_dst_operand (XEXP (x, 0), VOIDmode)
1218 || !h8300_src_operand (XEXP (x, 1), VOIDmode))
1219 return false;
3c50106f
RH
1220 *total = COSTS_N_INSNS (h8300_and_costs (x));
1221 return true;
1222
1223 /* We say that MOD and DIV are so expensive because otherwise we'll
1224 generate some really horrible code for division of a power of two. */
1225 case MOD:
1226 case DIV:
beed8fc0
AO
1227 case UMOD:
1228 case UDIV:
1229 if (TARGET_H8300SX)
1230 switch (GET_MODE (x))
1231 {
4e10a5a7
RS
1232 case E_QImode:
1233 case E_HImode:
f40751dd 1234 *total = COSTS_N_INSNS (!speed ? 4 : 10);
beed8fc0
AO
1235 return false;
1236
4e10a5a7 1237 case E_SImode:
f40751dd 1238 *total = COSTS_N_INSNS (!speed ? 4 : 18);
beed8fc0
AO
1239 return false;
1240
1241 default:
1242 break;
1243 }
1244 *total = COSTS_N_INSNS (12);
3c50106f
RH
1245 return true;
1246
1247 case MULT:
beed8fc0
AO
1248 if (TARGET_H8300SX)
1249 switch (GET_MODE (x))
1250 {
4e10a5a7
RS
1251 case E_QImode:
1252 case E_HImode:
beed8fc0
AO
1253 *total = COSTS_N_INSNS (2);
1254 return false;
1255
4e10a5a7 1256 case E_SImode:
beed8fc0
AO
1257 *total = COSTS_N_INSNS (5);
1258 return false;
1259
1260 default:
1261 break;
1262 }
1263 *total = COSTS_N_INSNS (4);
3c50106f
RH
1264 return true;
1265
1266 case ASHIFT:
1267 case ASHIFTRT:
1268 case LSHIFTRT:
beed8fc0
AO
1269 if (h8sx_binary_shift_operator (x, VOIDmode))
1270 {
1271 *total = COSTS_N_INSNS (2);
1272 return false;
1273 }
1274 else if (h8sx_unary_shift_operator (x, VOIDmode))
1275 {
1276 *total = COSTS_N_INSNS (1);
1277 return false;
1278 }
3c50106f
RH
1279 *total = COSTS_N_INSNS (h8300_shift_costs (x));
1280 return true;
1281
1282 case ROTATE:
1283 case ROTATERT:
1284 if (GET_MODE (x) == HImode)
1285 *total = 2;
1286 else
1287 *total = 8;
1288 return true;
1289
1290 default:
beed8fc0
AO
1291 *total = COSTS_N_INSNS (1);
1292 return false;
3c50106f
RH
1293 }
1294}
48837e29 1295\f
07aae5c2
SC
1296/* Documentation for the machine specific operand escapes:
1297
48837e29
DE
1298 'E' like s but negative.
1299 'F' like t but negative.
1300 'G' constant just the negative
15dc331e
JL
1301 'R' print operand as a byte:8 address if appropriate, else fall back to
1302 'X' handling.
48837e29 1303 'S' print operand as a long word
07aae5c2 1304 'T' print operand as a word
48837e29
DE
1305 'V' find the set bit, and print its number.
1306 'W' find the clear bit, and print its number.
1307 'X' print operand as a byte
07aae5c2 1308 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
15dc331e 1309 If this operand isn't a register, fall back to 'R' handling.
48837e29 1310 'Z' print int & 7.
b059c02a 1311 'c' print the opcode corresponding to rtl
da55315a 1312 'e' first word of 32-bit value - if reg, then least reg. if mem
48837e29 1313 then least. if const then most sig word
da55315a 1314 'f' second word of 32-bit value - if reg, then biggest reg. if mem
48837e29 1315 then +2. if const then least sig word
07aae5c2
SC
1316 'j' print operand as condition code.
1317 'k' print operand as reverse condition code.
beed8fc0
AO
1318 'm' convert an integer operand to a size suffix (.b, .w or .l)
1319 'o' print an integer without a leading '#'
da55315a
KH
1320 's' print as low byte of 16-bit value
1321 't' print as high byte of 16-bit value
1322 'w' print as low byte of 32-bit value
1323 'x' print as 2nd byte of 32-bit value
1324 'y' print as 3rd byte of 32-bit value
1325 'z' print as msb of 32-bit value
48837e29 1326*/
07aae5c2
SC
1327
1328/* Return assembly language string which identifies a comparison type. */
1329
441d04c6 1330static const char *
cb713a8d 1331cond_string (enum rtx_code code)
07aae5c2
SC
1332{
1333 switch (code)
1334 {
1335 case NE:
1336 return "ne";
1337 case EQ:
1338 return "eq";
1339 case GE:
1340 return "ge";
1341 case GT:
1342 return "gt";
1343 case LE:
1344 return "le";
1345 case LT:
1346 return "lt";
1347 case GEU:
1348 return "hs";
1349 case GTU:
1350 return "hi";
1351 case LEU:
1352 return "ls";
1353 case LTU:
1354 return "lo";
1355 default:
8c440872 1356 gcc_unreachable ();
07aae5c2
SC
1357 }
1358}
1359
1360/* Print operand X using operand code CODE to assembly language output file
1361 FILE. */
1362
88cb339e
N
1363static void
1364h8300_print_operand (FILE *file, rtx x, int code)
07aae5c2 1365{
269c14e1 1366 /* This is used for communication between codes V,W,Z and Y. */
07aae5c2
SC
1367 static int bitint;
1368
1369 switch (code)
1370 {
f46b8378
SKS
1371 case 'C':
1372 if (h8300_constant_length (x) == 2)
1373 fprintf (file, ":16");
1374 else
1375 fprintf (file, ":32");
1376 return;
48837e29
DE
1377 case 'E':
1378 switch (GET_CODE (x))
1379 {
1380 case REG:
1381 fprintf (file, "%sl", names_big[REGNO (x)]);
1382 break;
1383 case CONST_INT:
b47900aa 1384 fprintf (file, "#%ld", (-INTVAL (x)) & 0xff);
48837e29
DE
1385 break;
1386 default:
8c440872 1387 gcc_unreachable ();
48837e29
DE
1388 }
1389 break;
1390 case 'F':
1391 switch (GET_CODE (x))
1392 {
1393 case REG:
1394 fprintf (file, "%sh", names_big[REGNO (x)]);
1395 break;
1396 case CONST_INT:
b47900aa 1397 fprintf (file, "#%ld", ((-INTVAL (x)) & 0xff00) >> 8);
48837e29
DE
1398 break;
1399 default:
8c440872 1400 gcc_unreachable ();
48837e29
DE
1401 }
1402 break;
07aae5c2 1403 case 'G':
8c440872 1404 gcc_assert (GET_CODE (x) == CONST_INT);
b47900aa 1405 fprintf (file, "#%ld", 0xff & (-INTVAL (x)));
07aae5c2 1406 break;
48837e29
DE
1407 case 'S':
1408 if (GET_CODE (x) == REG)
1409 fprintf (file, "%s", names_extended[REGNO (x)]);
07aae5c2 1410 else
48837e29 1411 goto def;
07aae5c2 1412 break;
48837e29
DE
1413 case 'T':
1414 if (GET_CODE (x) == REG)
1415 fprintf (file, "%s", names_big[REGNO (x)]);
07aae5c2 1416 else
48837e29 1417 goto def;
07aae5c2 1418 break;
48837e29 1419 case 'V':
0f6b820c
KP
1420 bitint = (INTVAL (x) & 0xffff);
1421 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1)
1422 bitint = exact_log2 (bitint & 0xff);
1423 else
1424 bitint = exact_log2 ((bitint >> 8) & 0xff);
8c440872 1425 gcc_assert (bitint >= 0);
4d4d89e2 1426 fprintf (file, "#%d", bitint);
07aae5c2 1427 break;
48837e29 1428 case 'W':
0f6b820c
KP
1429 bitint = ((~INTVAL (x)) & 0xffff);
1430 if ((exact_log2 ((bitint >> 8) & 0xff)) == -1 )
1431 bitint = exact_log2 (bitint & 0xff);
1432 else
1433 bitint = (exact_log2 ((bitint >> 8) & 0xff));
8c440872 1434 gcc_assert (bitint >= 0);
4d4d89e2 1435 fprintf (file, "#%d", bitint);
07aae5c2 1436 break;
15dc331e 1437 case 'R':
48837e29
DE
1438 case 'X':
1439 if (GET_CODE (x) == REG)
1440 fprintf (file, "%s", byte_reg (x, 0));
1441 else
1442 goto def;
1443 break;
1444 case 'Y':
8c440872 1445 gcc_assert (bitint >= 0);
48837e29
DE
1446 if (GET_CODE (x) == REG)
1447 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
1448 else
88cb339e 1449 h8300_print_operand (file, x, 'R');
48837e29
DE
1450 bitint = -1;
1451 break;
1452 case 'Z':
1453 bitint = INTVAL (x);
07aae5c2
SC
1454 fprintf (file, "#%d", bitint & 7);
1455 break;
b059c02a
KH
1456 case 'c':
1457 switch (GET_CODE (x))
1458 {
1459 case IOR:
1460 fprintf (file, "or");
1461 break;
1462 case XOR:
1463 fprintf (file, "xor");
1464 break;
5abfd1af
KH
1465 case AND:
1466 fprintf (file, "and");
1467 break;
b059c02a
KH
1468 default:
1469 break;
1470 }
1471 break;
07aae5c2
SC
1472 case 'e':
1473 switch (GET_CODE (x))
1474 {
1475 case REG:
27c1afd5 1476 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
07aae5c2
SC
1477 break;
1478 case MEM:
88cb339e 1479 h8300_print_operand (file, x, 0);
07aae5c2
SC
1480 break;
1481 case CONST_INT:
b47900aa 1482 fprintf (file, "#%ld", ((INTVAL (x) >> 16) & 0xffff));
07aae5c2 1483 break;
808fbfac
JL
1484 case CONST_DOUBLE:
1485 {
1486 long val;
34a72c33 1487 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
441d04c6 1488 fprintf (file, "#%ld", ((val >> 16) & 0xffff));
808fbfac
JL
1489 break;
1490 }
07aae5c2 1491 default:
8c440872 1492 gcc_unreachable ();
07aae5c2
SC
1493 break;
1494 }
1495 break;
07aae5c2
SC
1496 case 'f':
1497 switch (GET_CODE (x))
1498 {
1499 case REG:
27c1afd5 1500 fprintf (file, "%s", names_big[REGNO (x)]);
07aae5c2 1501 break;
07aae5c2 1502 case MEM:
b72f00af 1503 x = adjust_address (x, HImode, 2);
88cb339e 1504 h8300_print_operand (file, x, 0);
07aae5c2 1505 break;
07aae5c2 1506 case CONST_INT:
b47900aa 1507 fprintf (file, "#%ld", INTVAL (x) & 0xffff);
07aae5c2 1508 break;
808fbfac
JL
1509 case CONST_DOUBLE:
1510 {
1511 long val;
34a72c33 1512 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
441d04c6 1513 fprintf (file, "#%ld", (val & 0xffff));
808fbfac
JL
1514 break;
1515 }
07aae5c2 1516 default:
8c440872 1517 gcc_unreachable ();
07aae5c2
SC
1518 }
1519 break;
07aae5c2 1520 case 'j':
f16897cb
AL
1521 if (GET_CODE (x) == LT && GET_MODE (XEXP (x, 0)) == E_CCZNmode)
1522 fputs ("mi", file);
1523 else if (GET_CODE (x) == GE && GET_MODE (XEXP (x, 0)) == E_CCZNmode)
1524 fputs ("pl", file);
1525 else
1526 fputs (cond_string (GET_CODE (x)), file);
07aae5c2 1527 break;
07aae5c2 1528 case 'k':
f16897cb
AL
1529 if (GET_CODE (x) == LT && GET_MODE (XEXP (x, 0)) == E_CCZNmode)
1530 fputs ("pl", file);
1531 else if (GET_CODE (x) == GE && GET_MODE (XEXP (x, 0)) == E_CCZNmode)
1532 fputs ("mi", file);
1533 else
1534 fputs (cond_string (reverse_condition (GET_CODE (x))), file);
07aae5c2 1535 break;
beed8fc0 1536 case 'm':
8c440872
NS
1537 gcc_assert (GET_CODE (x) == CONST_INT);
1538 switch (INTVAL (x))
1539 {
1540 case 1:
1541 fputs (".b", file);
1542 break;
1543
1544 case 2:
1545 fputs (".w", file);
1546 break;
1547
1548 case 4:
1549 fputs (".l", file);
1550 break;
1551
1552 default:
1553 gcc_unreachable ();
1554 }
beed8fc0
AO
1555 break;
1556 case 'o':
cc8ca59e 1557 h8300_print_operand_address (file, VOIDmode, x);
beed8fc0 1558 break;
48837e29
DE
1559 case 's':
1560 if (GET_CODE (x) == CONST_INT)
b47900aa 1561 fprintf (file, "#%ld", (INTVAL (x)) & 0xff);
64c9f2d9 1562 else if (GET_CODE (x) == REG)
48837e29 1563 fprintf (file, "%s", byte_reg (x, 0));
64c9f2d9
JL
1564 else
1565 output_operand_lossage ("Expected register or constant integer.");
48837e29
DE
1566 break;
1567 case 't':
1568 if (GET_CODE (x) == CONST_INT)
b47900aa 1569 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
64c9f2d9 1570 else if (GET_CODE (x) == REG)
48837e29 1571 fprintf (file, "%s", byte_reg (x, 1));
64c9f2d9
JL
1572 else
1573 output_operand_lossage ("Expected register or constant integer.");
48837e29 1574 break;
48837e29
DE
1575 case 'w':
1576 if (GET_CODE (x) == CONST_INT)
b47900aa 1577 fprintf (file, "#%ld", INTVAL (x) & 0xff);
64c9f2d9 1578 else if (GET_CODE (x) == REG)
27c1afd5 1579 fprintf (file, "%s", byte_reg (x, 0));
64c9f2d9
JL
1580 else
1581 output_operand_lossage ("Expected register or constant integer.");
48837e29
DE
1582 break;
1583 case 'x':
1584 if (GET_CODE (x) == CONST_INT)
b47900aa 1585 fprintf (file, "#%ld", (INTVAL (x) >> 8) & 0xff);
64c9f2d9 1586 else if (GET_CODE (x) == REG)
27c1afd5 1587 fprintf (file, "%s", byte_reg (x, 1));
64c9f2d9
JL
1588 else
1589 output_operand_lossage ("Expected register or constant integer.");
48837e29
DE
1590 break;
1591 case 'y':
1592 if (GET_CODE (x) == CONST_INT)
b47900aa 1593 fprintf (file, "#%ld", (INTVAL (x) >> 16) & 0xff);
64c9f2d9 1594 else if (GET_CODE (x) == REG)
48837e29 1595 fprintf (file, "%s", byte_reg (x, 0));
64c9f2d9
JL
1596 else
1597 output_operand_lossage ("Expected register or constant integer.");
48837e29
DE
1598 break;
1599 case 'z':
1600 if (GET_CODE (x) == CONST_INT)
b47900aa 1601 fprintf (file, "#%ld", (INTVAL (x) >> 24) & 0xff);
64c9f2d9 1602 else if (GET_CODE (x) == REG)
48837e29 1603 fprintf (file, "%s", byte_reg (x, 1));
64c9f2d9
JL
1604 else
1605 output_operand_lossage ("Expected register or constant integer.");
48837e29
DE
1606 break;
1607
07aae5c2 1608 default:
48837e29 1609 def:
07aae5c2
SC
1610 switch (GET_CODE (x))
1611 {
1612 case REG:
48837e29
DE
1613 switch (GET_MODE (x))
1614 {
4e10a5a7 1615 case E_QImode:
269c14e1 1616#if 0 /* Is it asm ("mov.b %0,r2l", ...) */
48837e29
DE
1617 fprintf (file, "%s", byte_reg (x, 0));
1618#else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1619 fprintf (file, "%s", names_big[REGNO (x)]);
1620#endif
1621 break;
4e10a5a7 1622 case E_HImode:
48837e29
DE
1623 fprintf (file, "%s", names_big[REGNO (x)]);
1624 break;
4e10a5a7
RS
1625 case E_SImode:
1626 case E_SFmode:
48837e29
DE
1627 fprintf (file, "%s", names_extended[REGNO (x)]);
1628 break;
1629 default:
8c440872 1630 gcc_unreachable ();
48837e29 1631 }
07aae5c2
SC
1632 break;
1633
1634 case MEM:
87e4ee91
KH
1635 {
1636 rtx addr = XEXP (x, 0);
1637
1638 fprintf (file, "@");
cc8ca59e 1639 output_address (GET_MODE (x), addr);
87e4ee91 1640
beed8fc0
AO
1641 /* Add a length suffix to constant addresses. Although this
1642 is often unnecessary, it helps to avoid ambiguity in the
1643 syntax of mova. If we wrote an insn like:
1644
1645 mova/w.l @(1,@foo.b),er0
1646
1647 then .b would be considered part of the symbol name.
1648 Adding a length after foo will avoid this. */
1649 if (CONSTANT_P (addr))
1650 switch (code)
1651 {
1652 case 'R':
1653 /* Used for mov.b and bit operations. */
1654 if (h8300_eightbit_constant_address_p (addr))
1655 {
1656 fprintf (file, ":8");
1657 break;
1658 }
1659
451c15c2
JL
1660 /* FALLTHRU */
1661
1662 /* We should not get here if we are processing bit
1663 operations on H8/300 or H8/300H because 'U'
1664 constraint does not allow bit operations on the
1665 tiny area on these machines. */
beed8fc0
AO
1666
1667 case 'X':
1668 case 'T':
1669 case 'S':
1670 if (h8300_constant_length (addr) == 2)
1671 fprintf (file, ":16");
1672 else
1673 fprintf (file, ":32");
1674 break;
1675 default:
1676 break;
1677 }
87e4ee91 1678 }
07aae5c2
SC
1679 break;
1680
1681 case CONST_INT:
1682 case SYMBOL_REF:
1683 case CONST:
1684 case LABEL_REF:
1685 fprintf (file, "#");
cc8ca59e 1686 h8300_print_operand_address (file, VOIDmode, x);
07aae5c2 1687 break;
808fbfac
JL
1688 case CONST_DOUBLE:
1689 {
1690 long val;
34a72c33 1691 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), val);
441d04c6 1692 fprintf (file, "#%ld", val);
808fbfac
JL
1693 break;
1694 }
441d04c6
KG
1695 default:
1696 break;
07aae5c2
SC
1697 }
1698 }
1699}
1700
88cb339e
N
1701/* Implements TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
1702
1703static bool
1704h8300_print_operand_punct_valid_p (unsigned char code)
1705{
1706 return (code == '#');
1707}
1708
07aae5c2
SC
1709/* Output assembly language output for the address ADDR to FILE. */
1710
88cb339e 1711static void
cc8ca59e 1712h8300_print_operand_address (FILE *file, machine_mode mode, rtx addr)
07aae5c2 1713{
beed8fc0
AO
1714 rtx index;
1715 int size;
1716
07aae5c2
SC
1717 switch (GET_CODE (addr))
1718 {
1719 case REG:
48837e29 1720 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
07aae5c2
SC
1721 break;
1722
1723 case PRE_DEC:
48837e29 1724 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
07aae5c2
SC
1725 break;
1726
1727 case POST_INC:
48837e29 1728 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
07aae5c2
SC
1729 break;
1730
beed8fc0
AO
1731 case PRE_INC:
1732 fprintf (file, "+%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1733 break;
1734
1735 case POST_DEC:
1736 fprintf (file, "%s-", h8_reg_names[REGNO (XEXP (addr, 0))]);
1737 break;
1738
07aae5c2
SC
1739 case PLUS:
1740 fprintf (file, "(");
beed8fc0
AO
1741
1742 index = h8300_get_index (XEXP (addr, 0), VOIDmode, &size);
1743 if (GET_CODE (index) == REG)
07aae5c2
SC
1744 {
1745 /* reg,foo */
cc8ca59e 1746 h8300_print_operand_address (file, mode, XEXP (addr, 1));
07aae5c2 1747 fprintf (file, ",");
beed8fc0
AO
1748 switch (size)
1749 {
1750 case 0:
cc8ca59e 1751 h8300_print_operand_address (file, mode, index);
beed8fc0
AO
1752 break;
1753
1754 case 1:
88cb339e 1755 h8300_print_operand (file, index, 'X');
beed8fc0
AO
1756 fputs (".b", file);
1757 break;
1758
1759 case 2:
88cb339e 1760 h8300_print_operand (file, index, 'T');
beed8fc0
AO
1761 fputs (".w", file);
1762 break;
1763
1764 case 4:
88cb339e 1765 h8300_print_operand (file, index, 'S');
beed8fc0
AO
1766 fputs (".l", file);
1767 break;
1768 }
88cb339e 1769 /* h8300_print_operand_address (file, XEXP (addr, 0)); */
07aae5c2
SC
1770 }
1771 else
1772 {
1773 /* foo+k */
cc8ca59e 1774 h8300_print_operand_address (file, mode, XEXP (addr, 0));
07aae5c2 1775 fprintf (file, "+");
cc8ca59e 1776 h8300_print_operand_address (file, mode, XEXP (addr, 1));
07aae5c2
SC
1777 }
1778 fprintf (file, ")");
1779 break;
1780
1781 case CONST_INT:
48837e29 1782 {
48837e29 1783 int n = INTVAL (addr);
7a770d8b 1784 fprintf (file, "%d", n);
48837e29
DE
1785 break;
1786 }
07aae5c2
SC
1787
1788 default:
1789 output_addr_const (file, addr);
1790 break;
1791 }
1792}
1793\f
07aae5c2
SC
1794/* Output all insn addresses and their sizes into the assembly language
1795 output file. This is helpful for debugging whether the length attributes
1796 in the md file are correct. This is not meant to be a user selectable
1797 option. */
1798
1799void
e7041fcc 1800final_prescan_insn (rtx_insn *insn, rtx *operand ATTRIBUTE_UNUSED,
cb713a8d 1801 int num_operands ATTRIBUTE_UNUSED)
07aae5c2
SC
1802{
1803 /* This holds the last insn address. */
1804 static int last_insn_address = 0;
1805
7798db98 1806 const int uid = INSN_UID (insn);
07aae5c2
SC
1807
1808 if (TARGET_ADDRESSES)
1809 {
9d98a694
AO
1810 fprintf (asm_out_file, "; 0x%x %d\n", INSN_ADDRESSES (uid),
1811 INSN_ADDRESSES (uid) - last_insn_address);
1812 last_insn_address = INSN_ADDRESSES (uid);
07aae5c2
SC
1813 }
1814}
1815
48837e29
DE
1816/* Prepare for an SI sized move. */
1817
1818int
1a793acf 1819h8300_expand_movsi (rtx operands[])
07aae5c2 1820{
48837e29
DE
1821 rtx src = operands[1];
1822 rtx dst = operands[0];
1823 if (!reload_in_progress && !reload_completed)
1824 {
1825 if (!register_operand (dst, GET_MODE (dst)))
1826 {
1827 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1828 emit_move_insn (tmp, src);
1829 operands[1] = tmp;
1830 }
1831 }
1832 return 0;
1833}
1834
7b5cbb57
AS
1835/* Given FROM and TO register numbers, say whether this elimination is allowed.
1836 Frame pointer elimination is automatically handled.
1837
1838 For the h8300, if frame pointer elimination is being done, we would like to
1839 convert ap and rp into sp, not fp.
1840
1841 All other eliminations are valid. */
1842
1843static bool
1844h8300_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1845{
1846 return (to == STACK_POINTER_REGNUM ? ! frame_pointer_needed : true);
1847}
1848
5efd84c5
NF
1849/* Conditionally modify register usage based on target flags. */
1850
1851static void
1852h8300_conditional_register_usage (void)
1853{
1854 if (!TARGET_MAC)
1855 fixed_regs[MAC_REG] = call_used_regs[MAC_REG] = 1;
1856}
1857
48837e29 1858/* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
07e4d94e
KH
1859 Define the offset between two registers, one to be eliminated, and
1860 the other its replacement, at the start of a routine. */
07aae5c2 1861
48837e29 1862int
cb713a8d 1863h8300_initial_elimination_offset (int from, int to)
48837e29 1864{
d18ad191
KH
1865 /* The number of bytes that the return address takes on the stack. */
1866 int pc_size = POINTER_SIZE / BITS_PER_UNIT;
48837e29 1867
1807b726
KH
1868 /* The number of bytes that the saved frame pointer takes on the stack. */
1869 int fp_size = frame_pointer_needed * UNITS_PER_WORD;
1870
1871 /* The number of bytes that the saved registers, excluding the frame
1872 pointer, take on the stack. */
1873 int saved_regs_size = 0;
48837e29 1874
1807b726
KH
1875 /* The number of bytes that the locals takes on the stack. */
1876 int frame_size = round_frame_size (get_frame_size ());
48837e29 1877
1807b726 1878 int regno;
48837e29 1879
1807b726
KH
1880 for (regno = 0; regno <= HARD_FRAME_POINTER_REGNUM; regno++)
1881 if (WORD_REG_USED (regno))
1882 saved_regs_size += UNITS_PER_WORD;
48837e29 1883
1807b726
KH
1884 /* Adjust saved_regs_size because the above loop took the frame
1885 pointer int account. */
1886 saved_regs_size -= fp_size;
39ba95b5 1887
8c440872 1888 switch (to)
1807b726 1889 {
8c440872 1890 case HARD_FRAME_POINTER_REGNUM:
1807b726
KH
1891 switch (from)
1892 {
1893 case ARG_POINTER_REGNUM:
1894 return pc_size + fp_size;
1895 case RETURN_ADDRESS_POINTER_REGNUM:
1896 return fp_size;
1897 case FRAME_POINTER_REGNUM:
1898 return -saved_regs_size;
1899 default:
8c440872 1900 gcc_unreachable ();
1807b726 1901 }
8c440872
NS
1902 break;
1903 case STACK_POINTER_REGNUM:
1807b726
KH
1904 switch (from)
1905 {
1906 case ARG_POINTER_REGNUM:
1907 return pc_size + saved_regs_size + frame_size;
1908 case RETURN_ADDRESS_POINTER_REGNUM:
1909 return saved_regs_size + frame_size;
1910 case FRAME_POINTER_REGNUM:
1911 return frame_size;
1912 default:
8c440872 1913 gcc_unreachable ();
1807b726 1914 }
8c440872
NS
1915 break;
1916 default:
1917 gcc_unreachable ();
1807b726 1918 }
8c440872 1919 gcc_unreachable ();
48837e29
DE
1920}
1921
e9eba255
KH
1922/* Worker function for RETURN_ADDR_RTX. */
1923
1aae372e 1924rtx
cb713a8d 1925h8300_return_addr_rtx (int count, rtx frame)
1aae372e
JL
1926{
1927 rtx ret;
1928
1929 if (count == 0)
1930 ret = gen_rtx_MEM (Pmode,
1931 gen_rtx_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM));
1932 else if (flag_omit_frame_pointer)
1933 return (rtx) 0;
1934 else
1935 ret = gen_rtx_MEM (Pmode,
1936 memory_address (Pmode,
0a81f074
RS
1937 plus_constant (Pmode, frame,
1938 UNITS_PER_WORD)));
1aae372e
JL
1939 set_mem_alias_set (ret, get_frame_alias_set ());
1940 return ret;
1941}
1942
f16897cb
AL
1943
1944machine_mode
1945h8300_select_cc_mode (enum rtx_code cond, rtx op0, rtx op1)
1946{
1947 if (op1 == const0_rtx
1948 && (cond == EQ || cond == NE || cond == LT || cond == GE)
1949 && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1950 || GET_CODE (op0) == NEG || GET_CODE (op0) == AND
1951 || GET_CODE (op0) == IOR || GET_CODE (op0) == XOR
1952 || GET_CODE (op0) == NOT || GET_CODE (op0) == ASHIFT
629cbc68 1953 || GET_CODE (op0) == MULT || GET_CODE (op0) == SYMBOL_REF
6fcba9ef
JL
1954 || GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND
1955 || REG_P (op0) || MEM_P (op0)))
f16897cb
AL
1956 return CCZNmode;
1957
1958 return CCmode;
1959}
1960
beed8fc0
AO
1961\f
1962/* Given that X occurs in an address of the form (plus X constant),
1963 return the part of X that is expected to be a register. There are
1964 four kinds of addressing mode to recognize:
1965
1966 @(dd,Rn)
1967 @(dd,RnL.b)
1968 @(dd,Rn.w)
1969 @(dd,ERn.l)
1970
1971 If SIZE is nonnull, and the address is one of the last three forms,
1972 set *SIZE to the index multiplication factor. Set it to 0 for
1973 plain @(dd,Rn) addresses.
1974
1975 MODE is the mode of the value being accessed. It can be VOIDmode
1976 if the address is known to be valid, but its mode is unknown. */
1977
f52d97da 1978static rtx
ef4bddc2 1979h8300_get_index (rtx x, machine_mode mode, int *size)
beed8fc0
AO
1980{
1981 int dummy, factor;
1982
1983 if (size == 0)
1984 size = &dummy;
1985
1986 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
1987 if (TARGET_H8300SX
1988 && factor <= 4
1989 && (mode == VOIDmode
1990 || GET_MODE_CLASS (mode) == MODE_INT
1991 || GET_MODE_CLASS (mode) == MODE_FLOAT))
1992 {
1993 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
1994 {
1995 /* When accessing byte-sized values, the index can be
1996 a zero-extended QImode or HImode register. */
1997 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
1998 return XEXP (x, 0);
1999 }
2000 else
2001 {
2002 /* We're looking for addresses of the form:
2003
2004 (mult X I)
2005 or (mult (zero_extend X) I)
2006
2007 where I is the size of the operand being accessed.
2008 The canonical form of the second expression is:
2009
2010 (and (mult (subreg X) I) J)
2011
2012 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2013 rtx index;
2014
2015 if (GET_CODE (x) == AND
2016 && GET_CODE (XEXP (x, 1)) == CONST_INT
2017 && (factor == 0
2018 || INTVAL (XEXP (x, 1)) == 0xff * factor
2019 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2020 {
2021 index = XEXP (x, 0);
2022 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2023 }
2024 else
2025 {
2026 index = x;
2027 *size = 4;
2028 }
2029
2030 if (GET_CODE (index) == MULT
2031 && GET_CODE (XEXP (index, 1)) == CONST_INT
2032 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2033 return XEXP (index, 0);
2034 }
2035 }
2036 *size = 0;
2037 return x;
2038}
2039\f
f52d97da
AS
2040/* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
2041
2042 On the H8/300, the predecrement and postincrement address depend thus
2043 (the amount of decrement or increment being the length of the operand). */
2044
2045static bool
5bfed9a9
GJL
2046h8300_mode_dependent_address_p (const_rtx addr,
2047 addr_space_t as ATTRIBUTE_UNUSED)
f52d97da
AS
2048{
2049 if (GET_CODE (addr) == PLUS
2050 && h8300_get_index (XEXP (addr, 0), VOIDmode, 0) != XEXP (addr, 0))
2051 return true;
2052
2053 return false;
2054}
2055\f
beed8fc0
AO
2056static const h8300_length_table addb_length_table =
2057{
2058 /* #xx Rs @aa @Rs @xx */
2059 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2060 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2061 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2062 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2063};
2064
2065static const h8300_length_table addw_length_table =
2066{
2067 /* #xx Rs @aa @Rs @xx */
2068 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2069 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2070 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2071 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2072};
2073
2074static const h8300_length_table addl_length_table =
2075{
2076 /* #xx Rs @aa @Rs @xx */
2077 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2078 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2079 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2080 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2081};
2082
2083#define logicb_length_table addb_length_table
2084#define logicw_length_table addw_length_table
2085
2086static const h8300_length_table logicl_length_table =
2087{
2088 /* #xx Rs @aa @Rs @xx */
2089 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2090 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2091 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2092 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2093};
2094
2095static const h8300_length_table movb_length_table =
2096{
2097 /* #xx Rs @aa @Rs @xx */
2098 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2099 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2100 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2101 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2102};
2103
2104#define movw_length_table movb_length_table
2105
2106static const h8300_length_table movl_length_table =
2107{
2108 /* #xx Rs @aa @Rs @xx */
2109 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2110 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2111 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2112 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2113};
2114
2115/* Return the size of the given address or displacement constant. */
2116
2117static unsigned int
2118h8300_constant_length (rtx constant)
2119{
2120 /* Check for (@d:16,Reg). */
2121 if (GET_CODE (constant) == CONST_INT
2122 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2123 return 2;
2124
2125 /* Check for (@d:16,Reg) in cases where the displacement is
2126 an absolute address. */
2127 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2128 return 2;
2129
2130 return 4;
2131}
2132
2133/* Return the size of a displacement field in address ADDR, which should
2134 have the form (plus X constant). SIZE is the number of bytes being
2135 accessed. */
2136
2137static unsigned int
2138h8300_displacement_length (rtx addr, int size)
2139{
2140 rtx offset;
2141
2142 offset = XEXP (addr, 1);
2143
2144 /* Check for @(d:2,Reg). */
2145 if (register_operand (XEXP (addr, 0), VOIDmode)
2146 && GET_CODE (offset) == CONST_INT
2147 && (INTVAL (offset) == size
2148 || INTVAL (offset) == size * 2
2149 || INTVAL (offset) == size * 3))
2150 return 0;
2151
2152 return h8300_constant_length (offset);
2153}
2154
0a2aaacc
KG
2155/* Store the class of operand OP in *OPCLASS and return the length of any
2156 extra operand fields. SIZE is the number of bytes in OP. OPCLASS
beed8fc0
AO
2157 can be null if only the length is needed. */
2158
2159static unsigned int
0a2aaacc 2160h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
beed8fc0
AO
2161{
2162 enum h8300_operand_class dummy;
2163
0a2aaacc
KG
2164 if (opclass == 0)
2165 opclass = &dummy;
beed8fc0
AO
2166
2167 if (CONSTANT_P (op))
2168 {
0a2aaacc 2169 *opclass = H8OP_IMMEDIATE;
beed8fc0
AO
2170
2171 /* Byte-sized immediates are stored in the opcode fields. */
2172 if (size == 1)
2173 return 0;
2174
2175 /* If this is a 32-bit instruction, see whether the constant
2176 will fit into a 16-bit immediate field. */
2177 if (TARGET_H8300SX
2178 && size == 4
2179 && GET_CODE (op) == CONST_INT
2180 && IN_RANGE (INTVAL (op), 0, 0xffff))
2181 return 2;
2182
2183 return size;
2184 }
2185 else if (GET_CODE (op) == MEM)
2186 {
2187 op = XEXP (op, 0);
2188 if (CONSTANT_P (op))
2189 {
0a2aaacc 2190 *opclass = H8OP_MEM_ABSOLUTE;
beed8fc0
AO
2191 return h8300_constant_length (op);
2192 }
2193 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2194 {
0a2aaacc 2195 *opclass = H8OP_MEM_COMPLEX;
beed8fc0
AO
2196 return h8300_displacement_length (op, size);
2197 }
2198 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2199 {
0a2aaacc 2200 *opclass = H8OP_MEM_COMPLEX;
beed8fc0
AO
2201 return 0;
2202 }
2203 else if (register_operand (op, VOIDmode))
2204 {
0a2aaacc 2205 *opclass = H8OP_MEM_BASE;
beed8fc0
AO
2206 return 0;
2207 }
2208 }
8c440872 2209 gcc_assert (register_operand (op, VOIDmode));
0a2aaacc 2210 *opclass = H8OP_REGISTER;
8c440872 2211 return 0;
beed8fc0
AO
2212}
2213
2214/* Return the length of the instruction described by TABLE given that
2215 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2216 and OP2 must be an h8300_src_operand. */
2217
2218static unsigned int
2219h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2220{
2221 enum h8300_operand_class op1_class, op2_class;
2222 unsigned int size, immediate_length;
2223
2224 size = GET_MODE_SIZE (GET_MODE (op1));
2225 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2226 + h8300_classify_operand (op2, size, &op2_class));
2227 return immediate_length + (*table)[op1_class - 1][op2_class];
2228}
2229
2230/* Return the length of a unary instruction such as neg or not given that
2231 its operand is OP. */
2232
2233unsigned int
2234h8300_unary_length (rtx op)
2235{
0a2aaacc 2236 enum h8300_operand_class opclass;
beed8fc0
AO
2237 unsigned int size, operand_length;
2238
2239 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc
KG
2240 operand_length = h8300_classify_operand (op, size, &opclass);
2241 switch (opclass)
beed8fc0
AO
2242 {
2243 case H8OP_REGISTER:
2244 return 2;
2245
2246 case H8OP_MEM_BASE:
2247 return (size == 4 ? 6 : 4);
2248
2249 case H8OP_MEM_ABSOLUTE:
2250 return operand_length + (size == 4 ? 6 : 4);
2251
2252 case H8OP_MEM_COMPLEX:
2253 return operand_length + 6;
2254
2255 default:
8c440872 2256 gcc_unreachable ();
beed8fc0
AO
2257 }
2258}
2259
2260/* Likewise short immediate instructions such as add.w #xx:3,OP. */
2261
2262static unsigned int
2263h8300_short_immediate_length (rtx op)
2264{
0a2aaacc 2265 enum h8300_operand_class opclass;
beed8fc0
AO
2266 unsigned int size, operand_length;
2267
2268 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc 2269 operand_length = h8300_classify_operand (op, size, &opclass);
beed8fc0 2270
0a2aaacc 2271 switch (opclass)
beed8fc0
AO
2272 {
2273 case H8OP_REGISTER:
2274 return 2;
2275
2276 case H8OP_MEM_BASE:
2277 case H8OP_MEM_ABSOLUTE:
2278 case H8OP_MEM_COMPLEX:
2279 return 4 + operand_length;
2280
2281 default:
8c440872 2282 gcc_unreachable ();
beed8fc0
AO
2283 }
2284}
2285
2286/* Likewise bitfield load and store instructions. */
48837e29 2287
beed8fc0
AO
2288static unsigned int
2289h8300_bitfield_length (rtx op, rtx op2)
2290{
0a2aaacc 2291 enum h8300_operand_class opclass;
beed8fc0
AO
2292 unsigned int size, operand_length;
2293
2294 if (GET_CODE (op) == REG)
2295 op = op2;
8c440872 2296 gcc_assert (GET_CODE (op) != REG);
beed8fc0
AO
2297
2298 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc 2299 operand_length = h8300_classify_operand (op, size, &opclass);
beed8fc0 2300
0a2aaacc 2301 switch (opclass)
beed8fc0
AO
2302 {
2303 case H8OP_MEM_BASE:
2304 case H8OP_MEM_ABSOLUTE:
2305 case H8OP_MEM_COMPLEX:
2306 return 4 + operand_length;
2307
2308 default:
8c440872 2309 gcc_unreachable ();
07aae5c2 2310 }
48837e29
DE
2311}
2312
beed8fc0 2313/* Calculate the length of general binary instruction INSN using TABLE. */
8ccf5d5f 2314
beed8fc0 2315static unsigned int
e7041fcc 2316h8300_binary_length (rtx_insn *insn, const h8300_length_table *table)
8ccf5d5f 2317{
beed8fc0 2318 rtx set;
f16897cb
AL
2319 rtx pattern;
2320
2321 if (GET_CODE (insn) != INSN)
2322 gcc_unreachable ();
beed8fc0 2323
f16897cb
AL
2324 pattern = PATTERN (insn);
2325 if (GET_CODE (pattern) == PARALLEL
2326 && GET_CODE (XVECEXP (pattern, 0, 0)) == SET
2327 && GET_CODE (SET_SRC (XVECEXP (pattern, 0, 0))) == COMPARE)
2328 set = XVECEXP (pattern, 0, 1);
2329 else
2330 set = single_set (insn);
8c440872 2331 gcc_assert (set);
beed8fc0
AO
2332
2333 if (BINARY_P (SET_SRC (set)))
2334 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2335 XEXP (SET_SRC (set), 1), table);
beed8fc0 2336 else
8c440872
NS
2337 {
2338 gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2339 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2340 XEXP (XEXP (SET_SRC (set), 1), 1),
2341 table);
2342 }
8ccf5d5f
KH
2343}
2344
beed8fc0
AO
2345/* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2346 memory reference and either (1) it has the form @(d:16,Rn) or
2347 (2) its address has the code given by INC_CODE. */
8ccf5d5f 2348
beed8fc0
AO
2349static bool
2350h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
8ccf5d5f 2351{
beed8fc0
AO
2352 rtx addr;
2353 unsigned int size;
2354
2355 if (GET_CODE (op) != MEM)
2356 return false;
2357
2358 addr = XEXP (op, 0);
2359 size = GET_MODE_SIZE (GET_MODE (op));
2360 if (size != 1 && size != 2)
2361 return false;
2362
2363 return (GET_CODE (addr) == inc_code
2364 || (GET_CODE (addr) == PLUS
2365 && GET_CODE (XEXP (addr, 0)) == REG
2366 && h8300_displacement_length (addr, size) == 2));
8ccf5d5f
KH
2367}
2368
beed8fc0
AO
2369/* Calculate the length of move instruction INSN using the given length
2370 table. Although the tables are correct for most cases, there is some
2371 irregularity in the length of mov.b and mov.w. The following forms:
8ccf5d5f 2372
beed8fc0
AO
2373 mov @ERs+, Rd
2374 mov @(d:16,ERs), Rd
2375 mov Rs, @-ERd
2376 mov Rs, @(d:16,ERd)
2377
2378 are two bytes shorter than most other "mov Rs, @complex" or
2379 "mov @complex,Rd" combinations. */
2380
2381static unsigned int
2382h8300_move_length (rtx *operands, const h8300_length_table *table)
8ccf5d5f 2383{
beed8fc0
AO
2384 unsigned int size;
2385
2386 size = h8300_length_from_table (operands[0], operands[1], table);
2387 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2388 size -= 2;
2389 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2390 size -= 2;
2391 return size;
8ccf5d5f
KH
2392}
2393
beed8fc0
AO
2394/* Return the length of a mova instruction with the given operands.
2395 DEST is the register destination, SRC is the source address and
2396 OFFSET is the 16-bit or 32-bit displacement. */
2873836b 2397
beed8fc0
AO
2398static unsigned int
2399h8300_mova_length (rtx dest, rtx src, rtx offset)
2873836b 2400{
beed8fc0
AO
2401 unsigned int size;
2402
2403 size = (2
2404 + h8300_constant_length (offset)
2405 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2406 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2407 size += 2;
2408 return size;
2873836b
KH
2409}
2410
beed8fc0
AO
2411/* Compute the length of INSN based on its length_table attribute.
2412 OPERANDS is the array of its operands. */
2873836b 2413
beed8fc0 2414unsigned int
e7041fcc 2415h8300_insn_length_from_table (rtx_insn *insn, rtx * operands)
2873836b 2416{
beed8fc0
AO
2417 switch (get_attr_length_table (insn))
2418 {
2419 case LENGTH_TABLE_NONE:
8c440872 2420 gcc_unreachable ();
beed8fc0 2421
c3c3ddbb
JL
2422 case LENGTH_TABLE_ADD:
2423 if (GET_MODE (operands[0]) == QImode)
2424 return h8300_binary_length (insn, &addb_length_table);
2425 else if (GET_MODE (operands[0]) == HImode)
2426 return h8300_binary_length (insn, &addw_length_table);
2427 else if (GET_MODE (operands[0]) == SImode)
2428 return h8300_binary_length (insn, &addl_length_table);
2429 gcc_unreachable ();
beed8fc0
AO
2430
2431 case LENGTH_TABLE_LOGICB:
2432 return h8300_binary_length (insn, &logicb_length_table);
2433
2434 case LENGTH_TABLE_MOVB:
2435 return h8300_move_length (operands, &movb_length_table);
2436
2437 case LENGTH_TABLE_MOVW:
2438 return h8300_move_length (operands, &movw_length_table);
2439
2440 case LENGTH_TABLE_MOVL:
2441 return h8300_move_length (operands, &movl_length_table);
2442
2443 case LENGTH_TABLE_MOVA:
2444 return h8300_mova_length (operands[0], operands[1], operands[2]);
2445
2446 case LENGTH_TABLE_MOVA_ZERO:
2447 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2448
2449 case LENGTH_TABLE_UNARY:
2450 return h8300_unary_length (operands[0]);
2451
2452 case LENGTH_TABLE_MOV_IMM4:
2453 return 2 + h8300_classify_operand (operands[0], 0, 0);
2454
2455 case LENGTH_TABLE_SHORT_IMMEDIATE:
2456 return h8300_short_immediate_length (operands[0]);
2457
2458 case LENGTH_TABLE_BITFIELD:
2459 return h8300_bitfield_length (operands[0], operands[1]);
2460
2461 case LENGTH_TABLE_BITBRANCH:
2462 return h8300_bitfield_length (operands[1], operands[2]) - 2;
8c440872
NS
2463
2464 default:
2465 gcc_unreachable ();
beed8fc0 2466 }
2873836b
KH
2467}
2468
beed8fc0
AO
2469/* Return true if LHS and RHS are memory references that can be mapped
2470 to the same h8sx assembly operand. LHS appears as the destination of
2471 an instruction and RHS appears as a source.
f9d2de4d 2472
beed8fc0
AO
2473 Three cases are allowed:
2474
2475 - RHS is @+Rn or @-Rn, LHS is @Rn
2476 - RHS is @Rn, LHS is @Rn+ or @Rn-
2477 - RHS and LHS have the same address and neither has side effects. */
2478
2479bool
2480h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
f9d2de4d 2481{
beed8fc0
AO
2482 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2483 {
2484 rhs = XEXP (rhs, 0);
2485 lhs = XEXP (lhs, 0);
2486
2487 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2488 return rtx_equal_p (XEXP (rhs, 0), lhs);
2489
2490 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2491 return rtx_equal_p (rhs, XEXP (lhs, 0));
2492
2493 if (rtx_equal_p (rhs, lhs))
2494 return true;
2495 }
2496 return false;
f9d2de4d
KH
2497}
2498
beed8fc0
AO
2499/* Return true if OPERANDS[1] can be mapped to the same assembly
2500 operand as OPERANDS[0]. */
f9d2de4d 2501
beed8fc0
AO
2502bool
2503h8300_operands_match_p (rtx *operands)
f9d2de4d 2504{
beed8fc0
AO
2505 if (register_operand (operands[0], VOIDmode)
2506 && register_operand (operands[1], VOIDmode))
2507 return true;
f9d2de4d 2508
beed8fc0
AO
2509 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2510 return true;
2511
2512 return false;
f9d2de4d 2513}
beed8fc0 2514\f
7948a9ea
KH
2515/* Return the length of mov instruction. */
2516
2517unsigned int
2518compute_mov_length (rtx *operands)
2519{
2520 /* If the mov instruction involves a memory operand, we compute the
2521 length, assuming the largest addressing mode is used, and then
2522 adjust later in the function. Otherwise, we compute and return
2523 the exact length in one step. */
ef4bddc2 2524 machine_mode mode = GET_MODE (operands[0]);
7948a9ea
KH
2525 rtx dest = operands[0];
2526 rtx src = operands[1];
2527 rtx addr;
2528
2529 if (GET_CODE (src) == MEM)
2530 addr = XEXP (src, 0);
2531 else if (GET_CODE (dest) == MEM)
2532 addr = XEXP (dest, 0);
2533 else
2534 addr = NULL_RTX;
2535
27c1afd5 2536 unsigned int base_length;
7948a9ea 2537
27c1afd5
JL
2538 switch (mode)
2539 {
2540 case E_QImode:
2541 if (addr == NULL_RTX)
2542 return 2;
7948a9ea 2543
27c1afd5
JL
2544 /* The eightbit addressing is available only in QImode, so
2545 go ahead and take care of it. */
2546 if (h8300_eightbit_constant_address_p (addr))
2547 return 2;
7948a9ea 2548
27c1afd5 2549 base_length = 8;
7948a9ea
KH
2550 break;
2551
27c1afd5
JL
2552 case E_HImode:
2553 if (addr == NULL_RTX)
2554 {
2555 if (REG_P (src))
2556 return 2;
7948a9ea 2557
27c1afd5
JL
2558 if (src == const0_rtx)
2559 return 2;
7948a9ea 2560
27c1afd5
JL
2561 return 4;
2562 }
7948a9ea 2563
27c1afd5
JL
2564 base_length = 8;
2565 break;
7948a9ea 2566
27c1afd5
JL
2567 case E_SImode:
2568 if (addr == NULL_RTX)
2569 {
2570 if (REG_P (src))
7948a9ea 2571 {
27c1afd5 2572 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
7948a9ea 2573 return 4;
27c1afd5
JL
2574 else
2575 return 2;
7948a9ea
KH
2576 }
2577
27c1afd5 2578 if (GET_CODE (src) == CONST_INT)
7948a9ea 2579 {
27c1afd5
JL
2580 int val = INTVAL (src);
2581
2582 if (val == 0)
2583 return 2;
7948a9ea 2584
27c1afd5 2585 if (val == (val & 0x00ff) || val == (val & 0xff00))
2c4a71b3
KH
2586 return 4;
2587
27c1afd5
JL
2588 switch (val & 0xffffffff)
2589 {
2590 case 0xffffffff:
2591 case 0xfffffffe:
2592 case 0xfffffffc:
2593 case 0x0000ffff:
2594 case 0x0000fffe:
2595 case 0xffff0000:
2596 case 0xfffe0000:
2597 case 0x00010000:
2598 case 0x00020000:
2599 return 4;
2600 }
7948a9ea 2601 }
27c1afd5 2602 return 6;
7948a9ea
KH
2603 }
2604
27c1afd5
JL
2605 base_length = 10;
2606 break;
7948a9ea 2607
27c1afd5
JL
2608 case E_SFmode:
2609 if (addr == NULL_RTX)
7948a9ea 2610 {
27c1afd5
JL
2611 if (REG_P (src))
2612 return 2;
7948a9ea 2613
27c1afd5 2614 if (satisfies_constraint_G (src))
7948a9ea
KH
2615 return 2;
2616
27c1afd5
JL
2617 return 6;
2618 }
7948a9ea 2619
27c1afd5
JL
2620 base_length = 10;
2621 break;
80e58519 2622
27c1afd5
JL
2623 default:
2624 gcc_unreachable ();
2625 }
7948a9ea 2626
27c1afd5
JL
2627 /* Adjust the length based on the addressing mode used.
2628 Specifically, we subtract the difference between the actual
2629 length and the longest one, which is @(d:24,ERs). */
7948a9ea 2630
27c1afd5
JL
2631 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
2632 if (GET_CODE (addr) == PRE_DEC
2633 || GET_CODE (addr) == POST_INC)
2634 return base_length - 6;
7948a9ea 2635
27c1afd5
JL
2636 /* @ERs and @ERd are 6 bytes shorter than the longest. */
2637 if (GET_CODE (addr) == REG)
2638 return base_length - 6;
2c4a71b3 2639
27c1afd5
JL
2640 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2641 longest. */
2642 if (GET_CODE (addr) == PLUS
2643 && GET_CODE (XEXP (addr, 0)) == REG
2644 && GET_CODE (XEXP (addr, 1)) == CONST_INT
2645 && INTVAL (XEXP (addr, 1)) > -32768
2646 && INTVAL (XEXP (addr, 1)) < 32767)
2647 return base_length - 4;
7948a9ea 2648
27c1afd5
JL
2649 /* @aa:16 is 4 bytes shorter than the longest. */
2650 if (h8300_tiny_constant_address_p (addr))
2651 return base_length - 4;
7948a9ea 2652
27c1afd5
JL
2653 /* @aa:24 is 2 bytes shorter than the longest. */
2654 if (CONSTANT_P (addr))
2655 return base_length - 2;
7948a9ea 2656
27c1afd5 2657 return base_length;
7948a9ea
KH
2658}
2659\f
e9eba255
KH
2660/* Output an addition insn. */
2661
366a7b27 2662const char *
f16897cb 2663output_plussi (rtx *operands, bool need_flags)
7d6ac401 2664{
ef4bddc2 2665 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2666
8c440872 2667 gcc_assert (mode == SImode);
7d6ac401 2668
27c1afd5
JL
2669 if (GET_CODE (operands[2]) == CONST_INT
2670 && register_operand (operands[1], VOIDmode))
7d6ac401 2671 {
27c1afd5 2672 HOST_WIDE_INT intval = INTVAL (operands[2]);
7d6ac401 2673
27c1afd5
JL
2674 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2675 return "add.l\t%S2,%S0";
2676 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2677 return "sub.l\t%G2,%S0";
beed8fc0 2678
27c1afd5 2679 /* See if we can finish with 2 bytes. */
7d6ac401 2680
27c1afd5
JL
2681 switch ((unsigned int) intval & 0xffffffff)
2682 {
f16897cb
AL
2683 /* INC/DEC set the flags, but adds/subs do not. So if we
2684 need flags, use the former and not the latter. */
27c1afd5 2685 case 0x00000001:
f16897cb
AL
2686 if (need_flags)
2687 return "inc.l\t#1,%S0";
2688 else
2689 return "adds\t%2,%S0";
27c1afd5 2690 case 0x00000002:
f16897cb
AL
2691 if (need_flags)
2692 return "inc.l\t#2,%S0";
2693 else
2694 return "adds\t%2,%S0";
27c1afd5 2695 case 0xffffffff:
f16897cb
AL
2696 if (need_flags)
2697 return "dec.l\t#1,%S0";
2698 else
2699 return "subs\t%G2,%S0";
27c1afd5 2700 case 0xfffffffe:
f16897cb
AL
2701 if (need_flags)
2702 return "dec.l\t#2,%S0";
2703 else
2704 return "subs\t%G2,%S0";
2705
2706 /* These six cases have optimized paths when we do not
2707 need flags. Otherwise we let them fallthru. */
2708 case 0x00000004:
2709 if (!need_flags)
2710 return "adds\t%2,%S0";
2711
12bb62fb
JL
2712 /* FALLTHRU */
2713
27c1afd5 2714 case 0xfffffffc:
f16897cb
AL
2715 if (!need_flags)
2716 return "subs\t%G2,%S0";
27c1afd5 2717
12bb62fb
JL
2718 /* FALLTHRU */
2719
27c1afd5
JL
2720 case 0x00010000:
2721 case 0x00020000:
f16897cb
AL
2722 if (!need_flags)
2723 {
2724 operands[2] = GEN_INT (intval >> 16);
2725 return "inc.w\t%2,%e0";
2726 }
27c1afd5 2727
12bb62fb
JL
2728 /* FALLTHRU */
2729
27c1afd5
JL
2730 case 0xffff0000:
2731 case 0xfffe0000:
f16897cb
AL
2732 if (!need_flags)
2733 {
2734 operands[2] = GEN_INT (intval >> 16);
2735 return "dec.w\t%G2,%e0";
2736 }
12bb62fb
JL
2737
2738 /* FALLTHRU */
2739
7d6ac401
KH
2740 }
2741
27c1afd5
JL
2742 /* See if we can finish with 4 bytes. */
2743 if ((intval & 0xffff) == 0)
beed8fc0 2744 {
27c1afd5
JL
2745 operands[2] = GEN_INT (intval >> 16);
2746 return "add.w\t%2,%e0";
beed8fc0 2747 }
7d6ac401 2748 }
27c1afd5
JL
2749
2750 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
2751 {
2752 operands[2] = GEN_INT (-INTVAL (operands[2]));
2753 return "sub.l\t%S2,%S0";
2754 }
2755 return "add.l\t%S2,%S0";
7d6ac401
KH
2756}
2757
beed8fc0
AO
2758/* ??? It would be much easier to add the h8sx stuff if a single function
2759 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
e9eba255
KH
2760/* Compute the length of an addition insn. */
2761
7d6ac401 2762unsigned int
f16897cb 2763compute_plussi_length (rtx *operands, bool need_flags)
7d6ac401 2764{
ef4bddc2 2765 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2766
8c440872 2767 gcc_assert (mode == SImode);
7d6ac401 2768
27c1afd5
JL
2769 if (GET_CODE (operands[2]) == CONST_INT
2770 && register_operand (operands[1], VOIDmode))
7d6ac401 2771 {
27c1afd5 2772 HOST_WIDE_INT intval = INTVAL (operands[2]);
cfedf91b 2773
27c1afd5
JL
2774 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2775 return 2;
2776 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2777 return 2;
cfedf91b 2778
27c1afd5 2779 /* See if we can finish with 2 bytes. */
cfedf91b 2780
27c1afd5 2781 switch ((unsigned int) intval & 0xffffffff)
7d6ac401 2782 {
27c1afd5
JL
2783 case 0x00000001:
2784 case 0x00000002:
27c1afd5 2785 return 2;
f16897cb
AL
2786 case 0x00000004:
2787 if (need_flags)
2788 return 6;
2789 else
2790 return 2;
27c1afd5
JL
2791
2792 case 0xffffffff:
2793 case 0xfffffffe:
27c1afd5 2794 return 2;
f16897cb
AL
2795 case 0xfffffffc:
2796 if (need_flags)
2797 return 6;
2798 else
2799 return 2;
27c1afd5
JL
2800
2801 case 0x00010000:
2802 case 0x00020000:
f16897cb
AL
2803 if (!need_flags)
2804 return 2;
27c1afd5 2805
12bb62fb
JL
2806 /* FALLTHRU */
2807
27c1afd5
JL
2808 case 0xffff0000:
2809 case 0xfffe0000:
f16897cb
AL
2810 if (!need_flags)
2811 return 2;
12bb62fb
JL
2812
2813 /* FALLTHRU */
2814
7d6ac401
KH
2815 }
2816
27c1afd5
JL
2817 /* See if we can finish with 4 bytes. */
2818 if ((intval & 0xffff) == 0)
2819 return 4;
7d6ac401 2820 }
27c1afd5
JL
2821
2822 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
2823 return h8300_length_from_table (operands[0],
2824 GEN_INT (-INTVAL (operands[2])),
2825 &addl_length_table);
2826 else
2827 return h8300_length_from_table (operands[0], operands[2],
2828 &addl_length_table);
7d6ac401
KH
2829}
2830
e9eba255
KH
2831/* Compute which flag bits are valid after an addition insn. */
2832
5ddb6eca 2833enum attr_old_cc
cb713a8d 2834compute_plussi_cc (rtx *operands)
7d6ac401 2835{
ef4bddc2 2836 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2837
8c440872 2838 gcc_assert (mode == SImode);
7d6ac401 2839
27c1afd5
JL
2840 if (GET_CODE (operands[2]) == CONST_INT
2841 && register_operand (operands[1], VOIDmode))
7d6ac401 2842 {
27c1afd5 2843 HOST_WIDE_INT intval = INTVAL (operands[2]);
7d6ac401 2844
27c1afd5 2845 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
5ddb6eca 2846 return OLD_CC_SET_ZN;
27c1afd5 2847 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
5ddb6eca 2848 return OLD_CC_SET_ZN;
beed8fc0 2849
27c1afd5 2850 /* See if we can finish with 2 bytes. */
7d6ac401 2851
27c1afd5
JL
2852 switch ((unsigned int) intval & 0xffffffff)
2853 {
2854 case 0x00000001:
2855 case 0x00000002:
2856 case 0x00000004:
5ddb6eca 2857 return OLD_CC_NONE_0HIT;
27c1afd5
JL
2858
2859 case 0xffffffff:
2860 case 0xfffffffe:
2861 case 0xfffffffc:
5ddb6eca 2862 return OLD_CC_NONE_0HIT;
27c1afd5
JL
2863
2864 case 0x00010000:
2865 case 0x00020000:
5ddb6eca 2866 return OLD_CC_CLOBBER;
27c1afd5
JL
2867
2868 case 0xffff0000:
2869 case 0xfffe0000:
5ddb6eca 2870 return OLD_CC_CLOBBER;
7d6ac401
KH
2871 }
2872
27c1afd5
JL
2873 /* See if we can finish with 4 bytes. */
2874 if ((intval & 0xffff) == 0)
5ddb6eca 2875 return OLD_CC_CLOBBER;
7d6ac401 2876 }
27c1afd5 2877
5ddb6eca 2878 return OLD_CC_SET_ZN;
7d6ac401
KH
2879}
2880\f
e9eba255
KH
2881/* Output a logical insn. */
2882
7d6ac401 2883const char *
4ea5fe8b 2884output_logical_op (machine_mode mode, rtx_code code, rtx *operands)
366a7b27
KH
2885{
2886 /* Pretend that every byte is affected if both operands are registers. */
7798db98 2887 const unsigned HOST_WIDE_INT intval =
366a7b27 2888 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
2889 /* Always use the full instruction if the
2890 first operand is in memory. It is better
2891 to use define_splits to generate the shorter
2892 sequence where valid. */
2893 && register_operand (operands[1], VOIDmode)
366a7b27
KH
2894 ? INTVAL (operands[2]) : 0x55555555);
2895 /* The determinant of the algorithm. If we perform an AND, 0
2896 affects a bit. Otherwise, 1 affects a bit. */
7798db98 2897 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
2898 /* Break up DET into pieces. */
2899 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
2900 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
2901 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
2902 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
2903 int lower_half_easy_p = 0;
2904 int upper_half_easy_p = 0;
366a7b27
KH
2905 /* The name of an insn. */
2906 const char *opname;
2907 char insn_buf[100];
2908
2909 switch (code)
2910 {
2911 case AND:
2912 opname = "and";
2913 break;
2914 case IOR:
2915 opname = "or";
2916 break;
2917 case XOR:
2918 opname = "xor";
2919 break;
2920 default:
8c440872 2921 gcc_unreachable ();
366a7b27
KH
2922 }
2923
2924 switch (mode)
2925 {
4ea5fe8b
JL
2926 case E_QImode:
2927 sprintf (insn_buf, "%s.b\t%%X2,%%X0", opname);
2928 output_asm_insn (insn_buf, operands);
2929 break;
4e10a5a7 2930 case E_HImode:
366a7b27 2931 /* First, see if we can finish with one insn. */
27c1afd5 2932 if (b0 != 0 && b1 != 0)
366a7b27
KH
2933 {
2934 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
2935 output_asm_insn (insn_buf, operands);
2936 }
2937 else
2938 {
2939 /* Take care of the lower byte. */
c5e7ce43 2940 if (b0 != 0)
366a7b27
KH
2941 {
2942 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
2943 output_asm_insn (insn_buf, operands);
2944 }
2945 /* Take care of the upper byte. */
c5e7ce43 2946 if (b1 != 0)
366a7b27
KH
2947 {
2948 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
2949 output_asm_insn (insn_buf, operands);
2950 }
2951 }
2952 break;
4e10a5a7 2953 case E_SImode:
27c1afd5
JL
2954 /* Determine if the lower half can be taken care of in no more
2955 than two bytes. */
2956 lower_half_easy_p = (b0 == 0
2957 || b1 == 0
2958 || (code != IOR && w0 == 0xffff));
2959
2960 /* Determine if the upper half can be taken care of in no more
2961 than two bytes. */
2962 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
2963 || (code == AND && w1 == 0xff00));
366a7b27 2964
f9ac2f95
KH
2965 /* Check if doing everything with one insn is no worse than
2966 using multiple insns. */
27c1afd5 2967 if (w0 != 0 && w1 != 0
472f2723
KH
2968 && !(lower_half_easy_p && upper_half_easy_p)
2969 && !(code == IOR && w1 == 0xffff
2970 && (w0 & 0x8000) != 0 && lower_half_easy_p))
366a7b27
KH
2971 {
2972 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
2973 output_asm_insn (insn_buf, operands);
2974 }
2975 else
2976 {
2977 /* Take care of the lower and upper words individually. For
2978 each word, we try different methods in the order of
2979
2980 1) the special insn (in case of AND or XOR),
2981 2) the word-wise insn, and
2982 3) The byte-wise insn. */
27c1afd5 2983 if (w0 == 0xffff && (code != IOR))
366a7b27 2984 output_asm_insn ((code == AND)
187462ac 2985 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
366a7b27 2986 operands);
27c1afd5 2987 else if ((b0 != 0) && (b1 != 0))
366a7b27
KH
2988 {
2989 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
2990 output_asm_insn (insn_buf, operands);
2991 }
2992 else
2993 {
c5e7ce43 2994 if (b0 != 0)
366a7b27
KH
2995 {
2996 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
2997 output_asm_insn (insn_buf, operands);
2998 }
c5e7ce43 2999 if (b1 != 0)
366a7b27
KH
3000 {
3001 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3002 output_asm_insn (insn_buf, operands);
3003 }
3004 }
3005
27c1afd5 3006 if ((w1 == 0xffff) && (code != IOR))
366a7b27 3007 output_asm_insn ((code == AND)
187462ac 3008 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
366a7b27 3009 operands);
27c1afd5 3010 else if (code == IOR
472f2723
KH
3011 && w1 == 0xffff
3012 && (w0 & 0x8000) != 0)
3013 {
3014 output_asm_insn ("exts.l\t%S0", operands);
3015 }
27c1afd5 3016 else if (code == AND
c5e7ce43 3017 && w1 == 0xff00)
a6e8d113 3018 {
dc5f17ec 3019 output_asm_insn ("extu.w\t%e0", operands);
a6e8d113 3020 }
27c1afd5 3021 else
366a7b27 3022 {
c5e7ce43 3023 if (w1 != 0)
366a7b27
KH
3024 {
3025 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3026 output_asm_insn (insn_buf, operands);
3027 }
3028 }
366a7b27
KH
3029 }
3030 break;
3031 default:
8c440872 3032 gcc_unreachable ();
366a7b27
KH
3033 }
3034 return "";
3035}
40367e2d 3036
e9eba255
KH
3037/* Compute the length of a logical insn. */
3038
40367e2d 3039unsigned int
4ea5fe8b 3040compute_logical_op_length (machine_mode mode, rtx_code code, rtx *operands)
40367e2d
KH
3041{
3042 /* Pretend that every byte is affected if both operands are registers. */
7798db98 3043 const unsigned HOST_WIDE_INT intval =
40367e2d 3044 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
3045 /* Always use the full instruction if the
3046 first operand is in memory. It is better
3047 to use define_splits to generate the shorter
3048 sequence where valid. */
3049 && register_operand (operands[1], VOIDmode)
40367e2d
KH
3050 ? INTVAL (operands[2]) : 0x55555555);
3051 /* The determinant of the algorithm. If we perform an AND, 0
3052 affects a bit. Otherwise, 1 affects a bit. */
7798db98 3053 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
3054 /* Break up DET into pieces. */
3055 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3056 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3057 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3058 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3059 int lower_half_easy_p = 0;
3060 int upper_half_easy_p = 0;
40367e2d
KH
3061 /* Insn length. */
3062 unsigned int length = 0;
3063
3064 switch (mode)
3065 {
4ea5fe8b
JL
3066 case E_QImode:
3067 return 2;
3068
4e10a5a7 3069 case E_HImode:
40367e2d 3070 /* First, see if we can finish with one insn. */
27c1afd5 3071 if (b0 != 0 && b1 != 0)
40367e2d 3072 {
beed8fc0
AO
3073 length = h8300_length_from_table (operands[1], operands[2],
3074 &logicw_length_table);
40367e2d
KH
3075 }
3076 else
3077 {
3078 /* Take care of the lower byte. */
c5e7ce43 3079 if (b0 != 0)
40367e2d
KH
3080 length += 2;
3081
3082 /* Take care of the upper byte. */
c5e7ce43 3083 if (b1 != 0)
40367e2d
KH
3084 length += 2;
3085 }
3086 break;
4e10a5a7 3087 case E_SImode:
27c1afd5
JL
3088 /* Determine if the lower half can be taken care of in no more
3089 than two bytes. */
3090 lower_half_easy_p = (b0 == 0
3091 || b1 == 0
3092 || (code != IOR && w0 == 0xffff));
3093
3094 /* Determine if the upper half can be taken care of in no more
3095 than two bytes. */
3096 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3097 || (code == AND && w1 == 0xff00));
40367e2d 3098
f9ac2f95
KH
3099 /* Check if doing everything with one insn is no worse than
3100 using multiple insns. */
27c1afd5 3101 if (w0 != 0 && w1 != 0
472f2723
KH
3102 && !(lower_half_easy_p && upper_half_easy_p)
3103 && !(code == IOR && w1 == 0xffff
3104 && (w0 & 0x8000) != 0 && lower_half_easy_p))
40367e2d 3105 {
beed8fc0
AO
3106 length = h8300_length_from_table (operands[1], operands[2],
3107 &logicl_length_table);
40367e2d
KH
3108 }
3109 else
3110 {
3111 /* Take care of the lower and upper words individually. For
3112 each word, we try different methods in the order of
3113
3114 1) the special insn (in case of AND or XOR),
3115 2) the word-wise insn, and
3116 3) The byte-wise insn. */
27c1afd5 3117 if (w0 == 0xffff && (code != IOR))
40367e2d
KH
3118 {
3119 length += 2;
3120 }
27c1afd5 3121 else if ((b0 != 0) && (b1 != 0))
40367e2d
KH
3122 {
3123 length += 4;
3124 }
3125 else
3126 {
c5e7ce43 3127 if (b0 != 0)
40367e2d
KH
3128 length += 2;
3129
c5e7ce43 3130 if (b1 != 0)
40367e2d
KH
3131 length += 2;
3132 }
3133
27c1afd5 3134 if (w1 == 0xffff && (code != IOR))
40367e2d
KH
3135 {
3136 length += 2;
3137 }
27c1afd5 3138 else if (code == IOR
472f2723
KH
3139 && w1 == 0xffff
3140 && (w0 & 0x8000) != 0)
3141 {
3142 length += 2;
3143 }
27c1afd5 3144 else if (code == AND && w1 == 0xff00)
a6e8d113
KH
3145 {
3146 length += 2;
3147 }
27c1afd5 3148 else
40367e2d 3149 {
c5e7ce43 3150 if (w1 != 0)
40367e2d
KH
3151 length += 4;
3152 }
40367e2d
KH
3153 }
3154 break;
3155 default:
8c440872 3156 gcc_unreachable ();
40367e2d
KH
3157 }
3158 return length;
3159}
b42cff6b 3160
e9eba255
KH
3161/* Compute which flag bits are valid after a logical insn. */
3162
f16897cb 3163int
ef4bddc2 3164compute_logical_op_cc (machine_mode mode, rtx *operands)
b42cff6b
KH
3165{
3166 /* Figure out the logical op that we need to perform. */
3167 enum rtx_code code = GET_CODE (operands[3]);
3168 /* Pretend that every byte is affected if both operands are registers. */
7798db98 3169 const unsigned HOST_WIDE_INT intval =
b42cff6b 3170 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
3171 /* Always use the full instruction if the
3172 first operand is in memory. It is better
3173 to use define_splits to generate the shorter
3174 sequence where valid. */
3175 && register_operand (operands[1], VOIDmode)
b42cff6b
KH
3176 ? INTVAL (operands[2]) : 0x55555555);
3177 /* The determinant of the algorithm. If we perform an AND, 0
3178 affects a bit. Otherwise, 1 affects a bit. */
7798db98 3179 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
3180 /* Break up DET into pieces. */
3181 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3182 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3183 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3184 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3185 int lower_half_easy_p = 0;
3186 int upper_half_easy_p = 0;
b42cff6b 3187 /* Condition code. */
5ddb6eca 3188 enum attr_old_cc cc = OLD_CC_CLOBBER;
b42cff6b
KH
3189
3190 switch (mode)
3191 {
4e10a5a7 3192 case E_HImode:
b42cff6b 3193 /* First, see if we can finish with one insn. */
27c1afd5 3194 if (b0 != 0 && b1 != 0)
b42cff6b 3195 {
5ddb6eca 3196 cc = OLD_CC_SET_ZNV;
b42cff6b
KH
3197 }
3198 break;
4e10a5a7 3199 case E_SImode:
27c1afd5
JL
3200 /* Determine if the lower half can be taken care of in no more
3201 than two bytes. */
3202 lower_half_easy_p = (b0 == 0
3203 || b1 == 0
3204 || (code != IOR && w0 == 0xffff));
3205
3206 /* Determine if the upper half can be taken care of in no more
3207 than two bytes. */
3208 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3209 || (code == AND && w1 == 0xff00));
b42cff6b 3210
f9ac2f95
KH
3211 /* Check if doing everything with one insn is no worse than
3212 using multiple insns. */
27c1afd5 3213 if (w0 != 0 && w1 != 0
472f2723
KH
3214 && !(lower_half_easy_p && upper_half_easy_p)
3215 && !(code == IOR && w1 == 0xffff
3216 && (w0 & 0x8000) != 0 && lower_half_easy_p))
b42cff6b 3217 {
5ddb6eca 3218 cc = OLD_CC_SET_ZNV;
b42cff6b 3219 }
472f2723
KH
3220 else
3221 {
27c1afd5 3222 if (code == IOR
472f2723
KH
3223 && w1 == 0xffff
3224 && (w0 & 0x8000) != 0)
3225 {
5ddb6eca 3226 cc = OLD_CC_SET_ZNV;
472f2723
KH
3227 }
3228 }
b42cff6b
KH
3229 break;
3230 default:
8c440872 3231 gcc_unreachable ();
b42cff6b
KH
3232 }
3233 return cc;
3234}
366a7b27 3235\f
f16897cb 3236#if 0
8981ecd3
KH
3237/* Expand a conditional branch. */
3238
3239void
f90b7a5a 3240h8300_expand_branch (rtx operands[])
8981ecd3 3241{
f90b7a5a
PB
3242 enum rtx_code code = GET_CODE (operands[0]);
3243 rtx op0 = operands[1];
3244 rtx op1 = operands[2];
3245 rtx label = operands[3];
8981ecd3
KH
3246 rtx tmp;
3247
f90b7a5a 3248 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
f7df4a84 3249 emit_insn (gen_rtx_SET (cc0_rtx, tmp));
f90b7a5a 3250
8981ecd3
KH
3251 tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3252 tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3253 gen_rtx_LABEL_REF (VOIDmode, label),
3254 pc_rtx);
f7df4a84 3255 emit_jump_insn (gen_rtx_SET (pc_rtx, tmp));
8981ecd3 3256}
f90b7a5a
PB
3257
3258
3259/* Expand a conditional store. */
3260
3261void
3262h8300_expand_store (rtx operands[])
3263{
3264 rtx dest = operands[0];
3265 enum rtx_code code = GET_CODE (operands[1]);
3266 rtx op0 = operands[2];
3267 rtx op1 = operands[3];
3268 rtx tmp;
3269
3270 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
f7df4a84 3271 emit_insn (gen_rtx_SET (cc0_rtx, tmp));
f90b7a5a
PB
3272
3273 tmp = gen_rtx_fmt_ee (code, GET_MODE (dest), cc0_rtx, const0_rtx);
f7df4a84 3274 emit_insn (gen_rtx_SET (dest, tmp));
f90b7a5a 3275}
f16897cb 3276#endif
8981ecd3 3277\f
48837e29
DE
3278/* Shifts.
3279
005e3e05
KH
3280 We devote a fair bit of code to getting efficient shifts since we
3281 can only shift one bit at a time on the H8/300 and H8/300H and only
3db11b5c 3282 one or two bits at a time on the H8S.
005e3e05
KH
3283
3284 All shift code falls into one of the following ways of
3285 implementation:
3286
3287 o SHIFT_INLINE: Emit straight line code for the shift; this is used
3288 when a straight line shift is about the same size or smaller than
3289 a loop.
3290
3291 o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3292 off the bits we don't need. This is used when only a few of the
3293 bits in the original value will survive in the shifted value.
3294
3295 o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3296 simulate a shift by 8, 16, or 24 bits. Once moved, a few inline
3297 shifts can be added if the shift count is slightly more than 8 or
3298 16. This case also includes other oddballs that are not worth
f411c849 3299 explaining here.
005e3e05 3300
3db11b5c 3301 o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
005e3e05 3302
5ec0b66e
KH
3303 For each shift count, we try to use code that has no trade-off
3304 between code size and speed whenever possible.
3305
3306 If the trade-off is unavoidable, we try to be reasonable.
3307 Specifically, the fastest version is one instruction longer than
3308 the shortest version, we take the fastest version. We also provide
3309 the use a way to switch back to the shortest version with -Os.
3310
3311 For the details of the shift algorithms for various shift counts,
3312 refer to shift_alg_[qhs]i. */
07aae5c2 3313
beed8fc0
AO
3314/* Classify a shift with the given mode and code. OP is the shift amount. */
3315
3316enum h8sx_shift_type
ef4bddc2 3317h8sx_classify_shift (machine_mode mode, enum rtx_code code, rtx op)
beed8fc0
AO
3318{
3319 if (!TARGET_H8300SX)
3320 return H8SX_SHIFT_NONE;
3321
3322 switch (code)
3323 {
3324 case ASHIFT:
3325 case LSHIFTRT:
3326 /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd). */
3327 if (GET_CODE (op) != CONST_INT)
3328 return H8SX_SHIFT_BINARY;
3329
3330 /* Reject out-of-range shift amounts. */
3331 if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3332 return H8SX_SHIFT_NONE;
3333
3334 /* Power-of-2 shifts are effectively unary operations. */
3335 if (exact_log2 (INTVAL (op)) >= 0)
3336 return H8SX_SHIFT_UNARY;
3337
3338 return H8SX_SHIFT_BINARY;
3339
3340 case ASHIFTRT:
3341 if (op == const1_rtx || op == const2_rtx)
3342 return H8SX_SHIFT_UNARY;
3343 return H8SX_SHIFT_NONE;
3344
3345 case ROTATE:
3346 if (GET_CODE (op) == CONST_INT
3347 && (INTVAL (op) == 1
3348 || INTVAL (op) == 2
3349 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3350 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3351 return H8SX_SHIFT_UNARY;
3352 return H8SX_SHIFT_NONE;
3353
3354 default:
3355 return H8SX_SHIFT_NONE;
3356 }
3357}
3358
beed8fc0
AO
3359/* Return the asm template for a single h8sx shift instruction.
3360 OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3361 is the source and OPERANDS[3] is the shift. SUFFIX is the
88cb339e 3362 size suffix ('b', 'w' or 'l') and OPTYPE is the h8300_print_operand
beed8fc0
AO
3363 prefix for the destination operand. */
3364
3365const char *
3366output_h8sx_shift (rtx *operands, int suffix, int optype)
3367{
3368 static char buffer[16];
3369 const char *stem;
3370
3371 switch (GET_CODE (operands[3]))
3372 {
3373 case ASHIFT:
3374 stem = "shll";
3375 break;
3376
3377 case ASHIFTRT:
3378 stem = "shar";
3379 break;
3380
3381 case LSHIFTRT:
3382 stem = "shlr";
3383 break;
3384
3385 case ROTATE:
3386 stem = "rotl";
3387 if (INTVAL (operands[2]) > 2)
3388 {
3389 /* This is really a right rotate. */
3390 operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3391 - INTVAL (operands[2]));
3392 stem = "rotr";
3393 }
3394 break;
3395
3396 default:
8c440872 3397 gcc_unreachable ();
beed8fc0
AO
3398 }
3399 if (operands[2] == const1_rtx)
3400 sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3401 else
3402 sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3403 return buffer;
3404}
48837e29 3405
317d21e9 3406/* Emit code to do shifts. */
48837e29 3407
beed8fc0 3408bool
ef4bddc2 3409expand_a_shift (machine_mode mode, enum rtx_code code, rtx operands[])
07aae5c2 3410{
beed8fc0
AO
3411 switch (h8sx_classify_shift (mode, code, operands[2]))
3412 {
3413 case H8SX_SHIFT_BINARY:
3414 operands[1] = force_reg (mode, operands[1]);
3415 return false;
3416
3417 case H8SX_SHIFT_UNARY:
3418 return false;
3419
3420 case H8SX_SHIFT_NONE:
3421 break;
3422 }
3423
07e4d94e
KH
3424 /* Need a loop to get all the bits we want - we generate the
3425 code at emit time, but need to allocate a scratch reg now. */
70033749
JL
3426 emit_move_insn (copy_rtx (operands[0]), operands[1]);
3427 if (operands[2] == CONST0_RTX (QImode))
3428 ;
3429 else if (GET_CODE (operands[2]) == CONST_INT
3430 && !h8300_shift_needs_scratch_p (INTVAL (operands[2]), mode, code))
3431 emit_insn (gen_rtx_SET (copy_rtx (operands[0]),
3432 gen_rtx_fmt_ee (code, mode,
3433 copy_rtx (operands[1]), operands[2])));
3434 else
3435 emit_insn (gen_rtx_PARALLEL
3436 (VOIDmode,
3437 gen_rtvec (2,
3438 gen_rtx_SET (copy_rtx (operands[0]),
3439 gen_rtx_fmt_ee (code, mode,
3440 copy_rtx (operands[0]), operands[2])),
3441 gen_rtx_CLOBBER (VOIDmode,
3442 gen_rtx_SCRATCH (QImode)))));
beed8fc0 3443 return true;
48837e29
DE
3444}
3445
48837e29
DE
3446/* Symbols of the various modes which can be used as indices. */
3447
3448enum shift_mode
1a63219b
KH
3449{
3450 QIshift, HIshift, SIshift
3451};
48837e29 3452
269c14e1 3453/* For single bit shift insns, record assembler and what bits of the
5ddb6eca 3454 condition code are valid afterwards (represented as various OLD_CC_FOO
269c14e1 3455 bits, 0 means CC isn't left in a usable state). */
48837e29
DE
3456
3457struct shift_insn
3458{
8b60264b 3459 const char *const assembler;
5ddb6eca 3460 const enum attr_old_cc cc_valid;
48837e29
DE
3461};
3462
3463/* Assembler instruction shift table.
3464
3465 These tables are used to look up the basic shifts.
07e4d94e 3466 They are indexed by cpu, shift_type, and mode. */
07aae5c2 3467
48837e29
DE
3468static const struct shift_insn shift_one[2][3][3] =
3469{
3470/* H8/300 */
3471 {
3472/* SHIFT_ASHIFT */
3473 {
5ddb6eca
JL
3474 { "shll\t%X0", OLD_CC_SET_ZNV },
3475 { "add.w\t%T0,%T0", OLD_CC_SET_ZN },
3476 { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", OLD_CC_CLOBBER }
48837e29
DE
3477 },
3478/* SHIFT_LSHIFTRT */
3479 {
5ddb6eca
JL
3480 { "shlr\t%X0", OLD_CC_SET_ZNV },
3481 { "shlr\t%t0\n\trotxr\t%s0", OLD_CC_CLOBBER },
3482 { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", OLD_CC_CLOBBER }
48837e29
DE
3483 },
3484/* SHIFT_ASHIFTRT */
3485 {
5ddb6eca
JL
3486 { "shar\t%X0", OLD_CC_SET_ZNV },
3487 { "shar\t%t0\n\trotxr\t%s0", OLD_CC_CLOBBER },
3488 { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", OLD_CC_CLOBBER }
48837e29
DE
3489 }
3490 },
3491/* H8/300H */
3492 {
3493/* SHIFT_ASHIFT */
3494 {
5ddb6eca
JL
3495 { "shll.b\t%X0", OLD_CC_SET_ZNV },
3496 { "shll.w\t%T0", OLD_CC_SET_ZNV },
3497 { "shll.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3498 },
3499/* SHIFT_LSHIFTRT */
3500 {
5ddb6eca
JL
3501 { "shlr.b\t%X0", OLD_CC_SET_ZNV },
3502 { "shlr.w\t%T0", OLD_CC_SET_ZNV },
3503 { "shlr.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3504 },
3505/* SHIFT_ASHIFTRT */
3506 {
5ddb6eca
JL
3507 { "shar.b\t%X0", OLD_CC_SET_ZNV },
3508 { "shar.w\t%T0", OLD_CC_SET_ZNV },
3509 { "shar.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3510 }
3511 }
3512};
07aae5c2 3513
51c0c1d7
JL
3514static const struct shift_insn shift_two[3][3] =
3515{
3516/* SHIFT_ASHIFT */
3517 {
5ddb6eca
JL
3518 { "shll.b\t#2,%X0", OLD_CC_SET_ZNV },
3519 { "shll.w\t#2,%T0", OLD_CC_SET_ZNV },
3520 { "shll.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3521 },
3522/* SHIFT_LSHIFTRT */
3523 {
5ddb6eca
JL
3524 { "shlr.b\t#2,%X0", OLD_CC_SET_ZNV },
3525 { "shlr.w\t#2,%T0", OLD_CC_SET_ZNV },
3526 { "shlr.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3527 },
3528/* SHIFT_ASHIFTRT */
3529 {
5ddb6eca
JL
3530 { "shar.b\t#2,%X0", OLD_CC_SET_ZNV },
3531 { "shar.w\t#2,%T0", OLD_CC_SET_ZNV },
3532 { "shar.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3533 }
3534};
3535
48837e29
DE
3536/* Rotates are organized by which shift they'll be used in implementing.
3537 There's no need to record whether the cc is valid afterwards because
3538 it is the AND insn that will decide this. */
07aae5c2 3539
48837e29
DE
3540static const char *const rotate_one[2][3][3] =
3541{
3542/* H8/300 */
3543 {
3544/* SHIFT_ASHIFT */
3545 {
51c0c1d7
JL
3546 "rotr\t%X0",
3547 "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
48837e29
DE
3548 0
3549 },
3550/* SHIFT_LSHIFTRT */
3551 {
51c0c1d7
JL
3552 "rotl\t%X0",
3553 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
48837e29
DE
3554 0
3555 },
3556/* SHIFT_ASHIFTRT */
3557 {
51c0c1d7
JL
3558 "rotl\t%X0",
3559 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
48837e29 3560 0
07aae5c2 3561 }
48837e29
DE
3562 },
3563/* H8/300H */
3564 {
3565/* SHIFT_ASHIFT */
3566 {
51c0c1d7
JL
3567 "rotr.b\t%X0",
3568 "rotr.w\t%T0",
3569 "rotr.l\t%S0"
48837e29
DE
3570 },
3571/* SHIFT_LSHIFTRT */
07aae5c2 3572 {
51c0c1d7
JL
3573 "rotl.b\t%X0",
3574 "rotl.w\t%T0",
3575 "rotl.l\t%S0"
48837e29
DE
3576 },
3577/* SHIFT_ASHIFTRT */
3578 {
51c0c1d7
JL
3579 "rotl.b\t%X0",
3580 "rotl.w\t%T0",
3581 "rotl.l\t%S0"
48837e29
DE
3582 }
3583 }
3584};
3585
51c0c1d7
JL
3586static const char *const rotate_two[3][3] =
3587{
3588/* SHIFT_ASHIFT */
3589 {
3590 "rotr.b\t#2,%X0",
3591 "rotr.w\t#2,%T0",
3592 "rotr.l\t#2,%S0"
3593 },
3594/* SHIFT_LSHIFTRT */
3595 {
3596 "rotl.b\t#2,%X0",
3597 "rotl.w\t#2,%T0",
3598 "rotl.l\t#2,%S0"
3599 },
3600/* SHIFT_ASHIFTRT */
3601 {
3602 "rotl.b\t#2,%X0",
3603 "rotl.w\t#2,%T0",
3604 "rotl.l\t#2,%S0"
3605 }
3606};
3607
35fb3d1f
KH
3608struct shift_info {
3609 /* Shift algorithm. */
3610 enum shift_alg alg;
3611
3612 /* The number of bits to be shifted by shift1 and shift2. Valid
3613 when ALG is SHIFT_SPECIAL. */
3614 unsigned int remainder;
3615
3616 /* Special insn for a shift. Valid when ALG is SHIFT_SPECIAL. */
3617 const char *special;
3618
3619 /* Insn for a one-bit shift. Valid when ALG is either SHIFT_INLINE
9cd10576 3620 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
35fb3d1f
KH
3621 const char *shift1;
3622
3623 /* Insn for a two-bit shift. Valid when ALG is either SHIFT_INLINE
9cd10576 3624 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
35fb3d1f
KH
3625 const char *shift2;
3626
45ca2106 3627 /* CC status for SHIFT_INLINE. */
5ddb6eca 3628 enum attr_old_cc cc_inline;
45ca2106
KH
3629
3630 /* CC status for SHIFT_SPECIAL. */
5ddb6eca 3631 enum attr_old_cc cc_special;
35fb3d1f
KH
3632};
3633
cb713a8d
KH
3634static void get_shift_alg (enum shift_type,
3635 enum shift_mode, unsigned int,
3636 struct shift_info *);
441d04c6 3637
c009a745
KH
3638/* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
3639 best algorithm for doing the shift. The assembler code is stored
5ec0b66e 3640 in the pointers in INFO. We achieve the maximum efficiency in most
27c1afd5 3641 cases.
5ec0b66e
KH
3642
3643 We first determine the strategy of the shift algorithm by a table
3644 lookup. If that tells us to use a hand crafted assembly code, we
3645 go into the big switch statement to find what that is. Otherwise,
3646 we resort to a generic way, such as inlining. In either case, the
3647 result is returned through INFO. */
48837e29 3648
cb33eb17 3649static void
cb713a8d
KH
3650get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
3651 unsigned int count, struct shift_info *info)
48837e29 3652{
b9b575e6 3653 enum h8_cpu cpu;
769828ab 3654
27c1afd5 3655 if (TARGET_H8300S)
b9b575e6 3656 cpu = H8_S;
2f6bd6eb
YS
3657 else
3658 cpu = H8_300H;
769828ab 3659
96eaf358 3660 /* Find the shift algorithm. */
b9b575e6 3661 info->alg = SHIFT_LOOP;
48837e29
DE
3662 switch (shift_mode)
3663 {
3664 case QIshift:
b9b575e6 3665 if (count < GET_MODE_BITSIZE (QImode))
96eaf358
KH
3666 info->alg = shift_alg_qi[cpu][shift_type][count];
3667 break;
769828ab 3668
96eaf358 3669 case HIshift:
b9b575e6 3670 if (count < GET_MODE_BITSIZE (HImode))
96eaf358
KH
3671 info->alg = shift_alg_hi[cpu][shift_type][count];
3672 break;
3673
3674 case SIshift:
b9b575e6 3675 if (count < GET_MODE_BITSIZE (SImode))
96eaf358
KH
3676 info->alg = shift_alg_si[cpu][shift_type][count];
3677 break;
3678
3679 default:
8c440872 3680 gcc_unreachable ();
96eaf358
KH
3681 }
3682
3683 /* Fill in INFO. Return unless we have SHIFT_SPECIAL. */
3684 switch (info->alg)
3685 {
3686 case SHIFT_INLINE:
3687 info->remainder = count;
3688 /* Fall through. */
3689
3690 case SHIFT_LOOP:
3691 /* It is up to the caller to know that looping clobbers cc. */
3692 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3693 info->shift2 = shift_two[shift_type][shift_mode].assembler;
45ca2106 3694 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
96eaf358
KH
3695 goto end;
3696
3697 case SHIFT_ROT_AND:
3698 info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
3699 info->shift2 = rotate_two[shift_type][shift_mode];
5ddb6eca 3700 info->cc_inline = OLD_CC_CLOBBER;
96eaf358
KH
3701 goto end;
3702
3703 case SHIFT_SPECIAL:
3704 /* REMAINDER is 0 for most cases, so initialize it to 0. */
3705 info->remainder = 0;
3706 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3707 info->shift2 = shift_two[shift_type][shift_mode].assembler;
45ca2106 3708 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
5ddb6eca 3709 info->cc_special = OLD_CC_CLOBBER;
96eaf358
KH
3710 break;
3711 }
51c0c1d7 3712
96eaf358
KH
3713 /* Here we only deal with SHIFT_SPECIAL. */
3714 switch (shift_mode)
3715 {
3716 case QIshift:
769828ab
KH
3717 /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
3718 through the entire value. */
8c440872
NS
3719 gcc_assert (shift_type == SHIFT_ASHIFTRT && count == 7);
3720 info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
3721 goto end;
769828ab
KH
3722
3723 case HIshift:
769828ab 3724 if (count == 7)
51c0c1d7 3725 {
a77b1dbc 3726 switch (shift_type)
51c0c1d7 3727 {
a77b1dbc 3728 case SHIFT_ASHIFT:
27c1afd5 3729 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
692b7eb3 3730 goto end;
a77b1dbc 3731 case SHIFT_LSHIFTRT:
27c1afd5 3732 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
692b7eb3 3733 goto end;
a77b1dbc 3734 case SHIFT_ASHIFTRT:
35fb3d1f 3735 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
692b7eb3 3736 goto end;
48837e29 3737 }
07aae5c2 3738 }
01512446 3739 else if ((count >= 8 && count <= 13)
a38b3eea 3740 || (TARGET_H8300S && count == 14))
07aae5c2 3741 {
a7812c0b
KH
3742 info->remainder = count - 8;
3743
51c0c1d7 3744 switch (shift_type)
48837e29 3745 {
51c0c1d7 3746 case SHIFT_ASHIFT:
35fb3d1f 3747 info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
692b7eb3 3748 goto end;
51c0c1d7 3749 case SHIFT_LSHIFTRT:
27c1afd5 3750 info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
5ddb6eca 3751 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3752 goto end;
51c0c1d7 3753 case SHIFT_ASHIFTRT:
27c1afd5 3754 info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
5ddb6eca 3755 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3756 goto end;
51c0c1d7
JL
3757 }
3758 }
5e98fba2
DD
3759 else if (count == 14)
3760 {
3761 switch (shift_type)
3762 {
3763 case SHIFT_ASHIFT:
5e98fba2 3764 case SHIFT_LSHIFTRT:
27c1afd5 3765 goto end;
5e98fba2
DD
3766 goto end;
3767 case SHIFT_ASHIFTRT:
27c1afd5 3768 if (TARGET_H8300H)
45ca2106
KH
3769 {
3770 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";
5ddb6eca 3771 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3772 }
5e98fba2 3773 else /* TARGET_H8300S */
8c440872 3774 gcc_unreachable ();
5e98fba2
DD
3775 goto end;
3776 }
3777 }
1e41e866 3778 else if (count == 15)
51c0c1d7 3779 {
1e41e866
KH
3780 switch (shift_type)
3781 {
3782 case SHIFT_ASHIFT:
3783 info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
3784 goto end;
3785 case SHIFT_LSHIFTRT:
3786 info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
3787 goto end;
3788 case SHIFT_ASHIFTRT:
3789 info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
3790 goto end;
3791 }
07aae5c2 3792 }
8c440872 3793 gcc_unreachable ();
51c0c1d7 3794
48837e29 3795 case SIshift:
27c1afd5 3796 if (count == 8)
e6bcfef9
JS
3797 {
3798 switch (shift_type)
3799 {
3800 case SHIFT_ASHIFT:
35fb3d1f 3801 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";
692b7eb3 3802 goto end;
e6bcfef9 3803 case SHIFT_LSHIFTRT:
35fb3d1f 3804 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";
692b7eb3 3805 goto end;
e6bcfef9 3806 case SHIFT_ASHIFTRT:
35fb3d1f 3807 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";
692b7eb3 3808 goto end;
e6bcfef9
JS
3809 }
3810 }
27c1afd5 3811 else if (count == 15)
dd69e230
KH
3812 {
3813 switch (shift_type)
3814 {
3815 case SHIFT_ASHIFT:
3816 info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
5ddb6eca 3817 info->cc_special = OLD_CC_SET_ZNV;
dd69e230
KH
3818 goto end;
3819 case SHIFT_LSHIFTRT:
18cf8dda 3820 info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
5ddb6eca 3821 info->cc_special = OLD_CC_SET_ZNV;
dd69e230 3822 goto end;
aefc5826 3823 case SHIFT_ASHIFTRT:
8c440872 3824 gcc_unreachable ();
dd69e230
KH
3825 }
3826 }
27c1afd5 3827 else if ((TARGET_H8300H && count >= 16 && count <= 19)
01512446 3828 || (TARGET_H8300S && count >= 16 && count <= 21))
48837e29 3829 {
a7812c0b
KH
3830 info->remainder = count - 16;
3831
48837e29
DE
3832 switch (shift_type)
3833 {
3834 case SHIFT_ASHIFT:
35fb3d1f 3835 info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
692b7eb3 3836 goto end;
51c0c1d7 3837 case SHIFT_LSHIFTRT:
27c1afd5 3838 info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
5ddb6eca 3839 info->cc_special = OLD_CC_SET_ZNV;
f9477efd
KH
3840 goto end;
3841 case SHIFT_ASHIFTRT:
27c1afd5 3842 info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
5ddb6eca 3843 info->cc_special = OLD_CC_SET_ZNV;
7f473594
KH
3844 goto end;
3845 }
3846 }
4a4ae922 3847 else if ((TARGET_H8300H && count == 24)
01512446 3848 || (TARGET_H8300S && count >= 24 && count <= 25))
e6bcfef9 3849 {
4a4ae922
KH
3850 info->remainder = count - 24;
3851
e6bcfef9
JS
3852 switch (shift_type)
3853 {
3854 case SHIFT_ASHIFT:
35fb3d1f 3855 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";
692b7eb3 3856 goto end;
e6bcfef9 3857 case SHIFT_LSHIFTRT:
35fb3d1f 3858 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
5ddb6eca 3859 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3860 goto end;
e6bcfef9 3861 case SHIFT_ASHIFTRT:
35fb3d1f 3862 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
5ddb6eca 3863 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3864 goto end;
e6bcfef9
JS
3865 }
3866 }
27c1afd5 3867 else if (count == 28)
1e5bdc40
KH
3868 {
3869 switch (shift_type)
3870 {
3871 case SHIFT_ASHIFT:
3872 if (TARGET_H8300H)
3873 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";
3874 else
3875 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";
1e5bdc40
KH
3876 goto end;
3877 case SHIFT_LSHIFTRT:
3878 if (TARGET_H8300H)
45ca2106
KH
3879 {
3880 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";
5ddb6eca 3881 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3882 }
1e5bdc40 3883 else
a7612343 3884 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
1e5bdc40
KH
3885 goto end;
3886 case SHIFT_ASHIFTRT:
8c440872 3887 gcc_unreachable ();
1e5bdc40
KH
3888 }
3889 }
27c1afd5 3890 else if (count == 29)
1e5bdc40
KH
3891 {
3892 switch (shift_type)
3893 {
3894 case SHIFT_ASHIFT:
3895 if (TARGET_H8300H)
3896 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";
3897 else
3898 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
1e5bdc40
KH
3899 goto end;
3900 case SHIFT_LSHIFTRT:
3901 if (TARGET_H8300H)
45ca2106
KH
3902 {
3903 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";
5ddb6eca 3904 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3905 }
1e5bdc40 3906 else
45ca2106
KH
3907 {
3908 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
5ddb6eca 3909 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3910 }
1e5bdc40
KH
3911 goto end;
3912 case SHIFT_ASHIFTRT:
8c440872 3913 gcc_unreachable ();
1e5bdc40
KH
3914 }
3915 }
27c1afd5 3916 else if (count == 30)
1e5bdc40
KH
3917 {
3918 switch (shift_type)
3919 {
3920 case SHIFT_ASHIFT:
3921 if (TARGET_H8300H)
3922 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3923 else
3924 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
1e5bdc40
KH
3925 goto end;
3926 case SHIFT_LSHIFTRT:
3927 if (TARGET_H8300H)
a7612343 3928 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
1e5bdc40 3929 else
a7612343 3930 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
1e5bdc40
KH
3931 goto end;
3932 case SHIFT_ASHIFTRT:
8c440872 3933 gcc_unreachable ();
1e5bdc40
KH
3934 }
3935 }
48837e29
DE
3936 else if (count == 31)
3937 {
27c1afd5 3938 switch (shift_type)
48837e29 3939 {
27c1afd5
JL
3940 case SHIFT_ASHIFT:
3941 info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
5ddb6eca 3942 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5
JL
3943 goto end;
3944 case SHIFT_LSHIFTRT:
3945 info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
5ddb6eca 3946 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5
JL
3947 goto end;
3948 case SHIFT_ASHIFTRT:
3949 info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
5ddb6eca 3950 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5 3951 goto end;
48837e29
DE
3952 }
3953 }
8c440872 3954 gcc_unreachable ();
51c0c1d7 3955
48837e29 3956 default:
8c440872 3957 gcc_unreachable ();
07aae5c2 3958 }
48837e29 3959
cb33eb17
KH
3960 end:
3961 if (!TARGET_H8300S)
3962 info->shift2 = NULL;
07aae5c2
SC
3963}
3964
be1e06df
KH
3965/* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
3966 needed for some shift with COUNT and MODE. Return 0 otherwise. */
3967
3968int
70033749 3969h8300_shift_needs_scratch_p (int count, machine_mode mode, enum rtx_code type)
be1e06df 3970{
b9b575e6 3971 enum h8_cpu cpu;
be1e06df
KH
3972 int a, lr, ar;
3973
3974 if (GET_MODE_BITSIZE (mode) <= count)
3975 return 1;
3976
3977 /* Find out the target CPU. */
27c1afd5 3978 if (TARGET_H8300S)
b9b575e6 3979 cpu = H8_S;
2f6bd6eb
YS
3980 else
3981 cpu = H8_300H;
be1e06df
KH
3982
3983 /* Find the shift algorithm. */
3984 switch (mode)
3985 {
4e10a5a7 3986 case E_QImode:
be1e06df
KH
3987 a = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
3988 lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
3989 ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
3990 break;
3991
4e10a5a7 3992 case E_HImode:
be1e06df
KH
3993 a = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
3994 lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
3995 ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
3996 break;
3997
4e10a5a7 3998 case E_SImode:
be1e06df
KH
3999 a = shift_alg_si[cpu][SHIFT_ASHIFT][count];
4000 lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
4001 ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4002 break;
4003
4004 default:
8c440872 4005 gcc_unreachable ();
be1e06df
KH
4006 }
4007
aa2fb4dd 4008 /* On H8/300H, count == 8 uses a scratch register. */
70033749
JL
4009 if (type == CLOBBER)
4010 return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4011 || (TARGET_H8300H && mode == SImode && count == 8));
4012 else if (type == ASHIFT)
4013 return (a == SHIFT_LOOP
4014 || (TARGET_H8300H && mode == SImode && count == 8));
4015 else if (type == LSHIFTRT)
4016 return (lr == SHIFT_LOOP
4017 || (TARGET_H8300H && mode == SImode && count == 8));
4018 else if (type == ASHIFTRT)
4019 return (ar == SHIFT_LOOP
4020 || (TARGET_H8300H && mode == SImode && count == 8));
12bb62fb 4021 gcc_unreachable ();
be1e06df
KH
4022}
4023
e9eba255 4024/* Output the assembler code for doing shifts. */
48837e29 4025
441d04c6 4026const char *
cb713a8d 4027output_a_shift (rtx *operands)
07aae5c2 4028{
48837e29 4029 static int loopend_lab;
48837e29 4030 rtx shift = operands[3];
ef4bddc2 4031 machine_mode mode = GET_MODE (shift);
48837e29
DE
4032 enum rtx_code code = GET_CODE (shift);
4033 enum shift_type shift_type;
4034 enum shift_mode shift_mode;
35fb3d1f 4035 struct shift_info info;
8c440872 4036 int n;
48837e29
DE
4037
4038 loopend_lab++;
4039
4040 switch (mode)
4041 {
4e10a5a7 4042 case E_QImode:
48837e29
DE
4043 shift_mode = QIshift;
4044 break;
4e10a5a7 4045 case E_HImode:
48837e29
DE
4046 shift_mode = HIshift;
4047 break;
4e10a5a7 4048 case E_SImode:
48837e29
DE
4049 shift_mode = SIshift;
4050 break;
4051 default:
8c440872 4052 gcc_unreachable ();
48837e29 4053 }
07aae5c2 4054
48837e29 4055 switch (code)
07aae5c2 4056 {
48837e29
DE
4057 case ASHIFTRT:
4058 shift_type = SHIFT_ASHIFTRT;
4059 break;
4060 case LSHIFTRT:
4061 shift_type = SHIFT_LSHIFTRT;
4062 break;
4063 case ASHIFT:
4064 shift_type = SHIFT_ASHIFT;
4065 break;
4066 default:
8c440872 4067 gcc_unreachable ();
48837e29 4068 }
07aae5c2 4069
8c440872
NS
4070 /* This case must be taken care of by one of the two splitters
4071 that convert a variable shift into a loop. */
4072 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4073
4074 n = INTVAL (operands[2]);
4075
4076 /* If the count is negative, make it 0. */
4077 if (n < 0)
4078 n = 0;
4079 /* If the count is too big, truncate it.
4080 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4081 do the intuitive thing. */
4082 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4083 n = GET_MODE_BITSIZE (mode);
4084
4085 get_shift_alg (shift_type, shift_mode, n, &info);
4086
4087 switch (info.alg)
48837e29 4088 {
8c440872
NS
4089 case SHIFT_SPECIAL:
4090 output_asm_insn (info.special, operands);
4091 /* Fall through. */
48837e29 4092
8c440872
NS
4093 case SHIFT_INLINE:
4094 n = info.remainder;
48837e29 4095
8c440872
NS
4096 /* Emit two bit shifts first. */
4097 if (info.shift2 != NULL)
48837e29 4098 {
8c440872
NS
4099 for (; n > 1; n -= 2)
4100 output_asm_insn (info.shift2, operands);
4101 }
51c0c1d7 4102
8c440872
NS
4103 /* Now emit one bit shifts for any residual. */
4104 for (; n > 0; n--)
4105 output_asm_insn (info.shift1, operands);
4106 return "";
4107
4108 case SHIFT_ROT_AND:
4109 {
4110 int m = GET_MODE_BITSIZE (mode) - n;
4111 const int mask = (shift_type == SHIFT_ASHIFT
4112 ? ((1 << m) - 1) << n
4113 : (1 << m) - 1);
4114 char insn_buf[200];
4115
4116 /* Not all possibilities of rotate are supported. They shouldn't
4117 be generated, but let's watch for 'em. */
4118 gcc_assert (info.shift1);
4119
4120 /* Emit two bit rotates first. */
4121 if (info.shift2 != NULL)
48837e29 4122 {
8c440872
NS
4123 for (; m > 1; m -= 2)
4124 output_asm_insn (info.shift2, operands);
4125 }
4126
4127 /* Now single bit rotates for any residual. */
4128 for (; m > 0; m--)
4129 output_asm_insn (info.shift1, operands);
4130
4131 /* Now mask off the high bits. */
4132 switch (mode)
4133 {
4e10a5a7 4134 case E_QImode:
8c440872
NS
4135 sprintf (insn_buf, "and\t#%d,%%X0", mask);
4136 break;
51c0c1d7 4137
4e10a5a7 4138 case E_HImode:
8c440872
NS
4139 sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4140 break;
51c0c1d7 4141
8c440872
NS
4142 default:
4143 gcc_unreachable ();
48837e29 4144 }
b5eaf9ba 4145
8c440872
NS
4146 output_asm_insn (insn_buf, operands);
4147 return "";
4148 }
b5eaf9ba 4149
8c440872
NS
4150 case SHIFT_LOOP:
4151 /* A loop to shift by a "large" constant value.
4152 If we have shift-by-2 insns, use them. */
4153 if (info.shift2 != NULL)
4154 {
4155 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n / 2,
4156 names_big[REGNO (operands[4])]);
4157 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4158 output_asm_insn (info.shift2, operands);
4159 output_asm_insn ("add #0xff,%X4", operands);
4160 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4161 if (n % 2)
4162 output_asm_insn (info.shift1, operands);
4163 }
4164 else
4165 {
4166 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n,
4167 names_big[REGNO (operands[4])]);
4168 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4169 output_asm_insn (info.shift1, operands);
4170 output_asm_insn ("add #0xff,%X4", operands);
4171 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
51c0c1d7 4172 }
8c440872
NS
4173 return "";
4174
4175 default:
4176 gcc_unreachable ();
07aae5c2 4177 }
07aae5c2 4178}
86855e8c 4179
0a2aaacc 4180/* Count the number of assembly instructions in a string TEMPL. */
e9eba255 4181
86855e8c 4182static unsigned int
0a2aaacc 4183h8300_asm_insn_count (const char *templ)
86855e8c
KH
4184{
4185 unsigned int count = 1;
4186
0a2aaacc
KG
4187 for (; *templ; templ++)
4188 if (*templ == '\n')
86855e8c
KH
4189 count++;
4190
4191 return count;
4192}
4193
e9eba255
KH
4194/* Compute the length of a shift insn. */
4195
86855e8c 4196unsigned int
4ea5fe8b 4197compute_a_shift_length (rtx *operands)
86855e8c
KH
4198{
4199 rtx shift = operands[3];
ef4bddc2 4200 machine_mode mode = GET_MODE (shift);
86855e8c
KH
4201 enum rtx_code code = GET_CODE (shift);
4202 enum shift_type shift_type;
4203 enum shift_mode shift_mode;
4204 struct shift_info info;
4205 unsigned int wlength = 0;
4206
4207 switch (mode)
4208 {
4e10a5a7 4209 case E_QImode:
86855e8c
KH
4210 shift_mode = QIshift;
4211 break;
4e10a5a7 4212 case E_HImode:
86855e8c
KH
4213 shift_mode = HIshift;
4214 break;
4e10a5a7 4215 case E_SImode:
86855e8c
KH
4216 shift_mode = SIshift;
4217 break;
4218 default:
8c440872 4219 gcc_unreachable ();
86855e8c
KH
4220 }
4221
4222 switch (code)
4223 {
4224 case ASHIFTRT:
4225 shift_type = SHIFT_ASHIFTRT;
4226 break;
4227 case LSHIFTRT:
4228 shift_type = SHIFT_LSHIFTRT;
4229 break;
4230 case ASHIFT:
4231 shift_type = SHIFT_ASHIFT;
4232 break;
4233 default:
8c440872 4234 gcc_unreachable ();
86855e8c
KH
4235 }
4236
4237 if (GET_CODE (operands[2]) != CONST_INT)
4238 {
4239 /* Get the assembler code to do one shift. */
4240 get_shift_alg (shift_type, shift_mode, 1, &info);
4241
4242 return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4243 }
4244 else
4245 {
4246 int n = INTVAL (operands[2]);
4247
4248 /* If the count is negative, make it 0. */
4249 if (n < 0)
4250 n = 0;
4251 /* If the count is too big, truncate it.
4252 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4253 do the intuitive thing. */
4254 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4255 n = GET_MODE_BITSIZE (mode);
4256
4257 get_shift_alg (shift_type, shift_mode, n, &info);
4258
4259 switch (info.alg)
4260 {
4261 case SHIFT_SPECIAL:
4262 wlength += h8300_asm_insn_count (info.special);
41c3eb5d
KH
4263
4264 /* Every assembly instruction used in SHIFT_SPECIAL case
4265 takes 2 bytes except xor.l, which takes 4 bytes, so if we
4266 see xor.l, we just pretend that xor.l counts as two insns
4267 so that the insn length will be computed correctly. */
4268 if (strstr (info.special, "xor.l") != NULL)
4269 wlength++;
4270
86855e8c
KH
4271 /* Fall through. */
4272
4273 case SHIFT_INLINE:
4274 n = info.remainder;
4275
4276 if (info.shift2 != NULL)
4277 {
4278 wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4279 n = n % 2;
4280 }
4281
4282 wlength += h8300_asm_insn_count (info.shift1) * n;
6b148bd9 4283
86855e8c
KH
4284 return 2 * wlength;
4285
4286 case SHIFT_ROT_AND:
4287 {
4288 int m = GET_MODE_BITSIZE (mode) - n;
4289
4290 /* Not all possibilities of rotate are supported. They shouldn't
4291 be generated, but let's watch for 'em. */
8c440872 4292 gcc_assert (info.shift1);
86855e8c
KH
4293
4294 if (info.shift2 != NULL)
4295 {
4296 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4297 m = m % 2;
4298 }
4299
4300 wlength += h8300_asm_insn_count (info.shift1) * m;
6b148bd9 4301
86855e8c
KH
4302 /* Now mask off the high bits. */
4303 switch (mode)
4304 {
4e10a5a7 4305 case E_QImode:
86855e8c
KH
4306 wlength += 1;
4307 break;
4e10a5a7 4308 case E_HImode:
86855e8c
KH
4309 wlength += 2;
4310 break;
4e10a5a7 4311 case E_SImode:
86855e8c
KH
4312 wlength += 3;
4313 break;
4314 default:
8c440872 4315 gcc_unreachable ();
86855e8c
KH
4316 }
4317 return 2 * wlength;
4318 }
4319
4320 case SHIFT_LOOP:
4321 /* A loop to shift by a "large" constant value.
4322 If we have shift-by-2 insns, use them. */
4323 if (info.shift2 != NULL)
4324 {
4325 wlength += 3 + h8300_asm_insn_count (info.shift2);
4326 if (n % 2)
4327 wlength += h8300_asm_insn_count (info.shift1);
4328 }
4329 else
4330 {
4331 wlength += 3 + h8300_asm_insn_count (info.shift1);
4332 }
4333 return 2 * wlength;
4334
4335 default:
8c440872 4336 gcc_unreachable ();
86855e8c
KH
4337 }
4338 }
4339}
45ca2106 4340
e9eba255
KH
4341/* Compute which flag bits are valid after a shift insn. */
4342
f16897cb 4343int
cb713a8d 4344compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
45ca2106
KH
4345{
4346 rtx shift = operands[3];
ef4bddc2 4347 machine_mode mode = GET_MODE (shift);
45ca2106
KH
4348 enum rtx_code code = GET_CODE (shift);
4349 enum shift_type shift_type;
4350 enum shift_mode shift_mode;
4351 struct shift_info info;
8c440872
NS
4352 int n;
4353
45ca2106
KH
4354 switch (mode)
4355 {
4e10a5a7 4356 case E_QImode:
45ca2106
KH
4357 shift_mode = QIshift;
4358 break;
4e10a5a7 4359 case E_HImode:
45ca2106
KH
4360 shift_mode = HIshift;
4361 break;
4e10a5a7 4362 case E_SImode:
45ca2106
KH
4363 shift_mode = SIshift;
4364 break;
4365 default:
8c440872 4366 gcc_unreachable ();
45ca2106
KH
4367 }
4368
4369 switch (code)
4370 {
4371 case ASHIFTRT:
4372 shift_type = SHIFT_ASHIFTRT;
4373 break;
4374 case LSHIFTRT:
4375 shift_type = SHIFT_LSHIFTRT;
4376 break;
4377 case ASHIFT:
4378 shift_type = SHIFT_ASHIFT;
4379 break;
4380 default:
8c440872 4381 gcc_unreachable ();
45ca2106
KH
4382 }
4383
8c440872
NS
4384 /* This case must be taken care of by one of the two splitters
4385 that convert a variable shift into a loop. */
4386 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4387
4388 n = INTVAL (operands[2]);
4389
4390 /* If the count is negative, make it 0. */
4391 if (n < 0)
4392 n = 0;
4393 /* If the count is too big, truncate it.
4394 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4395 do the intuitive thing. */
4396 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4397 n = GET_MODE_BITSIZE (mode);
4398
4399 get_shift_alg (shift_type, shift_mode, n, &info);
4400
4401 switch (info.alg)
45ca2106 4402 {
8c440872
NS
4403 case SHIFT_SPECIAL:
4404 if (info.remainder == 0)
4405 return info.cc_special;
45ca2106 4406
8c440872 4407 /* Fall through. */
45ca2106 4408
8c440872
NS
4409 case SHIFT_INLINE:
4410 return info.cc_inline;
4411
4412 case SHIFT_ROT_AND:
4413 /* This case always ends with an and instruction. */
5ddb6eca 4414 return OLD_CC_SET_ZNV;
8c440872
NS
4415
4416 case SHIFT_LOOP:
4417 /* A loop to shift by a "large" constant value.
4418 If we have shift-by-2 insns, use them. */
4419 if (info.shift2 != NULL)
45ca2106 4420 {
8c440872
NS
4421 if (n % 2)
4422 return info.cc_inline;
45ca2106 4423 }
5ddb6eca 4424 return OLD_CC_CLOBBER;
8c440872
NS
4425
4426 default:
4427 gcc_unreachable ();
45ca2106
KH
4428 }
4429}
48837e29 4430\f
edd71f0f
KH
4431/* A rotation by a non-constant will cause a loop to be generated, in
4432 which a rotation by one bit is used. A rotation by a constant,
4433 including the one in the loop, will be taken care of by
caf7f21a 4434 output_a_rotate () at the insn emit time. */
edd71f0f
KH
4435
4436int
3d2e90d6 4437expand_a_rotate (rtx operands[])
edd71f0f
KH
4438{
4439 rtx dst = operands[0];
4440 rtx src = operands[1];
4441 rtx rotate_amount = operands[2];
ef4bddc2 4442 machine_mode mode = GET_MODE (dst);
edd71f0f 4443
beed8fc0
AO
4444 if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4445 return false;
4446
edd71f0f
KH
4447 /* We rotate in place. */
4448 emit_move_insn (dst, src);
4449
4450 if (GET_CODE (rotate_amount) != CONST_INT)
4451 {
4452 rtx counter = gen_reg_rtx (QImode);
19f8b229
TS
4453 rtx_code_label *start_label = gen_label_rtx ();
4454 rtx_code_label *end_label = gen_label_rtx ();
edd71f0f
KH
4455
4456 /* If the rotate amount is less than or equal to 0,
4457 we go out of the loop. */
a556fd39 4458 emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
d43e0b7d 4459 QImode, 0, end_label);
edd71f0f
KH
4460
4461 /* Initialize the loop counter. */
4462 emit_move_insn (counter, rotate_amount);
4463
4464 emit_label (start_label);
4465
4466 /* Rotate by one bit. */
01ab5574
KH
4467 switch (mode)
4468 {
4e10a5a7 4469 case E_QImode:
01ab5574
KH
4470 emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
4471 break;
4e10a5a7 4472 case E_HImode:
01ab5574
KH
4473 emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
4474 break;
4e10a5a7 4475 case E_SImode:
01ab5574
KH
4476 emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
4477 break;
4478 default:
8c440872 4479 gcc_unreachable ();
01ab5574 4480 }
edd71f0f
KH
4481
4482 /* Decrement the counter by 1. */
01ab5574 4483 emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
edd71f0f 4484
9cd10576 4485 /* If the loop counter is nonzero, we go back to the beginning
edd71f0f 4486 of the loop. */
a556fd39 4487 emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
d43e0b7d 4488 start_label);
edd71f0f
KH
4489
4490 emit_label (end_label);
4491 }
4492 else
4493 {
4494 /* Rotate by AMOUNT bits. */
01ab5574
KH
4495 switch (mode)
4496 {
4e10a5a7 4497 case E_QImode:
01ab5574
KH
4498 emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
4499 break;
4e10a5a7 4500 case E_HImode:
01ab5574
KH
4501 emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
4502 break;
4e10a5a7 4503 case E_SImode:
01ab5574
KH
4504 emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
4505 break;
4506 default:
8c440872 4507 gcc_unreachable ();
01ab5574 4508 }
edd71f0f
KH
4509 }
4510
4511 return 1;
4512}
4513
e9eba255 4514/* Output a rotate insn. */
edd71f0f
KH
4515
4516const char *
caf7f21a 4517output_a_rotate (enum rtx_code code, rtx *operands)
edd71f0f
KH
4518{
4519 rtx dst = operands[0];
4520 rtx rotate_amount = operands[2];
4521 enum shift_mode rotate_mode;
4522 enum shift_type rotate_type;
4523 const char *insn_buf;
4524 int bits;
4525 int amount;
ef4bddc2 4526 machine_mode mode = GET_MODE (dst);
edd71f0f 4527
8c440872 4528 gcc_assert (GET_CODE (rotate_amount) == CONST_INT);
edd71f0f
KH
4529
4530 switch (mode)
4531 {
4e10a5a7 4532 case E_QImode:
edd71f0f
KH
4533 rotate_mode = QIshift;
4534 break;
4e10a5a7 4535 case E_HImode:
edd71f0f
KH
4536 rotate_mode = HIshift;
4537 break;
4e10a5a7 4538 case E_SImode:
edd71f0f
KH
4539 rotate_mode = SIshift;
4540 break;
4541 default:
8c440872 4542 gcc_unreachable ();
edd71f0f
KH
4543 }
4544
4545 switch (code)
4546 {
4547 case ROTATERT:
4548 rotate_type = SHIFT_ASHIFT;
4549 break;
4550 case ROTATE:
4551 rotate_type = SHIFT_LSHIFTRT;
4552 break;
4553 default:
8c440872 4554 gcc_unreachable ();
edd71f0f
KH
4555 }
4556
4557 amount = INTVAL (rotate_amount);
4558
4559 /* Clean up AMOUNT. */
4560 if (amount < 0)
4561 amount = 0;
4562 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4563 amount = GET_MODE_BITSIZE (mode);
4564
4565 /* Determine the faster direction. After this phase, amount will be
4566 at most a half of GET_MODE_BITSIZE (mode). */
e0c32c62 4567 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
edd71f0f
KH
4568 {
4569 /* Flip the direction. */
4570 amount = GET_MODE_BITSIZE (mode) - amount;
4571 rotate_type =
4572 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4573 }
4574
4575 /* See if a byte swap (in HImode) or a word swap (in SImode) can
4576 boost up the rotation. */
27c1afd5 4577 if ((mode == HImode && TARGET_H8300H && amount >= 6)
edd71f0f
KH
4578 || (mode == HImode && TARGET_H8300S && amount == 8)
4579 || (mode == SImode && TARGET_H8300H && amount >= 10)
4580 || (mode == SImode && TARGET_H8300S && amount >= 13))
4581 {
4582 switch (mode)
4583 {
4e10a5a7 4584 case E_HImode:
edd71f0f
KH
4585 /* This code works on any family. */
4586 insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
4587 output_asm_insn (insn_buf, operands);
4588 break;
4589
4e10a5a7 4590 case E_SImode:
3db11b5c 4591 /* This code works on the H8/300H and H8S. */
edd71f0f
KH
4592 insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
4593 output_asm_insn (insn_buf, operands);
4594 break;
4595
4596 default:
8c440872 4597 gcc_unreachable ();
edd71f0f
KH
4598 }
4599
4600 /* Adjust AMOUNT and flip the direction. */
4601 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4602 rotate_type =
4603 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4604 }
4605
01ab5574 4606 /* Output rotate insns. */
edd71f0f
KH
4607 for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
4608 {
4609 if (bits == 2)
4610 insn_buf = rotate_two[rotate_type][rotate_mode];
4611 else
4612 insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
2c54abce 4613
edd71f0f
KH
4614 for (; amount >= bits; amount -= bits)
4615 output_asm_insn (insn_buf, operands);
4616 }
4617
4618 return "";
4619}
caf7f21a 4620
e9eba255
KH
4621/* Compute the length of a rotate insn. */
4622
caf7f21a
KH
4623unsigned int
4624compute_a_rotate_length (rtx *operands)
4625{
4626 rtx src = operands[1];
343fd2c7 4627 rtx amount_rtx = operands[2];
ef4bddc2 4628 machine_mode mode = GET_MODE (src);
caf7f21a
KH
4629 int amount;
4630 unsigned int length = 0;
4631
8c440872 4632 gcc_assert (GET_CODE (amount_rtx) == CONST_INT);
caf7f21a 4633
343fd2c7 4634 amount = INTVAL (amount_rtx);
caf7f21a
KH
4635
4636 /* Clean up AMOUNT. */
4637 if (amount < 0)
4638 amount = 0;
4639 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4640 amount = GET_MODE_BITSIZE (mode);
4641
4642 /* Determine the faster direction. After this phase, amount
4643 will be at most a half of GET_MODE_BITSIZE (mode). */
4644 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
4645 /* Flip the direction. */
4646 amount = GET_MODE_BITSIZE (mode) - amount;
4647
4648 /* See if a byte swap (in HImode) or a word swap (in SImode) can
4649 boost up the rotation. */
27c1afd5 4650 if ((mode == HImode && TARGET_H8300H && amount >= 6)
caf7f21a
KH
4651 || (mode == HImode && TARGET_H8300S && amount == 8)
4652 || (mode == SImode && TARGET_H8300H && amount >= 10)
4653 || (mode == SImode && TARGET_H8300S && amount >= 13))
4654 {
4655 /* Adjust AMOUNT and flip the direction. */
4656 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4657 length += 6;
4658 }
4659
4660 /* We use 2-bit rotations on the H8S. */
4661 if (TARGET_H8300S)
4662 amount = amount / 2 + amount % 2;
4663
4664 /* The H8/300 uses three insns to rotate one bit, taking 6
4665 length. */
27c1afd5 4666 length += amount * 2;
caf7f21a
KH
4667
4668 return length;
4669}
edd71f0f 4670\f
48837e29 4671/* Fix the operands of a gen_xxx so that it could become a bit
2c54abce 4672 operating insn. */
07aae5c2
SC
4673
4674int
4093985c 4675fix_bit_operand (rtx *operands, enum rtx_code code)
07aae5c2 4676{
abc95ed3 4677 /* The bit_operand predicate accepts any memory during RTL generation, but
48837e29
DE
4678 only 'U' memory afterwards, so if this is a MEM operand, we must force
4679 it to be valid for 'U' by reloading the address. */
07aae5c2 4680
4093985c
KH
4681 if (code == AND
4682 ? single_zero_operand (operands[2], QImode)
4683 : single_one_operand (operands[2], QImode))
07aae5c2 4684 {
2e760b15
KH
4685 /* OK to have a memory dest. */
4686 if (GET_CODE (operands[0]) == MEM
ceaaaeab 4687 && !satisfies_constraint_U (operands[0]))
48837e29 4688 {
2e760b15
KH
4689 rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
4690 copy_to_mode_reg (Pmode,
4691 XEXP (operands[0], 0)));
4692 MEM_COPY_ATTRIBUTES (mem, operands[0]);
4693 operands[0] = mem;
4694 }
48837e29 4695
2e760b15 4696 if (GET_CODE (operands[1]) == MEM
ceaaaeab 4697 && !satisfies_constraint_U (operands[1]))
2e760b15
KH
4698 {
4699 rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
4700 copy_to_mode_reg (Pmode,
4701 XEXP (operands[1], 0)));
4702 MEM_COPY_ATTRIBUTES (mem, operands[0]);
4703 operands[1] = mem;
48837e29 4704 }
2e760b15 4705 return 0;
48837e29 4706 }
07aae5c2 4707
48837e29 4708 /* Dest and src op must be register. */
07aae5c2 4709
48837e29
DE
4710 operands[1] = force_reg (QImode, operands[1]);
4711 {
4712 rtx res = gen_reg_rtx (QImode);
fd57a6e4 4713 switch (code)
a3579575
KH
4714 {
4715 case AND:
4716 emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
4717 break;
4718 case IOR:
4719 emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
4720 break;
4721 case XOR:
4722 emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
4723 break;
4724 default:
8c440872 4725 gcc_unreachable ();
a3579575
KH
4726 }
4727 emit_insn (gen_movqi (operands[0], res));
48837e29
DE
4728 }
4729 return 1;
07aae5c2 4730}
f5b65a56 4731
f5b65a56
JL
4732/* Return nonzero if FUNC is an interrupt function as specified
4733 by the "interrupt" attribute. */
4734
4735static int
cb713a8d 4736h8300_interrupt_function_p (tree func)
f5b65a56
JL
4737{
4738 tree a;
4739
4740 if (TREE_CODE (func) != FUNCTION_DECL)
4741 return 0;
4742
91d231cb 4743 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
f5b65a56
JL
4744 return a != NULL_TREE;
4745}
4746
3cfa3702
KH
4747/* Return nonzero if FUNC is a saveall function as specified by the
4748 "saveall" attribute. */
4749
4750static int
4751h8300_saveall_function_p (tree func)
4752{
4753 tree a;
4754
4755 if (TREE_CODE (func) != FUNCTION_DECL)
4756 return 0;
4757
4758 a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
4759 return a != NULL_TREE;
4760}
4761
fabe72bb
JL
4762/* Return nonzero if FUNC is an OS_Task function as specified
4763 by the "OS_Task" attribute. */
4764
4765static int
cb713a8d 4766h8300_os_task_function_p (tree func)
fabe72bb
JL
4767{
4768 tree a;
4769
4770 if (TREE_CODE (func) != FUNCTION_DECL)
4771 return 0;
4772
91d231cb 4773 a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
fabe72bb
JL
4774 return a != NULL_TREE;
4775}
4776
4777/* Return nonzero if FUNC is a monitor function as specified
4778 by the "monitor" attribute. */
4779
4780static int
cb713a8d 4781h8300_monitor_function_p (tree func)
fabe72bb
JL
4782{
4783 tree a;
4784
4785 if (TREE_CODE (func) != FUNCTION_DECL)
4786 return 0;
4787
91d231cb 4788 a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
fabe72bb
JL
4789 return a != NULL_TREE;
4790}
4791
f5b65a56
JL
4792/* Return nonzero if FUNC is a function that should be called
4793 through the function vector. */
4794
4795int
cb713a8d 4796h8300_funcvec_function_p (tree func)
f5b65a56
JL
4797{
4798 tree a;
4799
4800 if (TREE_CODE (func) != FUNCTION_DECL)
4801 return 0;
4802
91d231cb 4803 a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
f5b65a56
JL
4804 return a != NULL_TREE;
4805}
4806
887a8bd9 4807/* Return nonzero if DECL is a variable that's in the eight bit
15dc331e
JL
4808 data area. */
4809
4810int
cb713a8d 4811h8300_eightbit_data_p (tree decl)
15dc331e
JL
4812{
4813 tree a;
4814
4815 if (TREE_CODE (decl) != VAR_DECL)
4816 return 0;
4817
91d231cb 4818 a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
15dc331e
JL
4819 return a != NULL_TREE;
4820}
4821
887a8bd9
JL
4822/* Return nonzero if DECL is a variable that's in the tiny
4823 data area. */
4824
4825int
cb713a8d 4826h8300_tiny_data_p (tree decl)
887a8bd9
JL
4827{
4828 tree a;
4829
4830 if (TREE_CODE (decl) != VAR_DECL)
4831 return 0;
4832
91d231cb 4833 a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
887a8bd9
JL
4834 return a != NULL_TREE;
4835}
4836
3cfa3702
KH
4837/* Generate an 'interrupt_handler' attribute for decls. We convert
4838 all the pragmas to corresponding attributes. */
2c1d2fcb
DD
4839
4840static void
cb713a8d 4841h8300_insert_attributes (tree node, tree *attributes)
2c1d2fcb 4842{
3cfa3702
KH
4843 if (TREE_CODE (node) == FUNCTION_DECL)
4844 {
4845 if (pragma_interrupt)
4846 {
4847 pragma_interrupt = 0;
2c1d2fcb 4848
3cfa3702
KH
4849 /* Add an 'interrupt_handler' attribute. */
4850 *attributes = tree_cons (get_identifier ("interrupt_handler"),
4851 NULL, *attributes);
4852 }
e392d367 4853
3cfa3702
KH
4854 if (pragma_saveall)
4855 {
4856 pragma_saveall = 0;
4857
4858 /* Add an 'saveall' attribute. */
4859 *attributes = tree_cons (get_identifier ("saveall"),
4860 NULL, *attributes);
4861 }
4862 }
2c1d2fcb
DD
4863}
4864
91d231cb 4865/* Supported attributes:
f5b65a56 4866
97c5ec1d 4867 interrupt_handler: output a prologue and epilogue suitable for an
f5b65a56
JL
4868 interrupt handler.
4869
3cfa3702
KH
4870 saveall: output a prologue and epilogue that saves and restores
4871 all registers except the stack pointer.
4872
97c5ec1d 4873 function_vector: This function should be called through the
887a8bd9
JL
4874 function vector.
4875
4876 eightbit_data: This variable lives in the 8-bit data area and can
4877 be referenced with 8-bit absolute memory addresses.
4878
4879 tiny_data: This variable lives in the tiny data area and can be
4880 referenced with 16-bit absolute memory references. */
f5b65a56 4881
6bc7bc14 4882static const struct attribute_spec h8300_attribute_table[] =
f5b65a56 4883{
4849deb1
JJ
4884 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4885 affects_type_identity, handler, exclude } */
4886 { "interrupt_handler", 0, 0, true, false, false, false,
4887 h8300_handle_fndecl_attribute, NULL },
4888 { "saveall", 0, 0, true, false, false, false,
4889 h8300_handle_fndecl_attribute, NULL },
4890 { "OS_Task", 0, 0, true, false, false, false,
4891 h8300_handle_fndecl_attribute, NULL },
4892 { "monitor", 0, 0, true, false, false, false,
4893 h8300_handle_fndecl_attribute, NULL },
4894 { "function_vector", 0, 0, true, false, false, false,
4895 h8300_handle_fndecl_attribute, NULL },
4896 { "eightbit_data", 0, 0, true, false, false, false,
4897 h8300_handle_eightbit_data_attribute, NULL },
4898 { "tiny_data", 0, 0, true, false, false, false,
4899 h8300_handle_tiny_data_attribute, NULL },
4900 { NULL, 0, 0, false, false, false, false, NULL, NULL }
91d231cb 4901};
f5b65a56 4902
15dc331e 4903
91d231cb
JM
4904/* Handle an attribute requiring a FUNCTION_DECL; arguments as in
4905 struct attribute_spec.handler. */
4906static tree
cb713a8d
KH
4907h8300_handle_fndecl_attribute (tree *node, tree name,
4908 tree args ATTRIBUTE_UNUSED,
4909 int flags ATTRIBUTE_UNUSED,
4910 bool *no_add_attrs)
91d231cb
JM
4911{
4912 if (TREE_CODE (*node) != FUNCTION_DECL)
4913 {
29d08eba
JM
4914 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4915 name);
91d231cb
JM
4916 *no_add_attrs = true;
4917 }
4918
4919 return NULL_TREE;
4920}
4921
4922/* Handle an "eightbit_data" attribute; arguments as in
4923 struct attribute_spec.handler. */
4924static tree
cb713a8d
KH
4925h8300_handle_eightbit_data_attribute (tree *node, tree name,
4926 tree args ATTRIBUTE_UNUSED,
4927 int flags ATTRIBUTE_UNUSED,
4928 bool *no_add_attrs)
91d231cb
JM
4929{
4930 tree decl = *node;
4931
4932 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
15dc331e 4933 {
f961457f 4934 set_decl_section_name (decl, ".eight");
91d231cb
JM
4935 }
4936 else
4937 {
29d08eba
JM
4938 warning (OPT_Wattributes, "%qE attribute ignored",
4939 name);
91d231cb 4940 *no_add_attrs = true;
887a8bd9
JL
4941 }
4942
91d231cb
JM
4943 return NULL_TREE;
4944}
4945
4946/* Handle an "tiny_data" attribute; arguments as in
4947 struct attribute_spec.handler. */
4948static tree
cb713a8d
KH
4949h8300_handle_tiny_data_attribute (tree *node, tree name,
4950 tree args ATTRIBUTE_UNUSED,
4951 int flags ATTRIBUTE_UNUSED,
4952 bool *no_add_attrs)
91d231cb
JM
4953{
4954 tree decl = *node;
4955
4956 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
887a8bd9 4957 {
f961457f 4958 set_decl_section_name (decl, ".tiny");
91d231cb
JM
4959 }
4960 else
4961 {
29d08eba
JM
4962 warning (OPT_Wattributes, "%qE attribute ignored",
4963 name);
91d231cb 4964 *no_add_attrs = true;
15dc331e 4965 }
07e4d94e 4966
91d231cb 4967 return NULL_TREE;
f5b65a56
JL
4968}
4969
dc66a1c4 4970/* Mark function vectors, and various small data objects. */
fb49053f
RH
4971
4972static void
cb713a8d 4973h8300_encode_section_info (tree decl, rtx rtl, int first)
fb49053f 4974{
dc66a1c4
RH
4975 int extra_flags = 0;
4976
c6a2438a 4977 default_encode_section_info (decl, rtl, first);
dc66a1c4 4978
fb49053f
RH
4979 if (TREE_CODE (decl) == FUNCTION_DECL
4980 && h8300_funcvec_function_p (decl))
dc66a1c4 4981 extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
fb49053f
RH
4982 else if (TREE_CODE (decl) == VAR_DECL
4983 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
4984 {
4985 if (h8300_eightbit_data_p (decl))
dc66a1c4 4986 extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
fb49053f 4987 else if (first && h8300_tiny_data_p (decl))
dc66a1c4 4988 extra_flags = SYMBOL_FLAG_TINY_DATA;
fb49053f 4989 }
772c5265 4990
dc66a1c4 4991 if (extra_flags)
c6a2438a 4992 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
772c5265
RH
4993}
4994
e9eba255
KH
4995/* Output a single-bit extraction. */
4996
441d04c6 4997const char *
cb713a8d 4998output_simode_bld (int bild, rtx operands[])
bd93f126 4999{
27c1afd5
JL
5000 /* Determine if we can clear the destination first. */
5001 int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5002 && REGNO (operands[0]) != REGNO (operands[1]));
6be580c7 5003
27c1afd5
JL
5004 if (clear_first)
5005 output_asm_insn ("sub.l\t%S0,%S0", operands);
bd93f126 5006
27c1afd5
JL
5007 /* Output the bit load or bit inverse load. */
5008 if (bild)
5009 output_asm_insn ("bild\t%Z2,%Y1", operands);
bd93f126 5010 else
27c1afd5 5011 output_asm_insn ("bld\t%Z2,%Y1", operands);
0eb933a0 5012
27c1afd5
JL
5013 if (!clear_first)
5014 output_asm_insn ("xor.l\t%S0,%S0", operands);
0eb933a0 5015
27c1afd5
JL
5016 /* Perform the bit store. */
5017 output_asm_insn ("rotxl.l\t%S0", operands);
bd93f126
JL
5018
5019 /* All done. */
5020 return "";
5021}
e6219736 5022
beed8fc0
AO
5023/* Delayed-branch scheduling is more effective if we have some idea
5024 how long each instruction will be. Use a shorten_branches pass
5025 to get an initial estimate. */
5026
5027static void
5028h8300_reorg (void)
5029{
5030 if (flag_delayed_branch)
5031 shorten_branches (get_insns ());
5032}
5033
7c143ed2
KH
5034/* Nonzero if X is a constant address suitable as an 8-bit absolute,
5035 which is a special case of the 'R' operand. */
5036
803d56f5 5037int
cb713a8d 5038h8300_eightbit_constant_address_p (rtx x)
803d56f5 5039{
ff482c8d 5040 /* The ranges of the 8-bit area. */
d2d199a3
KH
5041 const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5042 const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
803d56f5
KH
5043 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5044 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5045 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5046 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5047
5048 unsigned HOST_WIDE_INT addr;
5049
9675a91e 5050 /* We accept symbols declared with eightbit_data. */
dc66a1c4
RH
5051 if (GET_CODE (x) == SYMBOL_REF)
5052 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
9675a91e 5053
9f9ab303
JL
5054 if (GET_CODE (x) == CONST
5055 && GET_CODE (XEXP (x, 0)) == PLUS
5056 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
5057 && (SYMBOL_REF_FLAGS (XEXP (XEXP (x, 0), 0)) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0)
5058 return 1;
5059
803d56f5
KH
5060 if (GET_CODE (x) != CONST_INT)
5061 return 0;
5062
5063 addr = INTVAL (x);
5064
5065 return (0
27c1afd5 5066 || (TARGET_NORMAL_MODE && IN_RANGE (addr, n1, n2))
803d56f5
KH
5067 || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5068 || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5069}
5070
7c143ed2
KH
5071/* Nonzero if X is a constant address suitable as an 16-bit absolute
5072 on H8/300H and H8S. */
5073
803d56f5 5074int
cb713a8d 5075h8300_tiny_constant_address_p (rtx x)
803d56f5 5076{
3f7211f1 5077 /* The ranges of the 16-bit area. */
803d56f5
KH
5078 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5079 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5080 const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5081 const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5082 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5083 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5084 const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5085 const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5086
5087 unsigned HOST_WIDE_INT addr;
5088
d6456562
KH
5089 switch (GET_CODE (x))
5090 {
5091 case SYMBOL_REF:
a4bb41cc
KH
5092 /* In the normal mode, any symbol fits in the 16-bit absolute
5093 address range. We also accept symbols declared with
5094 tiny_data. */
5095 return (TARGET_NORMAL_MODE
5096 || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
56b8e164 5097
d6456562
KH
5098 case CONST_INT:
5099 addr = INTVAL (x);
5100 return (TARGET_NORMAL_MODE
5101 || (TARGET_H8300H
5102 && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5103 || (TARGET_H8300S
5104 && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
803d56f5 5105
a4bb41cc
KH
5106 case CONST:
5107 return TARGET_NORMAL_MODE;
5108
d6456562
KH
5109 default:
5110 return 0;
5111 }
803d56f5 5112
803d56f5 5113}
9b98dc74 5114
e9eba255
KH
5115/* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5116 locations that can be accessed as a 16-bit word. */
5117
9b98dc74 5118int
cb713a8d 5119byte_accesses_mergeable_p (rtx addr1, rtx addr2)
9b98dc74
KH
5120{
5121 HOST_WIDE_INT offset1, offset2;
5122 rtx reg1, reg2;
5123
5124 if (REG_P (addr1))
5125 {
5126 reg1 = addr1;
5127 offset1 = 0;
5128 }
5129 else if (GET_CODE (addr1) == PLUS
5130 && REG_P (XEXP (addr1, 0))
5131 && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5132 {
5133 reg1 = XEXP (addr1, 0);
5134 offset1 = INTVAL (XEXP (addr1, 1));
5135 }
5136 else
5137 return 0;
5138
5139 if (REG_P (addr2))
5140 {
5141 reg2 = addr2;
5142 offset2 = 0;
5143 }
5144 else if (GET_CODE (addr2) == PLUS
5145 && REG_P (XEXP (addr2, 0))
5146 && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5147 {
5148 reg2 = XEXP (addr2, 0);
5149 offset2 = INTVAL (XEXP (addr2, 1));
5150 }
5151 else
5152 return 0;
5153
5154 if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5155 || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5156 && offset1 % 2 == 0
5157 && offset1 + 1 == offset2)
5158 return 1;
5159
5160 return 0;
5161}
02529902
KH
5162
5163/* Return nonzero if we have the same comparison insn as I3 two insns
19cff4db 5164 before I3. I3 is assumed to be a comparison insn. */
02529902
KH
5165
5166int
82082f65 5167same_cmp_preceding_p (rtx_insn *i3)
02529902 5168{
68a1a6c0 5169 rtx_insn *i1, *i2;
02529902
KH
5170
5171 /* Make sure we have a sequence of three insns. */
5172 i2 = prev_nonnote_insn (i3);
68a1a6c0 5173 if (i2 == NULL)
02529902
KH
5174 return 0;
5175 i1 = prev_nonnote_insn (i2);
68a1a6c0 5176 if (i1 == NULL)
02529902
KH
5177 return 0;
5178
5179 return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5180 && any_condjump_p (i2) && onlyjump_p (i2));
5181}
c87ec0ba 5182
c06d5c85
KH
5183/* Return nonzero if we have the same comparison insn as I1 two insns
5184 after I1. I1 is assumed to be a comparison insn. */
5185
5186int
82082f65 5187same_cmp_following_p (rtx_insn *i1)
c06d5c85 5188{
68a1a6c0 5189 rtx_insn *i2, *i3;
c06d5c85
KH
5190
5191 /* Make sure we have a sequence of three insns. */
5192 i2 = next_nonnote_insn (i1);
68a1a6c0 5193 if (i2 == NULL)
c06d5c85
KH
5194 return 0;
5195 i3 = next_nonnote_insn (i2);
68a1a6c0 5196 if (i3 == NULL)
c06d5c85
KH
5197 return 0;
5198
5199 return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5200 && any_condjump_p (i2) && onlyjump_p (i2));
5201}
5202
a466bea3 5203/* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
1ae58c30 5204 (or pops) N registers. OPERANDS are assumed to be an array of
a466bea3
KH
5205 registers. */
5206
5207int
5208h8300_regs_ok_for_stm (int n, rtx operands[])
5209{
5210 switch (n)
5211 {
5212 case 2:
5213 return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5214 || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5215 || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5216 case 3:
5217 return ((REGNO (operands[0]) == 0
5218 && REGNO (operands[1]) == 1
5219 && REGNO (operands[2]) == 2)
5220 || (REGNO (operands[0]) == 4
5221 && REGNO (operands[1]) == 5
5222 && REGNO (operands[2]) == 6));
5223
5224 case 4:
5225 return (REGNO (operands[0]) == 0
5226 && REGNO (operands[1]) == 1
5227 && REGNO (operands[2]) == 2
5228 && REGNO (operands[3]) == 3);
8c440872
NS
5229 default:
5230 gcc_unreachable ();
a466bea3 5231 }
a466bea3
KH
5232}
5233
c87ec0ba
NY
5234/* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
5235
5236int
5237h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5238 unsigned int new_reg)
5239{
5240 /* Interrupt functions can only use registers that have already been
5241 saved by the prologue, even if they would normally be
5242 call-clobbered. */
5243
5244 if (h8300_current_function_interrupt_function_p ()
6fb5fa3c 5245 && !df_regs_ever_live_p (new_reg))
c87ec0ba
NY
5246 return 0;
5247
80e58519 5248 return 1;
c87ec0ba 5249}
d0022200 5250
2e762884
DD
5251/* Returns true if register REGNO is safe to be allocated as a scratch
5252 register in the current function. */
5253
5254static bool
5255h8300_hard_regno_scratch_ok (unsigned int regno)
5256{
5257 if (h8300_current_function_interrupt_function_p ()
5258 && ! WORD_REG_USED (regno))
5259 return false;
5260
5261 return true;
5262}
5263
5264
d0022200
KH
5265/* Return nonzero if X is a REG or SUBREG suitable as a base register. */
5266
5267static int
5268h8300_rtx_ok_for_base_p (rtx x, int strict)
5269{
5270 /* Strip off SUBREG if any. */
5271 if (GET_CODE (x) == SUBREG)
5272 x = SUBREG_REG (x);
5273
5274 return (REG_P (x)
5275 && (strict
5276 ? REG_OK_FOR_BASE_STRICT_P (x)
5277 : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5278}
5279
5280/* Return nozero if X is a legitimate address. On the H8/300, a
5281 legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5282 CONSTANT_ADDRESS. */
5283
c6c3dba9 5284static bool
ef4bddc2 5285h8300_legitimate_address_p (machine_mode mode, rtx x, bool strict)
d0022200
KH
5286{
5287 /* The register indirect addresses like @er0 is always valid. */
5288 if (h8300_rtx_ok_for_base_p (x, strict))
5289 return 1;
5290
5291 if (CONSTANT_ADDRESS_P (x))
5292 return 1;
5293
beed8fc0
AO
5294 if (TARGET_H8300SX
5295 && ( GET_CODE (x) == PRE_INC
5296 || GET_CODE (x) == PRE_DEC
5297 || GET_CODE (x) == POST_INC
5298 || GET_CODE (x) == POST_DEC)
5299 && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5300 return 1;
5301
d0022200
KH
5302 if (GET_CODE (x) == PLUS
5303 && CONSTANT_ADDRESS_P (XEXP (x, 1))
beed8fc0
AO
5304 && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5305 mode, 0), strict))
d0022200
KH
5306 return 1;
5307
5308 return 0;
5309}
07ee3b58 5310
f939c3e6 5311/* Implement TARGET_HARD_REGNO_MODE_OK. */
07ee3b58 5312
f939c3e6
RS
5313static bool
5314h8300_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
07ee3b58 5315{
27c1afd5
JL
5316 /* MAC register can only be of SImode. Otherwise, anything
5317 goes. */
5318 return regno == MAC_REG ? mode == SImode : 1;
07ee3b58 5319}
f9b4f8c1 5320
99e1629f
RS
5321/* Implement TARGET_MODES_TIEABLE_P. */
5322
5323static bool
5324h8300_modes_tieable_p (machine_mode mode1, machine_mode mode2)
5325{
5326 return (mode1 == mode2
5327 || ((mode1 == QImode
5328 || mode1 == HImode
27c1afd5 5329 || mode1 == SImode)
99e1629f
RS
5330 && (mode2 == QImode
5331 || mode2 == HImode
27c1afd5 5332 || mode2 == SImode)));
99e1629f
RS
5333}
5334
f9b4f8c1
RH
5335/* Helper function for the move patterns. Make sure a move is legitimate. */
5336
5337bool
5338h8300_move_ok (rtx dest, rtx src)
5339{
5340 rtx addr, other;
5341
5342 /* Validate that at least one operand is a register. */
5343 if (MEM_P (dest))
5344 {
5345 if (MEM_P (src) || CONSTANT_P (src))
5346 return false;
5347 addr = XEXP (dest, 0);
5348 other = src;
5349 }
5350 else if (MEM_P (src))
5351 {
5352 addr = XEXP (src, 0);
5353 other = dest;
5354 }
5355 else
5356 return true;
5357
5358 /* Validate that auto-inc doesn't affect OTHER. */
5359 if (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC)
5360 return true;
5361 addr = XEXP (addr, 0);
5362
5363 if (addr == stack_pointer_rtx)
5364 return register_no_sp_elim_operand (other, VOIDmode);
5365 else
5366 return !reg_overlap_mentioned_p(other, addr);
5367}
6e014ef3 5368\f
c15c90bb
ZW
5369/* Perform target dependent optabs initialization. */
5370static void
5371h8300_init_libfuncs (void)
5372{
5373 set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5374 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5375 set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5376 set_optab_libfunc (smod_optab, HImode, "__modhi3");
5377 set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5378}
5379\f
9eaa7740
AS
5380/* Worker function for TARGET_FUNCTION_VALUE.
5381
5382 On the H8 the return value is in R0/R1. */
5383
5384static rtx
5385h8300_function_value (const_tree ret_type,
5386 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
5387 bool outgoing ATTRIBUTE_UNUSED)
5388{
5389 return gen_rtx_REG (TYPE_MODE (ret_type), R0_REG);
5390}
5391
5392/* Worker function for TARGET_LIBCALL_VALUE.
5393
5394 On the H8 the return value is in R0/R1. */
5395
5396static rtx
ef4bddc2 5397h8300_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
9eaa7740
AS
5398{
5399 return gen_rtx_REG (mode, R0_REG);
5400}
5401
5402/* Worker function for TARGET_FUNCTION_VALUE_REGNO_P.
5403
5404 On the H8, R0 is the only register thus used. */
5405
5406static bool
5407h8300_function_value_regno_p (const unsigned int regno)
5408{
5409 return (regno == R0_REG);
5410}
5411
e9eba255
KH
5412/* Worker function for TARGET_RETURN_IN_MEMORY. */
5413
34bf1fe3 5414static bool
586de218 5415h8300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
34bf1fe3
KH
5416{
5417 return (TYPE_MODE (type) == BLKmode
27c1afd5 5418 || GET_MODE_SIZE (TYPE_MODE (type)) > 8);
34bf1fe3
KH
5419}
5420\f
9f6ef043
RH
5421/* We emit the entire trampoline here. Depending on the pointer size,
5422 we use a different trampoline.
5423
5424 Pmode == HImode
5425 vvvv context
5426 1 0000 7903xxxx mov.w #0x1234,r3
5427 2 0004 5A00xxxx jmp @0x1234
5428 ^^^^ function
5429
5430 Pmode == SImode
5431 vvvvvvvv context
5432 2 0000 7A03xxxxxxxx mov.l #0x12345678,er3
5433 3 0006 5Axxxxxx jmp @0x123456
5434 ^^^^^^ function
5435*/
5436
5437static void
5438h8300_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
5439{
5440 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
5441 rtx mem;
5442
5443 if (Pmode == HImode)
5444 {
5445 mem = adjust_address (m_tramp, HImode, 0);
5446 emit_move_insn (mem, GEN_INT (0x7903));
5447 mem = adjust_address (m_tramp, Pmode, 2);
5448 emit_move_insn (mem, cxt);
5449 mem = adjust_address (m_tramp, HImode, 4);
5450 emit_move_insn (mem, GEN_INT (0x5a00));
5451 mem = adjust_address (m_tramp, Pmode, 6);
5452 emit_move_insn (mem, fnaddr);
5453 }
5454 else
5455 {
5456 rtx tem;
5457
5458 mem = adjust_address (m_tramp, HImode, 0);
5459 emit_move_insn (mem, GEN_INT (0x7a03));
5460 mem = adjust_address (m_tramp, Pmode, 2);
5461 emit_move_insn (mem, cxt);
5462
5463 tem = copy_to_reg (fnaddr);
5464 emit_insn (gen_andsi3 (tem, tem, GEN_INT (0x00ffffff)));
5465 emit_insn (gen_iorsi3 (tem, tem, GEN_INT (0x5a000000)));
5466 mem = adjust_address (m_tramp, SImode, 6);
5467 emit_move_insn (mem, tem);
5468 }
5469}
7b4df2bf
RS
5470
5471/* Implement PUSH_ROUNDING.
5472
5473 On the H8/300, @-sp really pushes a byte if you ask it to - but that's
5474 dangerous, so we claim that it always pushes a word, then we catch
5475 the mov.b rx,@-sp and turn it into a mov.w rx,@-sp on output.
5476
5477 On the H8/300H, we simplify TARGET_QUICKCALL by setting this to 4
5478 and doing a similar thing. */
5479
5480poly_int64
5481h8300_push_rounding (poly_int64 bytes)
5482{
5483 return ((bytes + PARM_BOUNDARY / 8 - 1) & (-PARM_BOUNDARY / 8));
5484}
9f6ef043 5485\f
6e014ef3
KH
5486/* Initialize the GCC target structure. */
5487#undef TARGET_ATTRIBUTE_TABLE
5488#define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
5489
5490#undef TARGET_ASM_ALIGNED_HI_OP
5491#define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
5492
1bc7c5b6
ZW
5493#undef TARGET_ASM_FILE_START
5494#define TARGET_ASM_FILE_START h8300_file_start
5495#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
5496#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
5497
6e014ef3
KH
5498#undef TARGET_ASM_FILE_END
5499#define TARGET_ASM_FILE_END h8300_file_end
5500
88cb339e
N
5501#undef TARGET_PRINT_OPERAND
5502#define TARGET_PRINT_OPERAND h8300_print_operand
5503#undef TARGET_PRINT_OPERAND_ADDRESS
5504#define TARGET_PRINT_OPERAND_ADDRESS h8300_print_operand_address
5505#undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
5506#define TARGET_PRINT_OPERAND_PUNCT_VALID_P h8300_print_operand_punct_valid_p
5507
6e014ef3
KH
5508#undef TARGET_ENCODE_SECTION_INFO
5509#define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
5510
5511#undef TARGET_INSERT_ATTRIBUTES
5512#define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
5513
88cb339e
N
5514#undef TARGET_REGISTER_MOVE_COST
5515#define TARGET_REGISTER_MOVE_COST h8300_register_move_cost
5516
6e014ef3
KH
5517#undef TARGET_RTX_COSTS
5518#define TARGET_RTX_COSTS h8300_rtx_costs
5519
c15c90bb
ZW
5520#undef TARGET_INIT_LIBFUNCS
5521#define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
5522
9eaa7740
AS
5523#undef TARGET_FUNCTION_VALUE
5524#define TARGET_FUNCTION_VALUE h8300_function_value
5525
5526#undef TARGET_LIBCALL_VALUE
5527#define TARGET_LIBCALL_VALUE h8300_libcall_value
5528
5529#undef TARGET_FUNCTION_VALUE_REGNO_P
5530#define TARGET_FUNCTION_VALUE_REGNO_P h8300_function_value_regno_p
5531
34bf1fe3
KH
5532#undef TARGET_RETURN_IN_MEMORY
5533#define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
5534
56f9413b
NF
5535#undef TARGET_FUNCTION_ARG
5536#define TARGET_FUNCTION_ARG h8300_function_arg
5537
5538#undef TARGET_FUNCTION_ARG_ADVANCE
5539#define TARGET_FUNCTION_ARG_ADVANCE h8300_function_arg_advance
5540
beed8fc0
AO
5541#undef TARGET_MACHINE_DEPENDENT_REORG
5542#define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
5543
2e762884
DD
5544#undef TARGET_HARD_REGNO_SCRATCH_OK
5545#define TARGET_HARD_REGNO_SCRATCH_OK h8300_hard_regno_scratch_ok
5546
f939c3e6
RS
5547#undef TARGET_HARD_REGNO_MODE_OK
5548#define TARGET_HARD_REGNO_MODE_OK h8300_hard_regno_mode_ok
5549
99e1629f
RS
5550#undef TARGET_MODES_TIEABLE_P
5551#define TARGET_MODES_TIEABLE_P h8300_modes_tieable_p
5552
d81db636
SB
5553#undef TARGET_LRA_P
5554#define TARGET_LRA_P hook_bool_void_false
5555
c6c3dba9
PB
5556#undef TARGET_LEGITIMATE_ADDRESS_P
5557#define TARGET_LEGITIMATE_ADDRESS_P h8300_legitimate_address_p
5558
7b5cbb57
AS
5559#undef TARGET_CAN_ELIMINATE
5560#define TARGET_CAN_ELIMINATE h8300_can_eliminate
5561
5efd84c5
NF
5562#undef TARGET_CONDITIONAL_REGISTER_USAGE
5563#define TARGET_CONDITIONAL_REGISTER_USAGE h8300_conditional_register_usage
5564
9f6ef043
RH
5565#undef TARGET_TRAMPOLINE_INIT
5566#define TARGET_TRAMPOLINE_INIT h8300_trampoline_init
5567
c5387660
JM
5568#undef TARGET_OPTION_OVERRIDE
5569#define TARGET_OPTION_OVERRIDE h8300_option_override
5570
f52d97da
AS
5571#undef TARGET_MODE_DEPENDENT_ADDRESS_P
5572#define TARGET_MODE_DEPENDENT_ADDRESS_P h8300_mode_dependent_address_p
5573
efbf3c34
JL
5574#undef TARGET_HAVE_SPECULATION_SAFE_VALUE
5575#define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
5576
f16897cb
AL
5577#undef TARGET_FLAGS_REGNUM
5578#define TARGET_FLAGS_REGNUM 12
5579
6e014ef3 5580struct gcc_target targetm = TARGET_INITIALIZER;