]> gcc.gnu.org Git - gcc.git/blob - gcc/config/sparc/sparc.c
*** empty log message ***
[gcc.git] / gcc / config / sparc / sparc.c
1 /* Subroutines for insn-output.c for Sun SPARC.
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <stdio.h>
22 #include "config.h"
23 #include "tree.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 "expr.h"
35 #include "recog.h"
36
37 /* Global variables for machine-dependent things. */
38
39 /* Save the operands last given to a compare for use when we
40 generate a scc or bcc insn. */
41
42 rtx sparc_compare_op0, sparc_compare_op1;
43
44 /* We may need an epilogue if we spill too many registers.
45 If this is non-zero, then we branch here for the epilogue. */
46 static rtx leaf_label;
47
48 #ifdef LEAF_REGISTERS
49
50 /* Vector to say how input registers are mapped to output
51 registers. FRAME_POINTER_REGNUM cannot be remapped by
52 this function to eliminate it. You must use -fomit-frame-pointer
53 to get that. */
54 char leaf_reg_remap[] =
55 { 0, 1, 2, 3, 4, 5, 6, 7,
56 -1, -1, -1, -1, -1, -1, 14, -1,
57 -1, -1, -1, -1, -1, -1, -1, -1,
58 8, 9, 10, 11, 12, 13, -1, 15,
59
60 32, 33, 34, 35, 36, 37, 38, 39,
61 40, 41, 42, 43, 44, 45, 46, 47,
62 48, 49, 50, 51, 52, 53, 54, 55,
63 56, 57, 58, 59, 60, 61, 62, 63};
64
65 char leaf_reg_backmap[] =
66 { 0, 1, 2, 3, 4, 5, 6, 7,
67 24, 25, 26, 27, 28, 29, 14, 31,
68 -1, -1, -1, -1, -1, -1, -1, -1,
69 -1, -1, -1, -1, -1, -1, -1, -1,
70
71 32, 33, 34, 35, 36, 37, 38, 39,
72 40, 41, 42, 43, 44, 45, 46, 47,
73 48, 49, 50, 51, 52, 53, 54, 55,
74 56, 57, 58, 59, 60, 61, 62, 63};
75 #endif
76
77 /* Global variables set by FUNCTION_PROLOGUE. */
78 /* Size of frame. Need to know this to emit return insns from
79 leaf procedures. */
80 int apparent_fsize;
81 int actual_fsize;
82
83 /* Name of where we pretend to think the frame pointer points.
84 Normally, this is "%fp", but if we are in a leaf procedure,
85 this is "%sp+something". */
86 char *frame_base_name;
87
88 static rtx find_addr_reg ();
89
90 /* Return non-zero only if OP is a register of mode MODE,
91 or const0_rtx. */
92 int
93 reg_or_0_operand (op, mode)
94 rtx op;
95 enum machine_mode mode;
96 {
97 if (op == const0_rtx || register_operand (op, mode))
98 return 1;
99 if (GET_CODE (op) == CONST_DOUBLE
100 && CONST_DOUBLE_HIGH (op) == 0
101 && CONST_DOUBLE_LOW (op) == 0)
102 return 1;
103 return 0;
104 }
105
106 /* Nonzero if OP can appear as the dest of a RESTORE insn. */
107 int
108 restore_operand (op, mode)
109 rtx op;
110 enum machine_mode mode;
111 {
112 return (GET_CODE (op) == REG && GET_MODE (op) == mode
113 && (REGNO (op) < 8 || (REGNO (op) >= 24 && REGNO (op) < 32)));
114 }
115
116 /* PC-relative call insn on SPARC is independent of `memory_operand'. */
117
118 int
119 call_operand (op, mode)
120 rtx op;
121 enum machine_mode mode;
122 {
123 if (GET_CODE (op) != MEM)
124 abort ();
125 op = XEXP (op, 0);
126 return (REG_P (op) || CONSTANT_P (op));
127 }
128
129 int
130 call_operand_address (op, mode)
131 rtx op;
132 enum machine_mode mode;
133 {
134 return (REG_P (op) || CONSTANT_P (op));
135 }
136
137 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
138 reference and a constant. */
139
140 int
141 symbolic_operand (op, mode)
142 register rtx op;
143 enum machine_mode mode;
144 {
145 switch (GET_CODE (op))
146 {
147 case SYMBOL_REF:
148 case LABEL_REF:
149 return 1;
150
151 case CONST:
152 op = XEXP (op, 0);
153 return ((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
154 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
155 && GET_CODE (XEXP (op, 1)) == CONST_INT);
156
157 /* This clause seems to be irrelevant. */
158 case CONST_DOUBLE:
159 return GET_MODE (op) == mode;
160
161 default:
162 return 0;
163 }
164 }
165
166 /* Return truth value of statement that OP is a symbolic memory
167 operand of mode MODE. */
168
169 int
170 symbolic_memory_operand (op, mode)
171 rtx op;
172 enum machine_mode mode;
173 {
174 if (GET_CODE (op) == SUBREG)
175 op = SUBREG_REG (op);
176 if (GET_CODE (op) != MEM)
177 return 0;
178 op = XEXP (op, 0);
179 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST
180 || GET_CODE (op) == HIGH || GET_CODE (op) == LABEL_REF);
181 }
182
183 /* Return 1 if the operand is either a register or a memory operand that is
184 not symbolic. */
185
186 int
187 reg_or_nonsymb_mem_operand (op, mode)
188 register rtx op;
189 enum machine_mode mode;
190 {
191 if (register_operand (op, mode))
192 return 1;
193
194 if (memory_operand (op, mode) && ! symbolic_memory_operand (op, mode))
195 return 1;
196
197 return 0;
198 }
199
200 int
201 sparc_operand (op, mode)
202 rtx op;
203 enum machine_mode mode;
204 {
205 if (register_operand (op, mode))
206 return 1;
207 if (GET_CODE (op) == CONST_INT)
208 return SMALL_INT (op);
209 if (GET_MODE (op) != mode)
210 return 0;
211 if (GET_CODE (op) == SUBREG)
212 op = SUBREG_REG (op);
213 if (GET_CODE (op) != MEM)
214 return 0;
215
216 op = XEXP (op, 0);
217 if (GET_CODE (op) == LO_SUM)
218 return (GET_CODE (XEXP (op, 0)) == REG
219 && symbolic_operand (XEXP (op, 1), Pmode));
220 return memory_address_p (mode, op);
221 }
222
223 int
224 move_operand (op, mode)
225 rtx op;
226 enum machine_mode mode;
227 {
228 if (mode == DImode && arith_double_operand (op, mode))
229 return 1;
230 if (register_operand (op, mode))
231 return 1;
232 if (GET_CODE (op) == CONST_INT)
233 return (SMALL_INT (op) || (INTVAL (op) & 0x3ff) == 0);
234
235 if (GET_MODE (op) != mode)
236 return 0;
237 if (GET_CODE (op) == SUBREG)
238 op = SUBREG_REG (op);
239 if (GET_CODE (op) != MEM)
240 return 0;
241 op = XEXP (op, 0);
242 if (GET_CODE (op) == LO_SUM)
243 return (register_operand (XEXP (op, 0), Pmode)
244 && CONSTANT_P (XEXP (op, 1)));
245 return memory_address_p (mode, op);
246 }
247
248 int
249 move_pic_label (op, mode)
250 rtx op;
251 enum machine_mode mode;
252 {
253 /* Special case for PIC. */
254 if (flag_pic && GET_CODE (op) == LABEL_REF)
255 return 1;
256 return 0;
257 }
258 \f
259 /* The rtx for the global offset table which is a special form
260 that *is* a position independent symbolic constant. */
261 rtx pic_pc_rtx;
262
263 /* Ensure that we are not using patterns that are not OK with PIC. */
264
265 int
266 check_pic (i)
267 int i;
268 {
269 switch (flag_pic)
270 {
271 case 1:
272 if (GET_CODE (recog_operand[i]) == SYMBOL_REF
273 || (GET_CODE (recog_operand[i]) == CONST
274 && ! rtx_equal_p (pic_pc_rtx, recog_operand[i])))
275 abort ();
276 case 2:
277 default:
278 return 1;
279 }
280 }
281
282 /* Return true if X is an address which needs a temporary register when
283 reloaded while generating PIC code. */
284
285 int
286 pic_address_needs_scratch (x)
287 rtx x;
288 {
289 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
290 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
291 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
292 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
293 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
294 return 1;
295
296 return 0;
297 }
298
299 int
300 memop (op, mode)
301 rtx op;
302 enum machine_mode mode;
303 {
304 if (GET_CODE (op) == MEM)
305 return (mode == VOIDmode || mode == GET_MODE (op));
306 return 0;
307 }
308
309 /* Return truth value of whether OP is EQ or NE. */
310
311 int
312 eq_or_neq (op, mode)
313 rtx op;
314 enum machine_mode mode;
315 {
316 return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
317 }
318
319 /* Return 1 if this is a comparison operator, but not an EQ, NE, GEU,
320 or LTU for non-floating-point. We handle those specially. */
321
322 int
323 normal_comp_operator (op, mode)
324 rtx op;
325 enum machine_mode mode;
326 {
327 enum rtx_code code = GET_CODE (op);
328
329 if (GET_RTX_CLASS (code) != '<')
330 return 0;
331
332 if (GET_MODE (XEXP (op, 0)) == CCFPmode)
333 return 1;
334
335 return (code != NE && code != EQ && code != GEU && code != LTU);
336 }
337
338 /* Return 1 if this is a comparison operator. This allows the use of
339 MATCH_OPERATOR to recognize all the branch insns. */
340
341 int
342 noov_compare_op (op, mode)
343 register rtx op;
344 enum machine_mode mode;
345 {
346 enum rtx_code code = GET_CODE (op);
347
348 if (GET_RTX_CLASS (code) != '<')
349 return 0;
350
351 if (GET_MODE (XEXP (op, 0)) == CC_NOOVmode)
352 /* These are the only branches which work with CC_NOOVmode. */
353 return (code == EQ || code == NE || code == GE || code == LT);
354 return 1;
355 }
356
357 /* Return 1 if this is a SIGN_EXTEND or ZERO_EXTEND operation. */
358
359 int
360 extend_op (op, mode)
361 rtx op;
362 enum machine_mode mode;
363 {
364 return GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND;
365 }
366
367 /* Return nonzero if OP is an operator of mode MODE which can set
368 the condition codes explicitly. We do not include PLUS and MINUS
369 because these require CC_NOOVmode, which we handle explicitly. */
370
371 int
372 cc_arithop (op, mode)
373 rtx op;
374 enum machine_mode mode;
375 {
376 if (GET_CODE (op) == AND
377 || GET_CODE (op) == IOR
378 || GET_CODE (op) == XOR)
379 return 1;
380
381 return 0;
382 }
383
384 /* Return nonzero if OP is an operator of mode MODE which can bitwise
385 complement its second operand and set the condition codes explicitly. */
386
387 int
388 cc_arithopn (op, mode)
389 rtx op;
390 enum machine_mode mode;
391 {
392 /* XOR is not here because combine canonicalizes (xor (not ...) ...)
393 and (xor ... (not ...)) to (not (xor ...)). */
394 return (GET_CODE (op) == AND
395 || GET_CODE (op) == IOR);
396 }
397 \f
398 /* Return truth value of whether OP can be used as an operands in a three
399 address arithmetic insn (such as add %o1,7,%l2) of mode MODE. */
400
401 int
402 arith_operand (op, mode)
403 rtx op;
404 enum machine_mode mode;
405 {
406 return (register_operand (op, mode)
407 || (GET_CODE (op) == CONST_INT && SMALL_INT (op)));
408 }
409
410 /* Return truth value of whether OP is a register or a CONST_DOUBLE. */
411
412 int
413 arith_double_operand (op, mode)
414 rtx op;
415 enum machine_mode mode;
416 {
417 return (register_operand (op, mode)
418 || (GET_CODE (op) == CONST_DOUBLE
419 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
420 && (unsigned) (CONST_DOUBLE_LOW (op) + 0x1000) < 0x2000
421 && ((CONST_DOUBLE_HIGH (op) == -1
422 && (CONST_DOUBLE_LOW (op) & 0x1000) == 0x1000)
423 || (CONST_DOUBLE_HIGH (op) == 0
424 && (CONST_DOUBLE_LOW (op) & 0x1000) == 0)))
425 || (GET_CODE (op) == CONST_INT
426 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
427 && (unsigned) (INTVAL (op) + 0x1000) < 0x2000));
428 }
429
430 /* Return truth value of whether OP is a integer which fits the
431 range constraining immediate operands in three-address insns. */
432
433 int
434 small_int (op, mode)
435 rtx op;
436 enum machine_mode mode;
437 {
438 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
439 }
440
441 /* Return truth value of statement that OP is a call-clobbered register. */
442 int
443 clobbered_register (op, mode)
444 rtx op;
445 enum machine_mode mode;
446 {
447 return (GET_CODE (op) == REG && call_used_regs[REGNO (op)]);
448 }
449 \f
450 /* X and Y are two things to compare using CODE. Emit the compare insn and
451 return the rtx for register 0 in the proper mode. */
452
453 rtx
454 gen_compare_reg (code, x, y)
455 enum rtx_code code;
456 rtx x, y;
457 {
458 enum machine_mode mode = SELECT_CC_MODE (code, x);
459 rtx cc_reg = gen_rtx (REG, mode, 0);
460
461 emit_insn (gen_rtx (SET, VOIDmode, cc_reg,
462 gen_rtx (COMPARE, mode, x, y)));
463
464 return cc_reg;
465 }
466 \f
467 /* Return nonzero if a return peephole merging return with
468 setting of output register is ok. */
469 int
470 leaf_return_peephole_ok ()
471 {
472 return (actual_fsize == 0);
473 }
474
475 /* Return nonzero if TRIAL can go into the function epilogue's
476 delay slot. SLOT is the slot we are trying to fill. */
477
478 int
479 eligible_for_epilogue_delay (trial, slot)
480 rtx trial;
481 int slot;
482 {
483 static char *this_function_name;
484 rtx pat, src;
485
486 if (slot >= 1)
487 return 0;
488 if (GET_CODE (trial) != INSN
489 || GET_CODE (PATTERN (trial)) != SET)
490 return 0;
491 if (get_attr_length (trial) != 1)
492 return 0;
493
494 /* In the case of a true leaf function, anything can go into the delay slot.
495 A delay slot only exists however if the frame size is zero, otherwise
496 we will put an insn to adjust the stack after the return. */
497 if (leaf_function)
498 {
499 if (leaf_return_peephole_ok ())
500 return (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_TRUE);
501 return 0;
502 }
503
504 /* Otherwise, only operations which can be done in tandem with
505 a `restore' insn can go into the delay slot. */
506 pat = PATTERN (trial);
507 if (GET_CODE (SET_DEST (pat)) != REG
508 || REGNO (SET_DEST (pat)) == 0
509 || REGNO (SET_DEST (pat)) >= 32
510 || REGNO (SET_DEST (pat)) < 24)
511 return 0;
512
513 src = SET_SRC (pat);
514 if (arith_operand (src, GET_MODE (src)))
515 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
516 if (arith_double_operand (src, GET_MODE (src)))
517 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
518 if (GET_CODE (src) == PLUS)
519 {
520 if (register_operand (XEXP (src, 0), SImode)
521 && arith_operand (XEXP (src, 1), SImode))
522 return 1;
523 if (register_operand (XEXP (src, 1), SImode)
524 && arith_operand (XEXP (src, 0), SImode))
525 return 1;
526 if (register_operand (XEXP (src, 0), DImode)
527 && arith_double_operand (XEXP (src, 1), DImode))
528 return 1;
529 if (register_operand (XEXP (src, 1), DImode)
530 && arith_double_operand (XEXP (src, 0), DImode))
531 return 1;
532 }
533 if (GET_CODE (src) == MINUS
534 && register_operand (XEXP (src, 0), SImode)
535 && small_int (XEXP (src, 1), VOIDmode))
536 return 1;
537 if (GET_CODE (src) == MINUS
538 && register_operand (XEXP (src, 0), DImode)
539 && !register_operand (XEXP (src, 1), DImode)
540 && arith_double_operand (XEXP (src, 1), DImode))
541 return 1;
542 return 0;
543 }
544
545 int
546 short_branch (uid1, uid2)
547 int uid1, uid2;
548 {
549 unsigned int delta = insn_addresses[uid1] - insn_addresses[uid2];
550 if (delta + 1024 < 2048)
551 return 1;
552 /* warning ("long branch, distance %d", delta); */
553 return 0;
554 }
555
556 /* Return non-zero if REG is not used after INSN.
557 We assume REG is a reload reg, and therefore does
558 not live past labels or calls or jumps. */
559 int
560 reg_unused_after (reg, insn)
561 rtx reg;
562 rtx insn;
563 {
564 enum rtx_code code, prev_code = UNKNOWN;
565
566 while (insn = NEXT_INSN (insn))
567 {
568 if (prev_code == CALL_INSN && call_used_regs[REGNO (reg)])
569 return 1;
570
571 code = GET_CODE (insn);
572 if (GET_CODE (insn) == CODE_LABEL)
573 return 1;
574
575 if (GET_RTX_CLASS (code) == 'i')
576 {
577 rtx set = single_set (insn);
578 int in_src = set && reg_overlap_mentioned_p (reg, SET_SRC (set));
579 if (set && in_src)
580 return 0;
581 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
582 return 1;
583 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
584 return 0;
585 }
586 prev_code = code;
587 }
588 return 1;
589 }
590 \f
591 /* Legitimize PIC addresses. If the address is already position-independent,
592 we return ORIG. Newly generated position-independent addresses go into a
593 reg. This is REG if non zero, otherwise we allocate register(s) as
594 necessary. If this is called during reload, and we need a second temp
595 register, then we use SCRATCH, which is provided via the
596 SECONDARY_INPUT_RELOAD_CLASS mechanism. */
597
598 rtx
599 legitimize_pic_address (orig, mode, reg, scratch)
600 rtx orig;
601 enum machine_mode mode;
602 rtx reg, scratch;
603 {
604 if (GET_CODE (orig) == SYMBOL_REF)
605 {
606 rtx pic_ref, address;
607 rtx insn;
608
609 if (reg == 0)
610 {
611 if (reload_in_progress || reload_completed)
612 abort ();
613 else
614 reg = gen_reg_rtx (Pmode);
615 }
616
617 if (flag_pic == 2)
618 {
619 /* If not during reload, allocate another temp reg here for loading
620 in the address, so that these instructions can be optimized
621 properly. */
622 rtx temp_reg = ((reload_in_progress || reload_completed)
623 ? reg : gen_reg_rtx (Pmode));
624
625 /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
626 won't get confused into thinking that these two instructions
627 are loading in the true address of the symbol. If in the
628 future a PIC rtx exists, that should be used instead. */
629 emit_insn (gen_rtx (SET, VOIDmode, temp_reg,
630 gen_rtx (HIGH, Pmode,
631 gen_rtx (UNSPEC, Pmode,
632 gen_rtvec (1, orig),
633 0))));
634 emit_insn (gen_rtx (SET, VOIDmode, temp_reg,
635 gen_rtx (LO_SUM, Pmode, temp_reg,
636 gen_rtx (UNSPEC, Pmode,
637 gen_rtvec (1, orig),
638 0))));
639 address = temp_reg;
640 }
641 else
642 address = orig;
643
644 pic_ref = gen_rtx (MEM, Pmode,
645 gen_rtx (PLUS, Pmode,
646 pic_offset_table_rtx, address));
647 current_function_uses_pic_offset_table = 1;
648 RTX_UNCHANGING_P (pic_ref) = 1;
649 insn = emit_move_insn (reg, pic_ref);
650 /* Put a REG_EQUAL note on this insn, so that it can be optimized
651 by loop. */
652 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL, orig,
653 REG_NOTES (insn));
654 return reg;
655 }
656 else if (GET_CODE (orig) == CONST)
657 {
658 rtx base, offset;
659
660 if (GET_CODE (XEXP (orig, 0)) == PLUS
661 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
662 return orig;
663
664 if (reg == 0)
665 {
666 if (reload_in_progress || reload_completed)
667 abort ();
668 else
669 reg = gen_reg_rtx (Pmode);
670 }
671
672 if (GET_CODE (XEXP (orig, 0)) == PLUS)
673 {
674 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode,
675 reg, 0);
676 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
677 base == reg ? 0 : reg, 0);
678 }
679 else
680 abort ();
681
682 if (GET_CODE (offset) == CONST_INT)
683 {
684 if (SMALL_INT (offset))
685 return plus_constant_for_output (base, INTVAL (offset));
686 else if (! reload_in_progress && ! reload_completed)
687 offset = force_reg (Pmode, offset);
688 /* We can't create any new registers during reload, so use the
689 SCRATCH reg provided by the reload_insi pattern. */
690 else if (scratch)
691 {
692 emit_move_insn (scratch, offset);
693 offset = scratch;
694 }
695 else
696 /* If we reach here, then the SECONDARY_INPUT_RELOAD_CLASS
697 macro needs to be adjusted so that a scratch reg is provided
698 for this address. */
699 abort ();
700 }
701 return gen_rtx (PLUS, Pmode, base, offset);
702 }
703 else if (GET_CODE (orig) == LABEL_REF)
704 current_function_uses_pic_offset_table = 1;
705
706 return orig;
707 }
708
709 /* Set up PIC-specific rtl. This should not cause any insns
710 to be emitted. */
711
712 void
713 initialize_pic ()
714 {
715 }
716
717 /* Emit special PIC prologues and epilogues. */
718
719 void
720 finalize_pic ()
721 {
722 /* The table we use to reference PIC data. */
723 rtx global_offset_table;
724 /* Labels to get the PC in the prologue of this function. */
725 rtx l1, l2;
726 rtx seq;
727 int orig_flag_pic = flag_pic;
728
729 if (current_function_uses_pic_offset_table == 0)
730 return;
731
732 if (! flag_pic)
733 abort ();
734
735 flag_pic = 0;
736 l1 = gen_label_rtx ();
737 l2 = gen_label_rtx ();
738
739 start_sequence ();
740
741 emit_label (l1);
742 /* Note that we pun calls and jumps here! */
743 emit_jump_insn (gen_rtx (PARALLEL, VOIDmode,
744 gen_rtvec (2,
745 gen_rtx (SET, VOIDmode, pc_rtx, gen_rtx (LABEL_REF, VOIDmode, l2)),
746 gen_rtx (SET, VOIDmode, gen_rtx (REG, SImode, 15), gen_rtx (LABEL_REF, VOIDmode, l2)))));
747 emit_label (l2);
748
749 /* Initialize every time through, since we can't easily
750 know this to be permanent. */
751 global_offset_table = gen_rtx (SYMBOL_REF, Pmode, "*__GLOBAL_OFFSET_TABLE_");
752 pic_pc_rtx = gen_rtx (CONST, Pmode,
753 gen_rtx (MINUS, Pmode,
754 global_offset_table,
755 gen_rtx (CONST, Pmode,
756 gen_rtx (MINUS, Pmode,
757 gen_rtx (LABEL_REF, VOIDmode, l1),
758 pc_rtx))));
759
760 emit_insn (gen_rtx (SET, VOIDmode, pic_offset_table_rtx,
761 gen_rtx (HIGH, Pmode, pic_pc_rtx)));
762 emit_insn (gen_rtx (SET, VOIDmode,
763 pic_offset_table_rtx,
764 gen_rtx (LO_SUM, Pmode,
765 pic_offset_table_rtx, pic_pc_rtx)));
766 emit_insn (gen_rtx (SET, VOIDmode,
767 pic_offset_table_rtx,
768 gen_rtx (PLUS, Pmode,
769 pic_offset_table_rtx, gen_rtx (REG, Pmode, 15))));
770 /* emit_insn (gen_rtx (ASM_INPUT, VOIDmode, "!#PROLOGUE# 1")); */
771 LABEL_PRESERVE_P (l1) = 1;
772 LABEL_PRESERVE_P (l2) = 1;
773 flag_pic = orig_flag_pic;
774
775 seq = gen_sequence ();
776 end_sequence ();
777 emit_insn_after (seq, get_insns ());
778
779 /* Need to emit this whether or not we obey regdecls,
780 since setjmp/longjmp can cause life info to screw up. */
781 emit_insn (gen_rtx (USE, VOIDmode, pic_offset_table_rtx));
782 }
783 \f
784 /* For the SPARC, REG and REG+CONST is cost 0, REG+REG is cost 1,
785 and addresses involving symbolic constants are cost 2.
786
787 We make REG+REG slightly more expensive because it might keep
788 a register live for longer than we might like.
789
790 PIC addresses are very expensive.
791
792 It is no coincidence that this has the same structure
793 as GO_IF_LEGITIMATE_ADDRESS. */
794 int
795 sparc_address_cost (X)
796 rtx X;
797 {
798 #if 0
799 /* Handled before calling here. */
800 if (GET_CODE (X) == REG)
801 { return 1; }
802 #endif
803 if (GET_CODE (X) == PLUS)
804 {
805 if (GET_CODE (XEXP (X, 0)) == REG
806 && GET_CODE (XEXP (X, 1)) == REG)
807 return 2;
808 return 1;
809 }
810 else if (GET_CODE (X) == LO_SUM)
811 return 1;
812 else if (GET_CODE (X) == HIGH)
813 return 2;
814 return 4;
815 }
816 \f
817 /* Emit insns to move operands[1] into operands[0].
818
819 Return 1 if we have written out everything that needs to be done to
820 do the move. Otherwise, return 0 and the caller will emit the move
821 normally.
822
823 SCRATCH_REG if non zero can be used as a scratch register for the move
824 operation. It is provided by a SECONDARY_RELOAD_* macro if needed. */
825
826 int
827 emit_move_sequence (operands, mode, scratch_reg)
828 rtx *operands;
829 enum machine_mode mode;
830 rtx scratch_reg;
831 {
832 register rtx operand0 = operands[0];
833 register rtx operand1 = operands[1];
834
835 /* Handle most common case first: storing into a register. */
836 if (register_operand (operand0, mode))
837 {
838 if (register_operand (operand1, mode)
839 || (GET_CODE (operand1) == CONST_INT && SMALL_INT (operand1))
840 || (GET_CODE (operand1) == CONST_DOUBLE
841 && arith_double_operand (operand1, DImode))
842 || (GET_CODE (operand1) == HIGH && GET_MODE (operand1) != DImode)
843 /* Only `general_operands' can come here, so MEM is ok. */
844 || GET_CODE (operand1) == MEM)
845 {
846 /* Run this case quickly. */
847 emit_insn (gen_rtx (SET, VOIDmode, operand0, operand1));
848 return 1;
849 }
850 }
851 else if (GET_CODE (operand0) == MEM)
852 {
853 if (register_operand (operand1, mode) || operand1 == const0_rtx)
854 {
855 /* Run this case quickly. */
856 emit_insn (gen_rtx (SET, VOIDmode, operand0, operand1));
857 return 1;
858 }
859 if (! reload_in_progress)
860 {
861 operands[0] = validize_mem (operand0);
862 operands[1] = operand1 = force_reg (mode, operand1);
863 }
864 }
865
866 /* Simplify the source if we need to. Must handle DImode HIGH operators
867 here because such a move needs a clobber added. */
868 if ((GET_CODE (operand1) != HIGH && immediate_operand (operand1, mode))
869 || (GET_CODE (operand1) == HIGH && GET_MODE (operand1) == DImode))
870 {
871 if (flag_pic && symbolic_operand (operand1, mode))
872 {
873 rtx temp_reg = reload_in_progress ? operand0 : 0;
874
875 operands[1] = legitimize_pic_address (operand1, mode, temp_reg,
876 scratch_reg);
877 }
878 else if (GET_CODE (operand1) == CONST_INT
879 ? (! SMALL_INT (operand1)
880 && (INTVAL (operand1) & 0x3ff) != 0)
881 : (GET_CODE (operand1) == CONST_DOUBLE
882 ? ! arith_double_operand (operand1, DImode)
883 : 1))
884 {
885 /* For DImode values, temp must be operand0 because of the way
886 HI and LO_SUM work. The LO_SUM operator only copies half of
887 the LSW from the dest of the HI operator. If the LO_SUM dest is
888 not the same as the HI dest, then the MSW of the LO_SUM dest will
889 never be set.
890
891 ??? The real problem here is that the ...(HI:DImode pattern emits
892 multiple instructions, and the ...(LO_SUM:DImode pattern emits
893 one instruction. This fails, because the compiler assumes that
894 LO_SUM copies all bits of the first operand to its dest. Better
895 would be to have the HI pattern emit one instruction and the
896 LO_SUM pattern multiple instructions. Even better would be
897 to use four rtl insns. */
898 rtx temp = ((reload_in_progress || mode == DImode)
899 ? operand0 : gen_reg_rtx (mode));
900
901 emit_insn (gen_rtx (SET, VOIDmode, temp,
902 gen_rtx (HIGH, mode, operand1)));
903 operands[1] = gen_rtx (LO_SUM, mode, temp, operand1);
904 }
905 }
906
907 if (GET_CODE (operand1) == LABEL_REF && flag_pic)
908 {
909 /* The procedure for doing this involves using a call instruction to
910 get the pc into o7. We need to indicate this explicitly because
911 the tablejump pattern assumes that it can use this value also. */
912 emit_insn (gen_rtx (PARALLEL, VOIDmode,
913 gen_rtvec (2,
914 gen_rtx (SET, VOIDmode, operand0,
915 operand1),
916 gen_rtx (SET, VOIDmode,
917 gen_rtx (REG, mode, 15),
918 pc_rtx))));
919 return 1;
920 }
921
922 /* Now have insn-emit do whatever it normally does. */
923 return 0;
924 }
925 \f
926 /* Return the best assembler insn template
927 for moving operands[1] into operands[0] as a fullword. */
928
929 char *
930 singlemove_string (operands)
931 rtx *operands;
932 {
933 if (GET_CODE (operands[0]) == MEM)
934 {
935 if (GET_CODE (operands[1]) != MEM)
936 return "st %r1,%0";
937 else
938 abort ();
939 }
940 if (GET_CODE (operands[1]) == MEM)
941 return "ld %1,%0";
942 if (GET_CODE (operands[1]) == CONST_INT
943 && ! CONST_OK_FOR_LETTER_P (INTVAL (operands[1]), 'I'))
944 {
945 int i = INTVAL (operands[1]);
946
947 /* If all low order 10 bits are clear, then we only need a single
948 sethi insn to load the constant. */
949 if ((i & 0x000003FF) != 0)
950 return "sethi %%hi(%a1),%0\n\tor %0,%%lo(%a1),%0";
951 else
952 return "sethi %%hi(%a1),%0";
953 }
954 /* ??? Wrong if target is DImode? */
955 return "mov %1,%0";
956 }
957 \f
958 /* Output assembler code to perform a doubleword move insn
959 with operands OPERANDS. */
960
961 char *
962 output_move_double (operands)
963 rtx *operands;
964 {
965 enum { REGOP, OFFSOP, MEMOP, PUSHOP, POPOP, CNSTOP, RNDOP } optype0, optype1;
966 rtx latehalf[2];
967 rtx addreg0 = 0, addreg1 = 0;
968
969 /* First classify both operands. */
970
971 if (REG_P (operands[0]))
972 optype0 = REGOP;
973 else if (offsettable_memref_p (operands[0]))
974 optype0 = OFFSOP;
975 else if (GET_CODE (operands[0]) == MEM)
976 optype0 = MEMOP;
977 else
978 optype0 = RNDOP;
979
980 if (REG_P (operands[1]))
981 optype1 = REGOP;
982 else if (CONSTANT_P (operands[1]))
983 optype1 = CNSTOP;
984 else if (offsettable_memref_p (operands[1]))
985 optype1 = OFFSOP;
986 else if (GET_CODE (operands[1]) == MEM)
987 optype1 = MEMOP;
988 else
989 optype1 = RNDOP;
990
991 /* Check for the cases that the operand constraints are not
992 supposed to allow to happen. Abort if we get one,
993 because generating code for these cases is painful. */
994
995 if (optype0 == RNDOP || optype1 == RNDOP)
996 abort ();
997
998 /* If an operand is an unoffsettable memory ref, find a register
999 we can increment temporarily to make it refer to the second word. */
1000
1001 if (optype0 == MEMOP)
1002 addreg0 = find_addr_reg (XEXP (operands[0], 0));
1003
1004 if (optype1 == MEMOP)
1005 addreg1 = find_addr_reg (XEXP (operands[1], 0));
1006
1007 /* Ok, we can do one word at a time.
1008 Normally we do the low-numbered word first,
1009 but if either operand is autodecrementing then we
1010 do the high-numbered word first.
1011
1012 In either case, set up in LATEHALF the operands to use for the
1013 high-numbered (least significant) word and in some cases alter the
1014 operands in OPERANDS to be suitable for the low-numbered word. */
1015
1016 if (optype0 == REGOP)
1017 latehalf[0] = gen_rtx (REG, SImode, REGNO (operands[0]) + 1);
1018 else if (optype0 == OFFSOP)
1019 latehalf[0] = adj_offsettable_operand (operands[0], 4);
1020 else
1021 latehalf[0] = operands[0];
1022
1023 if (optype1 == REGOP)
1024 latehalf[1] = gen_rtx (REG, SImode, REGNO (operands[1]) + 1);
1025 else if (optype1 == OFFSOP)
1026 latehalf[1] = adj_offsettable_operand (operands[1], 4);
1027 else if (optype1 == CNSTOP)
1028 split_double (operands[1], &operands[1], &latehalf[1]);
1029 else
1030 latehalf[1] = operands[1];
1031
1032 /* If the first move would clobber the source of the second one,
1033 do them in the other order.
1034
1035 RMS says "This happens only for registers;
1036 such overlap can't happen in memory unless the user explicitly
1037 sets it up, and that is an undefined circumstance."
1038
1039 but it happens on the sparc when loading parameter registers,
1040 so I am going to define that circumstance, and make it work
1041 as expected. */
1042
1043 /* Easy case: try moving both words at once. */
1044 /* First check for moving between an even/odd register pair
1045 and a memory location. */
1046 if ((optype0 == REGOP && optype1 != REGOP && optype1 != CNSTOP
1047 && (REGNO (operands[0]) & 1) == 0)
1048 || (optype0 != REGOP && optype0 != CNSTOP && optype1 == REGOP
1049 && (REGNO (operands[1]) & 1) == 0))
1050 {
1051 rtx addr;
1052 rtx base, offset;
1053
1054 if (optype0 == REGOP)
1055 addr = operands[1];
1056 else
1057 addr = operands[0];
1058
1059 /* Now see if we can trust the address to be 8-byte aligned.
1060 Trust double-precision floats in global variables. */
1061
1062 if (GET_CODE (XEXP (addr, 0)) == LO_SUM && GET_MODE (addr) == DFmode)
1063 return (addr == operands[1] ? "ldd %1,%0" : "std %1,%0");
1064
1065 base = 0;
1066 if (GET_CODE (XEXP (addr, 0)) == PLUS)
1067 {
1068 rtx temp = XEXP (addr, 0);
1069 if (GET_CODE (XEXP (temp, 0)) == REG
1070 && GET_CODE (XEXP (temp, 1)) == CONST_INT)
1071 base = XEXP (temp, 0), offset = XEXP (temp, 1);
1072 }
1073 else if (GET_CODE (XEXP (addr, 0)) == REG)
1074 base = XEXP (addr, 0), offset = const0_rtx;
1075
1076 /* Trust round enough offsets from the stack or frame pointer.
1077 If TARGET_HOPE_ALIGN, trust round enough offset from any register
1078 for DFmode loads. If it is obviously unaligned, don't ever
1079 generate ldd or std. */
1080 if (base
1081 && (REGNO (base) == FRAME_POINTER_REGNUM
1082 || REGNO (base) == STACK_POINTER_REGNUM
1083 || (TARGET_HOPE_ALIGN && GET_MODE (addr) == DFmode)))
1084 {
1085 if ((INTVAL (offset) & 0x7) == 0)
1086 return (addr == operands[1] ? "ldd %1,%0" : "std %1,%0");
1087 }
1088 /* We know structs not on the stack are properly aligned. Since a
1089 double asks for 8-byte alignment, we know it must have got that
1090 if it is in a struct. But a DImode need not be 8-byte aligned,
1091 because it could be a struct containing two ints or pointers.
1092 Hence, a constant DFmode address will always be 8-byte aligned.
1093 Any DFmode access inside a struct will always be aligned.
1094 If TARGET_HOPE_ALIGN, then assume all doubles are aligned even if this
1095 is not a constant address. */
1096 else if (GET_MODE (addr) == DFmode
1097 && (CONSTANT_P (XEXP (addr, 0))
1098 || MEM_IN_STRUCT_P (addr)
1099 || TARGET_HOPE_ALIGN))
1100 return (addr == operands[1] ? "ldd %1,%0" : "std %1,%0");
1101 }
1102
1103 if (optype0 == REGOP && optype1 == REGOP
1104 && REGNO (operands[0]) == REGNO (latehalf[1]))
1105 {
1106 /* Make any unoffsettable addresses point at high-numbered word. */
1107 if (addreg0)
1108 output_asm_insn ("add %0,0x4,%0", &addreg0);
1109 if (addreg1)
1110 output_asm_insn ("add %0,0x4,%0", &addreg1);
1111
1112 /* Do that word. */
1113 output_asm_insn (singlemove_string (latehalf), latehalf);
1114
1115 /* Undo the adds we just did. */
1116 if (addreg0)
1117 output_asm_insn ("add %0,-0x4,%0", &addreg0);
1118 if (addreg1)
1119 output_asm_insn ("add %0,-0x4,%0", &addreg1);
1120
1121 /* Do low-numbered word. */
1122 return singlemove_string (operands);
1123 }
1124 else if (optype0 == REGOP && optype1 != REGOP
1125 && reg_overlap_mentioned_p (operands[0], operands[1]))
1126 {
1127 /* Do the late half first. */
1128 output_asm_insn (singlemove_string (latehalf), latehalf);
1129 /* Then clobber. */
1130 return singlemove_string (operands);
1131 }
1132
1133 /* Normal case: do the two words, low-numbered first. */
1134
1135 output_asm_insn (singlemove_string (operands), operands);
1136
1137 /* Make any unoffsettable addresses point at high-numbered word. */
1138 if (addreg0)
1139 output_asm_insn ("add %0,0x4,%0", &addreg0);
1140 if (addreg1)
1141 output_asm_insn ("add %0,0x4,%0", &addreg1);
1142
1143 /* Do that word. */
1144 output_asm_insn (singlemove_string (latehalf), latehalf);
1145
1146 /* Undo the adds we just did. */
1147 if (addreg0)
1148 output_asm_insn ("add %0,-0x4,%0", &addreg0);
1149 if (addreg1)
1150 output_asm_insn ("add %0,-0x4,%0", &addreg1);
1151
1152 return "";
1153 }
1154 \f
1155 /* Output assembler code to perform a doubleword move insn with perands
1156 OPERANDS, one of which must be a floating point register. */
1157
1158 char *
1159 output_fp_move_double (operands)
1160 rtx *operands;
1161 {
1162 rtx addr;
1163
1164 if (FP_REG_P (operands[0]))
1165 {
1166 if (FP_REG_P (operands[1]))
1167 return "fmovs %1,%0\n\tfmovs %R1,%R0";
1168 else if (GET_CODE (operands[1]) == REG)
1169 {
1170 if ((REGNO (operands[1]) & 1) == 0)
1171 return "std %1,[%@-8]\n\tldd [%@-8],%0";
1172 else
1173 return "st %R1,[%@-4]\n\tst %1,[%@-8]\n\tldd [%@-8],%0";
1174 }
1175 else
1176 return output_move_double (operands);
1177 }
1178 else if (FP_REG_P (operands[1]))
1179 {
1180 if (GET_CODE (operands[0]) == REG)
1181 {
1182 if ((REGNO (operands[0]) & 1) == 0)
1183 return "std %1,[%@-8]\n\tldd [%@-8],%0";
1184 else
1185 return "std %1,[%@-8]\n\tld [%@-4],%R0\n\tld [%@-8],%0";
1186 }
1187 else
1188 return output_move_double (operands);
1189 }
1190 else abort ();
1191 }
1192 \f
1193 /* Return a REG that occurs in ADDR with coefficient 1.
1194 ADDR can be effectively incremented by incrementing REG. */
1195
1196 static rtx
1197 find_addr_reg (addr)
1198 rtx addr;
1199 {
1200 while (GET_CODE (addr) == PLUS)
1201 {
1202 /* We absolutely can not fudge the frame pointer here, because the
1203 frame pointer must always be 8 byte aligned. It also confuses
1204 debuggers. */
1205 if (GET_CODE (XEXP (addr, 0)) == REG
1206 && REGNO (XEXP (addr, 0)) != FRAME_POINTER_REGNUM)
1207 addr = XEXP (addr, 0);
1208 else if (GET_CODE (XEXP (addr, 1)) == REG
1209 && REGNO (XEXP (addr, 1)) != FRAME_POINTER_REGNUM)
1210 addr = XEXP (addr, 1);
1211 else if (CONSTANT_P (XEXP (addr, 0)))
1212 addr = XEXP (addr, 1);
1213 else if (CONSTANT_P (XEXP (addr, 1)))
1214 addr = XEXP (addr, 0);
1215 else
1216 abort ();
1217 }
1218 if (GET_CODE (addr) == REG)
1219 return addr;
1220 abort ();
1221 }
1222
1223 void
1224 output_sized_memop (opname, mode, signedp)
1225 char *opname;
1226 enum machine_mode mode;
1227 int signedp;
1228 {
1229 static char *ld_size_suffix_u[] = { "ub", "uh", "", "?", "d" };
1230 static char *ld_size_suffix_s[] = { "sb", "sh", "", "?", "d" };
1231 static char *st_size_suffix[] = { "b", "h", "", "?", "d" };
1232 char **opnametab, *modename;
1233
1234 if (opname[0] == 'l')
1235 if (signedp)
1236 opnametab = ld_size_suffix_s;
1237 else
1238 opnametab = ld_size_suffix_u;
1239 else
1240 opnametab = st_size_suffix;
1241 modename = opnametab[GET_MODE_SIZE (mode) >> 1];
1242
1243 fprintf (asm_out_file, "\t%s%s", opname, modename);
1244 }
1245 \f
1246 void
1247 output_move_with_extension (operands)
1248 rtx *operands;
1249 {
1250 if (GET_MODE (operands[2]) == HImode)
1251 output_asm_insn ("sll %2,0x10,%0", operands);
1252 else if (GET_MODE (operands[2]) == QImode)
1253 output_asm_insn ("sll %2,0x18,%0", operands);
1254 else
1255 abort ();
1256 }
1257 \f
1258 /* Load the address specified by OPERANDS[3] into the register
1259 specified by OPERANDS[0].
1260
1261 OPERANDS[3] may be the result of a sum, hence it could either be:
1262
1263 (1) CONST
1264 (2) REG
1265 (2) REG + CONST_INT
1266 (3) REG + REG + CONST_INT
1267 (4) REG + REG (special case of 3).
1268
1269 Note that (3) is not a legitimate address.
1270 All cases are handled here. */
1271
1272 void
1273 output_load_address (operands)
1274 rtx *operands;
1275 {
1276 rtx base, offset;
1277
1278 if (CONSTANT_P (operands[3]))
1279 {
1280 output_asm_insn ("set %3,%0", operands);
1281 return;
1282 }
1283
1284 if (REG_P (operands[3]))
1285 {
1286 if (REGNO (operands[0]) != REGNO (operands[3]))
1287 output_asm_insn ("mov %3,%0", operands);
1288 return;
1289 }
1290
1291 if (GET_CODE (operands[3]) != PLUS)
1292 abort ();
1293
1294 base = XEXP (operands[3], 0);
1295 offset = XEXP (operands[3], 1);
1296
1297 if (GET_CODE (base) == CONST_INT)
1298 {
1299 rtx tmp = base;
1300 base = offset;
1301 offset = tmp;
1302 }
1303
1304 if (GET_CODE (offset) != CONST_INT)
1305 {
1306 /* Operand is (PLUS (REG) (REG)). */
1307 base = operands[3];
1308 offset = const0_rtx;
1309 }
1310
1311 if (REG_P (base))
1312 {
1313 operands[6] = base;
1314 operands[7] = offset;
1315 if (SMALL_INT (offset))
1316 output_asm_insn ("add %6,%7,%0", operands);
1317 else
1318 output_asm_insn ("set %7,%0\n\tadd %0,%6,%0", operands);
1319 }
1320 else if (GET_CODE (base) == PLUS)
1321 {
1322 operands[6] = XEXP (base, 0);
1323 operands[7] = XEXP (base, 1);
1324 operands[8] = offset;
1325
1326 if (SMALL_INT (offset))
1327 output_asm_insn ("add %6,%7,%0\n\tadd %0,%8,%0", operands);
1328 else
1329 output_asm_insn ("set %8,%0\n\tadd %0,%6,%0\n\tadd %0,%7,%0", operands);
1330 }
1331 else
1332 abort ();
1333 }
1334
1335 /* Output code to place a size count SIZE in register REG.
1336 ALIGN is the size of the unit of transfer.
1337
1338 Because block moves are pipelined, we don't include the
1339 first element in the transfer of SIZE to REG. */
1340
1341 static void
1342 output_size_for_block_move (size, reg, align)
1343 rtx size, reg;
1344 rtx align;
1345 {
1346 rtx xoperands[3];
1347
1348 xoperands[0] = reg;
1349 xoperands[1] = size;
1350 xoperands[2] = align;
1351 if (GET_CODE (size) == REG)
1352 output_asm_insn ("sub %1,%2,%0", xoperands);
1353 else
1354 {
1355 xoperands[1]
1356 = gen_rtx (CONST_INT, VOIDmode, INTVAL (size) - INTVAL (align));
1357 output_asm_insn ("set %1,%0", xoperands);
1358 }
1359 }
1360
1361 /* Emit code to perform a block move.
1362
1363 OPERANDS[0] is the destination.
1364 OPERANDS[1] is the source.
1365 OPERANDS[2] is the size.
1366 OPERANDS[3] is the alignment safe to use.
1367 OPERANDS[4] is a register we can safely clobber as a temp. */
1368
1369 char *
1370 output_block_move (operands)
1371 rtx *operands;
1372 {
1373 /* A vector for our computed operands. Note that load_output_address
1374 makes use of (and can clobber) up to the 8th element of this vector. */
1375 rtx xoperands[10];
1376 rtx zoperands[10];
1377 static int movstrsi_label = 0;
1378 int i;
1379 rtx temp1 = operands[4];
1380 rtx sizertx = operands[2];
1381 rtx alignrtx = operands[3];
1382 int align = INTVAL (alignrtx);
1383 char label3[30], label5[30];
1384
1385 xoperands[0] = operands[0];
1386 xoperands[1] = operands[1];
1387 xoperands[2] = temp1;
1388
1389 /* We can't move more than this many bytes at a time because we have only
1390 one register, %g1, to move them through. */
1391 if (align > UNITS_PER_WORD)
1392 {
1393 align = UNITS_PER_WORD;
1394 alignrtx = gen_rtx (CONST_INT, VOIDmode, UNITS_PER_WORD);
1395 }
1396
1397 /* We consider 8 ld/st pairs, for a total of 16 inline insns to be
1398 reasonable here. (Actually will emit a maximum of 18 inline insns for
1399 the case of size == 31 and align == 4). */
1400
1401 if (GET_CODE (sizertx) == CONST_INT && (INTVAL (sizertx) / align) <= 8
1402 && memory_address_p (QImode, plus_constant_for_output (xoperands[0],
1403 INTVAL (sizertx)))
1404 && memory_address_p (QImode, plus_constant_for_output (xoperands[1],
1405 INTVAL (sizertx))))
1406 {
1407 int size = INTVAL (sizertx);
1408 int offset = 0;
1409
1410 /* We will store different integers into this particular RTX. */
1411 xoperands[2] = rtx_alloc (CONST_INT);
1412 PUT_MODE (xoperands[2], VOIDmode);
1413
1414 /* This case is currently not handled. Abort instead of generating
1415 bad code. */
1416 if (align > 4)
1417 abort ();
1418
1419 if (align >= 4)
1420 {
1421 for (i = (size >> 2) - 1; i >= 0; i--)
1422 {
1423 INTVAL (xoperands[2]) = (i << 2) + offset;
1424 output_asm_insn ("ld [%a1+%2],%%g1\n\tst %%g1,[%a0+%2]",
1425 xoperands);
1426 }
1427 offset += (size & ~0x3);
1428 size = size & 0x3;
1429 if (size == 0)
1430 return "";
1431 }
1432
1433 if (align >= 2)
1434 {
1435 for (i = (size >> 1) - 1; i >= 0; i--)
1436 {
1437 INTVAL (xoperands[2]) = (i << 1) + offset;
1438 output_asm_insn ("lduh [%a1+%2],%%g1\n\tsth %%g1,[%a0+%2]",
1439 xoperands);
1440 }
1441 offset += (size & ~0x1);
1442 size = size & 0x1;
1443 if (size == 0)
1444 return "";
1445 }
1446
1447 if (align >= 1)
1448 {
1449 for (i = size - 1; i >= 0; i--)
1450 {
1451 INTVAL (xoperands[2]) = i + offset;
1452 output_asm_insn ("ldub [%a1+%2],%%g1\n\tstb %%g1,[%a0+%2]",
1453 xoperands);
1454 }
1455 return "";
1456 }
1457
1458 /* We should never reach here. */
1459 abort ();
1460 }
1461
1462 /* If the size isn't known to be a multiple of the alignment,
1463 we have to do it in smaller pieces. If we could determine that
1464 the size was a multiple of 2 (or whatever), we could be smarter
1465 about this. */
1466 if (GET_CODE (sizertx) != CONST_INT)
1467 align = 1;
1468 else
1469 {
1470 int size = INTVAL (sizertx);
1471 while (size % align)
1472 align >>= 1;
1473 }
1474
1475 if (align != INTVAL (alignrtx))
1476 alignrtx = gen_rtx (CONST_INT, VOIDmode, align);
1477
1478 xoperands[3] = gen_rtx (CONST_INT, VOIDmode, movstrsi_label++);
1479 xoperands[4] = gen_rtx (CONST_INT, VOIDmode, align);
1480 xoperands[5] = gen_rtx (CONST_INT, VOIDmode, movstrsi_label++);
1481
1482 ASM_GENERATE_INTERNAL_LABEL (label3, "Lm", INTVAL (xoperands[3]));
1483 ASM_GENERATE_INTERNAL_LABEL (label5, "Lm", INTVAL (xoperands[5]));
1484
1485 /* This is the size of the transfer. Emit code to decrement the size
1486 value by ALIGN, and store the result in the temp1 register. */
1487 output_size_for_block_move (sizertx, temp1, alignrtx);
1488
1489 /* Must handle the case when the size is zero or negative, so the first thing
1490 we do is compare the size against zero, and only copy bytes if it is
1491 zero or greater. Note that we have already subtracted off the alignment
1492 once, so we must copy 1 alignment worth of bytes if the size is zero
1493 here.
1494
1495 The SUN assembler complains about labels in branch delay slots, so we
1496 do this before outputting the load address, so that there will always
1497 be a harmless insn between the branch here and the next label emitted
1498 below. */
1499
1500 {
1501 char pattern[100];
1502
1503 sprintf (pattern, "cmp %%2,0\n\tbl %s", &label5[1]);
1504 output_asm_insn (pattern, xoperands);
1505 }
1506
1507 zoperands[0] = operands[0];
1508 zoperands[3] = plus_constant_for_output (operands[0], align);
1509 output_load_address (zoperands);
1510
1511 /* ??? This might be much faster if the loops below were preconditioned
1512 and unrolled.
1513
1514 That is, at run time, copy enough bytes one at a time to ensure that the
1515 target and source addresses are aligned to the the largest possible
1516 alignment. Then use a preconditioned unrolled loop to copy say 16
1517 bytes at a time. Then copy bytes one at a time until finish the rest. */
1518
1519 /* Output the first label separately, so that it is spaced properly. */
1520
1521 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "Lm", INTVAL (xoperands[3]));
1522
1523 {
1524 char pattern[200];
1525 register char *ld_suffix = (align == 1) ? "ub" : (align == 2) ? "uh" : "";
1526 register char *st_suffix = (align == 1) ? "b" : (align == 2) ? "h" : "";
1527
1528 sprintf (pattern, "ld%s [%%1+%%2],%%%%g1\n\tsubcc %%2,%%4,%%2\n\tbge %s\n\tst%s %%%%g1,[%%0+%%2]\n%s:", ld_suffix, &label3[1], st_suffix, &label5[1]);
1529 output_asm_insn (pattern, xoperands);
1530 }
1531
1532 return "";
1533 }
1534 \f
1535 /* Output reasonable peephole for set-on-condition-code insns.
1536 Note that these insns assume a particular way of defining
1537 labels. Therefore, *both* sparc.h and this function must
1538 be changed if a new syntax is needed. */
1539
1540 char *
1541 output_scc_insn (operands, insn)
1542 rtx operands[];
1543 rtx insn;
1544 {
1545 static char string[100];
1546 rtx label = 0, next = insn;
1547 int need_label = 0;
1548
1549 /* Try doing a jump optimization which jump.c can't do for us
1550 because we did not expose that setcc works by using branches.
1551
1552 If this scc insn is followed by an unconditional branch, then have
1553 the jump insn emitted here jump to that location, instead of to
1554 the end of the scc sequence as usual. */
1555
1556 do
1557 {
1558 if (GET_CODE (next) == CODE_LABEL)
1559 label = next;
1560 next = NEXT_INSN (next);
1561 if (next == 0)
1562 break;
1563 }
1564 while (GET_CODE (next) == NOTE || GET_CODE (next) == CODE_LABEL);
1565
1566 /* If we are in a sequence, and the following insn is a sequence also,
1567 then just following the current insn's next field will take us to the
1568 first insn of the next sequence, which is the wrong place. We don't
1569 want to optimize with a branch that has had its delay slot filled.
1570 Avoid this by verifying that NEXT_INSN (PREV_INSN (next)) == next
1571 which fails only if NEXT is such a branch. */
1572
1573 if (next && GET_CODE (next) == JUMP_INSN && simplejump_p (next)
1574 && (! final_sequence || NEXT_INSN (PREV_INSN (next)) == next))
1575 label = JUMP_LABEL (next);
1576 /* If not optimizing, jump label fields are not set. To be safe, always
1577 check here to whether label is still zero. */
1578 if (label == 0)
1579 {
1580 label = gen_label_rtx ();
1581 need_label = 1;
1582 }
1583
1584 LABEL_NUSES (label) += 1;
1585
1586 operands[2] = label;
1587
1588 /* If we are in a delay slot, assume it is the delay slot of an fpcc
1589 insn since our type isn't allowed anywhere else. */
1590
1591 /* ??? Fpcc instructions no longer have delay slots, so this code is
1592 probably obsolete. */
1593
1594 /* The fastest way to emit code for this is an annulled branch followed
1595 by two move insns. This will take two cycles if the branch is taken,
1596 and three cycles if the branch is not taken.
1597
1598 However, if we are in the delay slot of another branch, this won't work,
1599 because we can't put a branch in the delay slot of another branch.
1600 The above sequence would effectively take 3 or 4 cycles respectively
1601 since a no op would have be inserted between the two branches.
1602 In this case, we want to emit a move, annulled branch, and then the
1603 second move. This sequence always takes 3 cycles, and hence is faster
1604 when we are in a branch delay slot. */
1605
1606 if (final_sequence)
1607 {
1608 strcpy (string, "mov 0,%0\n\t");
1609 strcat (string, output_cbranch (operands[1], 2, 0, 1, 0));
1610 strcat (string, "\n\tmov 1,%0");
1611 }
1612 else
1613 {
1614 strcpy (string, output_cbranch (operands[1], 2, 0, 1, 0));
1615 strcat (string, "\n\tmov 1,%0\n\tmov 0,%0");
1616 }
1617
1618 if (need_label)
1619 strcat (string, "\n%l2:");
1620
1621 return string;
1622 }
1623 \f
1624 /* Vectors to keep interesting information about registers where
1625 it can easily be got. */
1626
1627 /* Modes for condition codes. */
1628 #define C_MODES \
1629 ((1 << (int) CCmode) | (1 << (int) CC_NOOVmode) | (1 << (int) CCFPmode))
1630
1631 /* Modes for single-word (and smaller) quantities. */
1632 #define S_MODES \
1633 (~C_MODES \
1634 & ~ ((1 << (int) DImode) | (1 << (int) TImode) \
1635 | (1 << (int) DFmode) | (1 << (int) TFmode)))
1636
1637 /* Modes for double-word (and smaller) quantities. */
1638 #define D_MODES \
1639 (~C_MODES \
1640 & ~ ((1 << (int) TImode) | (1 << (int) TFmode)))
1641
1642 /* Modes for quad-word quantities. */
1643 #define T_MODES (~C_MODES)
1644
1645 /* Modes for single-float quantities. */
1646 #define SF_MODES ((1 << (int) SFmode))
1647
1648 /* Modes for double-float quantities. */
1649 #define DF_MODES (SF_MODES | (1 << (int) DFmode) | (1 << (int) SCmode))
1650
1651 /* Modes for quad-float quantities. */
1652 #define TF_MODES (DF_MODES | (1 << (int) TFmode) | (1 << (int) DCmode))
1653
1654 /* Value is 1 if register/mode pair is acceptable on sparc.
1655 The funny mixture of D and T modes is because integer operations
1656 do not specially operate on tetra quantities, so non-quad-aligned
1657 registers can hold quadword quantities (except %o4 and %i4 because
1658 they cross fixed registers. */
1659
1660 int hard_regno_mode_ok[] = {
1661 C_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
1662 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
1663 T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
1664 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
1665
1666 TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
1667 TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
1668 TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
1669 TF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES};
1670 \f
1671 #ifdef __GNUC__
1672 inline
1673 #endif
1674 static int
1675 save_regs (file, low, high, base, offset, n_fregs)
1676 FILE *file;
1677 int low, high;
1678 char *base;
1679 int offset;
1680 int n_fregs;
1681 {
1682 int i;
1683
1684 for (i = low; i < high; i += 2)
1685 {
1686 if (regs_ever_live[i] && ! call_used_regs[i])
1687 if (regs_ever_live[i+1] && ! call_used_regs[i+1])
1688 fprintf (file, "\tstd %s,[%s+%d]\n",
1689 reg_names[i], base, offset + 4 * n_fregs),
1690 n_fregs += 2;
1691 else
1692 fprintf (file, "\tst %s,[%s+%d]\n",
1693 reg_names[i], base, offset + 4 * n_fregs),
1694 n_fregs += 2;
1695 else if (regs_ever_live[i+1] && ! call_used_regs[i+1])
1696 fprintf (file, "\tst %s,[%s+%d]\n",
1697 reg_names[i+1], base, offset + 4 * n_fregs),
1698 n_fregs += 2;
1699 }
1700 return n_fregs;
1701 }
1702
1703 #ifdef __GNUC__
1704 inline
1705 #endif
1706 static int
1707 restore_regs (file, low, high, base, offset, n_fregs)
1708 FILE *file;
1709 int low, high;
1710 char *base;
1711 int offset;
1712 {
1713 int i;
1714
1715 for (i = low; i < high; i += 2)
1716 {
1717 if (regs_ever_live[i] && ! call_used_regs[i])
1718 if (regs_ever_live[i+1] && ! call_used_regs[i+1])
1719 fprintf (file, "\tldd [%s+%d], %s\n",
1720 base, offset + 4 * n_fregs, reg_names[i]),
1721 n_fregs += 2;
1722 else
1723 fprintf (file, "\tld [%s+%d],%s\n",
1724 base, offset + 4 * n_fregs, reg_names[i]),
1725 n_fregs += 2;
1726 else if (regs_ever_live[i+1] && ! call_used_regs[i+1])
1727 fprintf (file, "\tld [%s+%d],%s\n",
1728 base, offset + 4 * n_fregs, reg_names[i+1]),
1729 n_fregs += 2;
1730 }
1731 return n_fregs;
1732 }
1733
1734 /* Static variables we want to share between prologue and epilogue. */
1735
1736 /* Number of live floating point registers needed to be saved. */
1737 static int num_fregs;
1738
1739 /* Nonzero if any floating point register was ever used. */
1740 static int fregs_ever_live;
1741
1742 int
1743 compute_frame_size (size, leaf_function)
1744 int size;
1745 int leaf_function;
1746 {
1747 int fregs_ever_live = 0;
1748 int n_fregs = 0, i;
1749 int outgoing_args_size = (current_function_outgoing_args_size
1750 + REG_PARM_STACK_SPACE (current_function_decl));
1751
1752 apparent_fsize = ((size) + 7 - STARTING_FRAME_OFFSET) & -8;
1753 for (i = 32; i < FIRST_PSEUDO_REGISTER; i += 2)
1754 fregs_ever_live |= regs_ever_live[i]|regs_ever_live[i+1];
1755
1756 if (TARGET_EPILOGUE && fregs_ever_live)
1757 {
1758 for (i = 32; i < FIRST_PSEUDO_REGISTER; i += 2)
1759 if ((regs_ever_live[i] && ! call_used_regs[i])
1760 || (regs_ever_live[i+1] && ! call_used_regs[i+1]))
1761 n_fregs += 2;
1762 }
1763
1764 /* Set up values for use in `function_epilogue'. */
1765 num_fregs = n_fregs;
1766
1767 apparent_fsize += (outgoing_args_size+7) & -8;
1768 if (leaf_function && n_fregs == 0
1769 && apparent_fsize == (REG_PARM_STACK_SPACE (current_function_decl)
1770 - STARTING_FRAME_OFFSET))
1771 apparent_fsize = 0;
1772
1773 actual_fsize = apparent_fsize + n_fregs*4;
1774
1775 /* Make sure nothing can clobber our register windows.
1776 If a SAVE must be done, or there is a stack-local variable,
1777 the register window area must be allocated. */
1778 if (leaf_function == 0 || size > 0)
1779 actual_fsize += (16 * UNITS_PER_WORD)+8;
1780
1781 return actual_fsize;
1782 }
1783
1784 /* Output code for the function prologue. */
1785
1786 void
1787 output_function_prologue (file, size, leaf_function)
1788 FILE *file;
1789 int size;
1790 int leaf_function;
1791 {
1792 if (leaf_function)
1793 frame_base_name = "%sp+80";
1794 else
1795 frame_base_name = "%fp";
1796
1797 /* Need to use actual_fsize, since we are also allocating
1798 space for our callee (and our own register save area). */
1799 actual_fsize = compute_frame_size (size, leaf_function);
1800
1801 fprintf (file, "\t!#PROLOGUE# 0\n");
1802 if (actual_fsize == 0)
1803 /* do nothing. */ ;
1804 else if (actual_fsize <= 4096)
1805 {
1806 if (! leaf_function)
1807 fprintf (file, "\tsave %%sp,-%d,%%sp\n", actual_fsize);
1808 else
1809 fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize);
1810 }
1811 else if (actual_fsize <= 8192)
1812 {
1813 /* For frames in the range 4097..8192, we can use just two insns. */
1814 if (! leaf_function)
1815 {
1816 fprintf (file, "\tsave %%sp,-4096,%%sp\n");
1817 fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize - 4096);
1818 }
1819 else
1820 {
1821 fprintf (file, "\tadd %%sp,-4096,%%sp\n");
1822 fprintf (file, "\tadd %%sp,-%d,%%sp\n", actual_fsize - 4096);
1823 }
1824 }
1825 else
1826 {
1827 if (! leaf_function)
1828 {
1829 fprintf (file, "\tsethi %%hi(-%d),%%g1\n", actual_fsize);
1830 if ((actual_fsize & 0x3ff) != 0)
1831 fprintf (file, "\tor %%g1,%%lo(-%d),%%g1\n", actual_fsize);
1832 fprintf (file, "\tsave %%sp,%%g1,%%sp\n");
1833 }
1834 else
1835 {
1836 fprintf (file, "\tsethi %%hi(-%d),%%g1\n", actual_fsize);
1837 if ((actual_fsize & 0x3ff) != 0)
1838 fprintf (file, "\tor %%g1,%%lo(-%d),%%g1\n", actual_fsize);
1839 fprintf (file, "\tadd %%sp,%%g1,%%sp\n");
1840 }
1841 }
1842
1843 /* If doing anything with PIC, do it now. */
1844 if (! flag_pic)
1845 fprintf (file, "\t!#PROLOGUE# 1\n");
1846
1847 /* Figure out where to save any special registers. */
1848 if (num_fregs)
1849 {
1850 int offset, n_fregs = num_fregs;
1851
1852 if (! leaf_function)
1853 offset = -apparent_fsize;
1854 else
1855 offset = 0;
1856
1857 if (TARGET_EPILOGUE && ! leaf_function)
1858 n_fregs = save_regs (file, 0, 16, frame_base_name, offset, 0);
1859 else if (leaf_function)
1860 n_fregs = save_regs (file, 0, 32, frame_base_name, offset, 0);
1861 if (TARGET_EPILOGUE)
1862 save_regs (file, 32, FIRST_PSEUDO_REGISTER,
1863 frame_base_name, offset, n_fregs);
1864 }
1865
1866 if (regs_ever_live[62])
1867 fprintf (file, "\tst %s,[%s-16]\n\tst %s,[%s-12]\n",
1868 reg_names[0], frame_base_name,
1869 reg_names[0], frame_base_name);
1870
1871 leaf_label = 0;
1872 if (leaf_function && actual_fsize != 0)
1873 {
1874 /* warning ("leaf procedure with frame size %d", actual_fsize); */
1875 if (! TARGET_EPILOGUE)
1876 leaf_label = gen_label_rtx ();
1877 }
1878 }
1879
1880 /* Output code for the function epilogue. */
1881
1882 void
1883 output_function_epilogue (file, size, leaf_function)
1884 FILE *file;
1885 int size;
1886 int leaf_function;
1887 {
1888 int n_fregs, i;
1889 char *ret;
1890
1891 if (leaf_label)
1892 {
1893 emit_label_after (leaf_label, get_last_insn ());
1894 final_scan_insn (get_last_insn (), file, 0, 0, 1);
1895 }
1896
1897 if (num_fregs)
1898 {
1899 int offset, n_fregs = num_fregs;
1900
1901 if (! leaf_function)
1902 offset = -apparent_fsize;
1903 else
1904 offset = 0;
1905
1906 if (TARGET_EPILOGUE && ! leaf_function)
1907 n_fregs = restore_regs (file, 0, 16, frame_base_name, offset, 0);
1908 else if (leaf_function)
1909 n_fregs = restore_regs (file, 0, 32, frame_base_name, offset, 0);
1910 if (TARGET_EPILOGUE)
1911 restore_regs (file, 32, FIRST_PSEUDO_REGISTER,
1912 frame_base_name, offset, n_fregs);
1913 }
1914
1915 /* Work out how to skip the caller's unimp instruction if required. */
1916 if (leaf_function)
1917 ret = (current_function_returns_struct ? "jmp %o7+12" : "retl");
1918 else
1919 ret = (current_function_returns_struct ? "jmp %i7+12" : "ret");
1920
1921 if (TARGET_EPILOGUE || leaf_label)
1922 {
1923 int old_target_epilogue = TARGET_EPILOGUE;
1924 target_flags &= ~old_target_epilogue;
1925
1926 if (! leaf_function)
1927 {
1928 /* If we wound up with things in our delay slot, flush them here. */
1929 if (current_function_epilogue_delay_list)
1930 {
1931 rtx insn = emit_jump_insn_after (gen_rtx (RETURN, VOIDmode),
1932 get_last_insn ());
1933 PATTERN (insn) = gen_rtx (PARALLEL, VOIDmode,
1934 gen_rtvec (2,
1935 PATTERN (XEXP (current_function_epilogue_delay_list, 0)),
1936 PATTERN (insn)));
1937 final_scan_insn (insn, file, 1, 0, 1);
1938 }
1939 else
1940 fprintf (file, "\t%s\n\trestore\n", ret);
1941 }
1942 /* All of the following cases are for leaf functions. */
1943 else if (current_function_epilogue_delay_list)
1944 {
1945 /* eligible_for_epilogue_delay_slot ensures that if this is a
1946 leaf function, then we will only have insn in the delay slot
1947 if the frame size is zero, thus no adjust for the stack is
1948 needed here. */
1949 if (actual_fsize != 0)
1950 abort ();
1951 fprintf (file, "\t%s\n", ret);
1952 final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
1953 file, 1, 0, 1);
1954 }
1955 else if (actual_fsize <= 4096)
1956 fprintf (file, "\t%s\n\tsub %%sp,-%d,%%sp\n", ret, actual_fsize);
1957 else if (actual_fsize <= 8192)
1958 fprintf (file, "\tsub %%sp,-4096,%%sp\n\t%s\n\tsub %%sp,-%d,%%sp\n",
1959 ret, actual_fsize - 4096);
1960 else if ((actual_fsize & 0x3ff) == 0)
1961 fprintf (file, "\tsethi %%hi(%d),%%g1\n\t%s\n\tadd %%sp,%%g1,%%sp\n",
1962 actual_fsize, ret);
1963 else
1964 fprintf (file, "\tsethi %%hi(%d),%%g1\n\tor %%g1,%%lo(%d),%%g1\n\t%s\n\tadd %%sp,%%g1,%%sp\n",
1965 actual_fsize, actual_fsize, ret);
1966 target_flags |= old_target_epilogue;
1967 }
1968 }
1969 \f
1970 /* Return the string to output a conditional branch to LABEL, which is
1971 the operand number of the label. OP is the conditional expression. The
1972 mode of register 0 says what kind of comparison we made.
1973
1974 REVERSED is non-zero if we should reverse the sense of the comparison.
1975
1976 ANNUL is non-zero if we should generate an annulling branch.
1977
1978 NOOP is non-zero if we have to follow this branch by a noop. */
1979
1980 char *
1981 output_cbranch (op, label, reversed, annul, noop)
1982 rtx op;
1983 int label;
1984 int reversed, annul, noop;
1985 {
1986 static char string[20];
1987 enum rtx_code code = GET_CODE (op);
1988 enum machine_mode mode = GET_MODE (XEXP (op, 0));
1989 static char labelno[] = " %lX";
1990
1991 /* ??? FP branches can not be preceded by another floating point insn.
1992 Because there is currently no concept of pre-delay slots, we can fix
1993 this only by always emitting a nop before a floating point branch. */
1994
1995 if (mode == CCFPmode)
1996 strcpy (string, "nop\n\t");
1997
1998 /* If not floating-point or if EQ or NE, we can just reverse the code. */
1999 if (reversed && (mode != CCFPmode || code == EQ || code == NE))
2000 code = reverse_condition (code), reversed = 0;
2001
2002 /* Start by writing the branch condition. */
2003 switch (code)
2004 {
2005 case NE:
2006 if (mode == CCFPmode)
2007 strcat (string, "fbne");
2008 else
2009 strcpy (string, "bne");
2010 break;
2011
2012 case EQ:
2013 if (mode == CCFPmode)
2014 strcat (string, "fbe");
2015 else
2016 strcpy (string, "be");
2017 break;
2018
2019 case GE:
2020 if (mode == CCFPmode)
2021 {
2022 if (reversed)
2023 strcat (string, "fbul");
2024 else
2025 strcat (string, "fbge");
2026 }
2027 else if (mode == CC_NOOVmode)
2028 strcpy (string, "bpos");
2029 else
2030 strcpy (string, "bge");
2031 break;
2032
2033 case GT:
2034 if (mode == CCFPmode)
2035 {
2036 if (reversed)
2037 strcat (string, "fbule");
2038 else
2039 strcat (string, "fbg");
2040 }
2041 else
2042 strcpy (string, "bg");
2043 break;
2044
2045 case LE:
2046 if (mode == CCFPmode)
2047 {
2048 if (reversed)
2049 strcat (string, "fbug");
2050 else
2051 strcat (string, "fble");
2052 }
2053 else
2054 strcpy (string, "ble");
2055 break;
2056
2057 case LT:
2058 if (mode == CCFPmode)
2059 {
2060 if (reversed)
2061 strcat (string, "fbuge");
2062 else
2063 strcat (string, "fbl");
2064 }
2065 else if (mode == CC_NOOVmode)
2066 strcpy (string, "bneg");
2067 else
2068 strcpy (string, "bl");
2069 break;
2070
2071 case GEU:
2072 strcpy (string, "bgeu");
2073 break;
2074
2075 case GTU:
2076 strcpy (string, "bgu");
2077 break;
2078
2079 case LEU:
2080 strcpy (string, "bleu");
2081 break;
2082
2083 case LTU:
2084 strcpy (string, "blu");
2085 break;
2086 }
2087
2088 /* Now add the annulling, the label, and a possible noop. */
2089 if (annul)
2090 strcat (string, ",a");
2091
2092 labelno[3] = label + '0';
2093 strcat (string, labelno);
2094
2095 if (noop)
2096 strcat (string, "\n\tnop");
2097
2098 return string;
2099 }
2100
2101 char *
2102 output_return (operands)
2103 rtx *operands;
2104 {
2105 if (leaf_label)
2106 {
2107 operands[0] = leaf_label;
2108 return "b,a %l0";
2109 }
2110 else if (leaf_function)
2111 {
2112 /* If we didn't allocate a frame pointer for the current function,
2113 the stack pointer might have been adjusted. Output code to
2114 restore it now. */
2115
2116 operands[0] = gen_rtx (CONST_INT, VOIDmode, actual_fsize);
2117
2118 /* Use sub of negated value in first two cases instead of add to
2119 allow actual_fsize == 4096. */
2120
2121 if (actual_fsize <= 4096)
2122 {
2123 if (current_function_returns_struct)
2124 return "jmp %%o7+12\n\tsub %%sp,-%0,%%sp";
2125 else
2126 return "retl\n\tsub %%sp,-%0,%%sp";
2127 }
2128 else if (actual_fsize <= 8192)
2129 {
2130 operands[0] = gen_rtx (CONST_INT, VOIDmode, actual_fsize - 4096);
2131 if (current_function_returns_struct)
2132 return "sub %%sp,-4096,%%sp\n\tjmp %%o7+12\n\tsub %%sp,-%0,%%sp";
2133 else
2134 return "sub %%sp,-4096,%%sp\n\tretl\n\tsub %%sp,-%0,%%sp";
2135 }
2136 else if (current_function_returns_struct)
2137 {
2138 if ((actual_fsize & 0x3ff) != 0)
2139 return "sethi %%hi(%a0),%%g1\n\tor %%g1,%%lo(%a0),%%g1\n\tjmp %%o7+12\n\tadd %%sp,%%g1,%%sp";
2140 else
2141 return "sethi %%hi(%a0),%%g1\n\tjmp %%o7+12\n\tadd %%sp,%%g1,%%sp";
2142 }
2143 else
2144 {
2145 if ((actual_fsize & 0x3ff) != 0)
2146 return "sethi %%hi(%a0),%%g1\n\tor %%g1,%%lo(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
2147 else
2148 return "sethi %%hi(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
2149 }
2150 }
2151 else
2152 {
2153 if (current_function_returns_struct)
2154 return "jmp %%i7+12\n\trestore";
2155 else
2156 return "ret\n\trestore";
2157 }
2158 }
2159
2160 char *
2161 output_floatsisf2 (operands)
2162 rtx *operands;
2163 {
2164 if (GET_CODE (operands[1]) == MEM)
2165 return "ld %1,%0\n\tfitos %0,%0";
2166 else if (FP_REG_P (operands[1]))
2167 return "fitos %1,%0";
2168 return "st %r1,[%%fp-4]\n\tld [%%fp-4],%0\n\tfitos %0,%0";
2169 }
2170
2171 char *
2172 output_floatsidf2 (operands)
2173 rtx *operands;
2174 {
2175 if (GET_CODE (operands[1]) == MEM)
2176 return "ld %1,%0\n\tfitod %0,%0";
2177 else if (FP_REG_P (operands[1]))
2178 return "fitod %1,%0";
2179 return "st %r1,[%%fp-4]\n\tld [%%fp-4],%0\n\tfitod %0,%0";
2180 }
2181 \f
2182 /* Leaf functions and non-leaf functions have different needs. */
2183
2184 static int
2185 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
2186
2187 static int
2188 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
2189
2190 static int *reg_alloc_orders[] = {
2191 reg_leaf_alloc_order,
2192 reg_nonleaf_alloc_order};
2193
2194 void
2195 order_regs_for_local_alloc ()
2196 {
2197 static int last_order_nonleaf = 1;
2198
2199 if (regs_ever_live[15] != last_order_nonleaf)
2200 {
2201 last_order_nonleaf = !last_order_nonleaf;
2202 bcopy (reg_alloc_orders[last_order_nonleaf], reg_alloc_order,
2203 FIRST_PSEUDO_REGISTER * sizeof (int));
2204 }
2205 }
2206 \f
2207 /* Machine dependent routines for the branch probability, arc profiling
2208 code. */
2209
2210 /* The label used by the arc profiling code. */
2211
2212 static rtx profiler_label;
2213
2214 void
2215 init_arc_profiler ()
2216 {
2217 /* Generate and save a copy of this so it can be shared. */
2218 profiler_label = gen_rtx (SYMBOL_REF, Pmode, "*LPBX2");
2219 }
2220
2221 void
2222 output_arc_profiler (arcno, insert_after)
2223 int arcno;
2224 rtx insert_after;
2225 {
2226 rtx profiler_target_addr
2227 = gen_rtx (CONST, Pmode,
2228 gen_rtx (PLUS, Pmode, profiler_label,
2229 gen_rtx (CONST_INT, VOIDmode, 4 * arcno)));
2230 register rtx profiler_reg = gen_reg_rtx (SImode);
2231 register rtx address_reg = gen_reg_rtx (Pmode);
2232 rtx mem_ref;
2233
2234 insert_after = emit_insn_after (gen_rtx (SET, VOIDmode, address_reg,
2235 gen_rtx (HIGH, Pmode,
2236 profiler_target_addr)),
2237 insert_after);
2238
2239 mem_ref = gen_rtx (MEM, SImode, gen_rtx (LO_SUM, Pmode, address_reg,
2240 profiler_target_addr));
2241 insert_after = emit_insn_after (gen_rtx (SET, VOIDmode, profiler_reg,
2242 mem_ref),
2243 insert_after);
2244
2245 insert_after = emit_insn_after (gen_rtx (SET, VOIDmode, profiler_reg,
2246 gen_rtx (PLUS, SImode, profiler_reg,
2247 const1_rtx)),
2248 insert_after);
2249
2250 /* This is the same rtx as above, but it is not legal to share this rtx. */
2251 mem_ref = gen_rtx (MEM, SImode, gen_rtx (LO_SUM, Pmode, address_reg,
2252 profiler_target_addr));
2253 emit_insn_after (gen_rtx (SET, VOIDmode, mem_ref, profiler_reg),
2254 insert_after);
2255 }
2256
2257 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
2258 This makes them candidates for using ldd and std insns.
2259
2260 Note reg1 and reg2 *must* be hard registers. To be sure we will
2261 abort if we are passed pseudo registers. */
2262
2263 int
2264 registers_ok_for_ldd (reg1, reg2)
2265 rtx reg1, reg2;
2266 {
2267
2268 /* We might have been passed a SUBREG. */
2269 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
2270 return 0;
2271
2272 /* Should never happen. */
2273 if (REGNO (reg1) > FIRST_PSEUDO_REGISTER
2274 || REGNO (reg2) > FIRST_PSEUDO_REGISTER)
2275 abort ();
2276
2277 if (REGNO (reg1) % 2 != 0)
2278 return 0;
2279
2280 return (REGNO (reg1) == REGNO (reg2) - 1);
2281
2282 }
2283
2284 /* Return 1 if addr1 and addr2 are suitable for use in an ldd or
2285 std insn.
2286
2287 This can only happen when addr1 and addr2 are consecutive memory
2288 locations (addr1 + 4 == addr2). addr1 must also be aligned on a
2289 64 bit boundary (addr1 % 8 == 0).
2290
2291 We know %sp and %fp are kept aligned on a 64 bit boundary. Other
2292 registers are assumed to *never* be properly aligned and are
2293 rejected.
2294
2295 Knowing %sp and %fp are kept aligned on a 64 bit boundary, we
2296 need only check that the offset for addr1 % 8 == 0. */
2297
2298 int
2299 memory_ok_for_ldd (addr1, addr2)
2300 rtx addr1, addr2;
2301 {
2302 int reg1, offset1;
2303
2304 /* Extract a register number and offset (if used) from the first addr. */
2305 if (GET_CODE (addr1) == PLUS)
2306 {
2307 /* If not a REG, return zero. */
2308 if (GET_CODE (XEXP (addr1, 0)) != REG)
2309 return 0;
2310 else
2311 {
2312 reg1 = REGNO (XEXP (addr1, 0));
2313 /* The offset must be constant! */
2314 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
2315 return 0;
2316 offset1 = INTVAL (XEXP (addr1, 1));
2317 }
2318 }
2319 else if (GET_CODE (addr1) != REG)
2320 return 0;
2321 else
2322 {
2323 reg1 = REGNO (addr1);
2324 /* This was a simple (mem (reg)) expression. Offset is 0. */
2325 offset1 = 0;
2326 }
2327
2328 /* Make sure the second address is a (mem (plus (reg) (const_int). */
2329 if (GET_CODE (addr2) != PLUS)
2330 return 0;
2331
2332 if (GET_CODE (XEXP (addr2, 0)) != REG
2333 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
2334 return 0;
2335
2336 /* Only %fp and %sp are allowed. Additionally both addresses must
2337 use the same register. */
2338 if (reg1 != FRAME_POINTER_REGNUM && reg1 != STACK_POINTER_REGNUM)
2339 return 0;
2340
2341 if (reg1 != REGNO (XEXP (addr2, 0)))
2342 return 0;
2343
2344 /* The first offset must be evenly divisable by 8 to ensure the
2345 address is 64 bit aligned. */
2346 if (offset1 % 8 != 0)
2347 return 0;
2348
2349 /* The offset for the second addr must be 4 more than the first addr. */
2350 if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
2351 return 0;
2352
2353 /* All the tests passed. addr1 and addr2 are valid for ldd and std
2354 instructions. */
2355 return 1;
2356 }
2357 \f
2358 /* Print operand X (an rtx) in assembler syntax to file FILE.
2359 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
2360 For `%' followed by punctuation, CODE is the punctuation and X is null. */
2361
2362 void
2363 print_operand (file, x, code)
2364 FILE *file;
2365 rtx x;
2366 int code;
2367 {
2368 switch (code)
2369 {
2370 case '#':
2371 /* Output a 'nop' if there's nothing for the delay slot. */
2372 if (dbr_sequence_length () == 0)
2373 fputs ("\n\tnop", file);
2374 return;
2375 case '*':
2376 /* Output an annul flag if there's nothing for the delay slot. */
2377 if (dbr_sequence_length () == 0)
2378 fputs (",a", file);
2379 return;
2380 case 'Y':
2381 /* Adjust the operand to take into account a RESTORE operation. */
2382 if (GET_CODE (x) != REG)
2383 abort ();
2384 if (REGNO (x) < 8)
2385 fputs (reg_names[REGNO (x)], file);
2386 else if (REGNO (x) >= 24 && REGNO (x) < 32)
2387 fputs (reg_names[REGNO (x)-16], file);
2388 else
2389 abort ();
2390 return;
2391 case '@':
2392 /* Print out what we are using as the frame pointer. This might
2393 be %fp, or might be %sp+offset. */
2394 fputs (frame_base_name, file);
2395 return;
2396 case 'R':
2397 /* Print out the second register name of a register pair.
2398 I.e., R (%o0) => %o1. */
2399 fputs (reg_names[REGNO (x)+1], file);
2400 return;
2401 case 'm':
2402 /* Print the operand's address only. */
2403 output_address (XEXP (x, 0));
2404 return;
2405 case 'r':
2406 /* In this case we need a register. Use %g0 if the
2407 operand in const0_rtx. */
2408 if (x == const0_rtx)
2409 {
2410 fputs ("%g0", file);
2411 return;
2412 }
2413 else
2414 break;
2415
2416 case 'A':
2417 switch (GET_CODE (x))
2418 {
2419 case IOR: fputs ("or", file); break;
2420 case AND: fputs ("and", file); break;
2421 case XOR: fputs ("xor", file); break;
2422 default: abort ();
2423 }
2424 return;
2425
2426 case 'B':
2427 switch (GET_CODE (x))
2428 {
2429 case IOR: fputs ("orn", file); break;
2430 case AND: fputs ("andn", file); break;
2431 case XOR: fputs ("xnor", file); break;
2432 default: abort ();
2433 }
2434 return;
2435
2436 case 'b':
2437 {
2438 /* Print a sign-extended character. */
2439 int i = INTVAL (x) & 0xff;
2440 if (i & 0x80)
2441 i |= 0xffffff00;
2442 fprintf (file, "%d", i);
2443 return;
2444 }
2445
2446 case 0:
2447 /* Do nothing special. */
2448 break;
2449
2450 default:
2451 /* Undocumented flag. */
2452 abort ();
2453 }
2454
2455 if (GET_CODE (x) == REG)
2456 fputs (reg_names[REGNO (x)], file);
2457 else if (GET_CODE (x) == MEM)
2458 {
2459 fputc ('[', file);
2460 if (CONSTANT_P (XEXP (x, 0)))
2461 /* Poor Sun assembler doesn't understand absolute addressing. */
2462 fputs ("%g0+", file);
2463 output_address (XEXP (x, 0));
2464 fputc (']', file);
2465 }
2466 else if (GET_CODE (x) == HIGH)
2467 {
2468 fputs ("%hi(", file);
2469 output_addr_const (file, XEXP (x, 0));
2470 fputc (')', file);
2471 }
2472 else if (GET_CODE (x) == LO_SUM)
2473 {
2474 print_operand (file, XEXP (x, 0), 0);
2475 fputs ("+%lo(", file);
2476 output_addr_const (file, XEXP (x, 1));
2477 fputc (')', file);
2478 }
2479 else if (GET_CODE (x) == CONST_DOUBLE)
2480 {
2481 if (CONST_DOUBLE_HIGH (x) == 0)
2482 fprintf (file, "%u", CONST_DOUBLE_LOW (x));
2483 else if (CONST_DOUBLE_HIGH (x) == -1
2484 && CONST_DOUBLE_LOW (x) < 0)
2485 fprintf (file, "%d", CONST_DOUBLE_LOW (x));
2486 else
2487 abort ();
2488 }
2489 else { output_addr_const (file, x); }
2490 }
2491 \f
2492 /* This function outputs assembler code for VALUE to FILE, where VALUE is
2493 a 64 bit (DImode) value. */
2494
2495 /* ??? If there is a 64 bit counterpart to .word that the assembler
2496 understands, then using that would simply this code greatly. */
2497
2498 void
2499 output_double_int (file, value)
2500 FILE *file;
2501 rtx value;
2502 {
2503 if (GET_CODE (value) == CONST_INT)
2504 {
2505 if (INTVAL (value) < 0)
2506 ASM_OUTPUT_INT (file, constm1_rtx);
2507 else
2508 ASM_OUTPUT_INT (file, const0_rtx);
2509 ASM_OUTPUT_INT (file, value);
2510 }
2511 else if (GET_CODE (value) == CONST_DOUBLE)
2512 {
2513 ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode,
2514 CONST_DOUBLE_HIGH (value)));
2515 ASM_OUTPUT_INT (file, gen_rtx (CONST_INT, VOIDmode,
2516 CONST_DOUBLE_LOW (value)));
2517 }
2518 else if (GET_CODE (value) == SYMBOL_REF
2519 || GET_CODE (value) == CONST
2520 || GET_CODE (value) == PLUS)
2521 {
2522 /* Addresses are only 32 bits. */
2523 ASM_OUTPUT_INT (file, const0_rtx);
2524 ASM_OUTPUT_INT (file, value);
2525 }
2526 else
2527 abort ();
2528 }
2529 \f
2530 /* Compute the code to put in the .proc statement
2531 for a function that returns type TYPE. */
2532
2533 unsigned long
2534 sparc_type_code (type)
2535 register tree type;
2536 {
2537 register unsigned long qualifiers = 0;
2538 register unsigned shift = 6;
2539
2540 for (;;)
2541 {
2542 switch (TREE_CODE (type))
2543 {
2544 case ERROR_MARK:
2545 return qualifiers;
2546
2547 case ARRAY_TYPE:
2548 qualifiers |= (3 << shift);
2549 shift += 2;
2550 type = TREE_TYPE (type);
2551 break;
2552
2553 case FUNCTION_TYPE:
2554 case METHOD_TYPE:
2555 qualifiers |= (2 << shift);
2556 shift += 2;
2557 type = TREE_TYPE (type);
2558 break;
2559
2560 case POINTER_TYPE:
2561 case REFERENCE_TYPE:
2562 case OFFSET_TYPE:
2563 qualifiers |= (1 << shift);
2564 shift += 2;
2565 type = TREE_TYPE (type);
2566 break;
2567
2568 case RECORD_TYPE:
2569 return (qualifiers | 8);
2570
2571 case UNION_TYPE:
2572 return (qualifiers | 9);
2573
2574 case ENUMERAL_TYPE:
2575 return (qualifiers | 10);
2576
2577 case VOID_TYPE:
2578 return (qualifiers | 16);
2579
2580 case INTEGER_TYPE:
2581 /* This return value is not always completely the same as Sun's
2582 but the Sun assembler's peephole optimizer probably doesn't
2583 care. */
2584 return (qualifiers | 4);
2585
2586 case REAL_TYPE:
2587 if (TYPE_PRECISION (type) == 32)
2588 return (qualifiers | 6);
2589 else
2590 return (qualifiers | 7); /* Who knows? */
2591
2592 case COMPLEX_TYPE: /* GNU Fortran COMPLEX type. */
2593 case CHAR_TYPE: /* GNU Pascal CHAR type. Not used in C. */
2594 case BOOLEAN_TYPE: /* GNU Fortran BOOLEAN type. */
2595 case FILE_TYPE: /* GNU Pascal FILE type. */
2596 case STRING_TYPE: /* GNU Fortran STRING type. */
2597 case LANG_TYPE: /* ? */
2598 abort ();
2599
2600 default:
2601 abort (); /* Not a type! */
2602 }
2603 }
2604 }
2605 \f
2606 #ifdef HANDLE_PRAGMA
2607
2608 /* Handle a pragma directive. HANDLE_PRAGMA conspires to parse the
2609 input following #pragma into tokens based on yylex. TOKEN is the
2610 current token, and STRING is its printable form. */
2611
2612 void
2613 handle_pragma_token (string, token)
2614 char *string;
2615 tree token;
2616 {
2617 static enum pragma_state
2618 {
2619 ps_start,
2620 ps_done,
2621 ps_bad,
2622 ps_weak,
2623 ps_name,
2624 ps_equals,
2625 ps_value,
2626 } state = ps_start, type;
2627 static char *name;
2628 static char *value;
2629 static int align;
2630
2631 if (string == 0)
2632 {
2633 #ifdef WEAK_ASM_OP
2634 if (type == ps_weak)
2635 {
2636 if (state == ps_name || state == ps_value)
2637 {
2638 fprintf (asm_out_file, "\t%s\t", WEAK_ASM_OP);
2639 ASM_OUTPUT_LABELREF (asm_out_file, name);
2640 fputc ('\n', asm_out_file);
2641 if (state == ps_value)
2642 {
2643 fputc ('\t', asm_out_file);
2644 ASM_OUTPUT_LABELREF (asm_out_file, name);
2645 fputs (" = ", asm_out_file);
2646 ASM_OUTPUT_LABELREF (asm_out_file, value);
2647 fputc ('\n', asm_out_file);
2648 }
2649 }
2650 else if (! (state == ps_done || state == ps_start))
2651 warning ("ignoring malformed #pragma weak symbol [=value]");
2652 }
2653 #endif /* WEAK_ASM_OP */
2654
2655 type = state = ps_start;
2656 return;
2657 }
2658
2659 switch (state)
2660 {
2661 case ps_start:
2662 if (token && TREE_CODE (token) == IDENTIFIER_NODE)
2663 {
2664 #ifdef WEAK_ASM_OP
2665 if (strcmp (IDENTIFIER_POINTER (token), "weak") == 0)
2666 type = state = ps_weak;
2667 else
2668 #endif
2669 type = state = ps_done;
2670 }
2671 else
2672 type = state = ps_done;
2673 break;
2674
2675 #ifdef WEAK_ASM_OP
2676 case ps_weak:
2677 if (token && TREE_CODE (token) == IDENTIFIER_NODE)
2678 {
2679 name = IDENTIFIER_POINTER (token);
2680 state = ps_name;
2681 }
2682 else
2683 state = ps_bad;
2684 break;
2685
2686 case ps_name:
2687 state = (strcmp (string, "=") ? ps_bad : ps_equals);
2688 break;
2689
2690 case ps_equals:
2691 if (token && TREE_CODE (token) == IDENTIFIER_NODE)
2692 {
2693 value = IDENTIFIER_POINTER (token);
2694 state = ps_value;
2695 }
2696 else
2697 state = ps_bad;
2698 break;
2699
2700 case ps_value:
2701 state = ps_bad;
2702 break;
2703 #endif /* WEAK_ASM_OP */
2704
2705 case ps_bad:
2706 case ps_done:
2707 break;
2708
2709 default:
2710 abort ();
2711 }
2712 }
2713 #endif /* HANDLE_PRAGMA */
This page took 0.164085 seconds and 6 git commands to generate.