Files
c3c/test/test_suite/statements/foreach_more_implementations.c3t
2022-08-28 23:33:39 +02:00

201 lines
7.9 KiB
C

// #target: macos-x64
module test;
import std::io;
struct Vector
{
usize size;
int* elements;
}
macro int Vector.get(Vector* vector, usize element) @operator(elementat)
{
return vector.elements[element];
}
macro int* Vector.get_ref(Vector* vector, usize element) @operator(elementref)
{
return &vector.elements[element];
}
macro usize Vector.size(Vector vector) @operator(len) {
return vector.size;
}
fn void main()
{
int[2] x = { 1, 2 };
Vector v = { 2, &x };
foreach (int* &ref : v)
{
std::io::printf("%d\n", *ref);
*ref += 2;
}
foreach (int i : v)
{
std::io::printf("%d\n", i);
}
}
/* #expect: test.ll
define void @test_main() #0 {
entry:
%x = alloca [2 x i32], align 4
%v = alloca %Vector, align 8
%.anon = alloca i64, align 8
%vector = alloca %Vector, align 8
%.anon1 = alloca i64, align 8
%ref = alloca i32*, align 8
%vector2 = alloca %Vector*, align 8
%element = alloca i64, align 8
%retparam = alloca i64, align 8
%taddr = alloca %"char[]", align 8
%vararg = alloca %"variant[]", align 8
%varargslots = alloca [1 x %variant], align 16
%.anon6 = alloca i64, align 8
%vector7 = alloca %Vector, align 8
%.anon8 = alloca i64, align 8
%i = alloca i32, align 4
%vector12 = alloca %Vector*, align 8
%element13 = alloca i64, align 8
%retparam15 = alloca i64, align 8
%taddr16 = alloca %"char[]", align 8
%vararg19 = alloca %"variant[]", align 8
%varargslots20 = alloca [1 x %variant], align 16
%0 = bitcast [2 x i32]* %x to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %0, i8* align 4 bitcast ([2 x i32]* @.__const to i8*), i32 8, i1 false)
%1 = getelementptr inbounds %Vector, %Vector* %v, i32 0, i32 0
store i64 2, i64* %1, align 8
%2 = getelementptr inbounds %Vector, %Vector* %v, i32 0, i32 1
%ptrptr = bitcast [2 x i32]* %x to i32*
store i32* %ptrptr, i32** %2, align 8
%3 = bitcast %Vector* %vector to i8*
%4 = bitcast %Vector* %v to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %3, i8* align 8 %4, i32 16, i1 false)
%5 = getelementptr inbounds %Vector, %Vector* %vector, i32 0, i32 0
%6 = load i64, i64* %5, align 8
store i64 %6, i64* %.anon, align 8
store i64 0, i64* %.anon1, align 8
br label %loop.cond
loop.cond: ; preds = %voiderr, %entry
%7 = load i64, i64* %.anon1, align 8
%8 = load i64, i64* %.anon, align 8
%lt = icmp ult i64 %7, %8
br i1 %lt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
store %Vector* %v, %Vector** %vector2, align 8
%9 = load i64, i64* %.anon1, align 8
store i64 %9, i64* %element, align 8
%10 = load %Vector*, %Vector** %vector2, align 8
%11 = getelementptr inbounds %Vector, %Vector* %10, i32 0, i32 1
%12 = load i32*, i32** %11, align 8
%13 = load i64, i64* %element, align 8
%ptroffset = getelementptr inbounds i32, i32* %12, i64 %13
store i32* %ptroffset, i32** %ref, align 8
store %"char[]" { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i64 3 }, %"char[]"* %taddr, align 8
%14 = bitcast %"char[]"* %taddr to { i8*, i64 }*
%15 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %14, i32 0, i32 0
%lo = load i8*, i8** %15, align 8
%16 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %14, i32 0, i32 1
%hi = load i64, i64* %16, align 8
%17 = load i32*, i32** %ref, align 8
%18 = bitcast i32* %17 to i8*
%19 = insertvalue %variant undef, i8* %18, 0
%20 = insertvalue %variant %19, i64 ptrtoint (%.introspect* @"ct$int" to i64), 1
%21 = getelementptr inbounds [1 x %variant], [1 x %variant]* %varargslots, i64 0, i64 0
store %variant %20, %variant* %21, align 16
%22 = getelementptr inbounds %"variant[]", %"variant[]"* %vararg, i32 0, i32 1
store i64 1, i64* %22, align 8
%23 = getelementptr inbounds %"variant[]", %"variant[]"* %vararg, i32 0, i32 0
%24 = bitcast [1 x %variant]* %varargslots to %variant*
store %variant* %24, %variant** %23, align 8
%25 = bitcast %"variant[]"* %vararg to { i8*, i64 }*
%26 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %25, i32 0, i32 0
%lo3 = load i8*, i8** %26, align 8
%27 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %25, i32 0, i32 1
%hi4 = load i64, i64* %27, align 8
%28 = call i64 @std_io_printf(i64* %retparam, i8* %lo, i64 %hi, i8* %lo3, i64 %hi4)
%not_err = icmp eq i64 %28, 0
br i1 %not_err, label %after_check, label %voiderr
after_check: ; preds = %loop.body
br label %voiderr
voiderr: ; preds = %after_check, %loop.body
%29 = load i32*, i32** %ref, align 8
%30 = load i32, i32* %29, align 8
%add = add i32 %30, 2
store i32 %add, i32* %29, align 8
%31 = load i64, i64* %.anon1, align 8
%add5 = add i64 %31, 1
store i64 %add5, i64* %.anon1, align 8
br label %loop.cond
loop.exit: ; preds = %loop.cond
%32 = bitcast %Vector* %vector7 to i8*
%33 = bitcast %Vector* %v to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %32, i8* align 8 %33, i32 16, i1 false)
%34 = getelementptr inbounds %Vector, %Vector* %vector7, i32 0, i32 0
%35 = load i64, i64* %34, align 8
store i64 %35, i64* %.anon6, align 8
store i64 0, i64* %.anon8, align 8
br label %loop.cond9
loop.cond9: ; preds = %voiderr25, %loop.exit
%36 = load i64, i64* %.anon8, align 8
%37 = load i64, i64* %.anon6, align 8
%lt10 = icmp ult i64 %36, %37
br i1 %lt10, label %loop.body11, label %loop.exit27
loop.body11: ; preds = %loop.cond9
store %Vector* %v, %Vector** %vector12, align 8
%38 = load i64, i64* %.anon8, align 8
store i64 %38, i64* %element13, align 8
%39 = load %Vector*, %Vector** %vector12, align 8
%40 = getelementptr inbounds %Vector, %Vector* %39, i32 0, i32 1
%41 = load i32*, i32** %40, align 8
%42 = load i64, i64* %element13, align 8
%ptroffset14 = getelementptr inbounds i32, i32* %41, i64 %42
%43 = load i32, i32* %ptroffset14, align 4
store i32 %43, i32* %i, align 4
store %"char[]" { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i64 3 }, %"char[]"* %taddr16, align 8
%44 = bitcast %"char[]"* %taddr16 to { i8*, i64 }*
%45 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %44, i32 0, i32 0
%lo17 = load i8*, i8** %45, align 8
%46 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %44, i32 0, i32 1
%hi18 = load i64, i64* %46, align 8
%47 = bitcast i32* %i to i8*
%48 = insertvalue %variant undef, i8* %47, 0
%49 = insertvalue %variant %48, i64 ptrtoint (%.introspect* @"ct$int" to i64), 1
%50 = getelementptr inbounds [1 x %variant], [1 x %variant]* %varargslots20, i64 0, i64 0
store %variant %49, %variant* %50, align 16
%51 = getelementptr inbounds %"variant[]", %"variant[]"* %vararg19, i32 0, i32 1
store i64 1, i64* %51, align 8
%52 = getelementptr inbounds %"variant[]", %"variant[]"* %vararg19, i32 0, i32 0
%53 = bitcast [1 x %variant]* %varargslots20 to %variant*
store %variant* %53, %variant** %52, align 8
%54 = bitcast %"variant[]"* %vararg19 to { i8*, i64 }*
%55 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %54, i32 0, i32 0
%lo21 = load i8*, i8** %55, align 8
%56 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %54, i32 0, i32 1
%hi22 = load i64, i64* %56, align 8
%57 = call i64 @std_io_printf(i64* %retparam15, i8* %lo17, i64 %hi18, i8* %lo21, i64 %hi22)
%not_err23 = icmp eq i64 %57, 0
br i1 %not_err23, label %after_check24, label %voiderr25
after_check24: ; preds = %loop.body11
br label %voiderr25
voiderr25: ; preds = %after_check24, %loop.body11
%58 = load i64, i64* %.anon8, align 8
%add26 = add i64 %58, 1
store i64 %add26, i64* %.anon8, align 8
br label %loop.cond9
loop.exit27: ; preds = %loop.cond9
ret void
}