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/LLJIT.h"
12 #include "llvm-c/Support.h"
13 #include "llvm-c/Target.h"
14 #include "llvm-c/TargetMachine.h"
18 int handleError(LLVMErrorRef Err
) {
19 char *ErrMsg
= LLVMGetErrorMessage(Err
);
20 fprintf(stderr
, "Error: %s\n", ErrMsg
);
21 LLVMDisposeErrorMessage(ErrMsg
);
25 LLVMModuleRef
createDemoModule(LLVMContextRef Ctx
) {
26 // Create a new LLVM module.
27 LLVMModuleRef M
= LLVMModuleCreateWithNameInContext("demo", Ctx
);
29 // Add a "sum" function":
30 // - Create the function type and function instance.
31 LLVMTypeRef ParamTypes
[] = {LLVMInt32Type(), LLVMInt32Type()};
32 LLVMTypeRef SumFunctionType
=
33 LLVMFunctionType(LLVMInt32Type(), ParamTypes
, 2, 0);
34 LLVMValueRef SumFunction
= LLVMAddFunction(M
, "sum", SumFunctionType
);
36 // - Add a basic block to the function.
37 LLVMBasicBlockRef EntryBB
= LLVMAppendBasicBlock(SumFunction
, "entry");
39 // - Add an IR builder and point it at the end of the basic block.
40 LLVMBuilderRef Builder
= LLVMCreateBuilder();
41 LLVMPositionBuilderAtEnd(Builder
, EntryBB
);
43 // - Get the two function arguments and use them co construct an "add"
45 LLVMValueRef SumArg0
= LLVMGetParam(SumFunction
, 0);
46 LLVMValueRef SumArg1
= LLVMGetParam(SumFunction
, 1);
47 LLVMValueRef Result
= LLVMBuildAdd(Builder
, SumArg0
, SumArg1
, "result");
49 // - Build the return instruction.
50 LLVMBuildRet(Builder
, Result
);
52 // - Free the builder.
53 LLVMDisposeBuilder(Builder
);
58 int main(int argc
, const char *argv
[]) {
62 // Parse command line arguments and initialize LLVM Core.
63 LLVMParseCommandLineOptions(argc
, argv
, "");
65 // Initialize native target codegen and asm printer.
66 LLVMInitializeNativeTarget();
67 LLVMInitializeNativeAsmPrinter();
69 // Create the JIT instance.
73 if ((Err
= LLVMOrcCreateLLJIT(&J
, 0))) {
74 MainResult
= handleError(Err
);
79 // Create our demo object file.
80 LLVMMemoryBufferRef ObjectFileBuffer
;
83 LLVMContextRef Ctx
= LLVMContextCreate();
84 LLVMModuleRef M
= createDemoModule(Ctx
);
87 const char *Triple
= LLVMOrcLLJITGetTripleString(J
);
88 LLVMTargetRef Target
= 0;
90 if (LLVMGetTargetFromTriple(Triple
, &Target
, &ErrorMsg
)) {
91 fprintf(stderr
, "Error getting target for %s: %s\n", Triple
, ErrorMsg
);
93 LLVMContextDispose(Ctx
);
97 // Construct a TargetMachine.
98 LLVMTargetMachineRef TM
=
99 LLVMCreateTargetMachine(Target
, Triple
, "", "", LLVMCodeGenLevelNone
,
100 LLVMRelocDefault
, LLVMCodeModelDefault
);
102 // Run CodeGen to produce the buffer.
103 if (LLVMTargetMachineEmitToMemoryBuffer(TM
, M
, LLVMObjectFile
, &ErrorMsg
,
104 &ObjectFileBuffer
)) {
105 fprintf(stderr
, "Error emitting object: %s\n", ErrorMsg
);
106 LLVMDisposeTargetMachine(TM
);
107 LLVMDisposeModule(M
);
108 LLVMContextDispose(Ctx
);
112 // CodeGen succeeded -- We have our module, so free the Module, LLVMContext,
113 // and TargetMachine.
114 LLVMDisposeModule(M
);
115 LLVMContextDispose(Ctx
);
116 LLVMDisposeTargetMachine(TM
);
119 // Add our object file buffer to the JIT.
121 LLVMOrcJITDylibRef MainJD
= LLVMOrcLLJITGetMainJITDylib(J
);
123 if ((Err
= LLVMOrcLLJITAddObjectFile(J
, MainJD
, ObjectFileBuffer
))) {
124 MainResult
= handleError(Err
);
129 // Look up the address of our demo entry point.
130 LLVMOrcJITTargetAddress SumAddr
;
133 if ((Err
= LLVMOrcLLJITLookup(J
, &SumAddr
, "sum"))) {
134 MainResult
= handleError(Err
);
139 // If we made it here then everything succeeded. Execute our JIT'd code.
140 int32_t (*Sum
)(int32_t, int32_t) = (int32_t(*)(int32_t, int32_t))SumAddr
;
141 int32_t Result
= Sum(1, 2);
144 printf("1 + 2 = %i\n", Result
);
147 // Destroy our JIT instance. This will clean up any memory that the JIT has
148 // taken ownership of. This operation is non-trivial (e.g. it may need to
149 // JIT static destructors) and may also fail. In that case we want to render
150 // the error to stderr, but not overwrite any existing return value.
153 if ((Err
= LLVMOrcDisposeLLJIT(J
))) {
154 int NewFailureResult
= handleError(Err
);
156 MainResult
= NewFailureResult
;