xo-jit: + 2 examples
This commit is contained in:
parent
e246f12d70
commit
2f593d15d5
5 changed files with 241 additions and 0 deletions
12
example/ex3_fptr/CMakeLists.txt
Normal file
12
example/ex3_fptr/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# xo-jit/example/ex3_fptr/CMakeLists.txt
|
||||
|
||||
set(SELF_EXE xo_fptr_ex3)
|
||||
set(SELF_SRCS ex3_fptr.cpp)
|
||||
|
||||
if (XO_ENABLE_EXAMPLES)
|
||||
xo_add_executable(${SELF_EXE} ${SELF_SRCS})
|
||||
xo_self_dependency(${SELF_EXE} xo_jit)
|
||||
#xo_dependency(${SELF_EXE} xo_expression)
|
||||
endif()
|
||||
|
||||
# end CMakeLists.txt
|
||||
45
example/ex3_fptr/ex3_fptr.cpp
Normal file
45
example/ex3_fptr/ex3_fptr.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/IR/IRBuilder.h"
|
||||
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
int main() {
|
||||
llvm::LLVMContext context;
|
||||
llvm::IRBuilder<> builder(context);
|
||||
llvm::Module *module = new llvm::Module("top", context);
|
||||
|
||||
// Create main function and basic block
|
||||
llvm::FunctionType *functionType = llvm::FunctionType::get(builder.getInt32Ty(), false);
|
||||
llvm::Function *mainFunction = llvm::Function::Create(functionType, llvm::Function::ExternalLinkage, "main", module);
|
||||
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entrypoint", mainFunction);
|
||||
builder.SetInsertPoint(entry);
|
||||
|
||||
// Create a global string pointer
|
||||
llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world\n");
|
||||
|
||||
// Create function pointer for puts
|
||||
std::vector<llvm::Type *> putArgs;
|
||||
putArgs.push_back(builder.getInt8Ty()->getPointerTo());
|
||||
llvm::ArrayRef<llvm::Type *> argsRef(putArgs);
|
||||
llvm::FunctionType *putsType = llvm::FunctionType::get(builder.getInt32Ty(), argsRef, false);
|
||||
/* = FunctionType + Callee-pointer */
|
||||
llvm::FunctionCallee putFunction_callee = module->getOrInsertFunction("puts", putsType);
|
||||
|
||||
#ifdef NOT_YET
|
||||
llvm::Constant * putFunction = llvm::Constant
|
||||
|
||||
// Allocate memory for the function pointer
|
||||
llvm::Value *p = builder.CreateAlloca(putFunction->getType(), nullptr, "p");
|
||||
builder.CreateStore(putFunction, p, false);
|
||||
|
||||
// Load the function pointer and call it
|
||||
llvm::Value *temp = builder.CreateLoad(p);
|
||||
builder.CreateCall(temp, helloWorld);
|
||||
|
||||
// Return 0 to complete the main function
|
||||
builder.CreateRet(llvm::ConstantInt::get(builder.getInt32Ty(), 0));
|
||||
|
||||
// Print the module (IR code)
|
||||
module->print(llvm::errs(), nullptr);
|
||||
#endif
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue