mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-12 19:22:37 +00:00
[FunC] Change some fields to enums instead of integers
It makes it easier to understand/debug Also, drop some unused enum values from that cases
This commit is contained in:
parent
5c392e0f2d
commit
0bc6305f96
5 changed files with 42 additions and 48 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -13,6 +13,8 @@ test/regression-tests.cache/
|
||||||
**/*build*/
|
**/*build*/
|
||||||
.idea
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
|
dev/
|
||||||
|
.DS_Store
|
||||||
zlib/
|
zlib/
|
||||||
libsodium/
|
libsodium/
|
||||||
libmicrohttpd-0.9.77-w32-bin/
|
libmicrohttpd-0.9.77-w32-bin/
|
||||||
|
|
|
@ -149,8 +149,8 @@ class IdSc {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct TypeExpr {
|
struct TypeExpr {
|
||||||
enum te_type { te_Unknown, te_Var, te_Indirect, te_Atomic, te_Tensor, te_Tuple, te_Map, te_Type, te_ForAll } constr;
|
enum te_type { te_Unknown, te_Var, te_Indirect, te_Atomic, te_Tensor, te_Tuple, te_Map, te_ForAll } constr;
|
||||||
enum {
|
enum AtomicType {
|
||||||
_Int = Keyword::_Int,
|
_Int = Keyword::_Int,
|
||||||
_Cell = Keyword::_Cell,
|
_Cell = Keyword::_Cell,
|
||||||
_Slice = Keyword::_Slice,
|
_Slice = Keyword::_Slice,
|
||||||
|
@ -216,7 +216,7 @@ struct TypeExpr {
|
||||||
void compute_width();
|
void compute_width();
|
||||||
bool recompute_width();
|
bool recompute_width();
|
||||||
void show_width(std::ostream& os);
|
void show_width(std::ostream& os);
|
||||||
std::ostream& print(std::ostream& os, int prio = 0);
|
std::ostream& print(std::ostream& os, int prio = 0) const;
|
||||||
void replace_with(TypeExpr* te2);
|
void replace_with(TypeExpr* te2);
|
||||||
int extract_components(std::vector<TypeExpr*>& comp_list);
|
int extract_components(std::vector<TypeExpr*>& comp_list);
|
||||||
static int holes, type_vars;
|
static int holes, type_vars;
|
||||||
|
@ -535,7 +535,7 @@ class ListIterator {
|
||||||
struct Stack;
|
struct Stack;
|
||||||
|
|
||||||
struct Op {
|
struct Op {
|
||||||
enum {
|
enum OpKind {
|
||||||
_Undef,
|
_Undef,
|
||||||
_Nop,
|
_Nop,
|
||||||
_Call,
|
_Call,
|
||||||
|
@ -554,10 +554,10 @@ struct Op {
|
||||||
_Repeat,
|
_Repeat,
|
||||||
_Again,
|
_Again,
|
||||||
_TryCatch,
|
_TryCatch,
|
||||||
_SliceConst
|
_SliceConst,
|
||||||
};
|
};
|
||||||
int cl;
|
OpKind cl;
|
||||||
enum { _Disabled = 1, _Reachable = 2, _NoReturn = 4, _ImpureR = 8, _ImpureW = 16, _Impure = 24 };
|
enum { _Disabled = 1, _NoReturn = 4, _Impure = 24 };
|
||||||
int flags;
|
int flags;
|
||||||
std::unique_ptr<Op> next;
|
std::unique_ptr<Op> next;
|
||||||
SymDef* fun_ref;
|
SymDef* fun_ref;
|
||||||
|
@ -568,25 +568,25 @@ struct Op {
|
||||||
std::unique_ptr<Op> block0, block1;
|
std::unique_ptr<Op> block0, block1;
|
||||||
td::RefInt256 int_const;
|
td::RefInt256 int_const;
|
||||||
std::string str_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 = {}, OpKind _cl = _Undef) : cl(_cl), flags(0), fun_ref(nullptr), where(_where) {
|
||||||
}
|
}
|
||||||
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left)
|
Op(const SrcLocation& _where, OpKind _cl, const std::vector<var_idx_t>& _left)
|
||||||
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left) {
|
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(_left) {
|
||||||
}
|
}
|
||||||
Op(const SrcLocation& _where, int _cl, std::vector<var_idx_t>&& _left)
|
Op(const SrcLocation& _where, OpKind _cl, std::vector<var_idx_t>&& _left)
|
||||||
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(std::move(_left)) {
|
: cl(_cl), flags(0), fun_ref(nullptr), where(_where), left(std::move(_left)) {
|
||||||
}
|
}
|
||||||
Op(const SrcLocation& _where, int _cl, const std::vector<var_idx_t>& _left, td::RefInt256 _const)
|
Op(const SrcLocation& _where, OpKind _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) {
|
: 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)
|
Op(const SrcLocation& _where, OpKind _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) {
|
: 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,
|
Op(const SrcLocation& _where, OpKind _cl, const std::vector<var_idx_t>& _left, const std::vector<var_idx_t>& _right,
|
||||||
SymDef* _fun = nullptr)
|
SymDef* _fun = nullptr)
|
||||||
: cl(_cl), flags(0), fun_ref(_fun), where(_where), left(_left), right(_right) {
|
: cl(_cl), flags(0), fun_ref(_fun), where(_where), left(_left), right(_right) {
|
||||||
}
|
}
|
||||||
Op(const SrcLocation& _where, int _cl, std::vector<var_idx_t>&& _left, std::vector<var_idx_t>&& _right,
|
Op(const SrcLocation& _where, OpKind _cl, std::vector<var_idx_t>&& _left, std::vector<var_idx_t>&& _right,
|
||||||
SymDef* _fun = nullptr)
|
SymDef* _fun = nullptr)
|
||||||
: cl(_cl), flags(0), fun_ref(_fun), where(_where), left(std::move(_left)), right(std::move(_right)) {
|
: cl(_cl), flags(0), fun_ref(_fun), where(_where), left(std::move(_left)), right(std::move(_right)) {
|
||||||
}
|
}
|
||||||
|
@ -599,9 +599,6 @@ struct Op {
|
||||||
void disable() {
|
void disable() {
|
||||||
flags |= _Disabled;
|
flags |= _Disabled;
|
||||||
}
|
}
|
||||||
bool unreachable() {
|
|
||||||
return !(flags & _Reachable);
|
|
||||||
}
|
|
||||||
void flags_set_clear(int set, int clear);
|
void flags_set_clear(int set, int clear);
|
||||||
void show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx = "", int mode = 0) const;
|
void show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx = "", int mode = 0) const;
|
||||||
void show_var_list(std::ostream& os, const std::vector<var_idx_t>& idx_list, const std::vector<TmpVar>& vars) const;
|
void show_var_list(std::ostream& os, const std::vector<var_idx_t>& idx_list, const std::vector<TmpVar>& vars) const;
|
||||||
|
@ -898,7 +895,7 @@ extern std::stack<src::SrcLocation> inclusion_locations;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct Expr {
|
struct Expr {
|
||||||
enum {
|
enum ExprCls {
|
||||||
_None,
|
_None,
|
||||||
_Apply,
|
_Apply,
|
||||||
_VarApply,
|
_VarApply,
|
||||||
|
@ -914,11 +911,11 @@ struct Expr {
|
||||||
_Hole,
|
_Hole,
|
||||||
_Type,
|
_Type,
|
||||||
_CondExpr,
|
_CondExpr,
|
||||||
_SliceConst
|
_SliceConst,
|
||||||
};
|
};
|
||||||
int cls;
|
ExprCls cls;
|
||||||
int val{0};
|
int val{0};
|
||||||
enum { _IsType = 1, _IsRvalue = 2, _IsLvalue = 4, _IsHole = 8, _IsNewVar = 16, _IsImpure = 32 };
|
enum { _IsType = 1, _IsRvalue = 2, _IsLvalue = 4, _IsImpure = 32 };
|
||||||
int flags{0};
|
int flags{0};
|
||||||
SrcLocation here;
|
SrcLocation here;
|
||||||
td::RefInt256 intval;
|
td::RefInt256 intval;
|
||||||
|
@ -926,19 +923,19 @@ struct Expr {
|
||||||
SymDef* sym{nullptr};
|
SymDef* sym{nullptr};
|
||||||
TypeExpr* e_type{nullptr};
|
TypeExpr* e_type{nullptr};
|
||||||
std::vector<Expr*> args;
|
std::vector<Expr*> args;
|
||||||
Expr(int c = _None) : cls(c) {
|
explicit Expr(ExprCls c = _None) : cls(c) {
|
||||||
}
|
}
|
||||||
Expr(int c, const SrcLocation& loc) : cls(c), here(loc) {
|
Expr(ExprCls c, const SrcLocation& loc) : cls(c), here(loc) {
|
||||||
}
|
}
|
||||||
Expr(int c, std::vector<Expr*> _args) : cls(c), args(std::move(_args)) {
|
Expr(ExprCls c, std::vector<Expr*> _args) : cls(c), args(std::move(_args)) {
|
||||||
}
|
}
|
||||||
Expr(int c, std::initializer_list<Expr*> _arglist) : cls(c), args(std::move(_arglist)) {
|
Expr(ExprCls c, std::initializer_list<Expr*> _arglist) : cls(c), args(std::move(_arglist)) {
|
||||||
}
|
}
|
||||||
Expr(int c, SymDef* _sym, std::initializer_list<Expr*> _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) {
|
Expr(ExprCls c, SymDef* _sym, std::initializer_list<Expr*> _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) {
|
||||||
}
|
}
|
||||||
Expr(int c, SymDef* _sym, std::vector<Expr*> _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) {
|
Expr(ExprCls c, SymDef* _sym, std::vector<Expr*> _arglist) : cls(c), sym(_sym), args(std::move(_arglist)) {
|
||||||
}
|
}
|
||||||
Expr(int c, sym_idx_t name_idx, std::initializer_list<Expr*> _arglist);
|
Expr(ExprCls c, sym_idx_t name_idx, std::initializer_list<Expr*> _arglist);
|
||||||
~Expr() {
|
~Expr() {
|
||||||
for (auto& arg_ptr : args) {
|
for (auto& arg_ptr : args) {
|
||||||
delete arg_ptr;
|
delete arg_ptr;
|
||||||
|
@ -979,7 +976,6 @@ struct Expr {
|
||||||
int define_new_vars(CodeBlob& code);
|
int define_new_vars(CodeBlob& code);
|
||||||
int predefine_vars();
|
int predefine_vars();
|
||||||
std::vector<var_idx_t> pre_compile(CodeBlob& code, std::vector<std::pair<SymDef*, var_idx_t>>* lval_globs = nullptr) const;
|
std::vector<var_idx_t> pre_compile(CodeBlob& code, std::vector<std::pair<SymDef*, var_idx_t>>* lval_globs = nullptr) const;
|
||||||
static std::vector<var_idx_t> pre_compile_let(CodeBlob& code, Expr* lhs, Expr* rhs, const SrcLocation& here);
|
|
||||||
var_idx_t new_tmp(CodeBlob& code) const;
|
var_idx_t new_tmp(CodeBlob& code) const;
|
||||||
std::vector<var_idx_t> new_tmp_vect(CodeBlob& code) const {
|
std::vector<var_idx_t> new_tmp_vect(CodeBlob& code) const {
|
||||||
return {new_tmp(code)};
|
return {new_tmp(code)};
|
||||||
|
@ -1000,9 +996,9 @@ using Const = td::RefInt256;
|
||||||
|
|
||||||
struct AsmOp {
|
struct AsmOp {
|
||||||
enum Type { a_none, a_xchg, a_push, a_pop, a_const, a_custom, a_magic };
|
enum Type { a_none, a_xchg, a_push, a_pop, a_const, a_custom, a_magic };
|
||||||
int t{a_none};
|
Type t{a_none};
|
||||||
int indent{0};
|
int indent{0};
|
||||||
int a, b, c;
|
int a, b;
|
||||||
bool gconst{false};
|
bool gconst{false};
|
||||||
std::string op;
|
std::string op;
|
||||||
td::RefInt256 origin;
|
td::RefInt256 origin;
|
||||||
|
@ -1012,26 +1008,22 @@ struct AsmOp {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
AsmOp() = default;
|
AsmOp() = default;
|
||||||
AsmOp(int _t) : t(_t) {
|
AsmOp(Type _t) : t(_t) {
|
||||||
}
|
}
|
||||||
AsmOp(int _t, std::string _op) : t(_t), op(std::move(_op)) {
|
AsmOp(Type _t, std::string _op) : t(_t), op(std::move(_op)) {
|
||||||
}
|
}
|
||||||
AsmOp(int _t, int _a) : t(_t), a(_a) {
|
AsmOp(Type _t, int _a) : t(_t), a(_a) {
|
||||||
}
|
}
|
||||||
AsmOp(int _t, int _a, std::string _op) : t(_t), a(_a), op(std::move(_op)) {
|
AsmOp(Type _t, int _a, std::string _op) : t(_t), a(_a), op(std::move(_op)) {
|
||||||
}
|
}
|
||||||
AsmOp(int _t, int _a, int _b) : t(_t), a(_a), b(_b) {
|
AsmOp(Type _t, int _a, int _b) : t(_t), a(_a), b(_b) {
|
||||||
}
|
}
|
||||||
AsmOp(int _t, int _a, int _b, std::string _op) : t(_t), a(_a), b(_b), op(std::move(_op)) {
|
AsmOp(Type _t, int _a, int _b, std::string _op) : t(_t), a(_a), b(_b), op(std::move(_op)) {
|
||||||
compute_gconst();
|
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) {
|
AsmOp(Type _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();
|
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)) {
|
|
||||||
}
|
|
||||||
void out(std::ostream& os) const;
|
void out(std::ostream& os) const;
|
||||||
void out_indent_nl(std::ostream& os, bool no_nl = false) const;
|
void out_indent_nl(std::ostream& os, bool no_nl = false) const;
|
||||||
std::string to_string() const;
|
std::string to_string() const;
|
||||||
|
|
|
@ -37,7 +37,7 @@ Expr* Expr::copy() const {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Expr::Expr(int c, sym_idx_t name_idx, std::initializer_list<Expr*> _arglist) : cls(c), args(std::move(_arglist)) {
|
Expr::Expr(ExprCls c, sym_idx_t name_idx, std::initializer_list<Expr*> _arglist) : cls(c), args(std::move(_arglist)) {
|
||||||
sym = sym::lookup_symbol(name_idx);
|
sym = sym::lookup_symbol(name_idx);
|
||||||
if (!sym) {
|
if (!sym) {
|
||||||
}
|
}
|
||||||
|
@ -233,7 +233,7 @@ void add_set_globs(CodeBlob& code, std::vector<std::pair<SymDef*, var_idx_t>>& g
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<var_idx_t> Expr::pre_compile_let(CodeBlob& code, Expr* lhs, Expr* rhs, const SrcLocation& here) {
|
std::vector<var_idx_t> pre_compile_let(CodeBlob& code, Expr* lhs, Expr* rhs, const SrcLocation& here) {
|
||||||
while (lhs->is_type_apply()) {
|
while (lhs->is_type_apply()) {
|
||||||
lhs = lhs->args.at(0);
|
lhs = lhs->args.at(0);
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ std::vector<var_idx_t> Expr::pre_compile_let(CodeBlob& code, Expr* lhs, Expr* rh
|
||||||
auto unpacked_type = rhs->e_type->args.at(0);
|
auto unpacked_type = rhs->e_type->args.at(0);
|
||||||
std::vector<var_idx_t> tmp{code.create_tmp_var(unpacked_type, &rhs->here)};
|
std::vector<var_idx_t> tmp{code.create_tmp_var(unpacked_type, &rhs->here)};
|
||||||
code.emplace_back(lhs->here, Op::_UnTuple, tmp, std::move(right));
|
code.emplace_back(lhs->here, Op::_UnTuple, tmp, std::move(right));
|
||||||
auto tvar = new Expr{_Var};
|
auto tvar = new Expr{Expr::_Var};
|
||||||
tvar->set_val(tmp[0]);
|
tvar->set_val(tmp[0]);
|
||||||
tvar->set_location(rhs->here);
|
tvar->set_location(rhs->here);
|
||||||
tvar->e_type = unpacked_type;
|
tvar->e_type = unpacked_type;
|
||||||
|
|
|
@ -580,7 +580,7 @@ Expr* parse_expr100(Lexer& lex, CodeBlob& code, bool nv) {
|
||||||
if (t == '_') {
|
if (t == '_') {
|
||||||
Expr* res = new Expr{Expr::_Hole, lex.cur().loc};
|
Expr* res = new Expr{Expr::_Hole, lex.cur().loc};
|
||||||
res->val = -1;
|
res->val = -1;
|
||||||
res->flags = (Expr::_IsLvalue | Expr::_IsHole | Expr::_IsNewVar);
|
res->flags = Expr::_IsLvalue;
|
||||||
res->e_type = TypeExpr::new_hole();
|
res->e_type = TypeExpr::new_hole();
|
||||||
lex.next();
|
lex.next();
|
||||||
return res;
|
return res;
|
||||||
|
@ -642,7 +642,7 @@ Expr* parse_expr100(Lexer& lex, CodeBlob& code, bool nv) {
|
||||||
if (nv) {
|
if (nv) {
|
||||||
res->val = ~lex.cur().val;
|
res->val = ~lex.cur().val;
|
||||||
res->e_type = TypeExpr::new_hole();
|
res->e_type = TypeExpr::new_hole();
|
||||||
res->flags = Expr::_IsLvalue | Expr::_IsNewVar;
|
res->flags = Expr::_IsLvalue;
|
||||||
// std::cerr << "defined new variable " << lex.cur().str << " : " << res->e_type << std::endl;
|
// std::cerr << "defined new variable " << lex.cur().str << " : " << res->e_type << std::endl;
|
||||||
} else {
|
} else {
|
||||||
if (!sym) {
|
if (!sym) {
|
||||||
|
|
|
@ -209,7 +209,7 @@ std::ostream& operator<<(std::ostream& os, TypeExpr* type_expr) {
|
||||||
return type_expr->print(os);
|
return type_expr->print(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& TypeExpr::print(std::ostream& os, int lex_level) {
|
std::ostream& TypeExpr::print(std::ostream& os, int lex_level) const {
|
||||||
switch (constr) {
|
switch (constr) {
|
||||||
case te_Unknown:
|
case te_Unknown:
|
||||||
return os << "??" << value;
|
return os << "??" << value;
|
||||||
|
|
Loading…
Reference in a new issue