/* * n is on stack, either local variable * or return value from function call. * return n's offset from SP. */ func stkof(n *gc.Node) int64 { switch n.Op { case gc.OINDREG: return n.Xoffset case gc.ODOT: t := n.Left.Type if gc.Isptr[t.Etype] { break } off := stkof(n.Left) if off == -1000 || off == 1000 { return off } return off + n.Xoffset case gc.OINDEX: t := n.Left.Type if !gc.Isfixedarray(t) { break } off := stkof(n.Left) if off == -1000 || off == 1000 { return off } if gc.Isconst(n.Right, gc.CTINT) { return off + t.Type.Width*gc.Mpgetfix(n.Right.Val.U.Xval) } return 1000 case gc.OCALLMETH, gc.OCALLINTER, gc.OCALLFUNC: t := n.Left.Type if gc.Isptr[t.Etype] { t = t.Type } var flist gc.Iter t = gc.Structfirst(&flist, gc.Getoutarg(t)) if t != nil { return t.Width + int64(gc.Widthptr) // +widthptr: correct for saved LR } } // botch - probably failing to recognize address // arithmetic on the above. eg INDEX and DOT return -1000 }
/* * generate: * newreg = &n; * res = newreg * * on exit, a has been changed to be *newreg. * caller must regfree(a). * The generated code checks that the result is not *nil. */ func igen(n *gc.Node, a *gc.Node, res *gc.Node) { if gc.Debug['g'] != 0 { gc.Dump("\nigen-n", n) } switch n.Op { case gc.ONAME: if (n.Class&gc.PHEAP != 0) || n.Class == gc.PPARAMREF { break } *a = *n return // Increase the refcount of the register so that igen's caller // has to call regfree. case gc.OINDREG: if n.Val.U.Reg != ppc64.REGSP { reg[n.Val.U.Reg]++ } *a = *n return case gc.ODOT: igen(n.Left, a, res) a.Xoffset += n.Xoffset a.Type = n.Type fixlargeoffset(a) return case gc.ODOTPTR: cgenr(n.Left, a, res) gc.Cgen_checknil(a) a.Op = gc.OINDREG a.Xoffset += n.Xoffset a.Type = n.Type fixlargeoffset(a) return case gc.OCALLFUNC, gc.OCALLMETH, gc.OCALLINTER: switch n.Op { case gc.OCALLFUNC: cgen_call(n, 0) case gc.OCALLMETH: gc.Cgen_callmeth(n, 0) case gc.OCALLINTER: cgen_callinter(n, nil, 0) } var flist gc.Iter fp := gc.Structfirst(&flist, gc.Getoutarg(n.Left.Type)) *a = gc.Node{} a.Op = gc.OINDREG a.Val.U.Reg = ppc64.REGSP a.Addable = 1 a.Xoffset = fp.Width + int64(gc.Widthptr) // +widthptr: saved lr at 0(SP) a.Type = n.Type return // Index of fixed-size array by constant can // put the offset in the addressing. // Could do the same for slice except that we need // to use the real index for the bounds checking. case gc.OINDEX: if gc.Isfixedarray(n.Left.Type) || (gc.Isptr[n.Left.Type.Etype] && gc.Isfixedarray(n.Left.Left.Type)) { if gc.Isconst(n.Right, gc.CTINT) { // Compute &a. if !gc.Isptr[n.Left.Type.Etype] { igen(n.Left, a, res) } else { var n1 gc.Node igen(n.Left, &n1, res) gc.Cgen_checknil(&n1) regalloc(a, gc.Types[gc.Tptr], res) gmove(&n1, a) regfree(&n1) a.Op = gc.OINDREG } // Compute &a[i] as &a + i*width. a.Type = n.Type a.Xoffset += gc.Mpgetfix(n.Right.Val.U.Xval) * n.Type.Width fixlargeoffset(a) return } } } agenr(n, a, res) a.Op = gc.OINDREG a.Type = n.Type }
/* * generate: * newreg = &n; * res = newreg * * on exit, a has been changed to be *newreg. * caller must regfree(a). * The generated code checks that the result is not *nil. */ func igen(n *gc.Node, a *gc.Node, res *gc.Node) { if gc.Debug['g'] != 0 { gc.Dump("\nigen-n", n) } switch n.Op { case gc.ONAME: if (n.Class&gc.PHEAP != 0) || n.Class == gc.PPARAMREF { break } *a = *n return // Increase the refcount of the register so that igen's caller // has to call regfree. case gc.OINDREG: if n.Val.U.Reg != x86.REG_SP { reg[n.Val.U.Reg]++ } *a = *n return case gc.ODOT: igen(n.Left, a, res) a.Xoffset += n.Xoffset a.Type = n.Type return case gc.ODOTPTR: switch n.Left.Op { // igen-able nodes. case gc.ODOT, gc.ODOTPTR, gc.OCALLFUNC, gc.OCALLMETH, gc.OCALLINTER: var n1 gc.Node igen(n.Left, &n1, res) regalloc(a, gc.Types[gc.Tptr], &n1) gmove(&n1, a) regfree(&n1) default: regalloc(a, gc.Types[gc.Tptr], res) cgen(n.Left, a) } gc.Cgen_checknil(a) a.Op = gc.OINDREG a.Xoffset += n.Xoffset a.Type = n.Type return case gc.OCALLFUNC, gc.OCALLMETH, gc.OCALLINTER: switch n.Op { case gc.OCALLFUNC: cgen_call(n, 0) case gc.OCALLMETH: gc.Cgen_callmeth(n, 0) case gc.OCALLINTER: cgen_callinter(n, nil, 0) } var flist gc.Iter fp := gc.Structfirst(&flist, gc.Getoutarg(n.Left.Type)) *a = gc.Node{} a.Op = gc.OINDREG a.Val.U.Reg = x86.REG_SP a.Addable = 1 a.Xoffset = fp.Width a.Type = n.Type return // Index of fixed-size array by constant can // put the offset in the addressing. // Could do the same for slice except that we need // to use the real index for the bounds checking. case gc.OINDEX: if gc.Isfixedarray(n.Left.Type) || (gc.Isptr[n.Left.Type.Etype] && gc.Isfixedarray(n.Left.Left.Type)) { if gc.Isconst(n.Right, gc.CTINT) { // Compute &a. if !gc.Isptr[n.Left.Type.Etype] { igen(n.Left, a, res) } else { var n1 gc.Node igen(n.Left, &n1, res) gc.Cgen_checknil(&n1) regalloc(a, gc.Types[gc.Tptr], res) gmove(&n1, a) regfree(&n1) a.Op = gc.OINDREG } // Compute &a[i] as &a + i*width. a.Type = n.Type a.Xoffset += gc.Mpgetfix(n.Right.Val.U.Xval) * n.Type.Width return } } } // release register for now, to avoid // confusing tempname. if res != nil && res.Op == gc.OREGISTER { reg[res.Val.U.Reg]-- } var n1 gc.Node gc.Tempname(&n1, gc.Types[gc.Tptr]) agen(n, &n1) if res != nil && res.Op == gc.OREGISTER { reg[res.Val.U.Reg]++ } regalloc(a, gc.Types[gc.Tptr], res) gmove(&n1, a) a.Op = gc.OINDREG a.Type = n.Type }
/* * allocate a register (reusing res if possible) and generate * a = &n * The caller must call regfree(a). * The generated code checks that the result is not nil. */ func agenr(n *gc.Node, a *gc.Node, res *gc.Node) { if gc.Debug['g'] != 0 { gc.Dump("\nagenr-n", n) } nl := n.Left nr := n.Right switch n.Op { case gc.ODOT, gc.ODOTPTR, gc.OCALLFUNC, gc.OCALLMETH, gc.OCALLINTER: var n1 gc.Node igen(n, &n1, res) regalloc(a, gc.Types[gc.Tptr], &n1) agen(&n1, a) regfree(&n1) case gc.OIND: cgenr(n.Left, a, res) gc.Cgen_checknil(a) case gc.OINDEX: freelen := 0 w := uint64(n.Type.Width) // Generate the non-addressable child first. var n3 gc.Node var nlen gc.Node var tmp gc.Node var n1 gc.Node if nr.Addable != 0 { goto irad } if nl.Addable != 0 { cgenr(nr, &n1, nil) if !gc.Isconst(nl, gc.CTSTR) { if gc.Isfixedarray(nl.Type) { agenr(nl, &n3, res) } else { igen(nl, &nlen, res) freelen = 1 nlen.Type = gc.Types[gc.Tptr] nlen.Xoffset += int64(gc.Array_array) regalloc(&n3, gc.Types[gc.Tptr], res) gmove(&nlen, &n3) nlen.Type = gc.Types[gc.Simtype[gc.TUINT]] nlen.Xoffset += int64(gc.Array_nel) - int64(gc.Array_array) } } goto index } gc.Tempname(&tmp, nr.Type) cgen(nr, &tmp) nr = &tmp irad: if !gc.Isconst(nl, gc.CTSTR) { if gc.Isfixedarray(nl.Type) { agenr(nl, &n3, res) } else { if nl.Addable == 0 { // igen will need an addressable node. var tmp2 gc.Node gc.Tempname(&tmp2, nl.Type) cgen(nl, &tmp2) nl = &tmp2 } igen(nl, &nlen, res) freelen = 1 nlen.Type = gc.Types[gc.Tptr] nlen.Xoffset += int64(gc.Array_array) regalloc(&n3, gc.Types[gc.Tptr], res) gmove(&nlen, &n3) nlen.Type = gc.Types[gc.Simtype[gc.TUINT]] nlen.Xoffset += int64(gc.Array_nel) - int64(gc.Array_array) } } if !gc.Isconst(nr, gc.CTINT) { cgenr(nr, &n1, nil) } goto index // &a is in &n3 (allocated in res) // i is in &n1 (if not constant) // len(a) is in nlen (if needed) // w is width // constant index index: if gc.Isconst(nr, gc.CTINT) { if gc.Isconst(nl, gc.CTSTR) { gc.Fatal("constant string constant index") // front end should handle } v := uint64(gc.Mpgetfix(nr.Val.U.Xval)) if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { if gc.Debug['B'] == 0 && !n.Bounded { var n2 gc.Node gc.Nodconst(&n2, gc.Types[gc.Simtype[gc.TUINT]], int64(v)) if gc.Smallintconst(nr) { gins(optoas(gc.OCMP, gc.Types[gc.Simtype[gc.TUINT]]), &nlen, &n2) } else { regalloc(&tmp, gc.Types[gc.Simtype[gc.TUINT]], nil) gmove(&n2, &tmp) gins(optoas(gc.OCMP, gc.Types[gc.Simtype[gc.TUINT]]), &nlen, &tmp) regfree(&tmp) } p1 := gc.Gbranch(optoas(gc.OGT, gc.Types[gc.Simtype[gc.TUINT]]), nil, +1) ginscall(gc.Panicindex, -1) gc.Patch(p1, gc.Pc) } regfree(&nlen) } if v*w != 0 { ginscon(optoas(gc.OADD, gc.Types[gc.Tptr]), int64(v*w), &n3) } *a = n3 break } // type of the index t := gc.Types[gc.TUINT64] if gc.Issigned[n1.Type.Etype] { t = gc.Types[gc.TINT64] } var n2 gc.Node regalloc(&n2, t, &n1) // i gmove(&n1, &n2) regfree(&n1) if gc.Debug['B'] == 0 && !n.Bounded { // check bounds t = gc.Types[gc.Simtype[gc.TUINT]] if gc.Is64(nr.Type) { t = gc.Types[gc.TUINT64] } if gc.Isconst(nl, gc.CTSTR) { gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval))) } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { if gc.Is64(nr.Type) { var n5 gc.Node regalloc(&n5, t, nil) gmove(&nlen, &n5) regfree(&nlen) nlen = n5 } } else { gc.Nodconst(&nlen, t, nl.Type.Bound) if !gc.Smallintconst(&nlen) { var n5 gc.Node regalloc(&n5, t, nil) gmove(&nlen, &n5) nlen = n5 freelen = 1 } } gins(optoas(gc.OCMP, t), &n2, &nlen) p1 := gc.Gbranch(optoas(gc.OLT, t), nil, +1) ginscall(gc.Panicindex, -1) gc.Patch(p1, gc.Pc) } if gc.Isconst(nl, gc.CTSTR) { regalloc(&n3, gc.Types[gc.Tptr], res) p1 := gins(x86.ALEAQ, nil, &n3) gc.Datastring(nl.Val.U.Sval, &p1.From) gins(x86.AADDQ, &n2, &n3) goto indexdone } if w == 0 { } else // nothing to do if w == 1 || w == 2 || w == 4 || w == 8 { p1 := gins(x86.ALEAQ, &n2, &n3) p1.From.Type = obj.TYPE_MEM p1.From.Scale = int16(w) p1.From.Index = p1.From.Reg p1.From.Reg = p1.To.Reg } else { ginscon(optoas(gc.OMUL, t), int64(w), &n2) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n2, &n3) } indexdone: *a = n3 regfree(&n2) if freelen != 0 { regfree(&nlen) } default: regalloc(a, gc.Types[gc.Tptr], res) agen(n, a) } }
/* * address gen * res = &n; * The generated code checks that the result is not nil. */ func agen(n *gc.Node, res *gc.Node) { if gc.Debug['g'] != 0 { gc.Dump("\nagen-res", res) gc.Dump("agen-r", n) } if n == nil || n.Type == nil || res == nil || res.Type == nil { gc.Fatal("agen") } for n.Op == gc.OCONVNOP { n = n.Left } if gc.Isconst(n, gc.CTNIL) && n.Type.Width > int64(gc.Widthptr) { // Use of a nil interface or nil slice. // Create a temporary we can take the address of and read. // The generated code is just going to panic, so it need not // be terribly efficient. See issue 3670. var n1 gc.Node gc.Tempname(&n1, n.Type) gc.Gvardef(&n1) clearfat(&n1) var n2 gc.Node regalloc(&n2, gc.Types[gc.Tptr], res) gins(x86.ALEAL, &n1, &n2) gmove(&n2, res) regfree(&n2) return } // addressable var is easy if n.Addable != 0 { if n.Op == gc.OREGISTER { gc.Fatal("agen OREGISTER") } var n1 gc.Node regalloc(&n1, gc.Types[gc.Tptr], res) gins(x86.ALEAL, n, &n1) gmove(&n1, res) regfree(&n1) return } // let's compute nl := n.Left nr := n.Right switch n.Op { default: gc.Fatal("agen %v", gc.Oconv(int(n.Op), 0)) case gc.OCALLMETH: gc.Cgen_callmeth(n, 0) cgen_aret(n, res) case gc.OCALLINTER: cgen_callinter(n, res, 0) cgen_aret(n, res) case gc.OCALLFUNC: cgen_call(n, 0) cgen_aret(n, res) case gc.OSLICE, gc.OSLICEARR, gc.OSLICESTR, gc.OSLICE3, gc.OSLICE3ARR: var n1 gc.Node gc.Tempname(&n1, n.Type) gc.Cgen_slice(n, &n1) agen(&n1, res) case gc.OEFACE: var n1 gc.Node gc.Tempname(&n1, n.Type) gc.Cgen_eface(n, &n1) agen(&n1, res) case gc.OINDEX: var p2 *obj.Prog // to be patched to panicindex. w := uint32(n.Type.Width) bounded := gc.Debug['B'] != 0 || n.Bounded var n3 gc.Node var tmp gc.Node var n1 gc.Node if nr.Addable != 0 { // Generate &nl first, and move nr into register. if !gc.Isconst(nl, gc.CTSTR) { igen(nl, &n3, res) } if !gc.Isconst(nr, gc.CTINT) { p2 = igenindex(nr, &tmp, bool2int(bounded)) regalloc(&n1, tmp.Type, nil) gmove(&tmp, &n1) } } else if nl.Addable != 0 { // Generate nr first, and move &nl into register. if !gc.Isconst(nr, gc.CTINT) { p2 = igenindex(nr, &tmp, bool2int(bounded)) regalloc(&n1, tmp.Type, nil) gmove(&tmp, &n1) } if !gc.Isconst(nl, gc.CTSTR) { igen(nl, &n3, res) } } else { p2 = igenindex(nr, &tmp, bool2int(bounded)) nr = &tmp if !gc.Isconst(nl, gc.CTSTR) { igen(nl, &n3, res) } regalloc(&n1, tmp.Type, nil) gins(optoas(gc.OAS, tmp.Type), &tmp, &n1) } // For fixed array we really want the pointer in n3. var n2 gc.Node if gc.Isfixedarray(nl.Type) { regalloc(&n2, gc.Types[gc.Tptr], &n3) agen(&n3, &n2) regfree(&n3) n3 = n2 } // &a[0] is in n3 (allocated in res) // i is in n1 (if not constant) // len(a) is in nlen (if needed) // w is width // constant index if gc.Isconst(nr, gc.CTINT) { if gc.Isconst(nl, gc.CTSTR) { gc.Fatal("constant string constant index") // front end should handle } v := uint64(gc.Mpgetfix(nr.Val.U.Xval)) if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { if gc.Debug['B'] == 0 && !n.Bounded { nlen := n3 nlen.Type = gc.Types[gc.TUINT32] nlen.Xoffset += int64(gc.Array_nel) gc.Nodconst(&n2, gc.Types[gc.TUINT32], int64(v)) gins(optoas(gc.OCMP, gc.Types[gc.TUINT32]), &nlen, &n2) p1 := gc.Gbranch(optoas(gc.OGT, gc.Types[gc.TUINT32]), nil, +1) ginscall(gc.Panicindex, -1) gc.Patch(p1, gc.Pc) } } // Load base pointer in n2 = n3. regalloc(&n2, gc.Types[gc.Tptr], &n3) n3.Type = gc.Types[gc.Tptr] n3.Xoffset += int64(gc.Array_array) gmove(&n3, &n2) regfree(&n3) if v*uint64(w) != 0 { gc.Nodconst(&n1, gc.Types[gc.Tptr], int64(v*uint64(w))) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n1, &n2) } gmove(&n2, res) regfree(&n2) break } // i is in register n1, extend to 32 bits. t := gc.Types[gc.TUINT32] if gc.Issigned[n1.Type.Etype] { t = gc.Types[gc.TINT32] } regalloc(&n2, t, &n1) // i gmove(&n1, &n2) regfree(&n1) if gc.Debug['B'] == 0 && !n.Bounded { // check bounds t := gc.Types[gc.TUINT32] var nlen gc.Node if gc.Isconst(nl, gc.CTSTR) { gc.Nodconst(&nlen, t, int64(len(nl.Val.U.Sval))) } else if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { nlen = n3 nlen.Type = t nlen.Xoffset += int64(gc.Array_nel) } else { gc.Nodconst(&nlen, t, nl.Type.Bound) } gins(optoas(gc.OCMP, t), &n2, &nlen) p1 := gc.Gbranch(optoas(gc.OLT, t), nil, +1) if p2 != nil { gc.Patch(p2, gc.Pc) } ginscall(gc.Panicindex, -1) gc.Patch(p1, gc.Pc) } if gc.Isconst(nl, gc.CTSTR) { regalloc(&n3, gc.Types[gc.Tptr], res) p1 := gins(x86.ALEAL, nil, &n3) gc.Datastring(nl.Val.U.Sval, &p1.From) p1.From.Scale = 1 p1.From.Index = n2.Val.U.Reg goto indexdone } // Load base pointer in n3. regalloc(&tmp, gc.Types[gc.Tptr], &n3) if gc.Isslice(nl.Type) || nl.Type.Etype == gc.TSTRING { n3.Type = gc.Types[gc.Tptr] n3.Xoffset += int64(gc.Array_array) gmove(&n3, &tmp) } regfree(&n3) n3 = tmp if w == 0 { } else // nothing to do if w == 1 || w == 2 || w == 4 || w == 8 { // LEAL (n3)(n2*w), n3 p1 := gins(x86.ALEAL, &n2, &n3) p1.From.Scale = int16(w) p1.From.Type = obj.TYPE_MEM p1.From.Index = p1.From.Reg p1.From.Reg = p1.To.Reg } else { gc.Nodconst(&tmp, gc.Types[gc.TUINT32], int64(w)) gins(optoas(gc.OMUL, gc.Types[gc.TUINT32]), &tmp, &n2) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n2, &n3) } indexdone: gmove(&n3, res) regfree(&n2) regfree(&n3) // should only get here with names in this func. case gc.ONAME: if n.Funcdepth > 0 && n.Funcdepth != gc.Funcdepth { gc.Dump("bad agen", n) gc.Fatal("agen: bad ONAME funcdepth %d != %d", n.Funcdepth, gc.Funcdepth) } // should only get here for heap vars or paramref if n.Class&gc.PHEAP == 0 && n.Class != gc.PPARAMREF { gc.Dump("bad agen", n) gc.Fatal("agen: bad ONAME class %#x", n.Class) } cgen(n.Heapaddr, res) if n.Xoffset != 0 { var n1 gc.Node gc.Nodconst(&n1, gc.Types[gc.Tptr], n.Xoffset) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n1, res) } case gc.OIND: cgen(nl, res) gc.Cgen_checknil(res) case gc.ODOT: agen(nl, res) if n.Xoffset != 0 { var n1 gc.Node gc.Nodconst(&n1, gc.Types[gc.Tptr], n.Xoffset) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n1, res) } case gc.ODOTPTR: t := nl.Type if !gc.Isptr[t.Etype] { gc.Fatal("agen: not ptr %v", gc.Nconv(n, 0)) } cgen(nl, res) gc.Cgen_checknil(res) if n.Xoffset != 0 { var n1 gc.Node gc.Nodconst(&n1, gc.Types[gc.Tptr], n.Xoffset) gins(optoas(gc.OADD, gc.Types[gc.Tptr]), &n1, res) } } }