Skip to content

Minimal ILGenerator implementation for new AssemblyBuilder.Save #92846

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

Merged
merged 5 commits into from
Oct 3, 2023

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Sep 30, 2023

Basically, replacing the ILGenerator.Emit overloads implementaiton with InstructionEncoder which will be written into the file when saving the methods in a type using MethodBodyStreamEncoder

Contributes to #83988

@ghost
Copy link

ghost commented Sep 30, 2023

Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.

Issue Details

More tests will be added

Contributes to #83988

Author: buyaa-n
Assignees: buyaa-n
Labels:

area-System.Reflection.Emit

Milestone: -

@buyaa-n buyaa-n marked this pull request as ready for review October 2, 2023 18:02
private static int AddMethodBody(MethodBuilderImpl method, ILGeneratorImpl il, MethodBodyStreamEncoder methodBodyEncoder) =>
methodBodyEncoder.AddMethodBody(
instructionEncoder: il.Instructions,
maxStack: 8, // TODO
Copy link
Contributor

@steveharter steveharter Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if 8 is exceeded? If there is an exception or some other issue, we should probably try to fix this for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can set maxstack == the number of opcodes for now that should protect us against any InvalidProgramException thrown by the JIT etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that until I have got a better handling


if ((_methodImplFlags & MethodImplAttributes.CodeTypeMask) != MethodImplAttributes.IL ||
(_methodImplFlags & MethodImplAttributes.Unmanaged) != 0 ||
(_attributes & MethodAttributes.PinvokeImpl) != 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CoreLib implementation also has a check for DllImport here. Is it intentionally omitted?

Copy link
Contributor Author

@buyaa-n buyaa-n Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are setting the _attributes with MethodAttributes.PinvokeImpl when DllImportAttribute is set, no need to keep another boolean flag for this

case "System.Runtime.InteropServices.DllImportAttribute":
{
_dllImportData = DllImportData.CreateDllImportData(CustomAttributeInfo.DecodeCustomAttribute(con, binaryAttribute), out var preserveSig);
_attributes |= MethodAttributes.PinvokeImpl;

Copy link
Contributor

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending the discussion on a value for maxstack that shouldn't result in an exception if exceeded.

@buyaa-n
Copy link
Contributor Author

buyaa-n commented Oct 3, 2023

The failures not related and known

@buyaa-n buyaa-n merged commit 76b63a3 into dotnet:main Oct 3, 2023
@buyaa-n buyaa-n deleted the ilgen branch October 3, 2023 23:07
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants