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