public static string Decompile(DomCecilMethod method, bool markup) { if (method.MethodDefinition.IsPInvokeImpl) { return(GettextCatalog.GetString("Method is P/Invoke")); } if (method.MethodDefinition.Body == null) { IType type = method.DeclaringType; return(type == null || type.ClassType == ClassType.Interface ? GettextCatalog.GetString("Interface method") : GettextCatalog.GetString("Abstract method")); } StringBuilder result = new StringBuilder(); try { //ControlFlowGraph controlFlowGraph = ControlFlowGraph.Create (method.MethodDefinition); ILanguage lang = CSharp.GetLanguage(CSharpVersion.V3); ColoredCSharpFormatter formatter = new ColoredCSharpFormatter(); ILanguageWriter langWriter = lang.GetWriter(formatter); langWriter.Write(method.MethodDefinition); result.Append(formatter.Text); } catch (Exception e) { result.Append("Decompilation failed: \n" + e); } return(result.ToString()); }
protected virtual void WriteTypeInANewWriterIfNeeded(TypeDefinition type, bool writeDocumentation, bool showCompilerGeneratedMembers = false) { if (this.CurrentType != type) { ILanguageWriter writer = Language.GetWriter(this.formatter, this.exceptionFormatter, this.WriteExceptionsAsComments); List <WritingInfo> nestedWritingInfos = writer.Write(type, writerContextService, writeDocumentation, showCompilerGeneratedMembers); this.writingInfos.AddRange(nestedWritingInfos); } else { WriteType(type, writeDocumentation, showCompilerGeneratedMembers); } }
protected virtual void WriteTypeInANewWriterIfNeeded(TypeDefinition type) { if (this.CurrentType != type) { ILanguageWriter writer = Language.GetWriter(this.formatter, this.exceptionFormatter, this.Settings); writer.ExceptionThrown += OnExceptionThrown; List <WritingInfo> nestedWritingInfos = writer.Write(type, writerContextService); writer.ExceptionThrown -= OnExceptionThrown; this.writingInfos.AddRange(nestedWritingInfos); } else { WriteType(type); } }
void displayItem(object item) { swriter.GetStringBuilder().Length = 0; if (item is MethodDefinition) { if (writer != null) { var mdef = item as MethodDefinition; try { writer.Write(mdef); } catch (Exception e) { swriter.WriteLine(e.ToString()); } } } textEditorControl.Document.TextContent = swriter.ToString(); textEditorControl.Refresh(); }
static void Main(string[] args) { Assembly ass = Assembly.LoadFrom(@"D:\Nick Personal\Programming\GPU\Projects\Cudafy\Cudafy\bin\Debug\ESA.Dataflow.dll"); //(typeof(Program)); // ass. ////AssemblyDefinition. AssemblyDefinition ad = AssemblyFactory.GetAssembly(ass.Location); ModuleDefinitionCollection mdc = ad.Modules; StringBuilder sb = new StringBuilder(); StringWriter streamWriter = new StringWriter(sb); foreach (ModuleDefinition mod in mdc) { Console.WriteLine(mod.Name); foreach (TypeDefinition type in mod.Types) { Console.WriteLine(type.FullName); if (type.Name == "ControlVector") { foreach (MethodDefinition md in type.Methods) { //foreach (CustomAttribute ca in md.CustomAttributes) //{ // if (ca.Constructor.DeclaringType.Name == "GPUFunctionAttribute") // { if (md.Name == "Add") { Console.WriteLine(md.Name); ILanguage lan = Cecil.Decompiler.Languages.CSharp.GetLanguage(Cecil.Decompiler.Languages.CSharpVersion.V3); ILanguageWriter lanWriter = lan.GetWriter(new PlainTextFormatter(streamWriter)); lanWriter.Write(md); Console.WriteLine(sb.ToString()); } // } //} } } } } int size = 4; int[] myArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }; int[][] inputA = new int[size][]; int[][] inputB = new int[size][]; int[][] outputC = new int[size][]; for (int i = 0; i < size; i++) { inputB[i] = new int[size]; outputC[i] = new int[size]; inputA[i] = new int[size]; int cnt = i; for (int x = 0; x < size; x++) { inputA[i][x] = cnt; inputB[i][x] = cnt++; } } HCudafy cuda = new HCudafy(); cuda.Cudafy(typeof(Program)); Stopwatch sw = new Stopwatch(); sw.Start(); int[] devMyArray = cuda.CopyToDevice(myArray); int[][] devA = cuda.CopyToDevice(inputA); int[][] devB = cuda.CopyToDevice(inputB); int[][] devC = cuda.Allocate(outputC); Dim3 grid = new Dim3(1); Dim3 block = new Dim3(size / 1); cuda.Launch(grid, block, "doVecAdd", devA, devB, devC, 42, devMyArray); cuda.CopyFromDevice(devC, outputC); sw.Stop(); for (int i = 0; i < 4; i++) { for (int x = 0; x < 4; x++) { Console.Write("{0}\t", outputC[i][x]); } Console.WriteLine(); } int[] somestuff = new int[512]; for (int y = 0; y < 512; y++) { somestuff[y] = y * 10; } int[] data = cuda.CopyToDevice(somestuff); int[] res = new int[512]; cuda.CopyFromDevice(data, res); for (int y = 0; y < 512; y++) { if (res[y] != somestuff[y]) { throw new Exception(); } } int[][] deviceArray2D = cuda.Allocate <int>(4, 8); int[] deviceArray = cuda.Allocate <int>(7); Console.WriteLine(sw.ElapsedMilliseconds + "ms"); Console.WriteLine("Done"); Console.ReadKey(); return; #region scrap //Action<object> action = (object obj) => //{ // Console.WriteLine("Task={0}, obj={1}, Thread={2}", Task.CurrentId, obj.ToString(), Thread.CurrentThread.ManagedThreadId); //}; //Task t = new Task(action, "hello"); //HThread ht = new HThread(action, "hello"); //HGrid grid = new HGrid( //HCudafy.Launch( int side = 1024; int[][] myJaggedArray = new int[side][]; for (int i = 0; i < side; i++) { myJaggedArray[i] = new int[side]; int cnt = i; for (int x = 0; x < side; x++) { myJaggedArray[i][x] = cnt++; } } int threads = Environment.ProcessorCount / 1; // _barrier = new Barrier(threads); //Console.WriteLine("Before"); //var po = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }; //Parallel.For(0, side, po, i => Process(myJaggedArray[i])); myJaggedArray.AsParallel().WithDegreeOfParallelism(threads).ForAll(x => Process(x)); //myJaggedArray.ToList().ForEach(x => Process(x)); //Console.WriteLine("Between"); //myJaggedArray.AsParallel().WithDegreeOfParallelism(threads).ForAll(x => Process(x)); sw.Stop(); // _barrier.Dispose(); Console.WriteLine(sw.ElapsedMilliseconds + "ms"); Console.WriteLine("Done"); Console.ReadKey(); #endregion }
public bool WriteTypeToFile(TypeDefinition type, IProjectItemFileWriter itemWriter, Dictionary <string, ICollection <string> > membersToSkip, bool shouldBePartial, ILanguage language, out List <WritingInfo> writingInfos, out string theCodeString) { theCodeString = string.Empty; writingInfos = null; StringWriter theWriter = new StringWriter(); bool showCompilerGeneratedMembers = Utilities.IsVbInternalTypeWithoutRootNamespace(type) || Utilities.IsVbInternalTypeWithRootNamespace(type); IFormatter formatter = GetFormatter(theWriter); IWriterSettings settings = new WriterSettings(writeExceptionsAsComments: true, writeFullyQualifiedNames: decompilationPreferences.WriteFullNames, writeDocumentation: decompilationPreferences.WriteDocumentation, showCompilerGeneratedMembers: showCompilerGeneratedMembers, writeLargeNumbersInHex: decompilationPreferences.WriteLargeNumbersInHex); ILanguageWriter writer = language.GetWriter(formatter, this.exceptionFormater, settings); IWriterContextService writerContextService = this.GetWriterContextService(); writer.ExceptionThrown += OnExceptionThrown; writerContextService.ExceptionThrown += OnExceptionThrown; bool exceptionOccurred = false; try { if (!(writer is INamespaceLanguageWriter)) { writingInfos = writer.Write(type, writerContextService); } else { if (shouldBePartial) { writingInfos = (writer as INamespaceLanguageWriter).WritePartialTypeAndNamespaces(type, writerContextService, membersToSkip); } else { writingInfos = (writer as INamespaceLanguageWriter).WriteTypeAndNamespaces(type, writerContextService); } } this.RecordGeneratedFileData(type, itemWriter.FullSourceFilePath, theWriter, formatter, writerContextService, writingInfos); MemoryStream sourceFileStream = new MemoryStream(Encoding.UTF8.GetBytes(theWriter.ToString())); itemWriter.CreateProjectSourceFile(sourceFileStream); sourceFileStream.Close(); theWriter.Close(); } catch (Exception e) { exceptionOccurred = true; string[] exceptionMessageLines = exceptionFormater.Format(e, type.FullName, itemWriter.FullSourceFilePath); string exceptionMessage = string.Join(Environment.NewLine, exceptionMessageLines); string commentedExceptionMessage = language.CommentLines(exceptionMessage); itemWriter.CreateProjectSourceFile(new MemoryStream(Encoding.UTF8.GetBytes(commentedExceptionMessage))); OnExceptionThrown(this, e); } theCodeString = theWriter.ToString(); writer.ExceptionThrown -= OnExceptionThrown; writerContextService.ExceptionThrown -= OnExceptionThrown; return(exceptionOccurred || writerContextService.ExceptionsWhileDecompiling.Any()); }