Hello, I am a llvm beginner. I recently try to run my own toy-c-compiler project but when I test ‘while’ statement, I find that it produces seemingly right IR codes but is unable to finish my “make run” command.
Here are my test codes and corresponding llvm IR and errors.
Test Code
extern void printi(int n);
extern void scanfi(int* intaddr);
int main(){
int a = 1;
int b = 2;
while(a<=10)
{
printi(a);
a=a+1;
}
printi(20);
return 0;
}
llvm IR
; ModuleID = 'main'
source_filename = "main"
declare i32 @printf(ptr, ...)
declare i32 @scanf(ptr, ...)
declare void @printi(i32)
declare void @scanfi(ptr)
define i32 @main() {
entry:
%a = alloca i32, align 4, addrspace(4)
store i32 1, ptr addrspace(4) %a, align 4
%b = alloca i32, align 4, addrspace(4)
store i32 2, ptr addrspace(4) %b, align 4
br label %cond
%letmp = icmp sle i32 %a1, 10
%addtmp = add i32 %a4, 1
br label %merge5
cond: ; preds = %body2, %entry
%a1 = load i32, ptr addrspace(4) %a, align 4
br i1 %letmp, label %body2, label %merge5
body2: ; preds = %cond
%a3 = load i32, ptr addrspace(4) %a, align 4
%calltmp = call void @printi(i32 %a3)
%a4 = load i32, ptr addrspace(4) %a, align 4
store i32 %a4, ptr addrspace(4) %a, align 4
br label %cond
merge5: ; preds = %entry, %cond
%calltmp6 = call void @printi(i32 20)
ret i32 0
}
Error
make: *** [Makefile:36: run] Segmentation fault (core dumped)
But it cannot run and error with segmentation fault when I use My engine trying to run it:
llvm::ExecutionEngine* EE = llvm::EngineBuilder(std::unique_ptr<llvm::Module>(_module)).create();
EE->finalizeObject();
std::vector<llvm::GenericValue> noargs;
llvm::GenericValue v = EE->runFunction(_module->getFunction("main"), noargs);
I don’t know where the problem is and I want to find a solution to debug my codes. Thank you in advance!