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

[Tolk] Support syntax tensorVar.0 and tupleVar.0

It works both for reading and writing:
> var t = (1, 2);
> t.0;      // 1
> t.0 = 5;
> t;        // (5, 2)

It also works for typed/untyped tuples, producing INDEX and SETINDEX.

Global tensors and tuples works. Nesting `t.0.1.2` works. `mutate` works.
Even mixing tuples inside tensors inside a global for writing works.
This commit is contained in:
tolk-vm 2025-01-27 10:29:17 +03:00
parent 565bc59735
commit 7a1602f591
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
42 changed files with 1119 additions and 338 deletions

View file

@ -44,21 +44,23 @@ typedef int var_idx_t;
typedef int const_idx_t;
struct TmpVar {
TypePtr v_type;
var_idx_t ir_idx;
const LocalVarData* v_sym; // points to var defined in code; nullptr for implicitly created tmp vars
SrcLocation where;
std::vector<std::function<void(SrcLocation)>> on_modification;
var_idx_t ir_idx; // every var in IR represents 1 stack slot
TypePtr v_type; // calc_width_on_stack() is 1
std::string name; // "x" for vars originated from user sources; "x.0" for tensor components; empty for implicitly created tmp vars
SrcLocation loc; // location of var declaration in sources or where a tmp var was originated
#ifdef TOLK_DEBUG
const char* desc = nullptr; // "origin" of tmp var, for debug output like `'15 (binary-op) '16 (glob-var)`
#endif
TmpVar(var_idx_t ir_idx, TypePtr type, const LocalVarData* v_sym, SrcLocation loc)
: v_type(type)
, ir_idx(ir_idx)
, v_sym(v_sym)
, where(loc) {
TmpVar(var_idx_t ir_idx, TypePtr v_type, std::string name, SrcLocation loc)
: ir_idx(ir_idx)
, v_type(v_type)
, name(std::move(name))
, loc(loc) {
}
void show(std::ostream& os, int omit_idx = 0) const;
void dump(std::ostream& os) const;
void show_as_stack_comment(std::ostream& os) const;
void show(std::ostream& os) const;
};
struct VarDescr {
@ -602,7 +604,6 @@ struct AsmOpList {
}
const_idx_t register_const(Const new_const);
Const get_const(const_idx_t idx);
void show_var(std::ostream& os, var_idx_t idx) const;
void show_var_ext(std::ostream& os, std::pair<var_idx_t, const_idx_t> idx_pair) const;
void adjust_last() {
if (list_.back().is_nop()) {
@ -1018,13 +1019,10 @@ struct Stack {
void rearrange_top(var_idx_t top, bool last);
void merge_const(const Stack& req_stack);
void merge_state(const Stack& req_stack);
void show(int _mode);
void show() {
show(mode);
}
void show();
void opt_show() {
if ((mode & (_StkCmt | _Shown)) == _StkCmt) {
show(mode);
show();
}
}
bool operator==(const Stack& y) const & {
@ -1108,9 +1106,15 @@ struct CodeBlob {
#endif
return res;
}
std::vector<var_idx_t> create_var(TypePtr var_type, const LocalVarData* v_sym, SrcLocation loc);
std::vector<var_idx_t> create_tmp_var(TypePtr var_type, SrcLocation loc) {
return create_var(var_type, nullptr, loc);
std::vector<var_idx_t> create_var(TypePtr var_type, SrcLocation loc, std::string name);
std::vector<var_idx_t> create_tmp_var(TypePtr var_type, SrcLocation loc, const char* desc) {
std::vector<var_idx_t> ir_idx = create_var(var_type, loc, {});
#ifdef TOLK_DEBUG
for (var_idx_t v : ir_idx) {
vars[v].desc = desc;
}
#endif
return ir_idx;
}
bool compute_used_code_vars();
bool compute_used_code_vars(std::unique_ptr<Op>& ops, const VarDescrList& var_info, bool edit) const;
@ -1135,14 +1139,6 @@ struct CodeBlob {
void mark_noreturn();
void generate_code(AsmOpList& out_list, int mode = 0);
void generate_code(std::ostream& os, int mode = 0, int indent = 0);
void on_var_modification(const std::vector<var_idx_t>& left_lval_indices, SrcLocation here) const {
for (var_idx_t ir_idx : left_lval_indices) {
for (auto& f : vars.at(ir_idx).on_modification) {
f(here);
}
}
}
};
// defined in builtins.cpp