[LLVMdev] Perfect forwarding?

Talin viridia at gmail.com
Sun Aug 30 13:20:43 PDT 2009


Hey all, it's been a while since I have posted on this list, but I've 
been continuing my work with LLVM and getting lots done :)

One question I wanted to ask is whether anyone had any advice on how to 
implement "perfect forwarding" in a runtime (as opposed to compile-time) 
context with LLVM. The general idea is that you have a bunch of methods 
that have different call signatures, and you want to have some generic 
handler function that can intercept these calls and then forward each 
call to another method that has the same signature as the original call.

A typical example of how this would be used is something like Mockito or 
EasyMock - you have some interface, and you dynamically create an 
implementation of that interface which is able to intercept all of the 
method calls and record the order in which they are called and the value 
of the arguments. The unit test code then performs some validation on 
the recording to ensure that the invocations match what was expected.

The key point is that the handler method doesn't know at compile-time 
the call signature of the various methods that are going to be 
intercepted. In the case of Java and C#, the VM is able to box all of 
the arguments that are primitive types, and pass to the handler method 
an array of type Object[]. Similarly, one can call method.invoke() on a 
method, passing in that same array of objects, and the VM will unbox the 
primitive types and then call the actual method with the right argument 
types.

One obvious way to do this is to generate two stub functions for every 
function emitted by the compiler, one that takes the original argument 
signature and creates a list of boxed objects, and one that takes a list 
of boxed objects, unboxes them and then calls the function. However, 
that's a lot of extra code to generate, and I am concerned about the 
amount of code bloat that would create.

What would be better is if there was some way for the generic handler 
function to be able to dynamically get a picture of the layout of the 
call frame. On the receiving end, this would be something similar to a 
varargs call, where the called function unpacks the argument list based 
on a runtime-provided schema. One problem with the varargs approach is 
that I wouldn't want every method call to have to use the varargs 
calling convention.

On the calling side, I'm not sure that any language has a means to 
dynamically construct an argument list for a call. If such a thing did 
exist, what I suppose it would do is given a description of a function's 
calling signature, figure out which arguments should be put in which 
registers and which should be pushed on the stack. In other words, to do 
at runtime what LLVM currently does at compile time.

-- Talin




More information about the llvm-dev mailing list