Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Myrddin: Partial C ABI compatibility


Ok, building C code is now supported, but only in the restricted subset
needed for binding.

We *only* accept files named 'foo.glue.c', and don't even take headers.
The foo.glue.c files can contain comments of the form

    /* CFLAGS: -I foo -Dbar=baz ... */
    /* LIBS: c gtk+-2.0 x11 ... */

The cflags are passed to the C compiler, separated by spaces. The libs are
passed to 'ld' prepended with '-l' to link them in.

muse has support for propagating the external libraries that we're depending
on through to the rest of the code, so linking to code that uses C will work
transparently.

Repository demoing this is here:

    http://git.eigenstate.org/ori/cbind-example.git/tree/

On Fri, 21 Aug 2015 22:37:15 -0500, Ryan Gonzalez <rymg19@xxxxxxxxx> wrote:

> 
> 
> On August 21, 2015 10:02:01 PM CDT, Ori Bernstein <ori@xxxxxxxxxxxxxx> wrote:
> >C ABI compatibility on Linux is partly done. Floats, ints, pointers,
> >and
> >anything else in that class of types is likely to work.
> >
> >Passing structs or unions will not work, although passing pointers to
> >them
> >should be ok. Doing anything interesting in C will involve adding a
> >call to
> >initialize libc.
> >
> >Making this work on Plan 9 shouldn't be too far off, and I believe that
> >it
> >will be easier to make it 100% ABI compatible.
> >
> >Integrating it into the build system is another task, and I'm not sure
> >how
> >that would be best done at the moment.
> >
> 
> Here be dragons. No one on earth can ever agree on how to build C code. Add in messes like configuration, and it's quite a bit of trouble.
> 
> I would personally just NOT support building C files, or have very limited support (no external pkg-config calls and such) and make people run some configure script first (not autoconf, hence my signature :).
> 
> >Example:
> >
> >=== Myrddin ===
> >
> >    use std
> >
> >    type s = struct
> >            a : int
> >            b : int
> >            c : byte[:]
> >    ;;
> >
> >extern const frob : (a : int, b : int, c : uint64, d : s#, str :
> >byte[:]# -> int)
> >    const main = {
> >            var s = [.a = 1000, .b = 2000, .c = "abc"]
> >            var str = "abcd"
> >            std.put("{}\n", frob(1, 2, 3, &s, &str))
> >    }
> >
> >=== C ===
> >
> >    #include <stdlib.h>
> >    #include <stdint.h>
> >
> >    typedef struct Myrsl Myrsl;
> >    typedef struct S S;
> >
> >    struct Myrsl {
> >            void *p;
> >            size_t len;
> >    };
> >
> >    struct S {
> >            int a;
> >            int b;
> >            Myrsl c;
> >    };
> >
> >    int
> >    frob(int a, int b, uint64_t c, S *d, Myrsl *str)
> >    {
> >            return a + b + c + d->a + d->b + d->c.len + str->len;
> >    }
> >
> >=== To compile ===
> >
> >    6m myrcode.myr
> >    cc -c ccode.c
> >ld -o mixed $prefix/lib/myr/_myrrt.o myrcode.o ccode.o
> >-L$prefix/lib/myr -lstd -lsys 
> >    $ ./mixed
> >    3013
> 
> -- 
> Sent from my Nexus 5 with K-9 Mail. Please excuse my brevity.
> 


-- 
    Ori Bernstein