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

106 lines
2.5 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(ptr %0) #0 {
entry:
%1 = call i32 (ptr, ...) @printf(ptr @.str.2)
%2 = getelementptr inbounds %Foo, ptr %0, i32 0, i32 0
%3 = load i32, ptr %2, align 8
%add = add i32 %3, 1
store i32 %add, ptr %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
call void @llvm.memcpy.p0.p0.i32(ptr align 4 %f, ptr align 4 @.__const, i32 4, i1 false)
store i32 0, ptr %y, align 4
call void @llvm.memcpy.p0.p0.i32(ptr align 4 %foo, ptr align 4 %f, i32 4, i1 false)
%0 = getelementptr inbounds %Foo, ptr %foo, i32 0, i32 0
%1 = load i32, ptr %0, align 4
store i32 %1, ptr %y, align 4
%2 = call i32 @withbody_Foo_mutate(ptr %foo)
store i32 %2, ptr %x, align 4
%3 = load i32, ptr %y, align 4
store i32 %3, ptr %dy, align 4
%4 = load i32, ptr %x, align 4
%5 = load i32, ptr %dy, align 4
%6 = call i32 (ptr, ...) @printf(ptr @.str, i32 %4, i32 %5)
store i32 10, ptr %times, align 4
store i32 0, ptr %i, align 4
br label %loop.cond
loop.cond: ; preds = %loop.body, %entry
%7 = load i32, ptr %i, align 4
%8 = load i32, ptr %times, align 4
%lt = icmp slt i32 %7, %8
br i1 %lt, label %loop.body, label %loop.exit
loop.body: ; preds = %loop.cond
%9 = load i32, ptr %i, align 4
%add = add i32 %9, 1
store i32 %add, ptr %loop, align 4
%10 = load i32, ptr %loop, align 4
%11 = call i32 (ptr, ...) @printf(ptr @.str.1, i32 %10)
%12 = load i32, ptr %i, align 4
%add1 = add i32 %12, 1
store i32 %add1, ptr %i, align 4
br label %loop.cond
loop.exit: ; preds = %loop.cond
ret void
}