Advertisement
Guest User

Untitled

a guest
Sep 4th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include "llvm-c/Core.h"
  4. #include "llvm-c/ExecutionEngine.h"
  5.  
  6. int main() {
  7.     LLVMContextRef llvmCtx = LLVMContextCreate();
  8.     LLVMBuilderRef builder = LLVMCreateBuilderInContext(llvmCtx);
  9.     LLVMModuleRef m1 = LLVMModuleCreateWithNameInContext("m1", llvmCtx);
  10.  
  11.     LLVMInitializeX86Target();
  12.     LLVMInitializeX86TargetInfo();
  13.     LLVMInitializeX86TargetMC();
  14.     LLVMInitializeX86AsmPrinter();
  15.  
  16.     LLVMLinkInMCJIT();
  17.  
  18.     char* errorPtr;
  19.     LLVMExecutionEngineRef jit;
  20.     if (LLVMCreateMCJITCompilerForModule(&jit, m1, NULL, 0, &errorPtr)) {
  21.         printf("%s\n", errorPtr);
  22.         LLVMDisposeMessage(errorPtr);
  23.  
  24.         return 1;
  25.     }
  26.  
  27.     LLVMModuleRef m2 = LLVMModuleCreateWithNameInContext("m2", llvmCtx);
  28.     LLVMAddModule(jit, m2);
  29.  
  30.     LLVMTypeRef type = LLVMInt32TypeInContext(llvmCtx);
  31.     LLVMTypeRef funType = LLVMFunctionType(type, NULL, 0, 0);
  32.     LLVMValueRef fun = LLVMAddFunction(m2, "foo", funType);
  33.  
  34.     LLVMBasicBlockRef bodyBB = LLVMAppendBasicBlockInContext(llvmCtx, fun, "");
  35.     LLVMPositionBuilderAtEnd(builder, bodyBB);
  36.     LLVMBuildRet(builder, LLVMConstInt(type, 42, 0));
  37.  
  38.     LLVMDumpModule(m1);
  39.     LLVMDumpModule(m2);
  40.  
  41.     LLVMGenericValueRef result = LLVMRunFunction(jit, fun, 0, NULL);
  42.     printf("%d\n", LLVMGenericValueToInt(result, 0));
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement