github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/cmd/compile/internal/arm64/ggen.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package arm64
     6  
     7  import (
     8  	"cmd/compile/internal/gc"
     9  	"cmd/internal/obj"
    10  	"cmd/internal/obj/arm64"
    11  	"fmt"
    12  )
    13  
    14  func defframe(ptxt *obj.Prog) {
    15  	// fill in argument size, stack size
    16  	ptxt.To.Type = obj.TYPE_TEXTSIZE
    17  
    18  	ptxt.To.Val = int32(gc.Rnd(gc.Curfn.Type.ArgWidth(), int64(gc.Widthptr)))
    19  	frame := uint32(gc.Rnd(gc.Stksize+gc.Maxarg, int64(gc.Widthreg)))
    20  
    21  	// arm64 requires that the frame size (not counting saved LR)
    22  	// be empty or be 8 mod 16. If not, pad it.
    23  	if frame != 0 && frame%16 != 8 {
    24  		frame += 8
    25  	}
    26  
    27  	ptxt.To.Offset = int64(frame)
    28  
    29  	// insert code to zero ambiguously live variables
    30  	// so that the garbage collector only sees initialized values
    31  	// when it looks for pointers.
    32  	p := ptxt
    33  
    34  	hi := int64(0)
    35  	lo := hi
    36  
    37  	// iterate through declarations - they are sorted in decreasing xoffset order.
    38  	for _, n := range gc.Curfn.Func.Dcl {
    39  		if !n.Name.Needzero {
    40  			continue
    41  		}
    42  		if n.Class != gc.PAUTO {
    43  			gc.Fatalf("needzero class %d", n.Class)
    44  		}
    45  		if n.Type.Width%int64(gc.Widthptr) != 0 || n.Xoffset%int64(gc.Widthptr) != 0 || n.Type.Width == 0 {
    46  			gc.Fatalf("var %v has size %d offset %d", gc.Nconv(n, gc.FmtLong), int(n.Type.Width), int(n.Xoffset))
    47  		}
    48  
    49  		if lo != hi && n.Xoffset+n.Type.Width >= lo-int64(2*gc.Widthreg) {
    50  			// merge with range we already have
    51  			lo = n.Xoffset
    52  
    53  			continue
    54  		}
    55  
    56  		// zero old range
    57  		p = zerorange(p, int64(frame), lo, hi)
    58  
    59  		// set new range
    60  		hi = n.Xoffset + n.Type.Width
    61  
    62  		lo = n.Xoffset
    63  	}
    64  
    65  	// zero final range
    66  	zerorange(p, int64(frame), lo, hi)
    67  }
    68  
    69  var darwin = obj.Getgoos() == "darwin"
    70  
    71  func zerorange(p *obj.Prog, frame int64, lo int64, hi int64) *obj.Prog {
    72  	cnt := hi - lo
    73  	if cnt == 0 {
    74  		return p
    75  	}
    76  	if cnt < int64(4*gc.Widthptr) {
    77  		for i := int64(0); i < cnt; i += int64(gc.Widthptr) {
    78  			p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGZERO, 0, obj.TYPE_MEM, arm64.REGSP, 8+frame+lo+i)
    79  		}
    80  	} else if cnt <= int64(128*gc.Widthptr) && !darwin { // darwin ld64 cannot handle BR26 reloc with non-zero addend
    81  		p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGSP, 0, obj.TYPE_REG, arm64.REGRT1, 0)
    82  		p = appendpp(p, arm64.AADD, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, arm64.REGRT1, 0)
    83  		p.Reg = arm64.REGRT1
    84  		p = appendpp(p, obj.ADUFFZERO, obj.TYPE_NONE, 0, 0, obj.TYPE_MEM, 0, 0)
    85  		f := gc.Sysfunc("duffzero")
    86  		gc.Naddr(&p.To, f)
    87  		gc.Afunclit(&p.To, f)
    88  		p.To.Offset = 4 * (128 - cnt/int64(gc.Widthptr))
    89  	} else {
    90  		p = appendpp(p, arm64.AMOVD, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, arm64.REGTMP, 0)
    91  		p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGSP, 0, obj.TYPE_REG, arm64.REGRT1, 0)
    92  		p = appendpp(p, arm64.AADD, obj.TYPE_REG, arm64.REGTMP, 0, obj.TYPE_REG, arm64.REGRT1, 0)
    93  		p.Reg = arm64.REGRT1
    94  		p = appendpp(p, arm64.AMOVD, obj.TYPE_CONST, 0, cnt, obj.TYPE_REG, arm64.REGTMP, 0)
    95  		p = appendpp(p, arm64.AADD, obj.TYPE_REG, arm64.REGTMP, 0, obj.TYPE_REG, arm64.REGRT2, 0)
    96  		p.Reg = arm64.REGRT1
    97  		p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGZERO, 0, obj.TYPE_MEM, arm64.REGRT1, int64(gc.Widthptr))
    98  		p.Scond = arm64.C_XPRE
    99  		p1 := p
   100  		p = appendpp(p, arm64.ACMP, obj.TYPE_REG, arm64.REGRT1, 0, obj.TYPE_NONE, 0, 0)
   101  		p.Reg = arm64.REGRT2
   102  		p = appendpp(p, arm64.ABNE, obj.TYPE_NONE, 0, 0, obj.TYPE_BRANCH, 0, 0)
   103  		gc.Patch(p, p1)
   104  	}
   105  
   106  	return p
   107  }
   108  
   109  func appendpp(p *obj.Prog, as obj.As, ftype obj.AddrType, freg int, foffset int64, ttype obj.AddrType, treg int, toffset int64) *obj.Prog {
   110  	q := gc.Ctxt.NewProg()
   111  	gc.Clearp(q)
   112  	q.As = as
   113  	q.Lineno = p.Lineno
   114  	q.From.Type = ftype
   115  	q.From.Reg = int16(freg)
   116  	q.From.Offset = foffset
   117  	q.To.Type = ttype
   118  	q.To.Reg = int16(treg)
   119  	q.To.Offset = toffset
   120  	q.Link = p.Link
   121  	p.Link = q
   122  	return q
   123  }
   124  
   125  func ginsnop() {
   126  	var con gc.Node
   127  	gc.Nodconst(&con, gc.Types[gc.TINT], 0)
   128  	gins(arm64.AHINT, &con, nil)
   129  }
   130  
   131  var panicdiv *gc.Node
   132  
   133  /*
   134   * generate division.
   135   * generates one of:
   136   *	res = nl / nr
   137   *	res = nl % nr
   138   * according to op.
   139   */
   140  func dodiv(op gc.Op, nl *gc.Node, nr *gc.Node, res *gc.Node) {
   141  	// Have to be careful about handling
   142  	// most negative int divided by -1 correctly.
   143  	// The hardware will generate undefined result.
   144  	// Also need to explicitly trap on division on zero,
   145  	// the hardware will silently generate undefined result.
   146  	// DIVW will leave unpredictable result in higher 32-bit,
   147  	// so always use DIVD/DIVDU.
   148  	t := nl.Type
   149  
   150  	t0 := t
   151  	check := false
   152  	if t.IsSigned() {
   153  		check = true
   154  		if gc.Isconst(nl, gc.CTINT) && nl.Int64() != -(1<<uint64(t.Width*8-1)) {
   155  			check = false
   156  		} else if gc.Isconst(nr, gc.CTINT) && nr.Int64() != -1 {
   157  			check = false
   158  		}
   159  	}
   160  
   161  	if t.Width < 8 {
   162  		if t.IsSigned() {
   163  			t = gc.Types[gc.TINT64]
   164  		} else {
   165  			t = gc.Types[gc.TUINT64]
   166  		}
   167  		check = false
   168  	}
   169  
   170  	a := optoas(gc.ODIV, t)
   171  
   172  	var tl gc.Node
   173  	gc.Regalloc(&tl, t0, nil)
   174  	var tr gc.Node
   175  	gc.Regalloc(&tr, t0, nil)
   176  	if nl.Ullman >= nr.Ullman {
   177  		gc.Cgen(nl, &tl)
   178  		gc.Cgen(nr, &tr)
   179  	} else {
   180  		gc.Cgen(nr, &tr)
   181  		gc.Cgen(nl, &tl)
   182  	}
   183  
   184  	if t != t0 {
   185  		// Convert
   186  		tl2 := tl
   187  
   188  		tr2 := tr
   189  		tl.Type = t
   190  		tr.Type = t
   191  		gmove(&tl2, &tl)
   192  		gmove(&tr2, &tr)
   193  	}
   194  
   195  	// Handle divide-by-zero panic.
   196  	p1 := gins(optoas(gc.OCMP, t), &tr, nil)
   197  	p1.Reg = arm64.REGZERO
   198  	p1 = gc.Gbranch(optoas(gc.ONE, t), nil, +1)
   199  	if panicdiv == nil {
   200  		panicdiv = gc.Sysfunc("panicdivide")
   201  	}
   202  	gc.Ginscall(panicdiv, -1)
   203  	gc.Patch(p1, gc.Pc)
   204  
   205  	var p2 *obj.Prog
   206  	if check {
   207  		var nm1 gc.Node
   208  		gc.Nodconst(&nm1, t, -1)
   209  		gcmp(optoas(gc.OCMP, t), &tr, &nm1)
   210  		p1 := gc.Gbranch(optoas(gc.ONE, t), nil, +1)
   211  		if op == gc.ODIV {
   212  			// a / (-1) is -a.
   213  			gins(optoas(gc.OMINUS, t), &tl, &tl)
   214  
   215  			gmove(&tl, res)
   216  		} else {
   217  			// a % (-1) is 0.
   218  			var nz gc.Node
   219  			gc.Nodconst(&nz, t, 0)
   220  
   221  			gmove(&nz, res)
   222  		}
   223  
   224  		p2 = gc.Gbranch(obj.AJMP, nil, 0)
   225  		gc.Patch(p1, gc.Pc)
   226  	}
   227  
   228  	p1 = gins(a, &tr, &tl)
   229  	if op == gc.ODIV {
   230  		gc.Regfree(&tr)
   231  		gmove(&tl, res)
   232  	} else {
   233  		// A%B = A-(A/B*B)
   234  		var tm gc.Node
   235  		gc.Regalloc(&tm, t, nil)
   236  
   237  		// patch div to use the 3 register form
   238  		// TODO(minux): add gins3?
   239  		p1.Reg = p1.To.Reg
   240  
   241  		p1.To.Reg = tm.Reg
   242  		gins(optoas(gc.OMUL, t), &tr, &tm)
   243  		gc.Regfree(&tr)
   244  		gins(optoas(gc.OSUB, t), &tm, &tl)
   245  		gc.Regfree(&tm)
   246  		gmove(&tl, res)
   247  	}
   248  
   249  	gc.Regfree(&tl)
   250  	if check {
   251  		gc.Patch(p2, gc.Pc)
   252  	}
   253  }
   254  
   255  /*
   256   * generate high multiply:
   257   *   res = (nl*nr) >> width
   258   */
   259  func cgen_hmul(nl *gc.Node, nr *gc.Node, res *gc.Node) {
   260  	// largest ullman on left.
   261  	if nl.Ullman < nr.Ullman {
   262  		nl, nr = nr, nl
   263  	}
   264  
   265  	t := nl.Type
   266  	w := t.Width * 8
   267  	var n1 gc.Node
   268  	gc.Cgenr(nl, &n1, res)
   269  	var n2 gc.Node
   270  	gc.Cgenr(nr, &n2, nil)
   271  	switch gc.Simtype[t.Etype] {
   272  	case gc.TINT8,
   273  		gc.TINT16,
   274  		gc.TINT32:
   275  		gins(optoas(gc.OMUL, t), &n2, &n1)
   276  		p := gins(arm64.AASR, nil, &n1)
   277  		p.From.Type = obj.TYPE_CONST
   278  		p.From.Offset = w
   279  
   280  	case gc.TUINT8,
   281  		gc.TUINT16,
   282  		gc.TUINT32:
   283  		gins(optoas(gc.OMUL, t), &n2, &n1)
   284  		p := gins(arm64.ALSR, nil, &n1)
   285  		p.From.Type = obj.TYPE_CONST
   286  		p.From.Offset = w
   287  
   288  	case gc.TINT64,
   289  		gc.TUINT64:
   290  		if t.IsSigned() {
   291  			gins(arm64.ASMULH, &n2, &n1)
   292  		} else {
   293  			gins(arm64.AUMULH, &n2, &n1)
   294  		}
   295  
   296  	default:
   297  		gc.Fatalf("cgen_hmul %v", t)
   298  	}
   299  
   300  	gc.Cgen(&n1, res)
   301  	gc.Regfree(&n1)
   302  	gc.Regfree(&n2)
   303  }
   304  
   305  /*
   306   * generate shift according to op, one of:
   307   *	res = nl << nr
   308   *	res = nl >> nr
   309   */
   310  func cgen_shift(op gc.Op, bounded bool, nl *gc.Node, nr *gc.Node, res *gc.Node) {
   311  	a := optoas(op, nl.Type)
   312  
   313  	if nr.Op == gc.OLITERAL {
   314  		var n1 gc.Node
   315  		gc.Regalloc(&n1, nl.Type, res)
   316  		gc.Cgen(nl, &n1)
   317  		sc := uint64(nr.Int64())
   318  		if sc >= uint64(nl.Type.Width)*8 {
   319  			// large shift gets 2 shifts by width-1
   320  			var n3 gc.Node
   321  			gc.Nodconst(&n3, gc.Types[gc.TUINT32], nl.Type.Width*8-1)
   322  
   323  			gins(a, &n3, &n1)
   324  			gins(a, &n3, &n1)
   325  		} else {
   326  			gins(a, nr, &n1)
   327  		}
   328  		gmove(&n1, res)
   329  		gc.Regfree(&n1)
   330  		return
   331  	}
   332  
   333  	if nl.Ullman >= gc.UINF {
   334  		var n4 gc.Node
   335  		gc.Tempname(&n4, nl.Type)
   336  		gc.Cgen(nl, &n4)
   337  		nl = &n4
   338  	}
   339  
   340  	if nr.Ullman >= gc.UINF {
   341  		var n5 gc.Node
   342  		gc.Tempname(&n5, nr.Type)
   343  		gc.Cgen(nr, &n5)
   344  		nr = &n5
   345  	}
   346  
   347  	// Allow either uint32 or uint64 as shift type,
   348  	// to avoid unnecessary conversion from uint32 to uint64
   349  	// just to do the comparison.
   350  	tcount := gc.Types[gc.Simtype[nr.Type.Etype]]
   351  
   352  	if tcount.Etype < gc.TUINT32 {
   353  		tcount = gc.Types[gc.TUINT32]
   354  	}
   355  
   356  	var n1 gc.Node
   357  	gc.Regalloc(&n1, nr.Type, nil) // to hold the shift type in CX
   358  	var n3 gc.Node
   359  	gc.Regalloc(&n3, tcount, &n1) // to clear high bits of CX
   360  
   361  	var n2 gc.Node
   362  	gc.Regalloc(&n2, nl.Type, res)
   363  
   364  	if nl.Ullman >= nr.Ullman {
   365  		gc.Cgen(nl, &n2)
   366  		gc.Cgen(nr, &n1)
   367  		gmove(&n1, &n3)
   368  	} else {
   369  		gc.Cgen(nr, &n1)
   370  		gmove(&n1, &n3)
   371  		gc.Cgen(nl, &n2)
   372  	}
   373  
   374  	gc.Regfree(&n3)
   375  
   376  	// test and fix up large shifts
   377  	if !bounded {
   378  		gc.Nodconst(&n3, tcount, nl.Type.Width*8)
   379  		gcmp(optoas(gc.OCMP, tcount), &n1, &n3)
   380  		p1 := gc.Gbranch(optoas(gc.OLT, tcount), nil, +1)
   381  		if op == gc.ORSH && nl.Type.IsSigned() {
   382  			gc.Nodconst(&n3, gc.Types[gc.TUINT32], nl.Type.Width*8-1)
   383  			gins(a, &n3, &n2)
   384  		} else {
   385  			gc.Nodconst(&n3, nl.Type, 0)
   386  			gmove(&n3, &n2)
   387  		}
   388  
   389  		gc.Patch(p1, gc.Pc)
   390  	}
   391  
   392  	gins(a, &n1, &n2)
   393  
   394  	gmove(&n2, res)
   395  
   396  	gc.Regfree(&n1)
   397  	gc.Regfree(&n2)
   398  }
   399  
   400  func clearfat(nl *gc.Node) {
   401  	/* clear a fat object */
   402  	if gc.Debug['g'] != 0 {
   403  		fmt.Printf("clearfat %v (%v, size: %d)\n", nl, nl.Type, nl.Type.Width)
   404  	}
   405  
   406  	w := uint64(nl.Type.Width)
   407  
   408  	// Avoid taking the address for simple enough types.
   409  	if gc.Componentgen(nil, nl) {
   410  		return
   411  	}
   412  
   413  	c := w % 8 // bytes
   414  	q := w / 8 // dwords
   415  
   416  	var r0 gc.Node
   417  	gc.Nodreg(&r0, gc.Types[gc.TUINT64], arm64.REGZERO)
   418  	var dst gc.Node
   419  
   420  	// REGRT1 is reserved on arm64, see arm64/gsubr.go.
   421  	gc.Nodreg(&dst, gc.Types[gc.Tptr], arm64.REGRT1)
   422  	gc.Agen(nl, &dst)
   423  
   424  	var boff uint64
   425  	if q > 128 {
   426  		p := gins(arm64.ASUB, nil, &dst)
   427  		p.From.Type = obj.TYPE_CONST
   428  		p.From.Offset = 8
   429  
   430  		var end gc.Node
   431  		gc.Regalloc(&end, gc.Types[gc.Tptr], nil)
   432  		p = gins(arm64.AMOVD, &dst, &end)
   433  		p.From.Type = obj.TYPE_ADDR
   434  		p.From.Offset = int64(q * 8)
   435  
   436  		p = gins(arm64.AMOVD, &r0, &dst)
   437  		p.To.Type = obj.TYPE_MEM
   438  		p.To.Offset = 8
   439  		p.Scond = arm64.C_XPRE
   440  		pl := p
   441  
   442  		p = gcmp(arm64.ACMP, &dst, &end)
   443  		gc.Patch(gc.Gbranch(arm64.ABNE, nil, 0), pl)
   444  
   445  		gc.Regfree(&end)
   446  
   447  		// The loop leaves R16 on the last zeroed dword
   448  		boff = 8
   449  	} else if q >= 4 && !darwin { // darwin ld64 cannot handle BR26 reloc with non-zero addend
   450  		p := gins(arm64.ASUB, nil, &dst)
   451  		p.From.Type = obj.TYPE_CONST
   452  		p.From.Offset = 8
   453  		f := gc.Sysfunc("duffzero")
   454  		p = gins(obj.ADUFFZERO, nil, f)
   455  		gc.Afunclit(&p.To, f)
   456  
   457  		// 4 and 128 = magic constants: see ../../runtime/asm_arm64x.s
   458  		p.To.Offset = int64(4 * (128 - q))
   459  
   460  		// duffzero leaves R16 on the last zeroed dword
   461  		boff = 8
   462  	} else {
   463  		var p *obj.Prog
   464  		for t := uint64(0); t < q; t++ {
   465  			p = gins(arm64.AMOVD, &r0, &dst)
   466  			p.To.Type = obj.TYPE_MEM
   467  			p.To.Offset = int64(8 * t)
   468  		}
   469  
   470  		boff = 8 * q
   471  	}
   472  
   473  	var p *obj.Prog
   474  	for t := uint64(0); t < c; t++ {
   475  		p = gins(arm64.AMOVB, &r0, &dst)
   476  		p.To.Type = obj.TYPE_MEM
   477  		p.To.Offset = int64(t + boff)
   478  	}
   479  }
   480  
   481  // Called after regopt and peep have run.
   482  // Expand CHECKNIL pseudo-op into actual nil pointer check.
   483  func expandchecks(firstp *obj.Prog) {
   484  	var p1 *obj.Prog
   485  
   486  	for p := firstp; p != nil; p = p.Link {
   487  		if gc.Debug_checknil != 0 && gc.Ctxt.Debugvlog != 0 {
   488  			fmt.Printf("expandchecks: %v\n", p)
   489  		}
   490  		if p.As != obj.ACHECKNIL {
   491  			continue
   492  		}
   493  		if gc.Debug_checknil != 0 && p.Lineno > 1 { // p->lineno==1 in generated wrappers
   494  			gc.Warnl(p.Lineno, "generated nil check")
   495  		}
   496  		if p.From.Type != obj.TYPE_REG {
   497  			gc.Fatalf("invalid nil check %v\n", p)
   498  		}
   499  
   500  		// check is
   501  		//	CBNZ arg, 2(PC)
   502  		//	MOVD ZR, 0(arg)
   503  		p1 = gc.Ctxt.NewProg()
   504  		gc.Clearp(p1)
   505  		p1.Link = p.Link
   506  		p.Link = p1
   507  		p1.Lineno = p.Lineno
   508  		p1.Pc = 9999
   509  
   510  		p.As = arm64.ACBNZ
   511  		p.To.Type = obj.TYPE_BRANCH
   512  		p.To.Val = p1.Link
   513  
   514  		// crash by write to memory address 0.
   515  		p1.As = arm64.AMOVD
   516  		p1.From.Type = obj.TYPE_REG
   517  		p1.From.Reg = arm64.REGZERO
   518  		p1.To.Type = obj.TYPE_MEM
   519  		p1.To.Reg = p.From.Reg
   520  		p1.To.Offset = 0
   521  	}
   522  }
   523  
   524  // res = runtime.getg()
   525  func getg(res *gc.Node) {
   526  	var n1 gc.Node
   527  	gc.Nodreg(&n1, res.Type, arm64.REGG)
   528  	gmove(&n1, res)
   529  }