Source file src/cmd/internal/obj/ppc64/obj9.go

     1  // cmd/9l/noop.c, cmd/9l/pass.c, cmd/9l/span.c from Vita Nuova.
     2  //
     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.
    11  //
    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:
    18  //
    19  // The above copyright notice and this permission notice shall be included in
    20  // all copies or substantial portions of the Software.
    21  //
    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
    28  // THE SOFTWARE.
    29  
    30  package ppc64
    31  
    32  import (
    33  	"cmd/internal/obj"
    34  	"cmd/internal/objabi"
    35  	"cmd/internal/src"
    36  	"cmd/internal/sys"
    37  	"internal/abi"
    38  	"internal/buildcfg"
    39  	"log"
    40  	"math"
    41  	"math/bits"
    42  	"strings"
    43  )
    44  
    45  // Test if this value can encoded as a mask for
    46  // li -1, rx; rlic rx,rx,sh,mb.
    47  // Masks can also extend from the msb and wrap to
    48  // the lsb too. That is, the valid masks are 32 bit strings
    49  // of the form: 0..01..10..0 or 1..10..01..1 or 1...1
    50  func isPPC64DoublewordRotateMask(v64 int64) bool {
    51  	// Isolate rightmost 1 (if none 0) and add.
    52  	v := uint64(v64)
    53  	vp := (v & -v) + v
    54  	// Likewise, for the wrapping case.
    55  	vn := ^v
    56  	vpn := (vn & -vn) + vn
    57  	return (v&vp == 0 || vn&vpn == 0) && v != 0
    58  }
    59  
    60  // Encode a doubleword rotate mask into mb (mask begin) and
    61  // me (mask end, inclusive). Note, POWER ISA labels bits in
    62  // big endian order.
    63  func encodePPC64RLDCMask(mask int64) (mb, me int) {
    64  	// Determine boundaries and then decode them
    65  	mb = bits.LeadingZeros64(uint64(mask))
    66  	me = 64 - bits.TrailingZeros64(uint64(mask))
    67  	mbn := bits.LeadingZeros64(^uint64(mask))
    68  	men := 64 - bits.TrailingZeros64(^uint64(mask))
    69  	// Check for a wrapping mask (e.g bits at 0 and 63)
    70  	if mb == 0 && me == 64 {
    71  		// swap the inverted values
    72  		mb, me = men, mbn
    73  	}
    74  	// Note, me is inclusive.
    75  	return mb, me - 1
    76  }
    77  
    78  // Is this a symbol which should never have a TOC prologue generated?
    79  // These are special functions which should not have a TOC regeneration
    80  // prologue.
    81  func isNOTOCfunc(name string) bool {
    82  	switch {
    83  	case name == "runtime.duffzero":
    84  		return true
    85  	case name == "runtime.duffcopy":
    86  		return true
    87  	case strings.HasPrefix(name, "runtime.elf_"):
    88  		return true
    89  	default:
    90  		return false
    91  	}
    92  }
    93  
    94  // Try converting FMOVD/FMOVS to XXSPLTIDP. If it is converted,
    95  // return true.
    96  func convertFMOVtoXXSPLTIDP(p *obj.Prog) bool {
    97  	if p.From.Type != obj.TYPE_FCONST || buildcfg.GOPPC64 < 10 {
    98  		return false
    99  	}
   100  	v := p.From.Val.(float64)
   101  	if float64(float32(v)) != v {
   102  		return false
   103  	}
   104  	// Secondly, is this value a normal value?
   105  	ival := int64(math.Float32bits(float32(v)))
   106  	isDenorm := ival&0x7F800000 == 0 && ival&0x007FFFFF != 0
   107  	if !isDenorm {
   108  		p.As = AXXSPLTIDP
   109  		p.From.Type = obj.TYPE_CONST
   110  		p.From.Offset = ival
   111  		// Convert REG_Fx into equivalent REG_VSx
   112  		p.To.Reg = REG_VS0 + (p.To.Reg & 31)
   113  	}
   114  	return !isDenorm
   115  }
   116  
   117  func progedit(ctxt *obj.Link, p *obj.Prog, newprog obj.ProgAlloc) {
   118  	p.From.Class = 0
   119  	p.To.Class = 0
   120  
   121  	c := ctxt9{ctxt: ctxt, newprog: newprog}
   122  
   123  	// Rewrite BR/BL to symbol as TYPE_BRANCH.
   124  	switch p.As {
   125  	case ABR,
   126  		ABL,
   127  		obj.ARET,
   128  		obj.ADUFFZERO,
   129  		obj.ADUFFCOPY:
   130  		if p.To.Sym != nil {
   131  			p.To.Type = obj.TYPE_BRANCH
   132  		}
   133  	}
   134  
   135  	// Rewrite float constants to values stored in memory.
   136  	switch p.As {
   137  	case AFMOVS:
   138  		if p.From.Type == obj.TYPE_FCONST && !convertFMOVtoXXSPLTIDP(p) {
   139  			f32 := float32(p.From.Val.(float64))
   140  			p.From.Type = obj.TYPE_MEM
   141  			p.From.Sym = ctxt.Float32Sym(f32)
   142  			p.From.Name = obj.NAME_EXTERN
   143  			p.From.Offset = 0
   144  		}
   145  
   146  	case AFMOVD:
   147  		if p.From.Type == obj.TYPE_FCONST {
   148  			f64 := p.From.Val.(float64)
   149  			// Constant not needed in memory for float +/- 0
   150  			if f64 != 0 && !convertFMOVtoXXSPLTIDP(p) {
   151  				p.From.Type = obj.TYPE_MEM
   152  				p.From.Sym = ctxt.Float64Sym(f64)
   153  				p.From.Name = obj.NAME_EXTERN
   154  				p.From.Offset = 0
   155  			}
   156  		}
   157  
   158  	case AMOVW, AMOVWZ:
   159  		// Note, for backwards compatibility, MOVW $const, Rx and MOVWZ $const, Rx are identical.
   160  		if p.From.Type == obj.TYPE_CONST && p.From.Offset != 0 && p.From.Offset&0xFFFF == 0 {
   161  			// This is a constant shifted 16 bits to the left, convert it to ADDIS/ORIS $const,...
   162  			p.As = AADDIS
   163  			// Use ORIS for large constants which should not be sign extended.
   164  			if p.From.Offset >= 0x80000000 {
   165  				p.As = AORIS
   166  			}
   167  			p.Reg = REG_R0
   168  			p.From.Offset >>= 16
   169  		}
   170  
   171  	case AMOVD:
   172  		// Skip this opcode if it is not a constant load.
   173  		if p.From.Type != obj.TYPE_CONST || p.From.Name != obj.NAME_NONE || p.From.Reg != 0 {
   174  			break
   175  		}
   176  
   177  		// 32b constants (signed and unsigned) can be generated via 1 or 2 instructions. They can be assembled directly.
   178  		isS32 := int64(int32(p.From.Offset)) == p.From.Offset
   179  		isU32 := uint64(uint32(p.From.Offset)) == uint64(p.From.Offset)
   180  		// If prefixed instructions are supported, a 34b signed constant can be generated by one pli instruction.
   181  		isS34 := pfxEnabled && (p.From.Offset<<30)>>30 == p.From.Offset
   182  
   183  		// Try converting MOVD $const,Rx into ADDIS/ORIS $s32>>16,R0,Rx
   184  		switch {
   185  		case isS32 && p.From.Offset&0xFFFF == 0 && p.From.Offset != 0:
   186  			p.As = AADDIS
   187  			p.From.Offset >>= 16
   188  			p.Reg = REG_R0
   189  
   190  		case isU32 && p.From.Offset&0xFFFF == 0 && p.From.Offset != 0:
   191  			p.As = AORIS
   192  			p.From.Offset >>= 16
   193  			p.Reg = REG_R0
   194  
   195  		case isS32 || isU32 || isS34:
   196  			// The assembler can generate this opcode in 1 (on Power10) or 2 opcodes.
   197  
   198  		// Otherwise, see if the large constant can be generated with 2 instructions. If not, load it from memory.
   199  		default:
   200  			// Is this a shifted 16b constant? If so, rewrite it to avoid a creating and loading a constant.
   201  			val := p.From.Offset
   202  			shift := bits.TrailingZeros64(uint64(val))
   203  			mask := int64(0xFFFF) << shift
   204  			if val&mask == val || (val>>(shift+16) == -1 && (val>>shift)<<shift == val) {
   205  				// Rewrite this value into MOVD $const>>shift, Rto; SLD $shift, Rto
   206  				q := obj.Appendp(p, c.newprog)
   207  				q.As = ASLD
   208  				q.From.SetConst(int64(shift))
   209  				q.To = p.To
   210  				p.From.Offset >>= shift
   211  				p = q
   212  			} else if isPPC64DoublewordRotateMask(val) {
   213  				// This constant is a mask value, generate MOVD $-1, Rto; RLDIC Rto, ^me, mb, Rto
   214  				mb, me := encodePPC64RLDCMask(val)
   215  				q := obj.Appendp(p, c.newprog)
   216  				q.As = ARLDC
   217  				q.AddRestSourceConst((^int64(me)) & 0x3F)
   218  				q.AddRestSourceConst(int64(mb))
   219  				q.From = p.To
   220  				q.To = p.To
   221  				p.From.Offset = -1
   222  				p = q
   223  			} else {
   224  				// Load the constant from memory.
   225  				p.From.Type = obj.TYPE_MEM
   226  				p.From.Sym = ctxt.Int64Sym(p.From.Offset)
   227  				p.From.Name = obj.NAME_EXTERN
   228  				p.From.Offset = 0
   229  			}
   230  		}
   231  	}
   232  
   233  	switch p.As {
   234  	// Rewrite SUB constants into ADD.
   235  	case ASUBC:
   236  		if p.From.Type == obj.TYPE_CONST {
   237  			p.From.Offset = -p.From.Offset
   238  			p.As = AADDC
   239  		}
   240  
   241  	case ASUBCCC:
   242  		if p.From.Type == obj.TYPE_CONST {
   243  			p.From.Offset = -p.From.Offset
   244  			p.As = AADDCCC
   245  		}
   246  
   247  	case ASUB:
   248  		if p.From.Type != obj.TYPE_CONST {
   249  			break
   250  		}
   251  		// Rewrite SUB $const,... into ADD $-const,...
   252  		p.From.Offset = -p.From.Offset
   253  		p.As = AADD
   254  		// This is now an ADD opcode, try simplifying it below.
   255  		fallthrough
   256  
   257  	// Rewrite ADD/OR/XOR/ANDCC $const,... forms into ADDIS/ORIS/XORIS/ANDISCC
   258  	case AADD:
   259  		// Don't rewrite if this is not adding a constant value, or is not an int32
   260  		if p.From.Type != obj.TYPE_CONST || p.From.Offset == 0 || int64(int32(p.From.Offset)) != p.From.Offset {
   261  			break
   262  		}
   263  		if p.From.Offset&0xFFFF == 0 {
   264  			// The constant can be added using ADDIS
   265  			p.As = AADDIS
   266  			p.From.Offset >>= 16
   267  		} else if buildcfg.GOPPC64 >= 10 {
   268  			// Let the assembler generate paddi for large constants.
   269  			break
   270  		} else if (p.From.Offset < -0x8000 && int64(int32(p.From.Offset)) == p.From.Offset) || (p.From.Offset > 0xFFFF && p.From.Offset < 0x7FFF8000) {
   271  			// For a constant x, 0xFFFF (UINT16_MAX) < x < 0x7FFF8000 or -0x80000000 (INT32_MIN) <= x < -0x8000 (INT16_MIN)
   272  			// This is not done for 0x7FFF < x < 0x10000; the assembler will generate a slightly faster instruction sequence.
   273  			//
   274  			// The constant x can be rewritten as ADDIS + ADD as follows:
   275  			//     ADDIS $x>>16 + (x>>15)&1, rX, rY
   276  			//     ADD   $int64(int16(x)), rY, rY
   277  			// The range is slightly asymmetric as 0x7FFF8000 and above overflow the sign bit, whereas for
   278  			// negative values, this would happen with constant values between -1 and -32768 which can
   279  			// assemble into a single addi.
   280  			is := p.From.Offset>>16 + (p.From.Offset>>15)&1
   281  			i := int64(int16(p.From.Offset))
   282  			p.As = AADDIS
   283  			p.From.Offset = is
   284  			q := obj.Appendp(p, c.newprog)
   285  			q.As = AADD
   286  			q.From.SetConst(i)
   287  			q.Reg = p.To.Reg
   288  			q.To = p.To
   289  			p = q
   290  		}
   291  	case AOR:
   292  		if p.From.Type == obj.TYPE_CONST && uint64(p.From.Offset)&0xFFFFFFFF0000FFFF == 0 && p.From.Offset != 0 {
   293  			p.As = AORIS
   294  			p.From.Offset >>= 16
   295  		}
   296  	case AXOR:
   297  		if p.From.Type == obj.TYPE_CONST && uint64(p.From.Offset)&0xFFFFFFFF0000FFFF == 0 && p.From.Offset != 0 {
   298  			p.As = AXORIS
   299  			p.From.Offset >>= 16
   300  		}
   301  	case AANDCC:
   302  		if p.From.Type == obj.TYPE_CONST && uint64(p.From.Offset)&0xFFFFFFFF0000FFFF == 0 && p.From.Offset != 0 {
   303  			p.As = AANDISCC
   304  			p.From.Offset >>= 16
   305  		}
   306  
   307  	// To maintain backwards compatibility, we accept some 4 argument usage of
   308  	// several opcodes which was likely not intended, but did work. These are not
   309  	// added to optab to avoid the chance this behavior might be used with newer
   310  	// instructions.
   311  	//
   312  	// Rewrite argument ordering like "ADDEX R3, $3, R4, R5" into
   313  	//                                "ADDEX R3, R4, $3, R5"
   314  	case AVSHASIGMAW, AVSHASIGMAD, AADDEX, AXXSLDWI, AXXPERMDI:
   315  		if len(p.RestArgs) == 2 && p.Reg == 0 && p.RestArgs[0].Addr.Type == obj.TYPE_CONST && p.RestArgs[1].Addr.Type == obj.TYPE_REG {
   316  			p.Reg = p.RestArgs[1].Addr.Reg
   317  			p.RestArgs = p.RestArgs[:1]
   318  		}
   319  	}
   320  
   321  	if c.ctxt.Headtype == objabi.Haix {
   322  		c.rewriteToUseTOC(p)
   323  	} else if c.ctxt.Flag_dynlink {
   324  		c.rewriteToUseGot(p)
   325  	}
   326  }
   327  
   328  // Rewrite p, if necessary, to access a symbol using its TOC anchor.
   329  // This code is for AIX only.
   330  func (c *ctxt9) rewriteToUseTOC(p *obj.Prog) {
   331  	if p.As == obj.ATEXT || p.As == obj.AFUNCDATA || p.As == obj.ACALL || p.As == obj.ARET || p.As == obj.AJMP {
   332  		return
   333  	}
   334  
   335  	if p.As == obj.ADUFFCOPY || p.As == obj.ADUFFZERO {
   336  		// ADUFFZERO/ADUFFCOPY is considered as an ABL except in dynamic
   337  		// link where it should be an indirect call.
   338  		if !c.ctxt.Flag_dynlink {
   339  			return
   340  		}
   341  		//     ADUFFxxx $offset
   342  		// becomes
   343  		//     MOVD runtime.duffxxx@TOC, R12
   344  		//     ADD $offset, R12
   345  		//     MOVD R12, LR
   346  		//     BL (LR)
   347  		var sym *obj.LSym
   348  		if p.As == obj.ADUFFZERO {
   349  			sym = c.ctxt.Lookup("runtime.duffzero")
   350  		} else {
   351  			sym = c.ctxt.Lookup("runtime.duffcopy")
   352  		}
   353  		// Retrieve or create the TOC anchor.
   354  		symtoc := c.ctxt.LookupInit("TOC."+sym.Name, func(s *obj.LSym) {
   355  			s.Type = objabi.SDATA
   356  			s.Set(obj.AttrDuplicateOK, true)
   357  			s.Set(obj.AttrStatic, true)
   358  			c.ctxt.Data = append(c.ctxt.Data, s)
   359  			s.WriteAddr(c.ctxt, 0, 8, sym, 0)
   360  		})
   361  
   362  		offset := p.To.Offset
   363  		p.As = AMOVD
   364  		p.From.Type = obj.TYPE_MEM
   365  		p.From.Name = obj.NAME_TOCREF
   366  		p.From.Sym = symtoc
   367  		p.To.Type = obj.TYPE_REG
   368  		p.To.Reg = REG_R12
   369  		p.To.Name = obj.NAME_NONE
   370  		p.To.Offset = 0
   371  		p.To.Sym = nil
   372  		p1 := obj.Appendp(p, c.newprog)
   373  		p1.As = AADD
   374  		p1.From.Type = obj.TYPE_CONST
   375  		p1.From.Offset = offset
   376  		p1.To.Type = obj.TYPE_REG
   377  		p1.To.Reg = REG_R12
   378  		p2 := obj.Appendp(p1, c.newprog)
   379  		p2.As = AMOVD
   380  		p2.From.Type = obj.TYPE_REG
   381  		p2.From.Reg = REG_R12
   382  		p2.To.Type = obj.TYPE_REG
   383  		p2.To.Reg = REG_LR
   384  		p3 := obj.Appendp(p2, c.newprog)
   385  		p3.As = obj.ACALL
   386  		p3.To.Type = obj.TYPE_REG
   387  		p3.To.Reg = REG_LR
   388  	}
   389  
   390  	var source *obj.Addr
   391  	if p.From.Name == obj.NAME_EXTERN || p.From.Name == obj.NAME_STATIC {
   392  		if p.From.Type == obj.TYPE_ADDR {
   393  			if p.As == ADWORD {
   394  				// ADWORD $sym doesn't need TOC anchor
   395  				return
   396  			}
   397  			if p.As != AMOVD {
   398  				c.ctxt.Diag("do not know how to handle TYPE_ADDR in %v", p)
   399  				return
   400  			}
   401  			if p.To.Type != obj.TYPE_REG {
   402  				c.ctxt.Diag("do not know how to handle LEAQ-type insn to non-register in %v", p)
   403  				return
   404  			}
   405  		} else if p.From.Type != obj.TYPE_MEM {
   406  			c.ctxt.Diag("do not know how to handle %v without TYPE_MEM", p)
   407  			return
   408  		}
   409  		source = &p.From
   410  
   411  	} else if p.To.Name == obj.NAME_EXTERN || p.To.Name == obj.NAME_STATIC {
   412  		if p.To.Type != obj.TYPE_MEM {
   413  			c.ctxt.Diag("do not know how to handle %v without TYPE_MEM", p)
   414  			return
   415  		}
   416  		if source != nil {
   417  			c.ctxt.Diag("cannot handle symbols on both sides in %v", p)
   418  			return
   419  		}
   420  		source = &p.To
   421  	} else {
   422  		return
   423  
   424  	}
   425  
   426  	if source.Sym == nil {
   427  		c.ctxt.Diag("do not know how to handle nil symbol in %v", p)
   428  		return
   429  	}
   430  
   431  	if source.Sym.Type == objabi.STLSBSS {
   432  		return
   433  	}
   434  
   435  	// Retrieve or create the TOC anchor.
   436  	symtoc := c.ctxt.LookupInit("TOC."+source.Sym.Name, func(s *obj.LSym) {
   437  		s.Type = objabi.SDATA
   438  		s.Set(obj.AttrDuplicateOK, true)
   439  		s.Set(obj.AttrStatic, true)
   440  		c.ctxt.Data = append(c.ctxt.Data, s)
   441  		s.WriteAddr(c.ctxt, 0, 8, source.Sym, 0)
   442  	})
   443  
   444  	if source.Type == obj.TYPE_ADDR {
   445  		// MOVD $sym, Rx becomes MOVD symtoc, Rx
   446  		// MOVD $sym+<off>, Rx becomes MOVD symtoc, Rx; ADD <off>, Rx
   447  		p.From.Type = obj.TYPE_MEM
   448  		p.From.Sym = symtoc
   449  		p.From.Name = obj.NAME_TOCREF
   450  
   451  		if p.From.Offset != 0 {
   452  			q := obj.Appendp(p, c.newprog)
   453  			q.As = AADD
   454  			q.From.Type = obj.TYPE_CONST
   455  			q.From.Offset = p.From.Offset
   456  			p.From.Offset = 0
   457  			q.To = p.To
   458  		}
   459  		return
   460  
   461  	}
   462  
   463  	// MOVx sym, Ry becomes MOVD symtoc, REGTMP; MOVx (REGTMP), Ry
   464  	// MOVx Ry, sym becomes MOVD symtoc, REGTMP; MOVx Ry, (REGTMP)
   465  	// An addition may be inserted between the two MOVs if there is an offset.
   466  
   467  	q := obj.Appendp(p, c.newprog)
   468  	q.As = AMOVD
   469  	q.From.Type = obj.TYPE_MEM
   470  	q.From.Sym = symtoc
   471  	q.From.Name = obj.NAME_TOCREF
   472  	q.To.Type = obj.TYPE_REG
   473  	q.To.Reg = REGTMP
   474  
   475  	q = obj.Appendp(q, c.newprog)
   476  	q.As = p.As
   477  	q.From = p.From
   478  	q.To = p.To
   479  	if p.From.Name != obj.NAME_NONE {
   480  		q.From.Type = obj.TYPE_MEM
   481  		q.From.Reg = REGTMP
   482  		q.From.Name = obj.NAME_NONE
   483  		q.From.Sym = nil
   484  	} else if p.To.Name != obj.NAME_NONE {
   485  		q.To.Type = obj.TYPE_MEM
   486  		q.To.Reg = REGTMP
   487  		q.To.Name = obj.NAME_NONE
   488  		q.To.Sym = nil
   489  	} else {
   490  		c.ctxt.Diag("unreachable case in rewriteToUseTOC with %v", p)
   491  	}
   492  
   493  	obj.Nopout(p)
   494  }
   495  
   496  // Rewrite p, if necessary, to access global data via the global offset table.
   497  func (c *ctxt9) rewriteToUseGot(p *obj.Prog) {
   498  	if p.As == obj.ADUFFCOPY || p.As == obj.ADUFFZERO {
   499  		//     ADUFFxxx $offset
   500  		// becomes
   501  		//     MOVD runtime.duffxxx@GOT, R12
   502  		//     ADD $offset, R12
   503  		//     MOVD R12, LR
   504  		//     BL (LR)
   505  		var sym *obj.LSym
   506  		if p.As == obj.ADUFFZERO {
   507  			sym = c.ctxt.LookupABI("runtime.duffzero", obj.ABIInternal)
   508  		} else {
   509  			sym = c.ctxt.LookupABI("runtime.duffcopy", obj.ABIInternal)
   510  		}
   511  		offset := p.To.Offset
   512  		p.As = AMOVD
   513  		p.From.Type = obj.TYPE_MEM
   514  		p.From.Name = obj.NAME_GOTREF
   515  		p.From.Sym = sym
   516  		p.To.Type = obj.TYPE_REG
   517  		p.To.Reg = REG_R12
   518  		p.To.Name = obj.NAME_NONE
   519  		p.To.Offset = 0
   520  		p.To.Sym = nil
   521  		p1 := obj.Appendp(p, c.newprog)
   522  		p1.As = AADD
   523  		p1.From.Type = obj.TYPE_CONST
   524  		p1.From.Offset = offset
   525  		p1.To.Type = obj.TYPE_REG
   526  		p1.To.Reg = REG_R12
   527  		p2 := obj.Appendp(p1, c.newprog)
   528  		p2.As = AMOVD
   529  		p2.From.Type = obj.TYPE_REG
   530  		p2.From.Reg = REG_R12
   531  		p2.To.Type = obj.TYPE_REG
   532  		p2.To.Reg = REG_LR
   533  		p3 := obj.Appendp(p2, c.newprog)
   534  		p3.As = obj.ACALL
   535  		p3.To.Type = obj.TYPE_REG
   536  		p3.To.Reg = REG_LR
   537  	}
   538  
   539  	// We only care about global data: NAME_EXTERN means a global
   540  	// symbol in the Go sense, and p.Sym.Local is true for a few
   541  	// internally defined symbols.
   542  	if p.From.Type == obj.TYPE_ADDR && p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local() {
   543  		// MOVD $sym, Rx becomes MOVD sym@GOT, Rx
   544  		// MOVD $sym+<off>, Rx becomes MOVD sym@GOT, Rx; ADD <off>, Rx
   545  		if p.As != AMOVD {
   546  			c.ctxt.Diag("do not know how to handle TYPE_ADDR in %v with -dynlink", p)
   547  		}
   548  		if p.To.Type != obj.TYPE_REG {
   549  			c.ctxt.Diag("do not know how to handle LEAQ-type insn to non-register in %v with -dynlink", p)
   550  		}
   551  		p.From.Type = obj.TYPE_MEM
   552  		p.From.Name = obj.NAME_GOTREF
   553  		if p.From.Offset != 0 {
   554  			q := obj.Appendp(p, c.newprog)
   555  			q.As = AADD
   556  			q.From.Type = obj.TYPE_CONST
   557  			q.From.Offset = p.From.Offset
   558  			q.To = p.To
   559  			p.From.Offset = 0
   560  		}
   561  	}
   562  	if p.GetFrom3() != nil && p.GetFrom3().Name == obj.NAME_EXTERN {
   563  		c.ctxt.Diag("don't know how to handle %v with -dynlink", p)
   564  	}
   565  	var source *obj.Addr
   566  	// MOVx sym, Ry becomes MOVD sym@GOT, REGTMP; MOVx (REGTMP), Ry
   567  	// MOVx Ry, sym becomes MOVD sym@GOT, REGTMP; MOVx Ry, (REGTMP)
   568  	// An addition may be inserted between the two MOVs if there is an offset.
   569  	if p.From.Name == obj.NAME_EXTERN && !p.From.Sym.Local() {
   570  		if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local() {
   571  			c.ctxt.Diag("cannot handle NAME_EXTERN on both sides in %v with -dynlink", p)
   572  		}
   573  		source = &p.From
   574  	} else if p.To.Name == obj.NAME_EXTERN && !p.To.Sym.Local() {
   575  		source = &p.To
   576  	} else {
   577  		return
   578  	}
   579  	if p.As == obj.ATEXT || p.As == obj.AFUNCDATA || p.As == obj.ACALL || p.As == obj.ARET || p.As == obj.AJMP {
   580  		return
   581  	}
   582  	if source.Sym.Type == objabi.STLSBSS {
   583  		return
   584  	}
   585  	if source.Type != obj.TYPE_MEM {
   586  		c.ctxt.Diag("don't know how to handle %v with -dynlink", p)
   587  	}
   588  	p1 := obj.Appendp(p, c.newprog)
   589  	p2 := obj.Appendp(p1, c.newprog)
   590  
   591  	p1.As = AMOVD
   592  	p1.From.Type = obj.TYPE_MEM
   593  	p1.From.Sym = source.Sym
   594  	p1.From.Name = obj.NAME_GOTREF
   595  	p1.To.Type = obj.TYPE_REG
   596  	p1.To.Reg = REGTMP
   597  
   598  	p2.As = p.As
   599  	p2.From = p.From
   600  	p2.To = p.To
   601  	if p.From.Name == obj.NAME_EXTERN {
   602  		p2.From.Reg = REGTMP
   603  		p2.From.Name = obj.NAME_NONE
   604  		p2.From.Sym = nil
   605  	} else if p.To.Name == obj.NAME_EXTERN {
   606  		p2.To.Reg = REGTMP
   607  		p2.To.Name = obj.NAME_NONE
   608  		p2.To.Sym = nil
   609  	} else {
   610  		return
   611  	}
   612  	obj.Nopout(p)
   613  }
   614  
   615  func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) {
   616  	// TODO(minux): add morestack short-cuts with small fixed frame-size.
   617  	if cursym.Func().Text == nil || cursym.Func().Text.Link == nil {
   618  		return
   619  	}
   620  
   621  	c := ctxt9{ctxt: ctxt, cursym: cursym, newprog: newprog}
   622  
   623  	p := c.cursym.Func().Text
   624  	textstksiz := p.To.Offset
   625  	if textstksiz == -8 {
   626  		// Compatibility hack.
   627  		p.From.Sym.Set(obj.AttrNoFrame, true)
   628  		textstksiz = 0
   629  	}
   630  	if textstksiz%8 != 0 {
   631  		c.ctxt.Diag("frame size %d not a multiple of 8", textstksiz)
   632  	}
   633  	if p.From.Sym.NoFrame() {
   634  		if textstksiz != 0 {
   635  			c.ctxt.Diag("NOFRAME functions must have a frame size of 0, not %d", textstksiz)
   636  		}
   637  	}
   638  
   639  	c.cursym.Func().Args = p.To.Val.(int32)
   640  	c.cursym.Func().Locals = int32(textstksiz)
   641  
   642  	/*
   643  	 * find leaf subroutines
   644  	 * expand RET
   645  	 * expand BECOME pseudo
   646  	 */
   647  
   648  	var q *obj.Prog
   649  	var q1 *obj.Prog
   650  	for p := c.cursym.Func().Text; p != nil; p = p.Link {
   651  		switch p.As {
   652  		/* too hard, just leave alone */
   653  		case obj.ATEXT:
   654  			q = p
   655  
   656  			p.Mark |= LABEL | LEAF | SYNC
   657  			if p.Link != nil {
   658  				p.Link.Mark |= LABEL
   659  			}
   660  
   661  		case ANOR:
   662  			q = p
   663  			if p.To.Type == obj.TYPE_REG {
   664  				if p.To.Reg == REGZERO {
   665  					p.Mark |= LABEL | SYNC
   666  				}
   667  			}
   668  
   669  		case ALWAR,
   670  			ALBAR,
   671  			ASTBCCC,
   672  			ASTWCCC,
   673  			AEIEIO,
   674  			AICBI,
   675  			AISYNC,
   676  			ATLBIE,
   677  			ATLBIEL,
   678  			ASLBIA,
   679  			ASLBIE,
   680  			ASLBMFEE,
   681  			ASLBMFEV,
   682  			ASLBMTE,
   683  			ADCBF,
   684  			ADCBI,
   685  			ADCBST,
   686  			ADCBT,
   687  			ADCBTST,
   688  			ADCBZ,
   689  			ASYNC,
   690  			ATLBSYNC,
   691  			APTESYNC,
   692  			ALWSYNC,
   693  			ATW,
   694  			AWORD,
   695  			ARFI,
   696  			ARFCI,
   697  			ARFID,
   698  			AHRFID:
   699  			q = p
   700  			p.Mark |= LABEL | SYNC
   701  			continue
   702  
   703  		case AMOVW, AMOVWZ, AMOVD:
   704  			q = p
   705  			if p.From.Reg >= REG_SPECIAL || p.To.Reg >= REG_SPECIAL {
   706  				p.Mark |= LABEL | SYNC
   707  			}
   708  			continue
   709  
   710  		case AFABS,
   711  			AFABSCC,
   712  			AFADD,
   713  			AFADDCC,
   714  			AFCTIW,
   715  			AFCTIWCC,
   716  			AFCTIWZ,
   717  			AFCTIWZCC,
   718  			AFDIV,
   719  			AFDIVCC,
   720  			AFMADD,
   721  			AFMADDCC,
   722  			AFMOVD,
   723  			AFMOVDU,
   724  			/* case AFMOVDS: */
   725  			AFMOVS,
   726  			AFMOVSU,
   727  
   728  			/* case AFMOVSD: */
   729  			AFMSUB,
   730  			AFMSUBCC,
   731  			AFMUL,
   732  			AFMULCC,
   733  			AFNABS,
   734  			AFNABSCC,
   735  			AFNEG,
   736  			AFNEGCC,
   737  			AFNMADD,
   738  			AFNMADDCC,
   739  			AFNMSUB,
   740  			AFNMSUBCC,
   741  			AFRSP,
   742  			AFRSPCC,
   743  			AFSUB,
   744  			AFSUBCC:
   745  			q = p
   746  
   747  			p.Mark |= FLOAT
   748  			continue
   749  
   750  		case ABL,
   751  			ABCL,
   752  			obj.ADUFFZERO,
   753  			obj.ADUFFCOPY:
   754  			c.cursym.Func().Text.Mark &^= LEAF
   755  			fallthrough
   756  
   757  		case ABC,
   758  			ABEQ,
   759  			ABGE,
   760  			ABGT,
   761  			ABLE,
   762  			ABLT,
   763  			ABNE,
   764  			ABR,
   765  			ABVC,
   766  			ABVS:
   767  			p.Mark |= BRANCH
   768  			q = p
   769  			q1 = p.To.Target()
   770  			if q1 != nil {
   771  				// NOPs are not removed due to #40689.
   772  
   773  				if q1.Mark&LEAF == 0 {
   774  					q1.Mark |= LABEL
   775  				}
   776  			} else {
   777  				p.Mark |= LABEL
   778  			}
   779  			q1 = p.Link
   780  			if q1 != nil {
   781  				q1.Mark |= LABEL
   782  			}
   783  			continue
   784  
   785  		case AFCMPO, AFCMPU:
   786  			q = p
   787  			p.Mark |= FCMP | FLOAT
   788  			continue
   789  
   790  		case obj.ARET:
   791  			q = p
   792  			if p.Link != nil {
   793  				p.Link.Mark |= LABEL
   794  			}
   795  			continue
   796  
   797  		case obj.ANOP:
   798  			// NOPs are not removed due to
   799  			// #40689
   800  			continue
   801  
   802  		default:
   803  			q = p
   804  			continue
   805  		}
   806  	}
   807  
   808  	autosize := int32(0)
   809  	for p := c.cursym.Func().Text; p != nil; p = p.Link {
   810  		o := p.As
   811  		switch o {
   812  		case obj.ATEXT:
   813  			autosize = int32(textstksiz)
   814  
   815  			if p.Mark&LEAF != 0 && autosize == 0 {
   816  				// A leaf function with no locals has no frame.
   817  				p.From.Sym.Set(obj.AttrNoFrame, true)
   818  			}
   819  
   820  			if !p.From.Sym.NoFrame() {
   821  				// If there is a stack frame at all, it includes
   822  				// space to save the LR.
   823  				autosize += int32(c.ctxt.Arch.FixedFrameSize)
   824  			}
   825  
   826  			if p.Mark&LEAF != 0 && autosize < abi.StackSmall {
   827  				// A leaf function with a small stack can be marked
   828  				// NOSPLIT, avoiding a stack check.
   829  				p.From.Sym.Set(obj.AttrNoSplit, true)
   830  			}
   831  
   832  			p.To.Offset = int64(autosize)
   833  
   834  			q = p
   835  
   836  			if NeedTOCpointer(c.ctxt) && !isNOTOCfunc(c.cursym.Name) {
   837  				// When compiling Go into PIC, without PCrel support, all functions must start
   838  				// with instructions to load the TOC pointer into r2:
   839  				//
   840  				//	addis r2, r12, .TOC.-func@ha
   841  				//	addi r2, r2, .TOC.-func@l+4
   842  				//
   843  				// We could probably skip this prologue in some situations
   844  				// but it's a bit subtle. However, it is both safe and
   845  				// necessary to leave the prologue off duffzero and
   846  				// duffcopy as we rely on being able to jump to a specific
   847  				// instruction offset for them.
   848  				//
   849  				// These are AWORDS because there is no (afaict) way to
   850  				// generate the addis instruction except as part of the
   851  				// load of a large constant, and in that case there is no
   852  				// way to use r12 as the source.
   853  				//
   854  				// Note that the same condition is tested in
   855  				// putelfsym in cmd/link/internal/ld/symtab.go
   856  				// where we set the st_other field to indicate
   857  				// the presence of these instructions.
   858  				q = obj.Appendp(q, c.newprog)
   859  				q.As = AWORD
   860  				q.Pos = p.Pos
   861  				q.From.Type = obj.TYPE_CONST
   862  				q.From.Offset = 0x3c4c0000
   863  				q = obj.Appendp(q, c.newprog)
   864  				q.As = AWORD
   865  				q.Pos = p.Pos
   866  				q.From.Type = obj.TYPE_CONST
   867  				q.From.Offset = 0x38420000
   868  				c.cursym.AddRel(c.ctxt, obj.Reloc{
   869  					Type: objabi.R_ADDRPOWER_PCREL,
   870  					Off:  0,
   871  					Siz:  8,
   872  					Sym:  c.ctxt.Lookup(".TOC."),
   873  				})
   874  			}
   875  
   876  			if !c.cursym.Func().Text.From.Sym.NoSplit() {
   877  				q = c.stacksplit(q, autosize) // emit split check
   878  			}
   879  
   880  			if autosize != 0 {
   881  				var prologueEnd *obj.Prog
   882  				// Save the link register and update the SP.  MOVDU is used unless
   883  				// the frame size is too large.  The link register must be saved
   884  				// even for non-empty leaf functions so that traceback works.
   885  				if autosize >= -BIG && autosize <= BIG {
   886  					// Use MOVDU to adjust R1 when saving R31, if autosize is small.
   887  					q = obj.Appendp(q, c.newprog)
   888  					q.As = AMOVD
   889  					q.Pos = p.Pos
   890  					q.From.Type = obj.TYPE_REG
   891  					q.From.Reg = REG_LR
   892  					q.To.Type = obj.TYPE_REG
   893  					q.To.Reg = REGTMP
   894  					prologueEnd = q
   895  
   896  					q = obj.Appendp(q, c.newprog)
   897  					q.As = AMOVDU
   898  					q.Pos = p.Pos
   899  					q.From.Type = obj.TYPE_REG
   900  					q.From.Reg = REGTMP
   901  					q.To.Type = obj.TYPE_MEM
   902  					q.To.Offset = int64(-autosize)
   903  					q.To.Reg = REGSP
   904  					q.Spadj = autosize
   905  				} else {
   906  					// Frame size is too large for an stdu MOVDU instruction, use stdux MOVDU.
   907  					q = obj.Appendp(q, c.newprog)
   908  					q.As = AMOVD
   909  					q.Pos = p.Pos
   910  					q.From.Type = obj.TYPE_REG
   911  					q.From.Reg = REG_LR
   912  					q.To.Type = obj.TYPE_REG
   913  					q.To.Reg = REG_R29
   914  
   915  					// Create stack adjustment in REGTMP
   916  					q = obj.Appendp(q, c.newprog)
   917  					q.As = AMOVD
   918  					q.Pos = p.Pos
   919  					q.From.Type = obj.TYPE_CONST
   920  					q.From.Offset = int64(-autosize)
   921  					q.To.Type = obj.TYPE_REG
   922  					q.To.Reg = REGTMP
   923  
   924  					prologueEnd = q
   925  
   926  					// MOVDU R29, R31(R1)
   927  					q = obj.Appendp(q, c.newprog)
   928  					q.As = AMOVDU
   929  					q.Pos = p.Pos
   930  					q.From.Type = obj.TYPE_REG
   931  					q.From.Reg = REG_R29
   932  					q.To.Type = obj.TYPE_MEM
   933  					q.To.Reg = REGTMP
   934  					q.To.Index = REGSP
   935  					q.Spadj = autosize
   936  				}
   937  
   938  				prologueEnd.Pos = prologueEnd.Pos.WithXlogue(src.PosPrologueEnd)
   939  			} else if c.cursym.Func().Text.Mark&LEAF == 0 {
   940  				// A very few functions that do not return to their caller
   941  				// (e.g. gogo) are not identified as leaves but still have
   942  				// no frame.
   943  				c.cursym.Func().Text.Mark |= LEAF
   944  			}
   945  
   946  			if c.cursym.Func().Text.Mark&LEAF != 0 {
   947  				c.cursym.Set(obj.AttrLeaf, true)
   948  				break
   949  			}
   950  
   951  			if NeedTOCpointer(c.ctxt) {
   952  				q = obj.Appendp(q, c.newprog)
   953  				q.As = AMOVD
   954  				q.Pos = p.Pos
   955  				q.From.Type = obj.TYPE_REG
   956  				q.From.Reg = REG_R2
   957  				q.To.Type = obj.TYPE_MEM
   958  				q.To.Reg = REGSP
   959  				q.To.Offset = 24
   960  			}
   961  
   962  		case obj.ARET:
   963  			if p.From.Type == obj.TYPE_CONST {
   964  				c.ctxt.Diag("using BECOME (%v) is not supported!", p)
   965  				break
   966  			}
   967  
   968  			retTarget, retReg := p.To.Sym, p.To.Reg
   969  			finish := func(last *obj.Prog) {}
   970  			if retReg == obj.REG_NONE {
   971  				retReg = REG_LR
   972  			} else {
   973  				// Move target address into REG_CTR.
   974  				// (Indirect branches can only go to REG_LR or REG_CTR.)
   975  				x := newprog()
   976  				*x = *p
   977  				p.As = AMOVD
   978  				p.From.Type = obj.TYPE_REG
   979  				p.From.Reg = retReg
   980  				p.To.Type = obj.TYPE_REG
   981  				p.To.Reg = REG_CTR
   982  				retReg = REG_CTR
   983  				p.Link = x
   984  				// Everything from here through the BR (CTR) must be an
   985  				// unsafe point. runtime.asyncPreempt does not preserve CTR,
   986  				// and its resume sequence leaves CTR holding the resume PC,
   987  				// so a goroutine preempted at the BR (CTR) would resume by
   988  				// branching to that very instruction and spin there forever.
   989  				c.ctxt.StartUnsafePoint(p, c.newprog)
   990  				finish = func(last *obj.Prog) {
   991  					c.ctxt.EndUnsafePoint(last, c.newprog, -1)
   992  				}
   993  				p = x
   994  			}
   995  
   996  			if c.cursym.Func().Text.Mark&LEAF != 0 {
   997  				if autosize == 0 {
   998  					p.As = ABR
   999  					p.From = obj.Addr{}
  1000  					if retTarget == nil {
  1001  						p.To.Type = obj.TYPE_REG
  1002  						p.To.Reg = retReg
  1003  					} else {
  1004  						p.To.Type = obj.TYPE_BRANCH
  1005  						p.To.Sym = retTarget
  1006  					}
  1007  					p.Mark |= BRANCH
  1008  					finish(p)
  1009  					break
  1010  				}
  1011  
  1012  				p.As = AADD
  1013  				p.From.Type = obj.TYPE_CONST
  1014  				p.From.Offset = int64(autosize)
  1015  				p.To.Type = obj.TYPE_REG
  1016  				p.To.Reg = REGSP
  1017  				p.Spadj = -autosize
  1018  
  1019  				q = c.newprog()
  1020  				q.As = ABR
  1021  				q.Pos = p.Pos
  1022  				if retTarget == nil {
  1023  					q.To.Type = obj.TYPE_REG
  1024  					q.To.Reg = retReg
  1025  				} else {
  1026  					q.To.Type = obj.TYPE_BRANCH
  1027  					q.To.Sym = retTarget
  1028  				}
  1029  				q.Mark |= BRANCH
  1030  				q.Spadj = +autosize
  1031  
  1032  				q.Link = p.Link
  1033  				p.Link = q
  1034  				finish(q)
  1035  				break
  1036  			}
  1037  
  1038  			p.As = AMOVD
  1039  			p.From.Type = obj.TYPE_MEM
  1040  			p.From.Offset = 0
  1041  			p.From.Reg = REGSP
  1042  			p.To.Type = obj.TYPE_REG
  1043  			p.To.Reg = REGTMP
  1044  
  1045  			q = c.newprog()
  1046  			q.As = AMOVD
  1047  			q.Pos = p.Pos
  1048  			q.From.Type = obj.TYPE_REG
  1049  			q.From.Reg = REGTMP
  1050  			q.To.Type = obj.TYPE_REG
  1051  			q.To.Reg = REG_LR
  1052  
  1053  			q.Link = p.Link
  1054  			p.Link = q
  1055  			p = q
  1056  
  1057  			if false {
  1058  				// Debug bad returns
  1059  				q = c.newprog()
  1060  
  1061  				q.As = AMOVD
  1062  				q.Pos = p.Pos
  1063  				q.From.Type = obj.TYPE_MEM
  1064  				q.From.Offset = 0
  1065  				q.From.Reg = REGTMP
  1066  				q.To.Type = obj.TYPE_REG
  1067  				q.To.Reg = REGTMP
  1068  
  1069  				q.Link = p.Link
  1070  				p.Link = q
  1071  				p = q
  1072  			}
  1073  			prev := p
  1074  			if autosize != 0 {
  1075  				q = c.newprog()
  1076  				q.As = AADD
  1077  				q.Pos = p.Pos
  1078  				q.From.Type = obj.TYPE_CONST
  1079  				q.From.Offset = int64(autosize)
  1080  				q.To.Type = obj.TYPE_REG
  1081  				q.To.Reg = REGSP
  1082  				q.Spadj = -autosize
  1083  
  1084  				q.Link = p.Link
  1085  				prev.Link = q
  1086  				prev = q
  1087  			}
  1088  
  1089  			q1 = c.newprog()
  1090  			q1.As = ABR
  1091  			q1.Pos = p.Pos
  1092  			if retTarget == nil {
  1093  				q1.To.Type = obj.TYPE_REG
  1094  				q1.To.Reg = retReg
  1095  			} else {
  1096  				q1.To.Type = obj.TYPE_BRANCH
  1097  				q1.To.Sym = retTarget
  1098  			}
  1099  			q1.Mark |= BRANCH
  1100  			q1.Spadj = +autosize
  1101  
  1102  			q1.Link = q.Link
  1103  			prev.Link = q1
  1104  			finish(q1)
  1105  
  1106  		case AADD:
  1107  			if p.To.Type == obj.TYPE_REG && p.To.Reg == REGSP && p.From.Type == obj.TYPE_CONST {
  1108  				p.Spadj = int32(-p.From.Offset)
  1109  			}
  1110  		case AMOVDU:
  1111  			if p.To.Type == obj.TYPE_MEM && p.To.Reg == REGSP {
  1112  				p.Spadj = int32(-p.To.Offset)
  1113  			}
  1114  			if p.From.Type == obj.TYPE_MEM && p.From.Reg == REGSP {
  1115  				p.Spadj = int32(-p.From.Offset)
  1116  			}
  1117  		case obj.AGETCALLERPC:
  1118  			if cursym.Leaf() {
  1119  				/* MOVD LR, Rd */
  1120  				p.As = AMOVD
  1121  				p.From.Type = obj.TYPE_REG
  1122  				p.From.Reg = REG_LR
  1123  			} else {
  1124  				/* MOVD (RSP), Rd */
  1125  				p.As = AMOVD
  1126  				p.From.Type = obj.TYPE_MEM
  1127  				p.From.Reg = REGSP
  1128  			}
  1129  		}
  1130  
  1131  		if p.To.Type == obj.TYPE_REG && p.To.Reg == REGSP && p.Spadj == 0 && p.As != ACMPU {
  1132  			f := c.cursym.Func()
  1133  			if f.FuncFlag&abi.FuncFlagSPWrite == 0 {
  1134  				c.cursym.Func().FuncFlag |= abi.FuncFlagSPWrite
  1135  				if ctxt.Debugvlog || !ctxt.IsAsm {
  1136  					ctxt.Logf("auto-SPWRITE: %s %v\n", c.cursym.Name, p)
  1137  					if !ctxt.IsAsm {
  1138  						ctxt.Diag("invalid auto-SPWRITE in non-assembly")
  1139  						ctxt.DiagFlush()
  1140  						log.Fatalf("bad SPWRITE")
  1141  					}
  1142  				}
  1143  			}
  1144  		}
  1145  	}
  1146  }
  1147  
  1148  /*
  1149  // instruction scheduling
  1150  
  1151  	if(debug['Q'] == 0)
  1152  		return;
  1153  
  1154  	curtext = nil;
  1155  	q = nil;	// p - 1
  1156  	q1 = firstp;	// top of block
  1157  	o = 0;		// count of instructions
  1158  	for(p = firstp; p != nil; p = p1) {
  1159  		p1 = p->link;
  1160  		o++;
  1161  		if(p->mark & NOSCHED){
  1162  			if(q1 != p){
  1163  				sched(q1, q);
  1164  			}
  1165  			for(; p != nil; p = p->link){
  1166  				if(!(p->mark & NOSCHED))
  1167  					break;
  1168  				q = p;
  1169  			}
  1170  			p1 = p;
  1171  			q1 = p;
  1172  			o = 0;
  1173  			continue;
  1174  		}
  1175  		if(p->mark & (LABEL|SYNC)) {
  1176  			if(q1 != p)
  1177  				sched(q1, q);
  1178  			q1 = p;
  1179  			o = 1;
  1180  		}
  1181  		if(p->mark & (BRANCH|SYNC)) {
  1182  			sched(q1, p);
  1183  			q1 = p1;
  1184  			o = 0;
  1185  		}
  1186  		if(o >= NSCHED) {
  1187  			sched(q1, p);
  1188  			q1 = p1;
  1189  			o = 0;
  1190  		}
  1191  		q = p;
  1192  	}
  1193  */
  1194  func (c *ctxt9) stacksplit(p *obj.Prog, framesize int32) *obj.Prog {
  1195  	if c.ctxt.Flag_maymorestack != "" {
  1196  		if c.ctxt.Flag_shared || c.ctxt.Flag_dynlink {
  1197  			// See the call to morestack for why these are
  1198  			// complicated to support.
  1199  			c.ctxt.Diag("maymorestack with -shared or -dynlink is not supported")
  1200  		}
  1201  
  1202  		// Spill arguments. This has to happen before we open
  1203  		// any more frame space.
  1204  		p = c.cursym.Func().SpillRegisterArgs(p, c.newprog)
  1205  
  1206  		// Save LR and REGCTXT
  1207  		frameSize := 8 + c.ctxt.Arch.FixedFrameSize
  1208  
  1209  		// MOVD LR, REGTMP
  1210  		p = obj.Appendp(p, c.newprog)
  1211  		p.As = AMOVD
  1212  		p.From.Type = obj.TYPE_REG
  1213  		p.From.Reg = REG_LR
  1214  		p.To.Type = obj.TYPE_REG
  1215  		p.To.Reg = REGTMP
  1216  		// MOVDU REGTMP, -16(SP)
  1217  		p = obj.Appendp(p, c.newprog)
  1218  		p.As = AMOVDU
  1219  		p.From.Type = obj.TYPE_REG
  1220  		p.From.Reg = REGTMP
  1221  		p.To.Type = obj.TYPE_MEM
  1222  		p.To.Offset = -frameSize
  1223  		p.To.Reg = REGSP
  1224  		p.Spadj = int32(frameSize)
  1225  
  1226  		// MOVD REGCTXT, 8(SP)
  1227  		p = obj.Appendp(p, c.newprog)
  1228  		p.As = AMOVD
  1229  		p.From.Type = obj.TYPE_REG
  1230  		p.From.Reg = REGCTXT
  1231  		p.To.Type = obj.TYPE_MEM
  1232  		p.To.Offset = 8
  1233  		p.To.Reg = REGSP
  1234  
  1235  		// BL maymorestack
  1236  		p = obj.Appendp(p, c.newprog)
  1237  		p.As = ABL
  1238  		p.To.Type = obj.TYPE_BRANCH
  1239  		// See ../x86/obj6.go
  1240  		p.To.Sym = c.ctxt.LookupABI(c.ctxt.Flag_maymorestack, c.cursym.ABI())
  1241  
  1242  		// Restore LR and REGCTXT
  1243  
  1244  		// MOVD 8(SP), REGCTXT
  1245  		p = obj.Appendp(p, c.newprog)
  1246  		p.As = AMOVD
  1247  		p.From.Type = obj.TYPE_MEM
  1248  		p.From.Offset = 8
  1249  		p.From.Reg = REGSP
  1250  		p.To.Type = obj.TYPE_REG
  1251  		p.To.Reg = REGCTXT
  1252  
  1253  		// MOVD 0(SP), REGTMP
  1254  		p = obj.Appendp(p, c.newprog)
  1255  		p.As = AMOVD
  1256  		p.From.Type = obj.TYPE_MEM
  1257  		p.From.Offset = 0
  1258  		p.From.Reg = REGSP
  1259  		p.To.Type = obj.TYPE_REG
  1260  		p.To.Reg = REGTMP
  1261  
  1262  		// MOVD REGTMP, LR
  1263  		p = obj.Appendp(p, c.newprog)
  1264  		p.As = AMOVD
  1265  		p.From.Type = obj.TYPE_REG
  1266  		p.From.Reg = REGTMP
  1267  		p.To.Type = obj.TYPE_REG
  1268  		p.To.Reg = REG_LR
  1269  
  1270  		// ADD $16, SP
  1271  		p = obj.Appendp(p, c.newprog)
  1272  		p.As = AADD
  1273  		p.From.Type = obj.TYPE_CONST
  1274  		p.From.Offset = frameSize
  1275  		p.To.Type = obj.TYPE_REG
  1276  		p.To.Reg = REGSP
  1277  		p.Spadj = -int32(frameSize)
  1278  
  1279  		// Unspill arguments.
  1280  		p = c.cursym.Func().UnspillRegisterArgs(p, c.newprog)
  1281  	}
  1282  
  1283  	// save entry point, but skipping the two instructions setting R2 in shared mode and maymorestack
  1284  	startPred := p
  1285  
  1286  	// MOVD	g_stackguard(g), R22
  1287  	p = obj.Appendp(p, c.newprog)
  1288  
  1289  	p.As = AMOVD
  1290  	p.From.Type = obj.TYPE_MEM
  1291  	p.From.Reg = REGG
  1292  	p.From.Offset = 2 * int64(c.ctxt.Arch.PtrSize) // G.stackguard0
  1293  	if c.cursym.CFunc() {
  1294  		p.From.Offset = 3 * int64(c.ctxt.Arch.PtrSize) // G.stackguard1
  1295  	}
  1296  	p.To.Type = obj.TYPE_REG
  1297  	p.To.Reg = REG_R22
  1298  
  1299  	// Mark the stack bound check and morestack call async nonpreemptible.
  1300  	// If we get preempted here, when resumed the preemption request is
  1301  	// cleared, but we'll still call morestack, which will double the stack
  1302  	// unnecessarily. See issue #35470.
  1303  	p = c.ctxt.StartUnsafePoint(p, c.newprog)
  1304  
  1305  	var q *obj.Prog
  1306  	if framesize <= abi.StackSmall {
  1307  		// small stack: SP < stackguard
  1308  		//	CMP	stackguard, SP
  1309  		p = obj.Appendp(p, c.newprog)
  1310  
  1311  		p.As = ACMPU
  1312  		p.From.Type = obj.TYPE_REG
  1313  		p.From.Reg = REG_R22
  1314  		p.To.Type = obj.TYPE_REG
  1315  		p.To.Reg = REGSP
  1316  	} else {
  1317  		// large stack: SP-framesize < stackguard-StackSmall
  1318  		offset := int64(framesize) - abi.StackSmall
  1319  		if framesize > abi.StackBig {
  1320  			// Such a large stack we need to protect against underflow.
  1321  			// The runtime guarantees SP > objabi.StackBig, but
  1322  			// framesize is large enough that SP-framesize may
  1323  			// underflow, causing a direct comparison with the
  1324  			// stack guard to incorrectly succeed. We explicitly
  1325  			// guard against underflow.
  1326  			//
  1327  			//	CMPU	SP, $(framesize-StackSmall)
  1328  			//	BLT	label-of-call-to-morestack
  1329  			if offset <= 0xffff {
  1330  				p = obj.Appendp(p, c.newprog)
  1331  				p.As = ACMPU
  1332  				p.From.Type = obj.TYPE_REG
  1333  				p.From.Reg = REGSP
  1334  				p.To.Type = obj.TYPE_CONST
  1335  				p.To.Offset = offset
  1336  			} else {
  1337  				// Constant is too big for CMPU.
  1338  				p = obj.Appendp(p, c.newprog)
  1339  				p.As = AMOVD
  1340  				p.From.Type = obj.TYPE_CONST
  1341  				p.From.Offset = offset
  1342  				p.To.Type = obj.TYPE_REG
  1343  				p.To.Reg = REG_R23
  1344  
  1345  				p = obj.Appendp(p, c.newprog)
  1346  				p.As = ACMPU
  1347  				p.From.Type = obj.TYPE_REG
  1348  				p.From.Reg = REGSP
  1349  				p.To.Type = obj.TYPE_REG
  1350  				p.To.Reg = REG_R23
  1351  			}
  1352  
  1353  			p = obj.Appendp(p, c.newprog)
  1354  			q = p
  1355  			p.As = ABLT
  1356  			p.To.Type = obj.TYPE_BRANCH
  1357  		}
  1358  
  1359  		// Check against the stack guard. We've ensured this won't underflow.
  1360  		//	ADD  $-(framesize-StackSmall), SP, R4
  1361  		//	CMPU stackguard, R4
  1362  		p = obj.Appendp(p, c.newprog)
  1363  
  1364  		p.As = AADD
  1365  		p.From.Type = obj.TYPE_CONST
  1366  		p.From.Offset = -offset
  1367  		p.Reg = REGSP
  1368  		p.To.Type = obj.TYPE_REG
  1369  		p.To.Reg = REG_R23
  1370  
  1371  		p = obj.Appendp(p, c.newprog)
  1372  		p.As = ACMPU
  1373  		p.From.Type = obj.TYPE_REG
  1374  		p.From.Reg = REG_R22
  1375  		p.To.Type = obj.TYPE_REG
  1376  		p.To.Reg = REG_R23
  1377  	}
  1378  
  1379  	// q1: BLT	done
  1380  	p = obj.Appendp(p, c.newprog)
  1381  	q1 := p
  1382  
  1383  	p.As = ABLT
  1384  	p.To.Type = obj.TYPE_BRANCH
  1385  
  1386  	p = obj.Appendp(p, c.newprog)
  1387  	p.As = obj.ANOP // zero-width place holder
  1388  
  1389  	if q != nil {
  1390  		q.To.SetTarget(p)
  1391  	}
  1392  
  1393  	// Spill the register args that could be clobbered by the
  1394  	// morestack code.
  1395  
  1396  	spill := c.cursym.Func().SpillRegisterArgs(p, c.newprog)
  1397  
  1398  	// MOVD LR, R5
  1399  	p = obj.Appendp(spill, c.newprog)
  1400  	p.As = AMOVD
  1401  	p.From.Type = obj.TYPE_REG
  1402  	p.From.Reg = REG_LR
  1403  	p.To.Type = obj.TYPE_REG
  1404  	p.To.Reg = REG_R5
  1405  
  1406  	p = c.ctxt.EmitEntryStackMap(c.cursym, p, c.newprog)
  1407  
  1408  	var morestacksym *obj.LSym
  1409  	if c.cursym.CFunc() {
  1410  		morestacksym = c.ctxt.Lookup("runtime.morestackc")
  1411  	} else if !c.cursym.Func().Text.From.Sym.NeedCtxt() {
  1412  		morestacksym = c.ctxt.Lookup("runtime.morestack_noctxt")
  1413  	} else {
  1414  		morestacksym = c.ctxt.Lookup("runtime.morestack")
  1415  	}
  1416  
  1417  	if NeedTOCpointer(c.ctxt) {
  1418  		// In PPC64 PIC code, R2 is used as TOC pointer derived from R12
  1419  		// which is the address of function entry point when entering
  1420  		// the function. We need to preserve R2 across call to morestack.
  1421  		// Fortunately, in shared mode, 8(SP) and 16(SP) are reserved in
  1422  		// the caller's frame, but not used (0(SP) is caller's saved LR,
  1423  		// 24(SP) is caller's saved R2). Use 8(SP) to save this function's R2.
  1424  		// MOVD R2, 8(SP)
  1425  		p = obj.Appendp(p, c.newprog)
  1426  		p.As = AMOVD
  1427  		p.From.Type = obj.TYPE_REG
  1428  		p.From.Reg = REG_R2
  1429  		p.To.Type = obj.TYPE_MEM
  1430  		p.To.Reg = REGSP
  1431  		p.To.Offset = 8
  1432  	}
  1433  
  1434  	if c.ctxt.Flag_dynlink {
  1435  		// Avoid calling morestack via a PLT when dynamically linking. The
  1436  		// PLT stubs generated by the system linker on ppc64le when "std r2,
  1437  		// 24(r1)" to save the TOC pointer in their callers stack
  1438  		// frame. Unfortunately (and necessarily) morestack is called before
  1439  		// the function that calls it sets up its frame and so the PLT ends
  1440  		// up smashing the saved TOC pointer for its caller's caller.
  1441  		//
  1442  		// According to the ABI documentation there is a mechanism to avoid
  1443  		// the TOC save that the PLT stub does (put a R_PPC64_TOCSAVE
  1444  		// relocation on the nop after the call to morestack) but at the time
  1445  		// of writing it is not supported at all by gold and my attempt to
  1446  		// use it with ld.bfd caused an internal linker error. So this hack
  1447  		// seems preferable.
  1448  
  1449  		// MOVD $runtime.morestack(SB), R12
  1450  		p = obj.Appendp(p, c.newprog)
  1451  		p.As = AMOVD
  1452  		p.From.Type = obj.TYPE_MEM
  1453  		p.From.Sym = morestacksym
  1454  		p.From.Name = obj.NAME_GOTREF
  1455  		p.To.Type = obj.TYPE_REG
  1456  		p.To.Reg = REG_R12
  1457  
  1458  		// MOVD R12, LR
  1459  		p = obj.Appendp(p, c.newprog)
  1460  		p.As = AMOVD
  1461  		p.From.Type = obj.TYPE_REG
  1462  		p.From.Reg = REG_R12
  1463  		p.To.Type = obj.TYPE_REG
  1464  		p.To.Reg = REG_LR
  1465  
  1466  		// BL LR
  1467  		p = obj.Appendp(p, c.newprog)
  1468  		p.As = obj.ACALL
  1469  		p.To.Type = obj.TYPE_REG
  1470  		p.To.Reg = REG_LR
  1471  	} else {
  1472  		// BL	runtime.morestack(SB)
  1473  		p = obj.Appendp(p, c.newprog)
  1474  
  1475  		p.As = ABL
  1476  		p.To.Type = obj.TYPE_BRANCH
  1477  		p.To.Sym = morestacksym
  1478  	}
  1479  
  1480  	if NeedTOCpointer(c.ctxt) {
  1481  		// MOVD 8(SP), R2
  1482  		p = obj.Appendp(p, c.newprog)
  1483  		p.As = AMOVD
  1484  		p.From.Type = obj.TYPE_MEM
  1485  		p.From.Reg = REGSP
  1486  		p.From.Offset = 8
  1487  		p.To.Type = obj.TYPE_REG
  1488  		p.To.Reg = REG_R2
  1489  	}
  1490  
  1491  	// The instructions which unspill regs should be preemptible.
  1492  	p = c.ctxt.EndUnsafePoint(p, c.newprog, -1)
  1493  	unspill := c.cursym.Func().UnspillRegisterArgs(p, c.newprog)
  1494  
  1495  	// BR	start
  1496  	p = obj.Appendp(unspill, c.newprog)
  1497  	p.As = ABR
  1498  	p.To.Type = obj.TYPE_BRANCH
  1499  	p.To.SetTarget(startPred.Link)
  1500  
  1501  	// placeholder for q1's jump target
  1502  	p = obj.Appendp(p, c.newprog)
  1503  
  1504  	p.As = obj.ANOP // zero-width place holder
  1505  	q1.To.SetTarget(p)
  1506  
  1507  	return p
  1508  }
  1509  
  1510  // MMA accumulator to/from instructions are slightly ambiguous since
  1511  // the argument represents both source and destination, specified as
  1512  // an accumulator. It is treated as a unary destination to simplify
  1513  // the code generation in ppc64map.
  1514  var unaryDst = map[obj.As]bool{
  1515  	AXXSETACCZ: true,
  1516  	AXXMTACC:   true,
  1517  	AXXMFACC:   true,
  1518  }
  1519  
  1520  var Linkppc64 = obj.LinkArch{
  1521  	Arch:           sys.ArchPPC64,
  1522  	Init:           buildop,
  1523  	Preprocess:     preprocess,
  1524  	Assemble:       span9,
  1525  	Progedit:       progedit,
  1526  	UnaryDst:       unaryDst,
  1527  	DWARFRegisters: PPC64DWARFRegisters,
  1528  }
  1529  
  1530  var Linkppc64le = obj.LinkArch{
  1531  	Arch:           sys.ArchPPC64LE,
  1532  	Init:           buildop,
  1533  	Preprocess:     preprocess,
  1534  	Assemble:       span9,
  1535  	Progedit:       progedit,
  1536  	UnaryDst:       unaryDst,
  1537  	DWARFRegisters: PPC64DWARFRegisters,
  1538  }
  1539  

View as plain text