fspl/generator/interface_test.go

252 lines
5.8 KiB
Go

package generator
import "testing"
func TestInterfaceBasic (test *testing.T) {
testString (test,
`%Doer = type { ptr, ptr }
%T = type i64
define void @main() {
0:
%1 = alloca %Doer
%2 = getelementptr %Doer, ptr %1, i32 0, i32 0
%3 = alloca %T
store ptr %3, ptr %2
%4 = getelementptr %Doer, ptr %1, i32 0, i32 1
store ptr @T.do, ptr %4
%5 = getelementptr %Doer, ptr %1, i32 0, i32 1
%6 = getelementptr %Doer, ptr %1, i32 0, i32 0
%7 = load ptr, ptr %6
%8 = load ptr, ptr %5
call void %8(ptr %7)
ret void
}
define void @T.do(ptr %this) {
0:
%1 = alloca ptr
store ptr %this, ptr %1
ret void
}
`,
`
Doer: ([do])
T: Int
T.[do] = { }
[main] = {
if:Doer = x:T
if.[do]
}
`)
}
func TestInterfaceIntegerReturn (test *testing.T) {
testString (test,
`%Number = type i64
%Numbered = type { ptr, ptr }
define i64 @main() {
0:
%1 = alloca %Number
store i64 5, ptr %1
%2 = alloca %Numbered
%3 = getelementptr %Numbered, ptr %2, i32 0, i32 0
store ptr %1, ptr %3
%4 = getelementptr %Numbered, ptr %2, i32 0, i32 1
store ptr @Number.number, ptr %4
%5 = getelementptr %Numbered, ptr %2, i32 0, i32 1
%6 = getelementptr %Numbered, ptr %2, i32 0, i32 0
%7 = load ptr, ptr %6
%8 = load ptr, ptr %5
%9 = call i64 %8(ptr %7)
ret i64 %9
}
define i64 @Number.number(ptr %this) {
0:
%1 = alloca ptr
store ptr %this, ptr %1
%2 = load ptr, ptr %1
%3 = load i64, ptr %2
ret i64 %3
}
`,
`
Numbered: ([number]: Int)
Number: Int
Number.[number]: Int = [.this]
[main]: Int = {
num:Number = 5
if:Numbered = num
if.[number]
}
`)
}
func TestInterfaceWriter (test *testing.T) {
testString (test,
`%File = type i32
%Writer = type { ptr, ptr }
%Index = type i64
%Byte = type i8
define i64 @main() {
0:
%1 = alloca %File
store i32 0, ptr %1
%2 = alloca %Writer
%3 = getelementptr %Writer, ptr %2, i32 0, i32 0
store ptr %1, ptr %3
%4 = getelementptr %Writer, ptr %2, i32 0, i32 1
store ptr @File.write, ptr %4
%5 = load %Writer, ptr %2
call void @sayHello(%Writer %5)
ret i64 0
}
define void @sayHello(%Writer %writer) {
0:
%1 = alloca %Writer
store %Writer %writer, ptr %1
%2 = alloca { ptr, %Index }
%3 = alloca [17 x %Byte]
%4 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 0
store i8 119, ptr %4
%5 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 1
store i8 101, ptr %5
%6 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 2
store i8 108, ptr %6
%7 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 3
store i8 108, ptr %7
%8 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 4
store i8 32, ptr %8
%9 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 5
store i8 104, ptr %9
%10 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 6
store i8 101, ptr %10
%11 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 7
store i8 108, ptr %11
%12 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 8
store i8 108, ptr %12
%13 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 9
store i8 111, ptr %13
%14 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 10
store i8 32, ptr %14
%15 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 11
store i8 116, ptr %15
%16 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 12
store i8 104, ptr %16
%17 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 13
store i8 101, ptr %17
%18 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 14
store i8 105, ptr %18
%19 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 15
store i8 114, ptr %19
%20 = getelementptr [17 x %Byte], ptr %3, i32 0, i32 16
store i8 10, ptr %20
%21 = getelementptr { ptr, %Index }, ptr %2, i32 0, i32 0
store ptr %3, ptr %21
%22 = getelementptr { ptr, %Index }, ptr %2, i32 0, i32 1
store %Index 17, ptr %22
%23 = load { ptr, %Index }, ptr %2
%24 = getelementptr %Writer, ptr %1, i32 0, i32 1
%25 = getelementptr %Writer, ptr %1, i32 0, i32 0
%26 = load ptr, ptr %25
%27 = load ptr, ptr %24
%28 = call %Index %27(ptr %26, { ptr, %Index } %23)
ret void
}
define %Index @File.write(ptr %this, { ptr, %Index } %buffer) {
0:
%1 = alloca ptr
store ptr %this, ptr %1
%2 = alloca { ptr, %Index }
store { ptr, %Index } %buffer, ptr %2
%3 = load ptr, ptr %1
%4 = load %File, ptr %3
%5 = getelementptr { ptr, %Index }, ptr %2, i32 0, i32 0
%6 = load ptr, ptr %5
%7 = getelementptr { ptr, %Index }, ptr %2, i32 0, i32 1
%8 = load %Index, ptr %7
%9 = call %Index @write(%File %4, ptr %6, %Index %8)
ret %Index %9
}
declare %Index @write(%File %fd, ptr %buffer, %Index %count)
`,
`
[write fd:File buffer:*Byte count:Index]: Index
Writer: ([write buffer:*:Byte]: Index)
File: I32
File.[write buffer:*:Byte]:Index = [write [.this] [~*Byte buffer] [#buffer]]
[sayHello writer:Writer] = writer.[write 'well hello their\n']
[main]:Int 'main' = {
stdout:File = 0
[sayHello stdout]
0
}
`)
}
func TestInterfaceInStruct (test *testing.T) {
testString (test,
`%Writer = type { ptr, ptr }
%A = type { %Writer }
%File = type i32
%Index = type i64
define void @main() {
0:
%1 = alloca %A
%2 = getelementptr %A, ptr %1, i32 0, i32 0
%3 = getelementptr %Writer, ptr %2, i32 0, i32 0
%4 = alloca i32
store i32 0, ptr %4
store ptr %4, ptr %3
%5 = getelementptr %Writer, ptr %2, i32 0, i32 1
store ptr @File.write, ptr %5
ret void
}
declare %Index @File.write(ptr %this, { ptr, %Index } %buffer)
`,
`
Writer: ([write buffer:*:Byte]: Index)
A: (output: Writer)
File: I32
File.[write buffer:*:Byte]:Index
[main] 'main' = {
a:A = (output: [~File 0])
}
`)
}
func TestInterfaceAssignment (test *testing.T) {
testString (test,
`%Face = type { ptr, ptr }
%Impl = type i64
define void @main() {
0:
%1 = alloca %Face
%2 = getelementptr %Face, ptr %1, i32 0, i32 0
%3 = alloca %Impl
store ptr %3, ptr %2
%4 = getelementptr %Face, ptr %1, i32 0, i32 1
store ptr @Impl.x, ptr %4
ret void
}
define i64 @Impl.x(ptr %this) {
0:
%1 = alloca ptr
store ptr %this, ptr %1
ret i64 5
}
`,
`
Face: ([x]:Int)
Impl: Int
Impl.[x]:Int = 5
[main] 'main' = {
i:Face = f:Impl
}
`)
}