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

Minimal ILGenerator implementation for new AssemblyBuilder.Save #92846

Merged
merged 5 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal sealed class ILGeneratorImpl : ILGenerator
private readonly BlobBuilder _builder;
private readonly InstructionEncoder _il;
private bool _hasDynamicStackAllocation;
private int _maxStackSize;

internal ILGeneratorImpl(MethodBuilder methodBuilder, int size)
{
Expand All @@ -25,6 +26,8 @@ internal ILGeneratorImpl(MethodBuilder methodBuilder, int size)
_il = new InstructionEncoder(_builder, new ControlFlowBuilder());
}

internal int GetMaxStackSize() => _maxStackSize;

internal InstructionEncoder Instructions => _il;
internal bool HasDynamicStackAllocation => _hasDynamicStackAllocation;

Expand All @@ -46,6 +49,10 @@ public override void Emit(OpCode opcode)
_hasDynamicStackAllocation = true;
}
_il.OpCode((ILOpCode)opcode.Value);

// TODO for now only count the Opcodes emitted, in order to correctly we might need to make below API public
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/Opcode.cs#L48
_maxStackSize++;
}

public override void Emit(OpCode opcode, byte arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private void WriteMethods(TypeBuilderImpl typeBuilder, List<GenericTypeParameter
private static int AddMethodBody(MethodBuilderImpl method, ILGeneratorImpl il, MethodBodyStreamEncoder methodBodyEncoder) =>
methodBodyEncoder.AddMethodBody(
instructionEncoder: il.Instructions,
maxStack: 8, // TODO
maxStack: il.GetMaxStackSize(),
localVariablesSignature: default, // TODO
attributes: method.InitLocals ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None,
hasDynamicStackAllocation: il.HasDynamicStackAllocation);
Expand Down