TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.33k stars 227 forks source link

Explore higher level macros #43

Open TheDan64 opened 6 years ago

TheDan64 commented 6 years ago

It'd be pretty neat if you could do something like:

let foo = function! {
    foo(a: u32, b: CustomStruct, c: Ptr<i32>) -> u32 {
        entry:
            a <- 3
            br end
        end:
            return 1;
    }
};

Which would return a function value or Result<FunctionValue, Box<Error>> with that definition. It might need to make some assumptions, like always using the global Context, though. Maybe you could optionally specify a context.

TheDan64 commented 6 years ago

Working with heterogeneous collections is a pain in rust.. We could also provide a macro which simplifies the process for the user. (We may even want this earlier than 0.3.0)

For example,

let values = mixed_value_array!(BasicValue, [
    float_value,
    int_value,
    ...
]);

If we do end up adding this macro, I'd strongly consider switching the current homogeneous function params from say, &[&BasicValue] to &[BasicValueEnum] since the former generally requires an extra heap allocation (unless you happen to have a fixed size of values on the stack, which probably isn't realistic) for the extra trait indirection which could be avoided.

Then, this macro could expand to something like let values: Vec<BasicValueEnum> = array.iter().map(|val| val.into()).collect(). Might need associated type to derive BasicValueEnum from BasicValue, though.

71 commented 6 years ago

The example function! macro you showed above would require the power brought by proc_macro, but it can be achieved similarly using a Lisp-like syntax (like clap.rs) with the macro_rules system.

Here's an example of doing dynamic code generation using this syntax: primitives.rs (note: it's part of a legacy compiler I wrote last year, the code is ugly in some places). The macro itself is defined here.

TheDan64 commented 6 years ago

Proc macros are probably fine; I don't expect to get to this issue before Macros 2.0 lands in stable(it seems pretty close: https://github.com/rust-lang/rust/issues/38356), but who knows.