From 56c5273116df3e6dcdfbd9672c4d6d44fdf9673d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 24 Aug 2019 23:22:50 +0200 Subject: [PATCH] Added code to refactor inline out expressions --- .../Translation/MethodDecompiler.cs | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 3046b64f..8a8c37f2 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Reflection.Metadata; using System.Text; using System.Text.RegularExpressions; +using ComputeSharp.Shaders.Mappings; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.Metadata; @@ -170,10 +171,13 @@ namespace ComputeSharp.Shaders.Translation } // Unwrap the nested fields - string unwrappedCode = UnwrapSyntaxTree(methodFixedCode); + string unwrappedSourceCode = UnwrapSyntaxTree(methodFixedCode); + + // Tweak the out declarations + string outFixedSourceCode = RefactorInlineOutDeclarations(unwrappedSourceCode, methodInfo.Name); // Load the type syntax tree - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(unwrappedCode); + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(outFixedSourceCode); // Get the root node to return rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(node => node.GetLeadingTrivia().ToFullString().Contains(methodInfo.Name)); @@ -255,5 +259,43 @@ namespace ComputeSharp.Shaders.Translation return source; } + + /// + /// Refactors all the inline out expressions with a variable declaration + /// + /// The input source code to process + /// The name of the shader method being processed + [Pure] + private string RefactorInlineOutDeclarations(string source, string entryPoint) + { + // Replace the discards + source = Regex.Replace(source, @"(? $"out {m.Groups[1].Value} {new string(Guid.NewGuid().ToByteArray().Select(b => (char)('a' + b % 26)).ToArray())}"); + + // Load the syntax tree and the entry node + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); + MethodDeclarationSyntax rootNode = syntaxTree.GetRoot().DescendantNodes().OfType().First(node => node.GetLeadingTrivia().ToFullString().Contains(entryPoint)); + + // Get the out declarations to replace + var outs = ( + from argument in rootNode.DescendantNodes().OfType() + where argument.RefKindKeyword.IsKind(SyntaxKind.OutKeyword) && + argument.Expression.IsKind(SyntaxKind.DeclarationExpression) + let match = Regex.Match(argument.Expression.ToFullString(), @"([\w.]+) ([\w_]+)") + let mappedType = HlslKnownTypes.GetMappedName(match.Groups[1].Value) + let declatation = $"{mappedType} {match.Groups[2].Value} = ({mappedType})0;" + select (match.Groups[1].Value, match.Groups[2].Value, declatation)).ToArray(); + + // Insert the explicit declarations at the start of the method + int start = rootNode.Body.ChildNodes().First().SpanStart; + foreach (var item in outs.Reverse()) + { + source = source.Insert(start, $"{item.declatation}{Environment.NewLine} "); + } + + // Remove the out keyword from the source + source = Regex.Replace(source, @"(?