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