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
42 changes: 19 additions & 23 deletions src/compile/c_runtime_cov.h
Original file line number Diff line number Diff line change
Expand Up @@ -970,36 +970,32 @@ static inline CVar FlCallClosure3Fn(State *state, CVar cl_var, CVar a0, CVar a1,
#define FlCallClosure3(...) FlCallClosure3Fn(__VA_ARGS__ FL_HINT_ARGS)

static inline CVar FlCallNamed0(State *state, const char *name, FlCallCache *cache) {
if (FAKELUA_JIT_TYPE == 1) {
if (LIKELY(cache->version == *fl_vm_version && !cache->is_vararg && cache->expected_args == 0)) {
if (cache->kind == 1) {
FlLuaLoc __fl_saved_loc = *fl_active_loc;
CVar __fl_ret = ((CVar (*)(VarClosure *))(cache->target))(NULL);
*fl_active_loc = __fl_saved_loc;
return __fl_ret;
}
if (cache->kind == 2) {
return FakeluaCallNativeCached(state, cache->target, 0, NULL);
}
if (LIKELY(cache->version == *fl_vm_version)) {
if (cache->kind == 2 && (cache->is_vararg || cache->expected_args == 0)) {
return FakeluaCallNativeCached(state, cache->target, 0, NULL);
}
if (cache->kind == 1 && !cache->is_vararg && cache->expected_args == 0) {
FlLuaLoc __fl_saved_loc = *fl_active_loc;
CVar __fl_ret = ((CVar (*)(VarClosure *))(cache->target))(NULL);
*fl_active_loc = __fl_saved_loc;
return __fl_ret;
}
}
return FakeluaCallByNameCached(state, FAKELUA_JIT_TYPE, name, cache, 0);
}

#define FL_DEF_CALL_NAMED(N, PARAMS, ARGS, CHK_MULTI, CTYPES) \
static inline CVar FlCallNamed##N(State *state, const char *name, FlCallCache *cache, PARAMS) { \
if (FAKELUA_JIT_TYPE == 1) { \
if (LIKELY(cache->version == *fl_vm_version && !cache->is_vararg && cache->expected_args == (N) && (CHK_MULTI))) { \
if (cache->kind == 1) { \
FlLuaLoc __fl_saved_loc = *fl_active_loc; \
CVar __fl_ret = ((CVar (*)(VarClosure *, CTYPES))(cache->target))(NULL, ARGS); \
*fl_active_loc = __fl_saved_loc; \
return __fl_ret; \
} \
if (cache->kind == 2) { \
CVar __fl_args[N] = {ARGS}; \
return FakeluaCallNativeCached(state, cache->target, N, __fl_args); \
} \
if (LIKELY(cache->version == *fl_vm_version && (CHK_MULTI))) { \
if (cache->kind == 2 && (cache->is_vararg || cache->expected_args == (N))) { \
CVar __fl_args[N] = {ARGS}; \
return FakeluaCallNativeCached(state, cache->target, N, __fl_args); \
} \
if (cache->kind == 1 && !cache->is_vararg && cache->expected_args == (N)) { \
FlLuaLoc __fl_saved_loc = *fl_active_loc; \
CVar __fl_ret = ((CVar (*)(VarClosure *, CTYPES))(cache->target))(NULL, ARGS); \
*fl_active_loc = __fl_saved_loc; \
return __fl_ret; \
} \
} \
return FakeluaCallByNameCached(state, FAKELUA_JIT_TYPE, name, cache, N, ARGS); \
Expand Down
6 changes: 4 additions & 2 deletions src/interp/func_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace fakelua {

struct FuncProto;
struct NativeFuncEntry;

// 一次编译单元:持有全部函数原型与文件级全局槽,生命周期绑定到 VmFunction 的 handle。
struct TransparentStringHash {
Expand All @@ -41,10 +42,11 @@ struct InterpUnit final : JITHandle {
std::string file_name;
};

// 命名调用点缓存。vm_version 与 Vm::VersionPtr 一致时 proto 仍然有效;
// RegisterFunction 改变版本号后下一次调用重新解析。proto 为空表示目标不是解释器原型。
// 命名调用点缓存。vm_version 与 Vm::VersionPtr 一致时 proto / native 仍然有效;
// RegisterFunction / RegisterNativeFunction 改变版本号后下一次调用重新解析。
struct InterpCallCache {
FuncProto *proto = nullptr;
const NativeFuncEntry *native = nullptr;
uint64_t vm_version = 0;
};

Expand Down
166 changes: 127 additions & 39 deletions src/interp/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,39 @@ std::string_view ConstToName(const CVar &c) {

struct NamedCallTarget {
FuncProto *proto = nullptr;
const NativeFuncEntry *native = nullptr;
bool direct = false;
bool native_direct = false;
};

// 文件级命名调用的调用点缓存。命中且实参个数精确、非变参、末参不是 Multi 时 direct 为真,
// 调用方直接进入解释器帧,不再查 local_funcs / VmFunction。
#if defined(__GNUC__)
__attribute__((noinline))
#endif
void RefillNamedCallCache(State *s, FuncProto *caller, const Inst &inst, InterpCallCache &slot, uint64_t ver) {
const std::string_view name = ConstToName(caller->constants[static_cast<size_t>(inst.sbx)]);
FuncProto *found_proto = nullptr;
const NativeFuncEntry *found_native = nullptr;
if ((inst.c & kCallLocalUnit) != 0 && caller->unit != nullptr) {
if (const auto it = caller->unit->local_funcs.find(name); it != caller->unit->local_funcs.end() && it->second) {
found_proto = it->second;
}
}
if (found_proto == nullptr) {
if (const VmFunction *fn = s->GetVM().FindFunction(name)) {
if (void *addr = fn->GetAddr(JIT_INTERP)) {
found_proto = AsInterpProto(addr);
}
} else {
found_native = s->GetVM().FindNativeFunction(name);
}
}
slot.proto = found_proto;
slot.native = found_native;
slot.vm_version = ver;
}

// 文件级命名调用的调用点缓存。命中且实参个数精确、非变参、末参不是 Multi 时 direct 为真;
// 目标是原生函数且无 Multi 实参、满足定参/变参要求时 native_direct 为真,直接调回调不再查哈希表。
NamedCallTarget ResolveNamedCall(State *s, FuncProto *caller, const Inst *ip_after, const Inst &inst, int nargs, const CVar *args) {
NamedCallTarget out;
if (caller == nullptr || ip_after == nullptr || caller->code.empty() || caller->call_cache.empty()) {
Expand All @@ -49,28 +77,24 @@ NamedCallTarget ResolveNamedCall(State *s, FuncProto *caller, const Inst *ip_aft
}
InterpCallCache &slot = caller->call_cache[static_cast<size_t>(off)];
const uint64_t ver = *s->GetVM().VersionPtr();
if (slot.vm_version != ver) {
const std::string_view name = ConstToName(caller->constants[static_cast<size_t>(inst.sbx)]);
FuncProto *found = nullptr;
if ((inst.c & kCallLocalUnit) != 0 && caller->unit != nullptr) {
if (const auto it = caller->unit->local_funcs.find(name); it != caller->unit->local_funcs.end() && it->second) {
found = it->second;
}
}
if (found == nullptr) {
if (const VmFunction *fn = s->GetVM().FindFunction(name)) {
if (void *addr = fn->GetAddr(JIT_INTERP)) {
found = AsInterpProto(addr);
}
if (FL_VM_UNLIKELY(slot.vm_version != ver)) {
RefillNamedCallCache(s, caller, inst, slot, ver);
}
out.proto = slot.proto;
out.native = slot.native;
if (out.proto != nullptr) {
const bool last_multi = nargs > 0 && args != nullptr && args[nargs - 1].type_ == static_cast<int>(VarType::Multi);
out.direct = !out.proto->is_vararg && !last_multi && nargs == out.proto->param_count && nargs <= static_cast<int>(kMaxFunctionInputParams);
} else if (out.native != nullptr) {
bool has_multi = false;
for (int i = 0; i < nargs; ++i) {
if (FL_VM_UNLIKELY(args[i].type_ == static_cast<int>(VarType::Multi))) {
has_multi = true;
break;
}
}
slot.proto = found;
slot.vm_version = ver;
out.native_direct = !has_multi && (out.native->is_vararg || nargs == out.native->arg_count);
}
out.proto = slot.proto;
const bool last_multi = nargs > 0 && args != nullptr && args[nargs - 1].type_ == static_cast<int>(VarType::Multi);
out.direct = out.proto != nullptr && !out.proto->is_vararg && !last_multi && nargs == out.proto->param_count &&
nargs <= static_cast<int>(kMaxFunctionInputParams);
return out;
}

Expand Down Expand Up @@ -155,6 +179,73 @@ CVar InterpExecuteInContext(State *s, FuncProto *proto, const CVar *args, int ar
return InterpreterExecute(s, proto, args, arg_count, cl);
}

inline CVar InterpCallNativeDirect(State *s, const NativeFuncEntry *native, CVar *args, int nargs) {
if (FL_VM_UNLIKELY(s->CurrentJit() != JIT_INTERP)) {
State::JitContextScope jit_scope(s, JIT_INTERP);
return native->callback(s, args, nargs);
}
return native->callback(s, args, nargs);
}

int FlattenByNameArgs(const CVar *raw, int arg_num, CVar *flat);

#if defined(__GNUC__)
__attribute__((noinline))
#endif
CVar InterpCallNativeSlow(State *s, const NativeFuncEntry *native, std::string_view name, const CVar *raw, int nargs) {
if (UNLIKELY(native->arg_count < 0 || native->arg_count > static_cast<int>(kMaxFunctionInputParams))) {
ThrowFakeluaException(std::format("FakeluaCallByName: function '{}' declares too many parameters ({}), max is {}", name, native->arg_count, kMaxFunctionInputParams));
}
CVar flat[kMaxFunctionInputParams];
const int nflat = FlattenByNameArgs(raw, nargs, flat);
const bool last_is_multi = (nargs > 0 && raw[nargs - 1].type_ == static_cast<int>(VarType::Multi));
if (UNLIKELY(!native->is_vararg && !last_is_multi && nflat != native->arg_count)) {
ThrowFakeluaException(std::format("FakeluaCallByName: function '{}' expects {} argument(s), got {}", name, native->arg_count, nflat));
}
return InterpCallNativeDirect(s, native, nflat == 0 ? nullptr : flat, nflat);
}

template<bool HasBoxes>
inline CVar InterpMakeMulti(State *s, CVar *stk, CVar **boxp, int base, int n) {
if constexpr (!HasBoxes) {
return interp_rt::MakeMulti(s, stk + base, static_cast<uint32_t>(n));
} else {
if (FL_VM_LIKELY(n <= static_cast<int>(kMaxFunctionInputParams))) {
CVar vals[kMaxFunctionInputParams];
for (int i = 0; i < n; ++i) {
vals[i] = interp_rt::VmSlot<HasBoxes>(stk, boxp, base + i);
}
return interp_rt::MakeMulti(s, vals, static_cast<uint32_t>(n));
}
std::vector<CVar> vals(static_cast<size_t>(n));
for (int i = 0; i < n; ++i) {
vals[static_cast<size_t>(i)] = interp_rt::VmSlot<HasBoxes>(stk, boxp, base + i);
}
return interp_rt::MakeMulti(s, vals.data(), static_cast<uint32_t>(n));
}
}

template<bool HasBoxes>
inline CVar InterpCombineMulti(State *s, CVar *stk, CVar **boxp, int base, int nprefix) {
const CVar last = interp_rt::VmSlot<HasBoxes>(stk, boxp, base + nprefix);
if constexpr (!HasBoxes) {
return interp_rt::CombineMulti(s, stk + base, static_cast<uint32_t>(nprefix), last);
} else {
if (FL_VM_LIKELY(nprefix <= static_cast<int>(kMaxFunctionInputParams))) {
CVar prefix[kMaxFunctionInputParams];
for (int i = 0; i < nprefix; ++i) {
prefix[i] = interp_rt::VmSlot<HasBoxes>(stk, boxp, base + i);
}
return interp_rt::CombineMulti(s, prefix, static_cast<uint32_t>(nprefix), last);
}
std::vector<CVar> prefix(static_cast<size_t>(nprefix));
for (int i = 0; i < nprefix; ++i) {
prefix[static_cast<size_t>(i)] = interp_rt::VmSlot<HasBoxes>(stk, boxp, base + i);
}
return interp_rt::CombineMulti(s, prefix.data(), static_cast<uint32_t>(nprefix), last);
}
}

// 文件级 / 全局命名调用的快路径:签名匹配且目标是 INTERP 原型时直接 InterpreterExecute,
// 跳过 CallByName 的 VmFunction 拷贝、GuardJitEntry 与参数再装箱。
// 变参、末参 Multi、参数个数不一致、原生函数或无 INTERP 地址时返回 false,由调用方走 CallByNameArgs。
Expand Down Expand Up @@ -438,8 +529,14 @@ bool InterpDoTailName(State *s, FuncProto *&proto, VarClosure *&cl, CVar *const
for (int i = 0; i < nargs; ++i) {
raw[i] = interp_rt::VmSlot<HasBoxes>(stk, boxp, inst.a + i);
}
if (const NamedCallTarget hit = ResolveNamedCall(s, proto, ip, inst, nargs, raw); hit.direct) {
if (const NamedCallTarget hit = ResolveNamedCall(s, proto, ip, inst, nargs, raw); hit.native_direct) {
ret = InterpCallNativeDirect(s, hit.native, nargs == 0 ? nullptr : raw, nargs);
return false;
} else if (hit.direct) {
return ApplyInterpTail<HasBoxes>(s, proto, cl, stk, boxp, saved, ip, hit.proto, nullptr, raw, nargs, ret);
} else if (hit.native) {
ret = InterpCallNativeSlow(s, hit.native, name, raw, nargs);
return false;
}
if ((inst.c & kCallLocalUnit) != 0 && proto->unit) {
if (const auto it = proto->unit->local_funcs.find(name); it != proto->unit->local_funcs.end() && it->second) {
Expand Down Expand Up @@ -900,8 +997,13 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c
}
CVar ret;
const NamedCallTarget hit = ResolveNamedCall(s, proto, ip, inst, nargs, call_args);
if (hit.direct) {
if (hit.native_direct) {
ret = InterpCallNativeDirect(s, hit.native, nargs == 0 ? nullptr : call_args, nargs);
} else if (hit.direct) {
ret = InterpExecuteInContext(s, hit.proto, nargs == 0 ? nullptr : call_args, nargs, nullptr);
} else if (hit.native) {
const std::string_view name = ConstToName(proto->constants[static_cast<size_t>(inst.sbx)]);
ret = InterpCallNativeSlow(s, hit.native, name, call_args, nargs);
} else {
const std::string_view name = ConstToName(proto->constants[static_cast<size_t>(inst.sbx)]);
if (hit.proto) {
Expand Down Expand Up @@ -1035,25 +1137,11 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c
VM_DISPATCH_NEXT();
}
VM_CASE(MAKEMULTI) {
{
const int n = inst.b;
std::vector<CVar> vals(static_cast<size_t>(n));
for (int i = 0; i < n; ++i) {
vals[static_cast<size_t>(i)] = VM_R(inst.a + i);
}
VM_W(inst.a, interp_rt::MakeMulti(s, vals.data(), static_cast<uint32_t>(n)));
}
VM_W(inst.a, InterpMakeMulti<HasBoxes>(s, stk, boxp, inst.a, inst.b));
VM_DISPATCH_NEXT();
}
VM_CASE(COMBINE) {
{
const int nprefix = inst.b;
std::vector<CVar> prefix(static_cast<size_t>(nprefix));
for (int i = 0; i < nprefix; ++i) {
prefix[static_cast<size_t>(i)] = VM_R(inst.a + i);
}
VM_W(inst.a, interp_rt::CombineMulti(s, prefix.data(), static_cast<uint32_t>(nprefix), VM_R(inst.a + nprefix)));
}
VM_W(inst.a, InterpCombineMulti<HasBoxes>(s, stk, boxp, inst.a, inst.b));
VM_DISPATCH_NEXT();
}
VM_CASE(THROW) {
Expand Down
Loading
Loading