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

updated fift + bugfixes

This commit is contained in:
ton 2019-12-05 16:51:51 +04:00
parent 090e0c16eb
commit ceaed40ac4
28 changed files with 530 additions and 108 deletions

View file

@ -85,6 +85,10 @@ class CellBuilder : public td::CntObject {
return idx < refs_cnt ? refs[idx] : Ref<Cell>{};
}
void reset();
bool reset_bool() {
reset();
return true;
}
CellBuilder& operator=(const CellBuilder&);
CellBuilder& operator=(CellBuilder&&);
CellBuilder& store_bytes(const char* str, std::size_t len);

View file

@ -218,6 +218,17 @@ unsigned CellSlice::get_level() const {
return l;
}
Ref<Cell> CellSlice::get_base_cell() const {
if (cell.is_null()) {
return {};
}
auto res = cell->virtualize(virt);
if (!tree_node.empty()) {
res = UsageCell::create(std::move(res), tree_node);
}
return res;
}
bool CellSlice::advance(unsigned bits) {
if (have(bits)) {
bits_st += bits;

View file

@ -137,6 +137,7 @@ class CellSlice : public td::CntObject {
}
unsigned get_cell_level() const;
unsigned get_level() const;
Ref<Cell> get_base_cell() const; // be careful with this one!
int fetch_octet();
int prefetch_octet() const;
unsigned long long prefetch_ulong_top(unsigned& bits) const;

View file

@ -107,6 +107,47 @@ ControlRegs& ControlRegs::operator&=(const ControlRegs& save) {
return *this;
}
bool ControlRegs::serialize(CellBuilder& cb) const {
Dictionary dict{4};
CellBuilder cb2;
for (int i = 0; i < creg_num; i++) {
if (c[i].not_null() &&
!(StackEntry{c[i]}.serialize(cb2) && dict.set_builder(td::BitArray<4>(i), cb2) && cb2.reset_bool())) {
return false;
}
}
for (int i = 0; i < dreg_num; i++) {
if (d[i].not_null() && !(StackEntry{d[i]}.serialize(cb2) && dict.set_builder(td::BitArray<4>(dreg_idx + i), cb2) &&
cb2.reset_bool())) {
return false;
}
}
return (c7.is_null() || (StackEntry{c7}.serialize(cb2) && dict.set_builder(td::BitArray<4>(7), cb2))) &&
std::move(dict).append_dict_to_bool(cb);
}
bool ControlData::serialize(CellBuilder& cb) const {
// vm_ctl_data$_ nargs:(Maybe int13) stack:(Maybe VmStack) save:VmSaveList
// cp:(Maybe int16) = VmControlData;
return cb.store_bool_bool(nargs >= 0) // vm_ctl_data$_ nargs:(Maybe ...
&& (nargs < 0 || cb.store_long_bool(nargs, 13)) // ... int13)
&& cb.store_bool_bool(stack.not_null()) // stack:(Maybe ...
&& (stack.is_null() || stack->serialize(cb)) // ... VmStack)
&& save.serialize(cb) // save:VmSaveList
&& cb.store_bool_bool(cp != -1) // cp:(Maybe ...
&& (cp == -1 || cb.store_long_bool(cp, 16)); // ... int16)
}
bool Continuation::serialize_ref(CellBuilder& cb) const {
vm::CellBuilder cb2;
return serialize(cb2) && cb.store_ref_bool(cb2.finalize());
}
bool QuitCont::serialize(CellBuilder& cb) const {
// vmc_quit$1000 exit_code:int32 = VmCont;
return cb.store_long_bool(8, 4) && cb.store_long_bool(exit_code, 32);
}
int ExcQuitCont::jump(VmState* st) const & {
int n = 0;
try {
@ -118,6 +159,11 @@ int ExcQuitCont::jump(VmState* st) const & {
return ~n;
}
bool ExcQuitCont::serialize(CellBuilder& cb) const {
// vmc_quit_exc$1001 = VmCont;
return cb.store_long_bool(9, 4);
}
int PushIntCont::jump(VmState* st) const & {
VM_LOG(st) << "execute implicit PUSH " << push_val << " (slow)";
st->get_stack().push_smallint(push_val);
@ -130,6 +176,11 @@ int PushIntCont::jump_w(VmState* st) & {
return st->jump(std::move(next));
}
bool PushIntCont::serialize(CellBuilder& cb) const {
// vmc_pushint$1111 value:int32 next:^VmCont = VmCont;
return cb.store_long_bool(15, 4) && cb.store_long_bool(push_val, 32) && next->serialize_ref(cb);
}
int ArgContExt::jump(VmState* st) const & {
st->adjust_cr(data.save);
if (data.cp != -1) {
@ -146,6 +197,11 @@ int ArgContExt::jump_w(VmState* st) & {
return st->jump_to(std::move(ext));
}
bool ArgContExt::serialize(CellBuilder& cb) const {
// vmc_envelope$01 cdata:VmControlData next:^VmCont = VmCont;
return cb.store_long_bool(1, 2) && data.serialize(cb) && ext->serialize_ref(cb);
}
int RepeatCont::jump(VmState* st) const & {
VM_LOG(st) << "repeat " << count << " more times (slow)\n";
if (count <= 0) {
@ -174,6 +230,12 @@ int RepeatCont::jump_w(VmState* st) & {
return st->jump(body);
}
bool RepeatCont::serialize(CellBuilder& cb) const {
// vmc_repeat$10100 count:uint63 body:^VmCont after:^VmCont = VmCont;
return cb.store_long_bool(0x14, 5) && cb.store_long_bool(count, 63) && body->serialize_ref(cb) &&
after->serialize_ref(cb);
}
int VmState::repeat(Ref<Continuation> body, Ref<Continuation> after, long long count) {
if (count <= 0) {
body.clear();
@ -201,6 +263,11 @@ int AgainCont::jump_w(VmState* st) & {
}
}
bool AgainCont::serialize(CellBuilder& cb) const {
// vmc_again$110001 body:^VmCont = VmCont;
return cb.store_long_bool(0x31, 6) && body->serialize_ref(cb);
}
int VmState::again(Ref<Continuation> body) {
return jump(Ref<AgainCont>{true, std::move(body)});
}
@ -233,6 +300,11 @@ int UntilCont::jump_w(VmState* st) & {
}
}
bool UntilCont::serialize(CellBuilder& cb) const {
// vmc_until$110000 body:^VmCont after:^VmCont = VmCont;
return cb.store_long_bool(0x30, 6) && body->serialize_ref(cb) && after->serialize_ref(cb);
}
int VmState::until(Ref<Continuation> body, Ref<Continuation> after) {
if (!body->has_c0()) {
set_c0(Ref<UntilCont>{true, body, std::move(after)});
@ -292,6 +364,13 @@ int WhileCont::jump_w(VmState* st) & {
}
}
bool WhileCont::serialize(CellBuilder& cb) const {
// vmc_while_cond$110010 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
// vmc_while_body$110011 cond:^VmCont body:^VmCont after:^VmCont = VmCont;
return cb.store_long_bool(0x19, 5) && cb.store_bool_bool(!chkcond) && cond->serialize_ref(cb) &&
body->serialize_ref(cb) && after->serialize_ref(cb);
}
int VmState::loop_while(Ref<Continuation> cond, Ref<Continuation> body, Ref<Continuation> after) {
if (!cond->has_c0()) {
set_c0(Ref<WhileCont>{true, cond, std::move(body), std::move(after), true});
@ -311,6 +390,11 @@ int OrdCont::jump_w(VmState* st) & {
return 0;
}
bool OrdCont::serialize(CellBuilder& cb) const {
// vmc_std$00 cdata:VmControlData code:VmCellSlice = VmCont;
return cb.store_long_bool(1, 2) && data.serialize(cb) && StackEntry{code}.serialize(cb);
}
void VmState::init_cregs(bool same_c3, bool push_0) {
cr.set_c0(quit0);
cr.set_c1(quit1);

View file

@ -135,6 +135,7 @@ struct ControlRegs {
ControlRegs& operator&=(const ControlRegs& save); // clears all c[i]'s which are present in save
ControlRegs& operator^=(const ControlRegs& save); // sets c[i]=save.c[i] for all save.c[i] != 0
ControlRegs& operator^=(ControlRegs&& save);
bool serialize(CellBuilder& cb) const;
};
struct ControlData {
@ -150,11 +151,12 @@ struct ControlData {
}
ControlData(int _cp, Ref<Stack> _stack, int _nargs = -1) : stack(std::move(_stack)), nargs(_nargs), cp(_cp) {
}
bool serialize(CellBuilder& cb) const;
};
class Continuation : public td::CntObject {
public:
virtual int jump(VmState* st) const& = 0;
virtual int jump(VmState* st) const & = 0;
virtual int jump_w(VmState* st) &;
virtual ControlData* get_cdata() {
return 0;
@ -162,6 +164,10 @@ class Continuation : public td::CntObject {
virtual const ControlData* get_cdata() const {
return 0;
}
virtual bool serialize(CellBuilder& cb) const {
return false;
}
bool serialize_ref(CellBuilder& cb) const;
bool has_c0() const;
Continuation() {
}
@ -184,16 +190,18 @@ class QuitCont : public Continuation {
QuitCont(int _code = 0) : exit_code(_code) {
}
~QuitCont() override = default;
int jump(VmState* st) const& override {
int jump(VmState* st) const & override {
return ~exit_code;
}
bool serialize(CellBuilder& cb) const override;
};
class ExcQuitCont : public Continuation {
public:
ExcQuitCont() = default;
~ExcQuitCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
bool serialize(CellBuilder& cb) const override;
};
class PushIntCont : public Continuation {
@ -204,8 +212,9 @@ class PushIntCont : public Continuation {
PushIntCont(int val, Ref<Continuation> _next) : push_val(val), next(_next) {
}
~PushIntCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
};
class RepeatCont : public Continuation {
@ -217,8 +226,9 @@ class RepeatCont : public Continuation {
: body(std::move(_body)), after(std::move(_after)), count(_count) {
}
~RepeatCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
};
class AgainCont : public Continuation {
@ -228,8 +238,9 @@ class AgainCont : public Continuation {
AgainCont(Ref<Continuation> _body) : body(std::move(_body)) {
}
~AgainCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
};
class UntilCont : public Continuation {
@ -239,8 +250,9 @@ class UntilCont : public Continuation {
UntilCont(Ref<Continuation> _body, Ref<Continuation> _after) : body(std::move(_body)), after(std::move(_after)) {
}
~UntilCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
};
class WhileCont : public Continuation {
@ -252,8 +264,9 @@ class WhileCont : public Continuation {
: cond(std::move(_cond)), body(std::move(_body)), after(std::move(_after)), chkcond(_chk) {
}
~WhileCont() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
bool serialize(CellBuilder& cb) const override;
};
class ArgContExt : public Continuation {
@ -268,7 +281,7 @@ class ArgContExt : public Continuation {
ArgContExt(const ArgContExt&) = default;
ArgContExt(ArgContExt&&) = default;
~ArgContExt() override = default;
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
ControlData* get_cdata() override {
return &data;
@ -279,6 +292,7 @@ class ArgContExt : public Continuation {
td::CntObject* make_copy() const override {
return new ArgContExt{*this};
}
bool serialize(CellBuilder& cb) const override;
};
class OrdCont : public Continuation {
@ -303,7 +317,7 @@ class OrdCont : public Continuation {
td::CntObject* make_copy() const override {
return new OrdCont{*this};
}
int jump(VmState* st) const& override;
int jump(VmState* st) const & override;
int jump_w(VmState* st) & override;
ControlData* get_cdata() override {
@ -321,12 +335,13 @@ class OrdCont : public Continuation {
Ref<Stack> get_stack_ref() const {
return data.stack;
}
Ref<OrdCont> copy_ord() const& {
Ref<OrdCont> copy_ord() const & {
return Ref<OrdCont>{true, *this};
}
Ref<OrdCont> copy_ord() && {
return Ref<OrdCont>{true, *this};
}
bool serialize(CellBuilder& cb) const override;
};
struct GasLimits {

View file

@ -671,4 +671,91 @@ void Stack::push_maybe_cellslice(Ref<CellSlice> cs) {
push_maybe(std::move(cs));
}
/*
*
* SERIALIZE/DESERIALIZE STACK VALUES
*
*/
bool StackEntry::serialize(vm::CellBuilder& cb, int mode) const {
switch (tp) {
case t_null:
return cb.store_long_bool(0, 8); // vm_stk_null#00 = VmStackValue;
case t_int: {
auto val = as_int();
if (!val->is_valid()) {
// vm_stk_nan#02ff = VmStackValue;
return cb.store_long_bool(0x02ff, 16);
} else if (!(mode & 1) && val->signed_fits_bits(64)) {
// vm_stk_tinyint#01 value:int64 = VmStackValue;
return cb.store_long_bool(1, 8) && cb.store_int256_bool(std::move(val), 256);
} else {
// vm_stk_int#0201_ value:int257 = VmStackValue;
return cb.store_long_bool(0x0200 / 2, 15) && cb.store_int256_bool(std::move(val), 257);
}
}
case t_cell:
// vm_stk_cell#03 cell:^Cell = VmStackValue;
return cb.store_long_bool(3, 8) && cb.store_ref_bool(as_cell());
case t_slice: {
// _ cell:^Cell st_bits:(## 10) end_bits:(## 10) { st_bits <= end_bits }
// st_ref:(#<= 4) end_ref:(#<= 4) { st_ref <= end_ref } = VmCellSlice;
const auto& cs = *static_cast<Ref<CellSlice>>(ref);
return cb.store_long_bool(4, 8) // vm_stk_slice#04 _:VmCellSlice = VmStackValue;
&& 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() + cs.size(), 10) // end_bits:(## 10)
&& cb.store_long_bool(cs.cur_ref(), 3) // st_ref:(#<= 4)
&& cb.store_long_bool(cs.cur_ref() + cs.size_refs(), 3); // end_ref:(#<= 4)
}
case t_builder:
// vm_stk_builder#05 cell:^Cell = VmStackValue;
return cb.store_long_bool(5, 8) && cb.store_ref_bool(as_builder()->finalize_copy());
case t_vmcont:
// vm_stk_cont#06 cont:VmCont = VmStackValue;
return !(mode & 2) && cb.store_long_bool(6, 8) && as_cont()->serialize(cb);
case t_tuple: {
const auto& tuple = *static_cast<Ref<Tuple>>(ref);
auto n = tuple.size();
// vm_stk_tuple#07 len:(## 16) data:(VmTuple len) = VmStackValue;
Ref<Cell> head, tail;
vm::CellBuilder cb2;
for (std::size_t i = 0; i < n; i++) {
std::swap(head, tail);
if (i > 1 &&
!(cb2.store_ref_bool(std::move(tail)) && cb2.store_ref_bool(std::move(head)) && cb2.finalize_to(head))) {
return false;
}
if (!(tuple[i].serialize(cb2, mode) && cb2.finalize_to(tail))) {
return false;
}
}
return cb.store_long_bool(7, 8) && cb.store_long_bool(n, 16) && (head.is_null() || cb.store_ref_bool(head)) &&
(tail.is_null() || cb.store_ref_bool(tail));
}
default:
return false;
}
}
bool Stack::serialize(vm::CellBuilder& cb, int mode) const {
// vm_stack#_ depth:(## 24) stack:(VmStackList depth) = VmStack;
unsigned n = depth();
if (!cb.store_ulong_rchk_bool(n, 24)) { // vm_stack#_ depth:(## 24)
return false;
}
if (!n) {
return true;
}
vm::CellBuilder cb2;
Ref<vm::Cell> rest = cb2.finalize(); // vm_stk_nil#_ = VmStackList 0;
for (unsigned i = 0; i < n - 1; i++) {
// vm_stk_cons#_ {n:#} rest:^(VmStackList n) tos:VmStackValue = VmStackList (n + 1);
if (!(cb2.store_ref_bool(std::move(rest)) && stack[i].serialize(cb2, mode) && cb2.finalize_to(rest))) {
return false;
}
}
return cb.store_ref_bool(std::move(rest)) && stack[n - 1].serialize(cb, mode);
}
} // namespace vm

View file

@ -166,6 +166,8 @@ class StackEntry {
Type type() const {
return tp;
}
// mode: +1 = disable short ints, +2 = disable continuations
bool serialize(vm::CellBuilder& cb, int mode = 0) const;
private:
static bool is_list(const StackEntry* se);
@ -508,6 +510,7 @@ class Stack : public td::CntObject {
}
// mode: +1 = add eoln, +2 = Lisp-style lists
void dump(std::ostream& os, int mode = 1) const;
bool serialize(vm::CellBuilder& cb, int mode = 0) const;
};
} // namespace vm