Announcing: Little Haskell Compiler v0.1.0 pre-release - Try it online!
An update is almost here. In the meantime, I want to talk about atoms and the C FFI. Brace yourselves for some C - you can’t avoid it when talking about the C FFI!
Atoms are tagged unions allowing us to pass around any value that fits in a cons-cell.
typedef enum AtomType : uint8_t {
ATOM_BIT, // Boolean type
ATOM_INT8
// ...
} AtomType;
typedef union AtomData {
bool bit;
int8 int8;
// ...
} AtomData;
typedef struct Atom {
AtomType type;
AtomData data;
} Atom;
#define FIELD_NAME(TYPE) FIELD_ ## TYPE
#define ATOM_NAME(TYPE) ATOM_ ## TYPE
#define FIELD_BIT bit
#define FIELD_INT_8 int8
In order to make this compiler more flexible, I have made base data types a choice. An example is the ‘boolean’ data type, which might be backed by a single `bit` (C really hates me calling it a bool) or if you don’t have a bool, might store it as an `int8`
// Obviously, we should do this, if we can
#define ATOM_BOOLEAN ATOM_BIT
#define FIELD_BOOLEAN FIELD_BIT
#define BOOLEAN_FALSE false
#define BOOLEAN_TRUE true
// But if bool was unavailable (WASM32, RISCV) we could do this:
#define ATOM_BOOLEAN ATOM_INT8
#define FIELD_BOOLEAN FIELD_INT8
#define BOOLEAN_FALSE 0
#define BOOLEAN_TRUE 1
This was useful for swapping out implementations, but then I realized that I can actually use it to make a C FFI:
#define UNPACK_ATOM(TYPE,ATOM) ((ATOM).data.FIELD_NAME(TYPE))
#define PACK_ATOM(TYPE,VAL) atom(ATOM_NAME(TYPE), (AtomData){ .FIELD_NAME(TYPE) = (VAL) })
#define UNPACK_BIT(ATOM) UNPACK_ATOM(BIT, ATOM)
#define PACK_BIT(VAL) PACK_ATOM(BIT, VAL)
#define UNPACK_INT8(ATOM) UNPACK_ATOM(INT8, ATOM)
#define PACK_INT8(VAL) PACK_ATOM(INT8, VAL)
The `PACK_` functions are probably useful just for creating Atom values, but where this really shine is when I define a small wrapper to pop arguments from a stack, and push the result:
#define C_FFI_WRAP_1(RET_TYPE, FN_NAME, ARG0_TYPE) \
void c_ffi_##FN_NAME(Machine * m) { \
Atom arg0 = cell_atom(yard_peek_pop(m->yard, &(m->reg.stack))); \
Atom out = PACK_ATOM(RET_TYPE, FN_NAME(UNPACK_ATOM(ARG0_TYPE, arg0))); \
YardAddr out_addr = yard_atom(m->yard, out); \
yard_push(m->yard, &(m->reg.stack), out_addr); \
}
Then, given a function
bool complement(bool b) {
return !b;
}
// generates void c_ffi_complement(Machine * m)
// Assume an FFI index of 0
C_FFI_WRAP_1(BIT, complement, BIT)
We could add a CFFI instruction taking an FFI index:
// State before
// S: [true]
// C: [CFFI, 0], triggers the FFI call
c_ffi_complement(m);
// State after
// S: [false]
And because of the type and field magic, we can use the same function on an `int8 fn(int8)`:
int8 increment(int8 a) {
return a + 1;
}
// generates void c_ffi_increment(Machine * m)
// Assume an FFI index of 1
C_FFI_WRAP_1(INT8, increment, INT8)
// State before
// S: [3]
// C: [CFFI, 1], triggers the FFI call
c_ffi_increment(m);
// State after
// S: [4]
This means I’ll need to add syntax support to the Landin / Haskell parser for foreign imports, of course, to automatically generate linker code - you know, these statements:
foreign import capi "botan/ffi.h f" some_fn :: CInt -> IO CInt
This would kick off compiler directives to patch in a C_FFI_WRAP_N with the appropriate types, and link it to the generated FFI index so it can be called via the CFFI opcode.
Oh yes, of course the first library I want to bind to is `botan` - I’ve been planning this for some time! This compiler will support code signing, and `memalloc` and `botan` both have big parts to play!