ssa: select
This commit is contained in:
45
cl/_testgo/select/in.go
Normal file
45
cl/_testgo/select/in.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
send()
|
||||
recv()
|
||||
}
|
||||
|
||||
func send() {
|
||||
ch1 := make(chan int)
|
||||
ch2 := make(chan int)
|
||||
|
||||
go func() {
|
||||
println(<-ch1)
|
||||
}()
|
||||
go func() {
|
||||
println(<-ch2)
|
||||
}()
|
||||
|
||||
select {
|
||||
case ch1 <- 100:
|
||||
case ch2 <- 200:
|
||||
}
|
||||
}
|
||||
|
||||
func recv() {
|
||||
c1 := make(chan string)
|
||||
c2 := make(chan string)
|
||||
go func() {
|
||||
c1 <- "ch1"
|
||||
}()
|
||||
go func() {
|
||||
c2 <- "ch2"
|
||||
}()
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case msg1 := <-c1:
|
||||
println(msg1)
|
||||
case msg2 := <-c2:
|
||||
println(msg2)
|
||||
default:
|
||||
println("exit")
|
||||
}
|
||||
}
|
||||
}
|
||||
1
cl/_testgo/select/out.ll
Normal file
1
cl/_testgo/select/out.ll
Normal file
@@ -0,0 +1 @@
|
||||
;
|
||||
@@ -588,6 +588,18 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
|
||||
t := v.Type()
|
||||
size := p.compileValue(b, v.Size)
|
||||
ret = b.MakeChan(p.prog.Type(t, llssa.InGo), size)
|
||||
case *ssa.Select:
|
||||
states := make([]*llssa.SelectState, len(v.States))
|
||||
for i, s := range v.States {
|
||||
states[i] = &llssa.SelectState{
|
||||
Chan: p.compileValue(b, s.Chan),
|
||||
Send: s.Dir == types.SendOnly,
|
||||
}
|
||||
if s.Send != nil {
|
||||
states[i].Value = p.compileValue(b, s.Send)
|
||||
}
|
||||
}
|
||||
ret = b.Select(states, v.Blocking)
|
||||
default:
|
||||
panic(fmt.Sprintf("compileInstrAndValue: unknown instr - %T\n", iv))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user