annotations icon indicating copy to clipboard operation
annotations copied to clipboard

Support for AOT code generation

Open hyperthunk opened this issue 13 years ago • 1 comments

It would be nice if we could write an annotation handler that is used to generate code at compile time (or when runtime code (re)gen is being applied). The canonical use-case I have for this at the moment is to avoid this:

starts_with(Axis, Value) ->
    binop(starts_with, Axis, Value).

eq(Axis, Value) ->
    binop(eq, Axis, Value).

%% etc
binop(Op, Axis, {literal, _}=Literal) ->
    {Axis, {operator, Op}, Literal}.

And instead to be able to do something like this:

-aliases({[eq, starts_with, ends_with|_Etc], binop, {prefix, function}}). 
binop(Op, Axis, {literal, _}=Literal) ->
    {Axis, {operator, Op}, Literal}.

such that the wrapper functions are generated for me. Quite what the callback/annotation handler module needs to look like isn't clear to me at the moment, but I suspect either a generate_code function or maybe some meta-data to indicate that we want code-gen to occur would do:

-module(aliases).
%% etc...

code_gen(#annotation{data={Aliases, Target, Options}, AST, Options) ->
    {export, [annotations:gen_function(A, redirect_here, Target) || A <- Aliases]}.

around_advice(#annotation{data=Target}, _M, F, _Inputs) ->
    annotation:call_advised(Mod, Target, [F|Inputs]).

hyperthunk avatar Jan 13 '12 17:01 hyperthunk

This is partly implemented in b4ed0cfd8ab2a94ef673763bba4db393129a44a7 (v0.0.2) but still needs some work.

hyperthunk avatar May 18 '12 13:05 hyperthunk