1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

updated fift/func

This commit is contained in:
ton 2019-12-12 19:16:25 +04:00
parent b6f6788532
commit d41ce55305
31 changed files with 717 additions and 66 deletions

View file

@ -713,8 +713,8 @@ gas_limits#_ remaining:int64 _:^[ max_limit:int64 cur_limit:int64 credit:int64 ]
= VmGasLimits; = VmGasLimits;
_ libraries:(HashmapE 256 ^Cell) = VmLibraries; _ libraries:(HashmapE 256 ^Cell) = VmLibraries;
vm_ctl_data$_ nargs:int14 stack:(Maybe VmStack) save:VmSaveList vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
cp:int16 = VmControlData; cp:(Maybe int16) = VmControlData;
vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont; vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
vmc_quit$1000 exit_code:int32 = VmCont; vmc_quit$1000 exit_code:int32 = VmCont;

View file

@ -199,10 +199,13 @@ void usage() {
int main(int argc, char* const argv[]) { int main(int argc, char* const argv[]) {
int i; int i;
int new_verbosity_level = VERBOSITY_NAME(INFO); int new_verbosity_level = VERBOSITY_NAME(INFO);
bool dump_state = false; bool dump_state = false, dump_vmcont = false;
auto zerostate = std::make_unique<block::ZerostateInfo>(); auto zerostate = std::make_unique<block::ZerostateInfo>();
while ((i = getopt(argc, argv, "Shv:")) != -1) { while ((i = getopt(argc, argv, "CShv:")) != -1) {
switch (i) { switch (i) {
case 'C':
dump_vmcont = true;
break;
case 'S': case 'S':
dump_state = true; dump_state = true;
break; break;
@ -230,12 +233,13 @@ int main(int argc, char* const argv[]) {
vm::CellSlice cs{vm::NoVm(), boc}; vm::CellSlice cs{vm::NoVm(), boc};
cs.print_rec(std::cout); cs.print_rec(std::cout);
std::cout << std::endl; std::cout << std::endl;
auto& type = dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block; auto& type = !dump_vmcont
std::string type_name = dump_state ? "ShardState" : "Block"; ? (dump_state ? (const tlb::TLB&)block::gen::t_ShardStateUnsplit : block::gen::t_Block)
: block::gen::t_VmCont;
type.print_ref(std::cout, boc); type.print_ref(std::cout, boc);
std::cout << std::endl; std::cout << std::endl;
bool ok = type.validate_ref(boc); bool ok = type.validate_ref(boc);
std::cout << "(" << (ok ? "" : "in") << "valid " << type_name << ")" << std::endl; std::cout << "(" << (ok ? "" : "in") << "valid " << type << ")" << std::endl;
} }
} }
if (!done) { if (!done) {

View file

@ -310,14 +310,14 @@ x{A938} @Defop(8u+1) MODPOW2#
x{A984} @Defop MULDIV x{A984} @Defop MULDIV
x{A985} @Defop MULDIVR x{A985} @Defop MULDIVR
x{A98C} @Defop MULDIVMOD x{A98C} @Defop MULDIVMOD
x{A9A4} @Defop MULRSHIFT
x{A9A5} @Defop MULRSHIFTR
x{A9B4} @Defop(8u+1) MULRSHIFT#
x{A9B5} @Defop(8u+1) MULRSHIFTR#
x{A9C4} @Defop LSHIFTDIV x{A9C4} @Defop LSHIFTDIV
x{A9C5} @Defop LSHIFTDIVR x{A9C5} @Defop LSHIFTDIVR
x{A9D4} @Defop(8u+1) LSHIFT#DIV x{A9D4} @Defop(8u+1) LSHIFT#DIV
x{A9D5} @Defop(8u+1) LSHIFT#DIVR x{A9D5} @Defop(8u+1) LSHIFT#DIVR
x{A9E4} @Defop MULRSHIFT
x{A9E5} @Defop MULRSHIFTR
x{A9F4} @Defop(8u+1) MULRSHIFT#
x{A9F5} @Defop(8u+1) MULRSHIFTR#
x{AA} @Defop(8u+1) LSHIFT# x{AA} @Defop(8u+1) LSHIFT#
x{AB} @Defop(8u+1) RSHIFT# x{AB} @Defop(8u+1) RSHIFT#
x{AC} @Defop LSHIFT x{AC} @Defop LSHIFT
@ -972,12 +972,17 @@ x{F800} @Defop ACCEPT
x{F801} @Defop SETGASLIMIT x{F801} @Defop SETGASLIMIT
x{F80F} @Defop COMMIT x{F80F} @Defop COMMIT
x{F810} @Defop RANDU256
x{F811} @Defop RAND
x{F814} @Defop SETRAND
x{F815} dup @Defop ADDRAND @Defop RANDOMIZE
x{F82} @Defop(4u) GETPARAM x{F82} @Defop(4u) GETPARAM
x{F823} @Defop NOW x{F823} @Defop NOW
x{F824} @Defop BLOCKLT x{F824} @Defop BLOCKLT
x{F825} @Defop LTIME x{F825} @Defop LTIME
x{F826} @Defop BALANCE x{F826} @Defop RANDSEED
x{F827} @Defop RANDSEED x{F827} @Defop BALANCE
x{F828} @Defop MYADDR x{F828} @Defop MYADDR
x{F829} @Defop CONFIGROOT x{F829} @Defop CONFIGROOT
x{F830} @Defop CONFIGDICT x{F830} @Defop CONFIGDICT

View file

@ -26,9 +26,14 @@ recursive list-delete-range {
} : $pfx? } : $pfx?
// ( s -- ? ) checks whether s is an option (a string beginning with '-') // ( s -- ? ) checks whether s is an option (a string beginning with '-')
{ dup $len 1 > { "-" $pfx? } { drop false } cond } : is-opt? { dup $len 1 > { "-" $pfx? } { drop false } cond } : is-opt?
// ( s -- ? ) checks whether s is a digit option
{ 2 $| drop 1 $| nip $>B 8 B>u@ dup 57 <= swap 48 >= and } : is-digit-opt?
0 box constant disable-digit-opts
// ( l -- s i or 0 ) finds first string in l beginning with '-' // ( l -- s i or 0 ) finds first string in l beginning with '-'
{ 0 { 1+ over null? { 2drop 0 true } { { 0 { 1+ over null? { 2drop 0 true } {
swap uncons over is-opt? { drop swap true } { nip swap false } cond swap uncons over is-opt?
{ disable-digit-opts @ { over is-digit-opt? not } { true } cond } { false } cond
{ drop swap true } { nip swap false } cond
} cond } until } cond } until
} : list-find-opt } : list-find-opt
// ( -- s i or 0 ) finds first option in cmdline args // ( -- s i or 0 ) finds first option in cmdline args
@ -51,11 +56,15 @@ recursive list-delete-range {
// ( s -- s' null or s' s'' ) Splits long option --opt=arg at '=' // ( s -- s' null or s' s'' ) Splits long option --opt=arg at '='
{ dup "=" $pos 1+ ?dup { tuck $| swap rot 1- $| drop swap } { null } cond { dup "=" $pos 1+ ?dup { tuck $| swap rot 1- $| drop swap } { null } cond
} : split-longopt } : split-longopt
// ( l -- f or 0 ) Extracts global option flags from first entry of l
{ dup null? { drop 0 } { car get-opt-flags -256 and } cond
} : get-global-option-flags
variable options-list variable options-list
// ( l -- i or 0 ) // ( l -- i or 0 )
// parses command line arguments according to option description list l // parses command line arguments according to option description list l
// and returns index i of first incorrect option // and returns index i of first incorrect option
{ options-list ! { dup options-list ! get-global-option-flags
256 and disable-digit-opts !
{ first-opt dup 0= { true } { { first-opt dup 0= { true } {
swap dup "--" $pfx? { // i s swap dup "--" $pfx? { // i s
dup $len 2 = { drop dup 1 $*del.. 0 true } { dup $len 2 = { drop dup 1 $*del.. 0 true } {
@ -116,5 +125,7 @@ anon constant opt-list-marker
{ 6 2swap 4 tuple } : short-long-option-?arg { 6 2swap 4 tuple } : short-long-option-?arg
// ( o s -- s' ) Adds help message to option // ( o s -- s' ) Adds help message to option
' , : option-help ' , : option-help
// ( s -- o ) Creates a generic help message // ( s f -- o ) Creates a generic help message
{ 'nop 8 "" 3 roll 4 tuple } : generic-help { swap 'nop rot "" 3 roll 4 tuple } : generic-help-setopt
{ 0 generic-help-setopt } : generic-help
256 constant disable-digit-options

View file

@ -2356,6 +2356,25 @@ void interpret_db_run_vm_parallel(IntCtx& ctx) {
do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, threads_n, tasks_n); do_interpret_db_run_vm_parallel(ctx.error_stream, ctx.stack, ctx.ton_db, threads_n, tasks_n);
} }
void interpret_store_vm_cont(vm::Stack& stack) {
auto vmcont = stack.pop_cont();
auto cb = stack.pop_builder();
if (!vmcont->serialize(cb.write())) {
throw IntError{"cannot serialize vm continuation"};
}
stack.push_builder(std::move(cb));
}
void interpret_fetch_vm_cont(vm::Stack& stack) {
auto cs = stack.pop_cellslice();
auto vmcont = vm::Continuation::deserialize(cs.write());
if (vmcont.is_null()) {
throw IntError{"cannot deserialize vm continuation"};
}
stack.push_cellslice(std::move(cs));
stack.push_cont(std::move(vmcont));
}
Ref<vm::Box> cmdline_args{true}; Ref<vm::Box> cmdline_args{true};
void interpret_get_fixed_cmdline_arg(vm::Stack& stack, int n) { void interpret_get_fixed_cmdline_arg(vm::Stack& stack, int n) {
@ -2858,6 +2877,8 @@ void init_words_vm(Dictionary& d) {
d.def_ctx_word("gasrunvmctx ", std::bind(interpret_run_vm_c7, _1, true)); d.def_ctx_word("gasrunvmctx ", std::bind(interpret_run_vm_c7, _1, true));
d.def_ctx_word("dbrunvm ", interpret_db_run_vm); d.def_ctx_word("dbrunvm ", interpret_db_run_vm);
d.def_ctx_word("dbrunvm-parallel ", interpret_db_run_vm_parallel); d.def_ctx_word("dbrunvm-parallel ", interpret_db_run_vm_parallel);
d.def_stack_word("vmcont, ", interpret_store_vm_cont);
d.def_stack_word("vmcont@ ", interpret_fetch_vm_cont);
} }
void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]) { void import_cmdline_args(Dictionary& d, std::string arg0, int n, const char* const argv[]) {

View file

@ -431,26 +431,25 @@ bool Op::compute_used_vars(const CodeBlob& code, bool edit) {
} }
case _While: { case _While: {
// while (block0 || left) block1; // while (block0 || left) block1;
// ... { block0 left block1 } block0 left next // ... block0 left { block1 block0 left } next
VarDescrList after_cond_first{next_var_info}; VarDescrList new_var_info{next_var_info};
after_cond_first += left;
code.compute_used_code_vars(block0, after_cond_first, false);
VarDescrList new_var_info{block0->var_info};
bool changes = false; bool changes = false;
do { do {
code.compute_used_code_vars(block1, block0->var_info, changes); VarDescrList after_cond{new_var_info};
VarDescrList after_cond{block1->var_info};
after_cond += left; after_cond += left;
code.compute_used_code_vars(block0, after_cond, changes); code.compute_used_code_vars(block0, after_cond, changes);
code.compute_used_code_vars(block1, block0->var_info, changes);
std::size_t n = new_var_info.size(); std::size_t n = new_var_info.size();
new_var_info += block0->var_info; new_var_info += block1->var_info;
new_var_info.clear_last(); new_var_info.clear_last();
if (changes) { if (changes) {
break; break;
} }
changes = (new_var_info.size() == n); changes = (new_var_info.size() == n);
} while (changes <= edit); } while (changes <= edit);
return set_var_info(std::move(new_var_info)); new_var_info += left;
code.compute_used_code_vars(block0, new_var_info, edit);
return set_var_info(block0->var_info);
} }
case _Until: { case _Until: {
// until (block0 || left); // until (block0 || left);

9
crypto/func/test/a10.fc Normal file
View file

@ -0,0 +1,9 @@
_ f(int a, int x) {
int y = 0;
int z = 0;
while ((y = x * x) > a) {
x -= 1;
z = 1;
}
return (y, z);
}

20
crypto/func/test/a6_4.fc Normal file
View file

@ -0,0 +1,20 @@
var calc_root(m) {
int base = 1;
repeat(70) { base *= 10; }
var (a, b, c) = (1, 0, - m);
var (p1, q1, p2, q2) = (1, 0, 0, 1);
do {
int k = -1;
var (a1, b1, c1) = (0, 0, 0);
do {
k += 1;
(a1, b1, c1) = (a, b, c);
c += b;
c += b += a;
} until (c > 0);
(a, b, c) = (- c1, - b1, - a1);
(p1, q1) = (k * p1 + q1, p1);
(p2, q2) = (k * p2 + q2, p2);
} until (p1 > base);
return (p1, q1, p2, q2);
}

22
crypto/func/test/w8.fc Normal file
View file

@ -0,0 +1,22 @@
int check_signatures(msg_hash, signatures, signers, bitmask_size) impure {
var bitmask = 0;
var id = -1;
do {
(id, var signature, var f) = signatures.udict_get_next?(32, id);
if (f){
var sig = signature.preload_bits(512);
var public_key = -1;
do {
(public_key, var cs, var _found) = signers.udict_get_next?(256, public_key);
if (_found){
if (check_signature(msg_hash, sig, public_key)){
var signer_index = cs~load_uint(bitmask_size);
bitmask = bitmask | (1 << (signer_index - 1));
}
}
} until (~ _found);
;; signature~touch();
}
} until (~ f);
return bitmask;
}

8
crypto/func/test/w9.fc Normal file
View file

@ -0,0 +1,8 @@
_ g(s) {
var (z, t) = (17, s);
while (z > 0) {
t = s;
z -= 1;
}
return ~ t;
}

View file

@ -14,7 +14,7 @@ begin-options
+"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-v2-wallet.fif, with private key loaded from file <filename-base>.pk " +"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-v2-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr +"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr
+"<order-file> is a text file with lines `SEND <dest-addr> <amount>`" +"<order-file> is a text file with lines `SEND <dest-addr> <amount>`"
generic-help disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option "n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help "Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option "b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -14,7 +14,7 @@ begin-options
+"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-wallet.fif, with private key loaded from file <filename-base>.pk " +"Creates a request with up to 254 orders loaded from <order-file> to high-load (sub)wallet created by new-highload-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr +"and address from <filename-base><subwallet-id>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +cr
+"<order-file> is a text file with lines `SEND <dest-addr> <amount>`" +"<order-file> is a text file with lines `SEND <dest-addr> <amount>`"
generic-help disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option "n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help "Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option "b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -15,7 +15,7 @@
throw_unless(33, msg_seqno == stored_seqno); throw_unless(33, msg_seqno == stored_seqno);
throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key)); throw_unless(34, check_signature(slice_hash(in_msg), signature, public_key));
accept_message(); accept_message();
cs~touch_slice(); cs~touch();
if (cs.slice_refs()) { if (cs.slice_refs()) {
var mode = cs~load_uint(8); var mode = cs~load_uint(8);
send_raw_message(cs~load_ref(), mode); send_raw_message(cs~load_ref(), mode);

View file

@ -30,7 +30,7 @@ slice do_verify_message(slice in_msg, int seqno, int public_key) {
(int stored_seqno, int public_key) = load_state(); (int stored_seqno, int public_key) = load_state();
var cs = do_verify_message(in_msg, stored_seqno, public_key); var cs = do_verify_message(in_msg, stored_seqno, public_key);
accept_message(); accept_message();
cs~touch_slice(); cs~touch();
if (cs.slice_refs()) { if (cs.slice_refs()) {
var mode = cs~load_uint(8); var mode = cs~load_uint(8);
send_raw_message(cs~load_ref(), mode); send_raw_message(cs~load_ref(), mode);
@ -57,11 +57,7 @@ cell prepare_send_message(int mode, cell msg) method_id {
return prepare_send_message_with_seqno(mode, msg, seqno()); return prepare_send_message_with_seqno(mode, msg, seqno());
} }
slice verify_message(slice msg) method_id { slice verify_message(slice msg) method_id {
var (stored_seqno, public_key) = load_state(); var (stored_seqno, public_key) = load_state();
return do_verify_message(msg, stored_seqno, public_key); return do_verify_message(msg, stored_seqno, public_key);
} }

View file

@ -21,6 +21,8 @@ forall X -> X null() asm "PUSHNULL";
int now() asm "NOW"; int now() asm "NOW";
slice my_address() asm "MYADDR"; slice my_address() asm "MYADDR";
tuple get_balance() asm "BALANCE"; tuple get_balance() asm "BALANCE";
int cur_lt() asm "LTIME";
int block_lt() asm "BLOCKLT";
int cell_hash(cell c) asm "HASHCU"; int cell_hash(cell c) asm "HASHCU";
int slice_hash(slice s) asm "HASHSU"; int slice_hash(slice s) asm "HASHSU";
@ -154,5 +156,9 @@ int cell_null?(cell c) asm "ISNULL";
() send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG"; () send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG";
() set_code(cell new_code) impure asm "SETCODE"; () set_code(cell new_code) impure asm "SETCODE";
slice touch_slice(slice s) asm "NOP"; int random() impure asm "RANDU256";
(slice,()) ~touch_slice(slice s) asm "NOP"; int rand(int range) impure asm "RAND";
int get_seed() impure asm "RANDSEED";
int set_seed() impure asm "SETRAND";
() randomize(int x) impure asm "ADDRAND";
() randomize_lt() impure asm "LTIME" "ADDRAND";

View file

@ -14,7 +14,7 @@ begin-options
" <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab " <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to advanced wallet created by new-wallet-v2.fif, with private key loaded from file <filename-base>.pk " +"Creates a request to advanced wallet created by new-wallet-v2.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option "n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help "Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option "b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -14,7 +14,7 @@ begin-options
" <filename-base> <dest-addr> <subwallet-id> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab " <filename-base> <dest-addr> <subwallet-id> <seqno> <amount> [-n|-b] [-t<timeout>] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to advanced wallet created by new-wallet-v3.fif, with private key loaded from file <filename-base>.pk " +"Creates a request to advanced wallet created by new-wallet-v3.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option "n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help "Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option "b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -13,7 +13,7 @@ begin-options
" <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab " <filename-base> <dest-addr> <seqno> <amount> [-n|-b] [-B <body-boc>] [-C <comment>] [<savefile>]" +cr +tab
+"Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file <filename-base>.pk " +"Creates a request to simple wallet created by new-wallet.fif, with private key loaded from file <filename-base>.pk "
+"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)" +"and address from <filename-base>.addr, and saves it into <savefile>.boc ('wallet-query.boc' by default)"
generic-help disable-digit-options generic-help-setopt
"n" "--no-bounce" { false =: allow-bounce } short-long-option "n" "--no-bounce" { false =: allow-bounce } short-long-option
"Clears bounce flag" option-help "Clears bounce flag" option-help
"b" "--force-bounce" { true =: force-bounce } short-long-option "b" "--force-bounce" { true =: force-bounce } short-long-option

View file

@ -102,13 +102,15 @@ unsigned long long get_special_value(std::string str) {
unsigned long long val = 0; unsigned long long val = 0;
int bits = 0; int bits = 0;
if (str[0] == '#') { if (str[0] == '#') {
while (i < n) { for (; i < n; i++) {
int c = str[i++]; int c = str[i];
if (c == '_') { if (c == '_') {
break; break;
} }
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
c -= '0'; c -= '0';
} else if (c >= 'A' && c <= 'F') {
c -= 'A' - 10;
} else if (c >= 'a' && c <= 'f') { } else if (c >= 'a' && c <= 'f') {
c -= 'a' - 10; c -= 'a' - 10;
} else { } else {
@ -146,6 +148,9 @@ unsigned long long get_special_value(std::string str) {
while (bits && !((val >> (64 - bits)) & 1)) { while (bits && !((val >> (64 - bits)) & 1)) {
--bits; --bits;
} }
if (bits) {
--bits;
}
} }
if (bits == 64) { if (bits == 64) {
return 0; return 0;

View file

@ -33,6 +33,12 @@ const NatWidth t_Nat{32};
const Anything t_Anything; const Anything t_Anything;
const RefAnything t_RefCell; const RefAnything t_RefCell;
std::string TLB::get_type_name() const {
std::ostringstream os;
print_type(os);
return os.str();
}
bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const { bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
int t = get_tag(cs); int t = get_tag(cs);
return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false"); return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false");

View file

@ -196,6 +196,7 @@ class TLB {
virtual std::ostream& print_type(std::ostream& os) const { virtual std::ostream& print_type(std::ostream& os) const {
return os << "<unknown-TLB-type>"; return os << "<unknown-TLB-type>";
} }
std::string get_type_name() const;
virtual bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const; virtual bool print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const;
virtual bool print(PrettyPrinter& pp, const vm::CellSlice& cs) const { virtual bool print(PrettyPrinter& pp, const vm::CellSlice& cs) const {
vm::CellSlice cs_copy{cs}; vm::CellSlice cs_copy{cs};

View file

@ -32,6 +32,17 @@ bool Continuation::has_c0() const {
return cont_data && cont_data->save.c[0].not_null(); return cont_data && cont_data->save.c[0].not_null();
} }
bool ControlRegs::clear() {
for (unsigned i = 0; i < creg_num; i++) {
c[i].clear();
}
for (unsigned i = 0; i < dreg_num; i++) {
d[i].clear();
}
c7.clear();
return true;
}
StackEntry ControlRegs::get(unsigned idx) const { StackEntry ControlRegs::get(unsigned idx) const {
if (idx < creg_num) { if (idx < creg_num) {
return get_c(idx); return get_c(idx);
@ -108,6 +119,7 @@ ControlRegs& ControlRegs::operator&=(const ControlRegs& save) {
} }
bool ControlRegs::serialize(CellBuilder& cb) const { bool ControlRegs::serialize(CellBuilder& cb) const {
// _ cregs:(HashmapE 4 VmStackValue) = VmSaveList;
Dictionary dict{4}; Dictionary dict{4};
CellBuilder cb2; CellBuilder cb2;
for (int i = 0; i < creg_num; i++) { for (int i = 0; i < creg_num; i++) {
@ -126,8 +138,34 @@ bool ControlRegs::serialize(CellBuilder& cb) const {
std::move(dict).append_dict_to_bool(cb); std::move(dict).append_dict_to_bool(cb);
} }
bool ControlRegs::deserialize(CellSlice& cs, int mode) {
// _ cregs:(HashmapE 4 VmStackValue) = VmSaveList;
Ref<Cell> root;
return cs.fetch_maybe_ref(root) && deserialize(std::move(root), mode);
}
bool ControlRegs::deserialize(Ref<Cell> root, int mode) {
try {
clear();
Dictionary dict{std::move(root), 4};
return dict.check_for_each([this, mode](Ref<CellSlice> val, td::ConstBitPtr key, int n) {
StackEntry value;
return value.deserialize(val.write(), mode) && val->empty_ext() && set((int)key.get_uint(4), std::move(value));
});
} catch (VmError&) {
return false;
}
}
bool ControlData::clear() {
stack.clear();
save.clear();
nargs = cp = -1;
return true;
}
bool ControlData::serialize(CellBuilder& cb) const { bool ControlData::serialize(CellBuilder& cb) const {
// vm_ctl_data$_ nargs:(Maybe int13) stack:(Maybe VmStack) save:VmSaveList // vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData; // cp:(Maybe int16) = VmControlData;
return cb.store_bool_bool(nargs >= 0) // vm_ctl_data$_ nargs:(Maybe ... return cb.store_bool_bool(nargs >= 0) // vm_ctl_data$_ nargs:(Maybe ...
&& (nargs < 0 || cb.store_long_bool(nargs, 13)) // ... int13) && (nargs < 0 || cb.store_long_bool(nargs, 13)) // ... int13)
@ -138,16 +176,88 @@ bool ControlData::serialize(CellBuilder& cb) const {
&& (cp == -1 || cb.store_long_bool(cp, 16)); // ... int16) && (cp == -1 || cb.store_long_bool(cp, 16)); // ... int16)
} }
bool ControlData::deserialize(CellSlice& cs, int mode) {
// vm_ctl_data$_ nargs:(Maybe uint13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData;
nargs = cp = -1;
stack.clear();
bool f;
return cs.fetch_bool_to(f) && (!f || cs.fetch_uint_to(13, nargs)) // nargs:(Maybe uint13)
&& cs.fetch_bool_to(f) && (!f || Stack::deserialize_to(cs, stack, mode)) // stack:(Maybe VmStack)
&& save.deserialize(cs, mode) // save:VmSaveList
&& cs.fetch_bool_to(f) && (!f || (cs.fetch_int_to(16, cp) && cp != -1)); // cp:(Maybe int16)
}
bool Continuation::serialize_ref(CellBuilder& cb) const { bool Continuation::serialize_ref(CellBuilder& cb) const {
vm::CellBuilder cb2; vm::CellBuilder cb2;
return serialize(cb2) && cb.store_ref_bool(cb2.finalize()); return serialize(cb2) && cb.store_ref_bool(cb2.finalize());
} }
Ref<Continuation> Continuation::deserialize(CellSlice& cs, int mode) {
if (mode & 0x1002) {
return {};
}
mode |= 0x1000;
switch (cs.bselect_ext(6, 0x100f011100010001ULL)) {
case 0:
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
return OrdCont::deserialize(cs, mode);
case 1:
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
return ArgContExt::deserialize(cs, mode);
case 2:
// vmc_quit$1000 exit_code:int32 = VmCont;
return QuitCont::deserialize(cs, mode);
case 3:
// vmc_quit_exc$1001 = VmCont;
return ExcQuitCont::deserialize(cs, mode);
case 4:
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
return RepeatCont::deserialize(cs, mode);
case 5:
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
return UntilCont::deserialize(cs, mode);
case 6:
// vmc_again$110001 body:^VmCont = VmCont;
return AgainCont::deserialize(cs, mode);
case 7:
// vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
return WhileCont::deserialize(cs, mode | 0x2000);
case 8:
// vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
return WhileCont::deserialize(cs, mode & ~0x2000);
case 9:
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
return PushIntCont::deserialize(cs, mode);
default:
return {};
}
}
bool Continuation::deserialize_to(Ref<Cell> cell, Ref<Continuation>& cont, int mode) {
if (cell.is_null()) {
cont.clear();
return false;
}
CellSlice cs = load_cell_slice(std::move(cell));
return deserialize_to(cs, cont, mode & ~0x1000) && cs.empty_ext();
}
bool QuitCont::serialize(CellBuilder& cb) const { bool QuitCont::serialize(CellBuilder& cb) const {
// vmc_quit$1000 exit_code:int32 = VmCont; // vmc_quit$1000 exit_code:int32 = VmCont;
return cb.store_long_bool(8, 4) && cb.store_long_bool(exit_code, 32); return cb.store_long_bool(8, 4) && cb.store_long_bool(exit_code, 32);
} }
Ref<QuitCont> QuitCont::deserialize(CellSlice& cs, int mode) {
// vmc_quit$1000 exit_code:int32 = VmCont;
int exit_code;
if (cs.fetch_ulong(4) == 8 && cs.fetch_int_to(32, exit_code)) {
return Ref<QuitCont>{true, exit_code};
} else {
return {};
}
}
int ExcQuitCont::jump(VmState* st) const & { int ExcQuitCont::jump(VmState* st) const & {
int n = 0; int n = 0;
try { try {
@ -164,6 +274,11 @@ bool ExcQuitCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(9, 4); return cb.store_long_bool(9, 4);
} }
Ref<ExcQuitCont> ExcQuitCont::deserialize(CellSlice& cs, int mode) {
// vmc_quit_exc$1001 = VmCont;
return cs.fetch_ulong(4) == 9 ? Ref<ExcQuitCont>{true} : Ref<ExcQuitCont>{};
}
int PushIntCont::jump(VmState* st) const & { int PushIntCont::jump(VmState* st) const & {
VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)"; VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)";
st->get_stack().push_smallint(push_val); st->get_stack().push_smallint(push_val);
@ -181,6 +296,19 @@ bool PushIntCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(15, 4) && cb.store_long_bool(push_val, 32) && next->serialize_ref(cb); return cb.store_long_bool(15, 4) && cb.store_long_bool(push_val, 32) && next->serialize_ref(cb);
} }
Ref<PushIntCont> PushIntCont::deserialize(CellSlice& cs, int mode) {
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
int value;
Ref<Cell> ref;
Ref<Continuation> next;
if (cs.fetch_ulong(4) == 15 && cs.fetch_int_to(32, value) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), next, mode)) {
return Ref<PushIntCont>{true, value, std::move(next)};
} else {
return {};
}
}
int ArgContExt::jump(VmState* st) const & { int ArgContExt::jump(VmState* st) const & {
st->adjust_cr(data.save); st->adjust_cr(data.save);
if (data.cp != -1) { if (data.cp != -1) {
@ -202,6 +330,18 @@ bool ArgContExt::serialize(CellBuilder& cb) const {
return cb.store_long_bool(1, 2) && data.serialize(cb) && ext->serialize_ref(cb); return cb.store_long_bool(1, 2) && data.serialize(cb) && ext->serialize_ref(cb);
} }
Ref<ArgContExt> ArgContExt::deserialize(CellSlice& cs, int mode) {
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
ControlData cdata;
Ref<Cell> ref;
Ref<Continuation> next;
mode &= ~0x1000;
return cs.fetch_ulong(2) == 1 && cdata.deserialize(cs, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), next, mode)
? Ref<ArgContExt>{true, std::move(next), std::move(cdata)}
: Ref<ArgContExt>{};
}
int RepeatCont::jump(VmState* st) const & { int RepeatCont::jump(VmState* st) const & {
VM_LOG(st) << "repeat " << count << " more times (slow)\n"; VM_LOG(st) << "repeat " << count << " more times (slow)\n";
if (count <= 0) { if (count <= 0) {
@ -236,6 +376,20 @@ bool RepeatCont::serialize(CellBuilder& cb) const {
after->serialize_ref(cb); after->serialize_ref(cb);
} }
Ref<RepeatCont> RepeatCont::deserialize(CellSlice& cs, int mode) {
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
long long count;
Ref<Cell> ref;
Ref<Continuation> body, after;
if (cs.fetch_ulong(5) == 0x14 && cs.fetch_uint_to(63, count) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), after, mode)) {
return Ref<RepeatCont>{true, std::move(body), std::move(after), count};
} else {
return {};
}
}
int VmState::repeat(Ref<Continuation> body, Ref<Continuation> after, long long count) { int VmState::repeat(Ref<Continuation> body, Ref<Continuation> after, long long count) {
if (count <= 0) { if (count <= 0) {
body.clear(); body.clear();
@ -268,6 +422,17 @@ bool AgainCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(0x31, 6) && body->serialize_ref(cb); return cb.store_long_bool(0x31, 6) && body->serialize_ref(cb);
} }
Ref<AgainCont> AgainCont::deserialize(CellSlice& cs, int mode) {
// vmc_again$110001 body:^VmCont = VmCont;
Ref<Cell> ref;
Ref<Continuation> body;
if (cs.fetch_ulong(6) == 0x31 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode)) {
return Ref<AgainCont>{true, std::move(body)};
} else {
return {};
}
}
int VmState::again(Ref<Continuation> body) { int VmState::again(Ref<Continuation> body) {
return jump(Ref<AgainCont>{true, std::move(body)}); return jump(Ref<AgainCont>{true, std::move(body)});
} }
@ -305,6 +470,18 @@ bool UntilCont::serialize(CellBuilder& cb) const {
return cb.store_long_bool(0x30, 6) && body->serialize_ref(cb) && after->serialize_ref(cb); return cb.store_long_bool(0x30, 6) && body->serialize_ref(cb) && after->serialize_ref(cb);
} }
Ref<UntilCont> UntilCont::deserialize(CellSlice& cs, int mode) {
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
Ref<Cell> ref;
Ref<Continuation> body, after;
if (cs.fetch_ulong(6) == 0x30 && cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), body, mode) &&
cs.fetch_ref_to(ref) && deserialize_to(std::move(ref), after, mode)) {
return Ref<UntilCont>{true, std::move(body), std::move(after)};
} else {
return {};
}
}
int VmState::until(Ref<Continuation> body, Ref<Continuation> after) { int VmState::until(Ref<Continuation> body, Ref<Continuation> after) {
if (!body->has_c0()) { if (!body->has_c0()) {
set_c0(Ref<UntilCont>{true, body, std::move(after)}); set_c0(Ref<UntilCont>{true, body, std::move(after)});
@ -371,6 +548,22 @@ bool WhileCont::serialize(CellBuilder& cb) const {
body->serialize_ref(cb) && after->serialize_ref(cb); body->serialize_ref(cb) && after->serialize_ref(cb);
} }
Ref<WhileCont> WhileCont::deserialize(CellSlice& cs, int mode) {
// vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
// vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
bool at_body;
Ref<Cell> ref;
Ref<Continuation> cond, body, after;
if (cs.fetch_ulong(5) == 0x19 && cs.fetch_bool_to(at_body) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), cond, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), body, mode) && cs.fetch_ref_to(ref) &&
deserialize_to(std::move(ref), after, mode)) {
return Ref<WhileCont>{true, std::move(cond), std::move(body), std::move(after), !at_body};
} else {
return {};
}
}
int VmState::loop_while(Ref<Continuation> cond, Ref<Continuation> body, Ref<Continuation> after) { int VmState::loop_while(Ref<Continuation> cond, Ref<Continuation> body, Ref<Continuation> after) {
if (!cond->has_c0()) { if (!cond->has_c0()) {
set_c0(Ref<WhileCont>{true, cond, std::move(body), std::move(after), true}); set_c0(Ref<WhileCont>{true, cond, std::move(body), std::move(after), true});
@ -392,7 +585,18 @@ int OrdCont::jump_w(VmState* st) & {
bool OrdCont::serialize(CellBuilder& cb) const { bool OrdCont::serialize(CellBuilder& cb) const {
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont; // vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
return cb.store_long_bool(1, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb); return cb.store_long_bool(0, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb, 0x1000);
}
Ref<OrdCont> OrdCont::deserialize(CellSlice& cs, int mode) {
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
ControlData cdata;
StackEntry val;
mode &= ~0x1000;
return cs.fetch_ulong(2) == 0 && cdata.deserialize(cs, mode) && val.deserialize(cs, 0x4000) &&
val.is(StackEntry::t_slice)
? Ref<OrdCont>{true, std::move(val).as_slice(), std::move(cdata)}
: Ref<OrdCont>{};
} }
void VmState::init_cregs(bool same_c3, bool push_0) { void VmState::init_cregs(bool same_c3, bool push_0) {

View file

@ -37,6 +37,7 @@ struct ControlRegs {
Ref<Continuation> c[creg_num]; // c0..c3 Ref<Continuation> c[creg_num]; // c0..c3
Ref<Cell> d[dreg_num]; // c4..c5 Ref<Cell> d[dreg_num]; // c4..c5
Ref<Tuple> c7; // c7 Ref<Tuple> c7; // c7
bool clear();
Ref<Continuation> get_c(unsigned idx) const { Ref<Continuation> get_c(unsigned idx) const {
return idx < creg_num ? c[idx] : Ref<Continuation>{}; return idx < creg_num ? c[idx] : Ref<Continuation>{};
} }
@ -136,6 +137,8 @@ struct ControlRegs {
ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0 ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0
ControlRegs& operator^=(ControlRegs&& save); ControlRegs& operator^=(ControlRegs&& save);
bool serialize(CellBuilder& cb) const; bool serialize(CellBuilder& cb) const;
bool deserialize(CellSlice& cs, int mode = 0);
bool deserialize(Ref<Cell> root, int mode = 0);
}; };
struct ControlData { struct ControlData {
@ -151,7 +154,9 @@ struct ControlData {
} }
ControlData(int _cp, Ref<Stack> _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) { ControlData(int _cp, Ref<Stack> _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) {
} }
bool clear();
bool serialize(CellBuilder& cb) const; bool serialize(CellBuilder& cb) const;
bool deserialize(CellSlice& cs, int mode = 0);
}; };
class Continuation : public td::CntObject { class Continuation : public td::CntObject {
@ -164,10 +169,6 @@ class Continuation : public td::CntObject {
virtual const ControlData* get_cdata() const { virtual const ControlData* get_cdata() const {
return 0; return 0;
} }
virtual bool serialize(CellBuilder& cb) const {
return false;
}
bool serialize_ref(CellBuilder& cb) const;
bool has_c0() const; bool has_c0() const;
Continuation() { Continuation() {
} }
@ -181,6 +182,15 @@ class Continuation : public td::CntObject {
return *this; return *this;
} }
~Continuation() override = default; ~Continuation() override = default;
virtual bool serialize(CellBuilder& cb) const {
return false;
}
bool serialize_ref(CellBuilder& cb) const;
static Ref<Continuation> deserialize(CellSlice& cs, int mode = 0);
static bool deserialize_to(CellSlice& cs, Ref<Continuation>& cont, int mode = 0) {
return (cont = deserialize(cs, mode)).not_null();
}
static bool deserialize_to(Ref<Cell> cell, Ref<Continuation>& cont, int mode = 0);
}; };
class QuitCont : public Continuation { class QuitCont : public Continuation {
@ -194,6 +204,7 @@ class QuitCont : public Continuation {
return ~exit_code; return ~exit_code;
} }
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<QuitCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class ExcQuitCont : public Continuation { class ExcQuitCont : public Continuation {
@ -202,6 +213,7 @@ class ExcQuitCont : public Continuation {
~ExcQuitCont() override = default; ~ExcQuitCont() override = default;
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<ExcQuitCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class PushIntCont : public Continuation { class PushIntCont : public Continuation {
@ -215,6 +227,7 @@ class PushIntCont : public Continuation {
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
int jump_w(VmState* st) & override; int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<PushIntCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class RepeatCont : public Continuation { class RepeatCont : public Continuation {
@ -229,6 +242,7 @@ class RepeatCont : public Continuation {
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
int jump_w(VmState* st) & override; int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<RepeatCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class AgainCont : public Continuation { class AgainCont : public Continuation {
@ -241,6 +255,7 @@ class AgainCont : public Continuation {
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
int jump_w(VmState* st) & override; int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<AgainCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class UntilCont : public Continuation { class UntilCont : public Continuation {
@ -253,6 +268,7 @@ class UntilCont : public Continuation {
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
int jump_w(VmState* st) & override; int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<UntilCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class WhileCont : public Continuation { class WhileCont : public Continuation {
@ -267,6 +283,7 @@ class WhileCont : public Continuation {
int jump(VmState* st) const & override; int jump(VmState* st) const & override;
int jump_w(VmState* st) & override; int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<WhileCont> deserialize(CellSlice& cs, int mode = 0);
}; };
class ArgContExt : public Continuation { class ArgContExt : public Continuation {
@ -274,9 +291,13 @@ class ArgContExt : public Continuation {
Ref<Continuation> ext; Ref<Continuation> ext;
public: public:
ArgContExt(Ref<Continuation> _ext) : data(), ext(_ext) { ArgContExt(Ref<Continuation> _ext) : data(), ext(std::move(_ext)) {
} }
ArgContExt(Ref<Continuation> _ext, Ref<Stack> _stack) : data(_stack), ext(_ext) { ArgContExt(Ref<Continuation> _ext, Ref<Stack> _stack) : data(std::move(_stack)), ext(std::move(_ext)) {
}
ArgContExt(Ref<Continuation> _ext, const ControlData& _cdata) : data(_cdata), ext(std::move(_ext)) {
}
ArgContExt(Ref<Continuation> _ext, ControlData&& _cdata) : data(std::move(_cdata)), ext(std::move(_ext)) {
} }
ArgContExt(const ArgContExt&) = default; ArgContExt(const ArgContExt&) = default;
ArgContExt(ArgContExt&&) = default; ArgContExt(ArgContExt&&) = default;
@ -293,6 +314,7 @@ class ArgContExt : public Continuation {
return new ArgContExt{*this}; return new ArgContExt{*this};
} }
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<ArgContExt> deserialize(CellSlice& cs, int mode = 0);
}; };
class OrdCont : public Continuation { class OrdCont : public Continuation {
@ -310,6 +332,10 @@ class OrdCont : public Continuation {
OrdCont(Ref<CellSlice> _code, int _cp, Ref<Stack> _stack, int nargs = -1) OrdCont(Ref<CellSlice> _code, int _cp, Ref<Stack> _stack, int nargs = -1)
: data(_cp, std::move(_stack), nargs), code(std::move(_code)) { : data(_cp, std::move(_stack), nargs), code(std::move(_code)) {
} }
OrdCont(Ref<CellSlice> _code, const ControlData& _cdata) : data(_cdata), code(std::move(_code)) {
}
OrdCont(Ref<CellSlice> _code, ControlData&& _cdata) : data(std::move(_cdata)), code(std::move(_code)) {
}
OrdCont(const OrdCont&) = default; OrdCont(const OrdCont&) = default;
OrdCont(OrdCont&&) = default; OrdCont(OrdCont&&) = default;
~OrdCont() override = default; ~OrdCont() override = default;
@ -342,6 +368,7 @@ class OrdCont : public Continuation {
return Ref<OrdCont>{true, *this}; return Ref<OrdCont>{true, *this};
} }
bool serialize(CellBuilder& cb) const override; bool serialize(CellBuilder& cb) const override;
static Ref<OrdCont> deserialize(CellSlice& cs, int mode = 0);
}; };
struct GasLimits { struct GasLimits {

View file

@ -688,7 +688,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
return cb.store_long_bool(0x02ff, 16); return cb.store_long_bool(0x02ff, 16);
} else if (!(mode & 1) && val->signed_fits_bits(64)) { } else if (!(mode & 1) && val->signed_fits_bits(64)) {
// vm_stk_tinyint#01 value:int64 = VmStackValue; // vm_stk_tinyint#01 value:int64 = VmStackValue;
return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 256); return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 64);
} else { } else {
// vm_stk_int#0201_ value:int257 = VmStackValue; // vm_stk_int#0201_ value:int257 = VmStackValue;
return cb.store_long_bool(0x0200 / 2, 15) && cb.store_int256_bool(std::move(val), 257); return cb.store_long_bool(0x0200 / 2, 15) && cb.store_int256_bool(std::move(val), 257);
@ -701,7 +701,7 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits } // _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice; // st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
const auto& cs = *static_cast<Ref<CellSlice>>(ref); const auto& cs = *static_cast<Ref<CellSlice>>(ref);
return cb.store_long_bool(4, 8) // vm_stk_slice#04 _:VmCellSlice = VmStackValue; return ((mode & 0x1000) || cb.store_long_bool(4, 8)) // vm_stk_slice#04 _:VmCellSlice = VmStackValue;
&& cb.store_ref_bool(cs.get_base_cell()) // _ cell:^Cell && cb.store_ref_bool(cs.get_base_cell()) // _ cell:^Cell
&& cb.store_long_bool(cs.cur_pos(), 10) // st_bits:(## 10) && cb.store_long_bool(cs.cur_pos(), 10) // st_bits:(## 10)
&& cb.store_long_bool(cs.cur_pos() + cs.size(), 10) // end_bits:(## 10) && cb.store_long_bool(cs.cur_pos() + cs.size(), 10) // end_bits:(## 10)
@ -738,6 +738,110 @@ bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
} }
} }
bool StackEntry::deserialize(CellSlice& cs, int mode) {
clear();
int t = (mode & 0xf000) ? ((mode >> 12) & 15) : (int)cs.prefetch_ulong(8);
switch (t) {
case 0:
// vm_stk_null#00 = VmStackValue;
return cs.advance(8);
case 1: {
// vm_stk_tinyint#01 value:int64 = VmStackValue;
td::RefInt256 val;
return !(mode & 1) && cs.advance(8) && cs.fetch_int256_to(64, val) && set_int(std::move(val));
}
case 2: {
t = (int)cs.prefetch_ulong(16) & 0x1ff;
if (t == 0xff) {
// vm_stk_nan#02ff = VmStackValue;
return cs.advance(16) && set_int(td::RefInt256{true});
} else {
// vm_stk_int#0201_ value:int257 = VmStackValue;
td::RefInt256 val;
return cs.fetch_ulong(15) == 0x0200 / 2 && cs.fetch_int256_to(257, val) && set_int(std::move(val));
}
}
case 3: {
// vm_stk_cell#03 cell:^Cell = VmStackValue;
return cs.have_refs() && cs.advance(8) && set(t_cell, cs.fetch_ref());
}
case 4: {
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
// vm_stk_slice#04 _:VmCellSlice = VmStackValue;
unsigned st_bits, end_bits, st_ref, end_ref;
Ref<Cell> cell;
Ref<CellSlice> csr;
return ((mode & 0xf000) || cs.advance(8)) // vm_stk_slice#04
&& cs.fetch_ref_to(cell) // cell:^Cell
&& cs.fetch_uint_to(10, st_bits) // st_bits:(## 10)
&& cs.fetch_uint_to(10, end_bits) // end_bits:(## 10)
&& st_bits <= end_bits // { st_bits <= end_bits }
&& cs.fetch_uint_to(3, st_ref) // st_ref:(#<= 4)
&& cs.fetch_uint_to(3, end_ref) // end_ref:(#<= 4)
&& st_ref <= end_ref && end_ref <= 4 // { st_ref <= end_ref }
&& (csr = load_cell_slice_ref(std::move(cell))).not_null() // load cell slice
&& csr->have(end_bits, end_ref) &&
csr.write().skip_last(csr->size() - end_bits, csr->size_refs() - end_ref) &&
csr.write().skip_first(st_bits, st_ref) && set(t_slice, std::move(csr));
}
case 5: {
// vm_stk_builder#05 cell:^Cell = VmStackValue;
Ref<Cell> cell;
Ref<CellSlice> csr;
Ref<CellBuilder> cb{true};
return cs.advance(8) && cs.fetch_ref_to(cell) && (csr = load_cell_slice_ref(std::move(cell))).not_null() &&
cb.write().append_cellslice_bool(std::move(csr)) && set(t_builder, std::move(cb));
}
case 6: {
// vm_stk_cont#06 cont:VmCont = VmStackValue;
Ref<Continuation> cont;
return !(mode & 2) && cs.advance(8) && Continuation::deserialize_to(cs, cont, mode) &&
set(t_vmcont, std::move(cont));
}
case 7: {
// vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
int n;
if (!(cs.advance(8) && cs.fetch_uint_to(16, n))) {
return false;
}
Ref<Tuple> tuple{true, n};
auto& t = tuple.write();
if (n > 1) {
Ref<Cell> head, tail;
n--;
if (!(cs.fetch_ref_to(head) && cs.fetch_ref_to(tail) && t[n].deserialize(std::move(tail), mode))) {
return false;
}
vm::CellSlice cs2;
while (--n > 0) {
if (!(cs2.load(std::move(head)) && cs2.fetch_ref_to(head) && cs2.fetch_ref_to(tail) && cs2.empty_ext() &&
t[n].deserialize(std::move(tail), mode))) {
return false;
}
}
if (!t[0].deserialize(std::move(head), mode)) {
return false;
}
} else if (n == 1) {
return cs.have_refs() && t[0].deserialize(cs.fetch_ref(), mode);
}
return set(t_tuple, std::move(tuple));
}
default:
return false;
}
}
bool StackEntry::deserialize(Ref<Cell> cell, int mode) {
if (cell.is_null()) {
clear();
return false;
}
CellSlice cs = load_cell_slice(std::move(cell));
return deserialize(cs, mode) && cs.empty_ext();
}
bool Stack::serialize(vm::CellBuilder& cb, int mode) const { bool Stack::serialize(vm::CellBuilder& cb, int mode) const {
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack; // vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
unsigned n = depth(); unsigned n = depth();
@ -758,4 +862,45 @@ bool Stack::serialize(vm::CellBuilder& cb, int mode) const {
return cb.store_ref_bool(std::move(rest)) && stack[n - 1].serialize(cb, mode); return cb.store_ref_bool(std::move(rest)) && stack[n - 1].serialize(cb, mode);
} }
bool Stack::deserialize(vm::CellSlice& cs, int mode) {
clear();
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
int n;
if (!cs.fetch_uint_to(24, n)) {
return false;
}
if (!n) {
return true;
}
stack.resize(n);
Ref<Cell> rest;
if (!(cs.fetch_ref_to(rest) && stack[n - 1].deserialize(cs, mode))) {
clear();
return false;
}
for (int i = n - 2; i >= 0; --i) {
// vm_stk_cons#_ {n:#} rest:^(VmStackList n) tos:VmStackValue = VmStackList (n + 1);
vm::CellSlice cs2 = load_cell_slice(std::move(rest));
if (!(cs2.fetch_ref_to(rest) && stack[i].deserialize(cs2, mode) && cs2.empty_ext())) {
clear();
return false;
}
}
if (!load_cell_slice(std::move(rest)).empty_ext()) {
clear();
return false;
}
return true;
}
bool Stack::deserialize_to(vm::CellSlice& cs, Ref<Stack>& stack, int mode) {
stack = Ref<Stack>{true};
if (stack.unique_write().deserialize(cs, mode)) {
return true;
} else {
stack.clear();
return false;
}
}
} // namespace vm } // namespace vm

View file

@ -135,6 +135,9 @@ class StackEntry {
tp = t_null; tp = t_null;
return *this; return *this;
} }
bool set_int(td::RefInt256 value) {
return set(t_int, std::move(value));
}
bool empty() const { bool empty() const {
return tp == t_null; return tp == t_null;
} }
@ -168,6 +171,8 @@ class StackEntry {
} }
// mode: +1 = disable short ints, +2 = disable continuations // mode: +1 = disable short ints, +2 = disable continuations
bool serialize(vm::CellBuilder& cb, int mode = 0) const; bool serialize(vm::CellBuilder& cb, int mode = 0) const;
bool deserialize(vm::CellSlice& cs, int mode = 0);
bool deserialize(Ref<Cell> cell, int mode = 0);
private: private:
static bool is_list(const StackEntry* se); static bool is_list(const StackEntry* se);
@ -195,6 +200,11 @@ class StackEntry {
Ref<T> move_as() & { Ref<T> move_as() & {
return tp == tag ? Ref<T>{td::static_cast_ref(), std::move(ref)} : td::Ref<T>{}; return tp == tag ? Ref<T>{td::static_cast_ref(), std::move(ref)} : td::Ref<T>{};
} }
bool set(Type _tp, RefAny _ref) {
tp = _tp;
ref = std::move(_ref);
return ref.not_null() || tp == t_null;
}
public: public:
static StackEntry make_list(std::vector<StackEntry>&& elems); static StackEntry make_list(std::vector<StackEntry>&& elems);
@ -511,6 +521,8 @@ class Stack : public td::CntObject {
// mode: +1 = add eoln, +2 = Lisp-style lists // mode: +1 = add eoln, +2 = Lisp-style lists
void dump(std::ostream& os, int mode = 1) const; void dump(std::ostream& os, int mode = 1) const;
bool serialize(vm::CellBuilder& cb, int mode = 0) const; bool serialize(vm::CellBuilder& cb, int mode = 0) const;
bool deserialize(vm::CellSlice& cs, int mode = 0);
static bool deserialize_to(vm::CellSlice& cs, Ref<Stack>& stack, int mode = 0);
}; };
} // namespace vm } // namespace vm

View file

@ -165,6 +165,8 @@ int exec_set_global_common(VmState* st, unsigned idx) {
if (idx >= 255) { if (idx >= 255) {
throw VmError{Excno::range_chk, "tuple index out of range"}; throw VmError{Excno::range_chk, "tuple index out of range"};
} }
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
auto tpay = tuple_extend_set_index(tuple, idx, std::move(x)); auto tpay = tuple_extend_set_index(tuple, idx, std::move(x));
if (tpay > 0) { if (tpay > 0) {
st->consume_tuple_gas(tpay); st->consume_tuple_gas(tpay);
@ -193,7 +195,8 @@ void register_ton_config_ops(OpcodeTable& cp0) {
.insert(OpcodeInstr::mksimple(0xf823, 16, "NOW", std::bind(exec_get_param, _1, 3, "NOW"))) .insert(OpcodeInstr::mksimple(0xf823, 16, "NOW", std::bind(exec_get_param, _1, 3, "NOW")))
.insert(OpcodeInstr::mksimple(0xf824, 16, "BLOCKLT", std::bind(exec_get_param, _1, 4, "BLOCKLT"))) .insert(OpcodeInstr::mksimple(0xf824, 16, "BLOCKLT", std::bind(exec_get_param, _1, 4, "BLOCKLT")))
.insert(OpcodeInstr::mksimple(0xf825, 16, "LTIME", std::bind(exec_get_param, _1, 5, "LTIME"))) .insert(OpcodeInstr::mksimple(0xf825, 16, "LTIME", std::bind(exec_get_param, _1, 5, "LTIME")))
.insert(OpcodeInstr::mkfixedrange(0xf826, 0xf828, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) .insert(OpcodeInstr::mksimple(0xf826, 16, "RANDSEED", std::bind(exec_get_param, _1, 6, "RANDSEED")))
.insert(OpcodeInstr::mksimple(0xf827, 16, "BALANCE", std::bind(exec_get_param, _1, 7, "BALANCE")))
.insert(OpcodeInstr::mksimple(0xf828, 16, "MYADDR", std::bind(exec_get_param, _1, 8, "MYADDR"))) .insert(OpcodeInstr::mksimple(0xf828, 16, "MYADDR", std::bind(exec_get_param, _1, 8, "MYADDR")))
.insert(OpcodeInstr::mksimple(0xf829, 16, "CONFIGROOT", std::bind(exec_get_param, _1, 9, "CONFIGROOT"))) .insert(OpcodeInstr::mksimple(0xf829, 16, "CONFIGROOT", std::bind(exec_get_param, _1, 9, "CONFIGROOT")))
.insert(OpcodeInstr::mkfixedrange(0xf82a, 0xf830, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param)) .insert(OpcodeInstr::mkfixedrange(0xf82a, 0xf830, 16, 4, instr::dump_1c("GETPARAM "), exec_get_var_param))
@ -206,6 +209,112 @@ void register_ton_config_ops(OpcodeTable& cp0) {
.insert(OpcodeInstr::mkfixedrange(0xf861, 0xf880, 16, 5, instr::dump_1c_and(31, "SETGLOB "), exec_set_global)); .insert(OpcodeInstr::mkfixedrange(0xf861, 0xf880, 16, 5, instr::dump_1c_and(31, "SETGLOB "), exec_set_global));
} }
static constexpr int randseed_idx = 6;
td::RefInt256 generate_randu256(VmState* st) {
auto tuple = st->get_c7();
auto t1 = tuple_index(*tuple, 0).as_tuple_range(255);
if (t1.is_null()) {
throw VmError{Excno::type_chk, "intermediate value is not a tuple"};
}
auto seedv = tuple_index(*t1, randseed_idx).as_int();
if (seedv.is_null()) {
throw VmError{Excno::type_chk, "random seed is not an integer"};
}
unsigned char seed[32];
if (!seedv->export_bytes(seed, 32, false)) {
throw VmError{Excno::range_chk, "random seed out of range"};
}
unsigned char hash[64];
digest::hash_str<digest::SHA512>(hash, seed, 32);
if (!seedv.write().import_bytes(hash, 32, false)) {
throw VmError{Excno::range_chk, "cannot store new random seed"};
}
td::RefInt256 res{true};
if (!res.write().import_bytes(hash + 32, 32, false)) {
throw VmError{Excno::range_chk, "cannot store new random number"};
}
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
tuple.write()[0].clear();
t1.write().at(randseed_idx) = std::move(seedv);
st->consume_tuple_gas(t1);
tuple.write().at(0) = std::move(t1);
st->consume_tuple_gas(tuple);
st->set_c7(std::move(tuple));
return res;
}
int exec_randu256(VmState* st) {
VM_LOG(st) << "execute RANDU256";
st->get_stack().push_int(generate_randu256(st));
return 0;
}
int exec_rand_int(VmState* st) {
VM_LOG(st) << "execute RAND";
auto& stack = st->get_stack();
stack.check_underflow(1);
auto x = stack.pop_int_finite();
auto y = generate_randu256(st);
typename td::BigInt256::DoubleInt tmp{0};
tmp.add_mul(*x, *y);
tmp.rshift(256, -1).normalize();
stack.push_int(td::RefInt256{true, tmp});
return 0;
}
int exec_set_rand(VmState* st, bool mix) {
VM_LOG(st) << "execute " << (mix ? "ADDRAND" : "SETRAND");
auto& stack = st->get_stack();
stack.check_underflow(1);
auto x = stack.pop_int_finite();
if (!x->unsigned_fits_bits(256)) {
throw VmError{Excno::range_chk, "new random seed out of range"};
}
auto tuple = st->get_c7();
auto t1 = tuple_index(*tuple, 0).as_tuple_range(255);
if (t1.is_null()) {
throw VmError{Excno::type_chk, "intermediate value is not a tuple"};
}
if (mix) {
auto seedv = tuple_index(*t1, randseed_idx).as_int();
if (seedv.is_null()) {
throw VmError{Excno::type_chk, "random seed is not an integer"};
}
unsigned char buffer[64], hash[32];
if (!std::move(seedv)->export_bytes(buffer, 32, false)) {
throw VmError{Excno::range_chk, "random seed out of range"};
}
if (!x->export_bytes(buffer + 32, 32, false)) {
throw VmError{Excno::range_chk, "mixed seed value out of range"};
}
digest::hash_str<digest::SHA256>(hash, buffer, 64);
if (!x.write().import_bytes(hash, 32, false)) {
throw VmError{Excno::range_chk, "new random seed value out of range"};
}
}
static auto empty_tuple = Ref<Tuple>{true};
st->set_c7(empty_tuple); // optimization; use only if no exception can be thrown until true set_c7()
tuple.write()[0].clear();
auto tpay = tuple_extend_set_index(t1, randseed_idx, std::move(x));
if (tpay > 0) {
st->consume_tuple_gas(tpay);
}
tuple.unique_write()[0] = std::move(t1);
st->consume_tuple_gas(tuple);
st->set_c7(std::move(tuple));
return 0;
}
void register_prng_ops(OpcodeTable& cp0) {
using namespace std::placeholders;
cp0.insert(OpcodeInstr::mksimple(0xf810, 16, "RANDU256", exec_randu256))
.insert(OpcodeInstr::mksimple(0xf811, 16, "RAND", exec_rand_int))
.insert(OpcodeInstr::mksimple(0xf814, 16, "SETRAND", std::bind(exec_set_rand, _1, false)))
.insert(OpcodeInstr::mksimple(0xf815, 16, "ADDRAND", std::bind(exec_set_rand, _1, true)));
}
int exec_compute_hash(VmState* st, int mode) { int exec_compute_hash(VmState* st, int mode) {
VM_LOG(st) << "execute HASH" << (mode & 1 ? 'S' : 'C') << 'U'; VM_LOG(st) << "execute HASH" << (mode & 1 ? 'S' : 'C') << 'U';
Stack& stack = st->get_stack(); Stack& stack = st->get_stack();
@ -677,7 +786,7 @@ int exec_set_lib_code(VmState* st) {
} }
return install_output_action(st, cb.finalize()); return install_output_action(st, cb.finalize());
} }
int exec_change_lib(VmState* st) { int exec_change_lib(VmState* st) {
VM_LOG(st) << "execute CHANGELIB"; VM_LOG(st) << "execute CHANGELIB";
Stack& stack = st->get_stack(); Stack& stack = st->get_stack();
@ -688,9 +797,9 @@ int exec_change_lib(VmState* st) {
throw VmError{Excno::range_chk, "library hash must be non-negative"}; throw VmError{Excno::range_chk, "library hash must be non-negative"};
} }
CellBuilder cb; CellBuilder cb;
if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n) if (!(cb.store_ref_bool(get_actions(st)) // out_list$_ {n:#} prev:^(OutList n)
&& cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4 && cb.store_long_bool(0x26fa1dd4, 32) // action_change_library#26fa1dd4
&& cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 } && cb.store_long_bool(mode * 2, 8) // mode:(## 7) { mode <= 2 }
&& cb.store_int256_bool(hash, 256, false))) { // libref:LibRef = OutAction; && cb.store_int256_bool(hash, 256, false))) { // libref:LibRef = OutAction;
throw VmError{Excno::cell_ov, "cannot serialize library hash into an output action cell"}; throw VmError{Excno::cell_ov, "cannot serialize library hash into an output action cell"};
} }
@ -710,6 +819,7 @@ void register_ton_message_ops(OpcodeTable& cp0) {
void register_ton_ops(OpcodeTable& cp0) { void register_ton_ops(OpcodeTable& cp0) {
register_basic_gas_ops(cp0); register_basic_gas_ops(cp0);
register_ton_gas_ops(cp0); register_ton_gas_ops(cp0);
register_prng_ops(cp0);
register_ton_config_ops(cp0); register_ton_config_ops(cp0);
register_ton_crypto_ops(cp0); register_ton_crypto_ops(cp0);
register_ton_currency_address_ops(cp0); register_ton_currency_address_ops(cp0);

View file

@ -1505,7 +1505,7 @@ The general encoding of a {\tt DIV}, {\tt DIVMOD}, or {\tt MOD} operation is {\t
\begin{itemize} \begin{itemize}
\item $0\leq m\leq1$ --- Indicates whether there is pre-multiplication ({\tt MULDIV} operation and its variants), possibly replaced by a left shift. \item $0\leq m\leq1$ --- Indicates whether there is pre-multiplication ({\tt MULDIV} operation and its variants), possibly replaced by a left shift.
\item $0\leq s\leq2$ --- Indicates whether either the multiplication or the division have been replaced by shifts: $s=0$---no replacement, $s=1$---division replaced by a right shift, $s=2$---multiplication replaced by a left shift (possible only for $m=1$). \item $0\leq s\leq2$ --- Indicates whether either the multiplication or the division have been replaced by shifts: $s=0$---no replacement, $s=1$---division replaced by a right shift, $s=2$---multiplication replaced by a left shift (possible only for $m=1$).
\item $0\leq c\leq1$ --- Indicates whether there is a constant one-byte argument $tt$ for the shift operator (if $s\neq0$). For $s=0$, $c=0$. If $c=1$, then $0\leq tt\leq 255$, and the shift is performed by $tt+1$ bits. \item $0\leq c\leq1$ --- Indicates whether there is a constant one-byte argument $tt$ for the shift operator (if $s\neq0$). For $s=0$, $c=0$. If $c=1$, then $0\leq tt\leq 255$, and the shift is performed by $tt+1$ bits. If $s\neq0$ and $c=0$, then the shift amount is provided to the instruction as a top-of-stack {\em Integer\/} in range $0\ldots256$.
\item $1\leq d\leq3$ --- Indicates which results of division are required: $1$---only the quotient, $2$---only the remainder, $3$---both. \item $1\leq d\leq3$ --- Indicates which results of division are required: $1$---only the quotient, $2$---only the remainder, $3$---both.
\item $0\leq f\leq2$ --- Rounding mode: $0$---floor, $1$---nearest integer, $2$---ceiling (cf.~\ptref{sp:div.round}). \item $0\leq f\leq2$ --- Rounding mode: $0$---floor, $1$---nearest integer, $2$---ceiling (cf.~\ptref{sp:div.round}).
\end{itemize} \end{itemize}
@ -1524,6 +1524,14 @@ Examples:
\item {\tt A938$tt$} --- {\tt MODPOW2 $tt+1$}: ($x$ -- $x\bmod 2^{tt+1}$). \item {\tt A938$tt$} --- {\tt MODPOW2 $tt+1$}: ($x$ -- $x\bmod 2^{tt+1}$).
\item {\tt A985} --- {\tt MULDIVR} ($x$ $y$ $z$ -- $q'$), where $q'=\lfloor xy/z+1/2\rfloor$. \item {\tt A985} --- {\tt MULDIVR} ($x$ $y$ $z$ -- $q'$), where $q'=\lfloor xy/z+1/2\rfloor$.
\item {\tt A98C} --- {\tt MULDIVMOD} ($x$ $y$ $z$ -- $q$ $r$), where $q:=\lfloor x\cdot y/z\rfloor$, $r:=x\cdot y\bmod z$ (same as {\tt */MOD} in Forth). \item {\tt A98C} --- {\tt MULDIVMOD} ($x$ $y$ $z$ -- $q$ $r$), where $q:=\lfloor x\cdot y/z\rfloor$, $r:=x\cdot y\bmod z$ (same as {\tt */MOD} in Forth).
\item {\tt A9A4} --- {\tt MULRSHIFT} ($x$ $y$ $z$ -- $\lfloor xy\cdot2^{-z}\rfloor$) for $0\leq z\leq 256$.
\item {\tt A9A5} --- {\tt MULRSHIFTR} ($x$ $y$ $z$ -- $\lfloor xy\cdot2^{-z}+1/2\rfloor$) for $0\leq z\leq 256$.
\item {\tt A9B4$tt$} --- {\tt MULRSHIFT $tt+1$} ($x$ $y$ -- $\lfloor xy\cdot 2^{-tt-1}\rfloor$).
\item {\tt A9B5$tt$} --- {\tt MULRSHIFTR $tt+1$} ($x$ $y$ -- $\lfloor xy\cdot 2^{-tt-1}+1/2\rfloor$).
\item {\tt A9C4} --- {\tt LSHIFTDIV} ($x$ $y$ $z$ -- $\lfloor 2^zx/y\rfloor$) for $0\leq z\leq 256$.
\item {\tt A9C5} --- {\tt LSHIFTDIVR} ($x$ $y$ $z$ -- $\lfloor 2^zx/y+1/2\rfloor$) for $0\leq z\leq 256$.
\item {\tt A9D4$tt$} --- {\tt LSHIFTDIV $tt+1$} ($x$ $y$ -- $\lfloor 2^{tt+1}x/y\rfloor$).
\item {\tt A9D5$tt$} --- {\tt LSHIFTDIVR $tt+1$} ($x$ $y$ -- $\lfloor 2^{tt+1}x/y+1/2\rfloor$).
\end{itemize} \end{itemize}
The most useful of these operations are {\tt DIV}, {\tt DIVMOD}, {\tt MOD}, {\tt DIVR}, {\tt DIVC}, {\tt MODPOW2 $t$}, and {\tt RSHIFTR $t$} (for integer arithmetic); and {\tt MULDIVMOD}, {\tt MULDIV}, {\tt MULDIVR}, {\tt LSHIFTDIVR $t$}, and {\tt MULRSHIFTR $t$} (for fixed-point arithmetic). The most useful of these operations are {\tt DIV}, {\tt DIVMOD}, {\tt MOD}, {\tt DIVR}, {\tt DIVC}, {\tt MODPOW2 $t$}, and {\tt RSHIFTR $t$} (for integer arithmetic); and {\tt MULDIVMOD}, {\tt MULDIV}, {\tt MULDIVR}, {\tt LSHIFTDIVR $t$}, and {\tt MULRSHIFTR $t$} (for fixed-point arithmetic).
@ -2165,8 +2173,12 @@ Of the following primitives, only the first two are ``pure'' in the sense that t
\end{itemize} \end{itemize}
\nxsubpoint\emb{Pseudo-random number generator primitives} \nxsubpoint\emb{Pseudo-random number generator primitives}
The pseudo-random number generator uses the random seed and (sometimes) other data kept in {\tt c7}. The pseudo-random number generator uses the random seed (parameter \#6, cf.~\ptref{sp:prim.conf.param}), an unsigned 256-bit {\em Integer}, and (sometimes) other data kept in {\tt c7}. The initial value of the random seed before a smart contract is executed in TON Blockchain is a hash of the smart contract address and the global block random seed. If there are several runs of the same smart contract inside a block, then all of these runs will have the same random seed. This can be fixed, for example, by running {\tt LTIME; ADDRAND} before using the pseudo-random number generator for the first time.
\begin{itemize} \begin{itemize}
\item {\tt F810} --- {\tt RANDU256} ( -- $x$), generates a new pseudo-random unsigned 256-bit {\em Integer}~$x$. The algorithm is as follows: if $r$ is the old value of the random seed, considered as a 32-byte array (by constructing the big-endian representation of an unsigned 256-bit integer), then its $\opsc{sha512}(r)$ is computed; the first 32 bytes of this hash are stored as the new value $r'$ of the random seed, and the remaining 32 bytes are returned as the next random value~$x$.
\item {\tt F811} --- {\tt RAND} ($y$ -- $z$), generates a new pseudo-random integer $z$ in the range $0\ldots y-1$ (or $y\ldots-1$, if $y<0$). More precisely, an unsigned random value $x$ is generated as in {\tt RAND256U}; then $z:=\lfloor xy/2^{256}\rfloor$ is computed. Equivalent to {\tt RANDU256; MULRSHIFT 256}.
\item {\tt F814} --- {\tt SETRAND} ($x$ -- ), sets the random seed to unsigned 256-bit {\em Integer\/}~$x$.
\item {\tt F815} --- {\tt ADDRAND} ($x$ -- ), mixes unsigned 256-bit {\em Integer\/}~$x$ into the random seed $r$ by setting the random seed to $\Sha$ of the concatenation of two 32-byte strings: the first with the big-endian representation of the old seed $r$, and the second with the big-endian representation of $x$.
\item {\tt F810}--{\tt F81F} --- Reserved for pseudo-random number generator primitives. \item {\tt F810}--{\tt F81F} --- Reserved for pseudo-random number generator primitives.
\end{itemize} \end{itemize}
@ -2177,8 +2189,8 @@ The following primitives read configuration data provided in the {\em Tuple\/} s
\item {\tt F823} --- {\tt NOW} ( -- $x$), returns the current Unix time as an {\em Integer}. If it is impossible to recover the requested value starting from {\tt c7}, throws a type checking or range checking exception as appropriate. Equivalent to {\tt GETPARAM 3}. \item {\tt F823} --- {\tt NOW} ( -- $x$), returns the current Unix time as an {\em Integer}. If it is impossible to recover the requested value starting from {\tt c7}, throws a type checking or range checking exception as appropriate. Equivalent to {\tt GETPARAM 3}.
\item {\tt F824} --- {\tt BLOCKLT} ( -- $x$), returns the starting logical time of the current block. Equivalent to {\tt GETPARAM 4}. \item {\tt F824} --- {\tt BLOCKLT} ( -- $x$), returns the starting logical time of the current block. Equivalent to {\tt GETPARAM 4}.
\item {\tt F825} --- {\tt LTIME} ( -- $x$), returns the logical time of the current transaction. Equivalent to {\tt GETPARAM 5}. \item {\tt F825} --- {\tt LTIME} ( -- $x$), returns the logical time of the current transaction. Equivalent to {\tt GETPARAM 5}.
\item {\tt F826} --- {\tt BALANCE} ( -- $t$), returns the remaining balance of the smart contract as a {\em Tuple\/} consisting of an {\em Integer} (the remaining Gram balance in nanograms) and a {\em Maybe Cell} (a dictionary with 32-bit keys representing the balance of ``extra currencies''). Equivalent to {\tt GETPARAM 6}. Note that {\tt RAW} primitives such as {\tt SENDRAWMSG} do not update this field. \item {\tt F826} --- {\tt RANDSEED} ( -- $x$), returns the current random seed as an unsigned 256-bit {\em Integer}. Equivalent to {\tt GETPARAM 6}.
\item {\tt F827} --- {\tt RANDSEED} ( -- $x$), returns the current random seed as an unsigned 256-bit {\em Integer}. Equivalent to {\tt GETPARAM 7}. \item {\tt F827} --- {\tt BALANCE} ( -- $t$), returns the remaining balance of the smart contract as a {\em Tuple\/} consisting of an {\em Integer} (the remaining Gram balance in nanograms) and a {\em Maybe Cell} (a dictionary with 32-bit keys representing the balance of ``extra currencies''). Equivalent to {\tt GETPARAM 7}. Note that {\tt RAW} primitives such as {\tt SENDRAWMSG} do not update this field.
\item {\tt F828} --- {\tt MYADDR} ( -- $s$), returns the internal address of the current smart contract as a {\em Slice\/} with a {\tt MsgAddressInt}. If necessary, it can be parsed further using primitives such as {\tt PARSESTDADDR} or {\tt REWRITESTDADDR}. Equivalent to {\tt GETPARAM 8}. \item {\tt F828} --- {\tt MYADDR} ( -- $s$), returns the internal address of the current smart contract as a {\em Slice\/} with a {\tt MsgAddressInt}. If necessary, it can be parsed further using primitives such as {\tt PARSESTDADDR} or {\tt REWRITESTDADDR}. Equivalent to {\tt GETPARAM 8}.
\item {\tt F829} --- {\tt CONFIGROOT} ( -- $D$), returns the {\em Maybe Cell\/}~$D$ with the current global configuration dictionary. Equivalent to {\tt GETPARAM 9}. \item {\tt F829} --- {\tt CONFIGROOT} ( -- $D$), returns the {\em Maybe Cell\/}~$D$ with the current global configuration dictionary. Equivalent to {\tt GETPARAM 9}.
\item {\tt F830} --- {\tt CONFIGDICT} ( -- $D$ $32$), returns the global configuration dictionary along with its key length (32). Equivalent to {\tt CONFIGROOT}; {\tt PUSHINT 32}. \item {\tt F830} --- {\tt CONFIGDICT} ( -- $D$ $32$), returns the global configuration dictionary along with its key length (32). Equivalent to {\tt CONFIGROOT}; {\tt PUSHINT 32}.

View file

@ -19,6 +19,7 @@
#include "td/db/RocksDb.h" #include "td/db/RocksDb.h"
#include "rocksdb/db.h" #include "rocksdb/db.h"
#include "rocksdb/table.h"
#include "rocksdb/statistics.h" #include "rocksdb/statistics.h"
#include "rocksdb/write_batch.h" #include "rocksdb/write_batch.h"
#include "rocksdb/utilities/optimistic_transaction_db.h" #include "rocksdb/utilities/optimistic_transaction_db.h"
@ -63,6 +64,13 @@ Result<RocksDb> RocksDb::open(std::string path) {
auto statistics = rocksdb::CreateDBStatistics(); auto statistics = rocksdb::CreateDBStatistics();
{ {
rocksdb::Options options; rocksdb::Options options;
static auto cache = rocksdb::NewLRUCache(1 << 30);
rocksdb::BlockBasedTableOptions table_options;
table_options.block_cache = cache;
options.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_options));
options.manual_wal_flush = true; options.manual_wal_flush = true;
options.create_if_missing = true; options.create_if_missing = true;
options.max_background_compactions = 4; options.max_background_compactions = 4;
@ -82,6 +90,10 @@ std::unique_ptr<KeyValueReader> RocksDb::snapshot() {
} }
std::string RocksDb::stats() const { std::string RocksDb::stats() const {
std::string out;
db_->GetProperty("rocksdb.stats", &out);
//db_->GetProperty("rocksdb.cur-size-all-mem-tables", &out);
return out;
return statistics_->ToString(); return statistics_->ToString();
} }

View file

@ -60,6 +60,10 @@ class RocksDb : public KeyValue {
RocksDb &operator=(RocksDb &&); RocksDb &operator=(RocksDb &&);
~RocksDb(); ~RocksDb();
std::shared_ptr<rocksdb::OptimisticTransactionDB> raw_db() const {
return db_;
};
private: private:
std::shared_ptr<rocksdb::OptimisticTransactionDB> db_; std::shared_ptr<rocksdb::OptimisticTransactionDB> db_;
std::shared_ptr<rocksdb::Statistics> statistics_; std::shared_ptr<rocksdb::Statistics> statistics_;

View file

@ -679,7 +679,7 @@ TEST(Buffers, CyclicBufferSimple) {
auto data = td::rand_string('a', 'z', 100001); auto data = td::rand_string('a', 'z', 100001);
td::Slice write_slice = data; td::Slice write_slice = data;
td::Slice read_slice = data; td::Slice read_slice = data;
for (size_t i = 1; (int)i < options.count; i++) { for (size_t i = 1; i < options.count; i++) {
ASSERT_EQ((i - 1) * options.chunk_size, reader.reader_size()); ASSERT_EQ((i - 1) * options.chunk_size, reader.reader_size());
ASSERT_EQ((i - 1) * options.chunk_size, writer.writer_size()); ASSERT_EQ((i - 1) * options.chunk_size, writer.writer_size());
auto slice = writer.prepare_write(); auto slice = writer.prepare_write();

View file

@ -170,7 +170,11 @@ TEST(KeyValue, Bench) {
TEST(KeyValue, Stress) { TEST(KeyValue, Stress) {
return; return;
td::Slice db_name = "testdb"; td::Slice db_name = "testdb";
td::RocksDb::destroy(db_name).ignore(); size_t N = 20;
auto db_name_i = [&](size_t i) { return PSTRING() << db_name << i; };
for (size_t i = 0; i < N; i++) {
td::RocksDb::destroy(db_name_i(i)).ignore();
}
td::actor::Scheduler scheduler({6}); td::actor::Scheduler scheduler({6});
auto watcher = td::create_shared_destructor([] { td::actor::SchedulerContext::get()->stop(); }); auto watcher = td::create_shared_destructor([] { td::actor::SchedulerContext::get()->stop(); });
@ -186,9 +190,13 @@ TEST(KeyValue, Stress) {
void tear_down() override { void tear_down() override {
} }
void loop() override { void loop() override {
if (stat_at_.is_in_past()) {
stat_at_ = td::Timestamp::in(10);
LOG(ERROR) << db_->stats();
}
if (!kv_) { if (!kv_) {
kv_ = td::KeyValueAsync<td::UInt128, td::BufferSlice>( db_ = std::make_shared<td::RocksDb>(td::RocksDb::open(db_name_).move_as_ok());
std::make_unique<td::RocksDb>(td::RocksDb::open(db_name_).move_as_ok())); kv_ = td::KeyValueAsync<td::UInt128, td::BufferSlice>(db_);
set_start_at_ = td::Timestamp::now(); set_start_at_ = td::Timestamp::now();
} }
if (next_set_ && next_set_.is_in_past()) { if (next_set_ && next_set_.is_in_past()) {
@ -207,6 +215,7 @@ TEST(KeyValue, Stress) {
private: private:
std::shared_ptr<td::Destructor> watcher_; std::shared_ptr<td::Destructor> watcher_;
std::shared_ptr<td::RocksDb> db_;
td::optional<td::KeyValueAsync<td::UInt128, td::BufferSlice>> kv_; td::optional<td::KeyValueAsync<td::UInt128, td::BufferSlice>> kv_;
std::string db_name_; std::string db_name_;
int left_cnt_ = 1000000000; int left_cnt_ = 1000000000;
@ -214,6 +223,7 @@ TEST(KeyValue, Stress) {
td::Timestamp next_set_ = td::Timestamp::now(); td::Timestamp next_set_ = td::Timestamp::now();
td::Timestamp set_start_at_; td::Timestamp set_start_at_;
td::Timestamp set_finish_at_; td::Timestamp set_finish_at_;
td::Timestamp stat_at_ = td::Timestamp::in(10);
void do_set() { void do_set() {
td::UInt128 key = td::UInt128::zero(); td::UInt128 key = td::UInt128::zero();
@ -236,8 +246,10 @@ TEST(KeyValue, Stress) {
} }
} }
}; };
scheduler.run_in_context([watcher = std::move(watcher), &db_name]() mutable { scheduler.run_in_context([watcher = std::move(watcher), &db_name_i, &N]() mutable {
td::actor::create_actor<Worker>("Worker", watcher, db_name.str()).release(); for (size_t i = 0; i < N; i++) {
td::actor::create_actor<Worker>("Worker", watcher, db_name_i(i)).release();
}
watcher.reset(); watcher.reset();
}); });