# How does JIT compilation and hot-swapping work at the level of individual procedures?

I am trying to understand the lifetime of a procedure, from source code through to being compiled to machine code and being called. I am specifically interested in the context of "live" environments, where (re)compilation can happen at any point during runtime and the new code is seamlessly integrated into an already running program. Good examples of such systems are the SBCL Common Lisp implementation and the live coding language/environment Extempore, meant for live music or otherwise creative performance through live coding.

When a runtime supports the ability to (re)compile a procedure and hot-swap the old version with the new, I assume that involves changing a function pointer to point from one implementation to another. That must mean that the compiled function (in its machine-code form) is simply a sequence of bytes in memory.

That must also mean that the current process must have access to the memory that holds the compiled function. In the case where the compiler lives in the same process, that seems to be straight-forward, as the process already has access to the compiled code data. In the case where the compiler is an external process, the resulting data would somehow have to be loaded. Is that the same thing as dynamically linking with a pre-compiled library object, or is the process a fundamentally different one?

Lastly, what are the steps required to be able to get to a minimal PoC program like the one in the following C++ pseudocode?

// The type that the dynamically compiled procedure will have
using FooType = std::function<int(int)>;

// Assuming that the source code is valid,
// this would return a pointer to compiled machine code
FooType* compile_footype_proc(std::string source_code) {...}

int main()
{
FooType* foo;

while (!should_exit())
{
// Get some source, compile it, and call it
std::string user_provided_source = getline();
foo = compile_footype_proc(user_provided_source);
cout << "The result is: " << foo(5) << endl;
}
return 0;
}


• I don't understand what your second question is asking. I suggest asking it separately and explain in more detail what you are asking. I don't know what you mean by "get a minimal PoC program like ...", and I'm not sure what kind of answer you are hoping for.
– D.W.
Feb 22 at 5:11
• Wikipedia is your best friend. en.wikipedia.org/wiki/Just-in-time_compilation Mar 24 at 8:30