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