Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Add support for func constants #70

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,14 @@ add_library(src_parser ${PARSER_SOURCE})
target_include_directories(src_parser PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(src_parser PUBLIC ton_crypto)

add_library(ton_block ${BLOCK_SOURCE})
target_include_directories(ton_block PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/block> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api)

add_executable(func func/func.cpp ${FUNC_LIB_SOURCE})
target_include_directories(func PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(func PUBLIC ton_crypto src_parser git)
target_link_libraries(func PUBLIC ton_crypto src_parser git ton_block)
if (WINGETOPT_FOUND)
target_link_libraries_system(func wingetopt)
endif()
Expand All @@ -319,11 +324,6 @@ if (WINGETOPT_FOUND)
target_link_libraries_system(pow-miner wingetopt)
endif()

add_library(ton_block ${BLOCK_SOURCE})
target_include_directories(ton_block PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/block> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
target_link_libraries(ton_block PUBLIC ton_crypto tdutils tdactor tl_api)

set(TURN_OFF_LSAN cd .)
if (TON_USE_ASAN AND NOT WIN32)
set(TURN_OFF_LSAN export LSAN_OPTIONS=detect_leaks=0)
Expand Down
10 changes: 10 additions & 0 deletions crypto/func/abscode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ void VarDescr::set_const(td::RefInt256 value) {
}
}

void VarDescr::set_const(std::string value) {
str_const = value;
val = _Const;
}

void VarDescr::set_const_nan() {
set_const(td::make_refint());
}
Expand Down Expand Up @@ -342,6 +347,11 @@ void Op::show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx
show_var_list(os, left, vars);
os << " := " << int_const << std::endl;
break;
case _SliceConst:
os << pfx << dis << "SCONST ";
show_var_list(os, left, vars);
os << " := " << str_const << std::endl;
break;
case _Import:
os << pfx << dis << "IMPORT ";
show_var_list(os, left, vars);
Expand Down
7 changes: 7 additions & 0 deletions crypto/func/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ bool Op::compute_used_vars(const CodeBlob& code, bool edit) {
}
switch (cl) {
case _IntConst:
case _SliceConst:
case _GlobVar:
case _Call:
case _CallInd:
Expand Down Expand Up @@ -540,6 +541,7 @@ bool prune_unreachable(std::unique_ptr<Op>& ops) {
bool reach;
switch (op.cl) {
case Op::_IntConst:
case Op::_SliceConst:
case Op::_GlobVar:
case Op::_SetGlob:
case Op::_Call:
Expand Down Expand Up @@ -707,6 +709,10 @@ VarDescrList Op::fwd_analyze(VarDescrList values) {
values.add_newval(left[0]).set_const(int_const);
break;
}
case _SliceConst: {
values.add_newval(left[0]).set_const(str_const);
break;
}
case _Call: {
prepare_args(values);
auto func = dynamic_cast<const SymValAsmFunc*>(fun_ref->value);
Expand Down Expand Up @@ -848,6 +854,7 @@ bool Op::mark_noreturn() {
// fallthrough
case _Import:
case _IntConst:
case _SliceConst:
case _Let:
case _Tuple:
case _UnTuple:
Expand Down
18 changes: 9 additions & 9 deletions crypto/func/asmops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ std::ostream& operator<<(std::ostream& os, AsmOp::SReg stack_reg) {
}
}

AsmOp AsmOp::Const(int arg, std::string push_op) {
AsmOp AsmOp::Const(int arg, std::string push_op, td::RefInt256 origin) {
std::ostringstream os;
os << arg << ' ' << push_op;
return AsmOp::Const(os.str());
return AsmOp::Const(os.str(), origin);
}

AsmOp AsmOp::make_stk2(int a, int b, const char* str, int delta) {
Expand Down Expand Up @@ -166,27 +166,27 @@ AsmOp AsmOp::UnTuple(int a) {

AsmOp AsmOp::IntConst(td::RefInt256 x) {
if (x->signed_fits_bits(8)) {
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT");
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT", x);
}
if (!x->is_valid()) {
return AsmOp::Const("PUSHNAN");
return AsmOp::Const("PUSHNAN", x);
}
int k = is_pos_pow2(x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2");
return AsmOp::Const(k, "PUSHPOW2", x);
}
k = is_pos_pow2(x + 1);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2DEC");
return AsmOp::Const(k, "PUSHPOW2DEC", x);
}
k = is_pos_pow2(-x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHNEGPOW2");
return AsmOp::Const(k, "PUSHNEGPOW2", x);
}
if (!x->mod_pow2_short(23)) {
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINTX");
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINTX", x);
}
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT");
return AsmOp::Const(dec_string(std::move(x)) + " PUSHINT", x);
}

AsmOp AsmOp::BoolConst(bool f) {
Expand Down
2 changes: 1 addition & 1 deletion crypto/func/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace std::literals::string_literals;
*
*/

int glob_func_cnt, undef_func_cnt, glob_var_cnt;
int glob_func_cnt, undef_func_cnt, glob_var_cnt, const_cnt;
std::vector<SymDef*> glob_func, glob_vars;

SymDef* predefine_builtin_func(std::string name, TypeExpr* func_type) {
Expand Down
9 changes: 9 additions & 0 deletions crypto/func/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ bool Op::generate_code_step(Stack& stack) {
}
return true;
}
case _SliceConst: {
auto p = next_var_info[left[0]];
if (!p || p->is_unused()) {
return true;
}
stack.o << AsmOp::Const("x{" + str_const + "} PUSHSLICE");
stack.push_new_var(left[0]);
return true;
}
case _GlobVar:
if (dynamic_cast<const SymValGlobVar*>(fun_ref->value)) {
bool used = false;
Expand Down
51 changes: 45 additions & 6 deletions crypto/func/func.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ enum Keyword {
_Operator,
_Infix,
_Infixl,
_Infixr
_Infixr,
_Const
};

void define_keywords();
Expand Down Expand Up @@ -333,6 +334,8 @@ struct VarDescr {
static constexpr int FiniteUInt = FiniteInt | _Pos;
int val;
td::RefInt256 int_const;
std::string str_const;

VarDescr(var_idx_t _idx = -1, int _flags = 0, int _val = 0) : idx(_idx), flags(_flags), val(_val) {
}
bool operator<(var_idx_t other_idx) const {
Expand Down Expand Up @@ -403,6 +406,7 @@ struct VarDescr {
}
void set_const(long long value);
void set_const(td::RefInt256 value);
void set_const(std::string value);
void set_const_nan();
void operator+=(const VarDescr& y) {
flags &= y.flags;
Expand Down Expand Up @@ -527,7 +531,8 @@ struct Op {
_While,
_Until,
_Repeat,
_Again
_Again,
_SliceConst
};
int cl;
enum { _Disabled = 1, _Reachable = 2, _NoReturn = 4, _ImpureR = 8, _ImpureW = 16, _Impure = 24 };
Expand All @@ -540,6 +545,7 @@ struct Op {
std::vector<var_idx_t> left, right;
std::unique_ptr<Op> block0, block1;
td::RefInt256 int_const;
std::string str_const;
Op(const SrcLocation& _where = {}, int _cl = _Undef) : cl(_cl), flags(0), fun_ref(nullptr), where(_where) {
}
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left)
Expand All @@ -551,6 +557,9 @@ struct Op {
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left, td::RefInt256 _const)
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left), int_const(_const) {
}
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left, std::string _const)
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left), str_const(_const) {
}
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left, const std::vector<var_idx_t>& _right,
SymDef* _fun = nullptr)
: cl(_cl), flags(0), fun_ref(_fun), where(_where), left(_left), right(_right) {
Expand Down Expand Up @@ -781,6 +790,30 @@ struct SymValGlobVar : sym::SymValBase {
}
};

struct SymValConst : sym::SymValBase {
td::RefInt256 intval;
std::string strval;
Keyword type;
SymValConst(int idx, td::RefInt256 value)
: sym::SymValBase(_Const, idx), intval(value) {
type = _Int;
}
SymValConst(int idx, std::string value)
: sym::SymValBase(_Const, idx), strval(value) {
type = _Slice;
}
~SymValConst() override = default;
td::RefInt256 get_int_value() const {
return intval;
}
std::string get_str_value() const {
return strval;
}
Keyword get_type() const {
return type;
}
};

extern int glob_func_cnt, undef_func_cnt, glob_var_cnt;
extern std::vector<SymDef*> glob_func, glob_vars;

Expand Down Expand Up @@ -817,14 +850,16 @@ struct Expr {
_LetFirst,
_Hole,
_Type,
_CondExpr
_CondExpr,
_SliceConst
};
int cls;
int val{0};
enum { _IsType = 1, _IsRvalue = 2, _IsLvalue = 4, _IsHole = 8, _IsNewVar = 16, _IsImpure = 32 };
int flags{0};
SrcLocation here;
td::RefInt256 intval;
std::string strval;
SymDef* sym{nullptr};
TypeExpr* e_type{nullptr};
std::vector<Expr*> args;
Expand Down Expand Up @@ -907,6 +942,7 @@ struct AsmOp {
int a, b, c;
bool gconst{false};
std::string op;
td::RefInt256 origin;
struct SReg {
int idx;
SReg(int _idx) : idx(_idx) {
Expand All @@ -926,6 +962,9 @@ struct AsmOp {
AsmOp(int _t, int _a, int _b, std::string _op) : t(_t), a(_a), b(_b), op(std::move(_op)) {
compute_gconst();
}
AsmOp(int _t, int _a, int _b, std::string _op, td::RefInt256 x) : t(_t), a(_a), b(_b), op(std::move(_op)), origin(x) {
compute_gconst();
}
AsmOp(int _t, int _a, int _b, int _c) : t(_t), a(_a), b(_b), c(_c) {
}
AsmOp(int _t, int _a, int _b, int _c, std::string _op) : t(_t), a(_a), b(_b), c(_c), op(std::move(_op)) {
Expand Down Expand Up @@ -1044,10 +1083,10 @@ struct AsmOp {
static AsmOp make_stk3(int a, int b, int c, const char* str, int delta);
static AsmOp IntConst(td::RefInt256 value);
static AsmOp BoolConst(bool f);
static AsmOp Const(std::string push_op) {
return AsmOp(a_const, 0, 1, std::move(push_op));
static AsmOp Const(std::string push_op, td::RefInt256 origin = {}) {
return AsmOp(a_const, 0, 1, std::move(push_op), origin);
}
static AsmOp Const(int arg, std::string push_op);
static AsmOp Const(int arg, std::string push_op, td::RefInt256 origin = {});
static AsmOp Comment(std::string comment) {
return AsmOp(a_none, std::string{"// "} + comment);
}
Expand Down
5 changes: 5 additions & 0 deletions crypto/func/gen-abscode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ std::vector<var_idx_t> Expr::pre_compile(CodeBlob& code, bool lval) const {
code.close_pop_cur(args[2]->here);
return rvect;
}
case _SliceConst: {
auto rvect = new_tmp_vect(code);
code.emplace_back(here, Op::_SliceConst, rvect, strval);
return rvect;
}
default:
std::cerr << "expression constructor is " << cls << std::endl;
throw src::Fatal{"cannot compile expression with unknown constructor"};
Expand Down
3 changes: 2 additions & 1 deletion crypto/func/keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ void define_keywords() {
.add_keyword("operator", Kw::_Operator)
.add_keyword("infix", Kw::_Infix)
.add_keyword("infixl", Kw::_Infixl)
.add_keyword("infixr", Kw::_Infixr);
.add_keyword("infixr", Kw::_Infixr)
.add_keyword("const", Kw::_Const);
}

} // namespace funC
Loading