Skip to content

Commit 006bc57

Browse files
committed
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í <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 7092a31 commit 006bc57

File tree

9 files changed

+52
-78
lines changed

9 files changed

+52
-78
lines changed

src/cmd/compile/internal/gc/const.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,25 +1373,22 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node {
13731373
return convlit(n, t)
13741374
}
13751375

1376-
if n.Val().Ctype() == CTNIL {
1376+
switch n.Val().Ctype() {
1377+
case CTNIL:
13771378
lineno = lno
13781379
if !n.Diag() {
13791380
yyerror("use of untyped nil")
13801381
n.SetDiag(true)
13811382
}
13821383

13831384
n.Type = nil
1384-
break
1385-
}
1386-
1387-
if n.Val().Ctype() == CTSTR {
1385+
case CTSTR:
13881386
t1 := types.Types[TSTRING]
13891387
n = convlit1(n, t1, false, reuse)
1390-
break
1388+
default:
1389+
yyerror("defaultlit: unknown literal: %v", n)
13911390
}
13921391

1393-
yyerror("defaultlit: unknown literal: %v", n)
1394-
13951392
case CTxxx:
13961393
Fatalf("defaultlit: idealkind is CTxxx: %+v", n)
13971394

src/cmd/compile/internal/gc/esc.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ const (
205205
// allowed level when a loop is encountered. Using -2 suffices to
206206
// pass all the tests we have written so far, which we assume matches
207207
// the level of complexity we want the escape analysis code to handle.
208-
const (
209-
MinLevel = -2
210-
)
208+
const MinLevel = -2
211209

212210
// A Level encodes the reference state and context applied to
213211
// (stack, heap) allocated memory.

src/cmd/compile/internal/gc/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func autoexport(n *Node, ctxt Class) {
8383
if (ctxt != PEXTERN && ctxt != PFUNC) || dclcontext != PEXTERN {
8484
return
8585
}
86-
if n.Type != nil && n.Type.IsKind(TFUNC) && n.Type.Recv() != nil { // method
86+
if n.Type != nil && n.Type.IsKind(TFUNC) && n.IsMethod() {
8787
return
8888
}
8989

src/cmd/compile/internal/gc/gsubr.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ func newProgs(fn *Node, worker int) *Progs {
7070
}
7171

7272
func (pp *Progs) NewProg() *obj.Prog {
73+
var p *obj.Prog
7374
if pp.cacheidx < len(pp.progcache) {
74-
p := &pp.progcache[pp.cacheidx]
75-
p.Ctxt = Ctxt
75+
p = &pp.progcache[pp.cacheidx]
7676
pp.cacheidx++
77-
return p
77+
} else {
78+
p = new(obj.Prog)
7879
}
79-
p := new(obj.Prog)
8080
p.Ctxt = Ctxt
8181
return p
8282
}

src/cmd/compile/internal/gc/obj.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import (
1616
)
1717

1818
// architecture-independent object file output
19-
const (
20-
ArhdrSize = 60
21-
)
19+
const ArhdrSize = 60
2220

2321
func formathdr(arhdr []byte, name string, size int64) {
2422
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) {
6260
fmt.Printf("can't create %s: %v\n", outfile, err)
6361
errorexit()
6462
}
63+
defer bout.Close()
6564

6665
startobj := int64(0)
6766
var arhdr [ArhdrSize]byte
@@ -108,7 +107,6 @@ func dumpobj1(outfile string, mode int) {
108107
}
109108

110109
if mode&modeLinkerObj == 0 {
111-
bout.Close()
112110
return
113111
}
114112

@@ -170,8 +168,6 @@ func dumpobj1(outfile string, mode int) {
170168
formathdr(arhdr[:], "_go_.o", size)
171169
bout.Write(arhdr[:])
172170
}
173-
174-
bout.Close()
175171
}
176172

177173
func addptabs() {

src/cmd/compile/internal/gc/select.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ func walkselect(sel *Node) {
156156
a.Nbody.Set1(mkcall("block", nil, &ln))
157157
l = ln.Slice()
158158
a = typecheck(a, Etop)
159-
l = append(l, a)
160-
l = append(l, n)
159+
l = append(l, a, n)
161160
}
162161

163162
l = append(l, cas.Nbody.Slice()...)

src/cmd/compile/internal/gc/ssa.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5119,10 +5119,11 @@ func (e *ssafn) DerefItab(it *obj.LSym, offset int64) *obj.LSym {
51195119
func (e *ssafn) splitSlot(parent *ssa.LocalSlot, suffix string, offset int64, t *types.Type) ssa.LocalSlot {
51205120
s := &types.Sym{Name: parent.N.(*Node).Sym.Name + suffix, Pkg: localpkg}
51215121

5122-
n := new(Node)
5123-
n.Name = new(Name)
5124-
n.Op = ONAME
5125-
n.Pos = parent.N.(*Node).Pos
5122+
n := &Node{
5123+
Name: new(Name),
5124+
Op: ONAME,
5125+
Pos: parent.N.(*Node).Pos,
5126+
}
51265127
n.Orig = n
51275128

51285129
s.Def = asTypesNode(n)

src/cmd/compile/internal/gc/subr.go

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,48 +1130,42 @@ func updateHasCall(n *Node) {
11301130
if n == nil {
11311131
return
11321132
}
1133+
n.SetHasCall(calcHasCall(n))
1134+
}
11331135

1134-
b := false
1136+
func calcHasCall(n *Node) bool {
11351137
if n.Ninit.Len() != 0 {
11361138
// TODO(mdempsky): This seems overly conservative.
1137-
b = true
1138-
goto out
1139+
return true
11391140
}
11401141

11411142
switch n.Op {
11421143
case OLITERAL, ONAME, OTYPE:
1143-
if b || n.HasCall() {
1144+
if n.HasCall() {
11441145
Fatalf("OLITERAL/ONAME/OTYPE should never have calls: %+v", n)
11451146
}
1146-
return
1147+
return false
11471148
case OCALL, OCALLFUNC, OCALLMETH, OCALLINTER:
1148-
b = true
1149-
goto out
1149+
return true
11501150
case OANDAND, OOROR:
11511151
// hard with instrumented code
11521152
if instrumenting {
1153-
b = true
1154-
goto out
1153+
return true
11551154
}
11561155
case OINDEX, OSLICE, OSLICEARR, OSLICE3, OSLICE3ARR, OSLICESTR,
11571156
OIND, ODOTPTR, ODOTTYPE, ODIV, OMOD:
11581157
// These ops might panic, make sure they are done
11591158
// before we start marshaling args for a call. See issue 16760.
1160-
b = true
1161-
goto out
1159+
return true
11621160
}
11631161

11641162
if n.Left != nil && n.Left.HasCall() {
1165-
b = true
1166-
goto out
1163+
return true
11671164
}
11681165
if n.Right != nil && n.Right.HasCall() {
1169-
b = true
1170-
goto out
1166+
return true
11711167
}
1172-
1173-
out:
1174-
n.SetHasCall(b)
1168+
return false
11751169
}
11761170

11771171
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
13831377
return
13841378
}
13851379
t.SetRecur(true)
1380+
defer t.SetRecur(false)
13861381

13871382
var u *types.Type
13881383
d--
@@ -1392,7 +1387,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
13921387
// below for embedded fields.
13931388
c = lookdot0(s, t, save, ignorecase)
13941389
if c != 0 {
1395-
goto out
1390+
return c, false
13961391
}
13971392
}
13981393

@@ -1401,7 +1396,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
14011396
u = u.Elem()
14021397
}
14031398
if !u.IsStruct() && !u.IsInterface() {
1404-
goto out
1399+
return c, false
14051400
}
14061401

14071402
for _, f := range u.Fields().Slice() {
@@ -1410,8 +1405,7 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
14101405
}
14111406
if d < 0 {
14121407
// Found an embedded field at target depth.
1413-
more = true
1414-
goto out
1408+
return c, true
14151409
}
14161410
a, more1 := adddot1(s, f.Type, d, save, ignorecase)
14171411
if a != 0 && c == 0 {
@@ -1423,8 +1417,6 @@ func adddot1(s *types.Sym, t *types.Type, d int, save **types.Field, ignorecase
14231417
}
14241418
}
14251419

1426-
out:
1427-
t.SetRecur(false)
14281420
return c, more
14291421
}
14301422

@@ -1553,21 +1545,18 @@ func expand1(t *types.Type, top, followptr bool) {
15531545
u = u.Elem()
15541546
}
15551547

1556-
if !u.IsStruct() && !u.IsInterface() {
1557-
goto out
1558-
}
1559-
1560-
for _, f := range u.Fields().Slice() {
1561-
if f.Embedded == 0 {
1562-
continue
1563-
}
1564-
if f.Sym == nil {
1565-
continue
1548+
if u.IsStruct() || u.IsInterface() {
1549+
for _, f := range u.Fields().Slice() {
1550+
if f.Embedded == 0 {
1551+
continue
1552+
}
1553+
if f.Sym == nil {
1554+
continue
1555+
}
1556+
expand1(f.Type, false, followptr)
15661557
}
1567-
expand1(f.Type, false, followptr)
15681558
}
15691559

1570-
out:
15711560
t.SetRecur(false)
15721561
}
15731562

src/cmd/compile/internal/gc/walk.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import (
1313
)
1414

1515
// The constant is known to runtime.
16-
const (
17-
tmpstringbufsize = 32
18-
)
16+
const tmpstringbufsize = 32
1917

2018
func walk(fn *Node) {
2119
Curfn = fn
@@ -2247,24 +2245,23 @@ func convas(n *Node, init *Nodes) *Node {
22472245
if n.Op != OAS {
22482246
Fatalf("convas: not OAS %v", n.Op)
22492247
}
2248+
defer updateHasCall(n)
22502249

22512250
n.SetTypecheck(1)
22522251

2253-
var lt *types.Type
2254-
var rt *types.Type
22552252
if n.Left == nil || n.Right == nil {
2256-
goto out
2253+
return n
22572254
}
22582255

2259-
lt = n.Left.Type
2260-
rt = n.Right.Type
2256+
lt := n.Left.Type
2257+
rt := n.Right.Type
22612258
if lt == nil || rt == nil {
2262-
goto out
2259+
return n
22632260
}
22642261

22652262
if isblank(n.Left) {
22662263
n.Right = defaultlit(n.Right, nil)
2267-
goto out
2264+
return n
22682265
}
22692266

22702267
if !eqtype(lt, rt) {
@@ -2273,8 +2270,6 @@ func convas(n *Node, init *Nodes) *Node {
22732270
}
22742271
dowidth(n.Right.Type)
22752272

2276-
out:
2277-
updateHasCall(n)
22782273
return n
22792274
}
22802275

@@ -2429,9 +2424,8 @@ func outervalue(n *Node) *Node {
24292424
}
24302425
}
24312426

2432-
break
2427+
return n
24332428
}
2434-
return n
24352429
}
24362430

24372431
// Is it possible that the computation of n might be

0 commit comments

Comments
 (0)