Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 893345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:12:21+00:00 2026-05-15T14:12:21+00:00

I’m toying around with the LLVM C++ API. I’d like to JIT compile code

  • 0

I’m toying around with the LLVM C++ API. I’d like to JIT compile code and run it.

However, I need to call a C++ method from said JIT-compiled code. Normally, LLVM treats method calls as function calls with the object pointer passed as the first argument, so calling shouldn’t be a problem. The real problem is to get that function into LLVM.

As far as I can see, it’s possible to use external linkage for functions and get it by its name. Problem is, since it’s a C++ method, its name is going to be mangled, so I don’t think it’s a good idea to go that way.

Making the FunctionType object is easy enough. But from there, how can I inform LLVM of my method and get a Function object for it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:12:22+00:00Added an answer on May 15, 2026 at 2:12 pm

    The dudes from the LLVM mailing list were helpful enough to provide a better solution. They didn’t say how to get the pointer from the method to the function, but I’ve already figured out this part so it’s okay.

    EDIT A clean way to do this is simply to wrap your method into a function:

    int Foo_Bar(Foo* foo)
    {
        return foo->bar();
    }
    

    Then use Foo_Bar‘s address instead of trying to get Foo::bar‘s. Use llvm::ExecutionEngine::addGlobalMapping to add the mapping as shown below.

    As usual, the simplest solution has some interesting benefits. For instance, it works with virtual functions without a hiccup. (But it’s so much less entertaining. The rest of the answer is kept for historical purposes, mainly because I had a lot of fun poking at the internals of my C++ runtime. Also note that it’s non-portable.)


    You’ll need something along these lines to figure the address of a method (be warned, that’s a dirty hack that probably will only be compatible with the Itanium ABI):

    template<typename T>
    const void* void_cast(const T& object)
    {
        union Retyper
        {
            const T object;
            void* pointer;
            Retyper(T obj) : object(obj) { }
        };
    
        return Retyper(object).pointer;
    }
    
    template<typename T, typename M>
    const void* getMethodPointer(const T* object, M method) // will work for virtual methods
    {
        union MethodEntry
        {
            intptr_t offset;
            void* function;
        };
    
        const MethodEntry* entry = static_cast<const MethodEntry*>(void_cast(&method));
    
        if (entry->offset % sizeof(intptr_t) == 0) // looks like that's how the runtime guesses virtual from static
            return getMethodPointer(method);
    
        const void* const* const vtable = *reinterpret_cast<const void* const* const* const>(object);
        return vtable[(entry->offset - 1) / sizeof(void*)];
    }
    
    template<typename M>
    const void* getMethodPointer(M method) // will only work with non-virtual methods
    {
        union MethodEntry
        {
            intptr_t offset;
            void* function;
        };
    
        return static_cast<const MethodEntry*>(void_cast(&method))->function;
    }
    

    Then use llvm::ExecutionEngine::addGlobalMapping to map a function to the address you’ve gotten. To call it, pass it your object as the first parameter, and the rest as usual. Here’s a quick example.

    class Foo
    {
        void Bar();
        virtual void Baz();
    };
    
    class FooFoo : public Foo
    {
        virtual void Baz();
    };
    
    Foo* foo = new FooFoo;
    
    const void* barMethodPointer = getMethodPointer(&Foo::Bar);
    const void* bazMethodPointer = getMethodPointer(foo, &Foo::Baz); // will get FooFoo::Baz
    
    llvm::ExecutionEngine* engine = llvm::EngineBuilder(module).Create();
    
    llvm::Function* bar = llvm::Function::Create(/* function type */, Function::ExternalLinkage, "foo", module);
    llvm::Function* baz = llvm::Function::Create(/* function type */, Function::ExternalLinkage, "baz", module);
    engine->addGlobalMapping(bar, const_cast<void*>(barMethodPointer)); // LLVM always takes non-const pointers
    engine->addGlobalMapping(baz, const_cast<void*>(bazMethodPointer));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Couldn't you just re-render the partial in your rjs template?… May 15, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer Fascinated to hear of Enlive. I've been thinking about this… May 15, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer Maybe you are looking for something like this: http://jsfiddle.net/JSvSn/1/ You… May 15, 2026 at 8:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.