Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <limits.h>
- #include <llvm-c/Core.h>
- #include <llvm-c/ExecutionEngine.h>
- int main()
- {
- LLVMModuleRef module;
- module = LLVMModuleCreateWithName("Say hello to LLVM");
- LLVMTypeRef putsArguments[] = { LLVMPointerType(LLVMInt8Type(), 0), };
- LLVMTypeRef putsFunctionType;
- putsFunctionType = LLVMFunctionType(
- /* Return type */ LLVMIntType(sizeof(int) * CHAR_BIT),
- /* Parameters */ putsArguments,
- /* Param count */ 1,
- /* Var Args ? */ 0);
- LLVMValueRef putsFunction;
- putsFunction = LLVMAddFunction(module, "puts", putsFunctionType);
- LLVMTypeRef functionType;
- LLVMValueRef function;
- functionType = LLVMFunctionType(
- /* Return type */ LLVMVoidType(),
- /* Parameters */ NULL,
- /* Param count */ 0,
- /* Var Args ? */ 0);
- function = LLVMAddFunction(module, "sayHelloWorld", functionType);
- LLVMBasicBlockRef block = LLVMAppendBasicBlock(function, "aName");
- LLVMBuilderRef builder = LLVMCreateBuilder();
- LLVMPositionBuilderAtEnd(builder, block);
- LLVMValueRef callArguments[] = { LLVMBuildGlobalStringPtr(builder, "Hello LLVM-C world!", ""),};
- LLVMBuildCall(builder, putsFunction, callArguments, 1, "");
- LLVMBuildRetVoid(builder);
- LLVMDisposeBuilder(builder);
- LLVMInitializeNativeTarget();
- LLVMLinkInJIT();
- LLVMExecutionEngineRef engine;
- void (*myHello)(void);
- if(LLVMCreateJITCompilerForModule(&engine,
- module, 0, NULL) == 0)
- {
- myHello = (void (*)(void))LLVMGetPointerToGlobal(engine, function);
- printf("Hello world!\n");
- myHello();
- }
- LLVMDumpModule(module);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement