-
Notifications
You must be signed in to change notification settings - Fork 4
/
zero-to-rust-jit.c
35 lines (26 loc) · 1003 Bytes
/
zero-to-rust-jit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "zero-to-rust-jit.h"
void helloImpl(void) { printf("Oh hello, that's called from JITed code!\n"); }
LLVMOrcJITTargetAddress handleUndefinedSymbol(const char *MangledName) {
if (strncmp(MangledName, "hello", 5) == 0)
return (LLVMOrcJITTargetAddress)&helloImpl;
return 0;
}
int main(int argc, const char *argv[]) {
// Init our bitcode JIT compiler and the LLVM context
LLVMOrcLLJITRef Jit;
LLVMOrcThreadSafeContextRef Ctx;
const char *FileName = init(argc, argv, &Jit, &Ctx);
// Create our demo function
LLVMModuleRef Mod = loadModule(FileName, Ctx);
LLVMOrcJITDylibRef Unit = addModule(Jit, Mod);
// Install our symbol resolver
addGenerator(Unit, &handleUndefinedSymbol);
// Materialize our demo function
LLVMErrorRef Err;
LLVMOrcJITTargetAddress SumFnAddr;
if ((Err = LLVMOrcLLJITLookup(Jit, &SumFnAddr, "sum")))
shutdown(handleError(Err)); // noreturn
// Execute our JITed code
loop((int (*)(int, int))SumFnAddr);
shutdown(EXIT_SUCCESS);
}