fspl/generator/misc_test.go

155 lines
2.9 KiB
Go

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 i8
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 = sdiv i64 20, -5
%2 = alloca i64
store i64 %1, ptr %2
%3 = sdiv i64 15, 3
%4 = alloca i64
store i64 %3, ptr %4
%5 = srem i64 7, 3
%6 = alloca i64
store i64 %5, ptr %6
%7 = udiv i64 20, 5
%8 = alloca i64
store i64 %7, ptr %8
%9 = udiv i64 15, 3
%10 = alloca i64
store i64 %9, ptr %10
%11 = urem i64 7, 3
%12 = alloca i64
store i64 %11, ptr %12
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 TestCompare (test *testing.T) {
testString (test,
`%Bool = type i8
define %Bool @main() {
0:
%1 = alloca i64
store i64 32, ptr %1
%2 = load i64, ptr %1
%3 = icmp sgt i64 %2, 1
%4 = alloca %Bool
store i1 %3, ptr %4
%5 = load i64, ptr %1
%6 = icmp slt i64 0, %5
%7 = icmp slt i1 %6, 50
store i1 %7, ptr %4
%8 = load i64, ptr %1
%9 = icmp eq i64 32, %8
ret i1 %9
}
`,
`
[main]:Bool = {
x:Int = 32
b:Bool = [> x 1]
b = [< 0 x 50]
[= 32 x]
}
`)
}
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:]
}
`)
}