Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] IL2CPU IL-level Optimization #169

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Cosmos.IL2CPU/AppAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ public void EmitEntrypoint(MethodBase aEntrypoint, MethodBase[] aBootEntries = n

if (ShouldOptimize)
{
Optimizer.Optimize(Assembler);

}
}

Expand Down
47 changes: 42 additions & 5 deletions source/Cosmos.IL2CPU/ILReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
using System.Reflection.Metadata;

using Cosmos.IL2CPU.Extensions;
using Cosmos.IL2CPU.Optimization;

namespace Cosmos.IL2CPU
{
public class ILReader
public class ILReader : IOptimizerLookaheadProvider
{
// We split this into two arrays since we have to read
// a byte at a time anways. In the future if we need to
Expand All @@ -19,6 +20,14 @@ public class ILReader
private static readonly OpCode[] mOpCodesLo = new OpCode[256];
private static readonly OpCode[] mOpCodesHi = new OpCode[256];

private static readonly Dictionary<MethodBase, List<ILOpCode>> methodCache = new();
private static readonly Dictionary<MethodBase, List<ILOpCode>> lookaheadCache = new();

/// <summary>
/// The optimizer to use.
/// </summary>
public Optimizer Optimizer { get; set; }

public ILReader()
{
LoadOpCodes();
Expand Down Expand Up @@ -50,10 +59,22 @@ protected void CheckBranch(int aTarget, int aMethodSize)
}
}

public List<ILOpCode> ProcessMethod(MethodBase aMethod)
public List<ILOpCode> ProcessMethodAhead(MethodBase aMethod)
{
var xResult = new List<ILOpCode>();
return ProcessMethod(aMethod, lookahead: true);
}

public List<ILOpCode> ProcessMethod(MethodBase aMethod, bool lookahead = false)
{
if (methodCache.TryGetValue(aMethod, out var result)) {
return result;
}

if(lookahead && lookaheadCache.TryGetValue(aMethod, out var lookaheadResult)) {
return lookaheadResult;
}

var xResult = new List<ILOpCode>();
var xBody = aMethod.GetMethodBody();
var xModule = aMethod.Module;

Expand Down Expand Up @@ -128,7 +149,6 @@ public List<ILOpCode> ProcessMethod(MethodBase aMethod)

#endregion


#region ByReference Intrinsic

if (aMethod.DeclaringType.IsGenericType
Expand Down Expand Up @@ -657,10 +677,27 @@ public List<ILOpCode> ProcessMethod(MethodBase aMethod)
default:
throw new Exception("Unknown OperandType");
}
xILOpCode.InitStackAnalysis(aMethod);

//xILOpCode.InitStackAnalysis(aMethod);
xResult.Add(xILOpCode);
}

if (!lookahead) {
xResult = Optimizer.Optimize(xResult);
}

foreach (var opCode in xResult) {
opCode.InitStackAnalysis(aMethod);
}

if (lookahead) {
lookaheadCache[aMethod] = xResult;
}
else {
methodCache[aMethod] = xResult;
lookaheadCache.Remove(aMethod);
}

return xResult;
}

Expand Down
Loading