Files
c3c/test/test_suite/macros/macro_with_body.c3t
Christoffer Lerno 44df6eb75b Cleanup.
2022-08-12 18:13:24 +02:00

109 lines
2.8 KiB
C

module withbody;
extern fn int printf(char *, ...);
struct Foo
{
int x;
}
fn int Foo.mutate(Foo *foo)
{
printf("Mutating\n");
return 10 * ++foo.x;
}
macro @macro_with_body(foo, &x; @body(x, y))
{
x = foo.x;
@body(foo.mutate(), x);
}
macro @repeat(int times; @body(x))
{
for (int i = 0; i < times; i++)
{
@body(i + 1);
}
}
fn void main()
{
Foo f = { 33 };
int y;
@macro_with_body(f, y; int x, int dy)
{
printf("Got values %d, %d\n", x, dy);
};
@repeat(10; int loop)
{
printf("Repeat %d\n", loop);
};
}
/* #expect: withbody.ll
define i32 @withbody_Foo_mutate(%Foo* %0) #0 {
entry:
%1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0))
%2 = getelementptr inbounds %Foo, %Foo* %0, i32 0, i32 0
%3 = load i32, i32* %2, align 8
%add = add i32 %3, 1
store i32 %add, i32* %2, align 8
%mul = mul i32 10, %add
ret i32 %mul
}
define void @withbody_main() #0 {
entry:
%f = alloca %Foo, align 4
%y = alloca i32, align 4
%foo = alloca %Foo, align 4
%x = alloca i32, align 4
%dy = alloca i32, align 4
%times = alloca i32, align 4
%i = alloca i32, align 4
%loop = alloca i32, align 4
%0 = bitcast %Foo* %f to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %0, i8* align 4 bitcast (%Foo* @.__const to i8*), i32 4, i1 false)
store i32 0, i32* %y, align 4
%1 = bitcast %Foo* %foo to i8*
%2 = bitcast %Foo* %f to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 %1, i8* align 4 %2, i32 4, i1 false)
%3 = getelementptr inbounds %Foo, %Foo* %foo, i32 0, i32 0
%4 = load i32, i32* %3, align 4
store i32 %4, i32* %y, align 4
%5 = call i32 @withbody_Foo_mutate(%Foo* %foo)
store i32 %5, i32* %x, align 4
%6 = load i32, i32* %y, align 4
store i32 %6, i32* %dy, align 4
%7 = load i32, i32* %x, align 4
%8 = load i32, i32* %dy, align 4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i32 0, i32 0), i32 %7, i32 %8)
store i32 10, i32* %times, align 4
store i32 0, i32* %i, align 4
br label %loop.cond
loop.cond: ; preds = %loop.body, %entry
%10 = load i32, i32* %i, align 4
%11 = load i32, i32* %times, align 4
%lt = icmp slt i32 %10, %11
br i1 %lt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
%12 = load i32, i32* %i, align 4
%add = add i32 %12, 1
store i32 %add, i32* %loop, align 4
%13 = load i32, i32* %loop, align 4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i32 %13)
%15 = load i32, i32* %i, align 4
%add1 = add i32 %15, 1
store i32 %add1, i32* %i, align 4
br label %loop.cond
loop.exit: ; preds = %loop.cond
ret void
}