forked from go-llvm/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
143 lines (131 loc) · 5.05 KB
/
channels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2012 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package llgo
import (
"code.google.com/p/go.tools/go/types"
"github.com/axw/gollvm/llvm"
)
// makeChan implements make(chantype[, size])
func (c *compiler) makeChan(chantyp types.Type, size *LLVMValue) *LLVMValue {
f := c.runtime.makechan.LLVMValue()
dyntyp := c.types.ToRuntime(chantyp)
dyntyp = c.builder.CreatePtrToInt(dyntyp, c.target.IntPtrType(), "")
ch := c.builder.CreateCall(f, []llvm.Value{dyntyp, size.LLVMValue()}, "")
return c.NewValue(ch, chantyp)
}
// chanSend implements ch<- x
func (c *compiler) chanSend(ch *LLVMValue, elem *LLVMValue) {
elemtyp := ch.Type().Underlying().(*types.Chan).Elem()
elem = elem.Convert(elemtyp).(*LLVMValue)
stackptr := c.stacksave()
elemptr := c.builder.CreateAlloca(elem.LLVMValue().Type(), "")
c.builder.CreateStore(elem.LLVMValue(), elemptr)
elemptr = c.builder.CreatePtrToInt(elemptr, c.target.IntPtrType(), "")
nb := boolLLVMValue(false)
chansend := c.runtime.chansend.LLVMValue()
chantyp := c.types.ToRuntime(ch.typ.Underlying())
chantyp = c.builder.CreateBitCast(chantyp, chansend.Type().ElementType().ParamTypes()[0], "")
c.builder.CreateCall(chansend, []llvm.Value{chantyp, ch.LLVMValue(), elemptr, nb}, "")
// Ignore result; only used in runtime.
c.stackrestore(stackptr)
}
// chanRecv implements x[, ok] = <-ch
func (c *compiler) chanRecv(ch *LLVMValue, commaOk bool) *LLVMValue {
elemtyp := ch.Type().Underlying().(*types.Chan).Elem()
stackptr := c.stacksave()
ptr := c.builder.CreateAlloca(c.types.ToLLVM(elemtyp), "")
chanrecv := c.runtime.chanrecv.LLVMValue()
chantyp := c.types.ToRuntime(ch.Type().Underlying())
chantyp = c.builder.CreateBitCast(chantyp, chanrecv.Type().ElementType().ParamTypes()[0], "")
ok := c.builder.CreateCall(chanrecv, []llvm.Value{
chantyp,
ch.LLVMValue(),
c.builder.CreatePtrToInt(ptr, c.target.IntPtrType(), ""),
boolLLVMValue(false), // nb
}, "")
elem := c.builder.CreateLoad(ptr, "")
c.stackrestore(stackptr)
if !commaOk {
return c.NewValue(elem, elemtyp)
}
typ := tupleType(elemtyp, types.Typ[types.Bool])
tuple := llvm.Undef(c.types.ToLLVM(typ))
tuple = c.builder.CreateInsertValue(tuple, elem, 0, "")
tuple = c.builder.CreateInsertValue(tuple, ok, 1, "")
return c.NewValue(tuple, typ)
}
// selectState is equivalent to ssa.SelectState
type selectState struct {
Dir types.ChanDir
Chan *LLVMValue
Send *LLVMValue
}
func (c *compiler) chanSelect(states []selectState, blocking bool) *LLVMValue {
stackptr := c.stacksave()
defer c.stackrestore(stackptr)
n := uint64(len(states))
if !blocking {
// blocking means there's no default case
n++
}
lln := llvm.ConstInt(llvm.Int32Type(), n, false)
allocsize := c.builder.CreateCall(c.runtime.selectsize.LLVMValue(), []llvm.Value{lln}, "")
selectp := c.builder.CreateArrayAlloca(llvm.Int8Type(), allocsize, "selectp")
c.memsetZero(selectp, allocsize)
selectp = c.builder.CreatePtrToInt(selectp, c.target.IntPtrType(), "")
c.builder.CreateCall(c.runtime.selectinit.LLVMValue(), []llvm.Value{lln, selectp}, "")
// Allocate stack for the values to send/receive.
//
// TODO(axw) request optimisation in ssa to special-
// case receive cases with no assignment, so we know
// not to allocate stack space or do a copy.
resTypes := []types.Type{types.Typ[types.Int], types.Typ[types.Bool]}
for _, state := range states {
if state.Dir == types.RecvOnly {
chantyp := state.Chan.Type().Underlying().(*types.Chan)
resTypes = append(resTypes, chantyp.Elem())
}
}
resType := tupleType(resTypes...)
llResType := c.types.ToLLVM(resType)
tupleptr := c.builder.CreateAlloca(llResType, "")
c.memsetZero(tupleptr, llvm.SizeOf(llResType))
var recvindex int
ptrs := make([]llvm.Value, len(states))
for i, state := range states {
chantyp := state.Chan.Type().Underlying().(*types.Chan)
elemtyp := c.types.ToLLVM(chantyp.Elem())
if state.Dir == types.SendOnly {
ptrs[i] = c.builder.CreateAlloca(elemtyp, "")
c.builder.CreateStore(state.Send.LLVMValue(), ptrs[i])
} else {
ptrs[i] = c.builder.CreateStructGEP(tupleptr, recvindex+2, "")
recvindex++
}
ptrs[i] = c.builder.CreatePtrToInt(ptrs[i], c.target.IntPtrType(), "")
}
// Create select{send,recv} calls.
selectsend := c.runtime.selectsend.LLVMValue()
selectrecv := c.runtime.selectrecv.LLVMValue()
var received llvm.Value
if recvindex > 0 {
received = c.builder.CreateStructGEP(tupleptr, 1, "")
}
if !blocking {
c.builder.CreateCall(c.runtime.selectdefault.LLVMValue(), []llvm.Value{selectp}, "")
}
for i, state := range states {
ch := state.Chan.LLVMValue()
if state.Dir == types.SendOnly {
c.builder.CreateCall(selectsend, []llvm.Value{selectp, ch, ptrs[i]}, "")
} else {
c.builder.CreateCall(selectrecv, []llvm.Value{selectp, ch, ptrs[i], received}, "")
}
}
// Fire off the select.
index := c.builder.CreateCall(c.runtime.selectgo.LLVMValue(), []llvm.Value{selectp}, "")
tuple := c.builder.CreateLoad(tupleptr, "")
tuple = c.builder.CreateInsertValue(tuple, index, 0, "")
return c.NewValue(tuple, resType)
}