1
0
mirror of https://github.com/golang/go synced 2024-11-19 07:44:49 -07:00

cmd/compile,runtime: make selectnbrecv return two values

The only different between selectnbrecv and selectnbrecv2 is the later
set the input pointer value by second return value from chanrecv.

So by making selectnbrecv return two values from chanrecv, we can get
rid of selectnbrecv2, the compiler can now call only selectnbrecv and
generate simpler code.

Change-Id: Ifaf6cf1314c4f47b06ed9606b1578319be808507
Reviewed-on: https://go-review.googlesource.com/c/go/+/292890
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2021-02-17 11:36:58 +07:00
parent 5e804ba17d
commit 4048491234
4 changed files with 13 additions and 41 deletions

View File

@ -316,7 +316,7 @@ func runtimeTypes() []*types.Type {
typs[92] = newSig(params(typs[1], typs[3]), nil) typs[92] = newSig(params(typs[1], typs[3]), nil)
typs[93] = newSig(params(typs[1], typs[3], typs[15], typs[3], typs[15]), params(typs[15])) typs[93] = newSig(params(typs[1], typs[3], typs[15], typs[3], typs[15]), params(typs[15]))
typs[94] = newSig(params(typs[87], typs[3]), params(typs[6])) typs[94] = newSig(params(typs[87], typs[3]), params(typs[6]))
typs[95] = newSig(params(typs[3], typs[84]), params(typs[6])) typs[95] = newSig(params(typs[3], typs[84]), params(typs[6], typs[6]))
typs[96] = types.NewPtr(typs[6]) typs[96] = types.NewPtr(typs[6])
typs[97] = newSig(params(typs[3], typs[96], typs[84]), params(typs[6])) typs[97] = newSig(params(typs[3], typs[96], typs[84]), params(typs[6]))
typs[98] = newSig(params(typs[63]), nil) typs[98] = newSig(params(typs[63]), nil)

View File

@ -166,7 +166,7 @@ func typedmemclr(typ *byte, dst *any)
func typedslicecopy(typ *byte, dstPtr *any, dstLen int, srcPtr *any, srcLen int) int func typedslicecopy(typ *byte, dstPtr *any, dstLen int, srcPtr *any, srcLen int) int
func selectnbsend(hchan chan<- any, elem *any) bool func selectnbsend(hchan chan<- any, elem *any) bool
func selectnbrecv(elem *any, hchan <-chan any) bool func selectnbrecv(elem *any, hchan <-chan any) (bool, bool)
func selectnbrecv2(elem *any, received *bool, hchan <-chan any) bool func selectnbrecv2(elem *any, received *bool, hchan <-chan any) bool
func selectsetpc(pc *uintptr) func selectsetpc(pc *uintptr)

View File

@ -106,7 +106,7 @@ func walkSelectCases(cases []*ir.CommClause) []ir.Node {
ir.SetPos(n) ir.SetPos(n)
r := ir.NewIfStmt(base.Pos, nil, nil, nil) r := ir.NewIfStmt(base.Pos, nil, nil, nil)
*r.PtrInit() = cas.Init() *r.PtrInit() = cas.Init()
var call ir.Node var cond ir.Node
switch n.Op() { switch n.Op() {
default: default:
base.Fatalf("select %v", n.Op()) base.Fatalf("select %v", n.Op())
@ -115,7 +115,7 @@ func walkSelectCases(cases []*ir.CommClause) []ir.Node {
// if selectnbsend(c, v) { body } else { default body } // if selectnbsend(c, v) { body } else { default body }
n := n.(*ir.SendStmt) n := n.(*ir.SendStmt)
ch := n.Chan ch := n.Chan
call = mkcall1(chanfn("selectnbsend", 2, ch.Type()), types.Types[types.TBOOL], r.PtrInit(), ch, n.Value) cond = mkcall1(chanfn("selectnbsend", 2, ch.Type()), types.Types[types.TBOOL], r.PtrInit(), ch, n.Value)
case ir.OSELRECV2: case ir.OSELRECV2:
n := n.(*ir.AssignListStmt) n := n.(*ir.AssignListStmt)
@ -125,18 +125,14 @@ func walkSelectCases(cases []*ir.CommClause) []ir.Node {
if ir.IsBlank(elem) { if ir.IsBlank(elem) {
elem = typecheck.NodNil() elem = typecheck.NodNil()
} }
if ir.IsBlank(n.Lhs[1]) { cond = typecheck.Temp(types.Types[types.TBOOL])
// if selectnbrecv(&v, c) { body } else { default body } fn := chanfn("selectnbrecv", 2, ch.Type())
call = mkcall1(chanfn("selectnbrecv", 2, ch.Type()), types.Types[types.TBOOL], r.PtrInit(), elem, ch) call := mkcall1(fn, fn.Type().Results(), r.PtrInit(), elem, ch)
} else { as := ir.NewAssignListStmt(r.Pos(), ir.OAS2, []ir.Node{cond, n.Lhs[1]}, []ir.Node{call})
// TODO(cuonglm): make this use selectnbrecv() r.PtrInit().Append(typecheck.Stmt(as))
// if selectnbrecv2(&v, &received, c) { body } else { default body }
receivedp := typecheck.Expr(typecheck.NodAddr(n.Lhs[1]))
call = mkcall1(chanfn("selectnbrecv2", 2, ch.Type()), types.Types[types.TBOOL], r.PtrInit(), elem, receivedp, ch)
}
} }
r.Cond = typecheck.Expr(call) r.Cond = typecheck.Expr(cond)
r.Body = cas.Body r.Body = cas.Body
r.Else = append(dflt.Init(), dflt.Body...) r.Else = append(dflt.Init(), dflt.Body...)
return []ir.Node{r, ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)} return []ir.Node{r, ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)}

View File

@ -687,28 +687,6 @@ func selectnbsend(c *hchan, elem unsafe.Pointer) (selected bool) {
return chansend(c, elem, false, getcallerpc()) return chansend(c, elem, false, getcallerpc())
} }
// compiler implements
//
// select {
// case v = <-c:
// ... foo
// default:
// ... bar
// }
//
// as
//
// if selectnbrecv(&v, c) {
// ... foo
// } else {
// ... bar
// }
//
func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected bool) {
selected, _ = chanrecv(c, elem, false)
return
}
// compiler implements // compiler implements
// //
// select { // select {
@ -720,16 +698,14 @@ func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected bool) {
// //
// as // as
// //
// if c != nil && selectnbrecv2(&v, &ok, c) { // if selected, ok = selectnbrecv(&v, c); selected {
// ... foo // ... foo
// } else { // } else {
// ... bar // ... bar
// } // }
// //
func selectnbrecv2(elem unsafe.Pointer, received *bool, c *hchan) (selected bool) { func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected, received bool) {
// TODO(khr): just return 2 values from this function, now that it is in Go. return chanrecv(c, elem, false)
selected, *received = chanrecv(c, elem, false)
return
} }
//go:linkname reflect_chansend reflect.chansend //go:linkname reflect_chansend reflect.chansend