SHARE
TWEET

Untitled

a guest Jan 1st, 2011 827 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <llvm-c/Core.h>
  4. #include <llvm-c/ExecutionEngine.h>
  5.  
  6. int main()
  7. {
  8.   LLVMModuleRef module;
  9.   module = LLVMModuleCreateWithName("Say hello to LLVM");
  10.  
  11.   LLVMTypeRef putsArguments[] = { LLVMPointerType(LLVMInt8Type(), 0), };
  12.  
  13.   LLVMTypeRef putsFunctionType;
  14.   putsFunctionType = LLVMFunctionType(
  15.        /* Return type */ LLVMIntType(sizeof(int) * CHAR_BIT),
  16.        /* Parameters */ putsArguments,
  17.        /* Param count */ 1,
  18.        /* Var Args ? */ 0);
  19.  
  20.   LLVMValueRef putsFunction;
  21.   putsFunction = LLVMAddFunction(module, "puts", putsFunctionType);
  22.  
  23.   LLVMTypeRef functionType;
  24.   LLVMValueRef function;
  25.   functionType = LLVMFunctionType(
  26.     /* Return type */ LLVMVoidType(),
  27.     /* Parameters */ NULL,
  28.     /* Param count */ 0,
  29.     /* Var Args ? */ 0);
  30.  
  31.   function = LLVMAddFunction(module, "sayHelloWorld", functionType);
  32.  
  33.   LLVMBasicBlockRef block = LLVMAppendBasicBlock(function, "aName");
  34.  
  35.   LLVMBuilderRef builder = LLVMCreateBuilder();
  36.  
  37.   LLVMPositionBuilderAtEnd(builder, block);
  38.  
  39.   LLVMValueRef callArguments[] = { LLVMBuildGlobalStringPtr(builder, "Hello LLVM-C world!", ""),};
  40.  
  41.   LLVMBuildCall(builder, putsFunction, callArguments, 1, "");
  42.   LLVMBuildRetVoid(builder);
  43.  
  44.   LLVMDisposeBuilder(builder);
  45.  
  46.   LLVMInitializeNativeTarget();
  47.   LLVMLinkInJIT();
  48.  
  49.   LLVMExecutionEngineRef engine;
  50.   void (*myHello)(void);
  51.  
  52.   if(LLVMCreateJITCompilerForModule(&engine,
  53.                                     module, 0, NULL) == 0)
  54.   {
  55.     myHello = (void (*)(void))LLVMGetPointerToGlobal(engine, function);
  56.  
  57.     printf("Hello world!\n");
  58.  
  59.     myHello();
  60.   }
  61.  
  62.   LLVMDumpModule(module);
  63.  
  64.   return 0;
  65. }
RAW Paste Data
Want to get better at C?
Learn to code C in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top