SHARE
TWEET

Untitled

a guest Dec 10th, 2018 132 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     extern (C) void hello() {
  2.         import core.stdc.stdio : printf;
  3.  
  4.         printf("Hello there!");
  5.     }
  6.  
  7.     private void testFunctionPass() {
  8.         auto printfType = LLVMFunctionType(LLVMInt32Type(),
  9.                 [LLVMPointerType(LLVMInt8Type(), 0)].ptr, 1, 1);
  10.         auto llvmPrintf = LLVMAddFunction(moduleBuilder.module_, "printf", printfType);
  11.  
  12.         auto passedType = LLVMFunctionType(LLVMVoidType(), null, 0, 0);
  13.         auto passedPtrType = LLVMPointerType(passedType, 0);
  14.         auto testFunctionType = LLVMFunctionType(LLVMVoidType(), &passedPtrType, 1, 0);
  15.         auto testFunction = LLVMAddFunction(moduleBuilder.module_, "fp_pass_test", testFunctionType);
  16.         auto entryBlock = LLVMAppendBasicBlock(testFunction, "entry");
  17.         auto builder = LLVMCreateBuilder();
  18.         auto paramValue = LLVMGetParam(testFunction, 0);
  19.         LLVMPositionBuilderAtEnd(builder, entryBlock);
  20.  
  21.         auto pointerPrintFormat = LLVMBuildGlobalStringPtr(builder, "received ptr: %x\n", "");
  22.         LLVMBuildCall(builder, llvmPrintf, [pointerPrintFormat, paramValue].ptr, 2, "");
  23.  
  24.         LLVMBuildCall(builder, paramValue, null, 0, "");
  25.         LLVMBuildRetVoid(builder);
  26.         LLVMDisposeBuilder(builder);
  27.         LLVMVerifyModule(moduleBuilder.module_, LLVMPrintMessageAction, null);
  28.         LLVMDumpValue(testFunction);
  29.         void* passedPtr = &hello;
  30.         LLVMGenericValueRef passedPtrGeneric = LLVMCreateGenericValueOfPointer(passedPtr);
  31.         auto unwrapped = LLVMGenericValueToPointer(passedPtrGeneric);
  32.         import core.stdc.stdio : printf;
  33.  
  34.         printf("passed ptr: %x\n", passedPtr);
  35.         printf("unwrapped ptr: %x\n", unwrapped);
  36.  
  37.         LLVMRunFunction(interpreter, testFunction, 1, &passedPtrGeneric);
  38.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top