digitalmars.D - D ABI tricks.
- Gor Gyolchanyan (37/37) Oct 04 2011 I was creating a ultra-fast dynamic method call mechanism when i
I was creating a ultra-fast dynamic method call mechanism when i realized, that I'm (ab)using D's standard ABI, which would also be interesting to use when dealing with functions. Particularly, interesting is the way parameter storage classes can be safely removed from the function without any change to the function itself: * Scope. Does not affect ABI. Prevents from escaping references to the parameter from the function. * In: Does not affect ABI. Wraps the type in a const and adds scope storage class. * Out: Equivalent to assigning the parameter to it's type's init value and passing it pointer. * Ref: Equivalent to passing the parameter by pointer. * lazy: Equivalent to passing the a nullary delegate, returning the parameter. So, given any function, it's possible to create alternative _declaration_ (not definition) for it without using any storage classes. The `scope` can be removed completely, `in` can be removed by replacing the parameter with it's const version, `ref` and `out` can be removed by replacing the parameter with it's pointer type and `lazy` can be removed by replacing the parameter with a nullary delegate, returning the original parameter type. Why is this useful? Example: Because this will allow to encapsulate and transport parameters for any function in a single struct (a definition of such a struct is easy to generate in compile-time) without resorting to unnecessarily heavy array of variants and per-parameter type checking. The actual type checking can be done for the entire parameter set at once by storing and comparing a single extra pointer in that struct, which will point to a string, stored in a shared array of function types. By generating a wrapper, which type-checks and unpacks the struct into that function call one can basically bring every possible function to a single ABI, allowing one to safely lose the type of both the function and the struct, implementing a unified dynamic method call.
Oct 04 2011