How to execute llvm code

I have c code that computes the factorial int "factorial.c". I compile it into llvm readable code "factorial.ll", and I modify llvm in the compiled code. The goal is to execute the modified llvm code and see its output. How can i do this?

+4
source share
1 answer

This will depend on how the LLVM you compiled is built and which libraries it links, but, for example, by executing the following factorial.llwith a shell commandlli

$ lli factorial.ll
Factorial of 10 = 3628800

Executes a function mainusing JIT and uses standard printfto output the result to standard output.

@.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
}
+3
source

Source: https://habr.com/ru/post/1546727/


All Articles