Skip to content

Commit

Permalink
Rework only StateInit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerg1996 committed Apr 9, 2024
1 parent c28c408 commit ff9341e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 6 deletions.
18 changes: 14 additions & 4 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,13 @@ static inline void tvm_transfer(schema::lazy<schema::MsgAddressInt> dest, unsign
builder b = build(out_msg.info);
b = build(b, out_msg.init);
slice payload_sl = payload.ctos();
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
if (b.brembits() > payload_sl.sbits()) {
out_msg.body = anyval{payload_sl};
b = build(b, out_msg.body);
} else {
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
}
tvm_sendmsg(b.endc(), flags);
}
static inline void tvm_transfer(slice dest, unsigned nanograms, unsigned bounce, unsigned flags, cell payload) {
Expand All @@ -312,8 +317,13 @@ static inline void tvm_transfer(slice dest, unsigned nanograms, unsigned bounce,
builder b = build(out_msg.info);
b = build(b, out_msg.init);
slice payload_sl = payload.ctos();
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
if (b.brembits() > payload_sl.sbits()) {
out_msg.body = anyval{payload_sl};
b = build(b, out_msg.body);
} else {
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
}
tvm_sendmsg(b.endc(), flags);
}
static inline void tvm_transfer(schema::lazy<schema::MsgAddressInt> dest, unsigned nanograms, bool bounce,
Expand Down
57 changes: 57 additions & 0 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/basics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,63 @@ struct anydict {
};
using optcell = anydict;

template<class X>
struct EitherLeft {
bitconst<1, 0b0> tag;
X val;

DEFAULT_EQUAL(EitherLeft)
};
template<class Y>
struct EitherRight {
bitconst<1, 0b1> tag;
Y val;

DEFAULT_EQUAL(EitherRight)
};
template<typename X, typename Y>
struct Either {
using base_t = std::variant<EitherLeft<X>, EitherRight<Y>>;

Either() {}
Either(X left) : val_(EitherLeft<X>{ {}, left }) {}
Either(Y right) : val_(EitherRight<Y>{ {}, right }) {}

Either& operator=(X left) {
val_ = EitherLeft<X>{ {}, left };
return *this;
}
Either& operator=(Y right) {
val_ = EitherRight<Y>{ {}, right };
return *this;
}

bool operator == (const Either& v) const {
return val_ == v.val_;
}
template<class T>
__always_inline bool isa() const {
if constexpr (std::is_same_v<T, X>)
return std::holds_alternative<EitherLeft<X>>(val_);
else if constexpr (std::is_same_v<T, Y>)
return std::holds_alternative<EitherRight<Y>>(val_);
else
return false;
}
template<class T>
__always_inline T get() const {
if constexpr (std::is_same_v<T, X>)
return std::get<EitherLeft<X>>(val_).val;
else if constexpr (std::is_same_v<T, Y>)
return std::get<EitherRight<Y>>(val_).val;
else
static_assert(std::is_same_v<T, X> || std::is_same_v<T, Y>,
"bad get in Either variant");
}
base_t operator()() const { return val_; }
base_t val_;
};

template<typename _Tp>
struct ref {
_Tp operator()() const { return val_; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ struct estimate_element<std::variant<Types...>> {
static constexpr unsigned min_refs = std::min({estimate_element<Types>::min_refs ... });
static constexpr unsigned max_refs = std::max({estimate_element<Types>::max_refs ... });
};
template<class X>
struct estimate_element<EitherLeft<X>> : detail::chain_to< to_std_tuple_t<EitherLeft<X>> > {};
template<class Y>
struct estimate_element<EitherRight<Y>> : detail::chain_to< to_std_tuple_t<EitherRight<Y>> > {};

template<class X, class Y>
struct estimate_element<Either<X, Y>> : detail::chain_to< to_std_tuple_t<Either<X, Y>> > {};

template<class _Tp>
struct estimate_element<lazy<_Tp>> : detail::chain_to<_Tp> {};
Expand Down
12 changes: 12 additions & 0 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/get_bitsize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ struct get_bitsize<std::variant<Types...>> {
static constexpr bool all_calculated = (get_bitsize<Types>::value && ...);
static constexpr unsigned value = all_calculated ? std::max({get_bitsize<Types>::value ... }) : 0;
};
template<class X>
struct get_bitsize<EitherLeft<X>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<EitherLeft<X>> >::value;
};
template<class Y>
struct get_bitsize<EitherRight<Y>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<EitherRight<Y>> >::value;
};
template<class X, class Y>
struct get_bitsize<Either<X, Y>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<Either<X, Y>> >::value;
};
template<class T>
struct get_bitsize<lazy<T>> {
static constexpr unsigned value = get_bitsize<T>::value;
Expand Down
4 changes: 2 additions & 2 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ template<typename X>
struct message {
CommonMsgInfo info;
optional<ref<StateInit>> init;
optional<ref<X>> body;
Either<X, ref<X>> body;
};

template<typename X>
struct message_relaxed {
CommonMsgInfoRelaxed info;
optional<ref<StateInit>> init;
optional<ref<X>> body;
Either<X, ref<X>> body;
};

struct addr_std_fixed {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ struct make_parser_impl<std::variant<Types...>> {
}
};

template<class X, class Y>
struct make_parser_impl<Either<X, Y>> {
using value_type = Either<X, Y>;
using base_t = typename value_type::base_t;

template<class _Ctx>
inline static std::tuple<optional<value_type>, parser, _Ctx> parse(parser p, _Ctx ctx) {
auto [opt_rv, new_p, new_ctx] = make_parser_impl<base_t>::parse(p, ctx);
if (opt_rv) {
value_type rv;
rv.val_ = *opt_rv;
return { rv, new_p, new_ctx };
}
return { {}, p, ctx };
}
};

}} // namespace tvm::schema

0 comments on commit ff9341e

Please sign in to comment.