[LLVMdev] how to call back from JITted code to some native custom functions/variables.

Christian Parpart trapni at gentoo.org
Wed Sep 8 08:17:07 PDT 2010


Hey,

I am designing/implementing a very simple "flow control language" [1] I
want to use in my HTTP server. I am still very new to LLVM and I've
already the lexer and parser part working pretty well, however, the code
generator (runner) does not yet feel as comfortable as I want.

Precisely, I want this language to be extensible in terms of custom
variables - of type string (i8*) or numbers (currently: double) - but do
not get the idea on how to *ideally* call back, from the design point of
view.

Imagine the following API:

class Runner {
public:
    // codegen's the AST of given handler (and all referring syms)
    // and executes it.
    // a handler returns true when actually handled the data,
    // false is returned otherwise.
    bool run(Handler *handlerAST);

    // registers a callback, to be invoked when a custom/native
    // variable is requested.
    // the type T may be double and std::string
    template<typename T>
    void registerNativeVariable(const std::string& symbolName,
        const std::function<T()>& callback);

    // registers a callback for custom handlers (special functions)
    // to further process the data.
    void registerNativeHandler(const std::string& symbolName,
        const std::function<bool()>& callback);

    // registers a native function with arbitary list of parameters
    // and some return value. types of each may be void/double/string.
    void registerNativeHandler(const std::string& symbolName,
        const std::function<GenericValue(std::vector<GenericValue>& args));

    // ...
};

Now, I have successfully constructed my AST, and can even run some little
JITted code, but that all makes just sense, when the JITted code can also
call back to some native routines and variables (later on: loaded via
custom plugins and registered the way the API above offers us).

But I need some advice in how to go further. How could I now make such
"callbacks" to handler (function) calls and variable "calls" possible?

Many thanks in advance,
Christian Parpart.

[1] github.com/trapni/flow (the *tiny* source code I'm talking about)





More information about the llvm-dev mailing list