1 // cmd/7l/noop.c, cmd/7l/obj.c, cmd/ld/pass.c from Vita Nuova.
2 // https://code.google.com/p/ken-cc/source/browse/
4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
6 // Portions Copyright © 1997-1999 Vita Nuova Limited
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8 // Portions Copyright © 2004,2006 Bruce Ellis
9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11 // Portions Copyright © 2009 The Go Authors. All rights reserved.
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43 var complements = []obj.As{
54 // zrReplace is the set of instructions for which $0 in the From operand
55 // should be replaced with REGZERO.
56 var zrReplace = map[obj.As]bool{
74 func (c *ctxt7) stacksplit(p *obj.Prog, framesize int32) *obj.Prog {
75 if c.ctxt.Flag_maymorestack != "" {
76 p = c.cursym.Func().SpillRegisterArgs(p, c.newprog)
78 // Save LR and make room for FP, REGCTXT. Leave room
79 // for caller's saved FP.
81 p = obj.Appendp(p, c.newprog)
83 p.From.Type = obj.TYPE_REG
85 p.To.Type = obj.TYPE_MEM
87 p.To.Offset = -frameSize
92 p = obj.Appendp(p, c.newprog)
94 p.From.Type = obj.TYPE_REG
96 p.To.Type = obj.TYPE_MEM
100 p = obj.Appendp(p, c.newprog)
102 p.From.Type = obj.TYPE_CONST
105 p.To.Type = obj.TYPE_REG
108 // Save REGCTXT (for simplicity we do this whether or
110 p = obj.Appendp(p, c.newprog)
112 p.From.Type = obj.TYPE_REG
114 p.To.Type = obj.TYPE_MEM
119 p = obj.Appendp(p, c.newprog)
121 p.To.Type = obj.TYPE_BRANCH
122 // See ../x86/obj6.go
123 p.To.Sym = c.ctxt.LookupABI(c.ctxt.Flag_maymorestack, c.cursym.ABI())
126 p = obj.Appendp(p, c.newprog)
128 p.From.Type = obj.TYPE_MEM
131 p.To.Type = obj.TYPE_REG
135 p = obj.Appendp(p, c.newprog)
137 p.From.Type = obj.TYPE_MEM
140 p.To.Type = obj.TYPE_REG
143 // Restore LR and SP.
144 p = obj.Appendp(p, c.newprog)
146 p.From.Type = obj.TYPE_MEM
148 p.From.Offset = frameSize
150 p.To.Type = obj.TYPE_REG
154 p = c.cursym.Func().UnspillRegisterArgs(p, c.newprog)
157 // Jump back to here after morestack returns.
160 // MOV g_stackguard(g), RT1
161 p = obj.Appendp(p, c.newprog)
164 p.From.Type = obj.TYPE_MEM
166 p.From.Offset = 2 * int64(c.ctxt.Arch.PtrSize) // G.stackguard0
167 if c.cursym.CFunc() {
168 p.From.Offset = 3 * int64(c.ctxt.Arch.PtrSize) // G.stackguard1
170 p.To.Type = obj.TYPE_REG
173 // Mark the stack bound check and morestack call async nonpreemptible.
174 // If we get preempted here, when resumed the preemption request is
175 // cleared, but we'll still call morestack, which will double the stack
176 // unnecessarily. See issue #35470.
177 p = c.ctxt.StartUnsafePoint(p, c.newprog)
179 q := (*obj.Prog)(nil)
180 if framesize <= objabi.StackSmall {
181 // small stack: SP < stackguard
182 // CMP stackguard, SP
184 p = obj.Appendp(p, c.newprog)
186 p.From.Type = obj.TYPE_REG
189 } else if framesize <= objabi.StackBig {
190 // large stack: SP-framesize < stackguard-StackSmall
191 // SUB $(framesize-StackSmall), SP, RT2
192 // CMP stackguard, RT2
193 p = obj.Appendp(p, c.newprog)
196 p.From.Type = obj.TYPE_CONST
197 p.From.Offset = int64(framesize) - objabi.StackSmall
199 p.To.Type = obj.TYPE_REG
202 p = obj.Appendp(p, c.newprog)
204 p.From.Type = obj.TYPE_REG
208 // Such a large stack we need to protect against underflow.
209 // The runtime guarantees SP > objabi.StackBig, but
210 // framesize is large enough that SP-framesize may
211 // underflow, causing a direct comparison with the
212 // stack guard to incorrectly succeed. We explicitly
213 // guard against underflow.
215 // SUBS $(framesize-StackSmall), SP, RT2
216 // // On underflow, jump to morestack
217 // BLO label_of_call_to_morestack
218 // CMP stackguard, RT2
220 p = obj.Appendp(p, c.newprog)
222 p.From.Type = obj.TYPE_CONST
223 p.From.Offset = int64(framesize) - objabi.StackSmall
225 p.To.Type = obj.TYPE_REG
228 p = obj.Appendp(p, c.newprog)
231 p.To.Type = obj.TYPE_BRANCH
233 p = obj.Appendp(p, c.newprog)
235 p.From.Type = obj.TYPE_REG
241 bls := obj.Appendp(p, c.newprog)
243 bls.To.Type = obj.TYPE_BRANCH
245 end := c.ctxt.EndUnsafePoint(bls, c.newprog, -1)
248 for last = c.cursym.Func().Text; last.Link != nil; last = last.Link {
251 // Now we are at the end of the function, but logically
252 // we are still in function prologue. We need to fix the
253 // SP data and PCDATA.
254 spfix := obj.Appendp(last, c.newprog)
256 spfix.Spadj = -framesize
258 pcdata := c.ctxt.EmitEntryStackMap(c.cursym, spfix, c.newprog)
259 pcdata = c.ctxt.StartUnsafePoint(pcdata, c.newprog)
262 q.To.SetTarget(pcdata)
264 bls.To.SetTarget(pcdata)
266 spill := c.cursym.Func().SpillRegisterArgs(pcdata, c.newprog)
269 movlr := obj.Appendp(spill, c.newprog)
271 movlr.From.Type = obj.TYPE_REG
272 movlr.From.Reg = REGLINK
273 movlr.To.Type = obj.TYPE_REG
274 movlr.To.Reg = REG_R3
278 debug = obj.Appendp(debug, c.newprog)
280 debug.From.Type = obj.TYPE_CONST
281 debug.From.Offset = int64(framesize)
282 debug.To.Type = obj.TYPE_REG
283 debug.To.Reg = REGTMP
286 // BL runtime.morestack(SB)
287 call := obj.Appendp(debug, c.newprog)
289 call.To.Type = obj.TYPE_BRANCH
290 morestack := "runtime.morestack"
292 case c.cursym.CFunc():
293 morestack = "runtime.morestackc"
294 case !c.cursym.Func().Text.From.Sym.NeedCtxt():
295 morestack = "runtime.morestack_noctxt"
297 call.To.Sym = c.ctxt.Lookup(morestack)
299 unspill := c.cursym.Func().UnspillRegisterArgs(call, c.newprog)
300 pcdata = c.ctxt.EndUnsafePoint(unspill, c.newprog, -1)
303 jmp := obj.Appendp(pcdata, c.newprog)
305 jmp.To.Type = obj.TYPE_BRANCH
306 jmp.To.SetTarget(startPred.Link)
307 jmp.Spadj = +framesize
312 func progedit(ctxt *obj.Link, p *obj.Prog, newprog obj.ProgAlloc) {
313 c := ctxt7{ctxt: ctxt, newprog: newprog}
318 // Previously we rewrote $0 to ZR, but we have now removed this change.
319 // In order to be compatible with some previous legal instruction formats,
320 // reserve the previous conversion for some specific instructions.
321 if p.From.Type == obj.TYPE_CONST && p.From.Offset == 0 && zrReplace[p.As] {
322 p.From.Type = obj.TYPE_REG
326 // Rewrite BR/BL to symbol as TYPE_BRANCH.
334 p.To.Type = obj.TYPE_BRANCH
339 // Rewrite float constants to values stored in memory.
342 if p.From.Type == obj.TYPE_FCONST {
343 f64 := p.From.Val.(float64)
345 if c.chipfloat7(f64) > 0 {
348 if math.Float32bits(f32) == 0 {
349 p.From.Type = obj.TYPE_REG
353 p.From.Type = obj.TYPE_MEM
354 p.From.Sym = c.ctxt.Float32Sym(f32)
355 p.From.Name = obj.NAME_EXTERN
360 if p.From.Type == obj.TYPE_FCONST {
361 f64 := p.From.Val.(float64)
362 if c.chipfloat7(f64) > 0 {
365 if math.Float64bits(f64) == 0 {
366 p.From.Type = obj.TYPE_REG
370 p.From.Type = obj.TYPE_MEM
371 p.From.Sym = c.ctxt.Float64Sym(f64)
372 p.From.Name = obj.NAME_EXTERN
379 // Rewrite negative immediates as positive immediates with
380 // complementary instruction.
382 case AADD, ASUB, ACMP, ACMN:
383 if p.From.Type == obj.TYPE_CONST && p.From.Offset < 0 && p.From.Offset != -1<<63 {
384 p.From.Offset = -p.From.Offset
385 p.As = complements[p.As]
387 case AADDW, ASUBW, ACMPW, ACMNW:
388 if p.From.Type == obj.TYPE_CONST && p.From.Offset < 0 && int32(p.From.Offset) != -1<<31 {
389 p.From.Offset = -p.From.Offset
390 p.As = complements[p.As]
394 if c.ctxt.Flag_dynlink {
399 // Rewrite p, if necessary, to access global data via the global offset table.
400 func (c *ctxt7) rewriteToUseGot(p *obj.Prog) {
401 if p.As == obj.ADUFFCOPY || p.As == obj.ADUFFZERO {
404 // MOVD runtime.duffxxx@GOT, REGTMP
405 // ADD $offset, REGTMP
408 if p.As == obj.ADUFFZERO {
409 sym = c.ctxt.LookupABI("runtime.duffzero", obj.ABIInternal)
411 sym = c.ctxt.LookupABI("runtime.duffcopy", obj.ABIInternal)
413 offset := p.To.Offset
415 p.From.Type = obj.TYPE_MEM
416 p.From.Name = obj.NAME_GOTREF
418 p.To.Type = obj.TYPE_REG
420 p.To.Name = obj.NAME_NONE
423 p1 := obj.Appendp(p, c.newprog)
425 p1.From.Type = obj.TYPE_CONST
426 p1.From.Offset = offset
427 p1.To.Type = obj.TYPE_REG
429 p2 := obj.Appendp(p1, c.newprog)
431 p2.To.Type = obj.TYPE_REG
435 // We only care about global data: NAME_EXTERN means a global
436 // symbol in the Go sense, and p.Sym.Local is true for a few
437 // internally defined symbols.
438 if p.From.Type == obj.TYPE_ADDR && p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local() {
439 // MOVD $sym, Rx becomes MOVD sym@GOT, Rx
440 // MOVD $sym+<off>, Rx becomes MOVD sym@GOT, Rx; ADD <off>, Rx
442 c.ctxt.Diag("do not know how to handle TYPE_ADDR in %v with -dynlink", p)
444 if p.To.Type != obj.TYPE_REG {
445 c.ctxt.Diag("do not know how to handle LEAQ-type insn to non-register in %v with -dynlink", p)
447 p.From.Type = obj.TYPE_MEM
448 p.From.Name = obj.NAME_GOTREF
449 if p.From.Offset != 0 {
450 q := obj.Appendp(p, c.newprog)
452 q.From.Type = obj.TYPE_CONST
453 q.From.Offset = p.From.Offset
458 if p.GetFrom3() != nil && p.GetFrom3().Name == obj.NAME_EXTERN {
459 c.ctxt.Diag("don't know how to handle %v with -dynlink", p)
462 // MOVx sym, Ry becomes MOVD sym@GOT, REGTMP; MOVx (REGTMP), Ry
463 // MOVx Ry, sym becomes MOVD sym@GOT, REGTMP; MOVD Ry, (REGTMP)
464 // An addition may be inserted between the two MOVs if there is an offset.
465 if p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local() {
466 if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local() {
467 c.ctxt.Diag("cannot handle NAME_EXTERN on both sides in %v with -dynlink", p)
470 } else if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local() {
475 if p.As == obj.ATEXT || p.As == obj.AFUNCDATA || p.As == obj.ACALL || p.As == obj.ARET || p.As == obj.AJMP {
478 if source.Sym.Type == objabi.STLSBSS {
481 if source.Type != obj.TYPE_MEM {
482 c.ctxt.Diag("don't know how to handle %v with -dynlink", p)
484 p1 := obj.Appendp(p, c.newprog)
485 p2 := obj.Appendp(p1, c.newprog)
487 p1.From.Type = obj.TYPE_MEM
488 p1.From.Sym = source.Sym
489 p1.From.Name = obj.NAME_GOTREF
490 p1.To.Type = obj.TYPE_REG
496 if p.From.Name == obj.NAME_EXTERN {
498 p2.From.Name = obj.NAME_NONE
500 } else if p.To.Name == obj.NAME_EXTERN {
502 p2.To.Name = obj.NAME_NONE
510 func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
511 if cursym.Func().Text == nil || cursym.Func().Text.Link == nil {
515 c := ctxt7{ctxt: ctxt, newprog: newprog, cursym: cursym}
517 p := c.cursym.Func().Text
518 textstksiz := p.To.Offset
519 if textstksiz == -8 {
520 // Historical way to mark NOFRAME.
521 p.From.Sym.Set(obj.AttrNoFrame, true)
525 c.ctxt.Diag("negative frame size %d - did you mean NOFRAME?", textstksiz)
527 if p.From.Sym.NoFrame() {
529 c.ctxt.Diag("NOFRAME functions must have a frame size of 0, not %d", textstksiz)
533 c.cursym.Func().Args = p.To.Val.(int32)
534 c.cursym.Func().Locals = int32(textstksiz)
537 * find leaf subroutines
539 for p := c.cursym.Func().Text; p != nil; p = p.Link {
547 c.cursym.Func().Text.Mark &^= LEAF
554 for p := c.cursym.Func().Text; p != nil; p = p.Link {
558 c.cursym.Func().Text = p
559 c.autosize = int32(textstksiz)
561 if p.Mark&LEAF != 0 && c.autosize == 0 {
562 // A leaf function with no locals has no frame.
563 p.From.Sym.Set(obj.AttrNoFrame, true)
566 if !p.From.Sym.NoFrame() {
567 // If there is a stack frame at all, it includes
568 // space to save the LR.
573 extrasize := int32(0)
574 if c.autosize%16 == 8 {
575 // Allocate extra 8 bytes on the frame top to save FP
577 } else if c.autosize&(16-1) == 0 {
578 // Allocate extra 16 bytes to save FP for the old frame whose size is 8 mod 16
581 c.ctxt.Diag("%v: unaligned frame size %d - must be 16 aligned", p, c.autosize-8)
583 c.autosize += extrasize
584 c.cursym.Func().Locals += extrasize
586 // low 32 bits for autosize
587 // high 32 bits for extrasize
588 p.To.Offset = int64(c.autosize) | int64(extrasize)<<32
594 if c.autosize == 0 && c.cursym.Func().Text.Mark&LEAF == 0 {
595 if c.ctxt.Debugvlog {
596 c.ctxt.Logf("save suppressed in: %s\n", c.cursym.Func().Text.From.Sym.Name)
598 c.cursym.Func().Text.Mark |= LEAF
601 if cursym.Func().Text.Mark&LEAF != 0 {
602 cursym.Set(obj.AttrLeaf, true)
603 if p.From.Sym.NoFrame() {
608 if p.Mark&LEAF != 0 && c.autosize < objabi.StackSmall {
609 // A leaf function with a small stack can be marked
610 // NOSPLIT, avoiding a stack check.
611 p.From.Sym.Set(obj.AttrNoSplit, true)
614 if !p.From.Sym.NoSplit() {
615 p = c.stacksplit(p, c.autosize) // emit split check
618 var prologueEnd *obj.Prog
620 aoffset := c.autosize
622 // MOVD.W offset variant range is -0x100 to 0xf8, SP should be 16-byte aligned.
623 // so the maximum aoffset value is 0xf0.
627 // Frame is non-empty. Make sure to save link register, even if
628 // it is a leaf function, so that traceback works.
630 if c.autosize > aoffset {
631 // Frame size is too large for a MOVD.W instruction. Store the frame pointer
632 // register and link register before decrementing SP, so if a signal comes
633 // during the execution of the function prologue, the traceback code will
634 // not see a half-updated stack frame.
636 // SUB $autosize, RSP, R20
637 q1 = obj.Appendp(q, c.newprog)
640 q1.From.Type = obj.TYPE_CONST
641 q1.From.Offset = int64(c.autosize)
643 q1.To.Type = obj.TYPE_REG
648 // STP (R29, R30), -8(R20)
649 q1 = obj.Appendp(q1, c.newprog)
652 q1.From.Type = obj.TYPE_REGREG
654 q1.From.Offset = REGLINK
655 q1.To.Type = obj.TYPE_MEM
659 // This is not async preemptible, as if we open a frame
660 // at the current SP, it will clobber the saved LR.
661 q1 = c.ctxt.StartUnsafePoint(q1, c.newprog)
664 q1 = obj.Appendp(q1, c.newprog)
667 q1.From.Type = obj.TYPE_REG
668 q1.From.Reg = REG_R20
669 q1.To.Type = obj.TYPE_REG
671 q1.Spadj = c.autosize
673 q1 = c.ctxt.EndUnsafePoint(q1, c.newprog, -1)
675 if buildcfg.GOOS == "ios" {
676 // iOS does not support SA_ONSTACK. We will run the signal handler
677 // on the G stack. If we write below SP, it may be clobbered by
678 // the signal handler. So we save FP and LR after decrementing SP.
679 // STP (R29, R30), -8(RSP)
680 q1 = obj.Appendp(q1, c.newprog)
683 q1.From.Type = obj.TYPE_REGREG
685 q1.From.Offset = REGLINK
686 q1.To.Type = obj.TYPE_MEM
691 // small frame, update SP and save LR in a single MOVD.W instruction.
692 // So if a signal comes during the execution of the function prologue,
693 // the traceback code will not see a half-updated stack frame.
694 // Also, on Linux, in a cgo binary we may get a SIGSETXID signal
695 // early on before the signal stack is set, as glibc doesn't allow
696 // us to block SIGSETXID. So it is important that we don't write below
697 // the SP until the signal stack is set.
698 // Luckily, all the functions from thread entry to setting the signal
699 // stack have small frames.
700 q1 = obj.Appendp(q, c.newprog)
703 q1.From.Type = obj.TYPE_REG
704 q1.From.Reg = REGLINK
705 q1.To.Type = obj.TYPE_MEM
707 q1.To.Offset = int64(-aoffset)
714 q1 = obj.Appendp(q1, c.newprog)
717 q1.From.Type = obj.TYPE_REG
719 q1.To.Type = obj.TYPE_MEM
724 prologueEnd.Pos = prologueEnd.Pos.WithXlogue(src.PosPrologueEnd)
726 q1 = obj.Appendp(q1, c.newprog)
729 q1.From.Type = obj.TYPE_CONST
732 q1.To.Type = obj.TYPE_REG
735 if c.cursym.Func().Text.From.Sym.Wrapper() {
736 // if(g->panic != nil && g->panic->argp == FP) g->panic->argp = bottom-of-frame
738 // MOV g_panic(g), RT1
742 // ... function body ...
744 // MOV panic_argp(RT1), RT2
745 // ADD $(autosize+8), RSP, R20
749 // MOVD R20, panic_argp(RT1)
752 // The NOP is needed to give the jumps somewhere to land.
753 // It is a liblink NOP, not an ARM64 NOP: it encodes to 0 instruction bytes.
756 // MOV g_panic(g), RT1
757 q = obj.Appendp(q, c.newprog)
759 q.From.Type = obj.TYPE_MEM
761 q.From.Offset = 4 * int64(c.ctxt.Arch.PtrSize) // G.panic
762 q.To.Type = obj.TYPE_REG
765 // CBNZ RT1, checkargp
766 cbnz := obj.Appendp(q, c.newprog)
768 cbnz.From.Type = obj.TYPE_REG
769 cbnz.From.Reg = REGRT1
770 cbnz.To.Type = obj.TYPE_BRANCH
772 // Empty branch target at the top of the function body
773 end := obj.Appendp(cbnz, c.newprog)
776 // find the end of the function
778 for last = end; last.Link != nil; last = last.Link {
781 // MOV panic_argp(RT1), RT2
782 mov := obj.Appendp(last, c.newprog)
784 mov.From.Type = obj.TYPE_MEM
785 mov.From.Reg = REGRT1
786 mov.From.Offset = 0 // Panic.argp
787 mov.To.Type = obj.TYPE_REG
790 // CBNZ branches to the MOV above
791 cbnz.To.SetTarget(mov)
793 // ADD $(autosize+8), SP, R20
794 q = obj.Appendp(mov, c.newprog)
796 q.From.Type = obj.TYPE_CONST
797 q.From.Offset = int64(c.autosize) + 8
799 q.To.Type = obj.TYPE_REG
803 q = obj.Appendp(q, c.newprog)
805 q.From.Type = obj.TYPE_REG
810 q = obj.Appendp(q, c.newprog)
812 q.To.Type = obj.TYPE_BRANCH
816 q = obj.Appendp(q, c.newprog)
818 q.From.Type = obj.TYPE_CONST
821 q.To.Type = obj.TYPE_REG
824 // MOV R20, panic_argp(RT1)
825 q = obj.Appendp(q, c.newprog)
827 q.From.Type = obj.TYPE_REG
829 q.To.Type = obj.TYPE_MEM
831 q.To.Offset = 0 // Panic.argp
834 q = obj.Appendp(q, c.newprog)
836 q.To.Type = obj.TYPE_BRANCH
842 if p.From.Type == obj.TYPE_CONST {
843 c.ctxt.Diag("using BECOME (%v) is not supported!", p)
849 if c.cursym.Func().Text.Mark&LEAF != 0 {
852 p.From.Type = obj.TYPE_CONST
853 p.From.Offset = int64(c.autosize)
854 p.To.Type = obj.TYPE_REG
856 p.Spadj = -c.autosize
859 p = obj.Appendp(p, c.newprog)
861 p.From.Type = obj.TYPE_CONST
864 p.To.Type = obj.TYPE_REG
868 aoffset := c.autosize
869 // LDP -8(RSP), (R29, R30)
871 p.From.Type = obj.TYPE_MEM
874 p.To.Type = obj.TYPE_REGREG
876 p.To.Offset = REGLINK
878 // ADD $aoffset, RSP, RSP
881 q.From.Type = obj.TYPE_CONST
882 q.From.Offset = int64(aoffset)
883 q.To.Type = obj.TYPE_REG
892 // If enabled, this code emits 'MOV PC, R27' before every 'MOV LR, PC',
893 // so that if you are debugging a low-level crash where PC and LR are zero,
894 // you can look at R27 to see what jumped to the zero.
895 // This is useful when bringing up Go on a new system.
896 // (There is similar code in ../ppc64/obj9.go:/if.false.)
897 const debugRETZERO = false
899 if p.As != obj.ARET {
907 p.From.Type = obj.TYPE_BRANCH
909 p.To.Type = obj.TYPE_REG
914 if p.As != obj.ARET {
922 if retjmp != nil { // retjmp
924 p.To.Type = obj.TYPE_BRANCH
926 p.Spadj = +c.autosize
931 p.To.Type = obj.TYPE_MEM
934 p.Spadj = +c.autosize
937 if p.To.Type == obj.TYPE_REG && p.To.Reg == REGSP && p.From.Type == obj.TYPE_CONST {
939 p.Spadj = int32(-p.From.Offset)
941 p.Spadj = int32(+p.From.Offset)
945 case obj.AGETCALLERPC:
949 p.From.Type = obj.TYPE_REG
954 p.From.Type = obj.TYPE_MEM
960 // STP (FP, R27), -24(SP)
967 // copy DUFFCOPY from q1 to q4
968 q4 := obj.Appendp(p, c.newprog)
970 q4.As = obj.ADUFFCOPY
974 q1.From.Type = obj.TYPE_BRANCH
975 q1.To.Type = obj.TYPE_REG
978 q2 := obj.Appendp(q1, c.newprog)
981 q2.From.Type = obj.TYPE_REGREG
983 q2.From.Offset = int64(REG_R27)
984 q2.To.Type = obj.TYPE_MEM
988 // maintain FP for DUFFCOPY
989 q3 := obj.Appendp(q2, c.newprog)
992 q3.From.Type = obj.TYPE_CONST
995 q3.To.Type = obj.TYPE_REG
998 q5 := obj.Appendp(q4, c.newprog)
1001 q5.From.Type = obj.TYPE_CONST
1004 q5.To.Type = obj.TYPE_REG
1006 q1.From.SetTarget(q5)
1010 // ADR ret_addr, R27
1011 // STP (FP, R27), -24(SP)
1018 // copy DUFFZERO from q1 to q4
1019 q4 := obj.Appendp(p, c.newprog)
1021 q4.As = obj.ADUFFZERO
1025 q1.From.Type = obj.TYPE_BRANCH
1026 q1.To.Type = obj.TYPE_REG
1029 q2 := obj.Appendp(q1, c.newprog)
1032 q2.From.Type = obj.TYPE_REGREG
1034 q2.From.Offset = int64(REG_R27)
1035 q2.To.Type = obj.TYPE_MEM
1039 // maintain FP for DUFFZERO
1040 q3 := obj.Appendp(q2, c.newprog)
1043 q3.From.Type = obj.TYPE_CONST
1046 q3.To.Type = obj.TYPE_REG
1049 q5 := obj.Appendp(q4, c.newprog)
1052 q5.From.Type = obj.TYPE_CONST
1055 q5.To.Type = obj.TYPE_REG
1057 q1.From.SetTarget(q5)
1061 if p.To.Type == obj.TYPE_REG && p.To.Reg == REGSP && p.Spadj == 0 {
1062 f := c.cursym.Func()
1063 if f.FuncFlag&objabi.FuncFlag_SPWRITE == 0 {
1064 c.cursym.Func().FuncFlag |= objabi.FuncFlag_SPWRITE
1065 if ctxt.Debugvlog || !ctxt.IsAsm {
1066 ctxt.Logf("auto-SPWRITE: %s %v\n", c.cursym.Name, p)
1068 ctxt.Diag("invalid auto-SPWRITE in non-assembly")
1070 log.Fatalf("bad SPWRITE")
1075 if p.From.Type == obj.TYPE_SHIFT && (p.To.Reg == REG_RSP || p.Reg == REG_RSP) {
1076 offset := p.From.Offset
1077 op := offset & (3 << 22)
1079 ctxt.Diag("illegal combination: %v", p)
1081 r := (offset >> 16) & 31
1082 shift := (offset >> 10) & 63
1084 // the shift amount is out of range, in order to avoid repeated error
1085 // reportings, don't call ctxt.Diag, because asmout case 27 has the
1089 p.From.Type = obj.TYPE_REG
1090 p.From.Reg = int16(REG_LSL + r + (shift&7)<<5)
1096 func nocache(p *obj.Prog) {
1102 var unaryDst = map[obj.As]bool{
1110 var Linkarm64 = obj.LinkArch{
1111 Arch: sys.ArchARM64,
1113 Preprocess: preprocess,
1117 DWARFRegisters: ARM64DWARFRegisters,