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

@ -52,10 +52,10 @@ std::ostream& operator<<(std::ostream& os, AsmOp::SReg stack_reg) {
}
}
AsmOp AsmOp::Const(int arg, std::string push_op, td::RefInt256 origin) {
AsmOp AsmOp::Const(int arg, const std::string& push_op) {
std::ostringstream os;
os << arg << ' ' << push_op;
return AsmOp::Const(os.str(), origin);
return AsmOp::Const(os.str());
}
AsmOp AsmOp::make_stk2(int a, int b, const char* str, int delta) {
@ -161,36 +161,36 @@ AsmOp AsmOp::UnTuple(int a) {
return AsmOp::Custom(os.str(), 1, a);
}
AsmOp AsmOp::IntConst(td::RefInt256 x) {
AsmOp AsmOp::IntConst(const td::RefInt256& x) {
if (x->signed_fits_bits(8)) {
return AsmOp::Const(dec_string(x) + " PUSHINT", x);
return AsmOp::Const(dec_string(x) + " PUSHINT");
}
if (!x->is_valid()) {
return AsmOp::Const("PUSHNAN", x);
return AsmOp::Const("PUSHNAN");
}
int k = is_pos_pow2(x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2", x);
return AsmOp::Const(k, "PUSHPOW2");
}
k = is_pos_pow2(x + 1);
if (k >= 0) {
return AsmOp::Const(k, "PUSHPOW2DEC", x);
return AsmOp::Const(k, "PUSHPOW2DEC");
}
k = is_pos_pow2(-x);
if (k >= 0) {
return AsmOp::Const(k, "PUSHNEGPOW2", x);
return AsmOp::Const(k, "PUSHNEGPOW2");
}
if (!x->mod_pow2_short(23)) {
return AsmOp::Const(dec_string(x) + " PUSHINTX", x);
return AsmOp::Const(dec_string(x) + " PUSHINTX");
}
return AsmOp::Const(dec_string(x) + " PUSHINT", x);
return AsmOp::Const(dec_string(x) + " PUSHINT");
}
AsmOp AsmOp::BoolConst(bool f) {
return AsmOp::Const(f ? "TRUE" : "FALSE");
}
AsmOp AsmOp::Parse(std::string custom_op) {
AsmOp AsmOp::Parse(const std::string& custom_op) {
if (custom_op == "NOP") {
return AsmOp::Nop();
} else if (custom_op == "SWAP") {