Fix issue with overloaded subscript and ++/--.

This commit is contained in:
Christoffer Lerno
2024-11-20 00:23:08 +01:00
parent 489bb70901
commit ba54232b8d
4 changed files with 131 additions and 4 deletions

View File

@@ -0,0 +1,65 @@
// #target: macos-x64
module test;
import std;
struct Foo
{
int[5] a;
}
fn int Foo.get(self, usz i) @operator([]) => self.a[i];
fn void Foo.set(&self, usz i, int j) @operator([]=) { self.a[i] = j; }
Foo m;
fn void main() {
m.a[3] = 100;
int x = m[3]--;
}
/* #expect: test.ll
define i32 @test.Foo.get(ptr byval(%Foo) align 8 %0, i64 %1) #0 {
entry:
%ptroffset = getelementptr inbounds [4 x i8], ptr %0, i64 %1
%2 = load i32, ptr %ptroffset, align 4
ret i32 %2
}
define void @test.Foo.set(ptr %0, i64 %1, i32 %2) #0 {
entry:
%ptroffset = getelementptr inbounds [4 x i8], ptr %0, i64 %1
store i32 %2, ptr %ptroffset, align 4
ret void
}
define void @test.main() #0 {
entry:
%x = alloca i32, align 4
%.anon = alloca ptr, align 8
%.anon1 = alloca i32, align 4
%.anon2 = alloca i32, align 4
%indirectarg = alloca %Foo, align 8
%.anon3 = alloca i32, align 4
store i32 100, ptr getelementptr inbounds (i8, ptr @test.m, i64 12), align 4
store ptr @test.m, ptr %.anon, align 8
store i32 3, ptr %.anon1, align 4
%0 = load ptr, ptr %.anon, align 8
%1 = load i32, ptr %.anon1, align 4
%sext = sext i32 %1 to i64
call void @llvm.memcpy.p0.p0.i32(ptr align 8 %indirectarg, ptr align 4 %0, i32 20, i1 false)
%2 = call i32 @test.Foo.get(ptr byval(%Foo) align 8 %indirectarg, i64 %sext)
store i32 %2, ptr %.anon2, align 4
%3 = load i32, ptr %.anon2, align 4
%sub = sub i32 %3, 1
store i32 %sub, ptr %.anon2, align 4
store i32 %3, ptr %.anon3, align 4
%4 = load i32, ptr %.anon1, align 4
%sext4 = sext i32 %4 to i64
%5 = load ptr, ptr %.anon, align 8
%6 = load i32, ptr %.anon2, align 4
call void @test.Foo.set(ptr %5, i64 %sext4, i32 %6)
%7 = load i32, ptr %.anon3, align 4
store i32 %7, ptr %x, align 4
ret void
}