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