Report saved fresh names as part of fcode env

This commit is contained in:
Gaith Hallak 2022-10-23 11:02:48 +03:00
parent f8f67ced47
commit 4715894471
3 changed files with 42 additions and 39 deletions

View File

@ -127,8 +127,7 @@
state_layout := state_layout(), state_layout := state_layout(),
event_type := ftype() | none, event_type := ftype() | none,
functions := #{ fun_name() => fun_def() }, functions := #{ fun_name() => fun_def() },
payable := boolean(), payable := boolean() }.
saved_fresh_names := #{ var_name() => var_name() } }.
-type type_def() :: fun(([ftype()]) -> ftype()). -type type_def() :: fun(([ftype()]) -> ftype()).
@ -150,17 +149,18 @@
-type state_layout() :: {tuple, [state_layout()]} | {reg, state_reg()}. -type state_layout() :: {tuple, [state_layout()]} | {reg, state_reg()}.
-type env() :: #{ type_env := type_env(), -type env() :: #{ type_env := type_env(),
fun_env := fun_env(), fun_env := fun_env(),
con_env := con_env(), con_env := con_env(),
child_con_env := child_con_env(), child_con_env := child_con_env(),
event_type => aeso_syntax:typedef(), event_type => aeso_syntax:typedef(),
builtins := builtins(), builtins := builtins(),
options := [option()], options := [option()],
state_layout => state_layout(), state_layout => state_layout(),
context => context(), context => context(),
vars => [var_name()], vars => [var_name()],
functions := #{ fun_name() => fun_def() } functions := #{ fun_name() => fun_def() },
saved_fresh_names := #{ var_name() => var_name() }
}. }.
-define(HASH_BYTES, 32). -define(HASH_BYTES, 32).
@ -180,10 +180,10 @@ ast_to_fcode(Code, Options) ->
fun (_, FC) -> optimize(FC, Options) end, fun (_, FC) -> optimize(FC, Options) end,
maps:get(child_con_env, Env1) maps:get(child_con_env, Env1)
)}, )},
FCode3 = FCode2#{saved_fresh_names => get(saved_fresh_names)}, Env3 = Env2#{ saved_fresh_names => get(saved_fresh_names) },
clear_fresh_names(), clear_fresh_names(),
clear_saved_fresh_names(), clear_saved_fresh_names(),
{Env2, FCode3}. {Env3, FCode2}.
optimize(FCode1, Options) -> optimize(FCode1, Options) ->
Verbose = lists:member(pp_fcode, Options), Verbose = lists:member(pp_fcode, Options),
@ -1748,7 +1748,7 @@ clear_saved_fresh_names() ->
fresh_name_save(Name) -> fresh_name_save(Name) ->
Fresh = fresh_name(), Fresh = fresh_name(),
Old = get(saved_fresh_names), Old = get(saved_fresh_names),
New = put(saved_fresh_names, Old#{Fresh => Name}), put(saved_fresh_names, Old#{Fresh => Name}),
Fresh. Fresh.
-spec fresh_name() -> var_name(). -spec fresh_name() -> var_name().

View File

@ -112,10 +112,10 @@ from_string(ContractString, Options) ->
from_string1(ContractString, Options) -> from_string1(ContractString, Options) ->
#{ fcode := FCode #{ fcode := FCode
, fcode_env := #{child_con_env := ChildContracts} , fcode_env := #{child_con_env := ChildContracts, saved_fresh_names := SavedFreshNames}
, folded_typed_ast := FoldedTypedAst , folded_typed_ast := FoldedTypedAst
, warnings := Warnings } = string_to_code(ContractString, Options), , warnings := Warnings } = string_to_code(ContractString, Options),
{FateCode, VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, FCode, Options), {FateCode, VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, FCode, SavedFreshNames, Options),
pp_assembler(FateCode, Options), pp_assembler(FateCode, Options),
ByteCode = aeb_fate_code:serialize(FateCode, []), ByteCode = aeb_fate_code:serialize(FateCode, []),
{ok, Version} = version(), {ok, Version} = version(),
@ -184,9 +184,9 @@ check_call1(ContractString0, FunName, Args, Options) ->
try try
%% First check the contract without the __call function %% First check the contract without the __call function
#{fcode := OrgFcode #{fcode := OrgFcode
, fcode_env := #{child_con_env := ChildContracts} , fcode_env := #{child_con_env := ChildContracts, saved_fresh_names := SavedFreshNames}
, ast := Ast} = string_to_code(ContractString0, Options), , ast := Ast} = string_to_code(ContractString0, Options),
{FateCode, _VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, OrgFcode, []), {FateCode, _VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, OrgFcode, SavedFreshNames, []),
%% collect all hashes and compute the first name without hash collision to %% collect all hashes and compute the first name without hash collision to
SymbolHashes = maps:keys(aeb_fate_code:symbols(FateCode)), SymbolHashes = maps:keys(aeb_fate_code:symbols(FateCode)),
CallName = first_none_match(?CALL_NAME, SymbolHashes, CallName = first_none_match(?CALL_NAME, SymbolHashes,

View File

@ -9,7 +9,7 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(aeso_fcode_to_fate). -module(aeso_fcode_to_fate).
-export([compile/2, compile/3, term_to_fate/1, term_to_fate/2]). -export([compile/3, compile/4, term_to_fate/1, term_to_fate/2]).
-ifdef(TEST). -ifdef(TEST).
-export([optimize_fun/4, to_basic_blocks/1]). -export([optimize_fun/4, to_basic_blocks/1]).
@ -78,26 +78,28 @@ code_error(Err) ->
%% -- Main ------------------------------------------------------------------- %% -- Main -------------------------------------------------------------------
%% @doc Main entry point. %% @doc Main entry point.
compile(FCode, Options) -> compile(FCode, SavedFreshNames, Options) ->
compile(#{}, FCode, Options). compile(#{}, FCode, SavedFreshNames, Options).
compile(ChildContracts, FCode, Options) -> compile(ChildContracts, FCode, SavedFreshNames, Options) ->
#{ contract_name := ContractName,
functions := Functions,
saved_fresh_names := SavedFreshNames } = FCode,
try try
SFuns = functions_to_scode(ChildContracts, ContractName, Functions, SavedFreshNames, Options), compile1(ChildContracts, FCode, SavedFreshNames, Options)
SFuns1 = optimize_scode(SFuns, Options),
FateCode = to_basic_blocks(SFuns1),
?debug(compile, Options, "~s\n", [aeb_fate_asm:pp(FateCode)]),
FateCode1 = case proplists:get_value(include_child_contract_symbols, Options, false) of
false -> FateCode;
true -> add_child_symbols(ChildContracts, FateCode)
end,
{FateCode1, get_variables_registers()}
after after
put(variables_registers, undefined) put(variables_registers, undefined)
end. end.
compile1(ChildContracts, FCode, SavedFreshNames, Options) ->
#{ contract_name := ContractName,
functions := Functions } = FCode,
SFuns = functions_to_scode(ChildContracts, ContractName, Functions, SavedFreshNames, Options),
SFuns1 = optimize_scode(SFuns, Options),
FateCode = to_basic_blocks(SFuns1),
?debug(compile, Options, "~s\n", [aeb_fate_asm:pp(FateCode)]),
FateCode1 = case proplists:get_value(include_child_contract_symbols, Options, false) of
false -> FateCode;
true -> add_child_symbols(ChildContracts, FateCode)
end,
{FateCode1, get_variables_registers()}.
make_function_id(X) -> make_function_id(X) ->
aeb_fate_code:symbol_identifier(make_function_name(X)). aeb_fate_code:symbol_identifier(make_function_name(X)).
@ -221,9 +223,10 @@ serialize_contract_code(Env, C) ->
end, end,
case maps:get(C, Cache, none) of case maps:get(C, Cache, none) of
none -> none ->
Options = Env#env.options, Options = Env#env.options,
FCode = maps:get(C, Env#env.child_contracts), SavedFreshNames = Env#env.saved_fresh_names,
{FateCode, _} = compile(Env#env.child_contracts, FCode, Options), FCode = maps:get(C, Env#env.child_contracts),
{FateCode, _} = compile1(Env#env.child_contracts, FCode, SavedFreshNames, Options),
ByteCode = aeb_fate_code:serialize(FateCode, []), ByteCode = aeb_fate_code:serialize(FateCode, []),
{ok, Version} = aeso_compiler:version(), {ok, Version} = aeso_compiler:version(),
OriginalSourceCode = proplists:get_value(original_src, Options, ""), OriginalSourceCode = proplists:get_value(original_src, Options, ""),