cl: TestVar done
This commit is contained in:
19
ssa/decl.go
19
ssa/decl.go
@@ -18,6 +18,7 @@ package ssa
|
||||
|
||||
import (
|
||||
"go/types"
|
||||
"strconv"
|
||||
|
||||
"github.com/goplus/llvm"
|
||||
)
|
||||
@@ -138,15 +139,21 @@ func (p Function) Param(i int) Expr {
|
||||
return Expr{p.impl.Param(i), p.params[i]}
|
||||
}
|
||||
|
||||
// NewBuilder creates a new Builder for the function.
|
||||
func (p Function) NewBuilder() Builder {
|
||||
prog := p.prog
|
||||
b := prog.ctx.NewBuilder()
|
||||
b.Finalize()
|
||||
return &aBuilder{b, p, prog}
|
||||
}
|
||||
|
||||
// MakeBody creates nblk basic blocks for the function, and creates
|
||||
// a new Builder associated to #0 block.
|
||||
func (p Function) MakeBody(nblk int) Builder {
|
||||
p.MakeBlocks(nblk)
|
||||
prog := p.prog
|
||||
b := prog.ctx.NewBuilder()
|
||||
b.Finalize()
|
||||
b.SetInsertPointAtEnd(p.blks[0].impl)
|
||||
return &aBuilder{b, p, prog}
|
||||
b := p.NewBuilder()
|
||||
b.impl.SetInsertPointAtEnd(p.blks[0].impl)
|
||||
return b
|
||||
}
|
||||
|
||||
// MakeBlocks creates nblk basic blocks for the function.
|
||||
@@ -157,7 +164,7 @@ func (p Function) MakeBlocks(nblk int) []BasicBlock {
|
||||
n := len(p.blks)
|
||||
f := p.impl
|
||||
for i := 0; i < nblk; i++ {
|
||||
label := ""
|
||||
label := "_llgo_" + strconv.Itoa(i)
|
||||
blk := llvm.AddBasicBlock(f, label)
|
||||
p.blks = append(p.blks, &aBasicBlock{blk, p, n + i})
|
||||
}
|
||||
|
||||
34
ssa/expr.go
34
ssa/expr.go
@@ -17,6 +17,7 @@
|
||||
package ssa
|
||||
|
||||
import (
|
||||
"go/constant"
|
||||
"go/token"
|
||||
"go/types"
|
||||
|
||||
@@ -42,12 +43,24 @@ func llvmValues(vals []Expr) []llvm.Value {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func (p Program) BoolVal(v bool) Expr {
|
||||
t := p.Bool()
|
||||
var bv uint64
|
||||
if v {
|
||||
bv = 1
|
||||
}
|
||||
ret := llvm.ConstInt(t.ll, bv, v)
|
||||
return Expr{ret, t}
|
||||
}
|
||||
|
||||
func (p Program) Val(v interface{}) Expr {
|
||||
switch v := v.(type) {
|
||||
case int:
|
||||
t := p.Int()
|
||||
ret := llvm.ConstInt(t.ll, uint64(v), false)
|
||||
return Expr{ret, t}
|
||||
case bool:
|
||||
return p.BoolVal(v)
|
||||
case float64:
|
||||
t := p.Float64()
|
||||
ret := llvm.ConstFloat(t.ll, v)
|
||||
@@ -56,6 +69,17 @@ func (p Program) Val(v interface{}) Expr {
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
func (b Builder) Const(v constant.Value, t types.Type) Expr {
|
||||
switch t := t.(type) {
|
||||
case *types.Basic:
|
||||
switch t.Kind() {
|
||||
case types.Bool:
|
||||
return b.prog.BoolVal(constant.BoolVal(v))
|
||||
}
|
||||
}
|
||||
panic("todo")
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
@@ -104,7 +128,7 @@ var logicOpToLLVM = []llvm.Opcode{
|
||||
token.OR - logicOpBase: llvm.Or,
|
||||
token.XOR - logicOpBase: llvm.Xor,
|
||||
token.SHL - logicOpBase: llvm.Shl,
|
||||
token.SHR - logicOpBase: llvm.LShr,
|
||||
token.SHR - logicOpBase: llvm.AShr, // Arithmetic Shift Right
|
||||
}
|
||||
|
||||
// AND OR XOR SHL SHR AND_NOT & | ^ << >> &^
|
||||
@@ -173,7 +197,7 @@ func (b Builder) BinOp(op token.Token, x, y Expr) Expr {
|
||||
kind := x.kind
|
||||
llop := logicOpToLLVM[op-logicOpBase]
|
||||
if op == token.SHR && kind == vkUnsigned {
|
||||
llop = llvm.AShr
|
||||
llop = llvm.LShr // Logical Shift Right
|
||||
}
|
||||
return Expr{llvm.CreateBinOp(b.impl, llop, x.impl, y.impl), x.Type}
|
||||
case isPredOp(op): // op: == != < <= < >=
|
||||
@@ -217,6 +241,12 @@ func (b Builder) Load(ptr Expr) Expr {
|
||||
return Expr{llvm.CreateLoad(b.impl, telem.ll, ptr.impl), telem}
|
||||
}
|
||||
|
||||
// Store stores val at the pointer ptr.
|
||||
func (b Builder) Store(ptr, val Expr) Builder {
|
||||
b.impl.CreateStore(val.impl, ptr.impl)
|
||||
return b
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package ssa
|
||||
|
||||
import (
|
||||
"go/constant"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"testing"
|
||||
@@ -27,6 +28,7 @@ func init() {
|
||||
}
|
||||
|
||||
func assertPkg(t *testing.T, p Package, expected string) {
|
||||
t.Helper()
|
||||
if v := p.String(); v != expected {
|
||||
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
|
||||
}
|
||||
@@ -43,6 +45,23 @@ source_filename = "foo/bar"
|
||||
`)
|
||||
}
|
||||
|
||||
func TestConst(t *testing.T) {
|
||||
prog := NewProgram(nil)
|
||||
pkg := prog.NewPackage("bar", "foo/bar")
|
||||
rets := types.NewTuple(types.NewVar(0, nil, "", types.Typ[types.Bool]))
|
||||
sig := types.NewSignatureType(nil, nil, nil, nil, rets, false)
|
||||
b := pkg.NewFunc("fn", sig).MakeBody(1)
|
||||
b.Return(b.Const(constant.MakeBool(true), types.Typ[types.Bool]))
|
||||
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i1 @fn() {
|
||||
_llgo_0:
|
||||
ret i1 true
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func TestStruct(t *testing.T) {
|
||||
empty := types.NewStruct(nil, nil)
|
||||
|
||||
@@ -99,6 +118,7 @@ func TestBasicFunc(t *testing.T) {
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(i64 %0, double %1) {
|
||||
_llgo_0:
|
||||
ret i64 1
|
||||
}
|
||||
`)
|
||||
@@ -118,6 +138,7 @@ func TestFuncParam(t *testing.T) {
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(i64 %0, double %1) {
|
||||
_llgo_0:
|
||||
ret i64 %0
|
||||
}
|
||||
`)
|
||||
@@ -145,11 +166,13 @@ func TestFuncCall(t *testing.T) {
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(i64 %0, double %1) {
|
||||
_llgo_0:
|
||||
ret i64 1
|
||||
}
|
||||
|
||||
define void @main() {
|
||||
%1 = call i64 @fn(i64 1, double 1.200000e+00)
|
||||
_llgo_0:
|
||||
%0 = call i64 @fn(i64 1, double 1.200000e+00)
|
||||
ret void
|
||||
}
|
||||
`)
|
||||
@@ -174,12 +197,30 @@ source_filename = "foo/bar"
|
||||
@a = external global i64
|
||||
|
||||
define { i64, double } @fn(double %0) {
|
||||
_llgo_0:
|
||||
%mrv = insertvalue { i64, double } { ptr @a, double poison }, double %0, 1
|
||||
ret { i64, double } %mrv
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func TestJump(t *testing.T) {
|
||||
prog := NewProgram(nil)
|
||||
pkg := prog.NewPackage("bar", "foo/bar")
|
||||
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
|
||||
fn := pkg.NewFunc("loop", sig)
|
||||
b := fn.MakeBody(1)
|
||||
b.Jump(fn.Block(0))
|
||||
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define void @loop() {
|
||||
_llgo_0:
|
||||
br label %_llgo_0
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func TestIf(t *testing.T) {
|
||||
prog := NewProgram(nil)
|
||||
pkg := prog.NewPackage("bar", "foo/bar")
|
||||
@@ -201,13 +242,14 @@ func TestIf(t *testing.T) {
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(i64 %0) {
|
||||
%2 = icmp sgt i64 %0, 0
|
||||
br i1 %2, label %3, label %4
|
||||
_llgo_0:
|
||||
%1 = icmp sgt i64 %0, 0
|
||||
br i1 %1, label %_llgo_1, label %_llgo_2
|
||||
|
||||
3: ; preds = %1
|
||||
_llgo_1: ; preds = %_llgo_0
|
||||
ret i64 1
|
||||
|
||||
4: ; preds = %1
|
||||
_llgo_2: ; preds = %_llgo_0
|
||||
ret i64 0
|
||||
}
|
||||
`)
|
||||
@@ -244,8 +286,9 @@ func TestBinOp(t *testing.T) {
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(i64 %0, double %1) {
|
||||
%3 = add i64 %0, 1
|
||||
ret i64 %3
|
||||
_llgo_0:
|
||||
%2 = add i64 %0, 1
|
||||
ret i64 %2
|
||||
}
|
||||
`)
|
||||
}
|
||||
@@ -260,13 +303,19 @@ func TestUnOp(t *testing.T) {
|
||||
sig := types.NewSignatureType(nil, nil, nil, params, rets, false)
|
||||
fn := pkg.NewFunc("fn", sig)
|
||||
b := fn.MakeBody(1)
|
||||
ret := b.UnOp(token.MUL, fn.Param(0))
|
||||
b.Return(ret)
|
||||
ptr := fn.Param(0)
|
||||
val := b.UnOp(token.MUL, ptr)
|
||||
val2 := b.BinOp(token.SHR, val, prog.Val(1))
|
||||
b.Store(ptr, val2)
|
||||
b.Return(val2)
|
||||
assertPkg(t, pkg, `; ModuleID = 'foo/bar'
|
||||
source_filename = "foo/bar"
|
||||
|
||||
define i64 @fn(ptr %0) {
|
||||
%2 = load i64, ptr %0, align 4
|
||||
_llgo_0:
|
||||
%1 = load i64, ptr %0, align 4
|
||||
%2 = ashr i64 %1, 1
|
||||
store i64 %2, ptr %0, align 4
|
||||
ret i64 %2
|
||||
}
|
||||
`)
|
||||
|
||||
@@ -73,6 +73,14 @@ func (b Builder) Return(results ...Expr) {
|
||||
}
|
||||
}
|
||||
|
||||
// Jump emits a jump instruction.
|
||||
func (b Builder) Jump(jmpb BasicBlock) {
|
||||
if b.fn != jmpb.fn {
|
||||
panic("mismatched function")
|
||||
}
|
||||
b.impl.CreateBr(jmpb.impl)
|
||||
}
|
||||
|
||||
// If emits an if instruction.
|
||||
func (b Builder) If(cond Expr, thenb, elseb BasicBlock) {
|
||||
if b.fn != thenb.fn || b.fn != elseb.fn {
|
||||
|
||||
Reference in New Issue
Block a user