fspl/generator/control-flow_test.go
2024-03-06 14:50:58 -05:00

269 lines
5.4 KiB
Go

package generator
import "testing"
func TestBranchAssign (test *testing.T) {
testString (test,
`define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca i64
br i1 true, label %2, label %4
2:
store i64 5, ptr %1
br label %3
3:
ret void
4:
store i64 6, ptr %1
br label %3
}
`,
`
[main] = {
x:Int
if true then {
x = 5
} else {
x = 6
}
}
`)
}
func TestBranchAssignNoFalse (test *testing.T) {
testString (test,
`define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca i64
br i1 true, label %2, label %3
2:
store i64 5, ptr %1
br label %3
3:
ret void
}
`,
`
[main] = {
x:Int
if true then {
x = 5
}
}
`)
}
func TestBranchResult (test *testing.T) {
testString (test,
`define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca i64
br i1 true, label %2, label %5
2:
br label %3
3:
%4 = phi i64 [ 5, %2 ], [ 6, %5 ]
store i64 %4, ptr %1
ret void
5:
br label %3
}
`,
`
[main] = {
x:Int = if true then 5 else 6
}
`)
}
func TestLoop (test *testing.T) {
testString (test,
`define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
br label %1
1:
br label %1
2:
ret void
}
`,
`
[main] = {
loop {
}
}
`)
}
func TestLoopResult (test *testing.T) {
testString (test,
`define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca i64
br label %2
2:
br label %3
3:
store i64 5, ptr %1
ret void
}
`,
`
[main] = {
x:Int = loop {
[break 5]
}
}
`)
}
func TestLoopBranchResult (test *testing.T) {
testString (test,
`define i64 @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca i64
store i64 6, ptr %1
br label %2
2:
%3 = load i64, ptr %1
%4 = icmp slt i64 %3, 3
br i1 %4, label %5, label %8
5:
%6 = load i64, ptr %1
br label %11
7:
br label %2
8:
%9 = load i64, ptr %1
%10 = sub i64 %9, 1
store i64 %10, ptr %1
br label %7
11:
ret i64 %6
}
`,
`
[main]:Int = {
y:Int = 6
loop {
if [< y 3]
then [break y]
else {
y = [-- y]
}
}
}
`)
}
func TestMatch (test *testing.T) {
testString (test,
`%"0zNZN147MN2wzMAQ6NS2dQ==::U" = type { i64, i64 }
define i64 @"0zNZN147MN2wzMAQ6NS2dQ==::matchToInt"(%"0zNZN147MN2wzMAQ6NS2dQ==::U" %u) {
0:
%1 = alloca %"0zNZN147MN2wzMAQ6NS2dQ==::U"
store %"0zNZN147MN2wzMAQ6NS2dQ==::U" %u, ptr %1
%2 = alloca i64
%3 = alloca double
%4 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 0
%5 = load i64, ptr %4
switch i64 %5, label %6 [
i64 7620767046192759206, label %8
i64 9186060094042213285, label %12
]
6:
%7 = phi i64 [ %11, %8 ], [ %16, %12 ], [ zeroinitializer, %0 ]
ret i64 %7
8:
%9 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 1
%10 = load i64, ptr %9
store i64 %10, ptr %2
%11 = load i64, ptr %2
br label %6
12:
%13 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 1
%14 = load double, ptr %13
store double %14, ptr %3
%15 = load double, ptr %3
%16 = fptosi double %15 to i64
br label %6
}
`,
`
U: (| Int F64)
[matchToInt u:U]:Int = match u in
| u:Int u
| u:F64 [~Int u]
`)
}
func TestMatchUnionUnderComplete (test *testing.T) {
testString (test,
`%"0zNZN147MN2wzMAQ6NS2dQ==::U" = type { i64, i64 }
%"AAAAAAAAAAAAAAAAAAAAAA==::Index" = type i64
%"AAAAAAAAAAAAAAAAAAAAAA==::String" = type { ptr, %"AAAAAAAAAAAAAAAAAAAAAA==::Index" }
define void @"0zNZN147MN2wzMAQ6NS2dQ==::matchToInt"(%"0zNZN147MN2wzMAQ6NS2dQ==::U" %u) {
0:
%1 = alloca %"0zNZN147MN2wzMAQ6NS2dQ==::U"
store %"0zNZN147MN2wzMAQ6NS2dQ==::U" %u, ptr %1
%2 = alloca i64
%3 = alloca %"AAAAAAAAAAAAAAAAAAAAAA==::String"
%4 = alloca [3 x i8]
%5 = alloca double
%6 = alloca %"AAAAAAAAAAAAAAAAAAAAAA==::String"
%7 = alloca [3 x i8]
%8 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 0
%9 = load i64, ptr %8
switch i64 %9, label %10 [
i64 7620767046192759206, label %11
i64 9186060094042213285, label %20
]
10:
ret void
11:
%12 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 1
%13 = load i64, ptr %12
store i64 %13, ptr %2
%14 = getelementptr [3 x i8], ptr %4, i32 0, i32 0
store i8 73, ptr %14
%15 = getelementptr [3 x i8], ptr %4, i32 0, i32 1
store i8 110, ptr %15
%16 = getelementptr [3 x i8], ptr %4, i32 0, i32 2
store i8 116, ptr %16
%17 = getelementptr %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %3, i32 0, i32 0
store ptr %4, ptr %17
%18 = getelementptr %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %3, i32 0, i32 1
store %"AAAAAAAAAAAAAAAAAAAAAA==::Index" 3, ptr %18
%19 = load %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %3
call void @"0zNZN147MN2wzMAQ6NS2dQ==::print"(%"AAAAAAAAAAAAAAAAAAAAAA==::String" %19)
br label %10
20:
%21 = getelementptr %"0zNZN147MN2wzMAQ6NS2dQ==::U", ptr %1, i32 0, i32 1
%22 = load double, ptr %21
store double %22, ptr %5
%23 = getelementptr [3 x i8], ptr %7, i32 0, i32 0
store i8 70, ptr %23
%24 = getelementptr [3 x i8], ptr %7, i32 0, i32 1
store i8 54, ptr %24
%25 = getelementptr [3 x i8], ptr %7, i32 0, i32 2
store i8 52, ptr %25
%26 = getelementptr %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %6, i32 0, i32 0
store ptr %7, ptr %26
%27 = getelementptr %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %6, i32 0, i32 1
store %"AAAAAAAAAAAAAAAAAAAAAA==::Index" 3, ptr %27
%28 = load %"AAAAAAAAAAAAAAAAAAAAAA==::String", ptr %6
call void @"0zNZN147MN2wzMAQ6NS2dQ==::print"(%"AAAAAAAAAAAAAAAAAAAAAA==::String" %28)
br label %10
}
declare void @"0zNZN147MN2wzMAQ6NS2dQ==::print"(%"AAAAAAAAAAAAAAAAAAAAAA==::String" %str)
`,
`
U: (| Int F64 UInt)
[print str:String]
[matchToInt u:U] = match u in
| u:Int [print 'Int']
| u:F64 [print 'F64']
`)
}