oakc icon indicating copy to clipboard operation
oakc copied to clipboard

C ABI support

Open guysv opened this issue 3 years ago • 4 comments

Oak's FFI supports calling C functions, only if the target function follows the oak FFI protocol (e.g. receive a vm object, and interact with it)

~~As Oak is advertised to be more portable than C~~ (not relevant, but still), being able to call any C function is a useful feature.

I guess a better description for the feature would be: "Be compatible with C's calling conventions on a variety of C-capable architectures"

Any thoughts on that?

guysv avatar Sep 22 '20 11:09 guysv

I made a similar suggestion in a different issue. Any ideas on how to implement this? And what the rules of such an interaction should be?

kevinramharak avatar Sep 26 '20 09:09 kevinramharak

Well, for the C backend, implementation is simple. All we need to do is to define some syntax that maps between oak's types and C types, and generate C function calls. That way don't even care about the interworking of certain C calling conventions, we rely on the C compiler for the implementation.

I guess this feature should not be part of the standard, as it ranges from being tricky to implement (go) to impossible without major dependencies (typescript).

guysv avatar Sep 26 '20 09:09 guysv

I'm not sure I understand. Would this require additional syntax in the Oak language? I think It should be somewhat abstract so that each backend can implement it. For example:

  • Pop arguments off the tape into memory allocated at runtime
  • transfer execution to native function while passing the arguments
  • Push return value back on the tape

These are abstract definitions thus they can be implemented by any backend.

kevinramharak avatar Sep 26 '20 23:09 kevinramharak

Yes! I was thinking about generalizing the new behavior too! So, to summerize:

  1. define a new syntax to call "native" functions
  2. every backend gets to define what "native" means
    • e.g. for typescript: call a typescript function.

I guess you'll also want to pass some metadata to the compiler (cdecl/stdcall pops into mind)

guysv avatar Sep 26 '20 23:09 guysv