Callum Rogers
Callum Rogers

Reputation: 15829

Putting source information into LLVM bitcode files for debugging compiler errors

I'm making a basic compiler and want to put the source lines of code somewhere near the llvm code that is produced for easy debugging. For example:

proc f(a:Int, b:Int):Int {
    return a + b;
}

start {
    print f(1,2);
    return 0;
}

Should be annotated somehow with the source like so:

@numFmt = internal constant [4 x i8] c"%d\0A\00"

declare i32 @printf(i8*, ...) nounwind

define i32 @main() {
entry:
  %print.0 = call i32 @f(i32 1, i32 2)     ; print f(1,2) maybe using a comment
  %tmp.3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @numFmt, i32 0, i32 0), i32 %print.0)
"return 0    - or perhaps using a basic block":
  ret i32 0
}

define i32 @f(i32 %a, i32 %b) {
entry:
  "return a + b" = <or the result of some dummy operation that doesn't get stripped away"
  %return.0 = add i32 %a, %b
  ret i32 %return.0
}

Any solutions/ways of doing this? (I'm using the llvm-fs bindings btw, but I just want a way that will work with the IR)

Upvotes: 4

Views: 328

Answers (0)

Related Questions