Skip to content
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
48 changes: 43 additions & 5 deletions src/interp/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,13 @@ void InterpCodegen::CompileStmtReturn(const SyntaxTreeInterfacePtr &stmt) {
return;
}
if (exps.size() == 1) {
// Lua 语义:`return f(...)` 是尾调用;`return (f(...))` 不是(括号是 kExp)。
if (IsFunctionCallExp(exps[0])) {
const auto e = std::dynamic_pointer_cast<SyntaxTreeExp>(exps[0]);
const auto pe = std::dynamic_pointer_cast<SyntaxTreePrefixexp>(e->Right());
CompileFunctioncall(pe->GetValue(), true, true);
return;
}
const int r = CompileExp(exps[0], true);
Emit(Op::RETURN, r, 0);
return;
Expand Down Expand Up @@ -1546,14 +1553,34 @@ void InterpCodegen::EmitCallArgs(const std::shared_ptr<SyntaxTreeArgs> &args_ptr
}
}

int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncall, bool preserve_multi) {
int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncall, bool preserve_multi, bool is_tail) {
const auto fc = std::dynamic_pointer_cast<SyntaxTreeFunctioncall>(functioncall);
const auto args_ptr = std::dynamic_pointer_cast<SyntaxTreeArgs>(fc->Args());
const auto pe_pre_ptr = std::dynamic_pointer_cast<SyntaxTreePrefixexp>(fc->prefixexp());
const int unbox = preserve_multi ? 0 : 1;

std::vector<int> arg_regs;
bool last_expand = false;
auto self_tail_c = [&](int n, bool named, const std::string &name, bool self_closure) -> int {
if (!is_tail || last_expand || !cur_func_ || cur_func_->is_vararg || !cur_func_->proto) {
return 0;
}
if (n != cur_func_->proto->param_count) {
return 0;
}
if (self_closure) {
return kTailSelfExact;
}
if (named && !name.empty()) {
if (name == cur_func_->name || name == cur_func_->unique_c_name) {
return kTailSelfExact;
}
if (!cur_package_name_.empty() && !cur_func_->name.empty() && name == cur_package_name_ + "." + cur_func_->name) {
return kTailSelfExact;
}
}
return 0;
};
auto place_callname = [&](const std::string &name, const std::vector<int> &args) {
const int n = static_cast<int>(args.size());
// 末尾是 f() / ... 这类展开时实参个数运行期才知道,检查不了。
Expand All @@ -1576,11 +1603,12 @@ int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncal
}
stack_top_ = base + n;
}
Emit(Op::CALLNAME, base, n, unbox, AddStringConst(name));
const int c = is_tail ? self_tail_c(n, true, name, false) : unbox;
Emit(is_tail ? Op::TAILNAME : Op::CALLNAME, base, n, c, AddStringConst(name));
stack_top_ = base + 1;
return base;
};
auto place_call = [&](int fn, const std::vector<int> &args) {
auto place_call = [&](int fn, const std::vector<int> &args, bool self_closure = false) {
const int n = static_cast<int>(args.size());
const int base = stack_top_;
EnsureStack(base + 1 + n);
Expand All @@ -1589,7 +1617,8 @@ int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncal
MoveTo(base + 1 + i, args[i]);
}
stack_top_ = base + 1 + n;
Emit(Op::CALL, base, n, unbox);
const int c = is_tail ? self_tail_c(n, false, {}, self_closure) : unbox;
Emit(is_tail ? Op::TAIL : Op::CALL, base, n, c);
stack_top_ = base + 1;
return base;
};
Expand All @@ -1605,6 +1634,9 @@ int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncal
Emit(Op::SETTABLE, t, k, val);
const int r = AllocReg();
Emit(Op::LOADNIL, r);
if (is_tail) {
Emit(Op::RETURN, r, 1);
}
return r;
}
}
Expand Down Expand Up @@ -1681,7 +1713,13 @@ int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncal
} else {
fn = CompilePrefixexp(fc->prefixexp(), false);
}
return place_call(fn, arg_regs);
bool self_closure = false;
if (is_tail && is_local_callee && var_ptr && cur_func_ && cur_func_->node) {
if (const auto it = var_to_def_map_.find(var_ptr); it != var_to_def_map_.end() && it->second->def_node == cur_func_->node) {
self_closure = true;
}
}
return place_call(fn, arg_regs, self_closure);
}

int InterpCodegen::CompileTableconstructor(const SyntaxTreeInterfacePtr &tc) {
Expand Down
2 changes: 1 addition & 1 deletion src/interp/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class InterpCodegen {
int CompileExp(const SyntaxTreeInterfacePtr &exp, bool preserve_multi = false, int dest = -1);
int CompilePrefixexp(const SyntaxTreeInterfacePtr &pe, bool preserve_multi, int dest = -1);
int CompileVar(const SyntaxTreeInterfacePtr &v);
int CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncall, bool preserve_multi);
int CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncall, bool preserve_multi, bool is_tail = false);
int CompileTableconstructor(const SyntaxTreeInterfacePtr &tc);
int CompileBinop(const SyntaxTreeInterfacePtr &exp, int dest = -1);
int CompileUnop(const SyntaxTreeInterfacePtr &exp, int dest = -1);
Expand Down
Loading
Loading