Added a test case for a Writer interface

I really need to cut out a lot of the unnecessary IR that the
generator emits
This commit is contained in:
Sasha Koshka 2024-01-26 04:32:55 +00:00
parent 794c41766c
commit cf201a08c6

View File

@ -87,3 +87,107 @@ Number.[number]: Int = [.this]
}
`)
}
func TestInterfaceWriter (test *testing.T) {
testString (test,
`%File = type i32
%Writer = type { ptr, ptr }
%Index = type i64
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 [17 x i8]
%3 = getelementptr [17 x i8], ptr %2, i32 0, i32 0
store i8 119, ptr %3
%4 = getelementptr [17 x i8], ptr %2, i32 0, i32 1
store i8 101, ptr %4
%5 = getelementptr [17 x i8], ptr %2, i32 0, i32 2
store i8 108, ptr %5
%6 = getelementptr [17 x i8], ptr %2, i32 0, i32 3
store i8 108, ptr %6
%7 = getelementptr [17 x i8], ptr %2, i32 0, i32 4
store i8 32, ptr %7
%8 = getelementptr [17 x i8], ptr %2, i32 0, i32 5
store i8 104, ptr %8
%9 = getelementptr [17 x i8], ptr %2, i32 0, i32 6
store i8 101, ptr %9
%10 = getelementptr [17 x i8], ptr %2, i32 0, i32 7
store i8 108, ptr %10
%11 = getelementptr [17 x i8], ptr %2, i32 0, i32 8
store i8 108, ptr %11
%12 = getelementptr [17 x i8], ptr %2, i32 0, i32 9
store i8 111, ptr %12
%13 = getelementptr [17 x i8], ptr %2, i32 0, i32 10
store i8 32, ptr %13
%14 = getelementptr [17 x i8], ptr %2, i32 0, i32 11
store i8 116, ptr %14
%15 = getelementptr [17 x i8], ptr %2, i32 0, i32 12
store i8 104, ptr %15
%16 = getelementptr [17 x i8], ptr %2, i32 0, i32 13
store i8 101, ptr %16
%17 = getelementptr [17 x i8], ptr %2, i32 0, i32 14
store i8 105, ptr %17
%18 = getelementptr [17 x i8], ptr %2, i32 0, i32 15
store i8 114, ptr %18
%19 = getelementptr [17 x i8], ptr %2, i32 0, i32 16
store i8 10, ptr %19
%20 = alloca { ptr, %Index }
%21 = getelementptr { ptr, %Index }, ptr %20, i32 0, i32 0
store ptr %2, ptr %21
%22 = getelementptr { ptr, %Index }, ptr %20, i32 0, i32 1
store %Index 17, ptr %22
%23 = load { ptr, %Index }, ptr %20
%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 = {
stdout:File = 0
[sayHello stdout]
0
}
`)
}