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