//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // WARNING: This example demonstrates how to use LLVM's older ExecutionEngine // JIT APIs. The newer LLJIT APIs should be preferred for new // projects. See llvm/examples/HowToUseLLJIT. // // This small program provides an example of how to quickly build a small // module with two functions and execute it with the JIT. // // Goal: // The goal of this snippet is to create in the memory // the LLVM module consisting of two functions as follow: // // int add1(int x) { // return x+1; // } // // int foo() { // return add1(10); // } // // then compile the module via JIT, then execute the `foo' // function and return result to a driver, i.e. to a "host program". // // Some remarks and questions: // // - could we invoke some code using noname functions too? // e.g. evaluate "foo()+foo()" without fears to introduce // conflict of temporary function name with some real // existing function name? // //===----------------------------------------------------------------------===// #include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/GenericValue.h" #include "llvm/ExecutionEngine/MCJIT.h" #include "llvm/IR/Argument.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cassert> #include <memory> #include <vector> usingnamespacellvm; int main() { … }