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

Interface with C unions #77

Open
zevlg opened this issue Feb 15, 2017 · 4 comments
Open

Interface with C unions #77

zevlg opened this issue Feb 15, 2017 · 4 comments

Comments

@zevlg
Copy link

zevlg commented Feb 15, 2017

Currently there is no possibility in pony to map directly to C
unions. Since unions are much similar to struct type, we could
just have annotation, for example:

struct \union\ MyUnion
  i: I32
  f: I64

LLVM does not have union types. So allocation could be done
using size of biggest union field. Accessing fields could be
done just by casting to apropriate type.

Here is example for LLVM IR generated for union types:

union {
        struct {
                float f1;
                double d1;
        };
        int i;
        long long l;
} u1;

union {
        struct {
                int i;
                void* ptr;
                unsigned long long ull;
        };

        char c1;
} u2;

int
main(void)
{
        u1.i = 0;
        char c = u2.c1;

        return 0;
}

Results in generated IR

%union.anon = type { %struct.anon }
%struct.anon = type { float, double }
%union.anon.0 = type { %struct.anon.1 }
%struct.anon.1 = type { i32, i8*, i64 }

@u1 = common global %union.anon zeroinitializer, align 8
@u2 = common global %union.anon.0 zeroinitializer, align 8

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
  %1 = alloca i32, align 4
  %c = alloca i8, align 1
  store i32 0, i32* %1, align 4
  store i32 0, i32* bitcast (%union.anon* @u1 to i32*), align 8
  %2 = load i8, i8* bitcast (%union.anon.0* @u2 to i8*), align 8
  store i8 %2, i8* %c, align 1
  ret i32 0
}
@jemc
Copy link
Member

jemc commented Feb 15, 2017

There was some initial discussion about this at the sync call. The primary concern is around making sure that use of such a "C union" type in a package would qualify as FFI use, for the purposes of Pony's security feature of allowing the user calling the compiler to limit FFI calls (which are inherently memory-unsafe operations) to specific whitelisted packages. Any use of a C union field would be inherently memory-unsafe in the same way, so we'd want to limit it as well, but there are some technical questions about how exactly that would be done.

@martoko
Copy link

martoko commented Apr 27, 2017

Not sure if this is the right issue, but it seems that some C libraries requires support for C unions. SDL2 for example won't work in pony, as it uses a union for its events (SDL_events.h).

It would be nice to use unsafe C unions for FFI, but somehow disallow it for regular pony code. E.g. by prefixing it with @ to indicate that it is an unsafe FFI feature.

@kamirr
Copy link

kamirr commented Feb 3, 2018

Has there been any progress regarding this issue? I wanted to use SFML for some simulations and UI, but it turned that Pony's FFI can't handle C unions and returning a struct from a C function, thus forcing me to write a lot (really, a lot) of C code to hide all of that from Pony. And this meant that using Pony wasn't an option anymore.

@SeanTAllen
Copy link
Member

No one has written an RFC for this at this time @KoczurekK.

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

5 participants