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

  • Thread starter Johannes Schaub (litb)
  • Start date
J

Johannes Schaub (litb)

Hello all. Not sure whether it fits this group, but since it's a common
problem and uses C++, maybe someone here knows a good solution. I've also
sent it to comp.compilers.

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top