package generator import "testing" func TestPrintDigit (test *testing.T) { testString (test, `%Byte = type i8 %Index = type i64 define void @printDigit(%Byte %digit) { 0: %1 = alloca %Byte store %Byte %digit, ptr %1 %2 = load %Byte, ptr %1 %3 = alloca %Byte store i8 48, ptr %3 %4 = load %Byte, ptr %3 %5 = add %Byte %2, %4 store %Byte %5, ptr %1 %6 = call %Index @write(i32 1, ptr %1, i64 1) ret void } declare %Index @write(i32 %file, ptr %buffer, %Index %count) `, ` [write file:I32 buffer:*Byte count:Index]: Index [printDigit digit:Byte] = { digit = [+ digit '0'] [write 1 [@digit] 1] } `) } func TestSignedUnsignedDivision (test *testing.T) { testString (test, `define void @main() { 0: %1 = alloca i64 %2 = sdiv i64 20, -5 store i64 %2, ptr %1 %3 = alloca i64 %4 = sdiv i64 15, 3 store i64 %4, ptr %3 %5 = alloca i64 %6 = srem i64 7, 3 store i64 %6, ptr %5 %7 = alloca i64 %8 = udiv i64 20, 5 store i64 %8, ptr %7 %9 = alloca i64 %10 = udiv i64 15, 3 store i64 %10, ptr %9 %11 = alloca i64 %12 = urem i64 7, 3 store i64 %12, ptr %11 ret void } `, ` [main] = { a:Int = [/ 20 -5] b:Int = [/ 15 3] c:Int = [% 7 3] d:UInt = [/ 20 5] e:UInt = [/ 15 3] f:UInt = [% 7 3] } `) } func TestFloatMath (test *testing.T) { testString (test, `define void @main() { 0: %1 = alloca double %2 = fadd double 2.0, 3.0 store double %2, ptr %1 %3 = alloca double %4 = load double, ptr %1 %5 = fmul double %4, 100.0 store double %5, ptr %3 ret void } `, ` [main] = { sum:F64 = [+ 2 3] prd:F64 = [* sum 100] } `) } func TestCompare (test *testing.T) { testString (test, `%Bool = type i8 %A = type i64 define void @main() { 0: %1 = alloca i64 store i64 32, ptr %1 %2 = alloca %Bool %3 = load i64, ptr %1 %4 = icmp sgt i64 %3, 1 store i1 %4, ptr %2 %5 = load i64, ptr %1 %6 = icmp slt i64 0, %5 %7 = icmp slt i1 %6, 50 store i1 %7, ptr %2 %8 = load i64, ptr %1 %9 = icmp eq i64 32, %8 store i1 %9, ptr %2 %10 = icmp sgt i64 5, 3 store i1 %10, ptr %2 ret void } `, ` A: Int [main] = { x:Int = 32 b:Bool = [> x 1] b = [< 0 x 50] b = [= 32 x] b = [> [~A 5] [~A 3]] } `) } func TestSlice (test *testing.T) { testString (test, `%Index = type i64 %String = type { ptr, %Index } define void @print(%String %string) { 0: %1 = alloca %String store %String %string, ptr %1 %2 = alloca %String br label %3 3: %4 = getelementptr { ptr, %Index }, ptr %1, i32 0, i32 1 %5 = load %Index, ptr %4 %6 = icmp ult %Index %5, 1 br i1 %6, label %7, label %8 7: br label %21 8: %9 = getelementptr { ptr, %Index }, ptr %1, i32 0, i32 0 %10 = load ptr, ptr %9 %11 = call %Index @write(i32 1, ptr %10, i64 1) %12 = getelementptr { ptr, %Index }, ptr %1, i32 0, i32 0 %13 = load ptr, ptr %12 %14 = getelementptr { ptr, %Index }, ptr %1, i32 0, i32 1 %15 = load %Index, ptr %14 %16 = sub %Index %15, 1 %17 = getelementptr i8, ptr %13, i64 1 %18 = getelementptr %String, ptr %2, i32 0, i32 0 store ptr %17, ptr %18 %19 = getelementptr %String, ptr %2, i32 0, i32 1 store %Index %16, ptr %19 %20 = load %String, ptr %2 store %String %20, ptr %1 br label %3 21: ret void } declare %Index @write(i32 %file, ptr %buffer, %Index %count) `, ` [write file:I32 buffer:*Byte count:Index]: Index [print string: String] = loop { if [< [#string] 1] then [break] [write 1 [~*Byte string] 1] string = [\string 1:] } `) }