adam-mcdaniel / oakc

A portable programming language with a compact intermediate representation
Apache License 2.0
725 stars 21 forks source link

C ABI support #89

Open guysv opened 3 years ago

guysv commented 3 years ago

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?

kevinramharak commented 3 years ago

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?

guysv commented 3 years ago

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).

kevinramharak commented 3 years ago

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:

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

guysv commented 3 years ago

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)