diff --git a/src/aeb_fate_asm.erl b/src/aeb_fate_asm.erl index f0cea24..9db8655 100644 --- a/src/aeb_fate_asm.erl +++ b/src/aeb_fate_asm.erl @@ -30,9 +30,7 @@ %%% arg0 %%% References to variables/registers start with var followed by an integer %%% var0 -%%% References to stack postions is either a (for stack 0) -%%% or start with stack followed by an integer -%%% stack1 +%%% References to the top of the stack is the letter a (for accumulator) %%% a %%% %%% Immediate values can be of 11 types: @@ -243,8 +241,8 @@ to_bytecode([{arg,_line, N}|Rest], Address, Env, Code, Opts) -> to_bytecode(Rest, Address, Env, [{arg, N}|Code], Opts); to_bytecode([{var,_line, N}|Rest], Address, Env, Code, Opts) -> to_bytecode(Rest, Address, Env, [{var, N}|Code], Opts); -to_bytecode([{stack,_line, N}|Rest], Address, Env, Code, Opts) -> - to_bytecode(Rest, Address, Env, [{stack, N}|Code], Opts); +to_bytecode([{stack,_line}|Rest], Address, Env, Code, Opts) -> + to_bytecode(Rest, Address, Env, [{stack, 0}|Code], Opts); to_bytecode([{int,_line, Int}|Rest], Address, Env, Code, Opts) -> to_bytecode(Rest, Address, Env, [{immediate, Int}|Code], Opts); to_bytecode([{boolean,_line, Bool}|Rest], Address, Env, Code, Opts) -> diff --git a/src/aeb_fate_asm_scan.template b/src/aeb_fate_asm_scan.template index cd0bb5f..bd67d28 100644 --- a/src/aeb_fate_asm_scan.template +++ b/src/aeb_fate_asm_scan.template @@ -27,8 +27,7 @@ BITS = (\!)?\<[\s01]*\> Rules. arg{INT} : {token, {arg, TokenLine, parse_arg(TokenChars)}}. var{INT} : {token, {var, TokenLine, parse_var(TokenChars)}}. -a : {token, {stack, TokenLine, 0}}. -a{INT} : {token, {stack, TokenLine, parse_acc(TokenChars)}}. +a : {token, {stack, TokenLine}}. true : {token, {boolean, TokenLine, true}}. false : {token, {boolean, TokenLine, false}}. @@ -108,7 +107,7 @@ parse_int(Chars) -> list_to_integer(Chars). parse_arg("arg" ++ N) -> list_to_integer(N). parse_var("var" ++ N) -> list_to_integer(N). -parse_acc("a" ++ N) -> list_to_integer(N). + parse_hash("#" ++ Chars) -> diff --git a/src/aeb_fate_code.erl b/src/aeb_fate_code.erl index cceb9e8..a0ea3b5 100644 --- a/src/aeb_fate_code.erl +++ b/src/aeb_fate_code.erl @@ -174,10 +174,10 @@ serialize_code([{_,_}|_] = List ) -> Mods = << <<(modifier_bits(Type)):2>> || {Type, _} <- pad_args(lists:reverse(Args)) >>, case Mods of <> -> - [M1, M2 | [serialize_data(Type, Arg) || {Type, Arg} <- Args]] ++ + [M1, M2 | [serialize_data(Type, Arg) || {Type, Arg} <- Args, Type =/= stack]] ++ serialize_code(Rest); <> -> - [M1 | [serialize_data(Type, Arg) || {Type, Arg} <- Args]] ++ + [M1 | [serialize_data(Type, Arg) || {Type, Arg} <- Args, Type =/= stack]] ++ serialize_code(Rest) end; serialize_code([Op|Rest]) -> @@ -304,15 +304,25 @@ deserialize_op(Op, Rest, Code) -> deserialize_n_args(N, <>) when N =< 4 -> ArgMods = lists:sublist([M0, M1, M2, M3], N), lists:mapfoldl(fun(M, Acc) -> - {Arg, Acc2} = aeb_fate_encoding:deserialize_one(Acc), - {{bits_to_modifier(M), Arg}, Acc2} + case bits_to_modifier(M) of + stack -> + {{stack, 0}, Acc}; + Modifier -> + {Arg, Acc2} = aeb_fate_encoding:deserialize_one(Acc), + {{Modifier, Arg}, Acc2} + end end, Rest, ArgMods); deserialize_n_args(N, <>) when N =< 8 -> ArgMods = lists:sublist([M0, M1, M2, M3, M4, M5, M6, M7], N), lists:mapfoldl(fun(M, Acc) -> - {Arg, Acc2} = aeb_fate_encoding:deserialize_one(Acc), - {{bits_to_modifier(M), Arg}, Acc2} + case bits_to_modifier(M) of + stack -> + {{stack, 0}, Acc}; + Modifier -> + {Arg, Acc2} = aeb_fate_encoding:deserialize_one(Acc), + {{Modifier, Arg}, Acc2} + end end, Rest, ArgMods). deserialize_signature(Binary) -> diff --git a/src/aeb_fate_generate_ops.erl b/src/aeb_fate_generate_ops.erl index 0fa7b0f..6b978d9 100644 --- a/src/aeb_fate_generate_ops.erl +++ b/src/aeb_fate_generate_ops.erl @@ -43,7 +43,7 @@ ops_defs() -> , { 'SWITCH_VN', 16#0c, true, 4, [a, li], switch, "Conditional jump to a basic block on variant tag."} , { 'CALL_VALUE', 16#0d, false, 3, [a], call_value, "The value sent in the current remote call."} , { 'PUSH', 16#0e, false, 2, [a], push, "Push argument to stack."} - , { 'DUPA', 16#0f, false, 3, [], dup, "push copy of accumulator on stack."} + , { 'DUPA', 16#0f, false, 3, [], dup, "Duplicate top of stack."} , { 'DUP', 16#10, false, 3, [a], dup, "push Arg0 stack pos on top of stack."} , { 'POP', 16#11, false, 3, [a], pop, "Arg0 := top of stack."} , { 'INCA', 16#12, false, 2, [], inc, "Increment accumulator."} @@ -92,7 +92,7 @@ ops_defs() -> , { 'INT_TO_ADDR', 16#3d, false, 3, [a,a], int_to_addr, "Arg0 := turn integer Arg1 into an address."} , { 'VARIANT', 16#3e, false, 3, [a,a,a,a], variant, "Arg0 := create a variant of size Arg1 with the tag Arg2 (Arg2 < Arg1) and take Arg3 elements from the stack."} , { 'VARIANT_TEST', 16#3f, false, 3, [a,a,a], variant_test, "Arg0 := true if variant Arg1 has the tag Arg2."} - , { 'VARIANT_ELEMENT', 16#40, false, 3, [a,a,a], variant_element, "Arg0 := element number Arg2 from variant Arg1."} + , { 'VARIANT_ELEMENT', 16#40, false, 3, [a,a,a], variant_element, "Arg0 := element number Arg2 from variant Arg1."} , { 'BITS_NONEA', 16#41, false, 3, [], bits_none, "accumulator := empty bitmap."} , { 'BITS_NONE', 16#42, false, 3, [a], bits_none, "Arg0 := empty bitmap."} , { 'BITS_ALLA', 16#43, false, 3, [], bits_all, "accumulator := full bitmap."} @@ -215,7 +215,7 @@ generate_code_ops(Modulename, SrcDir, Ops) -> "-type fate_arg_immediate(T) :: {immediate, T}.\n" "-type fate_arg_var() :: {var, integer()}.\n" "-type fate_arg_arg() :: {arg, integer()}.\n" - "-type fate_arg_stack() :: {stack, integer()}.\n" + "-type fate_arg_stack() :: {stack, 0}.\n" "-type fate_arg() :: fate_arg_immediate()\n" " | fate_arg_var()\n" " | fate_arg_arg()\n" @@ -425,8 +425,7 @@ gen_asm_pp(Module, Path, Ops) -> " aeb_fate_data:format(I);\n" "format_arg(a, {arg, N}) -> io_lib:format(\"arg~~p\", [N]);\n" "format_arg(a, {var, N}) -> io_lib:format(\"var~~p\", [N]);\n" - "format_arg(a, {stack, 0}) -> \"a\";\n" - "format_arg(a, {stack, N}) -> io_lib:format(\"a~~p\", [N]).\n\n" + "format_arg(a, {stack, 0}) -> \"a\".\n\n" "lookup(Name, Symbols) ->\n" " maps:get(Name, Symbols, io_lib:format(\"~~p\",[Name])).\n\n" "~s" @@ -562,7 +561,7 @@ gen_arg(t) -> "integer". any_arg() -> element(rand:uniform(5), {"a", stack_arg(), var_arg(), arg_arg(), imm_arg()}). -stack_arg() -> "a" ++ integer_to_list(rand:uniform(255)-1). +stack_arg() -> "a". arg_arg() -> "arg" ++ integer_to_list(rand:uniform(256)-1). var_arg() -> "var" ++ integer_to_list(rand:uniform(256)-1). imm_arg() -> diff --git a/test/asm_code/all_instructions.fate b/test/asm_code/all_instructions.fate index 664e61c..02ee883 100644 --- a/test/asm_code/all_instructions.fate +++ b/test/asm_code/all_instructions.fate @@ -6,7 +6,7 @@ FUNCTION foo () : {tuple, []} RETURN - RETURNR a13 + RETURNR a CALL "foo" @@ -24,7 +24,7 @@ FUNCTION foo () : {tuple, []} JUMPIF arg196 0x12c651665 - SWITCH_V2 a27 63 33 + SWITCH_V2 a 63 33 SWITCH_V3 var4 0x1d61723dd 79 7 @@ -36,29 +36,29 @@ FUNCTION foo () : {tuple, []} DUP a - POP a107 + POP a STORE arg183 var225 INCA - INC a25 + INC a DECA DEC a - ADD a217 a a + ADD a a a SUB arg35 arg165 var74 MUL 44 35 "foo" - DIV 263838340369912686645632650718169038811 a24 a + DIV 263838340369912686645632650718169038811 a a MOD var113 arg80 arg207 - POW a176 a a123 + POW a a a LT a 78 var81 @@ -66,11 +66,11 @@ FUNCTION foo () : {tuple, []} EQ 85 a arg164 - ELT a161 arg226 a168 + ELT a arg226 a - EGT a131 1 var250 + EGT a 1 var250 - NEQ a85 a a83 + NEQ a a a AND var255 0x294a24f6 var189 @@ -80,35 +80,35 @@ FUNCTION foo () : {tuple, []} TUPLE 5019186157739257888756115213149493826410 - ELEMENT arg148 var25 a219 + ELEMENT arg148 var25 a - MAP_EMPTY a135 + MAP_EMPTY a - MAP_LOOKUP a82 a a143 + MAP_LOOKUP a a a - MAP_LOOKUPD var112 arg35 a163 var112 + MAP_LOOKUPD var112 arg35 a var112 - MAP_UPDATE false a0 a56 a + MAP_UPDATE false a a a MAP_DELETE arg180 a var1 MAP_MEMBER a { true => 4} 94 - MAP_FROM_LIST () a159 + MAP_FROM_LIST () a NIL arg91 - IS_NIL a121 var6 + IS_NIL a var6 - CONS arg185 "foo" a114 + CONS arg185 "foo" a - HD a150 var124 + HD a var124 TL arg223 a - LENGTH var216 a143 + LENGTH var216 a - APPEND { 203961992615221001243597889146034217896 => 0x1f53a1843} 281217554184165828643225535776787296845 a177 + APPEND { 203961992615221001243597889146034217896 => 0x1f53a1843} 281217554184165828643225535776787296845 a STR_JOIN a a 7144184027126178769820155907121270843348 @@ -116,15 +116,15 @@ FUNCTION foo () : {tuple, []} ADDR_TO_STR a arg216 - STR_REVERSE a174 @ak_nv5B93FPzRHrGNmMdTDfGdd5xGZvep3MVSpJqzcQmMp59bBCv + STR_REVERSE a @ak_nv5B93FPzRHrGNmMdTDfGdd5xGZvep3MVSpJqzcQmMp59bBCv INT_TO_ADDR arg127 var207 VARIANT a a 0x1f7b72200 a - VARIANT_TEST a26 arg217 a + VARIANT_TEST a arg217 a - VARIANT_ELEMENT a86 arg103 arg108 + VARIANT_ELEMENT a arg103 arg108 BITS_NONEA @@ -132,31 +132,31 @@ FUNCTION foo () : {tuple, []} BITS_ALLA - BITS_ALL a164 + BITS_ALL a - BITS_ALL_N a221 arg135 + BITS_ALL_N a arg135 - BITS_SET arg150 a48 { 0x1a715e2a6 => 3} + BITS_SET arg150 a { 0x1a715e2a6 => 3} BITS_CLEAR arg98 a arg164 - BITS_TEST a a242 (| [0,0,3] | 2 | (1, "foo", ()) |) + BITS_TEST a a (| [0,0,3] | 2 | (1, "foo", ()) |) - BITS_SUM a244 a71 + BITS_SUM a a BITS_OR var20 var186 a - BITS_AND a187 4 arg203 + BITS_AND a 4 arg203 BITS_DIFF var200 arg247 var20 - ADDRESS a237 + ADDRESS a - BALANCE a231 + BALANCE a ORIGIN arg216 - CALLER a27 + CALLER a GASPRICE arg119 @@ -178,15 +178,15 @@ FUNCTION foo () : {tuple, []} GAS var35 - LOG0 a a85 + LOG0 a a LOG1 arg94 arg86 arg208 - LOG2 a113 (| [0,1,3] | 2 | (1, "foo", ()) |) arg238 var108 + LOG2 a (| [0,1,3] | 2 | (1, "foo", ()) |) arg238 var108 LOG3 arg255 arg15 arg211 var139 arg44 - LOG4 @ak_nv5B93FPzRHrGNmMdTDfGdd5xGZvep3MVSpJqzcQmMp59bBCv a247 a 9 a38 a + LOG4 @ak_nv5B93FPzRHrGNmMdTDfGdd5xGZvep3MVSpJqzcQmMp59bBCv a a 9 a a DEACTIVATE