Merge 'origin/lima' into merge_lima_to_master
This commit is contained in:
+73
-54
@@ -41,8 +41,8 @@
|
||||
-define(TODO(What), error({todo, ?FILE, ?LINE, ?FUNCTION_NAME, What})).
|
||||
|
||||
-define(i(X), {immediate, X}).
|
||||
-define(a, {stack, 0}).
|
||||
-define(s, {store, 1}).
|
||||
-define(a, {stack, 0}).
|
||||
-define(s(N), {store, N}).
|
||||
-define(void, {var, 9999}).
|
||||
|
||||
-record(env, { contract, vars = [], locals = [], current_function, tailpos = true }).
|
||||
@@ -120,9 +120,10 @@ type_to_scode(name) -> name;
|
||||
type_to_scode(channel) -> channel;
|
||||
type_to_scode(bits) -> bits;
|
||||
type_to_scode(any) -> any;
|
||||
type_to_scode({variant, Cons}) -> {variant, lists:map(fun(T) -> type_to_scode({tuple, T}) end, Cons)};
|
||||
type_to_scode({variant, Cons}) -> {variant, [{tuple, types_to_scode(Con)} || Con <- Cons]};
|
||||
type_to_scode({list, Type}) -> {list, type_to_scode(Type)};
|
||||
type_to_scode({tuple, Types}) -> {tuple, lists:map(fun type_to_scode/1, Types)};
|
||||
type_to_scode({tuple, [Type]}) -> type_to_scode(Type);
|
||||
type_to_scode({tuple, Types}) -> {tuple, types_to_scode(Types)};
|
||||
type_to_scode({map, Key, Val}) -> {map, type_to_scode(Key), type_to_scode(Val)};
|
||||
type_to_scode({function, _Args, _Res}) -> {tuple, [string, any]};
|
||||
type_to_scode({tvar, X}) ->
|
||||
@@ -134,6 +135,8 @@ type_to_scode({tvar, X}) ->
|
||||
J -> {tvar, J}
|
||||
end.
|
||||
|
||||
types_to_scode(Ts) -> lists:map(fun type_to_scode/1, Ts).
|
||||
|
||||
%% -- Phase I ----------------------------------------------------------------
|
||||
%% Icode to structured assembly
|
||||
|
||||
@@ -179,84 +182,99 @@ lit_to_fate(L) ->
|
||||
{typerep, T} -> aeb_fate_data:make_typerep(type_to_scode(T))
|
||||
end.
|
||||
|
||||
term_to_fate({lit, L}) ->
|
||||
term_to_fate(E) -> term_to_fate(#{}, E).
|
||||
|
||||
term_to_fate(_Env, {lit, L}) ->
|
||||
lit_to_fate(L);
|
||||
%% negative literals are parsed as 0 - N
|
||||
term_to_fate({op, '-', [{lit, {int, 0}}, {lit, {int, N}}]}) ->
|
||||
term_to_fate(_Env, {op, '-', [{lit, {int, 0}}, {lit, {int, N}}]}) ->
|
||||
aeb_fate_data:make_integer(-N);
|
||||
term_to_fate(nil) ->
|
||||
term_to_fate(_Env, nil) ->
|
||||
aeb_fate_data:make_list([]);
|
||||
term_to_fate({op, '::', [Hd, Tl]}) ->
|
||||
term_to_fate(Env, {op, '::', [Hd, Tl]}) ->
|
||||
%% The Tl will translate into a list, because FATE lists are just lists
|
||||
[term_to_fate(Hd) | term_to_fate(Tl)];
|
||||
term_to_fate({tuple, As}) ->
|
||||
aeb_fate_data:make_tuple(list_to_tuple([ term_to_fate(A) || A<-As]));
|
||||
term_to_fate({con, Ar, I, As}) ->
|
||||
FateAs = [ term_to_fate(A) || A <- As ],
|
||||
[term_to_fate(Env, Hd) | term_to_fate(Env, Tl)];
|
||||
term_to_fate(Env, {tuple, As}) ->
|
||||
aeb_fate_data:make_tuple(list_to_tuple([ term_to_fate(Env, A) || A<-As]));
|
||||
term_to_fate(Env, {con, Ar, I, As}) ->
|
||||
FateAs = [ term_to_fate(Env, A) || A <- As ],
|
||||
aeb_fate_data:make_variant(Ar, I, list_to_tuple(FateAs));
|
||||
term_to_fate({builtin, bits_all, []}) ->
|
||||
term_to_fate(_Env, {builtin, bits_all, []}) ->
|
||||
aeb_fate_data:make_bits(-1);
|
||||
term_to_fate({builtin, bits_none, []}) ->
|
||||
term_to_fate(_Env, {builtin, bits_none, []}) ->
|
||||
aeb_fate_data:make_bits(0);
|
||||
term_to_fate({op, bits_set, [B, I]}) ->
|
||||
term_to_fate(_Env, {op, bits_set, [B, I]}) ->
|
||||
{bits, N} = term_to_fate(B),
|
||||
J = term_to_fate(I),
|
||||
{bits, N bor (1 bsl J)};
|
||||
term_to_fate({op, bits_clear, [B, I]}) ->
|
||||
term_to_fate(_Env, {op, bits_clear, [B, I]}) ->
|
||||
{bits, N} = term_to_fate(B),
|
||||
J = term_to_fate(I),
|
||||
{bits, N band bnot (1 bsl J)};
|
||||
term_to_fate({builtin, map_empty, []}) ->
|
||||
term_to_fate(Env, {'let', X, E, Body}) ->
|
||||
Env1 = Env#{ X => term_to_fate(Env, E) },
|
||||
term_to_fate(Env1, Body);
|
||||
term_to_fate(Env, {var, X}) ->
|
||||
case maps:get(X, Env, undefined) of
|
||||
undefined -> throw(not_a_fate_value);
|
||||
V -> V
|
||||
end;
|
||||
term_to_fate(_Env, {builtin, map_empty, []}) ->
|
||||
aeb_fate_data:make_map(#{});
|
||||
term_to_fate({'let', _, {builtin, map_empty, []}, Set}) ->
|
||||
aeb_fate_data:make_map(map_to_fate(Set)).
|
||||
term_to_fate(Env, {op, map_set, [M, K, V]}) ->
|
||||
Map = term_to_fate(Env, M),
|
||||
Map#{term_to_fate(Env, K) => term_to_fate(Env, V)};
|
||||
term_to_fate(_Env, _) ->
|
||||
throw(not_a_fate_value).
|
||||
|
||||
map_to_fate({op, map_set, [{var, _}, K, V]}) ->
|
||||
#{term_to_fate(K) => term_to_fate(V)};
|
||||
map_to_fate({op, map_set, [Set, K, V]}) ->
|
||||
Map = map_to_fate(Set), Map#{term_to_fate(K) => term_to_fate(V)}.
|
||||
to_scode(Env, T) ->
|
||||
try term_to_fate(T) of
|
||||
V -> [push(?i(V))]
|
||||
catch throw:not_a_fate_value ->
|
||||
to_scode1(Env, T)
|
||||
end.
|
||||
|
||||
to_scode(_Env, {lit, L}) ->
|
||||
to_scode1(_Env, {lit, L}) ->
|
||||
[push(?i(lit_to_fate(L)))];
|
||||
|
||||
to_scode(_Env, nil) ->
|
||||
to_scode1(_Env, nil) ->
|
||||
[aeb_fate_ops:nil(?a)];
|
||||
|
||||
to_scode(Env, {var, X}) ->
|
||||
to_scode1(Env, {var, X}) ->
|
||||
[push(lookup_var(Env, X))];
|
||||
|
||||
to_scode(Env, {con, Ar, I, As}) ->
|
||||
to_scode1(Env, {con, Ar, I, As}) ->
|
||||
N = length(As),
|
||||
[[to_scode(notail(Env), A) || A <- As],
|
||||
aeb_fate_ops:variant(?a, ?i(Ar), ?i(I), ?i(N))];
|
||||
|
||||
to_scode(Env, {tuple, As}) ->
|
||||
to_scode1(Env, {tuple, As}) ->
|
||||
N = length(As),
|
||||
[[ to_scode(notail(Env), A) || A <- As ],
|
||||
tuple(N)];
|
||||
|
||||
to_scode(Env, {proj, E, I}) ->
|
||||
to_scode1(Env, {proj, E, I}) ->
|
||||
[to_scode(notail(Env), E),
|
||||
aeb_fate_ops:element_op(?a, ?i(I), ?a)];
|
||||
|
||||
to_scode(Env, {set_proj, R, I, E}) ->
|
||||
to_scode1(Env, {set_proj, R, I, E}) ->
|
||||
[to_scode(notail(Env), E),
|
||||
to_scode(notail(Env), R),
|
||||
aeb_fate_ops:setelement(?a, ?i(I), ?a, ?a)];
|
||||
|
||||
to_scode(Env, {op, Op, Args}) ->
|
||||
to_scode1(Env, {op, Op, Args}) ->
|
||||
call_to_scode(Env, op_to_scode(Op), Args);
|
||||
|
||||
to_scode(Env, {'let', X, {var, Y}, Body}) ->
|
||||
to_scode1(Env, {'let', X, {var, Y}, Body}) ->
|
||||
Env1 = bind_var(X, lookup_var(Env, Y), Env),
|
||||
to_scode(Env1, Body);
|
||||
to_scode(Env, {'let', X, Expr, Body}) ->
|
||||
to_scode1(Env, {'let', X, Expr, Body}) ->
|
||||
{I, Env1} = bind_local(X, Env),
|
||||
[ to_scode(notail(Env), Expr),
|
||||
aeb_fate_ops:store({var, I}, {stack, 0}),
|
||||
to_scode(Env1, Body) ];
|
||||
|
||||
to_scode(Env = #env{ current_function = Fun, tailpos = true }, {def, Fun, Args}) ->
|
||||
to_scode1(Env = #env{ current_function = Fun, tailpos = true }, {def, Fun, Args}) ->
|
||||
%% Tail-call to current function, f(e0..en). Compile to
|
||||
%% [ let xi = ei ]
|
||||
%% [ STORE argi xi ]
|
||||
@@ -274,17 +292,17 @@ to_scode(Env = #env{ current_function = Fun, tailpos = true }, {def, Fun, Args})
|
||||
|| {I, J} <- lists:zip(lists:seq(0, length(Vars) - 1),
|
||||
lists:reverse(Vars)) ],
|
||||
loop ];
|
||||
to_scode(Env, {def, Fun, Args}) ->
|
||||
to_scode1(Env, {def, Fun, Args}) ->
|
||||
FName = make_function_id(Fun),
|
||||
Lbl = aeb_fate_data:make_string(FName),
|
||||
call_to_scode(Env, local_call(Env, ?i(Lbl)), Args);
|
||||
to_scode(Env, {funcall, Fun, Args}) ->
|
||||
to_scode1(Env, {funcall, Fun, Args}) ->
|
||||
call_to_scode(Env, [to_scode(Env, Fun), local_call(Env, ?a)], Args);
|
||||
|
||||
to_scode(Env, {builtin, B, Args}) ->
|
||||
to_scode1(Env, {builtin, B, Args}) ->
|
||||
builtin_to_scode(Env, B, Args);
|
||||
|
||||
to_scode(Env, {remote, ArgsT, RetT, Ct, Fun, [Gas, Value | Args]}) ->
|
||||
to_scode1(Env, {remote, ArgsT, RetT, Ct, Fun, [Gas, Value | Args]}) ->
|
||||
Lbl = make_function_id(Fun),
|
||||
{ArgTypes, RetType0} = typesig_to_scode([{"_", T} || T <- ArgsT], RetT),
|
||||
ArgType = ?i(aeb_fate_data:make_typerep({tuple, ArgTypes})),
|
||||
@@ -298,10 +316,16 @@ to_scode(Env, {remote, ArgsT, RetT, Ct, Fun, [Gas, Value | Args]}) ->
|
||||
call_to_scode(Env, Call, [Ct, Value, Gas | Args])
|
||||
end;
|
||||
|
||||
to_scode(Env, {closure, Fun, FVs}) ->
|
||||
to_scode1(_Env, {get_state, Reg}) ->
|
||||
[push(?s(Reg))];
|
||||
to_scode1(Env, {set_state, Reg, Val}) ->
|
||||
call_to_scode(Env, [{'STORE', ?s(Reg), ?a},
|
||||
tuple(0)], [Val]);
|
||||
|
||||
to_scode1(Env, {closure, Fun, FVs}) ->
|
||||
to_scode(Env, {tuple, [{lit, {string, make_function_id(Fun)}}, FVs]});
|
||||
|
||||
to_scode(Env, {switch, Case}) ->
|
||||
to_scode1(Env, {switch, Case}) ->
|
||||
split_to_scode(Env, Case).
|
||||
|
||||
local_call( Env, Fun) when Env#env.tailpos -> aeb_fate_ops:call_t(Fun);
|
||||
@@ -420,11 +444,6 @@ call_to_scode(Env, CallCode, Args) ->
|
||||
[[to_scode(notail(Env), A) || A <- lists:reverse(Args)],
|
||||
CallCode].
|
||||
|
||||
builtin_to_scode(_Env, get_state, []) ->
|
||||
[push(?s)];
|
||||
builtin_to_scode(Env, set_state, [_] = Args) ->
|
||||
call_to_scode(Env, [{'STORE', ?s, ?a},
|
||||
tuple(0)], Args);
|
||||
builtin_to_scode(Env, chain_event, Args) ->
|
||||
call_to_scode(Env, [erlang:apply(aeb_fate_ops, log, lists:duplicate(length(Args), ?a)),
|
||||
tuple(0)], Args);
|
||||
@@ -672,11 +691,11 @@ pp_op(loop) -> "LOOP";
|
||||
pp_op(I) ->
|
||||
aeb_fate_pp:format_op(I, #{}).
|
||||
|
||||
pp_arg(?i(I)) -> io_lib:format("~w", [I]);
|
||||
pp_arg({arg, N}) -> io_lib:format("arg~p", [N]);
|
||||
pp_arg({store, N}) -> io_lib:format("store~p", [N]);
|
||||
pp_arg({var, N}) -> io_lib:format("var~p", [N]);
|
||||
pp_arg(?a) -> "a".
|
||||
pp_arg(?i(I)) -> io_lib:format("~w", [I]);
|
||||
pp_arg({arg, N}) -> io_lib:format("arg~p", [N]);
|
||||
pp_arg(?s(N)) -> io_lib:format("store~p", [N]);
|
||||
pp_arg({var, N}) -> io_lib:format("var~p", [N]);
|
||||
pp_arg(?a) -> "a".
|
||||
|
||||
%% -- Analysis --
|
||||
|
||||
@@ -1419,7 +1438,7 @@ desugar_args(I) when is_tuple(I) ->
|
||||
list_to_tuple([Op | lists:map(fun desugar_arg/1, Args)]);
|
||||
desugar_args(I) -> I.
|
||||
|
||||
desugar_arg({store, N}) -> {var, -N};
|
||||
desugar_arg(?s(N)) -> {var, -N};
|
||||
desugar_arg(A) -> A.
|
||||
|
||||
%% -- Phase III --------------------------------------------------------------
|
||||
@@ -1629,6 +1648,7 @@ tweak_returns(['RETURN', {'PUSH', A} | Code]) -> [{'RETURNR', A} | Code
|
||||
tweak_returns(['RETURN' | Code = [{'CALL_T', _} | _]]) -> Code;
|
||||
tweak_returns(['RETURN' | Code = [{'ABORT', _} | _]]) -> Code;
|
||||
tweak_returns(['RETURN' | Code = [{'EXIT', _} | _]]) -> Code;
|
||||
tweak_returns(['RETURN' | Code = [loop | _]]) -> Code;
|
||||
tweak_returns(Code) -> Code.
|
||||
|
||||
%% -- Split basic blocks at CALL instructions --
|
||||
@@ -1642,8 +1662,7 @@ split_calls(Ref, [], Acc, Blocks) ->
|
||||
split_calls(Ref, [I | Code], Acc, Blocks) when element(1, I) == 'CALL';
|
||||
element(1, I) == 'CALL_R';
|
||||
element(1, I) == 'CALL_GR';
|
||||
element(1, I) == 'jumpif';
|
||||
I == loop ->
|
||||
element(1, I) == 'jumpif' ->
|
||||
split_calls(make_ref(), Code, [], [{Ref, lists:reverse([I | Acc])} | Blocks]);
|
||||
split_calls(Ref, [{'ABORT', _} = I | _Code], Acc, Blocks) ->
|
||||
lists:reverse([{Ref, lists:reverse([I | Acc])} | Blocks]);
|
||||
|
||||
Reference in New Issue
Block a user