10d5f15f7SLang Hames //===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
20d5f15f7SLang Hames //
30d5f15f7SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40d5f15f7SLang Hames // See https://llvm.org/LICENSE.txt for license information.
50d5f15f7SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60d5f15f7SLang Hames //
70d5f15f7SLang Hames //===----------------------------------------------------------------------===//
80d5f15f7SLang Hames
90d5f15f7SLang Hames #include "llvm-c/Core.h"
100d5f15f7SLang Hames #include "llvm-c/Error.h"
110d5f15f7SLang Hames #include "llvm-c/Initialization.h"
12*f3570704SLang Hames #include "llvm-c/LLJIT.h"
130d5f15f7SLang Hames #include "llvm-c/Support.h"
140d5f15f7SLang Hames #include "llvm-c/Target.h"
150d5f15f7SLang Hames #include "llvm-c/TargetMachine.h"
160d5f15f7SLang Hames
170d5f15f7SLang Hames #include <stdio.h>
180d5f15f7SLang Hames
handleError(LLVMErrorRef Err)190d5f15f7SLang Hames int handleError(LLVMErrorRef Err) {
200d5f15f7SLang Hames char *ErrMsg = LLVMGetErrorMessage(Err);
210d5f15f7SLang Hames fprintf(stderr, "Error: %s\n", ErrMsg);
220d5f15f7SLang Hames LLVMDisposeErrorMessage(ErrMsg);
230d5f15f7SLang Hames return 1;
240d5f15f7SLang Hames }
250d5f15f7SLang Hames
createDemoModule(LLVMContextRef Ctx)260d5f15f7SLang Hames LLVMModuleRef createDemoModule(LLVMContextRef Ctx) {
270d5f15f7SLang Hames // Create a new LLVM module.
280d5f15f7SLang Hames LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
290d5f15f7SLang Hames
300d5f15f7SLang Hames // Add a "sum" function":
310d5f15f7SLang Hames // - Create the function type and function instance.
320d5f15f7SLang Hames LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
330d5f15f7SLang Hames LLVMTypeRef SumFunctionType =
340d5f15f7SLang Hames LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
350d5f15f7SLang Hames LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
360d5f15f7SLang Hames
370d5f15f7SLang Hames // - Add a basic block to the function.
380d5f15f7SLang Hames LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
390d5f15f7SLang Hames
400d5f15f7SLang Hames // - Add an IR builder and point it at the end of the basic block.
410d5f15f7SLang Hames LLVMBuilderRef Builder = LLVMCreateBuilder();
420d5f15f7SLang Hames LLVMPositionBuilderAtEnd(Builder, EntryBB);
430d5f15f7SLang Hames
440d5f15f7SLang Hames // - Get the two function arguments and use them co construct an "add"
450d5f15f7SLang Hames // instruction.
460d5f15f7SLang Hames LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
470d5f15f7SLang Hames LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
480d5f15f7SLang Hames LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
490d5f15f7SLang Hames
500d5f15f7SLang Hames // - Build the return instruction.
510d5f15f7SLang Hames LLVMBuildRet(Builder, Result);
520d5f15f7SLang Hames
530d5f15f7SLang Hames return M;
540d5f15f7SLang Hames }
550d5f15f7SLang Hames
main(int argc,char * argv[])560d5f15f7SLang Hames int main(int argc, char *argv[]) {
570d5f15f7SLang Hames
580d5f15f7SLang Hames int MainResult = 0;
590d5f15f7SLang Hames
600d5f15f7SLang Hames // Parse command line arguments and initialize LLVM Core.
610d5f15f7SLang Hames LLVMParseCommandLineOptions(argc, (const char **)argv, "");
620d5f15f7SLang Hames LLVMInitializeCore(LLVMGetGlobalPassRegistry());
630d5f15f7SLang Hames
640d5f15f7SLang Hames // Initialize native target codegen and asm printer.
650d5f15f7SLang Hames LLVMInitializeNativeTarget();
660d5f15f7SLang Hames LLVMInitializeNativeAsmPrinter();
670d5f15f7SLang Hames
680d5f15f7SLang Hames // Create the JIT instance.
690d5f15f7SLang Hames LLVMOrcLLJITRef J;
700d5f15f7SLang Hames {
710d5f15f7SLang Hames LLVMErrorRef Err;
720d5f15f7SLang Hames if ((Err = LLVMOrcCreateLLJIT(&J, 0))) {
730d5f15f7SLang Hames MainResult = handleError(Err);
740d5f15f7SLang Hames goto llvm_shutdown;
750d5f15f7SLang Hames }
760d5f15f7SLang Hames }
770d5f15f7SLang Hames
780d5f15f7SLang Hames // Create our demo object file.
790d5f15f7SLang Hames LLVMMemoryBufferRef ObjectFileBuffer;
800d5f15f7SLang Hames {
810d5f15f7SLang Hames // Create a module.
820d5f15f7SLang Hames LLVMContextRef Ctx = LLVMContextCreate();
830d5f15f7SLang Hames LLVMModuleRef M = createDemoModule(Ctx);
840d5f15f7SLang Hames
850d5f15f7SLang Hames // Get the Target.
860d5f15f7SLang Hames const char *Triple = LLVMOrcLLJITGetTripleString(J);
870d5f15f7SLang Hames LLVMTargetRef Target = 0;
880d5f15f7SLang Hames char *ErrorMsg = 0;
890d5f15f7SLang Hames if (LLVMGetTargetFromTriple(Triple, &Target, &ErrorMsg)) {
900d5f15f7SLang Hames fprintf(stderr, "Error getting target for %s: %s\n", Triple, ErrorMsg);
910d5f15f7SLang Hames LLVMDisposeModule(M);
920d5f15f7SLang Hames LLVMContextDispose(Ctx);
930d5f15f7SLang Hames goto jit_cleanup;
940d5f15f7SLang Hames }
950d5f15f7SLang Hames
960d5f15f7SLang Hames // Construct a TargetMachine.
970d5f15f7SLang Hames LLVMTargetMachineRef TM =
980d5f15f7SLang Hames LLVMCreateTargetMachine(Target, Triple, "", "", LLVMCodeGenLevelNone,
990d5f15f7SLang Hames LLVMRelocDefault, LLVMCodeModelDefault);
1000d5f15f7SLang Hames
1010d5f15f7SLang Hames // Run CodeGen to produce the buffer.
1020d5f15f7SLang Hames if (LLVMTargetMachineEmitToMemoryBuffer(TM, M, LLVMObjectFile, &ErrorMsg,
1030d5f15f7SLang Hames &ObjectFileBuffer)) {
1040d5f15f7SLang Hames fprintf(stderr, "Error emitting object: %s\n", ErrorMsg);
1050d5f15f7SLang Hames LLVMDisposeTargetMachine(TM);
1060d5f15f7SLang Hames LLVMDisposeModule(M);
1070d5f15f7SLang Hames LLVMContextDispose(Ctx);
1080d5f15f7SLang Hames goto jit_cleanup;
1090d5f15f7SLang Hames }
1100d5f15f7SLang Hames }
1110d5f15f7SLang Hames
1120d5f15f7SLang Hames // Add our object file buffer to the JIT.
1130d5f15f7SLang Hames {
11437bcf2dfSLang Hames LLVMOrcJITDylibRef MainJD = LLVMOrcLLJITGetMainJITDylib(J);
1150d5f15f7SLang Hames LLVMErrorRef Err;
11637bcf2dfSLang Hames if ((Err = LLVMOrcLLJITAddObjectFile(J, MainJD, ObjectFileBuffer))) {
1170d5f15f7SLang Hames MainResult = handleError(Err);
1180d5f15f7SLang Hames goto jit_cleanup;
1190d5f15f7SLang Hames }
1200d5f15f7SLang Hames }
1210d5f15f7SLang Hames
1220d5f15f7SLang Hames // Look up the address of our demo entry point.
1230d5f15f7SLang Hames LLVMOrcJITTargetAddress SumAddr;
1240d5f15f7SLang Hames {
1250d5f15f7SLang Hames LLVMErrorRef Err;
1260d5f15f7SLang Hames if ((Err = LLVMOrcLLJITLookup(J, &SumAddr, "sum"))) {
1270d5f15f7SLang Hames MainResult = handleError(Err);
1280d5f15f7SLang Hames goto jit_cleanup;
1290d5f15f7SLang Hames }
1300d5f15f7SLang Hames }
1310d5f15f7SLang Hames
1320d5f15f7SLang Hames // If we made it here then everything succeeded. Execute our JIT'd code.
1330d5f15f7SLang Hames int32_t (*Sum)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr;
1340d5f15f7SLang Hames int32_t Result = Sum(1, 2);
1350d5f15f7SLang Hames
1360d5f15f7SLang Hames // Print the result.
1370d5f15f7SLang Hames printf("1 + 2 = %i\n", Result);
1380d5f15f7SLang Hames
1390d5f15f7SLang Hames jit_cleanup:
1400d5f15f7SLang Hames // Destroy our JIT instance. This will clean up any memory that the JIT has
1410d5f15f7SLang Hames // taken ownership of. This operation is non-trivial (e.g. it may need to
1420d5f15f7SLang Hames // JIT static destructors) and may also fail. In that case we want to render
1430d5f15f7SLang Hames // the error to stderr, but not overwrite any existing return value.
1440d5f15f7SLang Hames {
1450d5f15f7SLang Hames LLVMErrorRef Err;
1460d5f15f7SLang Hames if ((Err = LLVMOrcDisposeLLJIT(J))) {
1470d5f15f7SLang Hames int NewFailureResult = handleError(Err);
1480d5f15f7SLang Hames if (MainResult == 0)
1490d5f15f7SLang Hames MainResult = NewFailureResult;
1500d5f15f7SLang Hames }
1510d5f15f7SLang Hames }
1520d5f15f7SLang Hames
1530d5f15f7SLang Hames llvm_shutdown:
1540d5f15f7SLang Hames // Shut down LLVM.
1550d5f15f7SLang Hames LLVMShutdown();
1560d5f15f7SLang Hames
1570d5f15f7SLang Hames return MainResult;
1580d5f15f7SLang Hames }
159