Advertisement
Guest User

evalString

a guest
Mar 26th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.97 KB | None | 0 0
  1.  string evalString(Expression e) in {
  2.         // FIXME: newtype
  3.         // assert(cast(SliceType) peelAlias(e.type).type, "this only CTFE strings.");
  4.     } body {
  5.         import std.stdio;
  6.  
  7.         // Create a global variable that recieve the string.
  8.         auto stringType = codeGen.visit(e.type);
  9.         auto receiver = LLVMAddGlobal(codeGen.dmodule, stringType, "__ctString");
  10.         scope(exit) LLVMDeleteGlobal(receiver);
  11.  
  12.         writeln("---1");
  13.  
  14.         LLVMValueRef[2] constInit = [ LLVMConstInt( LLVMInt64TypeInContext( codeGen.llvmCtx), 0, false),
  15.             LLVMConstNull( LLVMPointerType (LLVMInt8TypeInContext(codeGen.llvmCtx),0)) ];
  16.         LLVMDumpValue(LLVMGetUndef(stringType));
  17.         LLVMDumpValue(LLVMConstStruct( constInit.ptr, 2, false ));
  18.         LLVMDumpValue(receiver);
  19.         LLVMSetInitializer(receiver, LLVMConstStructInContext( codeGen.llvmCtx, constInit.ptr, 2, false ));
  20.         LLVMDumpValue(receiver);
  21.         writeln("---2");
  22.  
  23.         LLVMTypeRef[1] paramTypes = [LLVMInt8TypeInContext(codeGen.llvmCtx)];
  24.         writeln("---2.2");
  25.  
  26.         auto funType = LLVMFunctionType(LLVMVoidTypeInContext(codeGen.llvmCtx), paramTypes.ptr, 1, false);
  27.         writeln("---3");
  28.  
  29.         auto fun = LLVMAddFunction(codeGen.dmodule, "__ctfe", funType);
  30.         scope(exit) LLVMDeleteFunction(fun);
  31.         writeln("---4");
  32.  
  33.         auto backupCurrentBB = LLVMGetInsertBlock(codeGen.builder);
  34.         scope(exit) {
  35.             if(backupCurrentBB) {
  36.                 LLVMPositionBuilderAtEnd(codeGen.builder, backupCurrentBB);
  37.             } else {
  38.                 LLVMClearInsertionPosition(codeGen.builder);
  39.             }
  40.         }
  41.        
  42.         auto bodyBB = LLVMAppendBasicBlockInContext(codeGen.llvmCtx, fun, "");
  43.         LLVMPositionBuilderAtEnd(codeGen.builder, bodyBB);
  44.        
  45.         // Generate function's body.
  46.         import d.llvm.expression;
  47.         auto eg = ExpressionGen(codeGen);
  48.  
  49.  
  50.         writeln("---11");
  51.         auto castToPointer = LLVMBuildPointerCast(codeGen.builder,
  52.             LLVMGetFirstParam(fun),
  53.             LLVMPointerType(LLVMInt8TypeInContext(codeGen.llvmCtx), 0), "");
  54.         writeln("---12");
  55.  
  56.         auto casted = LLVMBuildPointerCast(codeGen.builder,
  57.             castToPointer,
  58.             LLVMPointerType(stringType,0), "");
  59.         writeln("---22");
  60.         LLVMBuildStore(codeGen.builder, eg.visit(e), casted);
  61.         writeln("---33");
  62.         LLVMBuildRetVoid(codeGen.builder);
  63.         writeln("---4");
  64.  
  65.         codeGen.checkModule(codeGen.dmodule);
  66.         import std.stdio;
  67.  
  68.         auto jitModule = LLVMCloneModule( codeGen.dmodule );
  69.         //scope(exit) LLVMDisposeModule(jitModule);
  70.         //scope(failure) LLVMDumpModule(jitModule);
  71.         auto executionEngine = createExecutionEngine(jitModule);
  72.         //scope(exit) LLVMDisposeExecutionEngine(executionEngine);
  73.  
  74.         auto jitFun = LLVMGetNamedFunction(jitModule, "__ctfe");
  75.         LLVMDumpValue(jitFun);
  76.  
  77.         string foo;
  78.         auto ret = LLVMRunFunction(executionEngine, jitFun, 1,
  79.             [LLVMCreateGenericValueOfInt(LLVMInt8TypeInContext(codeGen.llvmCtx), cast(uint)cast(void*)&foo, false)].ptr);
  80.         writeln("\"",foo, "\"");
  81.         return foo.idup; //return s.idup;
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement