Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question
+1 for asking a question about LLVM and F#. :-) – Jon Harrop Oct 23 '12 at 8:59
Why do you want to tie an actual source code in? Use llvm metadata to store the location triplets (file, line, column) for each instruction. – SK-logic Oct 23 '12 at 9:15
@SK-logic: It's just for simpler debugging of the compiler itself - you can see what part of IR links to which part of source without having to go through a file/line/col lookup. Take a look at pastebin.com/46h4fTCg (code from a random stack machine I've targeted before) to see what I've been doing before. – Callum Rogers Oct 23 '12 at 10:51
I see. You can always use a text metadata like this, or, alternatively (and it is better for debugging too) you can implement a preprocessor which will read both IR and your source and display the corresponding lines of code in between instructions with metadata. – SK-logic Oct 23 '12 at 12:19
Yes, metadata nodes seem to be the way forward here. Alas, I am using the llvm-fs bindings which uses the LLVM C api and looks like i'll need to ask another question about how to actually make metadata nodes. – Callum Rogers Oct 23 '12 at 15:40
show 3 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.