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