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

185 lines
3.6 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
// case OADD:
// if(n->right->op == OLITERAL) {
// v = n->right->vconst;
// naddr(n->left, a, canemitcode);
// } else
// if(n->left->op == OLITERAL) {
// v = n->left->vconst;
// naddr(n->right, a, canemitcode);
// } else
// goto bad;
// a->offset += v;
// break;
// a function named init is a special case.
// it is called by the initialization before
// main is run. to make it unique within a
// package and also uncallable, the name,
// normally "pkg.init", is altered to "pkg.init.1".
var renameinit_initgen int
func renameinit() *Sym {
renameinit_initgen++
return Lookupf("init.%d", renameinit_initgen)
}
// hand-craft the following initialization code
// var initdoneĀ· uint8 (1)
// func init() (2)
// if initdoneĀ· > 1 { (3)
// return (3a)
// if initdoneĀ· == 1 { (4)
// throw(); (4a)
// }
// initdoneĀ· = 1; (6)
// // over all matching imported symbols
// <pkg>.init() (7)
// { <init stmts> } (8)
// init.<n>() // if any (9)
// initdoneĀ· = 2; (10)
// return (11)
// }
func anyinit(n []*Node) bool {
// are there any interesting init statements
for _, ln := range n {
switch ln.Op {
case ODCLFUNC, ODCLCONST, ODCLTYPE, OEMPTY:
break
cmd/internal/gc: emit write barriers at lower level This is primarily preparation for inlining, not an optimization by itself, but it still helps some. name old new delta BenchmarkBinaryTree17 18.2s Ɨ (0.99,1.01) 17.9s Ɨ (0.99,1.01) -1.57% BenchmarkFannkuch11 4.44s Ɨ (1.00,1.00) 4.42s Ɨ (1.00,1.00) -0.40% BenchmarkFmtFprintfEmpty 119ns Ɨ (0.95,1.02) 118ns Ɨ (0.96,1.02) ~ BenchmarkFmtFprintfString 501ns Ɨ (0.99,1.02) 486ns Ɨ (0.99,1.01) -2.89% BenchmarkFmtFprintfInt 474ns Ɨ (0.99,1.00) 457ns Ɨ (0.99,1.01) -3.59% BenchmarkFmtFprintfIntInt 792ns Ɨ (1.00,1.00) 768ns Ɨ (1.00,1.01) -3.03% BenchmarkFmtFprintfPrefixedInt 574ns Ɨ (1.00,1.01) 584ns Ɨ (0.99,1.03) +1.83% BenchmarkFmtFprintfFloat 749ns Ɨ (1.00,1.00) 739ns Ɨ (0.99,1.00) -1.34% BenchmarkFmtManyArgs 2.94µs Ɨ (1.00,1.01) 2.77µs Ɨ (1.00,1.00) -5.76% BenchmarkGobDecode 39.5ms Ɨ (0.99,1.01) 39.3ms Ɨ (0.99,1.01) ~ BenchmarkGobEncode 39.4ms Ɨ (1.00,1.01) 39.4ms Ɨ (0.99,1.00) ~ BenchmarkGzip 658ms Ɨ (1.00,1.01) 661ms Ɨ (0.99,1.01) ~ BenchmarkGunzip 142ms Ɨ (1.00,1.00) 142ms Ɨ (1.00,1.00) +0.22% BenchmarkHTTPClientServer 134µs Ɨ (0.99,1.01) 133µs Ɨ (0.98,1.01) ~ BenchmarkJSONEncode 57.1ms Ɨ (0.99,1.01) 56.5ms Ɨ (0.99,1.01) ~ BenchmarkJSONDecode 141ms Ɨ (1.00,1.00) 143ms Ɨ (1.00,1.00) +1.09% BenchmarkMandelbrot200 6.01ms Ɨ (1.00,1.00) 6.01ms Ɨ (1.00,1.00) ~ BenchmarkGoParse 10.1ms Ɨ (0.91,1.09) 9.6ms Ɨ (0.94,1.07) ~ BenchmarkRegexpMatchEasy0_32 207ns Ɨ (1.00,1.01) 210ns Ɨ (1.00,1.00) +1.45% BenchmarkRegexpMatchEasy0_1K 592ns Ɨ (0.99,1.00) 596ns Ɨ (0.99,1.01) +0.68% BenchmarkRegexpMatchEasy1_32 184ns Ɨ (0.99,1.01) 184ns Ɨ (0.99,1.01) ~ BenchmarkRegexpMatchEasy1_1K 1.01µs Ɨ (1.00,1.00) 1.01µs Ɨ (0.99,1.01) ~ BenchmarkRegexpMatchMedium_32 327ns Ɨ (0.99,1.00) 327ns Ɨ (1.00,1.01) ~ BenchmarkRegexpMatchMedium_1K 92.5µs Ɨ (1.00,1.00) 93.0µs Ɨ (1.00,1.02) +0.48% BenchmarkRegexpMatchHard_32 4.79µs Ɨ (0.95,1.00) 4.76µs Ɨ (0.95,1.01) ~ BenchmarkRegexpMatchHard_1K 136µs Ɨ (1.00,1.00) 136µs Ɨ (1.00,1.01) ~ BenchmarkRevcomp 900ms Ɨ (0.99,1.01) 892ms Ɨ (1.00,1.01) ~ BenchmarkTemplate 170ms Ɨ (0.99,1.01) 175ms Ɨ (0.99,1.00) +2.95% BenchmarkTimeParse 645ns Ɨ (1.00,1.00) 638ns Ɨ (1.00,1.00) -1.16% BenchmarkTimeFormat 740ns Ɨ (1.00,1.00) 772ns Ɨ (1.00,1.00) +4.39% Change-Id: I0be905e32791e0cb70ff01f169c4b309a971d981 Reviewed-on: https://go-review.googlesource.com/9159 Reviewed-by: Rick Hudson <rlh@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-04-17 00:25:10 -04:00
case OAS, OASWB:
if isblank(ln.Left) && candiscard(ln.Right) {
break
}
fallthrough
// fall through
default:
return true
}
}
// is this main
if localpkg.Name == "main" {
return true
}
// is there an explicit init function
s := Lookup("init.1")
if s.Def != nil {
return true
}
// are there any imported init functions
for _, s := range initSyms {
if s.Def != nil {
return true
}
}
// then none
return false
}
func fninit(n *NodeList) {
if Debug['A'] != 0 {
// sys.go or unsafe.go during compiler build
return
}
nf := initfix(n)
if !anyinit(nf) {
return
}
var r []*Node
// (1)
gatevar := newname(Lookup("initdoneĀ·"))
addvar(gatevar, Types[TUINT8], PEXTERN)
// (2)
Maxarg = 0
fn := Nod(ODCLFUNC, nil, nil)
initsym := Lookup("init")
fn.Func.Nname = newname(initsym)
fn.Func.Nname.Name.Defn = fn
fn.Func.Nname.Name.Param.Ntype = Nod(OTFUNC, nil, nil)
declare(fn.Func.Nname, PFUNC)
funchdr(fn)
// (3)
a := Nod(OIF, nil, nil)
a.Left = Nod(OGT, gatevar, Nodintconst(1))
a.Likely = 1
r = append(r, a)
// (3a)
a.Nbody.Set([]*Node{Nod(ORETURN, nil, nil)})
// (4)
b := Nod(OIF, nil, nil)
b.Left = Nod(OEQ, gatevar, Nodintconst(1))
// this actually isn't likely, but code layout is better
// like this: no JMP needed after the call.
b.Likely = 1
r = append(r, b)
// (4a)
b.Nbody.Set([]*Node{Nod(OCALL, syslook("throwinit", 0), nil)})
// (6)
a = Nod(OAS, gatevar, Nodintconst(1))
r = append(r, a)
// (7)
for _, s := range initSyms {
if s.Def != nil && s != initsym {
// could check that it is fn of no args/returns
a = Nod(OCALL, s.Def, nil)
r = append(r, a)
}
}
// (8)
r = append(r, nf...)
// (9)
// could check that it is fn of no args/returns
for i := 1; ; i++ {
s := Lookupf("init.%d", i)
if s.Def == nil {
break
}
a = Nod(OCALL, s.Def, nil)
r = append(r, a)
}
// (10)
a = Nod(OAS, gatevar, Nodintconst(2))
r = append(r, a)
// (11)
a = Nod(ORETURN, nil, nil)
r = append(r, a)
exportsym(fn.Func.Nname)
fn.Nbody.Set(r)
funcbody(fn)
Curfn = fn
typecheck(&fn, Etop)
typecheckslice(r, Etop)
Curfn = nil
funccompile(fn)
}