我有一个c代码来计算int" factorial.c"的阶乘。我把它编译成llvm可读代码" factorial.ll"我在编译的llvm代码中修改。 目标是执行修改后的llvm代码并查看其输出,我该怎么做?
答案 0 :(得分:3)
这将取决于您输出的LLVM的组装方式以及链接的库,但是例如使用shell命令factorial.ll
执行以下lli
$ lli factorial.ll
Factorial of 10 = 3628800
将使用JIT执行main
函数并使用标准printf
将结果输出到stdout。
@.str = private unnamed_addr constant [22 x i8] c"Factorial of %d = %d\0A\00", align 1
declare i32 @printf(i8*, ...)
define i32 @factorial(i32 %n) nounwind uwtable {
entry:
%n.addr = alloca i32, align 4
store i32 %n, i32* %n.addr, align 4
%0 = load i32* %n.addr, align 4
%cmp = icmp sle i32 %0, 1
br i1 %cmp, label %cond.true, label %cond.false
cond.true: ; preds = %entry
br label %cond.end
cond.false: ; preds = %entry
%1 = load i32* %n.addr, align 4
%2 = load i32* %n.addr, align 4
%sub = sub nsw i32 %2, 1
%call = call i32 @factorial(i32 %sub)
%mul = mul nsw i32 %1, %call
br label %cond.end
cond.end: ; preds = %cond.false, %cond.true
%cond = phi i32 [ 1, %cond.true ], [ %mul, %cond.false ]
ret i32 %cond
}
define i32 @main(i32 %argc, i8** %argv) nounwind uwtable {
entry:
%retval = alloca i32, align 4
%argc.addr = alloca i32, align 4
%argv.addr = alloca i8**, align 8
store i32 0, i32* %retval
store i32 %argc, i32* %argc.addr, align 4
store i8** %argv, i8*** %argv.addr, align 8
%call = call i32 @factorial(i32 10)
%call1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([22 x i8]* @.str, i32 0, i32 0), i32 10, i32 %call)
ret i32 0
}