]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/h8300/h8300.c
s390: Allow more vcond_mask patterns.
[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
1953 || GET_CODE (op0) == REG || GET_CODE (op0) == MULT))
1954 return CCZNmode;
1955
1956 return CCmode;
1957}
1958
beed8fc0
AO
1959\f
1960/* Given that X occurs in an address of the form (plus X constant),
1961 return the part of X that is expected to be a register. There are
1962 four kinds of addressing mode to recognize:
1963
1964 @(dd,Rn)
1965 @(dd,RnL.b)
1966 @(dd,Rn.w)
1967 @(dd,ERn.l)
1968
1969 If SIZE is nonnull, and the address is one of the last three forms,
1970 set *SIZE to the index multiplication factor. Set it to 0 for
1971 plain @(dd,Rn) addresses.
1972
1973 MODE is the mode of the value being accessed. It can be VOIDmode
1974 if the address is known to be valid, but its mode is unknown. */
1975
f52d97da 1976static rtx
ef4bddc2 1977h8300_get_index (rtx x, machine_mode mode, int *size)
beed8fc0
AO
1978{
1979 int dummy, factor;
1980
1981 if (size == 0)
1982 size = &dummy;
1983
1984 factor = (mode == VOIDmode ? 0 : GET_MODE_SIZE (mode));
1985 if (TARGET_H8300SX
1986 && factor <= 4
1987 && (mode == VOIDmode
1988 || GET_MODE_CLASS (mode) == MODE_INT
1989 || GET_MODE_CLASS (mode) == MODE_FLOAT))
1990 {
1991 if (factor <= 1 && GET_CODE (x) == ZERO_EXTEND)
1992 {
1993 /* When accessing byte-sized values, the index can be
1994 a zero-extended QImode or HImode register. */
1995 *size = GET_MODE_SIZE (GET_MODE (XEXP (x, 0)));
1996 return XEXP (x, 0);
1997 }
1998 else
1999 {
2000 /* We're looking for addresses of the form:
2001
2002 (mult X I)
2003 or (mult (zero_extend X) I)
2004
2005 where I is the size of the operand being accessed.
2006 The canonical form of the second expression is:
2007
2008 (and (mult (subreg X) I) J)
2009
2010 where J == GET_MODE_MASK (GET_MODE (X)) * I. */
2011 rtx index;
2012
2013 if (GET_CODE (x) == AND
2014 && GET_CODE (XEXP (x, 1)) == CONST_INT
2015 && (factor == 0
2016 || INTVAL (XEXP (x, 1)) == 0xff * factor
2017 || INTVAL (XEXP (x, 1)) == 0xffff * factor))
2018 {
2019 index = XEXP (x, 0);
2020 *size = (INTVAL (XEXP (x, 1)) >= 0xffff ? 2 : 1);
2021 }
2022 else
2023 {
2024 index = x;
2025 *size = 4;
2026 }
2027
2028 if (GET_CODE (index) == MULT
2029 && GET_CODE (XEXP (index, 1)) == CONST_INT
2030 && (factor == 0 || factor == INTVAL (XEXP (index, 1))))
2031 return XEXP (index, 0);
2032 }
2033 }
2034 *size = 0;
2035 return x;
2036}
2037\f
f52d97da
AS
2038/* Worker function for TARGET_MODE_DEPENDENT_ADDRESS_P.
2039
2040 On the H8/300, the predecrement and postincrement address depend thus
2041 (the amount of decrement or increment being the length of the operand). */
2042
2043static bool
5bfed9a9
GJL
2044h8300_mode_dependent_address_p (const_rtx addr,
2045 addr_space_t as ATTRIBUTE_UNUSED)
f52d97da
AS
2046{
2047 if (GET_CODE (addr) == PLUS
2048 && h8300_get_index (XEXP (addr, 0), VOIDmode, 0) != XEXP (addr, 0))
2049 return true;
2050
2051 return false;
2052}
2053\f
beed8fc0
AO
2054static const h8300_length_table addb_length_table =
2055{
2056 /* #xx Rs @aa @Rs @xx */
2057 { 2, 2, 4, 4, 4 }, /* add.b xx,Rd */
2058 { 4, 4, 4, 4, 6 }, /* add.b xx,@aa */
2059 { 4, 4, 4, 4, 6 }, /* add.b xx,@Rd */
2060 { 6, 4, 4, 4, 6 } /* add.b xx,@xx */
2061};
2062
2063static const h8300_length_table addw_length_table =
2064{
2065 /* #xx Rs @aa @Rs @xx */
2066 { 2, 2, 4, 4, 4 }, /* add.w xx,Rd */
2067 { 4, 4, 4, 4, 6 }, /* add.w xx,@aa */
2068 { 4, 4, 4, 4, 6 }, /* add.w xx,@Rd */
2069 { 4, 4, 4, 4, 6 } /* add.w xx,@xx */
2070};
2071
2072static const h8300_length_table addl_length_table =
2073{
2074 /* #xx Rs @aa @Rs @xx */
2075 { 2, 2, 4, 4, 4 }, /* add.l xx,Rd */
2076 { 4, 4, 6, 6, 6 }, /* add.l xx,@aa */
2077 { 4, 4, 6, 6, 6 }, /* add.l xx,@Rd */
2078 { 4, 4, 6, 6, 6 } /* add.l xx,@xx */
2079};
2080
2081#define logicb_length_table addb_length_table
2082#define logicw_length_table addw_length_table
2083
2084static const h8300_length_table logicl_length_table =
2085{
2086 /* #xx Rs @aa @Rs @xx */
2087 { 2, 4, 4, 4, 4 }, /* and.l xx,Rd */
2088 { 4, 4, 6, 6, 6 }, /* and.l xx,@aa */
2089 { 4, 4, 6, 6, 6 }, /* and.l xx,@Rd */
2090 { 4, 4, 6, 6, 6 } /* and.l xx,@xx */
2091};
2092
2093static const h8300_length_table movb_length_table =
2094{
2095 /* #xx Rs @aa @Rs @xx */
2096 { 2, 2, 2, 2, 4 }, /* mov.b xx,Rd */
2097 { 4, 2, 4, 4, 4 }, /* mov.b xx,@aa */
2098 { 4, 2, 4, 4, 4 }, /* mov.b xx,@Rd */
2099 { 4, 4, 4, 4, 4 } /* mov.b xx,@xx */
2100};
2101
2102#define movw_length_table movb_length_table
2103
2104static const h8300_length_table movl_length_table =
2105{
2106 /* #xx Rs @aa @Rs @xx */
2107 { 2, 2, 4, 4, 4 }, /* mov.l xx,Rd */
2108 { 4, 4, 4, 4, 4 }, /* mov.l xx,@aa */
2109 { 4, 4, 4, 4, 4 }, /* mov.l xx,@Rd */
2110 { 4, 4, 4, 4, 4 } /* mov.l xx,@xx */
2111};
2112
2113/* Return the size of the given address or displacement constant. */
2114
2115static unsigned int
2116h8300_constant_length (rtx constant)
2117{
2118 /* Check for (@d:16,Reg). */
2119 if (GET_CODE (constant) == CONST_INT
2120 && IN_RANGE (INTVAL (constant), -0x8000, 0x7fff))
2121 return 2;
2122
2123 /* Check for (@d:16,Reg) in cases where the displacement is
2124 an absolute address. */
2125 if (Pmode == HImode || h8300_tiny_constant_address_p (constant))
2126 return 2;
2127
2128 return 4;
2129}
2130
2131/* Return the size of a displacement field in address ADDR, which should
2132 have the form (plus X constant). SIZE is the number of bytes being
2133 accessed. */
2134
2135static unsigned int
2136h8300_displacement_length (rtx addr, int size)
2137{
2138 rtx offset;
2139
2140 offset = XEXP (addr, 1);
2141
2142 /* Check for @(d:2,Reg). */
2143 if (register_operand (XEXP (addr, 0), VOIDmode)
2144 && GET_CODE (offset) == CONST_INT
2145 && (INTVAL (offset) == size
2146 || INTVAL (offset) == size * 2
2147 || INTVAL (offset) == size * 3))
2148 return 0;
2149
2150 return h8300_constant_length (offset);
2151}
2152
0a2aaacc
KG
2153/* Store the class of operand OP in *OPCLASS and return the length of any
2154 extra operand fields. SIZE is the number of bytes in OP. OPCLASS
beed8fc0
AO
2155 can be null if only the length is needed. */
2156
2157static unsigned int
0a2aaacc 2158h8300_classify_operand (rtx op, int size, enum h8300_operand_class *opclass)
beed8fc0
AO
2159{
2160 enum h8300_operand_class dummy;
2161
0a2aaacc
KG
2162 if (opclass == 0)
2163 opclass = &dummy;
beed8fc0
AO
2164
2165 if (CONSTANT_P (op))
2166 {
0a2aaacc 2167 *opclass = H8OP_IMMEDIATE;
beed8fc0
AO
2168
2169 /* Byte-sized immediates are stored in the opcode fields. */
2170 if (size == 1)
2171 return 0;
2172
2173 /* If this is a 32-bit instruction, see whether the constant
2174 will fit into a 16-bit immediate field. */
2175 if (TARGET_H8300SX
2176 && size == 4
2177 && GET_CODE (op) == CONST_INT
2178 && IN_RANGE (INTVAL (op), 0, 0xffff))
2179 return 2;
2180
2181 return size;
2182 }
2183 else if (GET_CODE (op) == MEM)
2184 {
2185 op = XEXP (op, 0);
2186 if (CONSTANT_P (op))
2187 {
0a2aaacc 2188 *opclass = H8OP_MEM_ABSOLUTE;
beed8fc0
AO
2189 return h8300_constant_length (op);
2190 }
2191 else if (GET_CODE (op) == PLUS && CONSTANT_P (XEXP (op, 1)))
2192 {
0a2aaacc 2193 *opclass = H8OP_MEM_COMPLEX;
beed8fc0
AO
2194 return h8300_displacement_length (op, size);
2195 }
2196 else if (GET_RTX_CLASS (GET_CODE (op)) == RTX_AUTOINC)
2197 {
0a2aaacc 2198 *opclass = H8OP_MEM_COMPLEX;
beed8fc0
AO
2199 return 0;
2200 }
2201 else if (register_operand (op, VOIDmode))
2202 {
0a2aaacc 2203 *opclass = H8OP_MEM_BASE;
beed8fc0
AO
2204 return 0;
2205 }
2206 }
8c440872 2207 gcc_assert (register_operand (op, VOIDmode));
0a2aaacc 2208 *opclass = H8OP_REGISTER;
8c440872 2209 return 0;
beed8fc0
AO
2210}
2211
2212/* Return the length of the instruction described by TABLE given that
2213 its operands are OP1 and OP2. OP1 must be an h8300_dst_operand
2214 and OP2 must be an h8300_src_operand. */
2215
2216static unsigned int
2217h8300_length_from_table (rtx op1, rtx op2, const h8300_length_table *table)
2218{
2219 enum h8300_operand_class op1_class, op2_class;
2220 unsigned int size, immediate_length;
2221
2222 size = GET_MODE_SIZE (GET_MODE (op1));
2223 immediate_length = (h8300_classify_operand (op1, size, &op1_class)
2224 + h8300_classify_operand (op2, size, &op2_class));
2225 return immediate_length + (*table)[op1_class - 1][op2_class];
2226}
2227
2228/* Return the length of a unary instruction such as neg or not given that
2229 its operand is OP. */
2230
2231unsigned int
2232h8300_unary_length (rtx op)
2233{
0a2aaacc 2234 enum h8300_operand_class opclass;
beed8fc0
AO
2235 unsigned int size, operand_length;
2236
2237 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc
KG
2238 operand_length = h8300_classify_operand (op, size, &opclass);
2239 switch (opclass)
beed8fc0
AO
2240 {
2241 case H8OP_REGISTER:
2242 return 2;
2243
2244 case H8OP_MEM_BASE:
2245 return (size == 4 ? 6 : 4);
2246
2247 case H8OP_MEM_ABSOLUTE:
2248 return operand_length + (size == 4 ? 6 : 4);
2249
2250 case H8OP_MEM_COMPLEX:
2251 return operand_length + 6;
2252
2253 default:
8c440872 2254 gcc_unreachable ();
beed8fc0
AO
2255 }
2256}
2257
2258/* Likewise short immediate instructions such as add.w #xx:3,OP. */
2259
2260static unsigned int
2261h8300_short_immediate_length (rtx op)
2262{
0a2aaacc 2263 enum h8300_operand_class opclass;
beed8fc0
AO
2264 unsigned int size, operand_length;
2265
2266 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc 2267 operand_length = h8300_classify_operand (op, size, &opclass);
beed8fc0 2268
0a2aaacc 2269 switch (opclass)
beed8fc0
AO
2270 {
2271 case H8OP_REGISTER:
2272 return 2;
2273
2274 case H8OP_MEM_BASE:
2275 case H8OP_MEM_ABSOLUTE:
2276 case H8OP_MEM_COMPLEX:
2277 return 4 + operand_length;
2278
2279 default:
8c440872 2280 gcc_unreachable ();
beed8fc0
AO
2281 }
2282}
2283
2284/* Likewise bitfield load and store instructions. */
48837e29 2285
beed8fc0
AO
2286static unsigned int
2287h8300_bitfield_length (rtx op, rtx op2)
2288{
0a2aaacc 2289 enum h8300_operand_class opclass;
beed8fc0
AO
2290 unsigned int size, operand_length;
2291
2292 if (GET_CODE (op) == REG)
2293 op = op2;
8c440872 2294 gcc_assert (GET_CODE (op) != REG);
beed8fc0
AO
2295
2296 size = GET_MODE_SIZE (GET_MODE (op));
0a2aaacc 2297 operand_length = h8300_classify_operand (op, size, &opclass);
beed8fc0 2298
0a2aaacc 2299 switch (opclass)
beed8fc0
AO
2300 {
2301 case H8OP_MEM_BASE:
2302 case H8OP_MEM_ABSOLUTE:
2303 case H8OP_MEM_COMPLEX:
2304 return 4 + operand_length;
2305
2306 default:
8c440872 2307 gcc_unreachable ();
07aae5c2 2308 }
48837e29
DE
2309}
2310
beed8fc0 2311/* Calculate the length of general binary instruction INSN using TABLE. */
8ccf5d5f 2312
beed8fc0 2313static unsigned int
e7041fcc 2314h8300_binary_length (rtx_insn *insn, const h8300_length_table *table)
8ccf5d5f 2315{
beed8fc0 2316 rtx set;
f16897cb
AL
2317 rtx pattern;
2318
2319 if (GET_CODE (insn) != INSN)
2320 gcc_unreachable ();
beed8fc0 2321
f16897cb
AL
2322 pattern = PATTERN (insn);
2323 if (GET_CODE (pattern) == PARALLEL
2324 && GET_CODE (XVECEXP (pattern, 0, 0)) == SET
2325 && GET_CODE (SET_SRC (XVECEXP (pattern, 0, 0))) == COMPARE)
2326 set = XVECEXP (pattern, 0, 1);
2327 else
2328 set = single_set (insn);
8c440872 2329 gcc_assert (set);
beed8fc0
AO
2330
2331 if (BINARY_P (SET_SRC (set)))
2332 return h8300_length_from_table (XEXP (SET_SRC (set), 0),
2333 XEXP (SET_SRC (set), 1), table);
beed8fc0 2334 else
8c440872
NS
2335 {
2336 gcc_assert (GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == RTX_TERNARY);
2337 return h8300_length_from_table (XEXP (XEXP (SET_SRC (set), 1), 0),
2338 XEXP (XEXP (SET_SRC (set), 1), 1),
2339 table);
2340 }
8ccf5d5f
KH
2341}
2342
beed8fc0
AO
2343/* Subroutine of h8300_move_length. Return true if OP is 1- or 2-byte
2344 memory reference and either (1) it has the form @(d:16,Rn) or
2345 (2) its address has the code given by INC_CODE. */
8ccf5d5f 2346
beed8fc0
AO
2347static bool
2348h8300_short_move_mem_p (rtx op, enum rtx_code inc_code)
8ccf5d5f 2349{
beed8fc0
AO
2350 rtx addr;
2351 unsigned int size;
2352
2353 if (GET_CODE (op) != MEM)
2354 return false;
2355
2356 addr = XEXP (op, 0);
2357 size = GET_MODE_SIZE (GET_MODE (op));
2358 if (size != 1 && size != 2)
2359 return false;
2360
2361 return (GET_CODE (addr) == inc_code
2362 || (GET_CODE (addr) == PLUS
2363 && GET_CODE (XEXP (addr, 0)) == REG
2364 && h8300_displacement_length (addr, size) == 2));
8ccf5d5f
KH
2365}
2366
beed8fc0
AO
2367/* Calculate the length of move instruction INSN using the given length
2368 table. Although the tables are correct for most cases, there is some
2369 irregularity in the length of mov.b and mov.w. The following forms:
8ccf5d5f 2370
beed8fc0
AO
2371 mov @ERs+, Rd
2372 mov @(d:16,ERs), Rd
2373 mov Rs, @-ERd
2374 mov Rs, @(d:16,ERd)
2375
2376 are two bytes shorter than most other "mov Rs, @complex" or
2377 "mov @complex,Rd" combinations. */
2378
2379static unsigned int
2380h8300_move_length (rtx *operands, const h8300_length_table *table)
8ccf5d5f 2381{
beed8fc0
AO
2382 unsigned int size;
2383
2384 size = h8300_length_from_table (operands[0], operands[1], table);
2385 if (REG_P (operands[0]) && h8300_short_move_mem_p (operands[1], POST_INC))
2386 size -= 2;
2387 if (REG_P (operands[1]) && h8300_short_move_mem_p (operands[0], PRE_DEC))
2388 size -= 2;
2389 return size;
8ccf5d5f
KH
2390}
2391
beed8fc0
AO
2392/* Return the length of a mova instruction with the given operands.
2393 DEST is the register destination, SRC is the source address and
2394 OFFSET is the 16-bit or 32-bit displacement. */
2873836b 2395
beed8fc0
AO
2396static unsigned int
2397h8300_mova_length (rtx dest, rtx src, rtx offset)
2873836b 2398{
beed8fc0
AO
2399 unsigned int size;
2400
2401 size = (2
2402 + h8300_constant_length (offset)
2403 + h8300_classify_operand (src, GET_MODE_SIZE (GET_MODE (src)), 0));
2404 if (!REG_P (dest) || !REG_P (src) || REGNO (src) != REGNO (dest))
2405 size += 2;
2406 return size;
2873836b
KH
2407}
2408
beed8fc0
AO
2409/* Compute the length of INSN based on its length_table attribute.
2410 OPERANDS is the array of its operands. */
2873836b 2411
beed8fc0 2412unsigned int
e7041fcc 2413h8300_insn_length_from_table (rtx_insn *insn, rtx * operands)
2873836b 2414{
beed8fc0
AO
2415 switch (get_attr_length_table (insn))
2416 {
2417 case LENGTH_TABLE_NONE:
8c440872 2418 gcc_unreachable ();
beed8fc0 2419
c3c3ddbb
JL
2420 case LENGTH_TABLE_ADD:
2421 if (GET_MODE (operands[0]) == QImode)
2422 return h8300_binary_length (insn, &addb_length_table);
2423 else if (GET_MODE (operands[0]) == HImode)
2424 return h8300_binary_length (insn, &addw_length_table);
2425 else if (GET_MODE (operands[0]) == SImode)
2426 return h8300_binary_length (insn, &addl_length_table);
2427 gcc_unreachable ();
beed8fc0
AO
2428
2429 case LENGTH_TABLE_LOGICB:
2430 return h8300_binary_length (insn, &logicb_length_table);
2431
2432 case LENGTH_TABLE_MOVB:
2433 return h8300_move_length (operands, &movb_length_table);
2434
2435 case LENGTH_TABLE_MOVW:
2436 return h8300_move_length (operands, &movw_length_table);
2437
2438 case LENGTH_TABLE_MOVL:
2439 return h8300_move_length (operands, &movl_length_table);
2440
2441 case LENGTH_TABLE_MOVA:
2442 return h8300_mova_length (operands[0], operands[1], operands[2]);
2443
2444 case LENGTH_TABLE_MOVA_ZERO:
2445 return h8300_mova_length (operands[0], operands[1], const0_rtx);
2446
2447 case LENGTH_TABLE_UNARY:
2448 return h8300_unary_length (operands[0]);
2449
2450 case LENGTH_TABLE_MOV_IMM4:
2451 return 2 + h8300_classify_operand (operands[0], 0, 0);
2452
2453 case LENGTH_TABLE_SHORT_IMMEDIATE:
2454 return h8300_short_immediate_length (operands[0]);
2455
2456 case LENGTH_TABLE_BITFIELD:
2457 return h8300_bitfield_length (operands[0], operands[1]);
2458
2459 case LENGTH_TABLE_BITBRANCH:
2460 return h8300_bitfield_length (operands[1], operands[2]) - 2;
8c440872
NS
2461
2462 default:
2463 gcc_unreachable ();
beed8fc0 2464 }
2873836b
KH
2465}
2466
beed8fc0
AO
2467/* Return true if LHS and RHS are memory references that can be mapped
2468 to the same h8sx assembly operand. LHS appears as the destination of
2469 an instruction and RHS appears as a source.
f9d2de4d 2470
beed8fc0
AO
2471 Three cases are allowed:
2472
2473 - RHS is @+Rn or @-Rn, LHS is @Rn
2474 - RHS is @Rn, LHS is @Rn+ or @Rn-
2475 - RHS and LHS have the same address and neither has side effects. */
2476
2477bool
2478h8sx_mergeable_memrefs_p (rtx lhs, rtx rhs)
f9d2de4d 2479{
beed8fc0
AO
2480 if (GET_CODE (rhs) == MEM && GET_CODE (lhs) == MEM)
2481 {
2482 rhs = XEXP (rhs, 0);
2483 lhs = XEXP (lhs, 0);
2484
2485 if (GET_CODE (rhs) == PRE_INC || GET_CODE (rhs) == PRE_DEC)
2486 return rtx_equal_p (XEXP (rhs, 0), lhs);
2487
2488 if (GET_CODE (lhs) == POST_INC || GET_CODE (lhs) == POST_DEC)
2489 return rtx_equal_p (rhs, XEXP (lhs, 0));
2490
2491 if (rtx_equal_p (rhs, lhs))
2492 return true;
2493 }
2494 return false;
f9d2de4d
KH
2495}
2496
beed8fc0
AO
2497/* Return true if OPERANDS[1] can be mapped to the same assembly
2498 operand as OPERANDS[0]. */
f9d2de4d 2499
beed8fc0
AO
2500bool
2501h8300_operands_match_p (rtx *operands)
f9d2de4d 2502{
beed8fc0
AO
2503 if (register_operand (operands[0], VOIDmode)
2504 && register_operand (operands[1], VOIDmode))
2505 return true;
f9d2de4d 2506
beed8fc0
AO
2507 if (h8sx_mergeable_memrefs_p (operands[0], operands[1]))
2508 return true;
2509
2510 return false;
f9d2de4d 2511}
beed8fc0 2512\f
7948a9ea
KH
2513/* Return the length of mov instruction. */
2514
2515unsigned int
2516compute_mov_length (rtx *operands)
2517{
2518 /* If the mov instruction involves a memory operand, we compute the
2519 length, assuming the largest addressing mode is used, and then
2520 adjust later in the function. Otherwise, we compute and return
2521 the exact length in one step. */
ef4bddc2 2522 machine_mode mode = GET_MODE (operands[0]);
7948a9ea
KH
2523 rtx dest = operands[0];
2524 rtx src = operands[1];
2525 rtx addr;
2526
2527 if (GET_CODE (src) == MEM)
2528 addr = XEXP (src, 0);
2529 else if (GET_CODE (dest) == MEM)
2530 addr = XEXP (dest, 0);
2531 else
2532 addr = NULL_RTX;
2533
27c1afd5 2534 unsigned int base_length;
7948a9ea 2535
27c1afd5
JL
2536 switch (mode)
2537 {
2538 case E_QImode:
2539 if (addr == NULL_RTX)
2540 return 2;
7948a9ea 2541
27c1afd5
JL
2542 /* The eightbit addressing is available only in QImode, so
2543 go ahead and take care of it. */
2544 if (h8300_eightbit_constant_address_p (addr))
2545 return 2;
7948a9ea 2546
27c1afd5 2547 base_length = 8;
7948a9ea
KH
2548 break;
2549
27c1afd5
JL
2550 case E_HImode:
2551 if (addr == NULL_RTX)
2552 {
2553 if (REG_P (src))
2554 return 2;
7948a9ea 2555
27c1afd5
JL
2556 if (src == const0_rtx)
2557 return 2;
7948a9ea 2558
27c1afd5
JL
2559 return 4;
2560 }
7948a9ea 2561
27c1afd5
JL
2562 base_length = 8;
2563 break;
7948a9ea 2564
27c1afd5
JL
2565 case E_SImode:
2566 if (addr == NULL_RTX)
2567 {
2568 if (REG_P (src))
7948a9ea 2569 {
27c1afd5 2570 if (REGNO (src) == MAC_REG || REGNO (dest) == MAC_REG)
7948a9ea 2571 return 4;
27c1afd5
JL
2572 else
2573 return 2;
7948a9ea
KH
2574 }
2575
27c1afd5 2576 if (GET_CODE (src) == CONST_INT)
7948a9ea 2577 {
27c1afd5
JL
2578 int val = INTVAL (src);
2579
2580 if (val == 0)
2581 return 2;
7948a9ea 2582
27c1afd5 2583 if (val == (val & 0x00ff) || val == (val & 0xff00))
2c4a71b3
KH
2584 return 4;
2585
27c1afd5
JL
2586 switch (val & 0xffffffff)
2587 {
2588 case 0xffffffff:
2589 case 0xfffffffe:
2590 case 0xfffffffc:
2591 case 0x0000ffff:
2592 case 0x0000fffe:
2593 case 0xffff0000:
2594 case 0xfffe0000:
2595 case 0x00010000:
2596 case 0x00020000:
2597 return 4;
2598 }
7948a9ea 2599 }
27c1afd5 2600 return 6;
7948a9ea
KH
2601 }
2602
27c1afd5
JL
2603 base_length = 10;
2604 break;
7948a9ea 2605
27c1afd5
JL
2606 case E_SFmode:
2607 if (addr == NULL_RTX)
7948a9ea 2608 {
27c1afd5
JL
2609 if (REG_P (src))
2610 return 2;
7948a9ea 2611
27c1afd5 2612 if (satisfies_constraint_G (src))
7948a9ea
KH
2613 return 2;
2614
27c1afd5
JL
2615 return 6;
2616 }
7948a9ea 2617
27c1afd5
JL
2618 base_length = 10;
2619 break;
80e58519 2620
27c1afd5
JL
2621 default:
2622 gcc_unreachable ();
2623 }
7948a9ea 2624
27c1afd5
JL
2625 /* Adjust the length based on the addressing mode used.
2626 Specifically, we subtract the difference between the actual
2627 length and the longest one, which is @(d:24,ERs). */
7948a9ea 2628
27c1afd5
JL
2629 /* @ERs+ and @-ERd are 6 bytes shorter than the longest. */
2630 if (GET_CODE (addr) == PRE_DEC
2631 || GET_CODE (addr) == POST_INC)
2632 return base_length - 6;
7948a9ea 2633
27c1afd5
JL
2634 /* @ERs and @ERd are 6 bytes shorter than the longest. */
2635 if (GET_CODE (addr) == REG)
2636 return base_length - 6;
2c4a71b3 2637
27c1afd5
JL
2638 /* @(d:16,ERs) and @(d:16,ERd) are 4 bytes shorter than the
2639 longest. */
2640 if (GET_CODE (addr) == PLUS
2641 && GET_CODE (XEXP (addr, 0)) == REG
2642 && GET_CODE (XEXP (addr, 1)) == CONST_INT
2643 && INTVAL (XEXP (addr, 1)) > -32768
2644 && INTVAL (XEXP (addr, 1)) < 32767)
2645 return base_length - 4;
7948a9ea 2646
27c1afd5
JL
2647 /* @aa:16 is 4 bytes shorter than the longest. */
2648 if (h8300_tiny_constant_address_p (addr))
2649 return base_length - 4;
7948a9ea 2650
27c1afd5
JL
2651 /* @aa:24 is 2 bytes shorter than the longest. */
2652 if (CONSTANT_P (addr))
2653 return base_length - 2;
7948a9ea 2654
27c1afd5 2655 return base_length;
7948a9ea
KH
2656}
2657\f
e9eba255
KH
2658/* Output an addition insn. */
2659
366a7b27 2660const char *
f16897cb 2661output_plussi (rtx *operands, bool need_flags)
7d6ac401 2662{
ef4bddc2 2663 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2664
8c440872 2665 gcc_assert (mode == SImode);
7d6ac401 2666
27c1afd5
JL
2667 if (GET_CODE (operands[2]) == CONST_INT
2668 && register_operand (operands[1], VOIDmode))
7d6ac401 2669 {
27c1afd5 2670 HOST_WIDE_INT intval = INTVAL (operands[2]);
7d6ac401 2671
27c1afd5
JL
2672 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2673 return "add.l\t%S2,%S0";
2674 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2675 return "sub.l\t%G2,%S0";
beed8fc0 2676
27c1afd5 2677 /* See if we can finish with 2 bytes. */
7d6ac401 2678
27c1afd5
JL
2679 switch ((unsigned int) intval & 0xffffffff)
2680 {
f16897cb
AL
2681 /* INC/DEC set the flags, but adds/subs do not. So if we
2682 need flags, use the former and not the latter. */
27c1afd5 2683 case 0x00000001:
f16897cb
AL
2684 if (need_flags)
2685 return "inc.l\t#1,%S0";
2686 else
2687 return "adds\t%2,%S0";
27c1afd5 2688 case 0x00000002:
f16897cb
AL
2689 if (need_flags)
2690 return "inc.l\t#2,%S0";
2691 else
2692 return "adds\t%2,%S0";
27c1afd5 2693 case 0xffffffff:
f16897cb
AL
2694 if (need_flags)
2695 return "dec.l\t#1,%S0";
2696 else
2697 return "subs\t%G2,%S0";
27c1afd5 2698 case 0xfffffffe:
f16897cb
AL
2699 if (need_flags)
2700 return "dec.l\t#2,%S0";
2701 else
2702 return "subs\t%G2,%S0";
2703
2704 /* These six cases have optimized paths when we do not
2705 need flags. Otherwise we let them fallthru. */
2706 case 0x00000004:
2707 if (!need_flags)
2708 return "adds\t%2,%S0";
2709
12bb62fb
JL
2710 /* FALLTHRU */
2711
27c1afd5 2712 case 0xfffffffc:
f16897cb
AL
2713 if (!need_flags)
2714 return "subs\t%G2,%S0";
27c1afd5 2715
12bb62fb
JL
2716 /* FALLTHRU */
2717
27c1afd5
JL
2718 case 0x00010000:
2719 case 0x00020000:
f16897cb
AL
2720 if (!need_flags)
2721 {
2722 operands[2] = GEN_INT (intval >> 16);
2723 return "inc.w\t%2,%e0";
2724 }
27c1afd5 2725
12bb62fb
JL
2726 /* FALLTHRU */
2727
27c1afd5
JL
2728 case 0xffff0000:
2729 case 0xfffe0000:
f16897cb
AL
2730 if (!need_flags)
2731 {
2732 operands[2] = GEN_INT (intval >> 16);
2733 return "dec.w\t%G2,%e0";
2734 }
12bb62fb
JL
2735
2736 /* FALLTHRU */
2737
7d6ac401
KH
2738 }
2739
27c1afd5
JL
2740 /* See if we can finish with 4 bytes. */
2741 if ((intval & 0xffff) == 0)
beed8fc0 2742 {
27c1afd5
JL
2743 operands[2] = GEN_INT (intval >> 16);
2744 return "add.w\t%2,%e0";
beed8fc0 2745 }
7d6ac401 2746 }
27c1afd5
JL
2747
2748 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
2749 {
2750 operands[2] = GEN_INT (-INTVAL (operands[2]));
2751 return "sub.l\t%S2,%S0";
2752 }
2753 return "add.l\t%S2,%S0";
7d6ac401
KH
2754}
2755
beed8fc0
AO
2756/* ??? It would be much easier to add the h8sx stuff if a single function
2757 classified the addition as either inc/dec, adds/subs, add.w or add.l. */
e9eba255
KH
2758/* Compute the length of an addition insn. */
2759
7d6ac401 2760unsigned int
f16897cb 2761compute_plussi_length (rtx *operands, bool need_flags)
7d6ac401 2762{
ef4bddc2 2763 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2764
8c440872 2765 gcc_assert (mode == SImode);
7d6ac401 2766
27c1afd5
JL
2767 if (GET_CODE (operands[2]) == CONST_INT
2768 && register_operand (operands[1], VOIDmode))
7d6ac401 2769 {
27c1afd5 2770 HOST_WIDE_INT intval = INTVAL (operands[2]);
cfedf91b 2771
27c1afd5
JL
2772 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
2773 return 2;
2774 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
2775 return 2;
cfedf91b 2776
27c1afd5 2777 /* See if we can finish with 2 bytes. */
cfedf91b 2778
27c1afd5 2779 switch ((unsigned int) intval & 0xffffffff)
7d6ac401 2780 {
27c1afd5
JL
2781 case 0x00000001:
2782 case 0x00000002:
27c1afd5 2783 return 2;
f16897cb
AL
2784 case 0x00000004:
2785 if (need_flags)
2786 return 6;
2787 else
2788 return 2;
27c1afd5
JL
2789
2790 case 0xffffffff:
2791 case 0xfffffffe:
27c1afd5 2792 return 2;
f16897cb
AL
2793 case 0xfffffffc:
2794 if (need_flags)
2795 return 6;
2796 else
2797 return 2;
27c1afd5
JL
2798
2799 case 0x00010000:
2800 case 0x00020000:
f16897cb
AL
2801 if (!need_flags)
2802 return 2;
27c1afd5 2803
12bb62fb
JL
2804 /* FALLTHRU */
2805
27c1afd5
JL
2806 case 0xffff0000:
2807 case 0xfffe0000:
f16897cb
AL
2808 if (!need_flags)
2809 return 2;
12bb62fb
JL
2810
2811 /* FALLTHRU */
2812
7d6ac401
KH
2813 }
2814
27c1afd5
JL
2815 /* See if we can finish with 4 bytes. */
2816 if ((intval & 0xffff) == 0)
2817 return 4;
7d6ac401 2818 }
27c1afd5
JL
2819
2820 if (GET_CODE (operands[2]) == CONST_INT && INTVAL (operands[2]) < 0)
2821 return h8300_length_from_table (operands[0],
2822 GEN_INT (-INTVAL (operands[2])),
2823 &addl_length_table);
2824 else
2825 return h8300_length_from_table (operands[0], operands[2],
2826 &addl_length_table);
7d6ac401
KH
2827}
2828
e9eba255
KH
2829/* Compute which flag bits are valid after an addition insn. */
2830
5ddb6eca 2831enum attr_old_cc
cb713a8d 2832compute_plussi_cc (rtx *operands)
7d6ac401 2833{
ef4bddc2 2834 machine_mode mode = GET_MODE (operands[0]);
7d6ac401 2835
8c440872 2836 gcc_assert (mode == SImode);
7d6ac401 2837
27c1afd5
JL
2838 if (GET_CODE (operands[2]) == CONST_INT
2839 && register_operand (operands[1], VOIDmode))
7d6ac401 2840 {
27c1afd5 2841 HOST_WIDE_INT intval = INTVAL (operands[2]);
7d6ac401 2842
27c1afd5 2843 if (TARGET_H8300SX && (intval >= 1 && intval <= 7))
5ddb6eca 2844 return OLD_CC_SET_ZN;
27c1afd5 2845 if (TARGET_H8300SX && (intval >= -7 && intval <= -1))
5ddb6eca 2846 return OLD_CC_SET_ZN;
beed8fc0 2847
27c1afd5 2848 /* See if we can finish with 2 bytes. */
7d6ac401 2849
27c1afd5
JL
2850 switch ((unsigned int) intval & 0xffffffff)
2851 {
2852 case 0x00000001:
2853 case 0x00000002:
2854 case 0x00000004:
5ddb6eca 2855 return OLD_CC_NONE_0HIT;
27c1afd5
JL
2856
2857 case 0xffffffff:
2858 case 0xfffffffe:
2859 case 0xfffffffc:
5ddb6eca 2860 return OLD_CC_NONE_0HIT;
27c1afd5
JL
2861
2862 case 0x00010000:
2863 case 0x00020000:
5ddb6eca 2864 return OLD_CC_CLOBBER;
27c1afd5
JL
2865
2866 case 0xffff0000:
2867 case 0xfffe0000:
5ddb6eca 2868 return OLD_CC_CLOBBER;
7d6ac401
KH
2869 }
2870
27c1afd5
JL
2871 /* See if we can finish with 4 bytes. */
2872 if ((intval & 0xffff) == 0)
5ddb6eca 2873 return OLD_CC_CLOBBER;
7d6ac401 2874 }
27c1afd5 2875
5ddb6eca 2876 return OLD_CC_SET_ZN;
7d6ac401
KH
2877}
2878\f
e9eba255
KH
2879/* Output a logical insn. */
2880
7d6ac401 2881const char *
4ea5fe8b 2882output_logical_op (machine_mode mode, rtx_code code, rtx *operands)
366a7b27
KH
2883{
2884 /* Pretend that every byte is affected if both operands are registers. */
7798db98 2885 const unsigned HOST_WIDE_INT intval =
366a7b27 2886 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
2887 /* Always use the full instruction if the
2888 first operand is in memory. It is better
2889 to use define_splits to generate the shorter
2890 sequence where valid. */
2891 && register_operand (operands[1], VOIDmode)
366a7b27
KH
2892 ? INTVAL (operands[2]) : 0x55555555);
2893 /* The determinant of the algorithm. If we perform an AND, 0
2894 affects a bit. Otherwise, 1 affects a bit. */
7798db98 2895 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
2896 /* Break up DET into pieces. */
2897 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
2898 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
2899 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
2900 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
2901 int lower_half_easy_p = 0;
2902 int upper_half_easy_p = 0;
366a7b27
KH
2903 /* The name of an insn. */
2904 const char *opname;
2905 char insn_buf[100];
2906
2907 switch (code)
2908 {
2909 case AND:
2910 opname = "and";
2911 break;
2912 case IOR:
2913 opname = "or";
2914 break;
2915 case XOR:
2916 opname = "xor";
2917 break;
2918 default:
8c440872 2919 gcc_unreachable ();
366a7b27
KH
2920 }
2921
2922 switch (mode)
2923 {
4ea5fe8b
JL
2924 case E_QImode:
2925 sprintf (insn_buf, "%s.b\t%%X2,%%X0", opname);
2926 output_asm_insn (insn_buf, operands);
2927 break;
4e10a5a7 2928 case E_HImode:
366a7b27 2929 /* First, see if we can finish with one insn. */
27c1afd5 2930 if (b0 != 0 && b1 != 0)
366a7b27
KH
2931 {
2932 sprintf (insn_buf, "%s.w\t%%T2,%%T0", opname);
2933 output_asm_insn (insn_buf, operands);
2934 }
2935 else
2936 {
2937 /* Take care of the lower byte. */
c5e7ce43 2938 if (b0 != 0)
366a7b27
KH
2939 {
2940 sprintf (insn_buf, "%s\t%%s2,%%s0", opname);
2941 output_asm_insn (insn_buf, operands);
2942 }
2943 /* Take care of the upper byte. */
c5e7ce43 2944 if (b1 != 0)
366a7b27
KH
2945 {
2946 sprintf (insn_buf, "%s\t%%t2,%%t0", opname);
2947 output_asm_insn (insn_buf, operands);
2948 }
2949 }
2950 break;
4e10a5a7 2951 case E_SImode:
27c1afd5
JL
2952 /* Determine if the lower half can be taken care of in no more
2953 than two bytes. */
2954 lower_half_easy_p = (b0 == 0
2955 || b1 == 0
2956 || (code != IOR && w0 == 0xffff));
2957
2958 /* Determine if the upper half can be taken care of in no more
2959 than two bytes. */
2960 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
2961 || (code == AND && w1 == 0xff00));
366a7b27 2962
f9ac2f95
KH
2963 /* Check if doing everything with one insn is no worse than
2964 using multiple insns. */
27c1afd5 2965 if (w0 != 0 && w1 != 0
472f2723
KH
2966 && !(lower_half_easy_p && upper_half_easy_p)
2967 && !(code == IOR && w1 == 0xffff
2968 && (w0 & 0x8000) != 0 && lower_half_easy_p))
366a7b27
KH
2969 {
2970 sprintf (insn_buf, "%s.l\t%%S2,%%S0", opname);
2971 output_asm_insn (insn_buf, operands);
2972 }
2973 else
2974 {
2975 /* Take care of the lower and upper words individually. For
2976 each word, we try different methods in the order of
2977
2978 1) the special insn (in case of AND or XOR),
2979 2) the word-wise insn, and
2980 3) The byte-wise insn. */
27c1afd5 2981 if (w0 == 0xffff && (code != IOR))
366a7b27 2982 output_asm_insn ((code == AND)
187462ac 2983 ? "sub.w\t%f0,%f0" : "not.w\t%f0",
366a7b27 2984 operands);
27c1afd5 2985 else if ((b0 != 0) && (b1 != 0))
366a7b27
KH
2986 {
2987 sprintf (insn_buf, "%s.w\t%%f2,%%f0", opname);
2988 output_asm_insn (insn_buf, operands);
2989 }
2990 else
2991 {
c5e7ce43 2992 if (b0 != 0)
366a7b27
KH
2993 {
2994 sprintf (insn_buf, "%s\t%%w2,%%w0", opname);
2995 output_asm_insn (insn_buf, operands);
2996 }
c5e7ce43 2997 if (b1 != 0)
366a7b27
KH
2998 {
2999 sprintf (insn_buf, "%s\t%%x2,%%x0", opname);
3000 output_asm_insn (insn_buf, operands);
3001 }
3002 }
3003
27c1afd5 3004 if ((w1 == 0xffff) && (code != IOR))
366a7b27 3005 output_asm_insn ((code == AND)
187462ac 3006 ? "sub.w\t%e0,%e0" : "not.w\t%e0",
366a7b27 3007 operands);
27c1afd5 3008 else if (code == IOR
472f2723
KH
3009 && w1 == 0xffff
3010 && (w0 & 0x8000) != 0)
3011 {
3012 output_asm_insn ("exts.l\t%S0", operands);
3013 }
27c1afd5 3014 else if (code == AND
c5e7ce43 3015 && w1 == 0xff00)
a6e8d113 3016 {
dc5f17ec 3017 output_asm_insn ("extu.w\t%e0", operands);
a6e8d113 3018 }
27c1afd5 3019 else
366a7b27 3020 {
c5e7ce43 3021 if (w1 != 0)
366a7b27
KH
3022 {
3023 sprintf (insn_buf, "%s.w\t%%e2,%%e0", opname);
3024 output_asm_insn (insn_buf, operands);
3025 }
3026 }
366a7b27
KH
3027 }
3028 break;
3029 default:
8c440872 3030 gcc_unreachable ();
366a7b27
KH
3031 }
3032 return "";
3033}
40367e2d 3034
e9eba255
KH
3035/* Compute the length of a logical insn. */
3036
40367e2d 3037unsigned int
4ea5fe8b 3038compute_logical_op_length (machine_mode mode, rtx_code code, rtx *operands)
40367e2d
KH
3039{
3040 /* Pretend that every byte is affected if both operands are registers. */
7798db98 3041 const unsigned HOST_WIDE_INT intval =
40367e2d 3042 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
3043 /* Always use the full instruction if the
3044 first operand is in memory. It is better
3045 to use define_splits to generate the shorter
3046 sequence where valid. */
3047 && register_operand (operands[1], VOIDmode)
40367e2d
KH
3048 ? INTVAL (operands[2]) : 0x55555555);
3049 /* The determinant of the algorithm. If we perform an AND, 0
3050 affects a bit. Otherwise, 1 affects a bit. */
7798db98 3051 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
3052 /* Break up DET into pieces. */
3053 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3054 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3055 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3056 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3057 int lower_half_easy_p = 0;
3058 int upper_half_easy_p = 0;
40367e2d
KH
3059 /* Insn length. */
3060 unsigned int length = 0;
3061
3062 switch (mode)
3063 {
4ea5fe8b
JL
3064 case E_QImode:
3065 return 2;
3066
4e10a5a7 3067 case E_HImode:
40367e2d 3068 /* First, see if we can finish with one insn. */
27c1afd5 3069 if (b0 != 0 && b1 != 0)
40367e2d 3070 {
beed8fc0
AO
3071 length = h8300_length_from_table (operands[1], operands[2],
3072 &logicw_length_table);
40367e2d
KH
3073 }
3074 else
3075 {
3076 /* Take care of the lower byte. */
c5e7ce43 3077 if (b0 != 0)
40367e2d
KH
3078 length += 2;
3079
3080 /* Take care of the upper byte. */
c5e7ce43 3081 if (b1 != 0)
40367e2d
KH
3082 length += 2;
3083 }
3084 break;
4e10a5a7 3085 case E_SImode:
27c1afd5
JL
3086 /* Determine if the lower half can be taken care of in no more
3087 than two bytes. */
3088 lower_half_easy_p = (b0 == 0
3089 || b1 == 0
3090 || (code != IOR && w0 == 0xffff));
3091
3092 /* Determine if the upper half can be taken care of in no more
3093 than two bytes. */
3094 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3095 || (code == AND && w1 == 0xff00));
40367e2d 3096
f9ac2f95
KH
3097 /* Check if doing everything with one insn is no worse than
3098 using multiple insns. */
27c1afd5 3099 if (w0 != 0 && w1 != 0
472f2723
KH
3100 && !(lower_half_easy_p && upper_half_easy_p)
3101 && !(code == IOR && w1 == 0xffff
3102 && (w0 & 0x8000) != 0 && lower_half_easy_p))
40367e2d 3103 {
beed8fc0
AO
3104 length = h8300_length_from_table (operands[1], operands[2],
3105 &logicl_length_table);
40367e2d
KH
3106 }
3107 else
3108 {
3109 /* Take care of the lower and upper words individually. For
3110 each word, we try different methods in the order of
3111
3112 1) the special insn (in case of AND or XOR),
3113 2) the word-wise insn, and
3114 3) The byte-wise insn. */
27c1afd5 3115 if (w0 == 0xffff && (code != IOR))
40367e2d
KH
3116 {
3117 length += 2;
3118 }
27c1afd5 3119 else if ((b0 != 0) && (b1 != 0))
40367e2d
KH
3120 {
3121 length += 4;
3122 }
3123 else
3124 {
c5e7ce43 3125 if (b0 != 0)
40367e2d
KH
3126 length += 2;
3127
c5e7ce43 3128 if (b1 != 0)
40367e2d
KH
3129 length += 2;
3130 }
3131
27c1afd5 3132 if (w1 == 0xffff && (code != IOR))
40367e2d
KH
3133 {
3134 length += 2;
3135 }
27c1afd5 3136 else if (code == IOR
472f2723
KH
3137 && w1 == 0xffff
3138 && (w0 & 0x8000) != 0)
3139 {
3140 length += 2;
3141 }
27c1afd5 3142 else if (code == AND && w1 == 0xff00)
a6e8d113
KH
3143 {
3144 length += 2;
3145 }
27c1afd5 3146 else
40367e2d 3147 {
c5e7ce43 3148 if (w1 != 0)
40367e2d
KH
3149 length += 4;
3150 }
40367e2d
KH
3151 }
3152 break;
3153 default:
8c440872 3154 gcc_unreachable ();
40367e2d
KH
3155 }
3156 return length;
3157}
b42cff6b 3158
e9eba255
KH
3159/* Compute which flag bits are valid after a logical insn. */
3160
f16897cb 3161int
ef4bddc2 3162compute_logical_op_cc (machine_mode mode, rtx *operands)
b42cff6b
KH
3163{
3164 /* Figure out the logical op that we need to perform. */
3165 enum rtx_code code = GET_CODE (operands[3]);
3166 /* Pretend that every byte is affected if both operands are registers. */
7798db98 3167 const unsigned HOST_WIDE_INT intval =
b42cff6b 3168 (unsigned HOST_WIDE_INT) ((GET_CODE (operands[2]) == CONST_INT)
beed8fc0
AO
3169 /* Always use the full instruction if the
3170 first operand is in memory. It is better
3171 to use define_splits to generate the shorter
3172 sequence where valid. */
3173 && register_operand (operands[1], VOIDmode)
b42cff6b
KH
3174 ? INTVAL (operands[2]) : 0x55555555);
3175 /* The determinant of the algorithm. If we perform an AND, 0
3176 affects a bit. Otherwise, 1 affects a bit. */
7798db98 3177 const unsigned HOST_WIDE_INT det = (code != AND) ? intval : ~intval;
f9ac2f95
KH
3178 /* Break up DET into pieces. */
3179 const unsigned HOST_WIDE_INT b0 = (det >> 0) & 0xff;
3180 const unsigned HOST_WIDE_INT b1 = (det >> 8) & 0xff;
3181 const unsigned HOST_WIDE_INT w0 = (det >> 0) & 0xffff;
3182 const unsigned HOST_WIDE_INT w1 = (det >> 16) & 0xffff;
3183 int lower_half_easy_p = 0;
3184 int upper_half_easy_p = 0;
b42cff6b 3185 /* Condition code. */
5ddb6eca 3186 enum attr_old_cc cc = OLD_CC_CLOBBER;
b42cff6b
KH
3187
3188 switch (mode)
3189 {
4e10a5a7 3190 case E_HImode:
b42cff6b 3191 /* First, see if we can finish with one insn. */
27c1afd5 3192 if (b0 != 0 && b1 != 0)
b42cff6b 3193 {
5ddb6eca 3194 cc = OLD_CC_SET_ZNV;
b42cff6b
KH
3195 }
3196 break;
4e10a5a7 3197 case E_SImode:
27c1afd5
JL
3198 /* Determine if the lower half can be taken care of in no more
3199 than two bytes. */
3200 lower_half_easy_p = (b0 == 0
3201 || b1 == 0
3202 || (code != IOR && w0 == 0xffff));
3203
3204 /* Determine if the upper half can be taken care of in no more
3205 than two bytes. */
3206 upper_half_easy_p = ((code != IOR && w1 == 0xffff)
3207 || (code == AND && w1 == 0xff00));
b42cff6b 3208
f9ac2f95
KH
3209 /* Check if doing everything with one insn is no worse than
3210 using multiple insns. */
27c1afd5 3211 if (w0 != 0 && w1 != 0
472f2723
KH
3212 && !(lower_half_easy_p && upper_half_easy_p)
3213 && !(code == IOR && w1 == 0xffff
3214 && (w0 & 0x8000) != 0 && lower_half_easy_p))
b42cff6b 3215 {
5ddb6eca 3216 cc = OLD_CC_SET_ZNV;
b42cff6b 3217 }
472f2723
KH
3218 else
3219 {
27c1afd5 3220 if (code == IOR
472f2723
KH
3221 && w1 == 0xffff
3222 && (w0 & 0x8000) != 0)
3223 {
5ddb6eca 3224 cc = OLD_CC_SET_ZNV;
472f2723
KH
3225 }
3226 }
b42cff6b
KH
3227 break;
3228 default:
8c440872 3229 gcc_unreachable ();
b42cff6b
KH
3230 }
3231 return cc;
3232}
366a7b27 3233\f
f16897cb 3234#if 0
8981ecd3
KH
3235/* Expand a conditional branch. */
3236
3237void
f90b7a5a 3238h8300_expand_branch (rtx operands[])
8981ecd3 3239{
f90b7a5a
PB
3240 enum rtx_code code = GET_CODE (operands[0]);
3241 rtx op0 = operands[1];
3242 rtx op1 = operands[2];
3243 rtx label = operands[3];
8981ecd3
KH
3244 rtx tmp;
3245
f90b7a5a 3246 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
f7df4a84 3247 emit_insn (gen_rtx_SET (cc0_rtx, tmp));
f90b7a5a 3248
8981ecd3
KH
3249 tmp = gen_rtx_fmt_ee (code, VOIDmode, cc0_rtx, const0_rtx);
3250 tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
3251 gen_rtx_LABEL_REF (VOIDmode, label),
3252 pc_rtx);
f7df4a84 3253 emit_jump_insn (gen_rtx_SET (pc_rtx, tmp));
8981ecd3 3254}
f90b7a5a
PB
3255
3256
3257/* Expand a conditional store. */
3258
3259void
3260h8300_expand_store (rtx operands[])
3261{
3262 rtx dest = operands[0];
3263 enum rtx_code code = GET_CODE (operands[1]);
3264 rtx op0 = operands[2];
3265 rtx op1 = operands[3];
3266 rtx tmp;
3267
3268 tmp = gen_rtx_COMPARE (VOIDmode, op0, op1);
f7df4a84 3269 emit_insn (gen_rtx_SET (cc0_rtx, tmp));
f90b7a5a
PB
3270
3271 tmp = gen_rtx_fmt_ee (code, GET_MODE (dest), cc0_rtx, const0_rtx);
f7df4a84 3272 emit_insn (gen_rtx_SET (dest, tmp));
f90b7a5a 3273}
f16897cb 3274#endif
8981ecd3 3275\f
48837e29
DE
3276/* Shifts.
3277
005e3e05
KH
3278 We devote a fair bit of code to getting efficient shifts since we
3279 can only shift one bit at a time on the H8/300 and H8/300H and only
3db11b5c 3280 one or two bits at a time on the H8S.
005e3e05
KH
3281
3282 All shift code falls into one of the following ways of
3283 implementation:
3284
3285 o SHIFT_INLINE: Emit straight line code for the shift; this is used
3286 when a straight line shift is about the same size or smaller than
3287 a loop.
3288
3289 o SHIFT_ROT_AND: Rotate the value the opposite direction, then mask
3290 off the bits we don't need. This is used when only a few of the
3291 bits in the original value will survive in the shifted value.
3292
3293 o SHIFT_SPECIAL: Often it's possible to move a byte or a word to
3294 simulate a shift by 8, 16, or 24 bits. Once moved, a few inline
3295 shifts can be added if the shift count is slightly more than 8 or
3296 16. This case also includes other oddballs that are not worth
f411c849 3297 explaining here.
005e3e05 3298
3db11b5c 3299 o SHIFT_LOOP: Emit a loop using one (or two on H8S) bit shifts.
005e3e05 3300
5ec0b66e
KH
3301 For each shift count, we try to use code that has no trade-off
3302 between code size and speed whenever possible.
3303
3304 If the trade-off is unavoidable, we try to be reasonable.
3305 Specifically, the fastest version is one instruction longer than
3306 the shortest version, we take the fastest version. We also provide
3307 the use a way to switch back to the shortest version with -Os.
3308
3309 For the details of the shift algorithms for various shift counts,
3310 refer to shift_alg_[qhs]i. */
07aae5c2 3311
beed8fc0
AO
3312/* Classify a shift with the given mode and code. OP is the shift amount. */
3313
3314enum h8sx_shift_type
ef4bddc2 3315h8sx_classify_shift (machine_mode mode, enum rtx_code code, rtx op)
beed8fc0
AO
3316{
3317 if (!TARGET_H8300SX)
3318 return H8SX_SHIFT_NONE;
3319
3320 switch (code)
3321 {
3322 case ASHIFT:
3323 case LSHIFTRT:
3324 /* Check for variable shifts (shll Rs,Rd and shlr Rs,Rd). */
3325 if (GET_CODE (op) != CONST_INT)
3326 return H8SX_SHIFT_BINARY;
3327
3328 /* Reject out-of-range shift amounts. */
3329 if (INTVAL (op) <= 0 || INTVAL (op) >= GET_MODE_BITSIZE (mode))
3330 return H8SX_SHIFT_NONE;
3331
3332 /* Power-of-2 shifts are effectively unary operations. */
3333 if (exact_log2 (INTVAL (op)) >= 0)
3334 return H8SX_SHIFT_UNARY;
3335
3336 return H8SX_SHIFT_BINARY;
3337
3338 case ASHIFTRT:
3339 if (op == const1_rtx || op == const2_rtx)
3340 return H8SX_SHIFT_UNARY;
3341 return H8SX_SHIFT_NONE;
3342
3343 case ROTATE:
3344 if (GET_CODE (op) == CONST_INT
3345 && (INTVAL (op) == 1
3346 || INTVAL (op) == 2
3347 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 2
3348 || INTVAL (op) == GET_MODE_BITSIZE (mode) - 1))
3349 return H8SX_SHIFT_UNARY;
3350 return H8SX_SHIFT_NONE;
3351
3352 default:
3353 return H8SX_SHIFT_NONE;
3354 }
3355}
3356
beed8fc0
AO
3357/* Return the asm template for a single h8sx shift instruction.
3358 OPERANDS[0] and OPERANDS[1] are the destination, OPERANDS[2]
3359 is the source and OPERANDS[3] is the shift. SUFFIX is the
88cb339e 3360 size suffix ('b', 'w' or 'l') and OPTYPE is the h8300_print_operand
beed8fc0
AO
3361 prefix for the destination operand. */
3362
3363const char *
3364output_h8sx_shift (rtx *operands, int suffix, int optype)
3365{
3366 static char buffer[16];
3367 const char *stem;
3368
3369 switch (GET_CODE (operands[3]))
3370 {
3371 case ASHIFT:
3372 stem = "shll";
3373 break;
3374
3375 case ASHIFTRT:
3376 stem = "shar";
3377 break;
3378
3379 case LSHIFTRT:
3380 stem = "shlr";
3381 break;
3382
3383 case ROTATE:
3384 stem = "rotl";
3385 if (INTVAL (operands[2]) > 2)
3386 {
3387 /* This is really a right rotate. */
3388 operands[2] = GEN_INT (GET_MODE_BITSIZE (GET_MODE (operands[0]))
3389 - INTVAL (operands[2]));
3390 stem = "rotr";
3391 }
3392 break;
3393
3394 default:
8c440872 3395 gcc_unreachable ();
beed8fc0
AO
3396 }
3397 if (operands[2] == const1_rtx)
3398 sprintf (buffer, "%s.%c\t%%%c0", stem, suffix, optype);
3399 else
3400 sprintf (buffer, "%s.%c\t%%X2,%%%c0", stem, suffix, optype);
3401 return buffer;
3402}
48837e29 3403
317d21e9 3404/* Emit code to do shifts. */
48837e29 3405
beed8fc0 3406bool
ef4bddc2 3407expand_a_shift (machine_mode mode, enum rtx_code code, rtx operands[])
07aae5c2 3408{
beed8fc0
AO
3409 switch (h8sx_classify_shift (mode, code, operands[2]))
3410 {
3411 case H8SX_SHIFT_BINARY:
3412 operands[1] = force_reg (mode, operands[1]);
3413 return false;
3414
3415 case H8SX_SHIFT_UNARY:
3416 return false;
3417
3418 case H8SX_SHIFT_NONE:
3419 break;
3420 }
3421
07e4d94e
KH
3422 /* Need a loop to get all the bits we want - we generate the
3423 code at emit time, but need to allocate a scratch reg now. */
70033749
JL
3424 emit_move_insn (copy_rtx (operands[0]), operands[1]);
3425 if (operands[2] == CONST0_RTX (QImode))
3426 ;
3427 else if (GET_CODE (operands[2]) == CONST_INT
3428 && !h8300_shift_needs_scratch_p (INTVAL (operands[2]), mode, code))
3429 emit_insn (gen_rtx_SET (copy_rtx (operands[0]),
3430 gen_rtx_fmt_ee (code, mode,
3431 copy_rtx (operands[1]), operands[2])));
3432 else
3433 emit_insn (gen_rtx_PARALLEL
3434 (VOIDmode,
3435 gen_rtvec (2,
3436 gen_rtx_SET (copy_rtx (operands[0]),
3437 gen_rtx_fmt_ee (code, mode,
3438 copy_rtx (operands[0]), operands[2])),
3439 gen_rtx_CLOBBER (VOIDmode,
3440 gen_rtx_SCRATCH (QImode)))));
beed8fc0 3441 return true;
48837e29
DE
3442}
3443
48837e29
DE
3444/* Symbols of the various modes which can be used as indices. */
3445
3446enum shift_mode
1a63219b
KH
3447{
3448 QIshift, HIshift, SIshift
3449};
48837e29 3450
269c14e1 3451/* For single bit shift insns, record assembler and what bits of the
5ddb6eca 3452 condition code are valid afterwards (represented as various OLD_CC_FOO
269c14e1 3453 bits, 0 means CC isn't left in a usable state). */
48837e29
DE
3454
3455struct shift_insn
3456{
8b60264b 3457 const char *const assembler;
5ddb6eca 3458 const enum attr_old_cc cc_valid;
48837e29
DE
3459};
3460
3461/* Assembler instruction shift table.
3462
3463 These tables are used to look up the basic shifts.
07e4d94e 3464 They are indexed by cpu, shift_type, and mode. */
07aae5c2 3465
48837e29
DE
3466static const struct shift_insn shift_one[2][3][3] =
3467{
3468/* H8/300 */
3469 {
3470/* SHIFT_ASHIFT */
3471 {
5ddb6eca
JL
3472 { "shll\t%X0", OLD_CC_SET_ZNV },
3473 { "add.w\t%T0,%T0", OLD_CC_SET_ZN },
3474 { "add.w\t%f0,%f0\n\taddx\t%y0,%y0\n\taddx\t%z0,%z0", OLD_CC_CLOBBER }
48837e29
DE
3475 },
3476/* SHIFT_LSHIFTRT */
3477 {
5ddb6eca
JL
3478 { "shlr\t%X0", OLD_CC_SET_ZNV },
3479 { "shlr\t%t0\n\trotxr\t%s0", OLD_CC_CLOBBER },
3480 { "shlr\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", OLD_CC_CLOBBER }
48837e29
DE
3481 },
3482/* SHIFT_ASHIFTRT */
3483 {
5ddb6eca
JL
3484 { "shar\t%X0", OLD_CC_SET_ZNV },
3485 { "shar\t%t0\n\trotxr\t%s0", OLD_CC_CLOBBER },
3486 { "shar\t%z0\n\trotxr\t%y0\n\trotxr\t%x0\n\trotxr\t%w0", OLD_CC_CLOBBER }
48837e29
DE
3487 }
3488 },
3489/* H8/300H */
3490 {
3491/* SHIFT_ASHIFT */
3492 {
5ddb6eca
JL
3493 { "shll.b\t%X0", OLD_CC_SET_ZNV },
3494 { "shll.w\t%T0", OLD_CC_SET_ZNV },
3495 { "shll.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3496 },
3497/* SHIFT_LSHIFTRT */
3498 {
5ddb6eca
JL
3499 { "shlr.b\t%X0", OLD_CC_SET_ZNV },
3500 { "shlr.w\t%T0", OLD_CC_SET_ZNV },
3501 { "shlr.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3502 },
3503/* SHIFT_ASHIFTRT */
3504 {
5ddb6eca
JL
3505 { "shar.b\t%X0", OLD_CC_SET_ZNV },
3506 { "shar.w\t%T0", OLD_CC_SET_ZNV },
3507 { "shar.l\t%S0", OLD_CC_SET_ZNV }
48837e29
DE
3508 }
3509 }
3510};
07aae5c2 3511
51c0c1d7
JL
3512static const struct shift_insn shift_two[3][3] =
3513{
3514/* SHIFT_ASHIFT */
3515 {
5ddb6eca
JL
3516 { "shll.b\t#2,%X0", OLD_CC_SET_ZNV },
3517 { "shll.w\t#2,%T0", OLD_CC_SET_ZNV },
3518 { "shll.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3519 },
3520/* SHIFT_LSHIFTRT */
3521 {
5ddb6eca
JL
3522 { "shlr.b\t#2,%X0", OLD_CC_SET_ZNV },
3523 { "shlr.w\t#2,%T0", OLD_CC_SET_ZNV },
3524 { "shlr.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3525 },
3526/* SHIFT_ASHIFTRT */
3527 {
5ddb6eca
JL
3528 { "shar.b\t#2,%X0", OLD_CC_SET_ZNV },
3529 { "shar.w\t#2,%T0", OLD_CC_SET_ZNV },
3530 { "shar.l\t#2,%S0", OLD_CC_SET_ZNV }
51c0c1d7
JL
3531 }
3532};
3533
48837e29
DE
3534/* Rotates are organized by which shift they'll be used in implementing.
3535 There's no need to record whether the cc is valid afterwards because
3536 it is the AND insn that will decide this. */
07aae5c2 3537
48837e29
DE
3538static const char *const rotate_one[2][3][3] =
3539{
3540/* H8/300 */
3541 {
3542/* SHIFT_ASHIFT */
3543 {
51c0c1d7
JL
3544 "rotr\t%X0",
3545 "shlr\t%t0\n\trotxr\t%s0\n\tbst\t#7,%t0",
48837e29
DE
3546 0
3547 },
3548/* SHIFT_LSHIFTRT */
3549 {
51c0c1d7
JL
3550 "rotl\t%X0",
3551 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
48837e29
DE
3552 0
3553 },
3554/* SHIFT_ASHIFTRT */
3555 {
51c0c1d7
JL
3556 "rotl\t%X0",
3557 "shll\t%s0\n\trotxl\t%t0\n\tbst\t#0,%s0",
48837e29 3558 0
07aae5c2 3559 }
48837e29
DE
3560 },
3561/* H8/300H */
3562 {
3563/* SHIFT_ASHIFT */
3564 {
51c0c1d7
JL
3565 "rotr.b\t%X0",
3566 "rotr.w\t%T0",
3567 "rotr.l\t%S0"
48837e29
DE
3568 },
3569/* SHIFT_LSHIFTRT */
07aae5c2 3570 {
51c0c1d7
JL
3571 "rotl.b\t%X0",
3572 "rotl.w\t%T0",
3573 "rotl.l\t%S0"
48837e29
DE
3574 },
3575/* SHIFT_ASHIFTRT */
3576 {
51c0c1d7
JL
3577 "rotl.b\t%X0",
3578 "rotl.w\t%T0",
3579 "rotl.l\t%S0"
48837e29
DE
3580 }
3581 }
3582};
3583
51c0c1d7
JL
3584static const char *const rotate_two[3][3] =
3585{
3586/* SHIFT_ASHIFT */
3587 {
3588 "rotr.b\t#2,%X0",
3589 "rotr.w\t#2,%T0",
3590 "rotr.l\t#2,%S0"
3591 },
3592/* SHIFT_LSHIFTRT */
3593 {
3594 "rotl.b\t#2,%X0",
3595 "rotl.w\t#2,%T0",
3596 "rotl.l\t#2,%S0"
3597 },
3598/* SHIFT_ASHIFTRT */
3599 {
3600 "rotl.b\t#2,%X0",
3601 "rotl.w\t#2,%T0",
3602 "rotl.l\t#2,%S0"
3603 }
3604};
3605
35fb3d1f
KH
3606struct shift_info {
3607 /* Shift algorithm. */
3608 enum shift_alg alg;
3609
3610 /* The number of bits to be shifted by shift1 and shift2. Valid
3611 when ALG is SHIFT_SPECIAL. */
3612 unsigned int remainder;
3613
3614 /* Special insn for a shift. Valid when ALG is SHIFT_SPECIAL. */
3615 const char *special;
3616
3617 /* Insn for a one-bit shift. Valid when ALG is either SHIFT_INLINE
9cd10576 3618 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
35fb3d1f
KH
3619 const char *shift1;
3620
3621 /* Insn for a two-bit shift. Valid when ALG is either SHIFT_INLINE
9cd10576 3622 or SHIFT_SPECIAL, and REMAINDER is nonzero. */
35fb3d1f
KH
3623 const char *shift2;
3624
45ca2106 3625 /* CC status for SHIFT_INLINE. */
5ddb6eca 3626 enum attr_old_cc cc_inline;
45ca2106
KH
3627
3628 /* CC status for SHIFT_SPECIAL. */
5ddb6eca 3629 enum attr_old_cc cc_special;
35fb3d1f
KH
3630};
3631
cb713a8d
KH
3632static void get_shift_alg (enum shift_type,
3633 enum shift_mode, unsigned int,
3634 struct shift_info *);
441d04c6 3635
c009a745
KH
3636/* Given SHIFT_TYPE, SHIFT_MODE, and shift count COUNT, determine the
3637 best algorithm for doing the shift. The assembler code is stored
5ec0b66e 3638 in the pointers in INFO. We achieve the maximum efficiency in most
27c1afd5 3639 cases.
5ec0b66e
KH
3640
3641 We first determine the strategy of the shift algorithm by a table
3642 lookup. If that tells us to use a hand crafted assembly code, we
3643 go into the big switch statement to find what that is. Otherwise,
3644 we resort to a generic way, such as inlining. In either case, the
3645 result is returned through INFO. */
48837e29 3646
cb33eb17 3647static void
cb713a8d
KH
3648get_shift_alg (enum shift_type shift_type, enum shift_mode shift_mode,
3649 unsigned int count, struct shift_info *info)
48837e29 3650{
b9b575e6 3651 enum h8_cpu cpu;
769828ab 3652
27c1afd5 3653 if (TARGET_H8300S)
b9b575e6 3654 cpu = H8_S;
2f6bd6eb
YS
3655 else
3656 cpu = H8_300H;
769828ab 3657
96eaf358 3658 /* Find the shift algorithm. */
b9b575e6 3659 info->alg = SHIFT_LOOP;
48837e29
DE
3660 switch (shift_mode)
3661 {
3662 case QIshift:
b9b575e6 3663 if (count < GET_MODE_BITSIZE (QImode))
96eaf358
KH
3664 info->alg = shift_alg_qi[cpu][shift_type][count];
3665 break;
769828ab 3666
96eaf358 3667 case HIshift:
b9b575e6 3668 if (count < GET_MODE_BITSIZE (HImode))
96eaf358
KH
3669 info->alg = shift_alg_hi[cpu][shift_type][count];
3670 break;
3671
3672 case SIshift:
b9b575e6 3673 if (count < GET_MODE_BITSIZE (SImode))
96eaf358
KH
3674 info->alg = shift_alg_si[cpu][shift_type][count];
3675 break;
3676
3677 default:
8c440872 3678 gcc_unreachable ();
96eaf358
KH
3679 }
3680
3681 /* Fill in INFO. Return unless we have SHIFT_SPECIAL. */
3682 switch (info->alg)
3683 {
3684 case SHIFT_INLINE:
3685 info->remainder = count;
3686 /* Fall through. */
3687
3688 case SHIFT_LOOP:
3689 /* It is up to the caller to know that looping clobbers cc. */
3690 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3691 info->shift2 = shift_two[shift_type][shift_mode].assembler;
45ca2106 3692 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
96eaf358
KH
3693 goto end;
3694
3695 case SHIFT_ROT_AND:
3696 info->shift1 = rotate_one[cpu_type][shift_type][shift_mode];
3697 info->shift2 = rotate_two[shift_type][shift_mode];
5ddb6eca 3698 info->cc_inline = OLD_CC_CLOBBER;
96eaf358
KH
3699 goto end;
3700
3701 case SHIFT_SPECIAL:
3702 /* REMAINDER is 0 for most cases, so initialize it to 0. */
3703 info->remainder = 0;
3704 info->shift1 = shift_one[cpu_type][shift_type][shift_mode].assembler;
3705 info->shift2 = shift_two[shift_type][shift_mode].assembler;
45ca2106 3706 info->cc_inline = shift_one[cpu_type][shift_type][shift_mode].cc_valid;
5ddb6eca 3707 info->cc_special = OLD_CC_CLOBBER;
96eaf358
KH
3708 break;
3709 }
51c0c1d7 3710
96eaf358
KH
3711 /* Here we only deal with SHIFT_SPECIAL. */
3712 switch (shift_mode)
3713 {
3714 case QIshift:
769828ab
KH
3715 /* For ASHIFTRT by 7 bits, the sign bit is simply replicated
3716 through the entire value. */
8c440872
NS
3717 gcc_assert (shift_type == SHIFT_ASHIFTRT && count == 7);
3718 info->special = "shll\t%X0\n\tsubx\t%X0,%X0";
3719 goto end;
769828ab
KH
3720
3721 case HIshift:
769828ab 3722 if (count == 7)
51c0c1d7 3723 {
a77b1dbc 3724 switch (shift_type)
51c0c1d7 3725 {
a77b1dbc 3726 case SHIFT_ASHIFT:
27c1afd5 3727 info->special = "shar.b\t%t0\n\tmov.b\t%s0,%t0\n\trotxr.w\t%T0\n\tand.b\t#0x80,%s0";
692b7eb3 3728 goto end;
a77b1dbc 3729 case SHIFT_LSHIFTRT:
27c1afd5 3730 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.w\t%T0\n\tand.b\t#0x01,%t0";
692b7eb3 3731 goto end;
a77b1dbc 3732 case SHIFT_ASHIFTRT:
35fb3d1f 3733 info->special = "shal.b\t%s0\n\tmov.b\t%t0,%s0\n\trotxl.b\t%s0\n\tsubx\t%t0,%t0";
692b7eb3 3734 goto end;
48837e29 3735 }
07aae5c2 3736 }
01512446 3737 else if ((count >= 8 && count <= 13)
a38b3eea 3738 || (TARGET_H8300S && count == 14))
07aae5c2 3739 {
a7812c0b
KH
3740 info->remainder = count - 8;
3741
51c0c1d7 3742 switch (shift_type)
48837e29 3743 {
51c0c1d7 3744 case SHIFT_ASHIFT:
35fb3d1f 3745 info->special = "mov.b\t%s0,%t0\n\tsub.b\t%s0,%s0";
692b7eb3 3746 goto end;
51c0c1d7 3747 case SHIFT_LSHIFTRT:
27c1afd5 3748 info->special = "mov.b\t%t0,%s0\n\textu.w\t%T0";
5ddb6eca 3749 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3750 goto end;
51c0c1d7 3751 case SHIFT_ASHIFTRT:
27c1afd5 3752 info->special = "mov.b\t%t0,%s0\n\texts.w\t%T0";
5ddb6eca 3753 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3754 goto end;
51c0c1d7
JL
3755 }
3756 }
5e98fba2
DD
3757 else if (count == 14)
3758 {
3759 switch (shift_type)
3760 {
3761 case SHIFT_ASHIFT:
5e98fba2 3762 case SHIFT_LSHIFTRT:
27c1afd5 3763 goto end;
5e98fba2
DD
3764 goto end;
3765 case SHIFT_ASHIFTRT:
27c1afd5 3766 if (TARGET_H8300H)
45ca2106
KH
3767 {
3768 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 3769 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3770 }
5e98fba2 3771 else /* TARGET_H8300S */
8c440872 3772 gcc_unreachable ();
5e98fba2
DD
3773 goto end;
3774 }
3775 }
1e41e866 3776 else if (count == 15)
51c0c1d7 3777 {
1e41e866
KH
3778 switch (shift_type)
3779 {
3780 case SHIFT_ASHIFT:
3781 info->special = "bld\t#0,%s0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#7,%t0";
3782 goto end;
3783 case SHIFT_LSHIFTRT:
3784 info->special = "bld\t#7,%t0\n\txor\t%s0,%s0\n\txor\t%t0,%t0\n\tbst\t#0,%s0";
3785 goto end;
3786 case SHIFT_ASHIFTRT:
3787 info->special = "shll\t%t0\n\tsubx\t%t0,%t0\n\tmov.b\t%t0,%s0";
3788 goto end;
3789 }
07aae5c2 3790 }
8c440872 3791 gcc_unreachable ();
51c0c1d7 3792
48837e29 3793 case SIshift:
27c1afd5 3794 if (count == 8)
e6bcfef9
JS
3795 {
3796 switch (shift_type)
3797 {
3798 case SHIFT_ASHIFT:
35fb3d1f 3799 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 3800 goto end;
e6bcfef9 3801 case SHIFT_LSHIFTRT:
35fb3d1f 3802 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 3803 goto end;
e6bcfef9 3804 case SHIFT_ASHIFTRT:
35fb3d1f 3805 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 3806 goto end;
e6bcfef9
JS
3807 }
3808 }
27c1afd5 3809 else if (count == 15)
dd69e230
KH
3810 {
3811 switch (shift_type)
3812 {
3813 case SHIFT_ASHIFT:
3814 info->special = "shlr.w\t%e0\n\tmov.w\t%f0,%e0\n\txor.w\t%f0,%f0\n\trotxr.l\t%S0";
5ddb6eca 3815 info->cc_special = OLD_CC_SET_ZNV;
dd69e230
KH
3816 goto end;
3817 case SHIFT_LSHIFTRT:
18cf8dda 3818 info->special = "shll.w\t%f0\n\tmov.w\t%e0,%f0\n\txor.w\t%e0,%e0\n\trotxl.l\t%S0";
5ddb6eca 3819 info->cc_special = OLD_CC_SET_ZNV;
dd69e230 3820 goto end;
aefc5826 3821 case SHIFT_ASHIFTRT:
8c440872 3822 gcc_unreachable ();
dd69e230
KH
3823 }
3824 }
27c1afd5 3825 else if ((TARGET_H8300H && count >= 16 && count <= 19)
01512446 3826 || (TARGET_H8300S && count >= 16 && count <= 21))
48837e29 3827 {
a7812c0b
KH
3828 info->remainder = count - 16;
3829
48837e29
DE
3830 switch (shift_type)
3831 {
3832 case SHIFT_ASHIFT:
35fb3d1f 3833 info->special = "mov.w\t%f0,%e0\n\tsub.w\t%f0,%f0";
692b7eb3 3834 goto end;
51c0c1d7 3835 case SHIFT_LSHIFTRT:
27c1afd5 3836 info->special = "mov.w\t%e0,%f0\n\textu.l\t%S0";
5ddb6eca 3837 info->cc_special = OLD_CC_SET_ZNV;
f9477efd
KH
3838 goto end;
3839 case SHIFT_ASHIFTRT:
27c1afd5 3840 info->special = "mov.w\t%e0,%f0\n\texts.l\t%S0";
5ddb6eca 3841 info->cc_special = OLD_CC_SET_ZNV;
7f473594
KH
3842 goto end;
3843 }
3844 }
4a4ae922 3845 else if ((TARGET_H8300H && count == 24)
01512446 3846 || (TARGET_H8300S && count >= 24 && count <= 25))
e6bcfef9 3847 {
4a4ae922
KH
3848 info->remainder = count - 24;
3849
e6bcfef9
JS
3850 switch (shift_type)
3851 {
3852 case SHIFT_ASHIFT:
35fb3d1f 3853 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 3854 goto end;
e6bcfef9 3855 case SHIFT_LSHIFTRT:
35fb3d1f 3856 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\textu.w\t%f0\n\textu.l\t%S0";
5ddb6eca 3857 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3858 goto end;
e6bcfef9 3859 case SHIFT_ASHIFTRT:
35fb3d1f 3860 info->special = "mov.w\t%e0,%f0\n\tmov.b\t%t0,%s0\n\texts.w\t%f0\n\texts.l\t%S0";
5ddb6eca 3861 info->cc_special = OLD_CC_SET_ZNV;
692b7eb3 3862 goto end;
e6bcfef9
JS
3863 }
3864 }
27c1afd5 3865 else if (count == 28)
1e5bdc40
KH
3866 {
3867 switch (shift_type)
3868 {
3869 case SHIFT_ASHIFT:
3870 if (TARGET_H8300H)
3871 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";
3872 else
3873 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
3874 goto end;
3875 case SHIFT_LSHIFTRT:
3876 if (TARGET_H8300H)
45ca2106
KH
3877 {
3878 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 3879 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3880 }
1e5bdc40 3881 else
a7612343 3882 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
3883 goto end;
3884 case SHIFT_ASHIFTRT:
8c440872 3885 gcc_unreachable ();
1e5bdc40
KH
3886 }
3887 }
27c1afd5 3888 else if (count == 29)
1e5bdc40
KH
3889 {
3890 switch (shift_type)
3891 {
3892 case SHIFT_ASHIFT:
3893 if (TARGET_H8300H)
3894 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";
3895 else
3896 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
3897 goto end;
3898 case SHIFT_LSHIFTRT:
3899 if (TARGET_H8300H)
45ca2106
KH
3900 {
3901 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 3902 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3903 }
1e5bdc40 3904 else
45ca2106
KH
3905 {
3906 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
5ddb6eca 3907 info->cc_special = OLD_CC_SET_ZNV;
45ca2106 3908 }
1e5bdc40
KH
3909 goto end;
3910 case SHIFT_ASHIFTRT:
8c440872 3911 gcc_unreachable ();
1e5bdc40
KH
3912 }
3913 }
27c1afd5 3914 else if (count == 30)
1e5bdc40
KH
3915 {
3916 switch (shift_type)
3917 {
3918 case SHIFT_ASHIFT:
3919 if (TARGET_H8300H)
3920 info->special = "sub.w\t%e0,%e0\n\trotr.l\t%S0\n\trotr.l\t%S0\n\tsub.w\t%f0,%f0";
3921 else
3922 info->special = "sub.w\t%e0,%e0\n\trotr.l\t#2,%S0\n\tsub.w\t%f0,%f0";
1e5bdc40
KH
3923 goto end;
3924 case SHIFT_LSHIFTRT:
3925 if (TARGET_H8300H)
a7612343 3926 info->special = "sub.w\t%f0,%f0\n\trotl.l\t%S0\n\trotl.l\t%S0\n\textu.l\t%S0";
1e5bdc40 3927 else
a7612343 3928 info->special = "sub.w\t%f0,%f0\n\trotl.l\t#2,%S0\n\textu.l\t%S0";
1e5bdc40
KH
3929 goto end;
3930 case SHIFT_ASHIFTRT:
8c440872 3931 gcc_unreachable ();
1e5bdc40
KH
3932 }
3933 }
48837e29
DE
3934 else if (count == 31)
3935 {
27c1afd5 3936 switch (shift_type)
48837e29 3937 {
27c1afd5
JL
3938 case SHIFT_ASHIFT:
3939 info->special = "shlr.l\t%S0\n\txor.l\t%S0,%S0\n\trotxr.l\t%S0";
5ddb6eca 3940 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5
JL
3941 goto end;
3942 case SHIFT_LSHIFTRT:
3943 info->special = "shll.l\t%S0\n\txor.l\t%S0,%S0\n\trotxl.l\t%S0";
5ddb6eca 3944 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5
JL
3945 goto end;
3946 case SHIFT_ASHIFTRT:
3947 info->special = "shll\t%e0\n\tsubx\t%w0,%w0\n\texts.w\t%T0\n\texts.l\t%S0";
5ddb6eca 3948 info->cc_special = OLD_CC_SET_ZNV;
27c1afd5 3949 goto end;
48837e29
DE
3950 }
3951 }
8c440872 3952 gcc_unreachable ();
51c0c1d7 3953
48837e29 3954 default:
8c440872 3955 gcc_unreachable ();
07aae5c2 3956 }
48837e29 3957
cb33eb17
KH
3958 end:
3959 if (!TARGET_H8300S)
3960 info->shift2 = NULL;
07aae5c2
SC
3961}
3962
be1e06df
KH
3963/* Given COUNT and MODE of a shift, return 1 if a scratch reg may be
3964 needed for some shift with COUNT and MODE. Return 0 otherwise. */
3965
3966int
70033749 3967h8300_shift_needs_scratch_p (int count, machine_mode mode, enum rtx_code type)
be1e06df 3968{
b9b575e6 3969 enum h8_cpu cpu;
be1e06df
KH
3970 int a, lr, ar;
3971
3972 if (GET_MODE_BITSIZE (mode) <= count)
3973 return 1;
3974
3975 /* Find out the target CPU. */
27c1afd5 3976 if (TARGET_H8300S)
b9b575e6 3977 cpu = H8_S;
2f6bd6eb
YS
3978 else
3979 cpu = H8_300H;
be1e06df
KH
3980
3981 /* Find the shift algorithm. */
3982 switch (mode)
3983 {
4e10a5a7 3984 case E_QImode:
be1e06df
KH
3985 a = shift_alg_qi[cpu][SHIFT_ASHIFT][count];
3986 lr = shift_alg_qi[cpu][SHIFT_LSHIFTRT][count];
3987 ar = shift_alg_qi[cpu][SHIFT_ASHIFTRT][count];
3988 break;
3989
4e10a5a7 3990 case E_HImode:
be1e06df
KH
3991 a = shift_alg_hi[cpu][SHIFT_ASHIFT][count];
3992 lr = shift_alg_hi[cpu][SHIFT_LSHIFTRT][count];
3993 ar = shift_alg_hi[cpu][SHIFT_ASHIFTRT][count];
3994 break;
3995
4e10a5a7 3996 case E_SImode:
be1e06df
KH
3997 a = shift_alg_si[cpu][SHIFT_ASHIFT][count];
3998 lr = shift_alg_si[cpu][SHIFT_LSHIFTRT][count];
3999 ar = shift_alg_si[cpu][SHIFT_ASHIFTRT][count];
4000 break;
4001
4002 default:
8c440872 4003 gcc_unreachable ();
be1e06df
KH
4004 }
4005
aa2fb4dd 4006 /* On H8/300H, count == 8 uses a scratch register. */
70033749
JL
4007 if (type == CLOBBER)
4008 return (a == SHIFT_LOOP || lr == SHIFT_LOOP || ar == SHIFT_LOOP
4009 || (TARGET_H8300H && mode == SImode && count == 8));
4010 else if (type == ASHIFT)
4011 return (a == SHIFT_LOOP
4012 || (TARGET_H8300H && mode == SImode && count == 8));
4013 else if (type == LSHIFTRT)
4014 return (lr == SHIFT_LOOP
4015 || (TARGET_H8300H && mode == SImode && count == 8));
4016 else if (type == ASHIFTRT)
4017 return (ar == SHIFT_LOOP
4018 || (TARGET_H8300H && mode == SImode && count == 8));
12bb62fb 4019 gcc_unreachable ();
be1e06df
KH
4020}
4021
e9eba255 4022/* Output the assembler code for doing shifts. */
48837e29 4023
441d04c6 4024const char *
cb713a8d 4025output_a_shift (rtx *operands)
07aae5c2 4026{
48837e29 4027 static int loopend_lab;
48837e29 4028 rtx shift = operands[3];
ef4bddc2 4029 machine_mode mode = GET_MODE (shift);
48837e29
DE
4030 enum rtx_code code = GET_CODE (shift);
4031 enum shift_type shift_type;
4032 enum shift_mode shift_mode;
35fb3d1f 4033 struct shift_info info;
8c440872 4034 int n;
48837e29
DE
4035
4036 loopend_lab++;
4037
4038 switch (mode)
4039 {
4e10a5a7 4040 case E_QImode:
48837e29
DE
4041 shift_mode = QIshift;
4042 break;
4e10a5a7 4043 case E_HImode:
48837e29
DE
4044 shift_mode = HIshift;
4045 break;
4e10a5a7 4046 case E_SImode:
48837e29
DE
4047 shift_mode = SIshift;
4048 break;
4049 default:
8c440872 4050 gcc_unreachable ();
48837e29 4051 }
07aae5c2 4052
48837e29 4053 switch (code)
07aae5c2 4054 {
48837e29
DE
4055 case ASHIFTRT:
4056 shift_type = SHIFT_ASHIFTRT;
4057 break;
4058 case LSHIFTRT:
4059 shift_type = SHIFT_LSHIFTRT;
4060 break;
4061 case ASHIFT:
4062 shift_type = SHIFT_ASHIFT;
4063 break;
4064 default:
8c440872 4065 gcc_unreachable ();
48837e29 4066 }
07aae5c2 4067
8c440872
NS
4068 /* This case must be taken care of by one of the two splitters
4069 that convert a variable shift into a loop. */
4070 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4071
4072 n = INTVAL (operands[2]);
4073
4074 /* If the count is negative, make it 0. */
4075 if (n < 0)
4076 n = 0;
4077 /* If the count is too big, truncate it.
4078 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4079 do the intuitive thing. */
4080 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4081 n = GET_MODE_BITSIZE (mode);
4082
4083 get_shift_alg (shift_type, shift_mode, n, &info);
4084
4085 switch (info.alg)
48837e29 4086 {
8c440872
NS
4087 case SHIFT_SPECIAL:
4088 output_asm_insn (info.special, operands);
4089 /* Fall through. */
48837e29 4090
8c440872
NS
4091 case SHIFT_INLINE:
4092 n = info.remainder;
48837e29 4093
8c440872
NS
4094 /* Emit two bit shifts first. */
4095 if (info.shift2 != NULL)
48837e29 4096 {
8c440872
NS
4097 for (; n > 1; n -= 2)
4098 output_asm_insn (info.shift2, operands);
4099 }
51c0c1d7 4100
8c440872
NS
4101 /* Now emit one bit shifts for any residual. */
4102 for (; n > 0; n--)
4103 output_asm_insn (info.shift1, operands);
4104 return "";
4105
4106 case SHIFT_ROT_AND:
4107 {
4108 int m = GET_MODE_BITSIZE (mode) - n;
4109 const int mask = (shift_type == SHIFT_ASHIFT
4110 ? ((1 << m) - 1) << n
4111 : (1 << m) - 1);
4112 char insn_buf[200];
4113
4114 /* Not all possibilities of rotate are supported. They shouldn't
4115 be generated, but let's watch for 'em. */
4116 gcc_assert (info.shift1);
4117
4118 /* Emit two bit rotates first. */
4119 if (info.shift2 != NULL)
48837e29 4120 {
8c440872
NS
4121 for (; m > 1; m -= 2)
4122 output_asm_insn (info.shift2, operands);
4123 }
4124
4125 /* Now single bit rotates for any residual. */
4126 for (; m > 0; m--)
4127 output_asm_insn (info.shift1, operands);
4128
4129 /* Now mask off the high bits. */
4130 switch (mode)
4131 {
4e10a5a7 4132 case E_QImode:
8c440872
NS
4133 sprintf (insn_buf, "and\t#%d,%%X0", mask);
4134 break;
51c0c1d7 4135
4e10a5a7 4136 case E_HImode:
8c440872
NS
4137 sprintf (insn_buf, "and.w\t#%d,%%T0", mask);
4138 break;
51c0c1d7 4139
8c440872
NS
4140 default:
4141 gcc_unreachable ();
48837e29 4142 }
b5eaf9ba 4143
8c440872
NS
4144 output_asm_insn (insn_buf, operands);
4145 return "";
4146 }
b5eaf9ba 4147
8c440872
NS
4148 case SHIFT_LOOP:
4149 /* A loop to shift by a "large" constant value.
4150 If we have shift-by-2 insns, use them. */
4151 if (info.shift2 != NULL)
4152 {
4153 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n / 2,
4154 names_big[REGNO (operands[4])]);
4155 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4156 output_asm_insn (info.shift2, operands);
4157 output_asm_insn ("add #0xff,%X4", operands);
4158 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
4159 if (n % 2)
4160 output_asm_insn (info.shift1, operands);
4161 }
4162 else
4163 {
4164 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n,
4165 names_big[REGNO (operands[4])]);
4166 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
4167 output_asm_insn (info.shift1, operands);
4168 output_asm_insn ("add #0xff,%X4", operands);
4169 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
51c0c1d7 4170 }
8c440872
NS
4171 return "";
4172
4173 default:
4174 gcc_unreachable ();
07aae5c2 4175 }
07aae5c2 4176}
86855e8c 4177
0a2aaacc 4178/* Count the number of assembly instructions in a string TEMPL. */
e9eba255 4179
86855e8c 4180static unsigned int
0a2aaacc 4181h8300_asm_insn_count (const char *templ)
86855e8c
KH
4182{
4183 unsigned int count = 1;
4184
0a2aaacc
KG
4185 for (; *templ; templ++)
4186 if (*templ == '\n')
86855e8c
KH
4187 count++;
4188
4189 return count;
4190}
4191
e9eba255
KH
4192/* Compute the length of a shift insn. */
4193
86855e8c 4194unsigned int
4ea5fe8b 4195compute_a_shift_length (rtx *operands)
86855e8c
KH
4196{
4197 rtx shift = operands[3];
ef4bddc2 4198 machine_mode mode = GET_MODE (shift);
86855e8c
KH
4199 enum rtx_code code = GET_CODE (shift);
4200 enum shift_type shift_type;
4201 enum shift_mode shift_mode;
4202 struct shift_info info;
4203 unsigned int wlength = 0;
4204
4205 switch (mode)
4206 {
4e10a5a7 4207 case E_QImode:
86855e8c
KH
4208 shift_mode = QIshift;
4209 break;
4e10a5a7 4210 case E_HImode:
86855e8c
KH
4211 shift_mode = HIshift;
4212 break;
4e10a5a7 4213 case E_SImode:
86855e8c
KH
4214 shift_mode = SIshift;
4215 break;
4216 default:
8c440872 4217 gcc_unreachable ();
86855e8c
KH
4218 }
4219
4220 switch (code)
4221 {
4222 case ASHIFTRT:
4223 shift_type = SHIFT_ASHIFTRT;
4224 break;
4225 case LSHIFTRT:
4226 shift_type = SHIFT_LSHIFTRT;
4227 break;
4228 case ASHIFT:
4229 shift_type = SHIFT_ASHIFT;
4230 break;
4231 default:
8c440872 4232 gcc_unreachable ();
86855e8c
KH
4233 }
4234
4235 if (GET_CODE (operands[2]) != CONST_INT)
4236 {
4237 /* Get the assembler code to do one shift. */
4238 get_shift_alg (shift_type, shift_mode, 1, &info);
4239
4240 return (4 + h8300_asm_insn_count (info.shift1)) * 2;
4241 }
4242 else
4243 {
4244 int n = INTVAL (operands[2]);
4245
4246 /* If the count is negative, make it 0. */
4247 if (n < 0)
4248 n = 0;
4249 /* If the count is too big, truncate it.
4250 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4251 do the intuitive thing. */
4252 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4253 n = GET_MODE_BITSIZE (mode);
4254
4255 get_shift_alg (shift_type, shift_mode, n, &info);
4256
4257 switch (info.alg)
4258 {
4259 case SHIFT_SPECIAL:
4260 wlength += h8300_asm_insn_count (info.special);
41c3eb5d
KH
4261
4262 /* Every assembly instruction used in SHIFT_SPECIAL case
4263 takes 2 bytes except xor.l, which takes 4 bytes, so if we
4264 see xor.l, we just pretend that xor.l counts as two insns
4265 so that the insn length will be computed correctly. */
4266 if (strstr (info.special, "xor.l") != NULL)
4267 wlength++;
4268
86855e8c
KH
4269 /* Fall through. */
4270
4271 case SHIFT_INLINE:
4272 n = info.remainder;
4273
4274 if (info.shift2 != NULL)
4275 {
4276 wlength += h8300_asm_insn_count (info.shift2) * (n / 2);
4277 n = n % 2;
4278 }
4279
4280 wlength += h8300_asm_insn_count (info.shift1) * n;
6b148bd9 4281
86855e8c
KH
4282 return 2 * wlength;
4283
4284 case SHIFT_ROT_AND:
4285 {
4286 int m = GET_MODE_BITSIZE (mode) - n;
4287
4288 /* Not all possibilities of rotate are supported. They shouldn't
4289 be generated, but let's watch for 'em. */
8c440872 4290 gcc_assert (info.shift1);
86855e8c
KH
4291
4292 if (info.shift2 != NULL)
4293 {
4294 wlength += h8300_asm_insn_count (info.shift2) * (m / 2);
4295 m = m % 2;
4296 }
4297
4298 wlength += h8300_asm_insn_count (info.shift1) * m;
6b148bd9 4299
86855e8c
KH
4300 /* Now mask off the high bits. */
4301 switch (mode)
4302 {
4e10a5a7 4303 case E_QImode:
86855e8c
KH
4304 wlength += 1;
4305 break;
4e10a5a7 4306 case E_HImode:
86855e8c
KH
4307 wlength += 2;
4308 break;
4e10a5a7 4309 case E_SImode:
86855e8c
KH
4310 wlength += 3;
4311 break;
4312 default:
8c440872 4313 gcc_unreachable ();
86855e8c
KH
4314 }
4315 return 2 * wlength;
4316 }
4317
4318 case SHIFT_LOOP:
4319 /* A loop to shift by a "large" constant value.
4320 If we have shift-by-2 insns, use them. */
4321 if (info.shift2 != NULL)
4322 {
4323 wlength += 3 + h8300_asm_insn_count (info.shift2);
4324 if (n % 2)
4325 wlength += h8300_asm_insn_count (info.shift1);
4326 }
4327 else
4328 {
4329 wlength += 3 + h8300_asm_insn_count (info.shift1);
4330 }
4331 return 2 * wlength;
4332
4333 default:
8c440872 4334 gcc_unreachable ();
86855e8c
KH
4335 }
4336 }
4337}
45ca2106 4338
e9eba255
KH
4339/* Compute which flag bits are valid after a shift insn. */
4340
f16897cb 4341int
cb713a8d 4342compute_a_shift_cc (rtx insn ATTRIBUTE_UNUSED, rtx *operands)
45ca2106
KH
4343{
4344 rtx shift = operands[3];
ef4bddc2 4345 machine_mode mode = GET_MODE (shift);
45ca2106
KH
4346 enum rtx_code code = GET_CODE (shift);
4347 enum shift_type shift_type;
4348 enum shift_mode shift_mode;
4349 struct shift_info info;
8c440872
NS
4350 int n;
4351
45ca2106
KH
4352 switch (mode)
4353 {
4e10a5a7 4354 case E_QImode:
45ca2106
KH
4355 shift_mode = QIshift;
4356 break;
4e10a5a7 4357 case E_HImode:
45ca2106
KH
4358 shift_mode = HIshift;
4359 break;
4e10a5a7 4360 case E_SImode:
45ca2106
KH
4361 shift_mode = SIshift;
4362 break;
4363 default:
8c440872 4364 gcc_unreachable ();
45ca2106
KH
4365 }
4366
4367 switch (code)
4368 {
4369 case ASHIFTRT:
4370 shift_type = SHIFT_ASHIFTRT;
4371 break;
4372 case LSHIFTRT:
4373 shift_type = SHIFT_LSHIFTRT;
4374 break;
4375 case ASHIFT:
4376 shift_type = SHIFT_ASHIFT;
4377 break;
4378 default:
8c440872 4379 gcc_unreachable ();
45ca2106
KH
4380 }
4381
8c440872
NS
4382 /* This case must be taken care of by one of the two splitters
4383 that convert a variable shift into a loop. */
4384 gcc_assert (GET_CODE (operands[2]) == CONST_INT);
4385
4386 n = INTVAL (operands[2]);
4387
4388 /* If the count is negative, make it 0. */
4389 if (n < 0)
4390 n = 0;
4391 /* If the count is too big, truncate it.
4392 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
4393 do the intuitive thing. */
4394 else if ((unsigned int) n > GET_MODE_BITSIZE (mode))
4395 n = GET_MODE_BITSIZE (mode);
4396
4397 get_shift_alg (shift_type, shift_mode, n, &info);
4398
4399 switch (info.alg)
45ca2106 4400 {
8c440872
NS
4401 case SHIFT_SPECIAL:
4402 if (info.remainder == 0)
4403 return info.cc_special;
45ca2106 4404
8c440872 4405 /* Fall through. */
45ca2106 4406
8c440872
NS
4407 case SHIFT_INLINE:
4408 return info.cc_inline;
4409
4410 case SHIFT_ROT_AND:
4411 /* This case always ends with an and instruction. */
5ddb6eca 4412 return OLD_CC_SET_ZNV;
8c440872
NS
4413
4414 case SHIFT_LOOP:
4415 /* A loop to shift by a "large" constant value.
4416 If we have shift-by-2 insns, use them. */
4417 if (info.shift2 != NULL)
45ca2106 4418 {
8c440872
NS
4419 if (n % 2)
4420 return info.cc_inline;
45ca2106 4421 }
5ddb6eca 4422 return OLD_CC_CLOBBER;
8c440872
NS
4423
4424 default:
4425 gcc_unreachable ();
45ca2106
KH
4426 }
4427}
48837e29 4428\f
edd71f0f
KH
4429/* A rotation by a non-constant will cause a loop to be generated, in
4430 which a rotation by one bit is used. A rotation by a constant,
4431 including the one in the loop, will be taken care of by
caf7f21a 4432 output_a_rotate () at the insn emit time. */
edd71f0f
KH
4433
4434int
3d2e90d6 4435expand_a_rotate (rtx operands[])
edd71f0f
KH
4436{
4437 rtx dst = operands[0];
4438 rtx src = operands[1];
4439 rtx rotate_amount = operands[2];
ef4bddc2 4440 machine_mode mode = GET_MODE (dst);
edd71f0f 4441
beed8fc0
AO
4442 if (h8sx_classify_shift (mode, ROTATE, rotate_amount) == H8SX_SHIFT_UNARY)
4443 return false;
4444
edd71f0f
KH
4445 /* We rotate in place. */
4446 emit_move_insn (dst, src);
4447
4448 if (GET_CODE (rotate_amount) != CONST_INT)
4449 {
4450 rtx counter = gen_reg_rtx (QImode);
19f8b229
TS
4451 rtx_code_label *start_label = gen_label_rtx ();
4452 rtx_code_label *end_label = gen_label_rtx ();
edd71f0f
KH
4453
4454 /* If the rotate amount is less than or equal to 0,
4455 we go out of the loop. */
a556fd39 4456 emit_cmp_and_jump_insns (rotate_amount, const0_rtx, LE, NULL_RTX,
d43e0b7d 4457 QImode, 0, end_label);
edd71f0f
KH
4458
4459 /* Initialize the loop counter. */
4460 emit_move_insn (counter, rotate_amount);
4461
4462 emit_label (start_label);
4463
4464 /* Rotate by one bit. */
01ab5574
KH
4465 switch (mode)
4466 {
4e10a5a7 4467 case E_QImode:
01ab5574
KH
4468 emit_insn (gen_rotlqi3_1 (dst, dst, const1_rtx));
4469 break;
4e10a5a7 4470 case E_HImode:
01ab5574
KH
4471 emit_insn (gen_rotlhi3_1 (dst, dst, const1_rtx));
4472 break;
4e10a5a7 4473 case E_SImode:
01ab5574
KH
4474 emit_insn (gen_rotlsi3_1 (dst, dst, const1_rtx));
4475 break;
4476 default:
8c440872 4477 gcc_unreachable ();
01ab5574 4478 }
edd71f0f
KH
4479
4480 /* Decrement the counter by 1. */
01ab5574 4481 emit_insn (gen_addqi3 (counter, counter, constm1_rtx));
edd71f0f 4482
9cd10576 4483 /* If the loop counter is nonzero, we go back to the beginning
edd71f0f 4484 of the loop. */
a556fd39 4485 emit_cmp_and_jump_insns (counter, const0_rtx, NE, NULL_RTX, QImode, 1,
d43e0b7d 4486 start_label);
edd71f0f
KH
4487
4488 emit_label (end_label);
4489 }
4490 else
4491 {
4492 /* Rotate by AMOUNT bits. */
01ab5574
KH
4493 switch (mode)
4494 {
4e10a5a7 4495 case E_QImode:
01ab5574
KH
4496 emit_insn (gen_rotlqi3_1 (dst, dst, rotate_amount));
4497 break;
4e10a5a7 4498 case E_HImode:
01ab5574
KH
4499 emit_insn (gen_rotlhi3_1 (dst, dst, rotate_amount));
4500 break;
4e10a5a7 4501 case E_SImode:
01ab5574
KH
4502 emit_insn (gen_rotlsi3_1 (dst, dst, rotate_amount));
4503 break;
4504 default:
8c440872 4505 gcc_unreachable ();
01ab5574 4506 }
edd71f0f
KH
4507 }
4508
4509 return 1;
4510}
4511
e9eba255 4512/* Output a rotate insn. */
edd71f0f
KH
4513
4514const char *
caf7f21a 4515output_a_rotate (enum rtx_code code, rtx *operands)
edd71f0f
KH
4516{
4517 rtx dst = operands[0];
4518 rtx rotate_amount = operands[2];
4519 enum shift_mode rotate_mode;
4520 enum shift_type rotate_type;
4521 const char *insn_buf;
4522 int bits;
4523 int amount;
ef4bddc2 4524 machine_mode mode = GET_MODE (dst);
edd71f0f 4525
8c440872 4526 gcc_assert (GET_CODE (rotate_amount) == CONST_INT);
edd71f0f
KH
4527
4528 switch (mode)
4529 {
4e10a5a7 4530 case E_QImode:
edd71f0f
KH
4531 rotate_mode = QIshift;
4532 break;
4e10a5a7 4533 case E_HImode:
edd71f0f
KH
4534 rotate_mode = HIshift;
4535 break;
4e10a5a7 4536 case E_SImode:
edd71f0f
KH
4537 rotate_mode = SIshift;
4538 break;
4539 default:
8c440872 4540 gcc_unreachable ();
edd71f0f
KH
4541 }
4542
4543 switch (code)
4544 {
4545 case ROTATERT:
4546 rotate_type = SHIFT_ASHIFT;
4547 break;
4548 case ROTATE:
4549 rotate_type = SHIFT_LSHIFTRT;
4550 break;
4551 default:
8c440872 4552 gcc_unreachable ();
edd71f0f
KH
4553 }
4554
4555 amount = INTVAL (rotate_amount);
4556
4557 /* Clean up AMOUNT. */
4558 if (amount < 0)
4559 amount = 0;
4560 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4561 amount = GET_MODE_BITSIZE (mode);
4562
4563 /* Determine the faster direction. After this phase, amount will be
4564 at most a half of GET_MODE_BITSIZE (mode). */
e0c32c62 4565 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
edd71f0f
KH
4566 {
4567 /* Flip the direction. */
4568 amount = GET_MODE_BITSIZE (mode) - amount;
4569 rotate_type =
4570 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4571 }
4572
4573 /* See if a byte swap (in HImode) or a word swap (in SImode) can
4574 boost up the rotation. */
27c1afd5 4575 if ((mode == HImode && TARGET_H8300H && amount >= 6)
edd71f0f
KH
4576 || (mode == HImode && TARGET_H8300S && amount == 8)
4577 || (mode == SImode && TARGET_H8300H && amount >= 10)
4578 || (mode == SImode && TARGET_H8300S && amount >= 13))
4579 {
4580 switch (mode)
4581 {
4e10a5a7 4582 case E_HImode:
edd71f0f
KH
4583 /* This code works on any family. */
4584 insn_buf = "xor.b\t%s0,%t0\n\txor.b\t%t0,%s0\n\txor.b\t%s0,%t0";
4585 output_asm_insn (insn_buf, operands);
4586 break;
4587
4e10a5a7 4588 case E_SImode:
3db11b5c 4589 /* This code works on the H8/300H and H8S. */
edd71f0f
KH
4590 insn_buf = "xor.w\t%e0,%f0\n\txor.w\t%f0,%e0\n\txor.w\t%e0,%f0";
4591 output_asm_insn (insn_buf, operands);
4592 break;
4593
4594 default:
8c440872 4595 gcc_unreachable ();
edd71f0f
KH
4596 }
4597
4598 /* Adjust AMOUNT and flip the direction. */
4599 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4600 rotate_type =
4601 (rotate_type == SHIFT_ASHIFT) ? SHIFT_LSHIFTRT : SHIFT_ASHIFT;
4602 }
4603
01ab5574 4604 /* Output rotate insns. */
edd71f0f
KH
4605 for (bits = TARGET_H8300S ? 2 : 1; bits > 0; bits /= 2)
4606 {
4607 if (bits == 2)
4608 insn_buf = rotate_two[rotate_type][rotate_mode];
4609 else
4610 insn_buf = rotate_one[cpu_type][rotate_type][rotate_mode];
2c54abce 4611
edd71f0f
KH
4612 for (; amount >= bits; amount -= bits)
4613 output_asm_insn (insn_buf, operands);
4614 }
4615
4616 return "";
4617}
caf7f21a 4618
e9eba255
KH
4619/* Compute the length of a rotate insn. */
4620
caf7f21a
KH
4621unsigned int
4622compute_a_rotate_length (rtx *operands)
4623{
4624 rtx src = operands[1];
343fd2c7 4625 rtx amount_rtx = operands[2];
ef4bddc2 4626 machine_mode mode = GET_MODE (src);
caf7f21a
KH
4627 int amount;
4628 unsigned int length = 0;
4629
8c440872 4630 gcc_assert (GET_CODE (amount_rtx) == CONST_INT);
caf7f21a 4631
343fd2c7 4632 amount = INTVAL (amount_rtx);
caf7f21a
KH
4633
4634 /* Clean up AMOUNT. */
4635 if (amount < 0)
4636 amount = 0;
4637 if ((unsigned int) amount > GET_MODE_BITSIZE (mode))
4638 amount = GET_MODE_BITSIZE (mode);
4639
4640 /* Determine the faster direction. After this phase, amount
4641 will be at most a half of GET_MODE_BITSIZE (mode). */
4642 if ((unsigned int) amount > GET_MODE_BITSIZE (mode) / (unsigned) 2)
4643 /* Flip the direction. */
4644 amount = GET_MODE_BITSIZE (mode) - amount;
4645
4646 /* See if a byte swap (in HImode) or a word swap (in SImode) can
4647 boost up the rotation. */
27c1afd5 4648 if ((mode == HImode && TARGET_H8300H && amount >= 6)
caf7f21a
KH
4649 || (mode == HImode && TARGET_H8300S && amount == 8)
4650 || (mode == SImode && TARGET_H8300H && amount >= 10)
4651 || (mode == SImode && TARGET_H8300S && amount >= 13))
4652 {
4653 /* Adjust AMOUNT and flip the direction. */
4654 amount = GET_MODE_BITSIZE (mode) / 2 - amount;
4655 length += 6;
4656 }
4657
4658 /* We use 2-bit rotations on the H8S. */
4659 if (TARGET_H8300S)
4660 amount = amount / 2 + amount % 2;
4661
4662 /* The H8/300 uses three insns to rotate one bit, taking 6
4663 length. */
27c1afd5 4664 length += amount * 2;
caf7f21a
KH
4665
4666 return length;
4667}
edd71f0f 4668\f
48837e29 4669/* Fix the operands of a gen_xxx so that it could become a bit
2c54abce 4670 operating insn. */
07aae5c2
SC
4671
4672int
4093985c 4673fix_bit_operand (rtx *operands, enum rtx_code code)
07aae5c2 4674{
abc95ed3 4675 /* The bit_operand predicate accepts any memory during RTL generation, but
48837e29
DE
4676 only 'U' memory afterwards, so if this is a MEM operand, we must force
4677 it to be valid for 'U' by reloading the address. */
07aae5c2 4678
4093985c
KH
4679 if (code == AND
4680 ? single_zero_operand (operands[2], QImode)
4681 : single_one_operand (operands[2], QImode))
07aae5c2 4682 {
2e760b15
KH
4683 /* OK to have a memory dest. */
4684 if (GET_CODE (operands[0]) == MEM
ceaaaeab 4685 && !satisfies_constraint_U (operands[0]))
48837e29 4686 {
2e760b15
KH
4687 rtx mem = gen_rtx_MEM (GET_MODE (operands[0]),
4688 copy_to_mode_reg (Pmode,
4689 XEXP (operands[0], 0)));
4690 MEM_COPY_ATTRIBUTES (mem, operands[0]);
4691 operands[0] = mem;
4692 }
48837e29 4693
2e760b15 4694 if (GET_CODE (operands[1]) == MEM
ceaaaeab 4695 && !satisfies_constraint_U (operands[1]))
2e760b15
KH
4696 {
4697 rtx mem = gen_rtx_MEM (GET_MODE (operands[1]),
4698 copy_to_mode_reg (Pmode,
4699 XEXP (operands[1], 0)));
4700 MEM_COPY_ATTRIBUTES (mem, operands[0]);
4701 operands[1] = mem;
48837e29 4702 }
2e760b15 4703 return 0;
48837e29 4704 }
07aae5c2 4705
48837e29 4706 /* Dest and src op must be register. */
07aae5c2 4707
48837e29
DE
4708 operands[1] = force_reg (QImode, operands[1]);
4709 {
4710 rtx res = gen_reg_rtx (QImode);
fd57a6e4 4711 switch (code)
a3579575
KH
4712 {
4713 case AND:
4714 emit_insn (gen_andqi3_1 (res, operands[1], operands[2]));
4715 break;
4716 case IOR:
4717 emit_insn (gen_iorqi3_1 (res, operands[1], operands[2]));
4718 break;
4719 case XOR:
4720 emit_insn (gen_xorqi3_1 (res, operands[1], operands[2]));
4721 break;
4722 default:
8c440872 4723 gcc_unreachable ();
a3579575
KH
4724 }
4725 emit_insn (gen_movqi (operands[0], res));
48837e29
DE
4726 }
4727 return 1;
07aae5c2 4728}
f5b65a56 4729
f5b65a56
JL
4730/* Return nonzero if FUNC is an interrupt function as specified
4731 by the "interrupt" attribute. */
4732
4733static int
cb713a8d 4734h8300_interrupt_function_p (tree func)
f5b65a56
JL
4735{
4736 tree a;
4737
4738 if (TREE_CODE (func) != FUNCTION_DECL)
4739 return 0;
4740
91d231cb 4741 a = lookup_attribute ("interrupt_handler", DECL_ATTRIBUTES (func));
f5b65a56
JL
4742 return a != NULL_TREE;
4743}
4744
3cfa3702
KH
4745/* Return nonzero if FUNC is a saveall function as specified by the
4746 "saveall" attribute. */
4747
4748static int
4749h8300_saveall_function_p (tree func)
4750{
4751 tree a;
4752
4753 if (TREE_CODE (func) != FUNCTION_DECL)
4754 return 0;
4755
4756 a = lookup_attribute ("saveall", DECL_ATTRIBUTES (func));
4757 return a != NULL_TREE;
4758}
4759
fabe72bb
JL
4760/* Return nonzero if FUNC is an OS_Task function as specified
4761 by the "OS_Task" attribute. */
4762
4763static int
cb713a8d 4764h8300_os_task_function_p (tree func)
fabe72bb
JL
4765{
4766 tree a;
4767
4768 if (TREE_CODE (func) != FUNCTION_DECL)
4769 return 0;
4770
91d231cb 4771 a = lookup_attribute ("OS_Task", DECL_ATTRIBUTES (func));
fabe72bb
JL
4772 return a != NULL_TREE;
4773}
4774
4775/* Return nonzero if FUNC is a monitor function as specified
4776 by the "monitor" attribute. */
4777
4778static int
cb713a8d 4779h8300_monitor_function_p (tree func)
fabe72bb
JL
4780{
4781 tree a;
4782
4783 if (TREE_CODE (func) != FUNCTION_DECL)
4784 return 0;
4785
91d231cb 4786 a = lookup_attribute ("monitor", DECL_ATTRIBUTES (func));
fabe72bb
JL
4787 return a != NULL_TREE;
4788}
4789
f5b65a56
JL
4790/* Return nonzero if FUNC is a function that should be called
4791 through the function vector. */
4792
4793int
cb713a8d 4794h8300_funcvec_function_p (tree func)
f5b65a56
JL
4795{
4796 tree a;
4797
4798 if (TREE_CODE (func) != FUNCTION_DECL)
4799 return 0;
4800
91d231cb 4801 a = lookup_attribute ("function_vector", DECL_ATTRIBUTES (func));
f5b65a56
JL
4802 return a != NULL_TREE;
4803}
4804
887a8bd9 4805/* Return nonzero if DECL is a variable that's in the eight bit
15dc331e
JL
4806 data area. */
4807
4808int
cb713a8d 4809h8300_eightbit_data_p (tree decl)
15dc331e
JL
4810{
4811 tree a;
4812
4813 if (TREE_CODE (decl) != VAR_DECL)
4814 return 0;
4815
91d231cb 4816 a = lookup_attribute ("eightbit_data", DECL_ATTRIBUTES (decl));
15dc331e
JL
4817 return a != NULL_TREE;
4818}
4819
887a8bd9
JL
4820/* Return nonzero if DECL is a variable that's in the tiny
4821 data area. */
4822
4823int
cb713a8d 4824h8300_tiny_data_p (tree decl)
887a8bd9
JL
4825{
4826 tree a;
4827
4828 if (TREE_CODE (decl) != VAR_DECL)
4829 return 0;
4830
91d231cb 4831 a = lookup_attribute ("tiny_data", DECL_ATTRIBUTES (decl));
887a8bd9
JL
4832 return a != NULL_TREE;
4833}
4834
3cfa3702
KH
4835/* Generate an 'interrupt_handler' attribute for decls. We convert
4836 all the pragmas to corresponding attributes. */
2c1d2fcb
DD
4837
4838static void
cb713a8d 4839h8300_insert_attributes (tree node, tree *attributes)
2c1d2fcb 4840{
3cfa3702
KH
4841 if (TREE_CODE (node) == FUNCTION_DECL)
4842 {
4843 if (pragma_interrupt)
4844 {
4845 pragma_interrupt = 0;
2c1d2fcb 4846
3cfa3702
KH
4847 /* Add an 'interrupt_handler' attribute. */
4848 *attributes = tree_cons (get_identifier ("interrupt_handler"),
4849 NULL, *attributes);
4850 }
e392d367 4851
3cfa3702
KH
4852 if (pragma_saveall)
4853 {
4854 pragma_saveall = 0;
4855
4856 /* Add an 'saveall' attribute. */
4857 *attributes = tree_cons (get_identifier ("saveall"),
4858 NULL, *attributes);
4859 }
4860 }
2c1d2fcb
DD
4861}
4862
91d231cb 4863/* Supported attributes:
f5b65a56 4864
97c5ec1d 4865 interrupt_handler: output a prologue and epilogue suitable for an
f5b65a56
JL
4866 interrupt handler.
4867
3cfa3702
KH
4868 saveall: output a prologue and epilogue that saves and restores
4869 all registers except the stack pointer.
4870
97c5ec1d 4871 function_vector: This function should be called through the
887a8bd9
JL
4872 function vector.
4873
4874 eightbit_data: This variable lives in the 8-bit data area and can
4875 be referenced with 8-bit absolute memory addresses.
4876
4877 tiny_data: This variable lives in the tiny data area and can be
4878 referenced with 16-bit absolute memory references. */
f5b65a56 4879
6bc7bc14 4880static const struct attribute_spec h8300_attribute_table[] =
f5b65a56 4881{
4849deb1
JJ
4882 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4883 affects_type_identity, handler, exclude } */
4884 { "interrupt_handler", 0, 0, true, false, false, false,
4885 h8300_handle_fndecl_attribute, NULL },
4886 { "saveall", 0, 0, true, false, false, false,
4887 h8300_handle_fndecl_attribute, NULL },
4888 { "OS_Task", 0, 0, true, false, false, false,
4889 h8300_handle_fndecl_attribute, NULL },
4890 { "monitor", 0, 0, true, false, false, false,
4891 h8300_handle_fndecl_attribute, NULL },
4892 { "function_vector", 0, 0, true, false, false, false,
4893 h8300_handle_fndecl_attribute, NULL },
4894 { "eightbit_data", 0, 0, true, false, false, false,
4895 h8300_handle_eightbit_data_attribute, NULL },
4896 { "tiny_data", 0, 0, true, false, false, false,
4897 h8300_handle_tiny_data_attribute, NULL },
4898 { NULL, 0, 0, false, false, false, false, NULL, NULL }
91d231cb 4899};
f5b65a56 4900
15dc331e 4901
91d231cb
JM
4902/* Handle an attribute requiring a FUNCTION_DECL; arguments as in
4903 struct attribute_spec.handler. */
4904static tree
cb713a8d
KH
4905h8300_handle_fndecl_attribute (tree *node, tree name,
4906 tree args ATTRIBUTE_UNUSED,
4907 int flags ATTRIBUTE_UNUSED,
4908 bool *no_add_attrs)
91d231cb
JM
4909{
4910 if (TREE_CODE (*node) != FUNCTION_DECL)
4911 {
29d08eba
JM
4912 warning (OPT_Wattributes, "%qE attribute only applies to functions",
4913 name);
91d231cb
JM
4914 *no_add_attrs = true;
4915 }
4916
4917 return NULL_TREE;
4918}
4919
4920/* Handle an "eightbit_data" attribute; arguments as in
4921 struct attribute_spec.handler. */
4922static tree
cb713a8d
KH
4923h8300_handle_eightbit_data_attribute (tree *node, tree name,
4924 tree args ATTRIBUTE_UNUSED,
4925 int flags ATTRIBUTE_UNUSED,
4926 bool *no_add_attrs)
91d231cb
JM
4927{
4928 tree decl = *node;
4929
4930 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
15dc331e 4931 {
f961457f 4932 set_decl_section_name (decl, ".eight");
91d231cb
JM
4933 }
4934 else
4935 {
29d08eba
JM
4936 warning (OPT_Wattributes, "%qE attribute ignored",
4937 name);
91d231cb 4938 *no_add_attrs = true;
887a8bd9
JL
4939 }
4940
91d231cb
JM
4941 return NULL_TREE;
4942}
4943
4944/* Handle an "tiny_data" attribute; arguments as in
4945 struct attribute_spec.handler. */
4946static tree
cb713a8d
KH
4947h8300_handle_tiny_data_attribute (tree *node, tree name,
4948 tree args ATTRIBUTE_UNUSED,
4949 int flags ATTRIBUTE_UNUSED,
4950 bool *no_add_attrs)
91d231cb
JM
4951{
4952 tree decl = *node;
4953
4954 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
887a8bd9 4955 {
f961457f 4956 set_decl_section_name (decl, ".tiny");
91d231cb
JM
4957 }
4958 else
4959 {
29d08eba
JM
4960 warning (OPT_Wattributes, "%qE attribute ignored",
4961 name);
91d231cb 4962 *no_add_attrs = true;
15dc331e 4963 }
07e4d94e 4964
91d231cb 4965 return NULL_TREE;
f5b65a56
JL
4966}
4967
dc66a1c4 4968/* Mark function vectors, and various small data objects. */
fb49053f
RH
4969
4970static void
cb713a8d 4971h8300_encode_section_info (tree decl, rtx rtl, int first)
fb49053f 4972{
dc66a1c4
RH
4973 int extra_flags = 0;
4974
c6a2438a 4975 default_encode_section_info (decl, rtl, first);
dc66a1c4 4976
fb49053f
RH
4977 if (TREE_CODE (decl) == FUNCTION_DECL
4978 && h8300_funcvec_function_p (decl))
dc66a1c4 4979 extra_flags = SYMBOL_FLAG_FUNCVEC_FUNCTION;
fb49053f
RH
4980 else if (TREE_CODE (decl) == VAR_DECL
4981 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
4982 {
4983 if (h8300_eightbit_data_p (decl))
dc66a1c4 4984 extra_flags = SYMBOL_FLAG_EIGHTBIT_DATA;
fb49053f 4985 else if (first && h8300_tiny_data_p (decl))
dc66a1c4 4986 extra_flags = SYMBOL_FLAG_TINY_DATA;
fb49053f 4987 }
772c5265 4988
dc66a1c4 4989 if (extra_flags)
c6a2438a 4990 SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= extra_flags;
772c5265
RH
4991}
4992
e9eba255
KH
4993/* Output a single-bit extraction. */
4994
441d04c6 4995const char *
cb713a8d 4996output_simode_bld (int bild, rtx operands[])
bd93f126 4997{
27c1afd5
JL
4998 /* Determine if we can clear the destination first. */
4999 int clear_first = (REG_P (operands[0]) && REG_P (operands[1])
5000 && REGNO (operands[0]) != REGNO (operands[1]));
6be580c7 5001
27c1afd5
JL
5002 if (clear_first)
5003 output_asm_insn ("sub.l\t%S0,%S0", operands);
bd93f126 5004
27c1afd5
JL
5005 /* Output the bit load or bit inverse load. */
5006 if (bild)
5007 output_asm_insn ("bild\t%Z2,%Y1", operands);
bd93f126 5008 else
27c1afd5 5009 output_asm_insn ("bld\t%Z2,%Y1", operands);
0eb933a0 5010
27c1afd5
JL
5011 if (!clear_first)
5012 output_asm_insn ("xor.l\t%S0,%S0", operands);
0eb933a0 5013
27c1afd5
JL
5014 /* Perform the bit store. */
5015 output_asm_insn ("rotxl.l\t%S0", operands);
bd93f126
JL
5016
5017 /* All done. */
5018 return "";
5019}
e6219736 5020
beed8fc0
AO
5021/* Delayed-branch scheduling is more effective if we have some idea
5022 how long each instruction will be. Use a shorten_branches pass
5023 to get an initial estimate. */
5024
5025static void
5026h8300_reorg (void)
5027{
5028 if (flag_delayed_branch)
5029 shorten_branches (get_insns ());
5030}
5031
7c143ed2
KH
5032/* Nonzero if X is a constant address suitable as an 8-bit absolute,
5033 which is a special case of the 'R' operand. */
5034
803d56f5 5035int
cb713a8d 5036h8300_eightbit_constant_address_p (rtx x)
803d56f5 5037{
ff482c8d 5038 /* The ranges of the 8-bit area. */
d2d199a3
KH
5039 const unsigned HOST_WIDE_INT n1 = trunc_int_for_mode (0xff00, HImode);
5040 const unsigned HOST_WIDE_INT n2 = trunc_int_for_mode (0xffff, HImode);
803d56f5
KH
5041 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00ffff00, SImode);
5042 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00ffffff, SImode);
5043 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0xffffff00, SImode);
5044 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0xffffffff, SImode);
5045
5046 unsigned HOST_WIDE_INT addr;
5047
9675a91e 5048 /* We accept symbols declared with eightbit_data. */
dc66a1c4
RH
5049 if (GET_CODE (x) == SYMBOL_REF)
5050 return (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0;
9675a91e 5051
9f9ab303
JL
5052 if (GET_CODE (x) == CONST
5053 && GET_CODE (XEXP (x, 0)) == PLUS
5054 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
5055 && (SYMBOL_REF_FLAGS (XEXP (XEXP (x, 0), 0)) & SYMBOL_FLAG_EIGHTBIT_DATA) != 0)
5056 return 1;
5057
803d56f5
KH
5058 if (GET_CODE (x) != CONST_INT)
5059 return 0;
5060
5061 addr = INTVAL (x);
5062
5063 return (0
27c1afd5 5064 || (TARGET_NORMAL_MODE && IN_RANGE (addr, n1, n2))
803d56f5
KH
5065 || (TARGET_H8300H && IN_RANGE (addr, h1, h2))
5066 || (TARGET_H8300S && IN_RANGE (addr, s1, s2)));
5067}
5068
7c143ed2
KH
5069/* Nonzero if X is a constant address suitable as an 16-bit absolute
5070 on H8/300H and H8S. */
5071
803d56f5 5072int
cb713a8d 5073h8300_tiny_constant_address_p (rtx x)
803d56f5 5074{
3f7211f1 5075 /* The ranges of the 16-bit area. */
803d56f5
KH
5076 const unsigned HOST_WIDE_INT h1 = trunc_int_for_mode (0x00000000, SImode);
5077 const unsigned HOST_WIDE_INT h2 = trunc_int_for_mode (0x00007fff, SImode);
5078 const unsigned HOST_WIDE_INT h3 = trunc_int_for_mode (0x00ff8000, SImode);
5079 const unsigned HOST_WIDE_INT h4 = trunc_int_for_mode (0x00ffffff, SImode);
5080 const unsigned HOST_WIDE_INT s1 = trunc_int_for_mode (0x00000000, SImode);
5081 const unsigned HOST_WIDE_INT s2 = trunc_int_for_mode (0x00007fff, SImode);
5082 const unsigned HOST_WIDE_INT s3 = trunc_int_for_mode (0xffff8000, SImode);
5083 const unsigned HOST_WIDE_INT s4 = trunc_int_for_mode (0xffffffff, SImode);
5084
5085 unsigned HOST_WIDE_INT addr;
5086
d6456562
KH
5087 switch (GET_CODE (x))
5088 {
5089 case SYMBOL_REF:
a4bb41cc
KH
5090 /* In the normal mode, any symbol fits in the 16-bit absolute
5091 address range. We also accept symbols declared with
5092 tiny_data. */
5093 return (TARGET_NORMAL_MODE
5094 || (SYMBOL_REF_FLAGS (x) & SYMBOL_FLAG_TINY_DATA) != 0);
56b8e164 5095
d6456562
KH
5096 case CONST_INT:
5097 addr = INTVAL (x);
5098 return (TARGET_NORMAL_MODE
5099 || (TARGET_H8300H
5100 && (IN_RANGE (addr, h1, h2) || IN_RANGE (addr, h3, h4)))
5101 || (TARGET_H8300S
5102 && (IN_RANGE (addr, s1, s2) || IN_RANGE (addr, s3, s4))));
803d56f5 5103
a4bb41cc
KH
5104 case CONST:
5105 return TARGET_NORMAL_MODE;
5106
d6456562
KH
5107 default:
5108 return 0;
5109 }
803d56f5 5110
803d56f5 5111}
9b98dc74 5112
e9eba255
KH
5113/* Return nonzero if ADDR1 and ADDR2 point to consecutive memory
5114 locations that can be accessed as a 16-bit word. */
5115
9b98dc74 5116int
cb713a8d 5117byte_accesses_mergeable_p (rtx addr1, rtx addr2)
9b98dc74
KH
5118{
5119 HOST_WIDE_INT offset1, offset2;
5120 rtx reg1, reg2;
5121
5122 if (REG_P (addr1))
5123 {
5124 reg1 = addr1;
5125 offset1 = 0;
5126 }
5127 else if (GET_CODE (addr1) == PLUS
5128 && REG_P (XEXP (addr1, 0))
5129 && GET_CODE (XEXP (addr1, 1)) == CONST_INT)
5130 {
5131 reg1 = XEXP (addr1, 0);
5132 offset1 = INTVAL (XEXP (addr1, 1));
5133 }
5134 else
5135 return 0;
5136
5137 if (REG_P (addr2))
5138 {
5139 reg2 = addr2;
5140 offset2 = 0;
5141 }
5142 else if (GET_CODE (addr2) == PLUS
5143 && REG_P (XEXP (addr2, 0))
5144 && GET_CODE (XEXP (addr2, 1)) == CONST_INT)
5145 {
5146 reg2 = XEXP (addr2, 0);
5147 offset2 = INTVAL (XEXP (addr2, 1));
5148 }
5149 else
5150 return 0;
5151
5152 if (((reg1 == stack_pointer_rtx && reg2 == stack_pointer_rtx)
5153 || (reg1 == frame_pointer_rtx && reg2 == frame_pointer_rtx))
5154 && offset1 % 2 == 0
5155 && offset1 + 1 == offset2)
5156 return 1;
5157
5158 return 0;
5159}
02529902
KH
5160
5161/* Return nonzero if we have the same comparison insn as I3 two insns
19cff4db 5162 before I3. I3 is assumed to be a comparison insn. */
02529902
KH
5163
5164int
82082f65 5165same_cmp_preceding_p (rtx_insn *i3)
02529902 5166{
68a1a6c0 5167 rtx_insn *i1, *i2;
02529902
KH
5168
5169 /* Make sure we have a sequence of three insns. */
5170 i2 = prev_nonnote_insn (i3);
68a1a6c0 5171 if (i2 == NULL)
02529902
KH
5172 return 0;
5173 i1 = prev_nonnote_insn (i2);
68a1a6c0 5174 if (i1 == NULL)
02529902
KH
5175 return 0;
5176
5177 return (INSN_P (i1) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5178 && any_condjump_p (i2) && onlyjump_p (i2));
5179}
c87ec0ba 5180
c06d5c85
KH
5181/* Return nonzero if we have the same comparison insn as I1 two insns
5182 after I1. I1 is assumed to be a comparison insn. */
5183
5184int
82082f65 5185same_cmp_following_p (rtx_insn *i1)
c06d5c85 5186{
68a1a6c0 5187 rtx_insn *i2, *i3;
c06d5c85
KH
5188
5189 /* Make sure we have a sequence of three insns. */
5190 i2 = next_nonnote_insn (i1);
68a1a6c0 5191 if (i2 == NULL)
c06d5c85
KH
5192 return 0;
5193 i3 = next_nonnote_insn (i2);
68a1a6c0 5194 if (i3 == NULL)
c06d5c85
KH
5195 return 0;
5196
5197 return (INSN_P (i3) && rtx_equal_p (PATTERN (i1), PATTERN (i3))
5198 && any_condjump_p (i2) && onlyjump_p (i2));
5199}
5200
a466bea3 5201/* Return nonzero if OPERANDS are valid for stm (or ldm) that pushes
1ae58c30 5202 (or pops) N registers. OPERANDS are assumed to be an array of
a466bea3
KH
5203 registers. */
5204
5205int
5206h8300_regs_ok_for_stm (int n, rtx operands[])
5207{
5208 switch (n)
5209 {
5210 case 2:
5211 return ((REGNO (operands[0]) == 0 && REGNO (operands[1]) == 1)
5212 || (REGNO (operands[0]) == 2 && REGNO (operands[1]) == 3)
5213 || (REGNO (operands[0]) == 4 && REGNO (operands[1]) == 5));
5214 case 3:
5215 return ((REGNO (operands[0]) == 0
5216 && REGNO (operands[1]) == 1
5217 && REGNO (operands[2]) == 2)
5218 || (REGNO (operands[0]) == 4
5219 && REGNO (operands[1]) == 5
5220 && REGNO (operands[2]) == 6));
5221
5222 case 4:
5223 return (REGNO (operands[0]) == 0
5224 && REGNO (operands[1]) == 1
5225 && REGNO (operands[2]) == 2
5226 && REGNO (operands[3]) == 3);
8c440872
NS
5227 default:
5228 gcc_unreachable ();
a466bea3 5229 }
a466bea3
KH
5230}
5231
c87ec0ba
NY
5232/* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
5233
5234int
5235h8300_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
5236 unsigned int new_reg)
5237{
5238 /* Interrupt functions can only use registers that have already been
5239 saved by the prologue, even if they would normally be
5240 call-clobbered. */
5241
5242 if (h8300_current_function_interrupt_function_p ()
6fb5fa3c 5243 && !df_regs_ever_live_p (new_reg))
c87ec0ba
NY
5244 return 0;
5245
80e58519 5246 return 1;
c87ec0ba 5247}
d0022200 5248
2e762884
DD
5249/* Returns true if register REGNO is safe to be allocated as a scratch
5250 register in the current function. */
5251
5252static bool
5253h8300_hard_regno_scratch_ok (unsigned int regno)
5254{
5255 if (h8300_current_function_interrupt_function_p ()
5256 && ! WORD_REG_USED (regno))
5257 return false;
5258
5259 return true;
5260}
5261
5262
d0022200
KH
5263/* Return nonzero if X is a REG or SUBREG suitable as a base register. */
5264
5265static int
5266h8300_rtx_ok_for_base_p (rtx x, int strict)
5267{
5268 /* Strip off SUBREG if any. */
5269 if (GET_CODE (x) == SUBREG)
5270 x = SUBREG_REG (x);
5271
5272 return (REG_P (x)
5273 && (strict
5274 ? REG_OK_FOR_BASE_STRICT_P (x)
5275 : REG_OK_FOR_BASE_NONSTRICT_P (x)));
5276}
5277
5278/* Return nozero if X is a legitimate address. On the H8/300, a
5279 legitimate address has the form REG, REG+CONSTANT_ADDRESS or
5280 CONSTANT_ADDRESS. */
5281
c6c3dba9 5282static bool
ef4bddc2 5283h8300_legitimate_address_p (machine_mode mode, rtx x, bool strict)
d0022200
KH
5284{
5285 /* The register indirect addresses like @er0 is always valid. */
5286 if (h8300_rtx_ok_for_base_p (x, strict))
5287 return 1;
5288
5289 if (CONSTANT_ADDRESS_P (x))
5290 return 1;
5291
beed8fc0
AO
5292 if (TARGET_H8300SX
5293 && ( GET_CODE (x) == PRE_INC
5294 || GET_CODE (x) == PRE_DEC
5295 || GET_CODE (x) == POST_INC
5296 || GET_CODE (x) == POST_DEC)
5297 && h8300_rtx_ok_for_base_p (XEXP (x, 0), strict))
5298 return 1;
5299
d0022200
KH
5300 if (GET_CODE (x) == PLUS
5301 && CONSTANT_ADDRESS_P (XEXP (x, 1))
beed8fc0
AO
5302 && h8300_rtx_ok_for_base_p (h8300_get_index (XEXP (x, 0),
5303 mode, 0), strict))
d0022200
KH
5304 return 1;
5305
5306 return 0;
5307}
07ee3b58 5308
f939c3e6 5309/* Implement TARGET_HARD_REGNO_MODE_OK. */
07ee3b58 5310
f939c3e6
RS
5311static bool
5312h8300_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
07ee3b58 5313{
27c1afd5
JL
5314 /* MAC register can only be of SImode. Otherwise, anything
5315 goes. */
5316 return regno == MAC_REG ? mode == SImode : 1;
07ee3b58 5317}
f9b4f8c1 5318
99e1629f
RS
5319/* Implement TARGET_MODES_TIEABLE_P. */
5320
5321static bool
5322h8300_modes_tieable_p (machine_mode mode1, machine_mode mode2)
5323{
5324 return (mode1 == mode2
5325 || ((mode1 == QImode
5326 || mode1 == HImode
27c1afd5 5327 || mode1 == SImode)
99e1629f
RS
5328 && (mode2 == QImode
5329 || mode2 == HImode
27c1afd5 5330 || mode2 == SImode)));
99e1629f
RS
5331}
5332
f9b4f8c1
RH
5333/* Helper function for the move patterns. Make sure a move is legitimate. */
5334
5335bool
5336h8300_move_ok (rtx dest, rtx src)
5337{
5338 rtx addr, other;
5339
5340 /* Validate that at least one operand is a register. */
5341 if (MEM_P (dest))
5342 {
5343 if (MEM_P (src) || CONSTANT_P (src))
5344 return false;
5345 addr = XEXP (dest, 0);
5346 other = src;
5347 }
5348 else if (MEM_P (src))
5349 {
5350 addr = XEXP (src, 0);
5351 other = dest;
5352 }
5353 else
5354 return true;
5355
5356 /* Validate that auto-inc doesn't affect OTHER. */
5357 if (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC)
5358 return true;
5359 addr = XEXP (addr, 0);
5360
5361 if (addr == stack_pointer_rtx)
5362 return register_no_sp_elim_operand (other, VOIDmode);
5363 else
5364 return !reg_overlap_mentioned_p(other, addr);
5365}
6e014ef3 5366\f
c15c90bb
ZW
5367/* Perform target dependent optabs initialization. */
5368static void
5369h8300_init_libfuncs (void)
5370{
5371 set_optab_libfunc (smul_optab, HImode, "__mulhi3");
5372 set_optab_libfunc (sdiv_optab, HImode, "__divhi3");
5373 set_optab_libfunc (udiv_optab, HImode, "__udivhi3");
5374 set_optab_libfunc (smod_optab, HImode, "__modhi3");
5375 set_optab_libfunc (umod_optab, HImode, "__umodhi3");
5376}
5377\f
9eaa7740
AS
5378/* Worker function for TARGET_FUNCTION_VALUE.
5379
5380 On the H8 the return value is in R0/R1. */
5381
5382static rtx
5383h8300_function_value (const_tree ret_type,
5384 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
5385 bool outgoing ATTRIBUTE_UNUSED)
5386{
5387 return gen_rtx_REG (TYPE_MODE (ret_type), R0_REG);
5388}
5389
5390/* Worker function for TARGET_LIBCALL_VALUE.
5391
5392 On the H8 the return value is in R0/R1. */
5393
5394static rtx
ef4bddc2 5395h8300_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
9eaa7740
AS
5396{
5397 return gen_rtx_REG (mode, R0_REG);
5398}
5399
5400/* Worker function for TARGET_FUNCTION_VALUE_REGNO_P.
5401
5402 On the H8, R0 is the only register thus used. */
5403
5404static bool
5405h8300_function_value_regno_p (const unsigned int regno)
5406{
5407 return (regno == R0_REG);
5408}
5409
e9eba255
KH
5410/* Worker function for TARGET_RETURN_IN_MEMORY. */
5411
34bf1fe3 5412static bool
586de218 5413h8300_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
34bf1fe3
KH
5414{
5415 return (TYPE_MODE (type) == BLKmode
27c1afd5 5416 || GET_MODE_SIZE (TYPE_MODE (type)) > 8);
34bf1fe3
KH
5417}
5418\f
9f6ef043
RH
5419/* We emit the entire trampoline here. Depending on the pointer size,
5420 we use a different trampoline.
5421
5422 Pmode == HImode
5423 vvvv context
5424 1 0000 7903xxxx mov.w #0x1234,r3
5425 2 0004 5A00xxxx jmp @0x1234
5426 ^^^^ function
5427
5428 Pmode == SImode
5429 vvvvvvvv context
5430 2 0000 7A03xxxxxxxx mov.l #0x12345678,er3
5431 3 0006 5Axxxxxx jmp @0x123456
5432 ^^^^^^ function
5433*/
5434
5435static void
5436h8300_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
5437{
5438 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
5439 rtx mem;
5440
5441 if (Pmode == HImode)
5442 {
5443 mem = adjust_address (m_tramp, HImode, 0);
5444 emit_move_insn (mem, GEN_INT (0x7903));
5445 mem = adjust_address (m_tramp, Pmode, 2);
5446 emit_move_insn (mem, cxt);
5447 mem = adjust_address (m_tramp, HImode, 4);
5448 emit_move_insn (mem, GEN_INT (0x5a00));
5449 mem = adjust_address (m_tramp, Pmode, 6);
5450 emit_move_insn (mem, fnaddr);
5451 }
5452 else
5453 {
5454 rtx tem;
5455
5456 mem = adjust_address (m_tramp, HImode, 0);
5457 emit_move_insn (mem, GEN_INT (0x7a03));
5458 mem = adjust_address (m_tramp, Pmode, 2);
5459 emit_move_insn (mem, cxt);
5460
5461 tem = copy_to_reg (fnaddr);
5462 emit_insn (gen_andsi3 (tem, tem, GEN_INT (0x00ffffff)));
5463 emit_insn (gen_iorsi3 (tem, tem, GEN_INT (0x5a000000)));
5464 mem = adjust_address (m_tramp, SImode, 6);
5465 emit_move_insn (mem, tem);
5466 }
5467}
7b4df2bf
RS
5468
5469/* Implement PUSH_ROUNDING.
5470
5471 On the H8/300, @-sp really pushes a byte if you ask it to - but that's
5472 dangerous, so we claim that it always pushes a word, then we catch
5473 the mov.b rx,@-sp and turn it into a mov.w rx,@-sp on output.
5474
5475 On the H8/300H, we simplify TARGET_QUICKCALL by setting this to 4
5476 and doing a similar thing. */
5477
5478poly_int64
5479h8300_push_rounding (poly_int64 bytes)
5480{
5481 return ((bytes + PARM_BOUNDARY / 8 - 1) & (-PARM_BOUNDARY / 8));
5482}
9f6ef043 5483\f
6e014ef3
KH
5484/* Initialize the GCC target structure. */
5485#undef TARGET_ATTRIBUTE_TABLE
5486#define TARGET_ATTRIBUTE_TABLE h8300_attribute_table
5487
5488#undef TARGET_ASM_ALIGNED_HI_OP
5489#define TARGET_ASM_ALIGNED_HI_OP "\t.word\t"
5490
1bc7c5b6
ZW
5491#undef TARGET_ASM_FILE_START
5492#define TARGET_ASM_FILE_START h8300_file_start
5493#undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
5494#define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
5495
6e014ef3
KH
5496#undef TARGET_ASM_FILE_END
5497#define TARGET_ASM_FILE_END h8300_file_end
5498
88cb339e
N
5499#undef TARGET_PRINT_OPERAND
5500#define TARGET_PRINT_OPERAND h8300_print_operand
5501#undef TARGET_PRINT_OPERAND_ADDRESS
5502#define TARGET_PRINT_OPERAND_ADDRESS h8300_print_operand_address
5503#undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
5504#define TARGET_PRINT_OPERAND_PUNCT_VALID_P h8300_print_operand_punct_valid_p
5505
6e014ef3
KH
5506#undef TARGET_ENCODE_SECTION_INFO
5507#define TARGET_ENCODE_SECTION_INFO h8300_encode_section_info
5508
5509#undef TARGET_INSERT_ATTRIBUTES
5510#define TARGET_INSERT_ATTRIBUTES h8300_insert_attributes
5511
88cb339e
N
5512#undef TARGET_REGISTER_MOVE_COST
5513#define TARGET_REGISTER_MOVE_COST h8300_register_move_cost
5514
6e014ef3
KH
5515#undef TARGET_RTX_COSTS
5516#define TARGET_RTX_COSTS h8300_rtx_costs
5517
c15c90bb
ZW
5518#undef TARGET_INIT_LIBFUNCS
5519#define TARGET_INIT_LIBFUNCS h8300_init_libfuncs
5520
9eaa7740
AS
5521#undef TARGET_FUNCTION_VALUE
5522#define TARGET_FUNCTION_VALUE h8300_function_value
5523
5524#undef TARGET_LIBCALL_VALUE
5525#define TARGET_LIBCALL_VALUE h8300_libcall_value
5526
5527#undef TARGET_FUNCTION_VALUE_REGNO_P
5528#define TARGET_FUNCTION_VALUE_REGNO_P h8300_function_value_regno_p
5529
34bf1fe3
KH
5530#undef TARGET_RETURN_IN_MEMORY
5531#define TARGET_RETURN_IN_MEMORY h8300_return_in_memory
5532
56f9413b
NF
5533#undef TARGET_FUNCTION_ARG
5534#define TARGET_FUNCTION_ARG h8300_function_arg
5535
5536#undef TARGET_FUNCTION_ARG_ADVANCE
5537#define TARGET_FUNCTION_ARG_ADVANCE h8300_function_arg_advance
5538
beed8fc0
AO
5539#undef TARGET_MACHINE_DEPENDENT_REORG
5540#define TARGET_MACHINE_DEPENDENT_REORG h8300_reorg
5541
2e762884
DD
5542#undef TARGET_HARD_REGNO_SCRATCH_OK
5543#define TARGET_HARD_REGNO_SCRATCH_OK h8300_hard_regno_scratch_ok
5544
f939c3e6
RS
5545#undef TARGET_HARD_REGNO_MODE_OK
5546#define TARGET_HARD_REGNO_MODE_OK h8300_hard_regno_mode_ok
5547
99e1629f
RS
5548#undef TARGET_MODES_TIEABLE_P
5549#define TARGET_MODES_TIEABLE_P h8300_modes_tieable_p
5550
d81db636
SB
5551#undef TARGET_LRA_P
5552#define TARGET_LRA_P hook_bool_void_false
5553
c6c3dba9
PB
5554#undef TARGET_LEGITIMATE_ADDRESS_P
5555#define TARGET_LEGITIMATE_ADDRESS_P h8300_legitimate_address_p
5556
7b5cbb57
AS
5557#undef TARGET_CAN_ELIMINATE
5558#define TARGET_CAN_ELIMINATE h8300_can_eliminate
5559
5efd84c5
NF
5560#undef TARGET_CONDITIONAL_REGISTER_USAGE
5561#define TARGET_CONDITIONAL_REGISTER_USAGE h8300_conditional_register_usage
5562
9f6ef043
RH
5563#undef TARGET_TRAMPOLINE_INIT
5564#define TARGET_TRAMPOLINE_INIT h8300_trampoline_init
5565
c5387660
JM
5566#undef TARGET_OPTION_OVERRIDE
5567#define TARGET_OPTION_OVERRIDE h8300_option_override
5568
f52d97da
AS
5569#undef TARGET_MODE_DEPENDENT_ADDRESS_P
5570#define TARGET_MODE_DEPENDENT_ADDRESS_P h8300_mode_dependent_address_p
5571
efbf3c34
JL
5572#undef TARGET_HAVE_SPECULATION_SAFE_VALUE
5573#define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
5574
f16897cb
AL
5575#undef TARGET_FLAGS_REGNUM
5576#define TARGET_FLAGS_REGNUM 12
5577
6e014ef3 5578struct gcc_target targetm = TARGET_INITIALIZER;