Alexander Mills
Alexander Mills

Reputation: 100190

Programmatic introspection/reflection - easier in VMs?

What makes programmatic introspection/reflection easier in virtual machines rather than native code?

I read somewhere that VMs by nature allow for better introspection/reflection capabilities but I cannot find more information about it online. Would like to know why.

Upvotes: 0

Views: 78

Answers (2)

Programmatic introspection essentially means to examine & inspect the current call stack, or the current continuation. (Read Appel's book: Compiling with Continuations).

Few programming languages provide this ability. Scheme's call/cc reifies the current continuation, but give no standard ways to inspect it.

The current call stack might be inspectable (e.g. see GCC __builtin_return_address as an ad hoc example).

Most compilers (but not all) do not have an easy way to give information about the layout of the current call frame (however, the debugger DWARF format contains it).

And optimizing compilers (e.g. for C) usually don't give access to the offset of some local variable in the call frame (even if the compiler computes this offset). BTW, the same stack slot might be reused for different variables; read about register spilling.

See also J.Pitrat's CAIA system - the generated C code is able to organize the stack to be able to inspect it;

In a bytecode VM like JVM or NekoVM or Parrot, introspection is easier because each local variable has a well defined slot in the call frame. This is not the case for most compiled languages (e.g. C or C++) because the compiler is able to reuse (for optimization purposes) some slots, or even put a variable only in some machine register, without even allocating any call stack slot to spill it.

Upvotes: 0

icanbenchurcat
icanbenchurcat

Reputation: 111

I believe you mean higher-level languages vs lower-level languages instead of virtual machines.

Higher level languages like Java and C# have implemented reflection and introspection, so there are functions available to the developer to use this information.

Languages like C do not have any pre-built reflection capabilities.

Reflection is very expensive (time-consuming) for any language to run, and should not be used in code that needs to be extremely fast.

Upvotes: 1

Related Questions