#include "llvm-c/Core.h"
#include "llvm-c/Error.h"
#include "llvm-c/Initialization.h"
#include "llvm-c/LLJIT.h"
#include "llvm-c/Support.h"
#include "llvm-c/Target.h"
#include <assert.h>
#include <stdio.h>
int handleError(LLVMErrorRef Err) {
char *ErrMsg = LLVMGetErrorMessage(Err);
fprintf(stderr, "Error: %s\n", ErrMsg);
LLVMDisposeErrorMessage(ErrMsg);
return 1;
}
int32_t add(int32_t X, int32_t Y) { return X + Y; }
int32_t mul(int32_t X, int32_t Y) { return X * Y; }
int allowedSymbols(void *Ctx, LLVMOrcSymbolStringPoolEntryRef Sym) {
assert(Ctx && "Cannot call allowedSymbols with a null context");
LLVMOrcSymbolStringPoolEntryRef *AllowList =
(LLVMOrcSymbolStringPoolEntryRef *)Ctx;
LLVMOrcSymbolStringPoolEntryRef *P = AllowList;
while (*P) {
if (Sym == *P)
return 1;
++P;
}
return 0;
}
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
LLVMTypeRef I32BinOpParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
LLVMTypeRef I32BinOpFunctionType =
LLVMFunctionType(LLVMInt32Type(), I32BinOpParamTypes, 2, 0);
LLVMValueRef AddI32Function = LLVMAddFunction(M, "add", I32BinOpFunctionType);
LLVMValueRef MulI32Function = LLVMAddFunction(M, "mul", I32BinOpFunctionType);
LLVMTypeRef MulAddParamTypes[] = {LLVMInt32Type(), LLVMInt32Type(),
LLVMInt32Type()};
LLVMTypeRef MulAddFunctionType =
LLVMFunctionType(LLVMInt32Type(), MulAddParamTypes, 3, 0);
LLVMValueRef MulAddFunction =
LLVMAddFunction(M, "mul_add", MulAddFunctionType);
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(MulAddFunction, "entry");
LLVMBuilderRef Builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(Builder, EntryBB);
LLVMValueRef SumArg0 = LLVMGetParam(MulAddFunction, 0);
LLVMValueRef SumArg1 = LLVMGetParam(MulAddFunction, 1);
LLVMValueRef SumArg2 = LLVMGetParam(MulAddFunction, 2);
LLVMValueRef MulArgs[] = {SumArg0, SumArg1};
LLVMValueRef MulResult = LLVMBuildCall2(Builder, I32BinOpFunctionType,
MulI32Function, MulArgs, 2, "t");
LLVMValueRef AddArgs[] = {MulResult, SumArg2};
LLVMValueRef AddResult = LLVMBuildCall2(Builder, I32BinOpFunctionType,
AddI32Function, AddArgs, 2, "r");
LLVMBuildRet(Builder, AddResult);
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
LLVMOrcDisposeThreadSafeContext(TSCtx);
return TSM;
}
int main(int argc, char *argv[]) {
int MainResult = 0;
LLVMParseCommandLineOptions(argc, (const char **)argv, "");
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMOrcLLJITRef J;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
MainResult = handleError(Err);
goto llvm_shutdown;
}
}
LLVMOrcSymbolStringPoolEntryRef AllowList[] = {
LLVMOrcLLJITMangleAndIntern(J, "mul"),
LLVMOrcLLJITMangleAndIntern(J, "add"), 0};
{
LLVMOrcDefinitionGeneratorRef ProcessSymbolsGenerator = 0;
LLVMErrorRef Err;
if ((Err = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
&ProcessSymbolsGenerator, LLVMOrcLLJITGetGlobalPrefix(J),
allowedSymbols, AllowList))) {
MainResult = handleError(Err);
goto jit_cleanup;
}
LLVMOrcJITDylibAddGenerator(LLVMOrcLLJITGetMainJITDylib(J),
ProcessSymbolsGenerator);
}
LLVMOrcThreadSafeModuleRef TSM = createDemoModule();
{
LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITAddLLVMIRModule(J, MainJD, TSM))) {
LLVMOrcDisposeThreadSafeModule(TSM);
MainResult = handleError(Err);
goto jit_cleanup;
}
}
LLVMOrcJITTargetAddress MulAddAddr;
{
LLVMErrorRef Err;
if ((Err = LLVMOrcLLJITLookup(J, &MulAddAddr, "mul_add"))) {
MainResult = handleError(Err);
goto jit_cleanup;
}
}
int32_t (*MulAdd)(int32_t, int32_t, int32_t) =
(int32_t(*)(int32_t, int32_t, int32_t))MulAddAddr;
int32_t Result = MulAdd(3, 4, 5);
printf("3 * 4 + 5 = %i\n", Result);
jit_cleanup:
{
LLVMOrcSymbolStringPoolEntryRef *P = AllowList;
while (*P)
LLVMOrcReleaseSymbolStringPoolEntry(*P++);
}
{
LLVMErrorRef Err;
if ((Err = LLVMOrcDisposeLLJIT(J))) {
int NewFailureResult = handleError(Err);
if (MainResult == 0)
MainResult = NewFailureResult;
}
}
llvm_shutdown:
LLVMShutdown();
return MainResult;
}