1 //===-------- BasicOrcV2CBindings.c - Basic OrcV2 C Bindings Demo ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm-c/Core.h"
10 #include "llvm-c/Error.h"
11 #include "llvm-c/Initialization.h"
12 #include "llvm-c/LLJIT.h"
13 #include "llvm-c/Support.h"
14 #include "llvm-c/Target.h"
15 #include "llvm-c/TargetMachine.h"
19 int handleError(LLVMErrorRef Err
) {
20 char *ErrMsg
= LLVMGetErrorMessage(Err
);
21 fprintf(stderr
, "Error: %s\n", ErrMsg
);
22 LLVMDisposeErrorMessage(ErrMsg
);
26 LLVMModuleRef
createDemoModule(LLVMContextRef Ctx
) {
27 // Create a new LLVM module.
28 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext("demo", Ctx
);
30 // Add a "sum" function":
31 // - Create the function type and function instance.
32 LLVMTypeRef ParamTypes
[] = {LLVMInt32Type(), LLVMInt32Type()};
33 LLVMTypeRef SumFunctionType
=
34 LLVMFunctionType(LLVMInt32Type(), ParamTypes
, 2, 0);
35 LLVMValueRef SumFunction
= LLVMAddFunction(M
, "sum", SumFunctionType
);
37 // - Add a basic block to the function.
38 LLVMBasicBlockRef EntryBB
= LLVMAppendBasicBlock(SumFunction
, "entry");
40 // - Add an IR builder and point it at the end of the basic block.
41 LLVMBuilderRef Builder
= LLVMCreateBuilder();
42 LLVMPositionBuilderAtEnd(Builder
, EntryBB
);
44 // - Get the two function arguments and use them co construct an "add"
46 LLVMValueRef SumArg0
= LLVMGetParam(SumFunction
, 0);
47 LLVMValueRef SumArg1
= LLVMGetParam(SumFunction
, 1);
48 LLVMValueRef Result
= LLVMBuildAdd(Builder
, SumArg0
, SumArg1
, "result");
50 // - Build the return instruction.
51 LLVMBuildRet(Builder
, Result
);
56 int main(int argc
, char *argv
[]) {
60 // Parse command line arguments and initialize LLVM Core.
61 LLVMParseCommandLineOptions(argc
, (const char **)argv
, "");
62 LLVMInitializeCore(LLVMGetGlobalPassRegistry());
64 // Initialize native target codegen and asm printer.
65 LLVMInitializeNativeTarget();
66 LLVMInitializeNativeAsmPrinter();
68 // Create the JIT instance.
72 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
73 MainResult
= handleError(Err
);
78 // Create our demo object file.
79 LLVMMemoryBufferRef ObjectFileBuffer
;
82 LLVMContextRef Ctx
= LLVMContextCreate();
83 LLVMModuleRef M
= createDemoModule(Ctx
);
86 const char *Triple
= LLVMOrcLLJITGetTripleString(J
);
87 LLVMTargetRef Target
= 0;
89 if (LLVMGetTargetFromTriple(Triple
, &Target
, &ErrorMsg
)) {
90 fprintf(stderr
, "Error getting target for %s: %s\n", Triple
, ErrorMsg
);
92 LLVMContextDispose(Ctx
);
96 // Construct a TargetMachine.
97 LLVMTargetMachineRef TM
=
98 LLVMCreateTargetMachine(Target
, Triple
, "", "", LLVMCodeGenLevelNone
,
99 LLVMRelocDefault
, LLVMCodeModelDefault
);
101 // Run CodeGen to produce the buffer.
102 if (LLVMTargetMachineEmitToMemoryBuffer(TM
, M
, LLVMObjectFile
, &ErrorMsg
,
103 &ObjectFileBuffer
)) {
104 fprintf(stderr
, "Error emitting object: %s\n", ErrorMsg
);
105 LLVMDisposeTargetMachine(TM
);
106 LLVMDisposeModule(M
);
107 LLVMContextDispose(Ctx
);
112 // Add our object file buffer to the JIT.
114 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
116 if ((Err
= LLVMOrcLLJITAddObjectFile(J
, MainJD
, ObjectFileBuffer
))) {
117 MainResult
= handleError(Err
);
122 // Look up the address of our demo entry point.
123 LLVMOrcJITTargetAddress SumAddr
;
126 if ((Err
= LLVMOrcLLJITLookup(J
, &SumAddr
, "sum"))) {
127 MainResult
= handleError(Err
);
132 // If we made it here then everything succeeded. Execute our JIT'd code.
133 int32_t (*Sum
)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr
;
134 int32_t Result
= Sum(1, 2);
137 printf("1 + 2 = %i\n", Result
);
140 // Destroy our JIT instance. This will clean up any memory that the JIT has
141 // taken ownership of. This operation is non-trivial (e.g. it may need to
142 // JIT static destructors) and may also fail. In that case we want to render
143 // the error to stderr, but not overwrite any existing return value.
146 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
147 int NewFailureResult
= handleError(Err
);
149 MainResult
= NewFailureResult
;