public static void EmitModule(LLVMExecutionEngineRef ee, LLVMModuleRef mod, LLVMCodeGenFileType fileType, string filename) { IntPtr error; IntPtr ptr = Marshal.StringToHGlobalAnsi(filename); LLVM.TargetMachineEmitToFile(LLVM.GetExecutionEngineTargetMachine(ee), mod, ptr, fileType, out error); Console.WriteLine(StringifyMessage(error)); }
public LLVMTranslator(LLVMModuleRef mod, LLVMExecutionEngineRef engine, LLVMBuilderRef builder, LLVMSymbolTable rootSymbol) { this.mod = mod; blockStack = new List <LLVMBasicBlockRef>(); symbolTables = new List <LLVMSymbolTable>(); symbolTables.Add(rootSymbol); this.engine = engine; this.builder = builder; }
public CodeGenParserListener( LLVMExecutionEngineRef ee, LLVMPassManagerRef passManager, CodeGenVisitor visitor) { _visitor = visitor; _ee = ee; _passManager = passManager; }
private LLVMSymbolTable ProcessGlobals(LLVMModuleRef mod, LLVMExecutionEngineRef engine, Dictionary <string, GCHandle> globals) { var res = new LLVMSymbolTable(null); foreach (var kv in globals) { var gs = mod.AddGlobal(LLVMTypeRef.Int32, kv.Key); engine.AddGlobalMapping(gs, kv.Value.AddrOfPinnedObject()); res.Add(kv.Key, gs); } return(res); }
public void SizeTest() { LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten"); LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine(); LLVMTargetDataRef target = engineRef.TargetData; LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct( new[] { LLVMTypeRef.Int16, LLVMTypeRef.Int32 }, true); Assert.AreEqual(48, target.SizeOfTypeInBits(testStruct)); Assert.AreEqual(6, target.StoreSizeOfType(testStruct)); Assert.AreEqual(6, target.ABISizeOfType(testStruct)); }
public void OffsetTest() { LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten"); LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine(); LLVMTargetDataRef target = engineRef.TargetData; LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct( new[] { LLVMTypeRef.Int16, LLVMTypeRef.Int32 }, true); Assert.AreEqual(0, target.OffsetOfElement(testStruct, 0)); Assert.AreEqual(2, target.OffsetOfElement(testStruct, 1)); Assert.AreEqual(target.ElementAtOffset(testStruct, 0), 0); Assert.AreEqual(target.ElementAtOffset(testStruct, 2), 1); }
public ExecutionContext(IRebarTargetRuntimeServices runtimeServices) { _contextWrapper = new ContextWrapper(); _runtimeServices = runtimeServices; _globalModule = _contextWrapper.CreateModule("global"); foreach (ContextFreeModule module in new[] { CommonModules.FakeDropModule, CommonModules.SchedulerModule, CommonModules.StringModule, CommonModules.OutputModule, CommonModules.RangeModule, CommonModules.FileModule }) { _globalModule.LinkInModule(_contextWrapper.LoadContextFreeModule(module)); } _engine = _globalModule.CreateMCJITCompilerForModule(); _targetData = LLVMSharp.LLVM.GetExecutionEngineTargetData(_engine); }
public void AlignmentTest() { LLVMModuleRef m = LLVMModuleRef.CreateWithName("netscripten"); m.Target = "wasm32-unknown-unknown-wasm"; m.DataLayout = "e-m:e-p:32:32-i64:64-n32:64-S128"; LLVMExecutionEngineRef engineRef = m.CreateExecutionEngine(); LLVMTargetDataRef target = engineRef.TargetData; LLVMTypeRef testStruct = LLVMTypeRef.CreateStruct( new[] { LLVMTypeRef.Int16, LLVMTypeRef.Int32 }, true); Assert.AreEqual(1, target.ABIAlignmentOfType(testStruct)); Assert.AreEqual(1, target.CallFrameAlignmentOfType(testStruct)); Assert.AreEqual(8, target.PreferredAlignmentOfType(testStruct)); LLVMValueRef global = m.AddGlobal(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), "someGlobal"); Assert.AreEqual(4, target.PreferredAlignmentOfGlobal(global)); }
public static extern void DisposeExecutionEngine(LLVMExecutionEngineRef* EE);
public static extern int CreateJITCompiler(ref LLVMExecutionEngineRef * OutJIT, LLVMModuleProviderRef* MP, uint OptLevel, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder OutError);
public static void EmitModule(LLVMExecutionEngineRef ee, LLVMModuleRef mod, string filename) { EmitModule(ee, mod, LLVMCodeGenFileType.LLVMObjectFile, filename); }
/// <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 static extern int RemoveModuleProvider(LLVMExecutionEngineRef* EE, LLVMModuleProviderRef* MP, ref LLVMModuleRef * OutMod, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder OutError);
public static extern void AddModule(LLVMExecutionEngineRef* EE, LLVMModuleRef* M);
public static extern LLVMGenericValueRef* RunFunction(LLVMExecutionEngineRef* EE, LLVMValueRef* F, uint NumArgs, System.IntPtr[] Args);
private void ExecuteCode() { var globals = new Dictionary <string, GCHandle>(); LLVMModuleRef mod; LLVMExecutionEngineRef engine = null; try { SymbolTable.IsReadOnly = true; mod = LLVMModuleRef.CreateWithName("main"); var options = LLVMMCJITCompilerOptions.Create(); engine = mod.CreateMCJITCompiler(ref options); var builder = mod.Context.CreateBuilder(); var globalSource = SymbolTable.ItemsSource as ICollection <SymbolEntry>; var globalStore = new Dictionary <string, object>(); foreach (var k in globalSource) { globalStore.Add(k.Name, k.Value); } foreach (var kv in globalStore) { globals.Add(kv.Key, GCHandle.Alloc(kv.Value, GCHandleType.Pinned)); } var fpm = mod.CreateFunctionPassManager(); fpm.AddPromoteMemoryToRegisterPass(); fpm.AddInstructionCombiningPass(); fpm.AddReassociatePass(); fpm.AddGVNPass(); fpm.AddCFGSimplificationPass(); fpm.InitializeFunctionPassManager(); var symT = ProcessGlobals(mod, engine, globals); var lexicals = new List <LexicalElement>(); try { using (var ms = new MemoryStream()) { var writer = new StreamWriter(ms); writer.Write(Editor.Document.CurrentSnapshot.GetText(LineTerminator.Newline)); writer.Flush(); ms.Seek(0L, SeekOrigin.Begin); var reader = new StreamReader(ms); var l = new LexerStateMachine(reader); l.AdvanceChar(); LexicalElement t; while ((t = l.NextToken()) != null) { if (t is LineFeedElement) { continue; } lexicals.Add(t); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "词法错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } var exceptions = new List <SyntaxException>(); SyntaxNode treeRoot; try { treeRoot = slr1Driver.Parse(new Queue <LexicalElement>(lexicals), CommonUtils.Closure( new HashSet <Item>() { new Item() { ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0 } }, generator.ProductionDict), typeof(SNode), null, exceptions); if (exceptions.Count > 0) { StringBuilder sb = new StringBuilder(); sb.Append($"语法分析器共检测到{exceptions.Count}个错误\n\n"); foreach (var exception in exceptions) { sb.Append(exception.Message); sb.Append('\n'); } MessageBox.Show(sb.ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } } catch (SyntaxException ex) { StringBuilder sb = new StringBuilder(); sb.Append($"语法分析器共检测到{exceptions.Count}个错误,无法恢复\n\n"); foreach (var exception in exceptions) { sb.Append(exception.Message); sb.Append('\n'); } MessageBox.Show(sb.ToString(), $"语法分析错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } var translator = new LLVMTranslator.LLVMTranslator(mod, engine, builder, symT); translator.Visit(treeRoot); fpm.RunFunctionPassManager(translator.func); IrBox.Text = mod.PrintToString(); PM main = engine.GetPointerToGlobal <PM>(translator.func); main(); foreach (var symbolEntry in globalSource) { symbolEntry.Value = (int)globalStore[symbolEntry.Name]; } } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } finally { SymbolTable.IsReadOnly = false; if (engine != null) { engine.Dispose(); } foreach (var kv in globals) { kv.Value.Free(); } } }
internal ExecutionEngine(LLVMExecutionEngineRef ee) { this._instance = ee; }
public static extern void RunStaticDestructors(LLVMExecutionEngineRef* EE);
public static extern int RunFunctionAsMain(LLVMExecutionEngineRef* EE, LLVMValueRef* F, uint ArgC, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder ArgV, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder EnvP);
private ExecutionEngine(LLVMExecutionEngineRef executionEngine) => _executionEngine = executionEngine;
public static extern void FreeMachineCodeForFunction(LLVMExecutionEngineRef* EE, LLVMValueRef* F);
public static extern System.IntPtr RecompileAndRelinkFunction(LLVMExecutionEngineRef* EE, LLVMValueRef* Fn);
public static extern void AddModuleProvider(LLVMExecutionEngineRef* EE, LLVMModuleProviderRef* MP);
public static extern void AddGlobalMapping(LLVMExecutionEngineRef* EE, LLVMValueRef* Global, System.IntPtr Addr);
public static extern int FindFunction(LLVMExecutionEngineRef* EE, [In][MarshalAs(UnmanagedType.LPStr)] string Name, ref LLVMValueRef * OutFn);
internal LegacyExecutionEngine(LLVMExecutionEngineRef handle) { EngineHandle = handle; }
public static extern LLVMTargetDataRef* GetExecutionEngineTargetData(LLVMExecutionEngineRef* EE);
public static extern int CreateExecutionEngineForModule(ref LLVMExecutionEngineRef * OutEE, LLVMModuleRef* M, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder OutError);
public static extern System.IntPtr GetPointerToGlobal(LLVMExecutionEngineRef* EE, LLVMValueRef* Global);
public static extern int CreateInterpreter(ref LLVMExecutionEngineRef * OutInterp, LLVMModuleProviderRef* MP, [MarshalAs(UnmanagedType.LPStr)] ref StringBuilder OutError);