Commit 006bc570 authored by Daniel Martí's avatar Daniel Martí

cmd/compile: clean up various bits of code

* replace a copy of IsMethod with a call of it.
* a few more switches where they simplify the code.
* prefer composite literals over "n := new(...); n.x = y; ...".
* use defers to get rid of three goto labels.
* rewrite updateHasCall into two funcs to remove gotos.

Passes toolstash-check on std cmd.

Change-Id: Icb5442a89a87319ef4b640bbc5faebf41b193ef1
Reviewed-on: https://go-review.googlesource.com/72070
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent 7092a312
...@@ -1373,7 +1373,8 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node { ...@@ -1373,7 +1373,8 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node {
return convlit(n, t) return convlit(n, t)
} }
if n.Val().Ctype() == CTNIL { switch n.Val().Ctype() {
case CTNIL:
lineno = lno lineno = lno
if !n.Diag() { if !n.Diag() {
yyerror("use of untyped nil") yyerror("use of untyped nil")
...@@ -1381,16 +1382,12 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node { ...@@ -1381,16 +1382,12 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node {
} }
n.Type = nil n.Type = nil
break case CTSTR:
}
if n.Val().Ctype() == CTSTR {
t1 := types.Types[TSTRING] t1 := types.Types[TSTRING]
n = convlit1(n, t1, false, reuse) n = convlit1(n, t1, false, reuse)
break default:
}
yyerror("defaultlit: unknown literal: %v", n) yyerror("defaultlit: unknown literal: %v", n)
}
case CTxxx: case CTxxx:
Fatalf("defaultlit: idealkind is CTxxx: %+v", n) Fatalf("defaultlit: idealkind is CTxxx: %+v", n)
......
...@@ -205,9 +205,7 @@ const ( ...@@ -205,9 +205,7 @@ const (
// allowed level when a loop is encountered. Using -2 suffices to // allowed level when a loop is encountered. Using -2 suffices to
// pass all the tests we have written so far, which we assume matches // pass all the tests we have written so far, which we assume matches
// the level of complexity we want the escape analysis code to handle. // the level of complexity we want the escape analysis code to handle.
const ( const MinLevel = -2
MinLevel = -2
)
// A Level encodes the reference state and context applied to // A Level encodes the reference state and context applied to
// (stack, heap) allocated memory. // (stack, heap) allocated memory.
......
...@@ -83,7 +83,7 @@ func autoexport(n *Node, ctxt Class) { ...@@ -83,7 +83,7 @@ func autoexport(n *Node, ctxt Class) {
if (ctxt != PEXTERN && ctxt != PFUNC) || dclcontext != PEXTERN { if (ctxt != PEXTERN && ctxt != PFUNC) || dclcontext != PEXTERN {
return return
} }
if n.Type != nil && n.Type.IsKind(TFUNC) && n.Type.Recv() != nil { // method if n.Type != nil && n.Type.IsKind(TFUNC) && n.IsMethod() {
return return
} }
......
...@@ -70,13 +70,13 @@ func newProgs(fn *Node, worker int) *Progs { ...@@ -70,13 +70,13 @@ func newProgs(fn *Node, worker int) *Progs {
} }
func (pp *Progs) NewProg() *obj.Prog { func (pp *Progs) NewProg() *obj.Prog {
var p *obj.Prog
if pp.cacheidx < len(pp.progcache) { if pp.cacheidx < len(pp.progcache) {
p := &pp.progcache[pp.cacheidx] p = &pp.progcache[pp.cacheidx]
p.Ctxt = Ctxt
pp.cacheidx++ pp.cacheidx++
return p } else {
p = new(obj.Prog)
} }
p := new(obj.Prog)
p.Ctxt = Ctxt p.Ctxt = Ctxt
return p return p
} }
......
...@@ -16,9 +16,7 @@ import ( ...@@ -16,9 +16,7 @@ import (
) )
// architecture-independent object file output // architecture-independent object file output
const ( const ArhdrSize = 60
ArhdrSize = 60
)
func formathdr(arhdr []byte, name string, size int64) { func formathdr(arhdr []byte, name string, size int64) {
copy(arhdr[:], fmt.Sprintf("%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 0, 0, 0, 0644, size)) copy(arhdr[:], fmt.Sprintf("%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 0, 0, 0, 0644, size))
...@@ -62,6 +60,7 @@ func dumpobj1(outfile string, mode int) { ...@@ -62,6 +60,7 @@ func dumpobj1(outfile string, mode int) {
fmt.Printf("can't create %s: %v\n", outfile, err) fmt.Printf("can't create %s: %v\n", outfile, err)
errorexit() errorexit()
} }
defer bout.Close()
startobj := int64(0) startobj := int64(0)
var arhdr [ArhdrSize]byte var arhdr [ArhdrSize]byte
...@@ -108,7 +107,6 @@ func dumpobj1(outfile string, mode int) { ...@@ -108,7 +107,6 @@ func dumpobj1(outfile string, mode int) {
} }
if mode&modeLinkerObj == 0 { if mode&modeLinkerObj == 0 {
bout.Close()
return return
} }
...@@ -170,8 +168,6 @@ func dumpobj1(outfile string, mode int) { ...@@ -170,8 +168,6 @@ func dumpobj1(outfile string, mode int) {
formathdr(arhdr[:], "_go_.o", size) formathdr(arhdr[:], "_go_.o", size)
bout.Write(arhdr[:]) bout.Write(arhdr[:])
} }
bout.Close()
} }
func addptabs() { func addptabs() {
......
...@@ -156,8 +156,7 @@ func walkselect(sel *Node) { ...@@ -156,8 +156,7 @@ func walkselect(sel *Node) {
a.Nbody.Set1(mkcall("block", nil, &ln)) a.Nbody.Set1(mkcall("block", nil, &ln))
l = ln.Slice() l = ln.Slice()
a = typecheck(a, Etop) a = typecheck(a, Etop)
l = append(l, a) l = append(l, a, n)
l = append(l, n)
} }
l = append(l, cas.Nbody.Slice()...) l = append(l, cas.Nbody.Slice()...)
......
...@@ -5119,10 +5119,11 @@ func (e *ssafn) DerefItab(it *obj.LSym, offset int64) *obj.LSym { ...@@ -5119,10 +5119,11 @@ func (e *ssafn) DerefItab(it *obj.LSym, offset int64) *obj.LSym {
func (e *ssafn) splitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot { func (e *ssafn) splitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot {
s := &types.Sym{Name: parent.N.(*Node).Sym.Name + suffix, Pkg: localpkg} s := &types.Sym{Name: parent.N.(*Node).Sym.Name + suffix, Pkg: localpkg}
n := new(Node) n := &Node{
n.Name = new(Name) Name: new(Name),
n.Op = ONAME Op: ONAME,
n.Pos = parent.N.(*Node).Pos Pos: parent.N.(*Node).Pos,
}
n.Orig = n n.Orig = n
s.Def = asTypesNode(n) s.Def = asTypesNode(n)
......
...@@ -1130,48 +1130,42 @@ func updateHasCall(n *Node) { ...@@ -1130,48 +1130,42 @@ func updateHasCall(n *Node) {
if n == nil { if n == nil {
return return
} }
n.SetHasCall(calcHasCall(n))
}
b := false func calcHasCall(n *Node) bool {
if n.Ninit.Len() != 0 { if n.Ninit.Len() != 0 {
// TODO(mdempsky): This seems overly conservative. // TODO(mdempsky): This seems overly conservative.
b = true return true
goto out
} }
switch n.Op { switch n.Op {
case OLITERAL, ONAME, OTYPE: case OLITERAL, ONAME, OTYPE:
if b || n.HasCall() { if n.HasCall() {
Fatalf("OLITERAL/ONAME/OTYPE should never have calls: %+v", n) Fatalf("OLITERAL/ONAME/OTYPE should never have calls: %+v", n)
} }
return return false
case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER: case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER:
b = true return true
goto out
case OANDAND, OOROR: case OANDAND, OOROR:
// hard with instrumented code // hard with instrumented code
if instrumenting { if instrumenting {
b = true return true
goto out
} }
case OINDEX, OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR, OSLICESTR, case OINDEX, OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR, OSLICESTR,
OIND, ODOTPTR, ODOTTYPE, ODIV, OMOD: OIND, ODOTPTR, ODOTTYPE, ODIV, OMOD:
// These ops might panic, make sure they are done // These ops might panic, make sure they are done
// before we start marshaling args for a call. See issue 16760. // before we start marshaling args for a call. See issue 16760.
b = true return true
goto out
} }
if n.Left != nil && n.Left.HasCall() { if n.Left != nil && n.Left.HasCall() {
b = true return true
goto out
} }
if n.Right != nil && n.Right.HasCall() { if n.Right != nil && n.Right.HasCall() {
b = true return true
goto out
} }
return false
out:
n.SetHasCall(b)
} }
func badtype(op Op, tl *types.Type, tr *types.Type) { func badtype(op Op, tl *types.Type, tr *types.Type) {
...@@ -1383,6 +1377,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase ...@@ -1383,6 +1377,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
return return
} }
t.SetRecur(true) t.SetRecur(true)
defer t.SetRecur(false)
var u *types.Type var u *types.Type
d-- d--
...@@ -1392,7 +1387,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase ...@@ -1392,7 +1387,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
// below for embedded fields. // below for embedded fields.
c = lookdot0(s, t, save, ignorecase) c = lookdot0(s, t, save, ignorecase)
if c != 0 { if c != 0 {
goto out return c, false
} }
} }
...@@ -1401,7 +1396,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase ...@@ -1401,7 +1396,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
u = u.Elem() u = u.Elem()
} }
if !u.IsStruct() && !u.IsInterface() { if !u.IsStruct() && !u.IsInterface() {
goto out return c, false
} }
for _, f := range u.Fields().Slice() { for _, f := range u.Fields().Slice() {
...@@ -1410,8 +1405,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase ...@@ -1410,8 +1405,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
} }
if d < 0 { if d < 0 {
// Found an embedded field at target depth. // Found an embedded field at target depth.
more = true return c, true
goto out
} }
a, more1 := adddot1(s, f.Type, d, save, ignorecase) a, more1 := adddot1(s, f.Type, d, save, ignorecase)
if a != 0 && c == 0 { if a != 0 && c == 0 {
...@@ -1423,8 +1417,6 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase ...@@ -1423,8 +1417,6 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
} }
} }
out:
t.SetRecur(false)
return c, more return c, more
} }
...@@ -1553,10 +1545,7 @@ func expand1(t *types.Type, top, followptr bool) { ...@@ -1553,10 +1545,7 @@ func expand1(t *types.Type, top, followptr bool) {
u = u.Elem() u = u.Elem()
} }
if !u.IsStruct() && !u.IsInterface() { if u.IsStruct() || u.IsInterface() {
goto out
}
for _, f := range u.Fields().Slice() { for _, f := range u.Fields().Slice() {
if f.Embedded == 0 { if f.Embedded == 0 {
continue continue
...@@ -1566,8 +1555,8 @@ func expand1(t *types.Type, top, followptr bool) { ...@@ -1566,8 +1555,8 @@ func expand1(t *types.Type, top, followptr bool) {
} }
expand1(f.Type, false, followptr) expand1(f.Type, false, followptr)
} }
}
out:
t.SetRecur(false) t.SetRecur(false)
} }
......
...@@ -13,9 +13,7 @@ import ( ...@@ -13,9 +13,7 @@ import (
) )
// The constant is known to runtime. // The constant is known to runtime.
const ( const tmpstringbufsize = 32
tmpstringbufsize = 32
)
func walk(fn *Node) { func walk(fn *Node) {
Curfn = fn Curfn = fn
...@@ -2247,24 +2245,23 @@ func convas(n *Node, init *Nodes) *Node { ...@@ -2247,24 +2245,23 @@ func convas(n *Node, init *Nodes) *Node {
if n.Op != OAS { if n.Op != OAS {
Fatalf("convas: not OAS %v", n.Op) Fatalf("convas: not OAS %v", n.Op)
} }
defer updateHasCall(n)
n.SetTypecheck(1) n.SetTypecheck(1)
var lt *types.Type
var rt *types.Type
if n.Left == nil || n.Right == nil { if n.Left == nil || n.Right == nil {
goto out return n
} }
lt = n.Left.Type lt := n.Left.Type
rt = n.Right.Type rt := n.Right.Type
if lt == nil || rt == nil { if lt == nil || rt == nil {
goto out return n
} }
if isblank(n.Left) { if isblank(n.Left) {
n.Right = defaultlit(n.Right, nil) n.Right = defaultlit(n.Right, nil)
goto out return n
} }
if !eqtype(lt, rt) { if !eqtype(lt, rt) {
...@@ -2273,8 +2270,6 @@ func convas(n *Node, init *Nodes) *Node { ...@@ -2273,8 +2270,6 @@ func convas(n *Node, init *Nodes) *Node {
} }
dowidth(n.Right.Type) dowidth(n.Right.Type)
out:
updateHasCall(n)
return n return n
} }
...@@ -2429,9 +2424,8 @@ func outervalue(n *Node) *Node { ...@@ -2429,9 +2424,8 @@ func outervalue(n *Node) *Node {
} }
} }
break
}
return n return n
}
} }
// Is it possible that the computation of n might be // Is it possible that the computation of n might be
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment