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