mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
109 lines
2.8 KiB
C
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 = load %Foo, %Foo* %f, align 4
|
|
store %Foo %1, %Foo* %foo, align 4
|
|
%2 = getelementptr inbounds %Foo, %Foo* %foo, i32 0, i32 0
|
|
%3 = load i32, i32* %2, align 4
|
|
store i32 %3, i32* %y, align 4
|
|
%4 = call i32 @withbody.Foo__mutate(%Foo* %foo)
|
|
store i32 %4, i32* %x, align 4
|
|
%5 = load i32, i32* %y, align 4
|
|
store i32 %5, i32* %dy, align 4
|
|
%6 = load i32, i32* %x, align 4
|
|
%7 = load i32, i32* %dy, align 4
|
|
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i32 0, i32 0), i32 %6, i32 %7)
|
|
store i32 10, i32* %times, align 4
|
|
store i32 0, i32* %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.cond: ; preds = %loop.body, %entry
|
|
%9 = load i32, i32* %i, align 4
|
|
%10 = load i32, i32* %times, align 4
|
|
%lt = icmp slt i32 %9, %10
|
|
br i1 %lt, label %loop.body, label %loop.exit
|
|
|
|
loop.body: ; preds = %loop.cond
|
|
%11 = load i32, i32* %i, align 4
|
|
%add = add i32 %11, 1
|
|
store i32 %add, i32* %loop, align 4
|
|
%12 = load i32, i32* %loop, align 4
|
|
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i32 %12)
|
|
%14 = load i32, i32* %i, align 4
|
|
%add1 = add i32 %14, 1
|
|
store i32 %add1, i32* %i, align 4
|
|
br label %loop.cond
|
|
|
|
loop.exit: ; preds = %loop.cond
|
|
ret void
|
|
}
|