/// <summary> /// Initializes a new instance of the <see cref="LoreLLVMCompiler"/> class. /// </summary> /// <param name="root">The AST root.</param> LoreLLVMCompiler(AstRoot root, LoreModule module, SymbolTable table = null) { // Initialize x86 target LLVM.InitializeX86Target(); LLVM.InitializeX86TargetInfo(); LLVM.InitializeX86AsmPrinter(); LLVM.InitializeX86TargetMC(); LLVM.LinkInMCJIT(); // Assign parameters Root = root; LoreModule = module; Table = table ?? SymbolTable.Create(); // Create the type helper Helper = TypeHelper.Create(this); // Create the stack Stack = new Stack <Symbol> (capacity: 512); // Create LLVM constants LLVMFalse = new LLVMBool(0); LLVMTrue = new LLVMBool(1); LLVMNull = new LLVMValueRef(IntPtr.Zero); // Create the LLVM IR builder Builder = LLVM.CreateBuilder(); // Create the LLVM module LLVMModule = LLVM.ModuleCreateWithName(module.Name); }
public bool Verify() { // Verify the module. LLVMBool result = LLVM.VerifyModule(this.reference, LLVMVerifierFailureAction.LLVMAbortProcessAction, out _); // Return whether the verification succeeded. return(result.Value == 0); }
public override CompilerResult VisitProgram(DesignScriptParser.ProgramContext context) { var success = new LLVMBool(0); module = LLVM.ModuleCreateWithName("DesignScript"); builder = LLVM.CreateBuilder(); LLVM.LinkInMCJIT(); LLVM.InitializeX86TargetInfo(); LLVM.InitializeX86Target(); LLVM.InitializeX86TargetMC(); LLVM.InitializeX86AsmParser(); LLVM.InitializeX86AsmPrinter(); LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 }; LLVM.InitializeMCJITCompilerOptions(options); if (LLVM.CreateExecutionEngineForModule(out engine, module, out var errorMessage).Value == 1) { Console.WriteLine(errorMessage); return(new NullCompilerResult()); } #region Add optimization passes // Create a function pass manager for this engine passManager = LLVM.CreateFunctionPassManagerForModule(module); // Do simple "peephole" optimizations and bit-twiddling optzns. LLVM.AddInstructionCombiningPass(passManager); // Reassociate expressions. LLVM.AddReassociatePass(passManager); // Eliminate Common SubExpressions. LLVM.AddGVNPass(passManager); // Simplify the control flow graph (deleting unreachable blocks, etc). LLVM.AddCFGSimplificationPass(passManager); LLVM.InitializeFunctionPassManager(passManager); #endregion base.VisitProgram(context); if (LLVM.VerifyModule(module, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != success) { Console.WriteLine($"Error: {error}"); } LLVM.DumpModule(module); LLVM.DisposeBuilder(builder); LLVM.DisposeExecutionEngine(engine); return(new NullCompilerResult()); }
public CodeGenVisitor(SymbolTableManager symTableManager) { _symTableManager = symTableManager; _module = LLVM.ModuleCreateWithName("Ryu"); _builder = LLVM.CreateBuilder(); _programAST = symTableManager.ProgramAST; _true = new LLVMBool(1); _false = new LLVMBool(0); }
private void PrintModuleToFile(LLVMModuleRef module, CompileStatus status) { IntPtr llvmErrorMessagePtr; LLVMBool llvmFailure = LLVM.PrintModuleToFile(module, OutputFileName, out llvmErrorMessagePtr); string errorMessage = Marshal.PtrToStringAnsi(llvmErrorMessagePtr); LLVM.DisposeMessage(llvmErrorMessagePtr); if (llvmFailure) { status.AddError(new CompilerError(OutputFileName, 0, 0, "Module Output failed : " + errorMessage)); } }
private void VerifyModule(LLVMModuleRef module, CompileStatus status) { IntPtr llvmErrorMessagePtr; //VerifyModule returns an inverted result... LLVMBool llvmFailure = LLVM.VerifyModule(module, LLVMVerifierFailureAction.LLVMReturnStatusAction, out llvmErrorMessagePtr); string errorMessage = Marshal.PtrToStringAnsi(llvmErrorMessagePtr); LLVM.DisposeMessage(llvmErrorMessagePtr); if (llvmFailure) { status.AddError(new CompilerError(OutputFileName, 0, 0, "Module Verification failed : " + errorMessage)); } }
public void Output(string directoryName, string fileNameWithoutExtension, CompileStatus status) { var outputFileName = Path.Combine(directoryName, fileNameWithoutExtension + ".js"); IntPtr llvmErrorMessagePtr; LLVMBool llvmFailure = LLVM.PrintModuleToFile(Module, outputFileName, out llvmErrorMessagePtr); string errorMessage = Marshal.PtrToStringAnsi(llvmErrorMessagePtr); LLVM.DisposeMessage(llvmErrorMessagePtr); if (llvmFailure) { status.AddError(new CompilerError(outputFileName, 0, 0, "Module Output failed : " + errorMessage)); } }
internal static LLVMExecutionEngineRef CreateMCJITCompilerForModule(this Module module) { LLVMExecutionEngineRef engine; string error; LLVMBool Success = new LLVMBool(0); if (LLVMSharp.LLVM.CreateMCJITCompilerForModule( out engine, module.GetModuleRef(), _mcJITCompilerOptions, out error) != Success) { throw new InvalidOperationException($"Error creating JIT: {error}"); } return(engine); }
public ExecutionContext(IRebarTargetRuntimeServices runtimeServices) { _runtimeServices = runtimeServices; _globalModule = new Module("global"); _globalModule.LinkInModule(CommonModules.StringModule.Clone()); _globalModule.LinkInModule(CommonModules.RangeModule.Clone()); _globalModule.LinkInModule(CommonModules.FileModule.Clone()); string error; LLVMBool Success = new LLVMBool(0); if (LLVMSharp.LLVM.CreateMCJITCompilerForModule( out _engine, _globalModule.GetModuleRef(), _options, out error) != Success) { throw new InvalidOperationException($"Error creating JIT: {error}"); } _targetData = LLVMSharp.LLVM.GetExecutionEngineTargetData(_engine); }
private unsafe static void Main(string[] args) { LLVMBool Success = new LLVMBool(0); LLVMModuleRef mod = LLVM.ModuleCreateWithName("LLVMSharpIntro"); LLVMTypeRef[] param_types = { LLVM.Int32Type(), LLVM.Int32Type() }; LLVMTypeRef ret_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false); LLVMValueRef sum = LLVM.AddFunction(mod, "sum", ret_type); LLVMBasicBlockRef entry = LLVM.AppendBasicBlock(sum, "entry"); LLVMBuilderRef builder = LLVM.CreateBuilder(); LLVM.PositionBuilderAtEnd(builder, entry); LLVMValueRef tmp = LLVM.BuildAdd(builder, LLVM.GetParam(sum, 0), LLVM.GetParam(sum, 1), "tmp"); LLVM.BuildRet(builder, tmp); LLVM.SetLinkage(sum, LLVMLinkage.LLVMExternalLinkage); LLVM.SetDLLStorageClass(sum, LLVMDLLStorageClass.LLVMDLLExportStorageClass); if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != Success) { Console.WriteLine($"Error: {error}"); } //LLVM.LinkInMCJIT(); LLVM.InitializeX86TargetMC(); LLVM.InitializeX86Target(); LLVM.InitializeX86TargetInfo(); LLVM.InitializeX86AsmParser(); LLVM.InitializeX86AsmPrinter(); /*LLVMMCJITCompilerOptions options = new LLVMMCJITCompilerOptions { NoFramePointerElim = 1 }; * LLVM.InitializeMCJITCompilerOptions(options); * if (LLVM.CreateMCJITCompilerForModule(out var engine, mod, options, out error) != Success) * { * Console.WriteLine($"Error: {error}"); * } * * var addMethod = (Add)Marshal.GetDelegateForFunctionPointer(LLVM.GetPointerToGlobal(engine, sum), typeof(Add)); * int result = addMethod(10, 10); * * Console.WriteLine("Result of sum is: " + result); * * if (LLVM.WriteBitcodeToFile(mod, "sum.bc") != 0) * { * Console.WriteLine("error writing bitcode to file, skipping"); * } * * LLVM.DumpModule(mod); * * LLVM.DisposeBuilder(builder); * LLVM.DisposeExecutionEngine(engine);*/ var aa = LLVM.GetLinkage(sum); LLVM.DumpModule(mod); if (LLVM.GetTargetFromTriple("x86_64-pc-win32", out var target, out error) == Success) { var targetMachine = LLVM.CreateTargetMachine(target, "x86_64-pc-win32", "generic", "", LLVMCodeGenOptLevel.LLVMCodeGenLevelDefault, LLVMRelocMode.LLVMRelocDefault, LLVMCodeModel.LLVMCodeModelDefault); var dl = LLVM.CreateTargetDataLayout(targetMachine); LLVM.SetModuleDataLayout(mod, dl); LLVM.SetTarget(mod, "x86_64-pc-win32"); byte[] buffer = System.Text.Encoding.Default.GetBytes("test.o\0"); fixed(byte *ptr = buffer) { LLVM.TargetMachineEmitToFile(targetMachine, mod, new IntPtr(ptr), LLVMCodeGenFileType.LLVMObjectFile, out error); } } }
public static bool ToBool(this LLVMBool b) { return(b.Value != 0); }
public static LLVMValueRef ConstStringInContext(LLVMContextRef Context, string Str, LLVMBool @DontNullTerminate) { return(ConstStringInContext(Context, Str, Str.Length, DontNullTerminate)); }
public static LLVMTypeRef FunctionType(LLVMTypeRef returnType, LLVMTypeRef[] parameterTypes, uint parameterCount, LLVMBool isVarArg) { return(LLVM.FunctionType(returnType, out parameterTypes[0], parameterCount, isVarArg)); }
public static void CompileMethod(WebAssemblyCodegenCompilation compilation, WebAssemblyMethodCodeNode methodCodeNodeNeedingCode) { MethodDesc method = methodCodeNodeNeedingCode.Method; if (compilation.Logger.IsVerbose) { string methodName = method.ToString(); compilation.Logger.Writer.WriteLine("Compiling " + methodName); } if (method.HasCustomAttribute("System.Runtime", "RuntimeImportAttribute")) { methodCodeNodeNeedingCode.CompilationCompleted = true; //throw new NotImplementedException(); //CompileExternMethod(methodCodeNodeNeedingCode, ((EcmaMethod)method).GetRuntimeImportName()); //return; } if (method.IsRawPInvoke()) { //CompileExternMethod(methodCodeNodeNeedingCode, method.GetPInvokeMethodMetadata().Name ?? method.Name); //return; } var methodIL = compilation.GetMethodIL(method); if (methodIL == null) { return; } ILImporter ilImporter = null; try { string mangledName; // TODO: Better detection of the StartupCodeMain method if (methodCodeNodeNeedingCode.Method.Signature.IsStatic && methodCodeNodeNeedingCode.Method.Name == "StartupCodeMain") { mangledName = "StartupCodeMain"; } else { mangledName = compilation.NameMangler.GetMangledMethodName(methodCodeNodeNeedingCode.Method).ToString(); } ilImporter = new ILImporter(compilation, method, methodIL, mangledName, methodCodeNodeNeedingCode is WebAssemblyUnboxingThunkNode); CompilerTypeSystemContext typeSystemContext = compilation.TypeSystemContext; //MethodDebugInformation debugInfo = compilation.GetDebugInfo(methodIL); /* if (!compilation.Options.HasOption(CppCodegenConfigProvider.NoLineNumbersString))*/ { //IEnumerable<ILSequencePoint> sequencePoints = debugInfo.GetSequencePoints(); /*if (sequencePoints != null) * ilImporter.SetSequencePoints(sequencePoints);*/ } //IEnumerable<ILLocalVariable> localVariables = debugInfo.GetLocalVariables(); /*if (localVariables != null) * ilImporter.SetLocalVariables(localVariables);*/ IEnumerable <string> parameters = GetParameterNamesForMethod(method); /*if (parameters != null) * ilImporter.SetParameterNames(parameters);*/ ilImporter.Import(); ilImporter.CreateEHData(methodCodeNodeNeedingCode); methodCodeNodeNeedingCode.CompilationCompleted = true; } catch (Exception e) { compilation.Logger.Writer.WriteLine(e.Message + " (" + method + ")"); methodCodeNodeNeedingCode.CompilationCompleted = true; // methodCodeNodeNeedingCode.SetDependencies(ilImporter.GetDependencies()); //throw new NotImplementedException(); //methodCodeNodeNeedingCode.SetCode(sb.ToString(), Array.Empty<Object>()); } // Uncomment the block below to get specific method failures when LLVM fails for cryptic reasons #if false LLVMBool result = LLVM.VerifyFunction(ilImporter._llvmFunction, LLVMVerifierFailureAction.LLVMPrintMessageAction); if (result.Value != 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Error compliling {method.OwningType}.{method}"); Console.ResetColor(); } #endif // false // Ensure dependencies show up regardless of exceptions to avoid breaking LLVM methodCodeNodeNeedingCode.SetDependencies(ilImporter.GetDependencies()); }
/// <summary> /// "1480643", "AAF0CD1C50D10BBCBC517779A6448DFA", "task_hash" /// </summary> private static void Main() { /* * try * { * const string i16Regex = @"constant \[\d+ x i\d+\] \[(.*)\]"; * using var sr = File.OpenText(@"c:\live\gvs.txt"); * var matches = Regex.Matches(sr.ReadToEnd(), i16Regex, RegexOptions.Multiline); * using var w = new StreamWriter(@"c:\live\01.txt", false); * foreach (var m in matches) * { * var match = (Match)m; * if (match.Groups.Count > 1) * { * var arr = match.Groups[1].Value * .Replace("i16", string.Empty) * .Replace(" ", string.Empty) * .Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries) * .Select(byte.Parse) * .ToArray(); * * var value = Encoding.ASCII.GetString(arr); * w.WriteLine($"{value}"); * } * } * } * catch (Exception e) * { * Console.WriteLine(e); * } * finally * { * Console.WriteLine("Process completed. Press any key."); * Console.ReadLine(); * } * * return; */ byte[] bytes = Encoding.UTF8.GetBytes(@"task_hash"); string str = Encoding.ASCII.GetString(new byte[] { 116, 97, 115, 107, 95, 104, 97, 115, 104 }); string str1 = Encoding.ASCII.GetString(new byte[] { 37, 46, 52, 120, 37, 46, 52, 120, 0 }); LLVM.LinkInMCJIT(); LLVM.InitializeX86TargetMC(); LLVM.InitializeX86Target(); LLVM.InitializeX86TargetInfo(); LLVM.InitializeX86AsmParser(); LLVM.InitializeX86AsmPrinter(); var success = new LLVMBool(0); var strPtr = Marshal.StringToHGlobalUni("1480643"); LLVMExecutionEngineRef ee = default; try { if (LLVM.CreateMemoryBufferWithContentsOfFile(@"c:\live\lr01\lr_client.exe.bc", out LLVMMemoryBufferRef outMemBuf, out var memBufMsg) != success) { Console.WriteLine($"Error: {memBufMsg}"); Console.ReadLine(); return; } if (LLVM.ParseBitcode(outMemBuf, out LLVMModuleRef module, out var pbMsg) != success) { Console.WriteLine($"Error: {pbMsg}"); Console.ReadLine(); return; } if (LLVM.CreateExecutionEngineForModule(out ee, module, out var eeMsg) != success) { Console.WriteLine($"Error: {eeMsg}"); Console.ReadLine(); return; } /* * if (LLVM.PrintModuleToFile(module, @"c:\live\out.ll", * out var ptfMsg) != success) * { * Console.WriteLine($"Error: {ptfMsg}"); * Console.ReadLine(); * return; * } */ using var sw = new StreamWriter(@"c:\live\01.txt", false); var g = LLVM.GetFirstGlobal(module); var i = 0; while (true) { if (g.Equals(default(LLVMValueRef))) { break; } /* * var value = Marshal.PtrToStringAuto(g.Pointer); * if (!string.IsNullOrEmpty(value)) * { * sw.WriteLine($"{value}"); * } */ sw.WriteLine($"{g}"); g = LLVM.GetNextGlobal(g); i++; } /* * var f = LLVM.GetFirstFunction(module); * while (true) * { * if (f.Equals(default(LLVMValueRef))) * { * break; * } * * try * { * var res = LLVM.RunFunction(ee, f, new[] * { * new LLVMGenericValueRef(strPtr) * }); * Console.WriteLine(Marshal.PtrToStringAuto(res.Pointer)); * } * catch (Exception e) * { * Console.WriteLine(e); * } * * f = LLVM.GetNextFunction(f); * } */ } catch (Exception e) { Console.WriteLine(e); } finally { Marshal.FreeHGlobal(strPtr); LLVM.DisposeExecutionEngine(ee); } Console.ReadLine(); }
public void main(IList <CompilationUnit> trees) { if (trees.Count == 0) { return; } LLVMPassManagerRef passManager = LLVM.CreatePassManager(); LLVM.AddStripDeadPrototypesPass(passManager); LLVM.AddPromoteMemoryToRegisterPass(passManager); CompilerNative.AddRewriteStatepointsForGCPass(passManager); LLVM.AddEarlyCSEPass(passManager); LLVM.AddCFGSimplificationPass(passManager); LLVM.AddNewGVNPass(passManager); LLVM.AddLateCFGSimplificationPass(passManager); context = LLVM.GetGlobalContext(); module = LLVM.ModuleCreateWithNameInContext("TheProgram", context); builder = context.CreateBuilderInContext(); foreach ((Type type, ArrayType arrayType) in symtab.arrayTypes) { if (type.IsPrimitive) { declareLLVMStructType(arrayType); createArrayMetadataRecord(arrayType); } } declareBuiltins(); declareRuntimeHelpers(); variableAllocator = new VariableAllocator(builder, typeResolver); build(trees); if (options.DumpIR) { dumpIR(); } LLVMBool success = new LLVMBool(0); if (LLVM.VerifyModule(module, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != success) { Console.WriteLine($"Verfy Error: {error}"); return; } LLVM.RunPassManager(passManager, module); if (options.DumpIR) { dumpIR(); } if (options.Execute) { mcJit(); } else { makeObjectFile(); } LLVM.DisposePassManager(passManager); }
public static extern LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef @Builder, LLVMDWARFSourceLanguage @Lang, LLVMMetadataRef @FileRef, [MarshalAs(UnmanagedType.LPStr)] string @Producer, size_t @ProducerLen, LLVMBool @isOptimized, [MarshalAs(UnmanagedType.LPStr)] string @Flags, size_t @FlagsLen, uint @RuntimeVer, [MarshalAs(UnmanagedType.LPStr)] string @SplitName, size_t @SplitNameLen, LLVMDWARFEmissionKind @Kind, uint @DWOId, LLVMBool @SplitDebugInlining, LLVMBool @DebugInfoForProfiling);
private static InitData Initialize(SyntaxTree tree) { LLVMModuleRef module = LLVM.ModuleCreateWithName("test jit"); LLVMBuilderRef builder = LLVM.CreateBuilder(); LLVM.LinkInMCJIT(); LLVM.InitializeX86TargetInfo(); LLVM.InitializeX86Target(); LLVM.InitializeX86TargetMC(); LLVM.InitializeX86AsmPrinter(); LLVMExecutionEngineRef engine; IntPtr errorMessage; if (LLVM.CreateExecutionEngineForModule(out engine, module, out errorMessage).Value == 1) { Console.WriteLine(Marshal.PtrToStringAnsi(errorMessage)); LLVM.DisposeMessage(errorMessage); throw new Exception("Dispose error"); } var platform = Environment.OSVersion.Platform; if (platform == PlatformID.Win32NT) // On Windows, LLVM currently (3.6) does not support PE/COFF { LLVM.SetTarget(module, string.Concat(Marshal.PtrToStringAnsi(LLVM.GetDefaultTargetTriple()), "-elf")); } var options = new LLVMMCJITCompilerOptions(); var optionsSize = (4 * sizeof(int)) + IntPtr.Size; // LLVMMCJITCompilerOptions has 4 ints and a pointer LLVM.InitializeMCJITCompilerOptions(out options, optionsSize); IntPtr error; LLVM.CreateMCJITCompilerForModule(out engine, module, out options, optionsSize, out error); // Create a function pass manager for this engine LLVMPassManagerRef passManager = LLVM.CreateFunctionPassManagerForModule(module); // Set up the optimizer pipeline. Start with registering info about how the // target lays out data structures. LLVM.AddTargetData(LLVM.GetExecutionEngineTargetData(engine), passManager); LLVM.AddGVNPass(passManager); LLVMBool f = LLVM.InitializeFunctionPassManager(passManager); var s = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var compilation = CSharpCompilation.Create("MyCompilation", new[] { tree }, new[] { s }); var model = compilation.GetSemanticModel(tree); var initData = new InitData(); initData.builder = builder; initData.engine = engine; initData.model = model; initData.module = module; return(initData); }
public static bool Failed(this LLVMBool status) => status;