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

[Tolk] AST-based semantic analysis, get rid of Expr

This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.

Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
  scope handling and resolving local/global identifiers,
  lvalue/rvalue calc and check, implicit return detection,
  mutability analysis, pure/impure validity checks,
  simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
  but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
  expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
  lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
  stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
  which was fully dropped; AST is converted to Ops (IR) directly
This commit is contained in:
tolk-vm 2024-12-16 21:19:45 +03:00
parent ea0dc16163
commit 3540424aa1
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
71 changed files with 4270 additions and 3060 deletions

View file

@ -25,17 +25,6 @@ namespace tolk {
*
*/
TmpVar::TmpVar(var_idx_t _idx, TypeExpr* _type, sym_idx_t sym_idx, SrcLocation loc)
: v_type(_type), idx(_idx), sym_idx(sym_idx), coord(0), where(loc) {
if (!_type) {
v_type = TypeExpr::new_hole();
}
}
void TmpVar::set_location(SrcLocation loc) {
where = loc;
}
void TmpVar::dump(std::ostream& os) const {
show(os);
os << " : " << v_type << " (width ";
@ -55,8 +44,8 @@ void TmpVar::dump(std::ostream& os) const {
}
void TmpVar::show(std::ostream& os, int omit_idx) const {
if (!is_unnamed()) {
os << G.symbols.get_name(sym_idx);
if (v_sym) {
os << v_sym->name;
if (omit_idx >= 2) {
return;
}
@ -149,10 +138,6 @@ void VarDescr::set_const(std::string value) {
val = _Const;
}
void VarDescr::set_const_nan() {
set_const(td::make_refint());
}
void VarDescr::operator|=(const VarDescr& y) {
val &= y.val;
if (is_int_const() && y.is_int_const() && cmp(int_const, y.int_const) != 0) {
@ -273,7 +258,7 @@ void Op::show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx
case _Call:
os << pfx << dis << "CALL: ";
show_var_list(os, left, vars);
os << " := " << (fun_ref ? fun_ref->name() : "(null)") << " ";
os << " := " << (f_sym ? f_sym->name : "(null)") << " ";
if ((mode & 4) && args.size() == right.size()) {
show_var_list(os, args, vars);
} else {
@ -332,11 +317,11 @@ void Op::show(std::ostream& os, const std::vector<TmpVar>& vars, std::string pfx
case _GlobVar:
os << pfx << dis << "GLOBVAR ";
show_var_list(os, left, vars);
os << " := " << (fun_ref ? fun_ref->name() : "(null)") << std::endl;
os << " := " << (g_sym ? g_sym->name : "(null)") << std::endl;
break;
case _SetGlob:
os << pfx << dis << "SETGLOB ";
os << (fun_ref ? fun_ref->name() : "(null)") << " := ";
os << (g_sym ? g_sym->name : "(null)") << " := ";
show_var_list(os, right, vars);
os << std::endl;
break;
@ -458,22 +443,22 @@ void CodeBlob::print(std::ostream& os, int flags) const {
os << "-------- END ---------\n\n";
}
var_idx_t CodeBlob::create_var(TypeExpr* var_type, var_idx_t sym_idx, SrcLocation location) {
vars.emplace_back(var_cnt, var_type, sym_idx, location);
var_idx_t CodeBlob::create_var(TypeExpr* var_type, const LocalVarData* v_sym, SrcLocation location) {
vars.emplace_back(var_cnt, var_type, v_sym, location);
return var_cnt++;
}
bool CodeBlob::import_params(FormalArgList arg_list) {
bool CodeBlob::import_params(FormalArgList&& arg_list) {
if (var_cnt || in_var_cnt) {
return false;
}
std::vector<var_idx_t> list;
for (const auto& par : arg_list) {
TypeExpr* arg_type;
SymDef* arg_sym;
const LocalVarData* arg_sym;
SrcLocation arg_loc;
std::tie(arg_type, arg_sym, arg_loc) = par;
list.push_back(create_var(arg_type, arg_sym ? arg_sym->sym_idx : 0, arg_loc));
list.push_back(create_var(arg_type, arg_sym, arg_loc));
}
emplace_back(loc, Op::_Import, list);
in_var_cnt = var_cnt;