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

Discussion: Open Issues in Function Pointers #3324

Closed
333fred opened this issue Apr 1, 2020 · 3 comments
Closed

Discussion: Open Issues in Function Pointers #3324

333fred opened this issue Apr 1, 2020 · 3 comments
Assignees

Comments

@333fred
Copy link
Member

333fred commented Apr 1, 2020

Issues in Function Pointers

Creating an issue for LDM topic on 4/1/20.

  • Confirm NativeCallableAttribute support:

    • This attribute can be applied to methods to indicate that a method can be called with a specific calling convention.
    • Has the following restrictions that need to be applied from C#:
      • Cannot be called from managed code.
      • Parameters must be blittable
      • Must be static
      • Method cannot be generic or in a generic class
      • Cannot be called or used as in a delegate creation
    • When taking the address of a method marked with this attribute, we will use it to determine the calling convention of the method.
    • NativeCallableAttribute should be a public API runtime#32462
  • Supporting extensible calling conventions:

    • Motivation: the runtime team would like to add support for more calling conventions in the future, without necessarily having to rev the language (and make more work in the compiler).
    • Proposal:
      • 335 specifies a calling convention bit that means "calling convention is defined by a modreq on the return type".
      • We specify that calling convention keywords are platform-dependent.
      • The calling convention comes from a type in the System.Runtime.CompilerServices namespace. Type names that start with CallConv are considered.
      • These types must come from CorLib.
      • You can put 1 and only 1 calling convention on the function pointer.
      • If the calling convention is one of the ones understood by the platform today (managed, thiscall, stdcall, cdecl), we'll use the existing bit.
      • Otherwise, we use the new bit and modreq.
    • For C# syntax here, my proposal is as follows:
      • We strip off the CallConv bit from the front, and use the lowercase version of the remaining part of the name as the convention. This allows us to continue having these be contextual keywords in traditional C# fashion (ie, all lowercase).
      • Alternatively, we can just directly use the text from the remaing characters after CallConv.
        • In this version, we likely want to standardize our existing keyword proposal with the type names in the BCL, so that both Stdcall and stdcall are not options, for example.
  • Supporting other attributes on function pointer types.

    • There is a request to support attributes like GCSuppressTransition as part of function pointers (ie, being able to tell the GC that this function pointer call is quick, and it shouldn't do a full managed->native transition).
    • There is no native place in a function pointer type to put this type of information, so the proposal is as follows:
      • We put types specified as modreqs on the return type.
      • Changing these attributes is a breaking change for library authors (because the type is changing).
      • Types used in these attributes must come from the System.Runtime.InteropServices (or some specific namespace, to be worked out with the runtime team. The intent is to make it so that any old type may not be used, just names supported by the runtime.)
    • Syntax proposal (where valid identifiers will be platform-dependent):
    funcptr_type
    : 'delegate' '*' calling_convention? '<' (funcptr_parameter_modifier? type ',')* funcptr_return_modifier? return_type '>'
    ;
    
    calling_convention
    : identifier 
    | '(' identifier ('|' identifier)* ')'
    ;
@333fred 333fred self-assigned this Apr 1, 2020
@YairHalberstadt
Copy link
Contributor

Is the date wrong?

@333fred
Copy link
Member Author

333fred commented Apr 1, 2020

Thanks @YairHalberstadt

@333fred
Copy link
Member Author

333fred commented Apr 18, 2020

This was discussed, closing.

@333fred 333fred closed this as completed Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants