]> gcc.gnu.org Git - gcc.git/blob - gcc/config/h8300/h8300.c
h8300.c (cpu_type, [...]): New variables.
[gcc.git] / gcc / config / h8300 / h8300.c
1 /* Subroutines for insn-output.c for Hitachi H8/300.
2 Copyright (C) 1992, 1993, 1994 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 GNU CC.
7
8 GNU CC 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 2, or (at your option)
11 any later version.
12
13 GNU CC 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 GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include <stdio.h>
23 #include "config.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "hard-reg-set.h"
27 #include "real.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "expr.h"
36 #include "tree.h"
37
38 /* Forward declarations. */
39 void print_operand_address ();
40 char *index ();
41
42 /* CPU_TYPE, says what cpu we're compiling for. */
43 int cpu_type;
44
45 /* True if a #pragma interrupt has been seen for the current function. */
46 int pragma_interrupt;
47
48 /* True if a #pragma saveall has been seen for the current function. */
49 int pragma_saveall;
50
51 static char *names_big[] =
52 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7"};
53
54 static char *names_extended[] =
55 {"er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"};
56
57 static char *names_upper_extended[] =
58 {"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"};
59
60 /* Points to one of the above. */
61 /* ??? The above could be put in an array indexed by CPU_TYPE. */
62 char **h8_reg_names;
63
64 /* Various operations needed by the following, indexed by CPU_TYPE. */
65 /* ??? The h8/300 assembler doesn't understand pop.w (yet). */
66
67 static char *h8_push_ops[2] =
68 {"push", "push.l"};
69 static char *h8_pop_ops[2] =
70 {"pop", "pop.l"};
71 static char *h8_mov_ops[2] =
72 {"mov.w", "mov.l"};
73
74 char *h8_push_op, *h8_pop_op, *h8_mov_op;
75
76 /* Initialize various cpu specific globals at start up. */
77
78 void
79 h8300_init_once ()
80 {
81 if (TARGET_H8300)
82 {
83 cpu_type = (int) CPU_H8300;
84 h8_reg_names = names_big;
85 }
86 else
87 {
88 cpu_type = (int) CPU_H8300H;
89 h8_reg_names = names_extended;
90 }
91 h8_push_op = h8_push_ops[cpu_type];
92 h8_pop_op = h8_pop_ops[cpu_type];
93 h8_mov_op = h8_mov_ops[cpu_type];
94 }
95
96 char *
97 byte_reg (x, b)
98 rtx x;
99 int b;
100 {
101 static char *names_small[] =
102 {"r0l", "r0h", "r1l", "r1h", "r2l", "r2h", "r3l", "r3h",
103 "r4l", "r4h", "r5l", "r5h", "r6l", "r6h", "r7lBAD", "r7hBAD"};
104
105 return names_small[REGNO (x) * 2 + b];
106 }
107
108 /* REGNO must be saved/restored across calls if this macro is true. */
109
110 #define WORD_REG_USED(regno) \
111 (regno < 7 && \
112 (pragma_interrupt \
113 || pragma_saveall \
114 || (regno == FRAME_POINTER_REGNUM && regs_ever_live[regno]) \
115 || (regs_ever_live[regno] & !call_used_regs[regno])))
116
117 /* Output assembly language to FILE for the operation OP with operand size
118 SIZE to adjust the stack pointer. */
119 /* ??? FPED is currently unused. */
120
121 static void
122 dosize (file, op, size, fped)
123 FILE *file;
124 char *op;
125 unsigned int size;
126 int fped;
127 {
128 switch (size)
129 {
130 case 4:
131 /* ??? TARGET_H8300H can do this in one insn. */
132 case 3:
133 fprintf (file, "\t%ss\t#%d,sp\n", op, 2);
134 size -= 2;
135 /* Fall through... */
136 case 2:
137 case 1:
138 fprintf (file, "\t%ss\t#%d,sp\n", op, size);
139 size = 0;
140 break;
141 case 0:
142 break;
143 default:
144 if (TARGET_H8300)
145 fprintf (file, "\tmov.w\t#%d,r3\n\t%s.w\tr3,sp\n", size, op);
146 else
147 fprintf (file, "\t%s\t#%d,sp\n", op, size);
148 size = 0;
149 break;
150 }
151 }
152
153 /* Output assembly language code for the function prologue. */
154 static int push_order[FIRST_PSEUDO_REGISTER] =
155 {6, 5, 4, 3, 2, 1, 0, -1, -1};
156 static int pop_order[FIRST_PSEUDO_REGISTER] =
157 {0, 1, 2, 3, 4, 5, 6, -1, -1};
158
159 /* This is what the stack looks like after the prolog of
160 a function with a frame has been set up:
161
162 <args>
163 PC
164 FP <- fp
165 <locals>
166 <saved registers> <- sp
167
168 This is what the stack looks like after the prolog of
169 a function which doesn't have a frame:
170
171 <args>
172 PC
173 <locals>
174 <saved registers> <- sp
175 */
176
177 int current_function_anonymous_args;
178
179 /* Extra arguments to pop, in words (IE: 2 bytes for 300, 4 for 300h */
180 static int extra_pop;
181
182 void
183 function_prologue (file, size)
184 FILE *file;
185 int size;
186 {
187 register int mask = 0;
188 int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
189 int idx;
190 extra_pop = 0;
191
192 if (current_function_anonymous_args && TARGET_QUICKCALL)
193 {
194 /* Push regs as if done by caller, and move around return address. */
195
196 switch (current_function_args_info.nbytes / UNITS_PER_WORD)
197 {
198 case 0:
199 /* get ret addr */
200 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
201 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
202 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[1]);
203 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[0]);
204 /* push it again */
205 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
206 extra_pop = 3;
207 break;
208 case 1:
209 /* get ret addr */
210 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
211 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
212 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[1]);
213 /* push it again */
214 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
215 extra_pop = 2;
216 break;
217 case 2:
218 /* get ret addr */
219 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
220 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[2]);
221 /* push it again */
222 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[3]);
223 extra_pop = 1;
224 break;
225 default:
226 fprintf (file, "; varargs\n");
227 break;
228 }
229 }
230
231 if (frame_pointer_needed)
232 {
233 /* Push fp */
234 fprintf (file, "\t%s\t%s\n", h8_push_op,
235 h8_reg_names[FRAME_POINTER_REGNUM]);
236 fprintf (file, "\t%s\t%s,%s\n", h8_mov_op,
237 h8_reg_names[STACK_POINTER_REGNUM],
238 h8_reg_names[FRAME_POINTER_REGNUM]);
239
240 /* leave room for locals */
241 dosize (file, "sub", fsize, 1);
242
243 /* Push the rest of the registers */
244 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
245 {
246 int regno = push_order[idx];
247
248 if (regno >= 0 && WORD_REG_USED (regno) && regno != FRAME_POINTER_REGNUM)
249 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[regno]);
250 }
251 }
252 else
253 {
254 dosize (file, "sub", fsize, 0);
255 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
256 {
257 int regno = push_order[idx];
258
259 if (regno >= 0 && WORD_REG_USED (regno))
260 fprintf (file, "\t%s\t%s\n", h8_push_op, h8_reg_names[regno]);
261 }
262 }
263 }
264
265 /* Output assembly language code for the function epilogue. */
266
267 void
268 function_epilogue (file, size)
269 FILE *file;
270 int size;
271 {
272 register int regno;
273 register int mask = 0;
274 int fsize = (size + STACK_BOUNDARY / 8 - 1) & -STACK_BOUNDARY / 8;
275 int nregs;
276 int offset;
277 int idx;
278 rtx insn = get_last_insn ();
279
280 /* If the last insn was a BARRIER, we don't have to write any code. */
281 if (GET_CODE (insn) == NOTE)
282 insn = prev_nonnote_insn (insn);
283 if (insn && GET_CODE (insn) == BARRIER)
284 return;
285
286 nregs = 0;
287
288 if (frame_pointer_needed)
289 {
290 /* Pop saved registers */
291 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
292 {
293 regno = pop_order[idx];
294 if (regno >= 0 && regno != FRAME_POINTER_REGNUM && WORD_REG_USED (regno))
295 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[regno]);
296 }
297 /* deallocate locals */
298 dosize (file, "add", fsize, 1);
299 /* pop frame pointer */
300 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[FRAME_POINTER_REGNUM]);
301 }
302 else
303 {
304 /* pop saved registers */
305 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
306 {
307 regno = pop_order[idx];
308 if (regno >= 0 && WORD_REG_USED (regno))
309 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[regno]);
310 }
311 /* deallocate locals */
312 dosize (file, "add", fsize, 0);
313 }
314
315 if (extra_pop)
316 {
317 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[3]);
318 while (extra_pop)
319 {
320 fprintf (file, "\t%s\t%s\n", h8_pop_op, h8_reg_names[2]);
321 extra_pop--;
322 }
323 fprintf (file, "\tjmp @%s\n", h8_reg_names[3]);
324 }
325 else
326 {
327 if (pragma_interrupt)
328 fprintf (file, "\trte\n");
329 else
330 fprintf (file, "\trts\n");
331 }
332
333 pragma_interrupt = 0;
334 pragma_saveall = 0;
335
336 current_function_anonymous_args = 0;
337 }
338
339 /* Output assembly code for the start of the file. */
340
341 asm_file_start (file)
342 FILE *file;
343 {
344 fprintf (file, ";\tGCC For the Hitachi H8/300\n");
345 fprintf (file, ";\tBy Hitachi America Ltd and Cygnus Support\n");
346 fprintf (file, ";\trelease F-1\n");
347 if (optimize)
348 fprintf (file, "; -O%d\n", optimize);
349 if (TARGET_H8300H)
350 fprintf (file, "\n\t.h8300h\n");
351 else
352 fprintf (file, "\n\n");
353 output_file_directive (file, main_input_filename);
354 }
355
356 /* Output assembly language code for the end of file. */
357
358 void
359 asm_file_end (file)
360 FILE *file;
361 {
362 fprintf (file, "\t.end\n");
363 }
364 \f
365 /* Return true if VALUE is a valid constant for constraint 'P'.
366 IE: VALUE is a power of two <= 2**15. */
367
368 int
369 small_power_of_two (value)
370 int value;
371 {
372 switch (value)
373 {
374 case 1:
375 case 2:
376 case 4:
377 case 8:
378 case 16:
379 case 32:
380 case 64:
381 case 128:
382 case 256:
383 case 512:
384 case 1024:
385 case 2048:
386 case 4096:
387 case 8192:
388 case 16384:
389 case 32768:
390 return 1;
391 }
392 return 0;
393 }
394
395 /* Return true if VALUE is a valid constant for constraint 'O', which
396 means that the constant would be ok to use as a bit for a bclr
397 instruction. */
398
399 int
400 ok_for_bclr (value)
401 int value;
402 {
403 return small_power_of_two ((~value) & 0xff);
404 }
405
406 /* Return true is OP is a valid source operand for an integer move
407 instruction. */
408
409 int
410 general_operand_src (op, mode)
411 rtx op;
412 enum machine_mode mode;
413 {
414 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
415 return 1;
416 return general_operand (op, mode);
417 }
418
419 /* Return true if OP is a valid destination operand for an integer move
420 instruction. */
421
422 int
423 general_operand_dst (op, mode)
424 rtx op;
425 enum machine_mode mode;
426 {
427 if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == PRE_DEC)
428 return 1;
429 return general_operand (op, mode);
430 }
431
432 /* Return true if OP is a const valid for a bit clear instruction. */
433
434 int
435 o_operand (operand, mode)
436 rtx operand;
437 enum machine_mode mode;
438 {
439 return (GET_CODE (operand) == CONST_INT
440 && CONST_OK_FOR_O (INTVAL (operand)));
441 }
442
443 /* Return true if OP is a const valid for a bit set or bit xor instruction. */
444
445 int
446 p_operand (operand, mode)
447 rtx operand;
448 enum machine_mode mode;
449 {
450 return (GET_CODE (operand) == CONST_INT
451 && CONST_OK_FOR_P (INTVAL (operand)));
452 }
453
454 /* Return true if OP is a valid call operand. */
455
456 int
457 call_insn_operand (op, mode)
458 rtx op;
459 enum machine_mode mode;
460 {
461 if (GET_CODE (op) == MEM)
462 {
463 rtx inside = XEXP (op, 0);
464 if (register_operand (inside, Pmode))
465 return 1;
466 if (CONSTANT_ADDRESS_P (inside))
467 return 1;
468 }
469 return 0;
470 }
471
472 /* Return true if OP is a valid jump operand. */
473
474 int
475 jump_address_operand (op, mode)
476 rtx op;
477 enum machine_mode mode;
478 {
479 if (GET_CODE (op) == REG)
480 return mode == Pmode;
481
482 if (GET_CODE (op) == MEM)
483 {
484 rtx inside = XEXP (op, 0);
485 if (register_operand (inside, Pmode))
486 return 1;
487 if (CONSTANT_ADDRESS_P (inside))
488 return 1;
489 }
490 return 0;
491 }
492
493 /* Recognize valid operands for bitfield instructions. */
494
495 extern int rtx_equal_function_value_matters;
496
497 int
498 bit_operand (op, mode)
499 rtx op;
500 enum machine_mode mode;
501 {
502 /* We can except any general operand, expept that MEM operands must
503 be limited to those that use addresses valid for the 'U' constraint. */
504 if (!general_operand (op, mode))
505 return 0;
506
507 /* Accept any mem during RTL generation. Otherwise, the code that does
508 insv and extzv will think that we can not handle memory. However,
509 to avoid reload problems, we only accept 'U' MEM operands after RTL
510 generation. This means that any named pattern which uses this predicate
511 must force its operands to match 'U' before emitting RTL. */
512
513 if (GET_CODE (op) == REG)
514 return 1;
515 if (GET_CODE (op) == SUBREG)
516 return 1;
517 if (!rtx_equal_function_value_matters)
518 {
519 /* We're building rtl */
520 return GET_CODE (op) == MEM;
521 }
522 else
523 {
524 return (GET_CODE (op) == MEM
525 && EXTRA_CONSTRAINT (op, 'U'));
526 }
527 }
528
529 /* Recognize valid operators for bit test. */
530
531 int
532 eq_operator (x, mode)
533 rtx x;
534 enum machine_mode mode;
535 {
536 return (GET_CODE (x) == EQ || GET_CODE (x) == NE);
537 }
538
539 /* Handle machine specific pragmas for compatibility with existing
540 compilers for the H8/300.
541
542 pragma saveall generates prolog/epilog code which saves and
543 restores all the registers on function entry.
544
545 pragma interrupt saves and restores all registers, and exits with
546 an rte instruction rather than an rts. A pointer to a function
547 with this attribute may be safely used in an interrupt vector. */
548
549 int
550 handle_pragma (file)
551 FILE *file;
552 {
553 int c;
554 char pbuf[20];
555 int psize = 0;
556
557 c = getc (file);
558 while (c == ' ' || c == '\t')
559 c = getc (file);
560
561 if (c == '\n' || c == EOF)
562 return c;
563
564 /* The only pragmas we understand are interrupt and saveall. */
565 while (psize < sizeof (pbuf) - 1
566 && isalpha (c))
567 {
568 pbuf[psize++] = c;
569 c = getc (file);
570 }
571 pbuf[psize] = 0;
572
573 if (strcmp (pbuf, "interrupt") == 0)
574 pragma_interrupt = 1;
575
576 if (strcmp (pbuf, "saveall") == 0)
577 pragma_saveall = 1;
578
579 /* ??? This is deprecated. Use section attributes. */
580 if (strcmp (pbuf, "section") == 0)
581 {
582 while (c && !isalpha (c))
583 c = getc (file);
584 psize = 0;
585 while (psize < sizeof (pbuf) - 1
586 && isalpha (c) || isdigit (c) || c == '_')
587 {
588 pbuf[psize++] = c;
589 c = getc (file);
590 }
591 pbuf[psize] = 0;
592 named_section (pbuf);
593 }
594 ungetc (c, file);
595 return c;
596 }
597 \f
598 /* If the next arg with MODE and TYPE is to be passed in a register, return
599 the rtx to represent where it is passed. CUM represents the state after
600 the last argument. NAMED is not used. */
601
602 static char *hand_list[] =
603 {
604 "__main",
605 "__cmpsi2",
606 "__divhi3",
607 "__modhi3",
608 "__udivhi3",
609 "__umodhi3",
610 "__divsi3",
611 "__modsi3",
612 "__udivsi3",
613 "__umodsi3",
614 "__mulhi3",
615 "__mulsi3",
616 "__reg_memcpy",
617 "__reg_memset",
618 "__ucmpsi2",
619 0,
620 };
621
622 /* Return an RTX to represent where a value with mode MODE will be returned
623 from a function. If the result is 0, the argument is pushed. */
624
625 rtx
626 function_arg (cum, mode, type, named)
627 CUMULATIVE_ARGS *cum;
628 enum machine_mode mode;
629 tree type;
630 int named;
631 {
632 rtx result = 0;
633 char *fname;
634 int regpass = 0;
635
636 /* Pass 3 regs worth of data in regs when user asked on the command line. */
637 if (TARGET_QUICKCALL)
638 regpass = 3;
639
640 /* If calling hand written assembler, use 4 regs of args. */
641
642 if (cum->libcall)
643 {
644 char **p;
645
646 fname = XSTR (cum->libcall, 0);
647
648 /* See if this libcall is one of the hand coded ones. */
649
650 for (p = hand_list; *p && strcmp (*p, fname) != 0; p++)
651 ;
652
653 if (*p)
654 regpass = 4;
655 }
656
657 if (regpass)
658 {
659 int size;
660
661 if (mode == BLKmode)
662 size = int_size_in_bytes (type);
663 else
664 size = GET_MODE_SIZE (mode);
665
666 if (size + cum->nbytes > regpass * UNITS_PER_WORD)
667 {
668 result = 0;
669 }
670 else
671 {
672 switch (cum->nbytes / UNITS_PER_WORD)
673 {
674 case 0:
675 result = gen_rtx (REG, mode, 0);
676 break;
677 case 1:
678 result = gen_rtx (REG, mode, 1);
679 break;
680 case 2:
681 result = gen_rtx (REG, mode, 2);
682 break;
683 case 3:
684 result = gen_rtx (REG, mode, 3);
685 break;
686 default:
687 result = 0;
688 }
689 }
690 }
691
692 return result;
693 }
694 \f
695 /* Return the cost of the rtx R with code CODE. */
696
697 int
698 const_costs (r, c)
699 rtx r;
700 enum rtx_code c;
701 {
702 switch (c)
703 {
704 case CONST_INT:
705 switch (INTVAL (r))
706 {
707 case 0:
708 case 1:
709 case 2:
710 case -1:
711 case -2:
712 return 0;
713 default:
714 return 1;
715 }
716
717 case CONST:
718 case LABEL_REF:
719 case SYMBOL_REF:
720 return 3;
721
722 case CONST_DOUBLE:
723 return 20;
724
725 default:
726 return 4;
727 }
728 }
729 \f
730 /* Documentation for the machine specific operand escapes:
731
732 'A' print rn in h8/300 mode, erN in H8/300H mode
733 'C' print (operand - 2).
734 'E' like s but negative.
735 'F' like t but negative.
736 'G' constant just the negative
737 'L' fake label, changed after used twice.
738 'M' turn a 'M' constant into its negative mod 2.
739 'P' if operand is incing/decing sp, print .w, otherwise .b.
740 'S' print operand as a long word
741 'T' print operand as a word
742 'U' if operand is incing/decing sp, print l, otherwise nothing.
743 'V' find the set bit, and print its number.
744 'W' find the clear bit, and print its number.
745 'X' print operand as a byte
746 'Y' print either l or h depending on whether last 'Z' operand < 8 or >= 8.
747 'Z' print int & 7.
748 'b' print the bit opcode
749 'c' print the ibit opcode
750 'd' bcc if EQ, bcs if NE
751 'e' first word of 32 bit value - if reg, then least reg. if mem
752 then least. if const then most sig word
753 'f' second word of 32 bit value - if reg, then biggest reg. if mem
754 then +2. if const then least sig word
755 'g' bcs if EQ, bcc if NE
756 'j' print operand as condition code.
757 'k' print operand as reverse condition code.
758 's' print as low byte of 16 bit value
759 't' print as high byte of 16 bit value
760 'w' print as low byte of 32 bit value
761 'x' print as 2nd byte of 32 bit value
762 'y' print as 3rd byte of 32 bit value
763 'z' print as msb of 32 bit value
764 */
765
766 /* Return assembly language string which identifies a comparison type. */
767
768 static char *
769 cond_string (code)
770 enum rtx_code code;
771 {
772 switch (code)
773 {
774 case NE:
775 if (cc_prev_status.flags & CC_DONE_CBIT)
776 return "cs";
777 return "ne";
778 case EQ:
779 if (cc_prev_status.flags & CC_DONE_CBIT)
780 return "cc";
781 return "eq";
782 case GE:
783 return "ge";
784 case GT:
785 return "gt";
786 case LE:
787 return "le";
788 case LT:
789 return "lt";
790 case GEU:
791 return "hs";
792 case GTU:
793 return "hi";
794 case LEU:
795 return "ls";
796 case LTU:
797 return "lo";
798 default:
799 abort ();
800 }
801 }
802
803 /* Print operand X using operand code CODE to assembly language output file
804 FILE. */
805
806 void
807 print_operand (file, x, code)
808 FILE *file;
809 rtx x;
810 int code;
811 {
812 /* This is used to general unique labels for the 'L' code. */
813 static int lab = 1000;
814
815 /* This is used for communication between the 'P' and 'U' codes. */
816 static char *last_p;
817
818 /* This is used for communication between the 'Z' and 'Y' codes. */
819 /* ??? 'V' and 'W' use it too. */
820 static int bitint;
821
822 switch (code)
823 {
824 case 'A':
825 if (GET_CODE (x) == REG)
826 fprintf (file, "%s", h8_reg_names[REGNO (x)]);
827 else
828 goto def;
829 break;
830 case 'C':
831 fprintf (file, "#%d", INTVAL (x) - 2);
832 break;
833 case 'E':
834 switch (GET_CODE (x))
835 {
836 case REG:
837 fprintf (file, "%sl", names_big[REGNO (x)]);
838 break;
839 case CONST_INT:
840 fprintf (file, "#%d", (-INTVAL (x)) & 0xff);
841 break;
842 default:
843 abort ();
844 }
845 break;
846 case 'F':
847 switch (GET_CODE (x))
848 {
849 case REG:
850 fprintf (file, "%sh", names_big[REGNO (x)]);
851 break;
852 case CONST_INT:
853 fprintf (file, "#%d", ((-INTVAL (x)) & 0xff00) >> 8);
854 break;
855 default:
856 abort ();
857 }
858 break;
859 case 'G':
860 if (GET_CODE (x) != CONST_INT)
861 abort ();
862 fprintf (file, "#%d", 0xff & (-INTVAL (x)));
863 break;
864 case 'L':
865 /* 'L' must always be used twice in a single pattern. It generates
866 the same lable twice, and then will generate a unique label the
867 next time it is used. */
868 asm_fprintf (file, "tl%d", (lab++) / 2);
869 break;
870 case 'M':
871 /* For 3/-3 and 4/-4, the other 2 is handled separately. */
872 switch (INTVAL (x))
873 {
874 case 2:
875 case 4:
876 case -2:
877 case -4:
878 fprintf (file, "#2");
879 break;
880 case 1:
881 case 3:
882 case -1:
883 case -3:
884 fprintf (file, "#1");
885 break;
886 default:
887 abort ();
888 }
889 break;
890 case 'P':
891 if (REGNO (XEXP (XEXP (x, 0), 0)) == STACK_POINTER_REGNUM)
892 {
893 last_p = "";
894 fprintf (file, ".w");
895 }
896 else
897 {
898 last_p = "l";
899 fprintf (file, ".b");
900 }
901 break;
902 case 'S':
903 if (GET_CODE (x) == REG)
904 fprintf (file, "%s", names_extended[REGNO (x)]);
905 else
906 goto def;
907 break;
908 case 'T':
909 if (GET_CODE (x) == REG)
910 fprintf (file, "%s", names_big[REGNO (x)]);
911 else
912 goto def;
913 break;
914 case 'U':
915 fprintf (file, "%s%s", names_big[REGNO (x)], last_p);
916 break;
917 case 'V':
918 bitint = exact_log2 (INTVAL (x));
919 if (bitint == -1)
920 abort ();
921 fprintf (file, "#%d", bitint & 7);
922 break;
923 case 'W':
924 bitint = exact_log2 ((~INTVAL (x)) & 0xff);
925 if (bitint == -1)
926 abort ();
927 fprintf (file, "#%d", bitint & 7);
928 break;
929 case 'X':
930 if (GET_CODE (x) == REG)
931 fprintf (file, "%s", byte_reg (x, 0));
932 else
933 goto def;
934 break;
935 case 'Y':
936 if (bitint == -1)
937 abort ();
938 if (GET_CODE (x) == REG)
939 fprintf (file, "%s%c", names_big[REGNO (x)], bitint > 7 ? 'h' : 'l');
940 else
941 print_operand (file, x, 0);
942 bitint = -1;
943 break;
944 case 'Z':
945 bitint = INTVAL (x);
946 fprintf (file, "#%d", bitint & 7);
947 break;
948 case 'b':
949 switch (GET_CODE (x))
950 {
951 case IOR:
952 fprintf (file, "bor");
953 break;
954 case XOR:
955 fprintf (file, "bxor");
956 break;
957 case AND:
958 fprintf (file, "band");
959 break;
960 }
961 break;
962 case 'c':
963 switch (GET_CODE (x))
964 {
965 case IOR:
966 fprintf (file, "bior");
967 break;
968 case XOR:
969 fprintf (file, "bixor");
970 break;
971 case AND:
972 fprintf (file, "biand");
973 break;
974 }
975 break;
976 case 'd':
977 switch (GET_CODE (x))
978 {
979 case EQ:
980 fprintf (file, "bcc");
981 break;
982 case NE:
983 fprintf (file, "bcs");
984 break;
985 default:
986 abort ();
987 }
988 break;
989 case 'e':
990 switch (GET_CODE (x))
991 {
992 case REG:
993 if (TARGET_H8300)
994 fprintf (file, "%s", names_big[REGNO (x)]);
995 else
996 fprintf (file, "%s", names_upper_extended[REGNO (x)]);
997 break;
998 case MEM:
999 x = adj_offsettable_operand (x, 0);
1000 print_operand (file, x, 0);
1001 break;
1002 case CONST_INT:
1003 fprintf (file, "#%d", ((INTVAL (x) >> 16) & 0xffff));
1004 break;
1005 default:
1006 abort ();
1007 break;
1008 }
1009 break;
1010 case 'f':
1011 switch (GET_CODE (x))
1012 {
1013 case REG:
1014 if (TARGET_H8300)
1015 fprintf (file, "%s", names_big[REGNO (x) + 1]);
1016 else
1017 fprintf (file, "%s", names_big[REGNO (x)]);
1018 break;
1019 case MEM:
1020 x = adj_offsettable_operand (x, 2);
1021 print_operand (file, x, 0);
1022 break;
1023 case CONST_INT:
1024 fprintf (file, "#%d", INTVAL (x) & 0xffff);
1025 break;
1026 default:
1027 abort ();
1028 }
1029 break;
1030 case 'g':
1031 switch (GET_CODE (x))
1032 {
1033 case NE:
1034 fprintf (file, "bcc");
1035 break;
1036 case EQ:
1037 fprintf (file, "bcs");
1038 break;
1039 default:
1040 abort ();
1041 }
1042 break;
1043 case 'j':
1044 asm_fprintf (file, cond_string (GET_CODE (x)));
1045 break;
1046 case 'k':
1047 asm_fprintf (file, cond_string (reverse_condition (GET_CODE (x))));
1048 break;
1049 case 's':
1050 if (GET_CODE (x) == CONST_INT)
1051 fprintf (file, "#%d", (INTVAL (x)) & 0xff);
1052 else
1053 fprintf (file, "%s", byte_reg (x, 0));
1054 break;
1055 case 't':
1056 if (GET_CODE (x) == CONST_INT)
1057 fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1058 else
1059 fprintf (file, "%s", byte_reg (x, 1));
1060 break;
1061 case 'u':
1062 if (GET_CODE (x) != CONST_INT)
1063 abort ();
1064 fprintf (file, "%d", INTVAL (x));
1065 break;
1066 case 'w':
1067 if (GET_CODE (x) == CONST_INT)
1068 fprintf (file, "#%d", INTVAL (x) & 0xff);
1069 else
1070 fprintf (file, "%s", byte_reg (x, TARGET_H8300 ? 2 : 0));
1071 break;
1072 case 'x':
1073 if (GET_CODE (x) == CONST_INT)
1074 fprintf (file, "#%d", (INTVAL (x) >> 8) & 0xff);
1075 else
1076 fprintf (file, "%s", byte_reg (x, TARGET_H8300 ? 3 : 1));
1077 break;
1078 case 'y':
1079 if (GET_CODE (x) == CONST_INT)
1080 fprintf (file, "#%d", (INTVAL (x) >> 16) & 0xff);
1081 else
1082 fprintf (file, "%s", byte_reg (x, 0));
1083 break;
1084 case 'z':
1085 if (GET_CODE (x) == CONST_INT)
1086 fprintf (file, "#%d", (INTVAL (x) >> 24) & 0xff);
1087 else
1088 fprintf (file, "%s", byte_reg (x, 1));
1089 break;
1090
1091 default:
1092 def:
1093 switch (GET_CODE (x))
1094 {
1095 case REG:
1096 switch (GET_MODE (x))
1097 {
1098 case QImode:
1099 #if 0 /* Is it asm ("mov.b %0,r2l", ...) */
1100 fprintf (file, "%s", byte_reg (x, 0));
1101 #else /* ... or is it asm ("mov.b %0l,r2l", ...) */
1102 fprintf (file, "%s", names_big[REGNO (x)]);
1103 #endif
1104 break;
1105 case HImode:
1106 fprintf (file, "%s", names_big[REGNO (x)]);
1107 break;
1108 case SImode:
1109 fprintf (file, "%s", names_extended[REGNO (x)]);
1110 break;
1111 default:
1112 abort ();
1113 }
1114 break;
1115
1116 case MEM:
1117 fprintf (file, "@");
1118 output_address (XEXP (x, 0));
1119 break;
1120
1121 case CONST_INT:
1122 case SYMBOL_REF:
1123 case CONST:
1124 case LABEL_REF:
1125 fprintf (file, "#");
1126 print_operand_address (file, x);
1127 break;
1128 }
1129 }
1130 }
1131
1132 /* Output assembly language output for the address ADDR to FILE. */
1133
1134 void
1135 print_operand_address (file, addr)
1136 FILE *file;
1137 rtx addr;
1138 {
1139 switch (GET_CODE (addr))
1140 {
1141 case REG:
1142 fprintf (file, "%s", h8_reg_names[REGNO (addr)]);
1143 break;
1144
1145 case PRE_DEC:
1146 fprintf (file, "-%s", h8_reg_names[REGNO (XEXP (addr, 0))]);
1147 break;
1148
1149 case POST_INC:
1150 fprintf (file, "%s+", h8_reg_names[REGNO (XEXP (addr, 0))]);
1151 break;
1152
1153 case PLUS:
1154 fprintf (file, "(");
1155 if (GET_CODE (XEXP (addr, 0)) == REG)
1156 {
1157 /* reg,foo */
1158 print_operand_address (file, XEXP (addr, 1));
1159 fprintf (file, ",");
1160 print_operand_address (file, XEXP (addr, 0));
1161 }
1162 else
1163 {
1164 /* foo+k */
1165 print_operand_address (file, XEXP (addr, 0));
1166 fprintf (file, "+");
1167 print_operand_address (file, XEXP (addr, 1));
1168 }
1169 fprintf (file, ")");
1170 break;
1171
1172 case CONST_INT:
1173 {
1174 /* Since the h8/300 only has 16 bit pointers, negative values are also
1175 those >= 32768. This happens for example with pointer minus a
1176 constant. We don't want to turn (char *p - 2) into
1177 (char *p + 65534) because loop unrolling can build upon this
1178 (IE: char *p + 131068). */
1179 int n = INTVAL (addr);
1180 if (TARGET_H8300)
1181 n = (int) (short) n;
1182 if (n < 0)
1183 /* ??? Why the special case for -ve values? */
1184 fprintf (file, "-%d", -n);
1185 else
1186 fprintf (file, "%d", n);
1187 break;
1188 }
1189
1190 default:
1191 output_addr_const (file, addr);
1192 break;
1193 }
1194 }
1195 \f
1196 /* Output all insn addresses and their sizes into the assembly language
1197 output file. This is helpful for debugging whether the length attributes
1198 in the md file are correct. This is not meant to be a user selectable
1199 option. */
1200
1201 void
1202 final_prescan_insn (insn, operand, num_operands)
1203 rtx insn, *operand;
1204 int num_operands;
1205 {
1206 /* This holds the last insn address. */
1207 static int last_insn_address = 0;
1208
1209 int uid = INSN_UID (insn);
1210
1211 if (TARGET_RTL_DUMP)
1212 {
1213 fprintf (asm_out_file, "\n****************");
1214 print_rtl (asm_out_file, PATTERN (insn));
1215 fprintf (asm_out_file, "\n");
1216 }
1217
1218 if (TARGET_ADDRESSES)
1219 {
1220 fprintf (asm_out_file, "; 0x%x %d\n", insn_addresses[uid],
1221 insn_addresses[uid] - last_insn_address);
1222 last_insn_address = insn_addresses[uid];
1223 }
1224 }
1225
1226 /* Prepare for an SI sized move. */
1227
1228 int
1229 do_movsi (operands)
1230 rtx operands[];
1231 {
1232 rtx src = operands[1];
1233 rtx dst = operands[0];
1234 if (!reload_in_progress && !reload_completed)
1235 {
1236 if (!register_operand (dst, GET_MODE (dst)))
1237 {
1238 rtx tmp = gen_reg_rtx (GET_MODE (dst));
1239 emit_move_insn (tmp, src);
1240 operands[1] = tmp;
1241 }
1242 }
1243 return 0;
1244 }
1245
1246 /* Function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET).
1247 Define the offset between two registers, one to be eliminated, and the other
1248 its replacement, at the start of a routine. */
1249
1250 int
1251 initial_offset (from, to)
1252 {
1253 int offset = 0;
1254
1255 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
1256 offset = UNITS_PER_WORD + frame_pointer_needed * UNITS_PER_WORD;
1257 else
1258 {
1259 int regno;
1260
1261 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1262 if ((regs_ever_live[regno]
1263 && (!call_used_regs[regno] || regno == FRAME_POINTER_REGNUM)))
1264 offset += UNITS_PER_WORD;
1265
1266 /* See the comments for get_frame_size. We need to round it up to
1267 STACK_BOUNDARY. */
1268
1269 offset += ((get_frame_size () + STACK_BOUNDARY / BITS_PER_UNIT - 1)
1270 & ~(STACK_BOUNDARY / BITS_PER_UNIT - 1));
1271
1272 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
1273 offset += UNITS_PER_WORD; /* Skip saved PC */
1274 }
1275 return offset;
1276 }
1277
1278 /* Update the condition code from the insn. */
1279
1280 int
1281 notice_update_cc (body, insn)
1282 rtx body;
1283 rtx insn;
1284 {
1285 switch (get_attr_cc (insn))
1286 {
1287 case CC_NONE:
1288 /* Insn does not affect the CC at all */
1289 break;
1290
1291 case CC_NONE_0HIT:
1292 /* Insn does not change the CC, but the 0't operand has been changed. */
1293
1294 if (cc_status.value1 != 0
1295 && reg_overlap_mentioned_p (recog_operand[0], cc_status.value1))
1296 cc_status.value1 = 0;
1297
1298 if (cc_status.value2 != 0
1299 && reg_overlap_mentioned_p (recog_operand[0], cc_status.value2))
1300 cc_status.value2 = 0;
1301
1302 break;
1303
1304 case CC_SET:
1305 /* Insn sets CC to recog_operand[0], but overflow is impossible. */
1306 CC_STATUS_INIT;
1307 cc_status.flags |= CC_NO_OVERFLOW;
1308 cc_status.value1 = recog_operand[0];
1309 break;
1310
1311 case CC_COMPARE:
1312 /* The insn is a compare instruction */
1313 CC_STATUS_INIT;
1314 cc_status.value1 = SET_SRC (body);
1315 break;
1316
1317 case CC_CBIT:
1318 CC_STATUS_INIT;
1319 cc_status.flags |= CC_DONE_CBIT;
1320 cc_status.value1 = 0;
1321 break;
1322
1323 case CC_WHOOPS:
1324 case CC_CLOBBER:
1325 /* Insn clobbers CC. */
1326 CC_STATUS_INIT;
1327 break;
1328 }
1329 }
1330
1331 /* Recognize valid operators for bit instructions */
1332
1333 int
1334 bit_operator (x, mode)
1335 rtx x;
1336 enum machine_mode mode;
1337 {
1338 enum rtx_code code = GET_CODE (x);
1339
1340 return (code == XOR
1341 || code == AND
1342 || code == IOR);
1343 }
1344 \f
1345 /* Shifts.
1346
1347 We devote a fair bit of code to getting efficient shifts since we can only
1348 shift one bit at a time. See the .md file for more comments.
1349
1350 Here are some thoughts on what the absolutely positively best code is.
1351 "Best" here means some rational trade-off between code size and speed,
1352 where speed is more preferred but not at the expense of generating 20 insns.
1353
1354 H8/300 QImode shifts
1355 1-4 - do them inline
1356 5-6 - ASHIFT | LSHIFTRT: rotate, mask off other bits
1357 ASHIFTRT: loop
1358 7 - ASHIFT | LSHIFTRT: rotate, mask off other bits
1359 ASHIFTRT: shll, subx (propagate carry bit to all bits)
1360
1361 H8/300 HImode shifts
1362 1-4 - do them inline
1363 5-6 - loop
1364 7 - shift other way once, move byte into place, move carry bit into place
1365 8 - move byte, zero (ASHIFT | LSHIFTRT) or sign extend other (ASHIFTRT)
1366 9 - inline shift 1-4, move byte, set other byte
1367 13-14 - ASHIFT | LSHIFTRT: rotate 3/2, mask, move byte, set other byte to 0
1368 - ASHIFTRT: loop
1369 15 - ASHIFT | LSHIFTRT: rotate 1, mask, move byte, set other byte to 0
1370 - ASHIFTRT: shll, subx, set other byte
1371
1372 H8/300 SImode shifts
1373 1-2 - do them inline
1374 3-6 - loop
1375 7 - shift other way once, move bytes into place,
1376 move carry into place (possibly with sign extension)
1377 8 - move bytes into place, zero or sign extend other
1378 9-14 - loop
1379 15 - shift other way once, move word into place, move carry into place
1380 16 - move word, zero or sign extend other
1381 17-23 - loop
1382 24 - move bytes into place, zero or sign extend other
1383 25-27 - loop
1384 28-30 - ASHIFT | LSHIFTRT: rotate top byte, mask, move byte into place,
1385 zero others
1386 ASHIFTRT: loop
1387 31 - ASHIFT | LSHIFTRT: rotate top byte, mask, byte byte into place,
1388 zero others
1389 ASHIFTRT: shll top byte, subx, copy to other bytes
1390
1391 H8/300H QImode shifts
1392 - same as H8/300
1393
1394 H8/300H HImode shifts
1395 - same as H8/300
1396
1397 H8/300H SImode shifts
1398 (These are complicated by the fact that we don't have byte level access to
1399 the top word.)
1400 A word is: bytes 3,2,1,0 (msb -> lsb), word 1,0 (msw -> lsw)
1401 1-4 - do them inline
1402 5-14 - loop
1403 15 - shift other way once, move word into place, move carry into place
1404 (with sign extension for ASHIFTRT)
1405 16 - move word into place, zero or sign extend other
1406 17-23 - loop
1407 24 - ASHIFT: move byte 0(msb) to byte 1, zero byte 0,
1408 move word 0 to word 1, zero word 0
1409 LSHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1410 zero word 1, zero byte 1
1411 ASHIFTRT: move word 1 to word 0, move byte 1 to byte 0,
1412 sign extend byte 0, sign extend word 0
1413 25-27 - either loop, or
1414 do 24 bit shift, inline rest
1415 28-30 - ASHIFT: rotate 4/3/2, mask
1416 LSHIFTRT: rotate 4/3/2, mask
1417 ASHIFTRT: loop
1418 31 - shll, subx byte 0, sign extend byte 0, sign extend word 0
1419
1420 Don't Panic!!!
1421
1422 All of these haven't been implemented. I've just documented them and
1423 provided hooks so they can be.
1424 */
1425
1426 int
1427 nshift_operator (x, mode)
1428 rtx x;
1429 enum machine_mode mode;
1430 {
1431 switch (GET_CODE (x))
1432 {
1433 case ASHIFTRT:
1434 case LSHIFTRT:
1435 case ASHIFT:
1436 return 1;
1437
1438 default:
1439 return 0;
1440 }
1441 }
1442
1443 /* Called from the .md file to emit code to do shifts.
1444 Returns a boolean indicating success
1445 (currently this is always TRUE). */
1446
1447 int
1448 expand_a_shift (mode, code, operands)
1449 enum machine_mode mode;
1450 int code;
1451 rtx operands[];
1452 {
1453 extern int rtx_equal_function_value_matters;
1454
1455 emit_move_insn (operands[0], operands[1]);
1456
1457 /* need a loop to get all the bits we want - we generate the
1458 code at emit time, but need to allocate a scratch reg now */
1459
1460 emit_insn (gen_rtx
1461 (PARALLEL, VOIDmode,
1462 gen_rtvec (2,
1463 gen_rtx (SET, VOIDmode, operands[0],
1464 gen_rtx (code, mode, operands[0], operands[2])),
1465 gen_rtx (CLOBBER, VOIDmode, gen_rtx (SCRATCH, QImode, 0)))));
1466
1467 return 1;
1468 }
1469
1470 /* Shift algorithm determination.
1471
1472 There are various ways of doing a shift:
1473 SHIFT_INLINE: If the amount is small enough, just generate as many one-bit
1474 shifts as we need.
1475 SHIFT_ROT_AND: If the amount is large but close to either end, rotate the
1476 necessary bits into position and then set the rest to zero.
1477 SHIFT_SPECIAL: Hand crafted assembler.
1478 SHIFT_LOOP: If the above methods fail, just loop. */
1479
1480 enum shift_alg
1481 {
1482 SHIFT_INLINE,
1483 SHIFT_ROT_AND,
1484 SHIFT_SPECIAL,
1485 SHIFT_LOOP,
1486 SHIFT_MAX
1487 };
1488
1489 /* Symbols of the various shifts which can be used as indices. */
1490
1491 enum shift_type
1492 {
1493 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
1494 };
1495
1496 /* Symbols of the various modes which can be used as indices. */
1497
1498 enum shift_mode
1499 {
1500 QIshift, HIshift, SIshift
1501 };
1502
1503 /* For single bit shift insns, record assembler and whether the condition code
1504 is valid afterwards. */
1505
1506 struct shift_insn
1507 {
1508 char *assembler;
1509 int cc_valid;
1510 };
1511
1512 /* Assembler instruction shift table.
1513
1514 These tables are used to look up the basic shifts.
1515 They are indexed by cpu, shift_type, and mode.
1516 */
1517
1518 static const struct shift_insn shift_one[2][3][3] =
1519 {
1520 /* H8/300 */
1521 {
1522 /* SHIFT_ASHIFT */
1523 {
1524 { "shal %X0", 1 },
1525 { "add.w %T0,%T0\t; shal.w", 1 },
1526 { "add.w %f0,%f0\t; shal.l\n\taddx %y0,%y0\n\taddx %z0,%z0\t; end shal.l", 0 }
1527 },
1528 /* SHIFT_LSHIFTRT */
1529 {
1530 { "shlr %X0", 1 },
1531 { "shlr %t0\t; shlr.w\n\trotxr %s0\t; end shlr.w", 0 },
1532 { "shlr %z0\t; shlr.l\n\trotxr %y0\n\trotxr %x0\n\trotxr %w0\t; end shlr.l", 0 }
1533 },
1534 /* SHIFT_ASHIFTRT */
1535 {
1536 { "shar %X0", 1 },
1537 { "shar %t0\t; shar.w\n\trotxr %s0\t; end shar.w", 0 },
1538 { "shar %z0\t; shar.l\n\trotxr %y0\n\trotxr %x0\n\trotxr %w0\t; end shar.l", 0 }
1539 }
1540 },
1541 /* H8/300H */
1542 {
1543 /* SHIFT_ASHIFT */
1544 {
1545 { "shal.b %X0", 1 },
1546 { "shal.w %T0", 1 },
1547 { "shal.l %S0", 1 }
1548 },
1549 /* SHIFT_LSHIFTRT */
1550 {
1551 { "shlr.b %X0", 1 },
1552 { "shlr.w %T0", 1 },
1553 { "shlr.l %S0", 1 }
1554 },
1555 /* SHIFT_ASHIFTRT */
1556 {
1557 { "shar.b %X0", 1 },
1558 { "shar.w %T0", 1 },
1559 { "shar.l %S0", 1 }
1560 }
1561 }
1562 };
1563
1564 /* Rotates are organized by which shift they'll be used in implementing.
1565 There's no need to record whether the cc is valid afterwards because
1566 it is the AND insn that will decide this. */
1567
1568 static const char *const rotate_one[2][3][3] =
1569 {
1570 /* H8/300 */
1571 {
1572 /* SHIFT_ASHIFT */
1573 {
1574 "rotr %X0",
1575 "shlr %t0\t; rotr.w\n\trotxr %s0\n\tbst #7,%t0\t; end rotr.w",
1576 0
1577 },
1578 /* SHIFT_LSHIFTRT */
1579 {
1580 "rotl %X0",
1581 "shll %s0\t; rotl.w\n\trotxl %t0\n\tbst #0,%s0\t; end rotl.w",
1582 0
1583 },
1584 /* SHIFT_ASHIFTRT */
1585 {
1586 "rotl %X0",
1587 "shll %s0\t; rotl.w\n\trotxl %t0\n\tbst #0,%s0\t; end rotl.w",
1588 0
1589 }
1590 },
1591 /* H8/300H */
1592 {
1593 /* SHIFT_ASHIFT */
1594 {
1595 "rotr.b %X0",
1596 "rotr.w %T0",
1597 "rotr.l %S0"
1598 },
1599 /* SHIFT_LSHIFTRT */
1600 {
1601 "rotl.b %X0",
1602 "rotl.w %T0",
1603 "rotl.l %S0"
1604 },
1605 /* SHIFT_ASHIFTRT */
1606 {
1607 "rotl.b %X0",
1608 "rotl.w %T0",
1609 "rotl.l %S0"
1610 }
1611 }
1612 };
1613
1614 /* Given CPU, MODE, SHIFT_TYPE, and shift count COUNT, determine the best
1615 algorithm for doing the shift. The assembler code is stored in ASSEMBLER.
1616 We don't achieve maximum efficiency in all cases, but the hooks are here
1617 to do so.
1618
1619 For now we just use lots of switch statements. Since we don't even come
1620 close to supporting all the cases, this is simplest. If this function ever
1621 gets too big, perhaps resort to a more table based lookup. Of course,
1622 at this point you may just wish to do it all in rtl.
1623
1624 WARNING: The constraints on insns shiftbyn_QI/HI/SI assume shifts of
1625 1,2,3,4 will be inlined (1,2 for SI). */
1626
1627 static enum shift_alg
1628 get_shift_alg (cpu, shift_type, mode, count, assembler_p, cc_valid_p)
1629 enum attr_cpu cpu;
1630 enum shift_type shift_type;
1631 enum machine_mode mode;
1632 int count;
1633 const char **assembler_p;
1634 int *cc_valid_p;
1635 {
1636 /* The default is to loop. */
1637 enum shift_alg alg = SHIFT_LOOP;
1638 enum shift_mode shift_mode;
1639
1640 /* We don't handle negative shifts or shifts greater than the word size,
1641 they should have been handled already. */
1642
1643 if (count < 0 || count > GET_MODE_BITSIZE (mode))
1644 abort ();
1645
1646 switch (mode)
1647 {
1648 case QImode:
1649 shift_mode = QIshift;
1650 break;
1651 case HImode:
1652 shift_mode = HIshift;
1653 break;
1654 case SImode:
1655 shift_mode = SIshift;
1656 break;
1657 default:
1658 abort ();
1659 }
1660
1661 /* Assume either SHIFT_LOOP or SHIFT_INLINE.
1662 It is up to the caller to know that looping clobbers cc. */
1663 *assembler_p = shift_one[cpu][shift_type][shift_mode].assembler;
1664 *cc_valid_p = shift_one[cpu][shift_type][shift_mode].cc_valid;
1665
1666 /* Now look for cases we want to optimize. */
1667
1668 switch (shift_mode)
1669 {
1670 case QIshift:
1671 if (count <= 4)
1672 return SHIFT_INLINE;
1673 else if (count <= 6)
1674 {
1675 if (shift_type == SHIFT_ASHIFTRT)
1676 {
1677 return SHIFT_LOOP;
1678 }
1679 else
1680 {
1681 *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1682 *cc_valid_p = 0;
1683 return SHIFT_ROT_AND;
1684 }
1685 }
1686 else if (count == 7)
1687 {
1688 if (shift_type == SHIFT_ASHIFTRT)
1689 {
1690 *assembler_p = "shll %X0\t; shar.b(7)\n\tsubx %X0,%X0\t; end shar.b(7)";
1691 *cc_valid_p = 0;
1692 return SHIFT_SPECIAL;
1693 }
1694 else
1695 {
1696 *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1697 *cc_valid_p = 0;
1698 return SHIFT_ROT_AND;
1699 }
1700 }
1701 break;
1702 case HIshift:
1703 if (count <= 4)
1704 return SHIFT_INLINE;
1705 else if (count == 8)
1706 {
1707 switch (shift_type)
1708 {
1709 case SHIFT_ASHIFT:
1710 *assembler_p = "mov.b %s0,%t0\t; shal.w(8)\n\tsub.b %s0,%s0\t; end shal.w(8)";
1711 *cc_valid_p = 0;
1712 return SHIFT_SPECIAL;
1713 case SHIFT_LSHIFTRT:
1714 *assembler_p = "mov.b %t0,%s0\t; shlr.w(8)\n\tsub.b %t0,%t0\t; end shlr.w(8)";
1715 *cc_valid_p = 0;
1716 return SHIFT_SPECIAL;
1717 case SHIFT_ASHIFTRT:
1718 if (cpu == CPU_H8300)
1719 *assembler_p = "mov.b %t0,%s0\t; shar.w(8)\n\tshll %t0\n\tsubx %t0,%t0\t; end shar.w(8)";
1720 else
1721 *assembler_p = "mov.b %t0,%s0\t; shar.w(8)\n\texts.w %T0\t; end shar.w(8)";
1722 *cc_valid_p = 0;
1723 return SHIFT_SPECIAL;
1724 }
1725 abort ();
1726 }
1727 else if (count == 15)
1728 {
1729 if (shift_type == SHIFT_ASHIFTRT)
1730 {
1731 *assembler_p = "shll %t0,%t0\t; shar.w(15)\n\tsubx %t0,%t0\n\tmov.b %t0,%s0\t; end shar.w(15)";
1732 *cc_valid_p = 0;
1733 return SHIFT_SPECIAL;
1734 }
1735 else
1736 {
1737 *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1738 *cc_valid_p = 0;
1739 return SHIFT_ROT_AND;
1740 }
1741 }
1742 break;
1743 case SIshift:
1744 if (count <= (cpu == CPU_H8300 ? 2 : 4))
1745 return SHIFT_INLINE;
1746 else if (count == 8)
1747 {
1748 if (cpu == CPU_H8300)
1749 {
1750 switch (shift_type)
1751 {
1752 case SHIFT_ASHIFT:
1753 *assembler_p = "mov.b %y0,%z0\t; shal.l(8)\n\tmov.b %x0,%y0\n\tmov.b %w0,%x0\n\tsub.b %w0,%w0\t; end shal.l(8)";
1754 *cc_valid_p = 0;
1755 return SHIFT_SPECIAL;
1756 case SHIFT_LSHIFTRT:
1757 *assembler_p = "mov.b %x0,%w0\t; shlr.l(8)\n\tmov.b %y0,%x0\n\tmov.b %z0,%y0\n\tsub.b %z0,%z0\t; end shlr.l(8)";
1758 *cc_valid_p = 0;
1759 return SHIFT_SPECIAL;
1760 case SHIFT_ASHIFTRT:
1761 *assembler_p = "mov.b %x0,%w0\t; shar.l(8)\n\tmov.b %y0,%x0\n\tmov.b %z0,%y0\n\tshll %z0\n\tsubx %z0,%z0; end shar.l(8)";
1762 *cc_valid_p = 0;
1763 return SHIFT_SPECIAL;
1764 }
1765 }
1766 else /* CPU_H8300H */
1767 /* We don't have byte level access to the high word so this isn't
1768 easy to do. For now, just loop. */
1769 ;
1770 }
1771 else if (count == 16)
1772 {
1773 switch (shift_type)
1774 {
1775 case SHIFT_ASHIFT:
1776 *assembler_p = "mov.w %f0,%e0\t; shal.l(16)\n\tsub.w %f0,%f0\t; end shal.l(16)";
1777 *cc_valid_p = 0;
1778 return SHIFT_SPECIAL;
1779 case SHIFT_LSHIFTRT:
1780 *assembler_p = "mov.w %e0,%f0\t; shlr.l(16)\n\tsub.w %e0,%e0\t; end shlr.l(16)";
1781 *cc_valid_p = 0;
1782 return SHIFT_SPECIAL;
1783 case SHIFT_ASHIFTRT:
1784 if (cpu == CPU_H8300)
1785 *assembler_p = "mov.w %e0,%f0\t; shar.l(16)\n\tshll %z0\n\tsubx %z0,%z0\n\tmov.b %z0,%y0\t; end shar.l(16)";
1786 else
1787 *assembler_p = "mov.w %e0,%f0\t; shar.l(16)\n\texts.l %S0\t; end shar.l(16)";
1788 *cc_valid_p = 0;
1789 return SHIFT_SPECIAL;
1790 }
1791 }
1792 else if (count >= 28 && count <= 30)
1793 {
1794 if (shift_type == SHIFT_ASHIFTRT)
1795 {
1796 return SHIFT_LOOP;
1797 }
1798 else
1799 {
1800 if (cpu == CPU_H8300)
1801 return SHIFT_LOOP;
1802 else
1803 {
1804 *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1805 *cc_valid_p = 0;
1806 return SHIFT_ROT_AND;
1807 }
1808 }
1809 }
1810 else if (count == 31)
1811 {
1812 if (shift_type == SHIFT_ASHIFTRT)
1813 {
1814 if (cpu == CPU_H8300)
1815 *assembler_p = "shll %z0\t; shar.l(31)\n\tsubx %w0,%w0\n\tmov.b %w0,%x0\n\tmov.w %f0,%e0\t; end shar.l(31)";
1816 else
1817 *assembler_p = "shll %e0\t; shar.l(31)\n\tsubx %w0,%w0\n\tmov.b %w0,%x0\n\tmov.w %f0,%e0\t; end shar.l(31)";
1818 *cc_valid_p = 0;
1819 return SHIFT_SPECIAL;
1820 }
1821 else
1822 {
1823 if (cpu == CPU_H8300)
1824 {
1825 if (shift_type == SHIFT_ASHIFT)
1826 *assembler_p = "sub.w %e0,%e0\t; shal.l(31)\n\tshlr %w0\n\tmov.w %e0,%f0\n\trotxr %z0\t; end shal.l(31)";
1827 else
1828 *assembler_p = "sub.w %f0,%f0\t; shlr.l(31)\n\tshll %z0\n\tmov.w %f0,%e0\n\trotxl %w0\t; end shlr.l(31)";
1829 *cc_valid_p = 0;
1830 return SHIFT_SPECIAL;
1831 }
1832 else
1833 {
1834 *assembler_p = rotate_one[cpu][shift_type][shift_mode];
1835 *cc_valid_p = 0;
1836 return SHIFT_ROT_AND;
1837 }
1838 }
1839 }
1840 break;
1841 default:
1842 abort ();
1843 }
1844
1845 return alg;
1846 }
1847
1848 /* Emit the assembler code for doing shifts. */
1849
1850 char *
1851 emit_a_shift (insn, operands)
1852 rtx insn;
1853 rtx *operands;
1854 {
1855 static int loopend_lab;
1856 char *assembler;
1857 int cc_valid;
1858 rtx inside = PATTERN (insn);
1859 rtx shift = operands[3];
1860 enum machine_mode mode = GET_MODE (shift);
1861 enum rtx_code code = GET_CODE (shift);
1862 enum shift_type shift_type;
1863 enum shift_mode shift_mode;
1864
1865 loopend_lab++;
1866
1867 switch (mode)
1868 {
1869 case QImode:
1870 shift_mode = QIshift;
1871 break;
1872 case HImode:
1873 shift_mode = HIshift;
1874 break;
1875 case SImode:
1876 shift_mode = SIshift;
1877 break;
1878 default:
1879 abort ();
1880 }
1881
1882 switch (code)
1883 {
1884 case ASHIFTRT:
1885 shift_type = SHIFT_ASHIFTRT;
1886 break;
1887 case LSHIFTRT:
1888 shift_type = SHIFT_LSHIFTRT;
1889 break;
1890 case ASHIFT:
1891 shift_type = SHIFT_ASHIFT;
1892 break;
1893 default:
1894 abort ();
1895 }
1896
1897 if (GET_CODE (operands[2]) != CONST_INT)
1898 {
1899 /* Indexing by reg, so have to loop and test at top */
1900 output_asm_insn ("mov.b %X2,%X4", operands);
1901 fprintf (asm_out_file, "\tble .Lle%d\n", loopend_lab);
1902
1903 /* Get the assembler code to do one shift. */
1904 get_shift_alg (cpu_type, shift_type, mode, 1, &assembler, &cc_valid);
1905 }
1906 else
1907 {
1908 int n = INTVAL (operands[2]);
1909 enum shift_alg alg;
1910
1911 /* If the count is negative, make it 0. */
1912 if (n < 0)
1913 n = 0;
1914 /* If the count is too big, truncate it.
1915 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
1916 do the intuitive thing. */
1917 else if (n > GET_MODE_BITSIZE (mode))
1918 n = GET_MODE_BITSIZE (mode);
1919
1920 alg = get_shift_alg (cpu_type, shift_type, mode, n, &assembler, &cc_valid);
1921
1922 switch (alg)
1923 {
1924 case SHIFT_INLINE:
1925 while (--n >= 0)
1926 output_asm_insn (assembler, operands);
1927 if (cc_valid)
1928 cc_status.value1 = operands[0];
1929 return "";
1930 case SHIFT_ROT_AND:
1931 {
1932 int m = GET_MODE_BITSIZE (mode) - n;
1933 int mask = (shift_type == SHIFT_ASHIFT
1934 ? ((1 << GET_MODE_BITSIZE (mode) - n) - 1) << n
1935 : (1 << GET_MODE_BITSIZE (mode) - n) - 1);
1936 char insn_buf[200];
1937 /* Not all possibilities of rotate are supported. They shouldn't
1938 be generated, but let's watch for 'em. */
1939 if (assembler == 0)
1940 abort ();
1941 while (--m >= 0)
1942 output_asm_insn (assembler, operands);
1943 if (TARGET_H8300)
1944 {
1945 switch (mode)
1946 {
1947 case QImode:
1948 sprintf (insn_buf, "and #%d,%%X0\t; end shift %d via rotate+and",
1949 mask, n);
1950 cc_status.value1 = operands[0];
1951 break;
1952 case HImode:
1953 sprintf (insn_buf, "and #%d,%%s0\n\tand #%d,%%t0\t; end shift %d via rotate+and",
1954 mask & 255, mask >> 8, n);
1955 break;
1956 case SImode:
1957 abort ();
1958 }
1959 }
1960 else
1961 {
1962 sprintf (insn_buf, "and.%c #%d,%%%c0",
1963 "bwl"[shift_mode], mask,
1964 mode == QImode ? 'X' : mode == HImode ? 'T' : 'S');
1965 cc_status.value1 = operands[0];
1966 }
1967 output_asm_insn (insn_buf, operands);
1968 return "";
1969 }
1970 case SHIFT_SPECIAL:
1971 output_asm_insn (assembler, operands);
1972 return "";
1973 }
1974
1975 /* Need a loop, move limit to tmp reg */
1976 fprintf (asm_out_file, "\tmov.b #%d,%sl\n", n, names_big[REGNO (operands[4])]);
1977 }
1978
1979 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
1980 output_asm_insn (assembler, operands);
1981 output_asm_insn ("add #0xff,%X4", operands);
1982 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
1983 fprintf (asm_out_file, ".Lle%d:\n", loopend_lab);
1984
1985 return "";
1986 }
1987 \f
1988 /* Fix the operands of a gen_xxx so that it could become a bit
1989 operating insn. */
1990
1991 int
1992 fix_bit_operand (operands, what, type)
1993 rtx *operands;
1994 char what;
1995 enum rtx_code type;
1996 {
1997 /* The bit_operand predicate accepts any memory durint RTL generation, but
1998 only 'U' memory afterwards, so if this is a MEM operand, we must force
1999 it to be valid for 'U' by reloading the address. */
2000
2001 if (GET_CODE (operands[2]) == CONST_INT)
2002 {
2003 if (CONST_OK_FOR_LETTER_P (INTVAL (operands[2]), what))
2004 {
2005 /* Ok to have a memory dest. */
2006 if (GET_CODE (operands[0]) == MEM && !EXTRA_CONSTRAINT (operands[0], 'U'))
2007 {
2008 rtx mem;
2009 mem = gen_rtx (MEM, GET_MODE (operands[0]),
2010 copy_to_mode_reg (Pmode, XEXP (operands[0], 0)));
2011 RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (operands[0]);
2012 MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (operands[0]);
2013 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (operands[0]);
2014 operands[0] = mem;
2015 }
2016
2017 if (GET_CODE (operands[1]) == MEM && !EXTRA_CONSTRAINT (operands[1], 'U'))
2018 {
2019 rtx mem;
2020 mem = gen_rtx (MEM, GET_MODE (operands[1]),
2021 copy_to_mode_reg (Pmode, XEXP (operands[1], 0)));
2022 RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (operands[1]);
2023 MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (operands[1]);
2024 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (operands[1]);
2025 operands[1] = mem;
2026 }
2027 return 0;
2028 }
2029 }
2030
2031 /* Dest and src op must be register. */
2032
2033 operands[1] = force_reg (QImode, operands[1]);
2034 {
2035 rtx res = gen_reg_rtx (QImode);
2036 emit_insn (gen_rtx (SET, VOIDmode, res, gen_rtx (type, QImode, operands[1], operands[2])));
2037 emit_insn (gen_rtx (SET, VOIDmode, operands[0], res));
2038 }
2039 return 1;
2040 }
This page took 0.12825 seconds and 6 git commands to generate.