mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
141 lines
4.7 KiB
C
141 lines
4.7 KiB
C
// #target: x64-darwin
|
|
|
|
module foo;
|
|
struct Foo
|
|
{
|
|
int[] x;
|
|
}
|
|
|
|
struct FooIterator
|
|
{
|
|
usize index;
|
|
Foo *f;
|
|
}
|
|
func FooIterator Foo.iterator(Foo *f)
|
|
{
|
|
return FooIterator {0, f};
|
|
}
|
|
|
|
func bool FooIterator.next(FooIterator *it, int *value)
|
|
{
|
|
if (it.index == it.f.x.len) return false;
|
|
*value = it.f.x[it.index++];
|
|
return true;
|
|
}
|
|
|
|
func void main()
|
|
{
|
|
int[*] i = { 1, 3, 10 };
|
|
Foo x = { &i };
|
|
foreach FOO: (int f : x) {
|
|
printf("%d\n", f);
|
|
while (1)
|
|
{
|
|
break FOO;
|
|
}
|
|
}
|
|
}
|
|
|
|
extern func int printf(char *fmt, ...);
|
|
|
|
// #expect: foo.ll
|
|
|
|
define { i64, %Foo* } @foo.Foo__iterator(%Foo* %0)
|
|
entry:
|
|
%f = alloca %Foo*, align 8
|
|
%literal = alloca %FooIterator, align 8
|
|
%tempcoerce = alloca { i64, %Foo* }, align 8
|
|
store %Foo* %0, %Foo** %f, align 8
|
|
%1 = getelementptr inbounds %FooIterator, %FooIterator* %literal, i32 0, i32 0
|
|
store i64 0, i64* %1, align 8
|
|
%2 = getelementptr inbounds %FooIterator, %FooIterator* %literal, i32 0, i32 1
|
|
%3 = load %Foo*, %Foo** %f, align 8
|
|
store %Foo* %3, %Foo** %2, align 8
|
|
%4 = bitcast { i64, %Foo* }* %tempcoerce to i8*
|
|
%5 = bitcast %FooIterator* %literal to i8*
|
|
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %4, i8* align 8 %5, i32 16, i1 false)
|
|
%6 = load { i64, %Foo* }, { i64, %Foo* }* %tempcoerce, align 8
|
|
ret { i64, %Foo* } %6
|
|
}
|
|
|
|
; Function Attrs: nounwind
|
|
define zeroext i8 @foo.FooIterator__next(%FooIterator* %0, i32* %1) #0 {
|
|
entry:
|
|
%it = alloca %FooIterator*, align 8
|
|
%value = alloca i32*, align 8
|
|
store %FooIterator* %0, %FooIterator** %it, align 8
|
|
store i32* %1, i32** %value, align 8
|
|
%2 = load %FooIterator*, %FooIterator** %it, align 8
|
|
%3 = getelementptr inbounds %FooIterator, %FooIterator* %2, i32 0, i32 0
|
|
%4 = load i64, i64* %3, align 8
|
|
%5 = load %FooIterator*, %FooIterator** %it, align 8
|
|
%6 = getelementptr inbounds %FooIterator, %FooIterator* %5, i32 0, i32 1
|
|
%7 = load %Foo*, %Foo** %6, align 8
|
|
%8 = getelementptr inbounds %Foo, %Foo* %7, i32 0, i32 0
|
|
%9 = getelementptr inbounds %"int[]", %"int[]"* %8, i32 0, i32 1
|
|
%10 = load i64, i64* %9, align 8
|
|
%eq = icmp eq i64 %4, %10
|
|
br i1 %eq, label %if.then, label %if.exit
|
|
|
|
if.then: ; preds = %entry
|
|
ret i8 0
|
|
|
|
if.exit: ; preds = %entry
|
|
%11 = load i32*, i32** %value, align 8
|
|
%12 = load %FooIterator*, %FooIterator** %it, align 8
|
|
%13 = getelementptr inbounds %FooIterator, %FooIterator* %12, i32 0, i32 1
|
|
%14 = load %Foo*, %Foo** %13, align 8
|
|
%15 = getelementptr inbounds %Foo, %Foo* %14, i32 0, i32 0
|
|
%16 = getelementptr inbounds %"int[]", %"int[]"* %15, i32 0, i32 0
|
|
%17 = load i32*, i32** %16, align 8
|
|
%18 = load %FooIterator*, %FooIterator** %it, align 8
|
|
%19 = getelementptr inbounds %FooIterator, %FooIterator* %18, i32 0, i32 0
|
|
%20 = load i64, i64* %19, align 8
|
|
%add = add i64 %20, 1
|
|
store i64 %add, i64* %19, align 8
|
|
%ptroffset = getelementptr inbounds i32, i32* %17, i64 %20
|
|
%21 = load i32, i32* %ptroffset, align 4
|
|
store i32 %21, i32* %11, align 8
|
|
ret i8 1
|
|
}
|
|
|
|
; Function Attrs: nounwind
|
|
define void @main() #0 {
|
|
entry:
|
|
%i = alloca [3 x i32], align 4
|
|
%x = alloca %Foo, align 8
|
|
%f = alloca i32, align 4
|
|
%.iterator = alloca %FooIterator, align 8
|
|
%result = alloca %FooIterator, align 8
|
|
%0 = bitcast [3 x i32]* %i to i8*
|
|
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %0, i8* align 4 bitcast ([3 x i32]* @.__const to i8*), i32 12, i1 false)
|
|
%1 = getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0
|
|
%2 = bitcast [3 x i32]* %i to i32*
|
|
%3 = insertvalue %"int[]" undef, i32* %2, 0
|
|
%4 = insertvalue %"int[]" %3, i64 3, 1
|
|
store %"int[]" %4, %"int[]"* %1, align 8
|
|
store i32 0, i32* %f, align 4
|
|
%5 = call { i64, %Foo* } @foo.Foo__iterator(%Foo* %x)
|
|
%6 = bitcast %FooIterator* %result to { i64, %Foo* }*
|
|
store { i64, %Foo* } %5, { i64, %Foo* }* %6, align 8
|
|
%7 = bitcast %FooIterator* %.iterator to i8*
|
|
%8 = bitcast %FooIterator* %result to i8*
|
|
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %7, i8* align 8 %8, i32 16, i1 false)
|
|
br label %for.cond
|
|
|
|
for.cond: ; preds = %entry
|
|
%9 = call i8 @foo.FooIterator__next(%FooIterator* %.iterator, i32* %f)
|
|
%10 = trunc i8 %9 to i1
|
|
br i1 %10, label %for.body, label %for.exit
|
|
|
|
for.body: ; preds = %for.cond
|
|
%11 = load i32, i32* %f, align 4
|
|
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %11)
|
|
br label %while.begin
|
|
|
|
while.begin: ; preds = %for.body
|
|
br label %for.exit
|
|
|
|
for.exit: ; preds = %while.begin, %for.cond
|
|
ret void
|
|
} |