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

CodeGen: implement the wide architecture set #356

Merged
merged 58 commits into from
Nov 23, 2023
Merged

Conversation

ForNeVeR
Copy link
Owner

@ForNeVeR ForNeVeR commented Dec 11, 2022

Closes #354.

TODO

  • Documentation & planning
    • Allow raw pointers as function parameters?
    • CPtr for structure fields
    • CFuncPtr for function pointers in structure fields
  • Make sure all the cases described in the documentation are covered by the added tests
  • Merge with main
  • Review the standard library changes
    • Most of them may be unnecessary since we no longer change the pointers in signatures
  • Fix the tests
  • Add tests on interop
    • Call .NET function accepting a raw pointer from wide arch
    • Call .NET function accepting a (C|F|V)Ptr from wide arch
  • present pointers as long CPtr<T> in codegen on Wide architecture
    • only in structures; see Cesium.CodeGen.Tests.ArchitectureDependentTypeTests.StructWithPointer
    • test for ptr to ptr
  • Present function pointers as FPtr on Wide architecture
    • also only in structures
  • Accommodate Tests: add a workaround for https://github.com/dotnet/sdk/issues/34653 #440 here
  • Add a special InteropType, CLI imports should produce functions with interop types as arguments
    • InteropType knows how to convert a normal type to CLI type
  • At this point, all the tests should become green
    • Well, apparently they didn't, but I should make them so
  • CodeGenNetInteropTests: add actual run test for CPtr interop
  • Add a test for array addressing in the wide arch
  • Document the function pointer solution
  • standard library enhancement
    • create a CPtr<T> type
    • add implicit conversions
    • add codegen support
    • support CPtr interop
  • FPtr interop
  • integration test
  • Check what's going with __nint
    • Perhaps just don't support it in the wide architecture?
  • TODO sweep
  • Make sure everything discussed in Wide architecture support #354 is implemented
  • Final review

Before merge

  • TODO monitoring
    • comments
    • ToDo
  • Create an issue on reverse *Ptr interop (e.g. as a function return value, or by pointer)
  • Create an issue to turn nint/nuint into long/ulong on the Wide arch

@ForNeVeR ForNeVeR self-assigned this Dec 11, 2022
namespace Cesium.Runtime;

/// <summary>A class encapsulating a C function pointer.</summary>
public readonly unsafe struct FPtr<TDelegate> where TDelegate : Delegate // TODO: Think about vararg and empty parameter list encoding.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe FuncPtr?

@@ -19,7 +19,7 @@ private CPtr(long value)

/// <summary>A class encapsulating an object pointer.</summary>
/// <typeparam name="T">Type this pointer may be resolved to.</typeparam>
public unsafe readonly struct CPtr<T> where T : unmanaged
public readonly unsafe struct CPtr<T> where T : unmanaged
Copy link
Collaborator

Choose a reason for hiding this comment

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

And maybe VoidPtr. I do like CPtr, but it sounds too broad for me. But that's just remark. I do have only vague feeling here.

kant2002 added a commit to kant2002/Cesium that referenced this pull request Apr 10, 2023
That's taken from ForNeVeR#356 in attempt to take what's good and unblock other work if possible
kant2002 added a commit to kant2002/Cesium that referenced this pull request Apr 10, 2023
That's taken from ForNeVeR#356 in attempt to take what's good and unblock other work if possible
ForNeVeR pushed a commit that referenced this pull request Apr 10, 2023
That's taken from #356 in attempt to take what's good and unblock other work if possible
@kant2002
Copy link
Collaborator

Not sure if somebody think about it, so I leave it.
Function pointers IMO we whould represent as RuntimeMethodHandle. When convert to void* we can convert to nuint or CPtr and then back. For calls of this method we can use calli.
Approximately like this.
https://github.com/dotnet/runtime/blob/476bce3456006245bdade1fc38a1a300b10f91e8/src/mono/mono/tests/calli_sig_check.il#L13-L21

@ForNeVeR
Copy link
Owner Author

ForNeVeR commented Apr 12, 2023

My general idea is to preserve the function pointers as function pointers, when possible. And fall back to various strange things (delegates or whatnot) when not.

But importantly, not every function may be presented by a RuntimeMethodHandle: if you got a native function, it may not have a "method" per se.

Cesium interop with native code is a bit of underspecified right now (and that's a pretty generous statement already), but it's a thing we'll want to get better at. Eventually. So, our current design should at least consider how it will work in the future.

# Conflicts:
#	Cesium.CodeGen.Tests/ArchitectureDependentCodeTests.cs
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Bit32.verified.txt
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Bit64.verified.txt
#	Cesium.CodeGen.Tests/verified/ArchitectureDependentCodeTests.PointerArrayMemberAssign_arch=Dynamic.verified.txt
#	Cesium.CodeGen/Ir/BlockItems/FunctionDeclaration.cs
#	Cesium.CodeGen/Ir/BlockItems/FunctionDefinition.cs
#	Cesium.Runtime/RuntimeHelpers.cs
#	Cesium.Runtime/StdIoFunctions.cs
#	Cesium.Runtime/StdLibFunctions.cs
#	Cesium.Runtime/StringFunctions.cs
#	docs/architecture-sets.md
#	docs/type-system.md
# Conflicts:
#	Cesium.CodeGen/Contexts/Meta/FunctionInfo.cs
@ForNeVeR ForNeVeR marked this pull request as ready for review November 21, 2023 22:37
@ForNeVeR ForNeVeR merged commit 8d6772f into main Nov 23, 2023
3 checks passed
@ForNeVeR ForNeVeR deleted the feature/354.wide-arch branch November 23, 2023 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wide architecture support
2 participants