Show line numbers in diagnostics for a scripting language - how can this be done?

"Johannes Schaub \(litb\)" <schaub-johannes@web.de>
Fri, 29 Oct 2010 17:22:25 +0200

          From comp.compilers

Related articles
Show line numbers in diagnostics for a scripting language - how can th schaub-johannes@web.de (Johannes Schaub \(litb\)) (2010-10-29)
Re: Show line numbers in diagnostics for a scripting language - how ca idbaxter@semdesigns.com (Ira Baxter) (2010-11-01)
Re: Show line numbers in diagnostics for a scripting language - how ca gneuner2@comcast.net (George Neuner) (2010-11-02)
Re: Show line numbers in diagnostics for a scripting language - how ca schaub-johannes@web.de (Johannes Schaub \(litb\)) (2010-11-15)
Re: Show line numbers in diagnostics for a scripting language - how ca bc@freeuk.com (BartC) (2010-11-06)
Re: Show line numbers in diagnostics for a scripting language - how ca gneuner2@comcast.net (George Neuner) (2010-11-09)
| List of all articles for this month |

From: "Johannes Schaub \(litb\)" <schaub-johannes@web.de>
Newsgroups: comp.compilers
Date: Fri, 29 Oct 2010 17:22:25 +0200
Organization: T-Online
Keywords: code, debug, question
Posted-Date: 30 Oct 2010 17:59:02 EDT

Hello all.


I'm using LLVM, and I'm writing a backend for a closed-source compiler of a
language. Now, suppose I have for example generated roughly the following
code with LLVM (the runtime library that "rtEmitAdd" is defined in is
written in C++):


        ; allocate three values. Two for the operands and one for the result.
        ; all three are of type "myvalue".
        %num1 = alloca %myvalue
        %num2 = alloca %myvalue
        %result = alloca %myvalue
        ; evaluates 42 + 33. first store the values into the stack
        store %myvalue { %type* @inttype, i32 42 }, %myvalue* %num1
        store %myvalue { %type* @inttype, i32 33 }, %myvalue* %num2
        ; then let the runtime lib calculate it
        call void @rtEmitAdd(%myvalue* %num1,
                                                  %myvalue* %num2,
                                                  %myvalue* %result)


The runtime library will check whether both passed values could be added
(for example a string and an int can be added, by converting the int to a
string first). If the values can't be added, it will emit a diagnostic.


My problem is now - in the AST, I know what nodes correspond to what source
lines and even what source columns. But I want to display that information
(or at least the line-number information) in the diagnostic too.


What are the usual ways to solve this? I have thought abou this, and one way
could be to pass the line number along to the runtime functions like the
following


        call void @rtEmitAdd(i32 3, ; appeared in line 3
                                                  %myvalue* %num1,
                                                  %myvalue* %num2,
                                                  %myvalue* %result)


But this is not satisfactory, because I want to not pay the cost of pushin
the integer along, just for the few cases where I would need to diagnose an
error. Then I thought about letting the runtime library's direct entry point
(i.e in the above case, it would be the function "rtEmitAdd") using the
built-in (GCC has this one) __builtin_return_adress to get the PC of the
call instruction, and then search debug-information for the corresponding
line number. This seems like a bit of work to realize.


I wonder now - how is this generally solved for such languages? Thanks in
advance!
[There's no generally satisfactory approach. One thing I've done is
to embed the line numbers and routine names in no-op instructions
after each call, so I can get the info via a stack traceback, but not
affect runtime very much. Searching the debug symbols is not unreasonable;
it's slow, but it doesn't matter since it only needs to be fast enough to
display the results to a human user. -John]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.