[WIP] Start handling inline expressions

This commit is contained in:
Yoshi Askharoun
2025-07-17 00:46:05 -05:00
parent 2f09fbf67c
commit c4a8be37cb
6 changed files with 195 additions and 20 deletions
+87 -16
View File
@@ -1,11 +1,14 @@
using Humanizer;
using Microsoft.Iris.Asm;
using Microsoft.Iris.Asm.Models;
using Microsoft.Iris.DecompXml.Mock;
using Microsoft.Iris.Markup;
using Microsoft.Iris.Markup.Validation;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Xml;
using System.Xml.Linq;
@@ -100,8 +103,7 @@ public class Decompiler
var propertyElements = new XElement[export.Properties.Length];
Stack<XNode> xStack = new([xExport]);
Stack<Disassembler.RawConstantInfo> constantsStack = new();
Stack<object> stack = new([xExport]);
for (int i = 0; i < initPropsBody.Length; i++)
{
@@ -114,13 +116,64 @@ public class Decompiler
{
var constantOperand = (OperandReference)instruction.Operands.First();
var constant = _constants[constantOperand.Index];
constantsStack.Push(constant);
//xStack.Push(IntoXNode(constant.Value));
stack.Push(constant);
}
else if (instruction.OpCode is OpCode.PushNull)
{
constantsStack.Push(null);
//xStack.Push(IntoXNode(null));
stack.Push(null);
}
else if (instruction.OpCode is OpCode.ConstructObject)
{
var type = _loadResult.ImportTables.TypeImports[(ushort)instruction.Operands.ElementAt(0).Value];
var xObj = new XElement(GetXName(type));
stack.Push(xObj);
}
else if (instruction.OpCode is OpCode.MethodInvokeStatic)
{
var method = _loadResult.ImportTables.MethodImports[(ushort)instruction.Operands.First().Value];
int parameterCount = method.ParameterTypes.Length;
object[] parameters = new object[parameterCount];
for (parameterCount--; parameterCount >= 0; parameterCount--)
parameters[parameterCount] = stack.Pop();
var callExpression = new IrisMethodCallExpression(method, null, parameters.Select(p =>
{
return p switch
{
null => Expression.Constant(null),
Expression expr => expr,
Disassembler.RawConstantInfo constantInfo => new IrisConstantExpression(constantInfo.Value, constantInfo.Type),
_ => throw new NotImplementedException()
};
}));
stack.Push(callExpression);
}
else if (instruction.OpCode is OpCode.PropertyInitialize)
{
var property = _loadResult.ImportTables.PropertyImports[(ushort)instruction.Operands.ElementAt(0).Value];
var value = stack.Pop();
var xTarget = (XElement)stack.Peek();
if (value is XElement xValue)
{
var xProperty = new XElement(property.Name);
xProperty.Add(xValue);
xTarget.Add(xProperty);
}
else
{
string strValue = value switch
{
IStringEncodable strEnc => strEnc.EncodeString(),
IrisExpression irisExpr => irisExpr.Decompile(this),
_ => value.ToString()
};
xTarget.SetAttributeValue(property.Name, strValue);
}
}
else if (instruction.OpCode is OpCode.PropertyDictionaryAdd)
{
@@ -129,24 +182,42 @@ public class Decompiler
var keyReference = (OperandReference)instruction.Operands.ElementAt(1);
var key = _constants[keyReference.Index].Value.ToString();
//var xValue = xStack.Pop();
var value = constantsStack.Pop();
var value = stack.Pop();
var targetInstance = xStack.Peek() as XElement;
var targetInstance = stack.Peek() as XElement;
var xDictionary = GetOrCreateElement(targetInstance, nsUix + targetProperty.Name);
XElement xDictionaryEntry = new(GetXName(value.Type));
xDictionaryEntry.SetAttributeValue("Name", key);
xDictionary.Add(xDictionaryEntry);
XElement xDictionaryEntry;
if (value.Value is IStringEncodable valStrEnc)
if (value is Disassembler.RawConstantInfo constantValue)
{
xDictionaryEntry.SetAttributeValue(value.Type.Name, valStrEnc);
xDictionaryEntry = new(GetXName(constantValue.Type));
if (constantValue.Value is IStringEncodable valStrEnc)
{
xDictionaryEntry.SetAttributeValue(constantValue.Type.Name, valStrEnc);
}
else
{
xDictionaryEntry.SetAttributeValue(constantValue.Type.Name, constantValue.Value.ToString());
}
}
else if (value is XElement xValue)
{
xDictionaryEntry = xValue;
}
else if (value is IrisExpression exprValue and IReturnValueProvider exprWithReturnValue)
{
var returnType = exprWithReturnValue.ReturnType;
xDictionaryEntry = new(GetXName(returnType));
xDictionaryEntry.SetAttributeValue(returnType.Name, exprValue.Decompile(this));
}
else
{
xDictionaryEntry.SetAttributeValue(value.Type.Name, value.Value.ToString());
throw new InvalidOperationException();
}
xDictionaryEntry.SetAttributeValue("Name", key);
xDictionary.Add(xDictionaryEntry);
}
}
@@ -257,7 +328,7 @@ public class Decompiler
};
}
private QualifiedTypeName GetQualifiedName(TypeSchema schema)
internal QualifiedTypeName GetQualifiedName(TypeSchema schema)
{
_uriAliasMap.TryGetValue(schema.Owner.Uri, out string prefix);
return new(prefix, schema.Name);
@@ -0,0 +1,8 @@
using Microsoft.Iris.Markup;
namespace Microsoft.Iris.DecompXml.Mock;
internal interface IReturnValueProvider
{
TypeSchema ReturnType { get; }
}
@@ -0,0 +1,34 @@
using Microsoft.Iris.Markup;
using System.Linq.Expressions;
namespace Microsoft.Iris.DecompXml.Mock;
internal class IrisConstantExpression : IrisExpression, IReturnValueProvider
{
public IrisConstantExpression(object value, TypeSchema typeSchema)
{
Value = value;
TypeSchema = typeSchema;
}
public sealed override ExpressionType NodeType => ExpressionType.Constant;
public object Value { get; }
public TypeSchema TypeSchema { get; }
public TypeSchema ReturnType => TypeSchema;
public override string Decompile(Decompiler decompiler)
{
if (TypeSchema.IsEnum)
return $"{decompiler.GetQualifiedName(TypeSchema)}.{Value}";
return Value switch
{
string str => str,
IStringEncodable strEnc => strEnc.EncodeString(),
_ => Value?.ToString() ?? "null",
};
}
}
@@ -0,0 +1,8 @@
using System.Linq.Expressions;
namespace Microsoft.Iris.DecompXml.Mock;
internal class IrisExpression : Expression
{
public virtual string Decompile(Decompiler decompiler) => ToString();
}
@@ -0,0 +1,54 @@
using Microsoft.Iris.Markup;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Microsoft.Iris.DecompXml.Mock;
internal class IrisMethodCallExpression : IrisExpression, IArgumentProvider, IReturnValueProvider
{
private readonly Expression[] _arguments;
public IrisMethodCallExpression(MethodSchema method, Expression? target, IEnumerable<Expression> arguments)
{
Method = method;
Target = target;
_arguments = arguments.ToArray();
}
public MethodSchema Method { get; }
public sealed override ExpressionType NodeType => ExpressionType.Call;
public Expression? Target { get; }
public int ArgumentCount => _arguments.Length;
public TypeSchema ReturnType => Method.ReturnType;
public Expression GetArgument(int index) => _arguments[index];
public override string Decompile(Decompiler decompiler)
{
StringBuilder sb = new();
var qfn = decompiler.GetQualifiedName(Method.Owner);
sb.Append(qfn);
sb.Append('.');
sb.Append(Method.Name);
sb.Append('(');
sb.Append(string.Join(", ", _arguments.Select(exprToString)));
sb.Append(')');
return sb.ToString();
string exprToString(Expression x)
{
return x is IrisExpression irisExpr
? irisExpr.Decompile(decompiler)
: x.ToString();
}
}
}
+4 -4
View File
@@ -2,10 +2,10 @@
<UIX xmlns="http://schemas.microsoft.com/2007/uix" xmlns:me="Me" xmlns:dialog="res://UIXControls!Dialog.uix" xmlns:iris="assembly://UIX/Microsoft.Iris" xmlns:zuneUI="assembly://ZuneShell/ZuneUI" xmlns:system="assembly://System.Private.CoreLib/System" xmlns:styles="res://ZuneShellResources!Styles.uix" xmlns:label="res://UIXControls!Label.uix" xmlns:style="res://UIXControls!Style.uix" xmlns:linkButtons="res://ZuneShellResources!LinkButtons.uix" xmlns:button="res://UIXControls!Button.uix">
<Class Name="AboutDialog" Base="dialog:Dialog">
<Properties>
<String Name="ContentUI" String="res://ZuneShellResources!AboutDialog.uix#AboutDialogContentUI" />
<zuneUI:StringId Name="Cancel" StringId="IDS_DIALOG_OK" />
<zuneUI:StringId Name="TechSupportLink" StringId="IDS_WWW_ZUNE_NET_SUPPORT_URL" />
<zuneUI:StringId Name="AccessibleDescription" StringId="IDS_ABOUTDIALOG_TITLE" />
<String String="res://ZuneShellResources!AboutDialog.uix#AboutDialogContentUI" Name="ContentUI" />
<iris:Command Description="zuneUI:Shell.LoadString(zuneUI:StringId.IDS_DIALOG_OK)" Name="Cancel" />
<zuneUI:WebHelpCommand Description="zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_TECHNICAL_SUPPORT_INFORMATION)" Url="zuneUI:Shell.LoadString(zuneUI:StringId.IDS_WWW_ZUNE_NET_SUPPORT_URL)" Name="TechSupportLink" />
<system:String String="zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_TITLE)" Name="AccessibleDescription" />
</Properties>
</Class>
<UI Name="AboutDialogContentUI" Base="dialog:DialogContentUI" />