Segment section

Hello,

I’ve been playing lately a lot with LLVM Interpreter. During the execution of IR I would like to know where my variables are allocated (e.g. stack, heap, global). Is there a function inside of the execution engine which provides such information?

Best regards,
Jasmin

I've been playing lately a lot with LLVM Interpreter. During the execution
of IR I would like to know where my variables are allocated (e.g. stack,
heap, global). Is there a function inside of the execution engine which
provides such information?

Are you interested in the classification of an underlying address
(likely to be invariant over an object's lifetime), or the value of
the variable at some random point during execution (where the answer
may be "it's in register r10 at the moment")?

LLVM doesn't provide a function for the first one, you might start by
looking is at a process's memory map (/proc/$PID/map on Linux,
presumably with some libdl interface) and decide which part your
address is in. Debuggers have to do this kind of thing, so you might
find helpful source snippets there.

For the second, we've recently got an @llvm.experimental.stackmap
intrinsic (http://llvm.org/docs/StackMaps.html), which can provide the
information in a separate area for use by JITs and so on. Its support
may be limited at the moment though (x86 Darwin I'm fairly sure of,
possibly Linux too by now).

Cheers.

Tim.

Hello,

thank you for the reply. Let me give you concrete example, so you can better understand to what I’m referring:
During the load process of variable, at some random point during execution (loadInst), how can I know (based on instruction or its value) is the variable allocated on the stack, in the heap or elsewhere?

Hi Jasmin,

During the load process of variable, at some random point during execution
(loadInst), how can I know (based on instruction or its value) is the
variable allocated on the stack, in the heap or elsewhere?

I'm afraid I'm still not clear on whether this is execution-time or
before. If you've got a LoadInst, that suggests it's before execution
has started (in which case the question is unanswerable: the answer
can depend on the runtime path the program takes[*]), but on the other
hand you're talking about the interpreter and mentioned "during
execution".

Have you somehow paused the interpreter? Do you want your program to
print out reasonably helpful debugging information at that LoadInst
(and if so, by hacking the interpreter or by modifying the LLVM IR to
do it automatically, or don't you care)? Or am I barking up the wrong
tree entirely?

Cheers.

Tim.

[*] E.g. "int *getVar() { return rand() % 2 ? malloc(sizeof(int)) :
&globalVar; }". Is a LoadInst acting on the result of that function
loading a heap or global variable?