1 // Based on cmd/internal/obj/ppc64/obj9.go.
3 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
4 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
5 // Portions Copyright © 1997-1999 Vita Nuova Limited
6 // Portions Copyright © 2000-2008 Vita Nuova Holdings Limited (www.vitanuova.com)
7 // Portions Copyright © 2004,2006 Bruce Ellis
8 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
9 // Revisions Copyright © 2000-2008 Lucent Technologies Inc. and others
10 // Portions Copyright © 2009 The Go Authors. All rights reserved.
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to deal
14 // in the Software without restriction, including without limitation the rights
15 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 // copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
19 // The above copyright notice and this permission notice shall be included in
20 // all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 func progedit(ctxt *obj.Link, p *obj.Prog) {
43 // Rewrite BR/BL to symbol as TYPE_BRANCH.
51 p.To.Type = obj.TYPE_BRANCH
55 // Rewrite float constants to values stored in memory unless they are +0.
58 if p.From.Type == obj.TYPE_FCONST {
59 f32 := float32(p.From.Val.(float64))
60 i32 := math.Float32bits(f32)
64 literal := fmt.Sprintf("$f32.%08x", i32)
65 s := obj.Linklookup(ctxt, literal, 0)
67 p.From.Type = obj.TYPE_MEM
69 p.From.Sym.Local = true
70 p.From.Name = obj.NAME_EXTERN
75 if p.From.Type == obj.TYPE_FCONST {
76 i64 := math.Float64bits(p.From.Val.(float64))
80 literal := fmt.Sprintf("$f64.%016x", i64)
81 s := obj.Linklookup(ctxt, literal, 0)
83 p.From.Type = obj.TYPE_MEM
85 p.From.Sym.Local = true
86 p.From.Name = obj.NAME_EXTERN
90 // put constants not loadable by LOAD IMMEDIATE into memory
92 if p.From.Type == obj.TYPE_CONST {
94 if int64(int32(val)) != val &&
95 int64(uint32(val)) != val &&
96 int64(uint64(val)&(0xffffffff<<32)) != val {
97 literal := fmt.Sprintf("$i64.%016x", uint64(p.From.Offset))
98 s := obj.Linklookup(ctxt, literal, 0)
100 p.From.Type = obj.TYPE_MEM
102 p.From.Sym.Local = true
103 p.From.Name = obj.NAME_EXTERN
109 // Rewrite SUB constants into ADD.
112 if p.From.Type == obj.TYPE_CONST {
113 p.From.Offset = -p.From.Offset
118 if p.From.Type == obj.TYPE_CONST {
119 p.From.Offset = -p.From.Offset
124 if ctxt.Flag_dynlink {
125 rewriteToUseGot(ctxt, p)
129 // Rewrite p, if necessary, to access global data via the global offset table.
130 func rewriteToUseGot(ctxt *obj.Link, p *obj.Prog) {
131 // At the moment EXRL instructions are not emitted by the compiler and only reference local symbols in
137 // We only care about global data: NAME_EXTERN means a global
138 // symbol in the Go sense, and p.Sym.Local is true for a few
139 // internally defined symbols.
140 if p.From.Type == obj.TYPE_ADDR && p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local {
141 // MOVD $sym, Rx becomes MOVD sym@GOT, Rx
142 // MOVD $sym+<off>, Rx becomes MOVD sym@GOT, Rx; ADD <off>, Rx
143 if p.To.Type != obj.TYPE_REG || p.As != AMOVD {
144 ctxt.Diag("do not know how to handle LEA-type insn to non-register in %v with -dynlink", p)
146 p.From.Type = obj.TYPE_MEM
147 p.From.Name = obj.NAME_GOTREF
149 if p.From.Offset != 0 {
150 q = obj.Appendp(ctxt, p)
152 q.From.Type = obj.TYPE_CONST
153 q.From.Offset = p.From.Offset
158 if p.From3 != nil && p.From3.Name == obj.NAME_EXTERN {
159 ctxt.Diag("don't know how to handle %v with -dynlink", p)
162 // MOVD sym, Ry becomes MOVD sym@GOT, REGTMP; MOVD (REGTMP), Ry
163 // MOVD Ry, sym becomes MOVD sym@GOT, REGTMP; MOVD Ry, (REGTMP)
164 // An addition may be inserted between the two MOVs if there is an offset.
165 if p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local {
166 if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local {
167 ctxt.Diag("cannot handle NAME_EXTERN on both sides in %v with -dynlink", p)
170 } else if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local {
175 if p.As == obj.ATEXT || p.As == obj.AFUNCDATA || p.As == obj.ACALL || p.As == obj.ARET || p.As == obj.AJMP {
178 if source.Sym.Type == obj.STLSBSS {
181 if source.Type != obj.TYPE_MEM {
182 ctxt.Diag("don't know how to handle %v with -dynlink", p)
184 p1 := obj.Appendp(ctxt, p)
185 p2 := obj.Appendp(ctxt, p1)
188 p1.From.Type = obj.TYPE_MEM
189 p1.From.Sym = source.Sym
190 p1.From.Name = obj.NAME_GOTREF
191 p1.To.Type = obj.TYPE_REG
197 if p.From.Name == obj.NAME_EXTERN {
199 p2.From.Name = obj.NAME_NONE
201 } else if p.To.Name == obj.NAME_EXTERN {
203 p2.To.Name = obj.NAME_NONE
211 func preprocess(ctxt *obj.Link, cursym *obj.LSym) {
212 // TODO(minux): add morestack short-cuts with small fixed frame-size.
215 if cursym.Text == nil || cursym.Text.Link == nil {
220 textstksiz := p.To.Offset
221 if textstksiz == -8 {
222 // Compatibility hack.
223 p.From3.Offset |= obj.NOFRAME
226 if textstksiz%8 != 0 {
227 ctxt.Diag("frame size %d not a multiple of 8", textstksiz)
229 if p.From3.Offset&obj.NOFRAME != 0 {
231 ctxt.Diag("NOFRAME functions must have a frame size of 0, not %d", textstksiz)
235 cursym.Args = p.To.Val.(int32)
236 cursym.Locals = int32(textstksiz)
239 * find leaf subroutines
242 * expand BECOME pseudo
244 if ctxt.Debugvlog != 0 {
245 fmt.Fprintf(ctxt.Bso, "%5.2f noops\n", obj.Cputime())
251 for p := cursym.Text; p != nil; p = p.Link {
253 /* too hard, just leave alone */
257 p.Mark |= LABEL | LEAF | SYNC
264 if p.To.Type == obj.TYPE_REG {
265 if p.To.Reg == REGZERO {
266 p.Mark |= LABEL | SYNC
273 p.Mark |= LABEL | SYNC
276 case AMOVW, AMOVWZ, AMOVD:
278 if p.From.Reg >= REG_RESERVED || p.To.Reg >= REG_RESERVED {
279 p.Mark |= LABEL | SYNC
307 cursym.Text.Mark &^= LEAF
336 for q1.As == obj.ANOP {
341 if q1.Mark&LEAF == 0 {
355 p.Mark |= FCMP | FLOAT
367 q.Link = q1 /* q is non-nop */
382 var pPreempt *obj.Prog
384 for p := cursym.Text; p != nil; p = p.Link {
388 autosize = int32(textstksiz)
390 if p.Mark&LEAF != 0 && autosize == 0 && p.From3.Offset&obj.NOFRAME == 0 {
391 // A leaf function with no locals has no frame.
392 p.From3.Offset |= obj.NOFRAME
395 if p.From3.Offset&obj.NOFRAME == 0 {
396 // If there is a stack frame at all, it includes
397 // space to save the LR.
398 autosize += int32(ctxt.FixedFrameSize())
401 p.To.Offset = int64(autosize)
405 if p.From3.Offset&obj.NOSPLIT == 0 {
406 p, pPreempt = stacksplitPre(ctxt, p, autosize) // emit pre part of split check
408 wasSplit = true //need post part of split
412 q = obj.Appendp(ctxt, p)
414 q.From.Type = obj.TYPE_ADDR
415 q.From.Offset = int64(-autosize)
416 q.From.Reg = REGSP // not actually needed - REGSP is assumed if no reg is provided
417 q.To.Type = obj.TYPE_REG
420 } else if cursym.Text.Mark&LEAF == 0 {
421 // A very few functions that do not return to their caller
422 // (e.g. gogo) are not identified as leaves but still have
424 cursym.Text.Mark |= LEAF
427 if cursym.Text.Mark&LEAF != 0 {
432 q = obj.Appendp(ctxt, q)
434 q.From.Type = obj.TYPE_REG
436 q.To.Type = obj.TYPE_MEM
440 if cursym.Text.From3.Offset&obj.WRAPPER != 0 {
441 // if(g->panic != nil && g->panic->argp == FP) g->panic->argp = bottom-of-frame
443 // MOVD g_panic(g), R3
446 // MOVD panic_argp(R3), R4
447 // ADD $(autosize+8), R1, R5
451 // MOVD R6, panic_argp(R3)
455 // The NOP is needed to give the jumps somewhere to land.
456 // It is a liblink NOP, not a s390x NOP: it encodes to 0 instruction bytes.
458 q = obj.Appendp(ctxt, q)
461 q.From.Type = obj.TYPE_MEM
463 q.From.Offset = 4 * int64(ctxt.Arch.PtrSize) // G.panic
464 q.To.Type = obj.TYPE_REG
467 q = obj.Appendp(ctxt, q)
469 q.From.Type = obj.TYPE_REG
471 q.To.Type = obj.TYPE_REG
474 q = obj.Appendp(ctxt, q)
476 q.To.Type = obj.TYPE_BRANCH
479 q = obj.Appendp(ctxt, q)
481 q.From.Type = obj.TYPE_MEM
483 q.From.Offset = 0 // Panic.argp
484 q.To.Type = obj.TYPE_REG
487 q = obj.Appendp(ctxt, q)
489 q.From.Type = obj.TYPE_CONST
490 q.From.Offset = int64(autosize) + ctxt.FixedFrameSize()
492 q.To.Type = obj.TYPE_REG
495 q = obj.Appendp(ctxt, q)
497 q.From.Type = obj.TYPE_REG
499 q.To.Type = obj.TYPE_REG
502 q = obj.Appendp(ctxt, q)
504 q.To.Type = obj.TYPE_BRANCH
507 q = obj.Appendp(ctxt, q)
509 q.From.Type = obj.TYPE_CONST
510 q.From.Offset = ctxt.FixedFrameSize()
512 q.To.Type = obj.TYPE_REG
515 q = obj.Appendp(ctxt, q)
517 q.From.Type = obj.TYPE_REG
519 q.To.Type = obj.TYPE_MEM
521 q.To.Offset = 0 // Panic.argp
523 q = obj.Appendp(ctxt, q)
531 if p.From.Type == obj.TYPE_CONST {
532 ctxt.Diag("using BECOME (%v) is not supported!", p)
536 retTarget := p.To.Sym
538 if cursym.Text.Mark&LEAF != 0 {
542 if retTarget == nil {
543 p.To.Type = obj.TYPE_REG
546 p.To.Type = obj.TYPE_BRANCH
554 p.From.Type = obj.TYPE_CONST
555 p.From.Offset = int64(autosize)
556 p.To.Type = obj.TYPE_REG
560 q = obj.Appendp(ctxt, p)
563 q.To.Type = obj.TYPE_REG
571 p.From.Type = obj.TYPE_MEM
574 p.To.Type = obj.TYPE_REG
580 q = obj.Appendp(ctxt, q)
582 q.From.Type = obj.TYPE_CONST
583 q.From.Offset = int64(autosize)
584 q.To.Type = obj.TYPE_REG
589 q = obj.Appendp(ctxt, q)
592 if retTarget == nil {
593 q.To.Type = obj.TYPE_REG
596 q.To.Type = obj.TYPE_BRANCH
603 if p.To.Type == obj.TYPE_REG && p.To.Reg == REGSP && p.From.Type == obj.TYPE_CONST {
604 p.Spadj = int32(-p.From.Offset)
609 pLast = stacksplitPost(ctxt, pLast, pPre, pPreempt) // emit post part of split check
614 // instruction scheduling
620 q1 = firstp; // top of block
621 o = 0; // count of instructions
622 for(p = firstp; p != nil; p = p1) {
625 if(p->mark & NOSCHED){
629 for(; p != nil; p = p->link){
630 if(!(p->mark & NOSCHED))
639 if(p->mark & (LABEL|SYNC)) {
645 if(p->mark & (BRANCH|SYNC)) {
658 func stacksplitPre(ctxt *obj.Link, p *obj.Prog, framesize int32) (*obj.Prog, *obj.Prog) {
661 // MOVD g_stackguard(g), R3
662 p = obj.Appendp(ctxt, p)
665 p.From.Type = obj.TYPE_MEM
667 p.From.Offset = 2 * int64(ctxt.Arch.PtrSize) // G.stackguard0
668 if ctxt.Cursym.Cfunc {
669 p.From.Offset = 3 * int64(ctxt.Arch.PtrSize) // G.stackguard1
671 p.To.Type = obj.TYPE_REG
675 if framesize <= obj.StackSmall {
676 // small stack: SP < stackguard
677 // CMP stackguard, SP
679 //p.To.Type = obj.TYPE_REG
684 p = obj.Appendp(ctxt, p)
686 p.From.Type = obj.TYPE_REG
690 p.To.Type = obj.TYPE_BRANCH
691 //p = obj.Appendp(ctxt, p)
694 //p.From.Type = obj.TYPE_REG
695 //p.From.Reg = REG_R3
696 //p.To.Type = obj.TYPE_REG
699 //p = obj.Appendp(ctxt, p)
701 //p.To.Type = obj.TYPE_BRANCH
703 } else if framesize <= obj.StackBig {
704 // large stack: SP-framesize < stackguard-StackSmall
705 // ADD $-framesize, SP, R4
706 // CMP stackguard, R4
707 p = obj.Appendp(ctxt, p)
710 p.From.Type = obj.TYPE_CONST
711 p.From.Offset = int64(-framesize)
713 p.To.Type = obj.TYPE_REG
716 p = obj.Appendp(ctxt, p)
717 p.From.Type = obj.TYPE_REG
721 p.To.Type = obj.TYPE_BRANCH
724 // Such a large stack we need to protect against wraparound.
725 // If SP is close to zero:
726 // SP-stackguard+StackGuard <= framesize + (StackGuard-StackSmall)
727 // The +StackGuard on both sides is required to keep the left side positive:
728 // SP is allowed to be slightly below stackguard. See stack.h.
730 // Preemption sets stackguard to StackPreempt, a very large value.
731 // That breaks the math above, so we have to check for that explicitly.
732 // // stackguard is R3
733 // CMP R3, $StackPreempt
734 // BEQ label-of-call-to-morestack
735 // ADD $StackGuard, SP, R4
737 // MOVD $(framesize+(StackGuard-StackSmall)), TEMP
739 p = obj.Appendp(ctxt, p)
742 p.From.Type = obj.TYPE_REG
744 p.To.Type = obj.TYPE_CONST
745 p.To.Offset = obj.StackPreempt
747 p = obj.Appendp(ctxt, p)
750 p.To.Type = obj.TYPE_BRANCH
752 p = obj.Appendp(ctxt, p)
754 p.From.Type = obj.TYPE_CONST
755 p.From.Offset = obj.StackGuard
757 p.To.Type = obj.TYPE_REG
760 p = obj.Appendp(ctxt, p)
762 p.From.Type = obj.TYPE_REG
764 p.To.Type = obj.TYPE_REG
767 p = obj.Appendp(ctxt, p)
769 p.From.Type = obj.TYPE_CONST
770 p.From.Offset = int64(framesize) + obj.StackGuard - obj.StackSmall
771 p.To.Type = obj.TYPE_REG
774 p = obj.Appendp(ctxt, p)
775 p.From.Type = obj.TYPE_REG
779 p.To.Type = obj.TYPE_BRANCH
785 func stacksplitPost(ctxt *obj.Link, p *obj.Prog, pPre *obj.Prog, pPreempt *obj.Prog) *obj.Prog {
788 p = obj.Appendp(ctxt, p)
791 p.From.Type = obj.TYPE_REG
793 p.To.Type = obj.TYPE_REG
799 // BL runtime.morestack(SB)
800 p = obj.Appendp(ctxt, p)
803 p.To.Type = obj.TYPE_BRANCH
804 if ctxt.Cursym.Cfunc {
805 p.To.Sym = obj.Linklookup(ctxt, "runtime.morestackc", 0)
806 } else if ctxt.Cursym.Text.From3.Offset&obj.NEEDCTXT == 0 {
807 p.To.Sym = obj.Linklookup(ctxt, "runtime.morestack_noctxt", 0)
809 p.To.Sym = obj.Linklookup(ctxt, "runtime.morestack", 0)
813 p = obj.Appendp(ctxt, p)
816 p.To.Type = obj.TYPE_BRANCH
817 p.Pcond = ctxt.Cursym.Text.Link
823 func follow(ctxt *obj.Link, s *obj.LSym) {
827 firstp := ctxt.NewProg()
829 xfol(ctxt, s.Text, &lastp)
834 func relinv(a obj.As) obj.As {
860 func xfol(ctxt *obj.Link, p *obj.Prog, last **obj.Prog) {
869 if (p.Mark&NOSCHED != 0) || q != nil && (q.Mark&NOSCHED != 0) {
878 if p != nil && p.Mark&FOLL == 0 {
887 if p.Mark&FOLL == 0 {
893 if p.Mark&FOLL != 0 {
895 for i := 0; i < 4; i, q = i+1, q.Link {
896 if q == *last || (q.Mark&NOSCHED != 0) {
905 if a != ABR && a != obj.ARET {
906 if q.Pcond == nil || (q.Pcond.Mark&FOLL != 0) {
918 if r.Mark&FOLL == 0 {
919 fmt.Printf("can't happen 1\n")
935 if a == ABR || a == obj.ARET {
941 if r.Link.Mark&FOLL == 0 {
942 xfol(ctxt, r.Link, last)
944 if r.Pcond.Mark&FOLL == 0 {
945 fmt.Printf("can't happen 2\n")
955 q.To.Type = obj.TYPE_BRANCH
967 if a == ABR || a == obj.ARET {
968 if p.Mark&NOSCHED != 0 {
977 if a != ABL && p.Link != nil {
978 xfol(ctxt, p.Link, last)
980 if p == nil || (p.Mark&FOLL != 0) {
991 var unaryDst = map[obj.As]bool{
1001 var Links390x = obj.LinkArch{
1002 Arch: sys.ArchS390X,
1003 Preprocess: preprocess,