/// <summary> /// Gets the kernel code. /// </summary> /// <param name="kernalClass">The kernal class.</param> /// <returns></returns> private string GetKernelCode(Type kernalClass) { string assemblyPath = kernalClass.Assembly.Location; CSharpDecompiler cSharpDecompiler = new CSharpDecompiler(assemblyPath, new Amplifier.Decompiler.DecompilerSettings() { ThrowOnAssemblyResolveErrors = false, ForEachStatement = false }); StringBuilder result = new StringBuilder(); ITypeDefinition typeInfo = cSharpDecompiler.TypeSystem.MainModule.Compilation.FindType(new FullTypeName(kernalClass.FullName)).GetDefinition(); List <IMethod> kernelMethods = new List <IMethod>(); List <IMethod> nonKernelMethods = new List <IMethod>(); foreach (var item in typeInfo.Methods) { if (item.IsConstructor) { continue; } if (item.GetAttributes().FirstOrDefault(x => (x.AttributeType.Name == "OpenCLKernelAttribute")) == null) { nonKernelMethods.Add(item); continue; } kernelMethods.Add(item); var k = new KernelFunction() { Name = item.Name }; foreach (var p in item.Parameters) { k.Parameters.Add(p.Name, p.Type.FullName); } KernelFunctions.Add(k); } var kernelHandles = kernelMethods.ToList().Select(x => (x.MetadataToken)).ToList(); var nonKernelHandles = nonKernelMethods.ToList().Select(x => (x.MetadataToken)).ToList(); result.AppendLine(cSharpDecompiler.DecompileAsString(nonKernelHandles)); result.AppendLine(cSharpDecompiler.DecompileAsString(kernelHandles)); return(CodeTranslator.Translate(result.ToString())); }
private string GetStructCode(Type structInstance) { string assemblyPath = structInstance.Assembly.Location; CSharpDecompiler cSharpDecompiler = new CSharpDecompiler(assemblyPath, new Amplifier.Decompiler.DecompilerSettings() { ThrowOnAssemblyResolveErrors = false, ForEachStatement = false }); var tree = cSharpDecompiler.DecompileType(new FullTypeName(structInstance.FullName)); string code = cSharpDecompiler.DecompileTypeAsString(new FullTypeName(structInstance.FullName)); return(CodeTranslator.Translate(code).Trim() + ";"); }
public string[] WriteProject(IIntermediateAssembly assembly, string rootFolder, string extension = ".cs") { List<string> fileTracker = new List<string>(); Dictionary<IIntermediateAssembly, string> fileNameLookup = new Dictionary<IIntermediateAssembly, string>(); this.nameProvider = new NameProvider(this, fileNameLookup); var information = (from part in new[] { assembly }.Concat(assembly.Parts) let partFile = CSharpProjectTranslator.AssemblyFileVisitor.GetFileInfo(part, this, fileTracker) where partFile.YieldsFile let fnTrans = GetTranslatorAndFinalName(rootFolder, extension, partFile.FileName, part, fileNameLookup) select new { Part = part, FileName = partFile.FileName, FinalName = fnTrans.Item2, Translator = fnTrans.Item1 }).ToArray(); #if !DO_NOT_WRITE_MULTITHREADED Parallel.ForEach(information, async assemblyFileInfo => #else foreach (var assemblyFileInfo in information) #endif { CodeTranslator translator = assemblyFileInfo.Translator; var fileStream = new FileStream(assemblyFileInfo.FinalName, FileMode.Create, FileAccess.Write, FileShare.Read); var streamWriter = new StreamWriter(fileStream); IndentedTextWriter itw ; if (options.IndentationSpaceCount != null) itw = new IndentedTextWriter(streamWriter, new string(' ', options.IndentationSpaceCount.Value)); else itw = new IndentedTextWriter(streamWriter); translator.Target = itw; translator.Translate(assemblyFileInfo.Part); #if !DO_NOT_WRITE_MULTITHREADED await streamWriter.FlushAsync(); #else streamWriter.Flush(); #endif streamWriter.Dispose(); fileStream.Close(); fileStream.Dispose(); }