I'm currently working on programming language compiler that generated LLVM-IR. I'm not using any library for the emitting, so I'm just writing instructions to a file. The problem is mutable variables, I'm registering a %i, %i.(index) with the value and then loading %i with %i.(index). This may not be the right way to have mutable variables in llvm, but is the best of what I've tried before as i need them to work with recursion for example when looping over i as long as the value is less than ten which is what this code does.
LLVM out code:
@.str_0 = private unnamed_addr constant [4 x i8] c"%i\0A\00"
declare i32 @printf(i8*, ...) nounwind
define i32 @add(i32 %a, i32 %b) nounwind {
entry:
%t1 = add i32 %a, %b
ret i32 %t1
}
define i32 @main() nounwind {
entry:
%i = alloca i32
%i.0 = alloca i32
store i32 0, i32* %i.0
store i32 %i.0, i32* %i
%t2 = icmp slt i32 %i, 10
br i1 %t2, label %lb1, label %lb2
lb1:
%t3 = getelementptr [4 x i8], [4 x i8]* @.str_0, i32 0, i32 0
%t4 = call i32 @printf(i8* %t3, i32 %i)
%t5 = call i32 @add(i32 %i, i32 1)
%i.1 = alloca i32
store i32 %t5, i32* %i
store i32 %i.1, i32* %i
%t6 = icmp slt i32 %i, 10
br i1 %t6, label %lb1, label %lb2
lb2:
ret i32 0
}
The code my compiler is generating from:
fn add(a: int, b: int): int {
return a + b;
}
fn main(): void {
let i: int = 0;
while (i < 10) {
printf("%i\n", i);
i = add(i, 1);
}
return;
}
Error:
./out/test.zk.ll:15:15: error: '%i.0' defined with type 'ptr' but expected 'i32'
15 | store i32 %i.0, i32* %i
| ^
1 error generated.
I expect to be able to define a mutable variable then change it's value after the fact with it working in recursion.
Thanks in advance for any help.
%iand%i.0). In line 14 you store a value of 0 at i32 pointed to by%i. In line 15, you try to store a pointer (modern systems this is generally 64 bits) into the i32 pointed to by%i.0, and the tool complains about a type error because of that.