Skip to content

Commit

Permalink
Core: ISIL: Resolve unambigous method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Aug 12, 2024
1 parent 19fce16 commit a297189
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
13 changes: 9 additions & 4 deletions Cpp2IL.Core/Graphs/Processors/CallProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public void Process(MethodAnalysisContext methodAnalysisContext, Block block)
HandleKeyFunction(methodAnalysisContext.AppContext, callInstruction, target, keyFunctionAddresses);
return;
}
else
{
// TODO: We could possibly try to resolve some non de-duplicated managed methods early on?
}

//Non-key function call. Try to find a single match
if (!methodAnalysisContext.AppContext.MethodsByAddress.TryGetValue(target, out var targetMethods))
return;

if (targetMethods is not [{ } singleTargetMethod])
return;

callInstruction.Operands[0] = InstructionSetIndependentOperand.MakeMethodReference(singleTargetMethod);
}

private void HandleKeyFunction(ApplicationAnalysisContext appContext, InstructionSetIndependentInstruction instruction, ulong target, BaseKeyFunctionAddresses kFA)
Expand Down
7 changes: 4 additions & 3 deletions Cpp2IL.Core/ISIL/InstructionSetIndependentOperand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public readonly struct InstructionSetIndependentOperand
public static InstructionSetIndependentOperand MakeStack(int value) => new(OperandType.StackOffset, new IsilStackOperand(value));
public static InstructionSetIndependentOperand MakeInstruction(InstructionSetIndependentInstruction instruction) => new(OperandType.Instruction, instruction);
public static InstructionSetIndependentOperand MakeVectorElement(string registerName, IsilVectorRegisterElementOperand.VectorElementWidth width, int index) => new(OperandType.Register, new IsilVectorRegisterElementOperand(registerName, width, index));

public static InstructionSetIndependentOperand MakeTypeMetadataUsage(TypeAnalysisContext value) => new(OperandType.TypeMetadataUsage, new IsilTypeMetadataUsageOperand(value));
public static InstructionSetIndependentOperand MakeMethodReference(MethodAnalysisContext value) => new(OperandType.MethodReference, new IsilMethodOperand(value));


private InstructionSetIndependentOperand(OperandType type, IsilOperandData data)
Expand All @@ -41,11 +41,12 @@ public enum OperandType
Memory = 8,
Instruction = 16,
TypeMetadataUsage = 32,
MethodReference = 64,

MemoryOrStack = Memory | StackOffset,
NotStack = Immediate | Register | Memory | Instruction | TypeMetadataUsage,
NotStack = Immediate | Register | Memory | Instruction | TypeMetadataUsage | MethodReference,


Any = Immediate | StackOffset | Register | Memory | TypeMetadataUsage
Any = Immediate | StackOffset | Register | Memory | TypeMetadataUsage | MethodReference
}
}
15 changes: 15 additions & 0 deletions Cpp2IL.Core/ISIL/IsilMethodOperand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Cpp2IL.Core.Model.Contexts;

namespace Cpp2IL.Core.ISIL;

public readonly struct IsilMethodOperand : IsilOperandData
{
public readonly MethodAnalysisContext Method { get; }

public IsilMethodOperand(MethodAnalysisContext method)
{
Method = method;
}

public override string ToString() => Method.DeclaringType?.Name + "." + Method.Name;
}
9 changes: 6 additions & 3 deletions Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ private void PopulateMethodsByAddressTable()

public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
{
if (_keyFunctionAddresses == null)
(_keyFunctionAddresses = InstructionSet.CreateKeyFunctionAddressesInstance()).Find(this);
lock (InstructionSet)
{
if (_keyFunctionAddresses == null)
(_keyFunctionAddresses = InstructionSet.CreateKeyFunctionAddressesInstance()).Find(this);

return _keyFunctionAddresses;
return _keyFunctionAddresses;
}
}

public MultiAssemblyInjectedType InjectTypeIntoAllAssemblies(string ns, string name, TypeAnalysisContext? baseType)
Expand Down
5 changes: 4 additions & 1 deletion LibCpp2IL/LibCpp2IlMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ public static void Reset()
if (TheMetadata == null) return null;

var typeGlobal = GetRawTypeGlobalByAddress(address);

if (typeGlobal?.Type is not (MetadataUsageType.Type or MetadataUsageType.TypeInfo))
return null;

return typeGlobal?.AsType();
return typeGlobal.AsType();
}

public static MetadataUsage? GetRawFieldGlobalByAddress(ulong address)
Expand Down

0 comments on commit a297189

Please sign in to comment.