Initial revision
[official-gcc.git] / gcc / config / mn10200 / mn10200.c
blob6c929c81136463848b675e8b1086640a913b504c
1 /* Subroutines for insn-output.c for Matsushita MN10200 series
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 Contributed by Jeff Law (law@cygnus.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <stdio.h>
23 #include "config.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "hard-reg-set.h"
27 #include "real.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "recog.h"
35 #include "expr.h"
36 #include "tree.h"
37 #include "obstack.h"
39 /* Global registers known to hold the value zero.
41 Normally we'd depend on CSE and combine to put zero into a
42 register and re-use it.
44 However, on the mn10x00 processors we implicitly use the constant
45 zero in tst instructions, so we might be able to do better by
46 loading the value into a register in the prologue, then re-useing
47 that register throughout the function.
49 We could perform similar optimizations for other constants, but with
50 gcse due soon, it doesn't seem worth the effort.
52 These variables hold a rtx for a register known to hold the value
53 zero throughout the entire function, or NULL if no register of
54 the appropriate class has such a value throughout the life of the
55 function. */
56 rtx zero_dreg;
57 rtx zero_areg;
59 /* Note whether or not we need an out of line epilogue. */
60 static int out_of_line_epilogue;
62 /* Indicate this file was compiled by gcc and what optimization
63 level was used. */
64 void
65 asm_file_start (file)
66 FILE *file;
68 fprintf (file, "#\tGCC For the Matsushita MN10200\n");
69 if (optimize)
70 fprintf (file, "# -O%d\n", optimize);
71 else
72 fprintf (file, "\n\n");
73 output_file_directive (file, main_input_filename);
76 /* Print operand X using operand code CODE to assembly language output file
77 FILE. */
79 void
80 print_operand (file, x, code)
81 FILE *file;
82 rtx x;
83 int code;
85 switch (code)
87 case 'b':
88 case 'B':
89 /* These are normal and reversed branches. */
90 switch (code == 'b' ? GET_CODE (x) : reverse_condition (GET_CODE (x)))
92 case NE:
93 fprintf (file, "ne");
94 break;
95 case EQ:
96 fprintf (file, "eq");
97 break;
98 case GE:
99 fprintf (file, "ge");
100 break;
101 case GT:
102 fprintf (file, "gt");
103 break;
104 case LE:
105 fprintf (file, "le");
106 break;
107 case LT:
108 fprintf (file, "lt");
109 break;
110 case GEU:
111 fprintf (file, "cc");
112 break;
113 case GTU:
114 fprintf (file, "hi");
115 break;
116 case LEU:
117 fprintf (file, "ls");
118 break;
119 case LTU:
120 fprintf (file, "cs");
121 break;
122 default:
123 abort ();
125 break;
126 case 'C':
127 /* This is used for the operand to a call instruction;
128 if it's a REG, enclose it in parens, else output
129 the operand normally. */
130 if (GET_CODE (x) == REG)
132 fputc ('(', file);
133 print_operand (file, x, 0);
134 fputc (')', file);
136 else
137 print_operand (file, x, 0);
138 break;
140 /* These are the least significant word in a 32bit value.
141 'o' allows us to sign extend a constant if doing so
142 makes for more compact code. */
143 case 'L':
144 case 'o':
145 switch (GET_CODE (x))
147 case MEM:
148 fputc ('(', file);
149 output_address (XEXP (x, 0));
150 fputc (')', file);
151 break;
153 case REG:
154 fprintf (file, "%s", reg_names[REGNO (x)]);
155 break;
157 case SUBREG:
158 fprintf (file, "%s",
159 reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)]);
160 break;
162 case CONST_DOUBLE:
163 if (code == 'L')
165 long val;
166 REAL_VALUE_TYPE rv;
168 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
169 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
170 print_operand_address (file, GEN_INT (val & 0xffff));
172 else
174 long val;
175 REAL_VALUE_TYPE rv;
177 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
178 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
180 val &= 0xffff;
181 val = (((val) & 0xffff) ^ (~0x7fff)) + 0x8000;
182 print_operand_address (file, GEN_INT (val));
184 break;
186 case CONST_INT:
187 if (code == 'L')
188 print_operand_address (file, GEN_INT ((INTVAL (x) & 0xffff)));
189 else
191 unsigned int val = INTVAL (x) & 0xffff;
192 val = (((val) & 0xffff) ^ (~0x7fff)) + 0x8000;
193 print_operand_address (file, GEN_INT (val));
195 break;
196 default:
197 abort ();
199 break;
201 /* Similarly, but for the most significant word. */
202 case 'H':
203 case 'h':
204 switch (GET_CODE (x))
206 case MEM:
207 fputc ('(', file);
208 x = adj_offsettable_operand (x, 2);
209 output_address (XEXP (x, 0));
210 fputc (')', file);
211 break;
213 case REG:
214 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
215 break;
217 case SUBREG:
218 fprintf (file, "%s",
219 reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)] + 1);
220 break;
222 case CONST_DOUBLE:
223 if (code == 'H')
225 long val;
226 REAL_VALUE_TYPE rv;
228 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
229 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
231 print_operand_address (file, GEN_INT ((val >> 16) & 0xffff));
233 else
235 long val;
236 REAL_VALUE_TYPE rv;
238 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
239 REAL_VALUE_TO_TARGET_SINGLE (rv, val);
241 val = (val >> 16) & 0xffff;
242 val = (((val) & 0xffff) ^ (~0x7fff)) + 0x8000;
244 print_operand_address (file, GEN_INT (val));
246 break;
248 case CONST_INT:
249 if (code == 'H')
250 print_operand_address (file,
251 GEN_INT ((INTVAL (x) >> 16) & 0xffff));
252 else
254 unsigned int val = (INTVAL (x) >> 16) & 0xffff;
255 val = (((val) & 0xffff) ^ (~0x7fff)) + 0x8000;
257 print_operand_address (file, GEN_INT (val));
259 break;
260 default:
261 abort ();
263 break;
265 /* Output ~CONST_INT. */
266 case 'N':
267 if (GET_CODE (x) != CONST_INT)
268 abort ();
269 fprintf (file, "%d", ~INTVAL (x));
270 break;
272 /* An address which can not be register indirect, if it is
273 register indirect, then turn it into reg + disp. */
274 case 'A':
275 if (GET_CODE (x) != MEM)
276 abort ();
277 if (GET_CODE (XEXP (x, 0)) == REG)
278 x = gen_rtx (PLUS, PSImode, XEXP (x, 0), GEN_INT (0));
279 else
280 x = XEXP (x, 0);
281 fputc ('(', file);
282 output_address (x);
283 fputc (')', file);
284 break;
286 case 'Z':
287 print_operand (file, XEXP (x, 1), 0);
288 break;
290 /* More cases where we can sign-extend a CONST_INT if it
291 results in more compact code. */
292 case 's':
293 case 'S':
294 if (GET_CODE (x) == CONST_INT)
296 int val = INTVAL (x);
298 if (code == 's')
299 x = GEN_INT (((val & 0xffff) ^ (~0x7fff)) + 0x8000);
300 else
301 x = GEN_INT (((val & 0xff) ^ (~0x7f)) + 0x80);
303 /* FALL THROUGH */
304 default:
305 switch (GET_CODE (x))
307 case MEM:
308 fputc ('(', file);
309 output_address (XEXP (x, 0));
310 fputc (')', file);
311 break;
313 case REG:
314 fprintf (file, "%s", reg_names[REGNO (x)]);
315 break;
317 case SUBREG:
318 fprintf (file, "%s",
319 reg_names[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)]);
320 break;
322 case CONST_INT:
323 case CONST_DOUBLE:
324 case SYMBOL_REF:
325 case CONST:
326 case LABEL_REF:
327 case CODE_LABEL:
328 print_operand_address (file, x);
329 break;
330 default:
331 abort ();
333 break;
337 /* Output assembly language output for the address ADDR to FILE. */
339 void
340 print_operand_address (file, addr)
341 FILE *file;
342 rtx addr;
344 switch (GET_CODE (addr))
346 case REG:
347 print_operand (file, addr, 0);
348 break;
349 case PLUS:
351 rtx base, index;
352 /* The base and index could be in any order, so we have
353 to figure out which is the base and which is the index.
354 Uses the same code as GO_IF_LEGITIMATE_ADDRESS. */
355 if (REG_P (XEXP (addr, 0))
356 && REG_OK_FOR_BASE_P (XEXP (addr, 0)))
357 base = XEXP (addr, 0), index = XEXP (addr, 1);
358 else if (REG_P (XEXP (addr, 1))
359 && REG_OK_FOR_BASE_P (XEXP (addr, 1)))
360 base = XEXP (addr, 1), index = XEXP (addr, 0);
361 else
362 abort ();
363 print_operand (file, index, 0);
364 fputc (',', file);
365 print_operand (file, base, 0);;
366 break;
368 case SYMBOL_REF:
369 output_addr_const (file, addr);
370 break;
371 default:
372 output_addr_const (file, addr);
373 break;
377 /* Count the number of tst insns which compare an address register
378 with zero. */
379 static void
380 count_tst_insns (areg_countp)
381 int *areg_countp;
383 rtx insn;
385 /* Assume no tst insns exist. */
386 *areg_countp = 0;
388 /* If not optimizing, then quit now. */
389 if (!optimize)
390 return;
392 /* Walk through all the insns. */
393 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
395 rtx pat;
397 /* Ignore anything that is not a normal INSN. */
398 if (GET_CODE (insn) != INSN)
399 continue;
401 /* Ignore anything that isn't a SET. */
402 pat = PATTERN (insn);
403 if (GET_CODE (pat) != SET)
404 continue;
406 /* Check for a tst insn. */
407 if (SET_DEST (pat) == cc0_rtx
408 && GET_CODE (SET_SRC (pat)) == REG
409 && REGNO_REG_CLASS (REGNO (SET_SRC (pat))) == ADDRESS_REGS)
410 (*areg_countp)++;
414 /* Return the total size (in bytes) of the current function's frame.
415 This is the size of the register save area + the size of locals,
416 spills, etc. */
418 total_frame_size ()
420 unsigned int size = get_frame_size ();
421 unsigned int outgoing_args_size = current_function_outgoing_args_size;
422 int i;
424 /* First figure out if we're going to use an out of line
425 prologue, if so we have to make space for all the
426 registers, even if we don't use them. */
427 if (optimize && !current_function_needs_context && !frame_pointer_needed)
429 int inline_count, outline_count;
431 /* Compute how many bytes an inline prologue would take.
433 Each address register store takes two bytes, each data register
434 store takes three bytes. */
435 inline_count = 0;
436 if (regs_ever_live[5])
437 inline_count += 2;
438 if (regs_ever_live[6])
439 inline_count += 2;
440 if (regs_ever_live[2])
441 inline_count += 3;
442 if (regs_ever_live[3])
443 inline_count += 3;
445 /* If this function has any stack, then the stack adjustment
446 will take two (or more) bytes. */
447 if (size || outgoing_args_size
448 || regs_ever_live[5] || regs_ever_live[6]
449 || regs_ever_live[2] || regs_ever_live[3])
450 inline_count += 2;
452 /* Multiply the current count by two and add one to account for the
453 epilogue insns. */
454 inline_count = inline_count * 2 + 1;
456 /* Now compute how many bytes an out of line sequence would take. */
457 /* A relaxed jsr will be three bytes. */
458 outline_count = 3;
460 /* If there are outgoing arguments, then we will need a stack
461 pointer adjustment after the call to the prologue, two
462 more bytes. */
463 outline_count += (outgoing_args_size == 0 ? 0 : 2);
465 /* If there is some local frame to allocate, it will need to be
466 done before the call to the prologue, two more bytes. */
467 if (get_frame_size () != 0)
468 outline_count += 2;
470 /* Now account for the epilogue, multiply the base count by two,
471 then deal with optimizing away the rts instruction. */
472 outline_count = outline_count * 2 + 1;
474 if (get_frame_size () == 0 && outgoing_args_size == 0)
475 outline_count -= 1;
477 /* If an out of line prologue is smaller, use it. */
478 if (inline_count > outline_count)
479 return size + outgoing_args_size + 16;
483 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
485 if (regs_ever_live[i] && !call_used_regs[i] && ! fixed_regs[i]
486 || (i == FRAME_POINTER_REGNUM && frame_pointer_needed))
487 size += 4;
490 return (size + outgoing_args_size);
493 /* Expand the prologue into RTL. */
494 void
495 expand_prologue ()
497 unsigned int size = total_frame_size ();
498 unsigned int outgoing_args_size = current_function_outgoing_args_size;
499 int offset, i;
501 zero_areg = NULL_RTX;
502 zero_dreg = NULL_RTX;
504 /* If optimizing, see if we should do an out of line prologue/epilogue
505 sequence.
507 We don't support out of line prologues if the current function
508 needs a context or frame pointer. */
509 if (optimize && !current_function_needs_context && !frame_pointer_needed)
511 int inline_count, outline_count, areg_count;
513 /* We need to end the current sequence so that count_tst_insns can
514 look at all the insns in this function. Normally this would be
515 unsafe, but it's OK in the prologue/epilogue expanders. */
516 end_sequence ();
518 /* Get a count of the number of tst insns which use address
519 registers (it's not profitable to try and improve tst insns
520 which use data registers). */
521 count_tst_insns (&areg_count);
523 /* Now start a new sequence. */
524 start_sequence ();
526 /* Compute how many bytes an inline prologue would take.
528 Each address register store takes two bytes, each data register
529 store takes three bytes. */
530 inline_count = 0;
531 if (regs_ever_live[5])
532 inline_count += 2;
533 if (regs_ever_live[6])
534 inline_count += 2;
535 if (regs_ever_live[2])
536 inline_count += 3;
537 if (regs_ever_live[3])
538 inline_count += 3;
540 /* If this function has any stack, then the stack adjustment
541 will take two (or more) bytes. */
542 if (size || outgoing_args_size
543 || regs_ever_live[5] || regs_ever_live[6]
544 || regs_ever_live[2] || regs_ever_live[3])
545 inline_count += 2;
547 /* Multiply the current count by two and add one to account for the
548 epilogue insns. */
549 inline_count = inline_count * 2 + 1;
551 /* Now compute how many bytes an out of line sequence would take. */
552 /* A relaxed jsr will be three bytes. */
553 outline_count = 3;
555 /* If there are outgoing arguments, then we will need a stack
556 pointer adjustment after the call to the prologue, two
557 more bytes. */
558 outline_count += (outgoing_args_size == 0 ? 0 : 2);
560 /* If there is some local frame to allocate, it will need to be
561 done before the call to the prologue, two more bytes. */
562 if (get_frame_size () != 0)
563 outline_count += 2;
565 /* Now account for the epilogue, multiply the base count by two,
566 then deal with optimizing away the rts instruction. */
567 outline_count = outline_count * 2 + 1;
569 if (get_frame_size () == 0 && outgoing_args_size == 0)
570 outline_count -= 1;
572 /* If an out of line prologue is smaller, use it. */
573 if (inline_count > outline_count)
575 if (get_frame_size () != 0)
576 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
577 GEN_INT (-size + outgoing_args_size + 16)));
578 emit_insn (gen_outline_prologue_call ());
580 if (outgoing_args_size)
581 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
582 GEN_INT (-outgoing_args_size)));
584 out_of_line_epilogue = 1;
586 /* Determine if it is profitable to put the value zero into a register
587 for the entire function. If so, set ZERO_DREG and ZERO_AREG. */
589 /* First see if we could load the value into a data register
590 since that's the most efficient way. */
591 if (areg_count > 1
592 && (!regs_ever_live[2] || !regs_ever_live[3]))
594 if (!regs_ever_live[2])
596 regs_ever_live[2] = 1;
597 zero_dreg = gen_rtx (REG, HImode, 2);
599 if (!regs_ever_live[3])
601 regs_ever_live[3] = 1;
602 zero_dreg = gen_rtx (REG, HImode, 3);
606 /* Now see if we could load the value into a address register. */
607 if (zero_dreg == NULL_RTX
608 && areg_count > 2
609 && (!regs_ever_live[5] || !regs_ever_live[6]))
611 if (!regs_ever_live[5])
613 regs_ever_live[5] = 1;
614 zero_dreg = gen_rtx (REG, HImode, 5);
616 if (!regs_ever_live[6])
618 regs_ever_live[6] = 1;
619 zero_dreg = gen_rtx (REG, HImode, 6);
623 if (zero_dreg)
624 emit_move_insn (zero_dreg, const0_rtx);
626 if (zero_areg)
627 emit_move_insn (zero_areg, const0_rtx);
629 return;
633 out_of_line_epilogue = 0;
635 /* Temporarily stuff the static chain onto the stack so we can
636 use a0 as a scratch register during the prologue. */
637 if (current_function_needs_context)
639 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
640 GEN_INT (-4)));
641 emit_move_insn (gen_rtx (MEM, PSImode, stack_pointer_rtx),
642 gen_rtx (REG, PSImode, STATIC_CHAIN_REGNUM));
645 if (frame_pointer_needed)
647 /* Store a2 into a0 temporarily. */
648 emit_move_insn (gen_rtx (REG, PSImode, 4), frame_pointer_rtx);
650 /* Set up the frame pointer. */
651 emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
654 /* Make any necessary space for the saved registers and local frame. */
655 if (size)
656 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
657 GEN_INT (-size)));
659 /* Save the callee saved registers. They're saved into the top
660 of the frame, using the stack pointer. */
661 for (i = 0, offset = outgoing_args_size;
662 i < FIRST_PSEUDO_REGISTER; i++)
664 if (regs_ever_live[i] && !call_used_regs[i] && ! fixed_regs[i]
665 || (i == FRAME_POINTER_REGNUM && frame_pointer_needed))
667 int regno;
669 /* If we're saving the frame pointer, then it will be found in
670 register 4 (a0). */
671 regno = (i == FRAME_POINTER_REGNUM && frame_pointer_needed) ? 4 : i;
673 emit_move_insn (gen_rtx (MEM, PSImode,
674 gen_rtx (PLUS, Pmode,
675 stack_pointer_rtx,
676 GEN_INT (offset))),
677 gen_rtx (REG, PSImode, regno));
678 offset += 4;
682 /* Now put the static chain back where the rest of the function
683 expects to find it. */
684 if (current_function_needs_context)
686 emit_move_insn (gen_rtx (REG, PSImode, STATIC_CHAIN_REGNUM),
687 gen_rtx (MEM, PSImode,
688 gen_rtx (PLUS, PSImode, stack_pointer_rtx,
689 GEN_INT (size))));
693 /* Expand the epilogue into RTL. */
694 void
695 expand_epilogue ()
697 unsigned int size;
698 unsigned int outgoing_args_size = current_function_outgoing_args_size;
699 int offset, i, temp_regno;
700 rtx basereg;
702 size = total_frame_size ();
704 if (DECL_RESULT (current_function_decl)
705 && DECL_RTL (DECL_RESULT (current_function_decl))
706 && REG_P (DECL_RTL (DECL_RESULT (current_function_decl))))
707 temp_regno = (REGNO (DECL_RTL (DECL_RESULT (current_function_decl))) == 4
708 ? 0 : 4);
709 else
710 temp_regno = 4;
712 /* Emit an out of line epilogue sequence if it's profitable to do so. */
713 if (out_of_line_epilogue)
715 /* If there were no outgoing arguments and no local frame, then
716 we will be able to omit the rts at the end of this function,
717 so just jump to the epilogue_noreturn routine. */
718 if (get_frame_size () == 0 && outgoing_args_size == 0)
720 emit_jump_insn (gen_outline_epilogue_jump ());
721 return;
724 if (outgoing_args_size)
725 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
726 GEN_INT (outgoing_args_size)));
728 if (temp_regno == 0)
729 emit_insn (gen_outline_epilogue_call_d0 ());
730 else if (temp_regno == 4)
731 emit_insn (gen_outline_epilogue_call_a0 ());
733 if (get_frame_size () != 0)
734 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
735 GEN_INT (size - outgoing_args_size - 16)));
736 emit_jump_insn (gen_return_internal ());
737 return;
740 /* Registers are restored from the frame pointer if we have one,
741 else they're restored from the stack pointer. Figure out
742 the appropriate offset to the register save area for both cases. */
743 if (frame_pointer_needed)
745 basereg = frame_pointer_rtx;
746 offset = -(size - outgoing_args_size);
748 else
750 basereg = stack_pointer_rtx;
751 offset = outgoing_args_size;
754 /* Restore each register. */
755 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
757 if (regs_ever_live[i] && !call_used_regs[i] && ! fixed_regs[i]
758 || (i == FRAME_POINTER_REGNUM && frame_pointer_needed))
760 int regno;
762 /* Restore the frame pointer (if it exists) into a temporary
763 register. */
764 regno = ((i == FRAME_POINTER_REGNUM && frame_pointer_needed)
765 ? temp_regno : i);
767 emit_move_insn (gen_rtx (REG, PSImode, regno),
768 gen_rtx (MEM, PSImode,
769 gen_rtx (PLUS, Pmode,
770 basereg,
771 GEN_INT (offset))));
772 offset += 4;
776 if (frame_pointer_needed)
778 /* Deallocate this frame's stack. */
779 emit_move_insn (stack_pointer_rtx, frame_pointer_rtx);
780 /* Restore the old frame pointer. */
781 emit_move_insn (frame_pointer_rtx, gen_rtx (REG, PSImode, temp_regno));
783 else if (size)
785 /* Deallocate this function's stack. */
786 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx,
787 GEN_INT (size)));
790 /* If we had to allocate a slot to save the context pointer,
791 then it must be deallocated here. */
792 if (current_function_needs_context)
793 emit_insn (gen_addpsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (4)));
795 /* Emit the return insn, if this function had no stack, then we
796 can use the standard return (which allows more optimizations),
797 else we have to use the special one which inhibits optimizations. */
798 if (size == 0 && !current_function_needs_context)
799 emit_jump_insn (gen_return ());
800 else
801 emit_jump_insn (gen_return_internal ());
804 /* Update the condition code from the insn. */
806 void
807 notice_update_cc (body, insn)
808 rtx body;
809 rtx insn;
811 switch (get_attr_cc (insn))
813 case CC_NONE:
814 /* Insn does not affect CC at all. */
815 break;
817 case CC_NONE_0HIT:
818 /* Insn does not change CC, but the 0'th operand has been changed. */
819 if (cc_status.value1 != 0
820 && reg_overlap_mentioned_p (recog_operand[0], cc_status.value1))
821 cc_status.value1 = 0;
822 break;
824 case CC_SET_ZN:
825 /* Insn sets the Z,N flags of CC to recog_operand[0].
826 V,C is in an unusable state. */
827 CC_STATUS_INIT;
828 cc_status.flags |= CC_OVERFLOW_UNUSABLE | CC_NO_CARRY;
829 cc_status.value1 = recog_operand[0];
830 break;
832 case CC_SET_ZNV:
833 /* Insn sets the Z,N,V flags of CC to recog_operand[0].
834 C is in an unusable state. */
835 CC_STATUS_INIT;
836 cc_status.flags |= CC_NO_CARRY;
837 cc_status.value1 = recog_operand[0];
838 break;
840 case CC_COMPARE:
841 /* The insn is a compare instruction. */
842 CC_STATUS_INIT;
843 cc_status.value1 = SET_SRC (body);
844 break;
846 case CC_CLOBBER:
847 /* Insn doesn't leave CC in a usable state. */
848 CC_STATUS_INIT;
849 break;
851 default:
852 CC_STATUS_INIT;
853 break;
857 /* Return true if OP is a valid call operand. Valid call operands
858 are SYMBOL_REFs and REGs. */
860 call_address_operand (op, mode)
861 rtx op;
862 enum machine_mode mode;
864 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == REG);
867 /* Return true if OP is an indirect memory operand, the "bset" and "bclr"
868 insns use this predicate. */
870 indirect_memory_operand (op, mode)
871 rtx op;
872 enum machine_mode mode;
874 return (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == REG);
877 /* Return true if OP is a memory operand with a constant address.
878 A special PSImode move pattern uses this predicate. */
880 constant_memory_operand (op, mode)
881 rtx op;
882 enum machine_mode mode;
884 return GET_CODE (op) == MEM && CONSTANT_ADDRESS_P (XEXP (op, 0));
887 /* What (if any) secondary registers are needed to move IN with mode
888 MODE into a register from in register class CLASS.
890 We might be able to simplify this. */
891 enum reg_class
892 secondary_reload_class (class, mode, in, input)
893 enum reg_class class;
894 enum machine_mode mode;
895 rtx in;
896 int input;
898 int regno;
900 /* Memory loads less than a full word wide can't have an
901 address or stack pointer destination. They must use
902 a data register as an intermediate register. */
903 if (input
904 && GET_CODE (in) == MEM
905 && (mode == QImode)
906 && class == ADDRESS_REGS)
907 return DATA_REGS;
909 /* Address register stores which are not PSImode need a scrach register. */
910 if (! input
911 && GET_CODE (in) == MEM
912 && (mode != PSImode)
913 && class == ADDRESS_REGS)
914 return DATA_REGS;
916 /* Otherwise assume no secondary reloads are needed. */
917 return NO_REGS;
921 /* Shifts.
923 We devote a fair bit of code to getting efficient shifts since we can only
924 shift one bit at a time, and each single bit shift may take multiple
925 instructions.
927 The basic shift methods:
929 * loop shifts -- emit a loop using one (or two on H8/S) bit shifts;
930 this is the default. SHIFT_LOOP
932 * inlined shifts -- emit straight line code for the shift; this is
933 used when a straight line shift is about the same size or smaller
934 than a loop. We allow the inline version to be slightly longer in
935 some cases as it saves a register. SHIFT_INLINE
937 * There other oddballs. Not worth explaining. SHIFT_SPECIAL
940 HImode shifts:
942 1-4 do them inline
944 5-7 If ashift, then multiply, else loop.
946 8-14 - If ashift, then multiply, if lshiftrt, then divide, else loop.
947 15 - rotate the bit we want into the carry, clear the destination,
948 (use mov 0,dst, not sub as sub will clobber the carry), then
949 move bit into place.
951 Don't Panic, it's not nearly as bad as the H8 shifting code!!! */
954 nshift_operator (x, mode)
955 rtx x;
956 enum machine_mode mode;
958 switch (GET_CODE (x))
960 case ASHIFTRT:
961 case LSHIFTRT:
962 case ASHIFT:
963 return 1;
965 default:
966 return 0;
970 /* Called from the .md file to emit code to do shifts.
971 Returns a boolean indicating success
972 (currently this is always TRUE). */
975 expand_a_shift (mode, code, operands)
976 enum machine_mode mode;
977 int code;
978 rtx operands[];
980 emit_move_insn (operands[0], operands[1]);
982 /* need a loop to get all the bits we want - we generate the
983 code at emit time, but need to allocate a scratch reg now */
985 emit_insn (gen_rtx
986 (PARALLEL, VOIDmode,
987 gen_rtvec (2,
988 gen_rtx (SET, VOIDmode, operands[0],
989 gen_rtx (code, mode,
990 operands[0], operands[2])),
991 gen_rtx (CLOBBER, VOIDmode,
992 gen_rtx (SCRATCH, HImode, 0)))));
994 return 1;
997 /* Shift algorithm determination.
999 There are various ways of doing a shift:
1000 SHIFT_INLINE: If the amount is small enough, just generate as many one-bit
1001 shifts as we need.
1002 SHIFT_SPECIAL: Hand crafted assembler.
1003 SHIFT_LOOP: If the above methods fail, just loop. */
1005 enum shift_alg
1007 SHIFT_INLINE,
1008 SHIFT_SPECIAL,
1009 SHIFT_LOOP,
1010 SHIFT_MAX
1013 /* Symbols of the various shifts which can be used as indices. */
1015 enum shift_type
1017 SHIFT_ASHIFT, SHIFT_LSHIFTRT, SHIFT_ASHIFTRT
1020 /* Symbols of the various modes which can be used as indices. */
1022 enum shift_mode
1024 HIshift,
1027 /* For single bit shift insns, record assembler and what bits of the
1028 condition code are valid afterwards (represented as various CC_FOO
1029 bits, 0 means CC isn't left in a usable state). */
1031 struct shift_insn
1033 char *assembler;
1034 int cc_valid;
1037 /* Assembler instruction shift table.
1039 These tables are used to look up the basic shifts.
1040 They are indexed by cpu, shift_type, and mode.
1043 static const struct shift_insn shift_one[3][3] =
1046 /* SHIFT_ASHIFT */
1047 { "add\t%0,%0", CC_OVERFLOW_UNUSABLE | CC_NO_CARRY },
1049 /* SHIFT_LSHIFTRT */
1051 { "lsr\t%0", CC_NO_CARRY },
1053 /* SHIFT_ASHIFTRT */
1055 { "asr\t%0", CC_NO_CARRY },
1059 /* Given CPU, MODE, SHIFT_TYPE, and shift count COUNT, determine the best
1060 algorithm for doing the shift. The assembler code is stored in ASSEMBLER.
1061 We don't achieve maximum efficiency in all cases, but the hooks are here
1062 to do so.
1064 For now we just use lots of switch statements. Since we don't even come
1065 close to supporting all the cases, this is simplest. If this function ever
1066 gets too big, perhaps resort to a more table based lookup. Of course,
1067 at this point you may just wish to do it all in rtl. */
1069 static enum shift_alg
1070 get_shift_alg (shift_type, mode, count, assembler_p, cc_valid_p)
1071 enum shift_type shift_type;
1072 enum machine_mode mode;
1073 int count;
1074 const char **assembler_p;
1075 int *cc_valid_p;
1077 /* The default is to loop. */
1078 enum shift_alg alg = SHIFT_LOOP;
1079 enum shift_mode shift_mode;
1081 /* We don't handle negative shifts or shifts greater than the word size,
1082 they should have been handled already. */
1084 if (count < 0 || count > GET_MODE_BITSIZE (mode))
1085 abort ();
1087 switch (mode)
1089 case HImode:
1090 shift_mode = HIshift;
1091 break;
1092 default:
1093 abort ();
1096 /* Assume either SHIFT_LOOP or SHIFT_INLINE.
1097 It is up to the caller to know that looping clobbers cc. */
1098 *assembler_p = shift_one[shift_type][shift_mode].assembler;
1099 *cc_valid_p = shift_one[shift_type][shift_mode].cc_valid;
1101 /* Now look for cases we want to optimize. */
1103 switch (shift_mode)
1105 case HIshift:
1106 if (count <= 4)
1107 return SHIFT_INLINE;
1108 else if (count < 15 && shift_type != SHIFT_ASHIFTRT)
1110 switch (count)
1112 case 5:
1113 if (shift_type == SHIFT_ASHIFT)
1114 *assembler_p = "mov 32,%4\n\tmul %4,%0";
1115 else if (shift_type == SHIFT_LSHIFTRT)
1116 *assembler_p
1117 = "sub %4,%4\n\tmov %4,mdr\n\tmov 32,%4\n\tdivu %4,%0";
1118 *cc_valid_p = CC_NO_CARRY;
1119 return SHIFT_SPECIAL;
1120 case 6:
1121 if (shift_type == SHIFT_ASHIFT)
1122 *assembler_p = "mov 64,%4\n\tmul %4,%0";
1123 else if (shift_type == SHIFT_LSHIFTRT)
1124 *assembler_p
1125 = "sub %4,%4\n\tmov %4,mdr\n\tmov 64,%4\n\tdivu %4,%0";
1126 *cc_valid_p = CC_NO_CARRY;
1127 return SHIFT_SPECIAL;
1128 case 7:
1129 if (shift_type == SHIFT_ASHIFT)
1130 *assembler_p = "mov 128,%4\n\tmul %4,%0";
1131 else if (shift_type == SHIFT_LSHIFTRT)
1132 *assembler_p
1133 = "sub %4,%4\n\tmov %4,mdr\n\tmov 128,%4\n\tdivu %4,%0";
1134 *cc_valid_p = CC_NO_CARRY;
1135 return SHIFT_SPECIAL;
1136 case 8:
1137 if (shift_type == SHIFT_ASHIFT)
1138 *assembler_p = "mov 256,%4\n\tmul %4,%0";
1139 else if (shift_type == SHIFT_LSHIFTRT)
1140 *assembler_p
1141 = "sub %4,%4\n\tmov %4,mdr\n\tmov 256,%4\n\tdivu %4,%0";
1142 *cc_valid_p = CC_NO_CARRY;
1143 return SHIFT_SPECIAL;
1144 case 9:
1145 if (shift_type == SHIFT_ASHIFT)
1146 *assembler_p = "mov 512,%4\n\tmul %4,%0";
1147 else if (shift_type == SHIFT_LSHIFTRT)
1148 *assembler_p
1149 = "sub %4,%4\n\tmov %4,mdr\n\tmov 512,%4\n\tdivu %4,%0";
1150 *cc_valid_p = CC_NO_CARRY;
1151 return SHIFT_SPECIAL;
1152 case 10:
1153 if (shift_type == SHIFT_ASHIFT)
1154 *assembler_p = "mov 1024,%4\n\tmul %4,%0";
1155 else if (shift_type == SHIFT_LSHIFTRT)
1156 *assembler_p
1157 = "sub %4,%4\n\tmov %4,mdr\n\tmov 1024,%4\n\tdivu %4,%0";
1158 *cc_valid_p = CC_NO_CARRY;
1159 return SHIFT_SPECIAL;
1160 case 11:
1161 if (shift_type == SHIFT_ASHIFT)
1162 *assembler_p = "mov 2048,%4\n\tmul %4,%0";
1163 else if (shift_type == SHIFT_LSHIFTRT)
1164 *assembler_p
1165 = "sub %4,%4\n\tmov %4,mdr\n\tmov 2048,%4\n\tdivu %4,%0";
1166 *cc_valid_p = CC_NO_CARRY;
1167 return SHIFT_SPECIAL;
1168 case 12:
1169 if (shift_type == SHIFT_ASHIFT)
1170 *assembler_p = "mov 4096,%4\n\tmul %4,%0";
1171 else if (shift_type == SHIFT_LSHIFTRT)
1172 *assembler_p
1173 = "sub %4,%4\n\tmov %4,mdr\n\tmov 4096,%4\n\tdivu %4,%0";
1174 *cc_valid_p = CC_NO_CARRY;
1175 return SHIFT_SPECIAL;
1176 case 13:
1177 if (shift_type == SHIFT_ASHIFT)
1178 *assembler_p = "mov 8192,%4\n\tmul %4,%0";
1179 else if (shift_type == SHIFT_LSHIFTRT)
1180 *assembler_p
1181 = "sub %4,%4\n\tmov %4,mdr\n\tmov 8192,%4\n\tdivu %4,%0";
1182 *cc_valid_p = CC_NO_CARRY;
1183 return SHIFT_SPECIAL;
1184 case 14:
1185 if (shift_type == SHIFT_ASHIFT)
1186 *assembler_p = "mov 16384,%4\n\tmul %4,%0";
1187 else if (shift_type == SHIFT_LSHIFTRT)
1188 *assembler_p
1189 = "sub %4,%4\n\tmov %4,mdr\n\tmov 16384,%4\n\tdivu %4,%0";
1190 *cc_valid_p = CC_NO_CARRY;
1191 return SHIFT_SPECIAL;
1194 else if (count == 15)
1196 if (shift_type == SHIFT_ASHIFTRT)
1198 *assembler_p = "add\t%0,%0\n\tsubc\t%0,%0\n";
1199 *cc_valid_p = CC_NO_CARRY;
1200 return SHIFT_SPECIAL;
1202 if (shift_type == SHIFT_LSHIFTRT)
1204 *assembler_p = "add\t%0,%0\n\tmov 0,%0\n\trol %0\n";
1205 *cc_valid_p = CC_NO_CARRY;
1206 return SHIFT_SPECIAL;
1208 if (shift_type == SHIFT_ASHIFT)
1210 *assembler_p = "ror\t%0\n\tmov 0,%0\n\tror %0\n";
1211 *cc_valid_p = CC_NO_CARRY;
1212 return SHIFT_SPECIAL;
1215 break;
1217 default:
1218 abort ();
1221 return alg;
1224 /* Emit the assembler code for doing shifts. */
1226 char *
1227 emit_a_shift (insn, operands)
1228 rtx insn;
1229 rtx *operands;
1231 static int loopend_lab;
1232 char *assembler;
1233 int cc_valid;
1234 rtx inside = PATTERN (insn);
1235 rtx shift = operands[3];
1236 enum machine_mode mode = GET_MODE (shift);
1237 enum rtx_code code = GET_CODE (shift);
1238 enum shift_type shift_type;
1239 enum shift_mode shift_mode;
1241 loopend_lab++;
1243 switch (mode)
1245 case HImode:
1246 shift_mode = HIshift;
1247 break;
1248 default:
1249 abort ();
1252 switch (code)
1254 case ASHIFTRT:
1255 shift_type = SHIFT_ASHIFTRT;
1256 break;
1257 case LSHIFTRT:
1258 shift_type = SHIFT_LSHIFTRT;
1259 break;
1260 case ASHIFT:
1261 shift_type = SHIFT_ASHIFT;
1262 break;
1263 default:
1264 abort ();
1267 if (GET_CODE (operands[2]) != CONST_INT)
1269 /* Indexing by reg, so have to loop and test at top */
1270 output_asm_insn ("mov %2,%4", operands);
1271 output_asm_insn ("cmp 0,%4", operands);
1272 fprintf (asm_out_file, "\tble .Lle%d\n", loopend_lab);
1274 /* Get the assembler code to do one shift. */
1275 get_shift_alg (shift_type, mode, 1, &assembler, &cc_valid);
1277 else
1279 int n = INTVAL (operands[2]);
1280 enum shift_alg alg;
1282 /* If the count is negative, make it 0. */
1283 if (n < 0)
1284 n = 0;
1285 /* If the count is too big, truncate it.
1286 ANSI says shifts of GET_MODE_BITSIZE are undefined - we choose to
1287 do the intuitive thing. */
1288 else if (n > GET_MODE_BITSIZE (mode))
1289 n = GET_MODE_BITSIZE (mode);
1291 alg = get_shift_alg (shift_type, mode, n, &assembler, &cc_valid);
1294 switch (alg)
1296 case SHIFT_INLINE:
1297 /* Emit one bit shifts. */
1298 while (n > 0)
1300 output_asm_insn (assembler, operands);
1301 n -= 1;
1304 /* Keep track of CC. */
1305 if (cc_valid)
1307 cc_status.value1 = operands[0];
1308 cc_status.flags |= cc_valid;
1310 return "";
1312 case SHIFT_SPECIAL:
1313 output_asm_insn (assembler, operands);
1315 /* Keep track of CC. */
1316 if (cc_valid)
1318 cc_status.value1 = operands[0];
1319 cc_status.flags |= cc_valid;
1321 return "";
1325 fprintf (asm_out_file, "\tmov %d,%s\n", n,
1326 reg_names[REGNO (operands[4])]);
1327 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
1328 output_asm_insn (assembler, operands);
1329 output_asm_insn ("add -1,%4", operands);
1330 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
1331 return "";
1335 fprintf (asm_out_file, ".Llt%d:\n", loopend_lab);
1336 output_asm_insn (assembler, operands);
1337 output_asm_insn ("add -1,%4", operands);
1338 fprintf (asm_out_file, "\tbne .Llt%d\n", loopend_lab);
1339 fprintf (asm_out_file, ".Lle%d:\n", loopend_lab);
1341 return "";
1344 /* Return an RTX to represent where a value with mode MODE will be returned
1345 from a function. If the result is 0, the argument is pushed. */
1348 function_arg (cum, mode, type, named)
1349 CUMULATIVE_ARGS *cum;
1350 enum machine_mode mode;
1351 tree type;
1352 int named;
1354 rtx result = 0;
1355 int size, align;
1357 /* We only support using 2 data registers as argument registers. */
1358 int nregs = 2;
1360 /* Only pass named arguments in registers. */
1361 if (!named)
1362 return NULL_RTX;
1364 /* Figure out the size of the object to be passed. We lie and claim
1365 PSImode values are only two bytes since they fit in a single
1366 register. */
1367 if (mode == BLKmode)
1368 size = int_size_in_bytes (type);
1369 else if (mode == PSImode)
1370 size = 2;
1371 else
1372 size = GET_MODE_SIZE (mode);
1374 /* Figure out the alignment of the object to be passed. */
1375 align = size;
1377 cum->nbytes = (cum->nbytes + 1) & ~1;
1379 /* Don't pass this arg via a register if all the argument registers
1380 are used up. */
1381 if (cum->nbytes + size > nregs * UNITS_PER_WORD)
1382 return 0;
1384 switch (cum->nbytes / UNITS_PER_WORD)
1386 case 0:
1387 result = gen_rtx (REG, mode, 0);
1388 break;
1389 case 1:
1390 result = gen_rtx (REG, mode, 1);
1391 break;
1392 default:
1393 result = 0;
1396 return result;
1399 /* Return the number of registers to use for an argument passed partially
1400 in registers and partially in memory. */
1403 function_arg_partial_nregs (cum, mode, type, named)
1404 CUMULATIVE_ARGS *cum;
1405 enum machine_mode mode;
1406 tree type;
1407 int named;
1409 int size, align;
1411 /* We only support using 2 data registers as argument registers. */
1412 int nregs = 2;
1414 return 0;
1415 /* Only pass named arguments in registers. */
1416 if (!named)
1417 return 0;
1419 /* Figure out the size of the object to be passed. */
1420 if (mode == BLKmode)
1421 size = int_size_in_bytes (type);
1422 else if (mode == PSImode)
1423 size = 2;
1424 else
1425 size = GET_MODE_SIZE (mode);
1427 /* Figure out the alignment of the object to be passed. */
1428 align = size;
1430 cum->nbytes = (cum->nbytes + 1) & ~1;
1432 /* Don't pass this arg via a register if all the argument registers
1433 are used up. */
1434 if (cum->nbytes > nregs * UNITS_PER_WORD)
1435 return 0;
1437 if (cum->nbytes + size <= nregs * UNITS_PER_WORD)
1438 return 0;
1440 /* Don't pass this arg via a register if it would be split between
1441 registers and memory. */
1442 if (type == NULL_TREE
1443 && cum->nbytes + size > nregs * UNITS_PER_WORD)
1444 return 0;
1446 return (nregs * UNITS_PER_WORD - cum->nbytes) / UNITS_PER_WORD;
1449 char *
1450 output_tst (operand, insn)
1451 rtx operand, insn;
1454 rtx temp;
1455 int past_call = 0;
1457 /* Only tst insns using address registers can be optimized. */
1458 if (REGNO_REG_CLASS (REGNO (operand)) != ADDRESS_REGS)
1459 return "cmp 0,%0";
1461 /* If testing an address register against zero, we can do better if
1462 we know there's a register already holding the value zero. First
1463 see if a global register has been set to zero, else we do a search
1464 for a register holding zero, if both of those fail, then we use a
1465 compare against zero. */
1466 if (zero_dreg || zero_areg)
1468 rtx xoperands[2];
1469 xoperands[0] = operand;
1470 xoperands[1] = zero_dreg ? zero_dreg : zero_areg;
1472 output_asm_insn ("cmp %1,%0", xoperands);
1473 return "";
1476 /* We can save a byte if we can find a register which has the value
1477 zero in it. */
1478 temp = PREV_INSN (insn);
1479 while (temp)
1481 rtx set;
1483 /* We allow the search to go through call insns. We record
1484 the fact that we've past a CALL_INSN and reject matches which
1485 use call clobbered registers. */
1486 if (GET_CODE (temp) == CODE_LABEL
1487 || GET_CODE (temp) == JUMP_INSN
1488 || GET_CODE (temp) == BARRIER)
1489 break;
1491 if (GET_CODE (temp) == CALL_INSN)
1492 past_call = 1;
1494 if (GET_CODE (temp) == NOTE)
1496 temp = PREV_INSN (temp);
1497 continue;
1500 /* It must be an insn, see if it is a simple set. */
1501 set = single_set (temp);
1502 if (!set)
1504 temp = PREV_INSN (temp);
1505 continue;
1508 /* Are we setting a register to zero?
1510 If it's a call clobbered register, have we past a call? */
1511 if (REG_P (SET_DEST (set))
1512 && SET_SRC (set) == CONST0_RTX (GET_MODE (SET_DEST (set)))
1513 && !reg_set_between_p (SET_DEST (set), temp, insn)
1514 && (!past_call
1515 || !call_used_regs[REGNO (SET_DEST (set))]))
1517 rtx xoperands[2];
1518 xoperands[0] = operand;
1519 xoperands[1] = SET_DEST (set);
1521 output_asm_insn ("cmp %1,%0", xoperands);
1522 return "";
1524 temp = PREV_INSN (temp);
1526 return "cmp 0,%0";
1529 /* Return nonzero if OP is a valid operand for a {zero,sign}_extendpsisi
1530 instruction.
1532 It accepts anything that is a general operand or the sum of the
1533 stack pointer and a general operand. */
1534 extendpsi_operand (op, mode)
1535 rtx op;
1536 enum machine_mode mode;
1538 return (general_operand (op, mode)
1539 || (GET_CODE (op) == PLUS
1540 && XEXP (op, 0) == stack_pointer_rtx
1541 && general_operand (XEXP (op, 1), VOIDmode)));