Return mapping from variables to registers in fate compilation #902

Merged
ghallak merged 13 commits from ghallak/vars-registers-map into master 2022-10-25 15:42:03 +09:00
2 changed files with 28 additions and 9 deletions
Showing only changes of commit ebd72d9203 - Show all commits

View File

@ -115,7 +115,7 @@ from_string1(ContractString, Options) ->
, fcode_env := #{child_con_env := ChildContracts}
, folded_typed_ast := FoldedTypedAst
, warnings := Warnings } = string_to_code(ContractString, Options),
FateCode = aeso_fcode_to_fate:compile(ChildContracts, FCode, Options),
{FateCode, VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, FCode, Options),
pp_assembler(FateCode, Options),
ByteCode = aeb_fate_code:serialize(FateCode, []),
{ok, Version} = version(),
@ -126,6 +126,7 @@ from_string1(ContractString, Options) ->
fate_code => FateCode,
abi_version => aeb_fate_abi:abi_version(),
payable => maps:get(payable, FCode),
variables_registers => VarsRegs,
warnings => Warnings
},
{ok, maybe_generate_aci(Res, FoldedTypedAst, Options)}.
@ -185,7 +186,7 @@ check_call1(ContractString0, FunName, Args, Options) ->
#{fcode := OrgFcode
, fcode_env := #{child_con_env := ChildContracts}
, ast := Ast} = string_to_code(ContractString0, Options),
FateCode = aeso_fcode_to_fate:compile(ChildContracts, OrgFcode, []),
{FateCode, _VarsRegs} = aeso_fcode_to_fate:compile(ChildContracts, OrgFcode, []),
%% collect all hashes and compute the first name without hash collision to
SymbolHashes = maps:keys(aeb_fate_code:symbols(FateCode)),
CallName = first_none_match(?CALL_NAME, SymbolHashes,

View File

@ -76,13 +76,18 @@ compile(FCode, Options) ->
compile(ChildContracts, FCode, Options) ->
#{ contract_name := ContractName,
functions := Functions } = FCode,
SFuns = functions_to_scode(ChildContracts, ContractName, Functions, Options),
SFuns1 = optimize_scode(SFuns, Options),
FateCode = to_basic_blocks(SFuns1),
?debug(compile, Options, "~s\n", [aeb_fate_asm:pp(FateCode)]),
case proplists:get_value(include_child_contract_symbols, Options, false) of
false -> FateCode;
true -> add_child_symbols(ChildContracts, FateCode)
try
SFuns = functions_to_scode(ChildContracts, ContractName, Functions, 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
put(variables_registers, undefined)
end.
make_function_id(X) ->
@ -110,9 +115,21 @@ function_to_scode(ChildContracts, ContractName, Functions, Name, Attrs0, Args, B
{ArgTypes, ResType1} = typesig_to_scode(Args, ResType),
Attrs = Attrs0 -- [stateful], %% Only track private and payable from here.
Env = init_env(ChildContracts, ContractName, Functions, Name, Args, Options),
[ add_variables_register(Env, Arg, Register) || {Arg, Register} <- Env#env.vars ],
SCode = to_scode(Env, Body),
{Attrs, {ArgTypes, ResType1}, SCode}.
get_variables_registers() ->
case get(variables_registers) of
undefined -> [];
Vs -> Vs
end.
add_variables_register(Env, Name, Register) ->
Olds = get_variables_registers(),
New = {Env#env.contract, Env#env.current_function, Name, Register},
put(variables_registers, [New | Olds]).
-define(tvars, '$tvars').
typesig_to_scode(Args, Res) ->
@ -170,6 +187,7 @@ next_var(#env{ vars = Vars }) ->
1 + lists:max([-1 | [J || {_, {var, J}} <- Vars]]).
bind_var(Name, Var, Env = #env{ vars = Vars }) ->
add_variables_register(Env, Name, Var),
Env#env{ vars = [{Name, Var} | Vars] }.
bind_local(Name, Env) ->