forked from go-llvm/llgo
/
channels.go
45 lines (40 loc) · 1.44 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
// 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/greggoryhz/gollvm/llvm"
"go/ast"
)
func (c *compiler) VisitSendStmt(stmt *ast.SendStmt) {
channel := c.VisitExpr(stmt.Chan).(*LLVMValue)
value := c.VisitExpr(stmt.Value)
channel.chanSend(value)
}
func (v *LLVMValue) chanSend(value Value) {
var ptr llvm.Value
if value, ok := value.(*LLVMValue); ok && value.pointer != nil {
ptr = value.pointer.LLVMValue()
}
elttyp := v.typ.Underlying().(*types.Chan).Elem()
c := v.compiler
if ptr.IsNil() {
ptr = c.builder.CreateAlloca(c.types.ToLLVM(elttyp), "")
value := value.Convert(elttyp).LLVMValue()
c.builder.CreateStore(value, ptr)
}
uintptr_ := c.builder.CreatePtrToInt(ptr, c.target.IntPtrType(), "")
f := c.NamedFunction("runtime.chansend", "func f(c, ptr uintptr)")
c.builder.CreateCall(f, []llvm.Value{v.LLVMValue(), uintptr_}, "")
}
func (v *LLVMValue) chanRecv() *LLVMValue {
c := v.compiler
elttyp := v.typ.Underlying().(*types.Chan).Elem()
ptr := c.builder.CreateAlloca(c.types.ToLLVM(elttyp), "")
uintptr_ := c.builder.CreatePtrToInt(ptr, c.target.IntPtrType(), "")
f := c.NamedFunction("runtime.chanrecv", "func f(c, ptr uintptr)")
c.builder.CreateCall(f, []llvm.Value{v.LLVMValue(), uintptr_}, "")
value := c.builder.CreateLoad(ptr, "")
return c.NewValue(value, elttyp)
}