Skip to content

Commit

Permalink
Implement function pointer signatures (dotnet#57)
Browse files Browse the repository at this point in the history
* Implement function pointer signatures

Will be needed to trim anything touching corelib.

Fixes dotnet#35.

* Fix modifiers rewrite

Co-authored-by: vitek-karas <vitek.karas@microsoft.com>
  • Loading branch information
MichalStrehovsky and vitek-karas authored Oct 14, 2021
1 parent cbc5eb7 commit b6f8007
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

[module:KeptAttributeAttribute(typeof(System.Security.UnverifiableCodeAttribute))]

namespace Mono.Linker.Tests.Cases.Basic
{
[Kept]
[SetupCompileArgument("/unsafe")]
unsafe class FunctionPointer
{
[Kept]
static void Main()
{
MethodTakingFunctionPointer(null);
}

[Kept]
static void MethodTakingFunctionPointer(delegate*<OneType, OtherType> del) { }

[Kept]
class OneType { }

[Kept]
class OtherType { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Platforms>x64;x86</Platforms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);INCLUDE_EXPECTATIONS</DefineConstants>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private void AnalyzeType()

private void AnalyzeType(SignatureTypeCode typeCode)
{
again:
switch (typeCode)
{
case SignatureTypeCode.Void:
Expand Down Expand Up @@ -85,7 +86,8 @@ private void AnalyzeType(SignatureTypeCode typeCode)
case SignatureTypeCode.RequiredModifier:
case SignatureTypeCode.OptionalModifier:
AnalyzeCustomModifier(typeCode);
break;
typeCode = _blobReader.ReadSignatureTypeCode();
goto again;
case SignatureTypeCode.GenericTypeInstance:
_blobReader.ReadCompressedInteger();
Dependencies.Add(_factory.GetNodeForToken(_module, _blobReader.ReadTypeHandle()), "Signature reference");
Expand All @@ -96,7 +98,8 @@ private void AnalyzeType(SignatureTypeCode typeCode)
}
break;
case SignatureTypeCode.FunctionPointer:
throw new NotImplementedException();
AnalyzeMethodSignature();
break;
default:
throw new BadImageFormatException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private void RewriteType(SignatureTypeEncoder encoder)

private void RewriteType(SignatureTypeCode typeCode, SignatureTypeEncoder encoder)
{
again:
switch (typeCode)
{
case SignatureTypeCode.Boolean:
Expand Down Expand Up @@ -96,7 +97,8 @@ private void RewriteType(SignatureTypeCode typeCode, SignatureTypeEncoder encode
case SignatureTypeCode.RequiredModifier:
case SignatureTypeCode.OptionalModifier:
RewriteCustomModifier(typeCode, encoder.CustomModifiers());
break;
typeCode = _blobReader.ReadSignatureTypeCode();
goto again;
case SignatureTypeCode.GenericTypeInstance:
{
int classOrValueType = _blobReader.ReadCompressedInteger();
Expand All @@ -119,7 +121,15 @@ private void RewriteType(SignatureTypeCode typeCode, SignatureTypeEncoder encode
case SignatureTypeCode.TypedReference:
encoder.PrimitiveType(PrimitiveTypeCode.TypedReference); break;
case SignatureTypeCode.FunctionPointer:
throw new NotImplementedException();
{
SignatureHeader header = _blobReader.ReadSignatureHeader();
int arity = header.IsGeneric ? _blobReader.ReadCompressedInteger() : 0;
MethodSignatureEncoder sigEncoder = encoder.FunctionPointer(header.CallingConvention, 0, arity);
int count = _blobReader.ReadCompressedInteger();
sigEncoder.Parameters(count, out ReturnTypeEncoder retTypeEncoder, out ParametersEncoder paramEncoder);
RewriteMethodSignature(count, retTypeEncoder, paramEncoder);
}
break;
default:
throw new BadImageFormatException();
}
Expand Down Expand Up @@ -185,6 +195,11 @@ private void RewriteMethodSignature(BlobBuilder blobBuilder, SignatureHeader hea

sigEncoder.Parameters(count, out ReturnTypeEncoder returnTypeEncoder, out ParametersEncoder paramsEncoder);

RewriteMethodSignature(count, returnTypeEncoder, paramsEncoder);
}

private void RewriteMethodSignature(int count, ReturnTypeEncoder returnTypeEncoder, ParametersEncoder paramsEncoder)
{
bool isByRef = false;
againReturnType:
SignatureTypeCode typeCode = _blobReader.ReadSignatureTypeCode();
Expand Down

0 comments on commit b6f8007

Please sign in to comment.