cl: TestBasicFunc

This commit is contained in:
xushiwei
2024-04-20 22:30:38 +08:00
parent 294bd6c606
commit cb148c1a3e
2 changed files with 42 additions and 3 deletions

View File

@@ -83,3 +83,34 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0
}
`)
}
func TestBasicFunc(t *testing.T) {
testCompile(t, `package foo
func fn(a int, b float64) int {
return 1
}
`, `; ModuleID = 'foo'
source_filename = "foo"
@"init$guard" = external global ptr
define void @init() {
_llgo_0:
%0 = load i1, ptr @"init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define i64 @fn(i64 %0, double %1) {
_llgo_0:
ret i64 1
}
`)
}

View File

@@ -53,12 +53,16 @@ func (p Program) BoolVal(v bool) Expr {
return Expr{ret, t}
}
func (p Program) IntVal(v int) Expr {
t := p.Int()
ret := llvm.ConstInt(t.ll, uint64(v), false)
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}
return p.IntVal(v)
case bool:
return p.BoolVal(v)
case float64:
@@ -75,6 +79,10 @@ func (b Builder) Const(v constant.Value, t types.Type) Expr {
switch t.Kind() {
case types.Bool:
return b.prog.BoolVal(constant.BoolVal(v))
case types.Int:
if v, exact := constant.Int64Val(v); exact {
return b.prog.IntVal(int(v))
}
}
}
panic("todo")