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
152 changes: 139 additions & 13 deletions src/interp/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "var/var_closure.h"
#include "var/var_multi.h"
#include "var/var_string.h"
#include "var/var_table.h"
#include <algorithm>
#include <format>
#include <string_view>
Expand Down Expand Up @@ -205,6 +206,46 @@ CVar InterpCallNativeSlow(State *s, const NativeFuncEntry *native, std::string_v
return InterpCallNativeDirect(s, native, nflat == 0 ? nullptr : flat, nflat);
}

[[noreturn]]
#if defined(__GNUC__)
__attribute__((noinline, cold))
#endif
void InterpThrowIndexError(bool non_table) {
if (non_table) {
ThrowFakeluaException("attempt to index a non-table value");
}
ThrowFakeluaException("table index is nil");
}

#if defined(__GNUC__)
__attribute__((noinline))
#endif
CVar InterpGetTableSlow(State *s, CVar t, CVar k) {
if (UNLIKELY(t.type_ != interp_rt::kTable)) {
InterpThrowIndexError(true);
}
if (UNLIKELY(k.type_ == interp_rt::kNil)) {
InterpThrowIndexError(false);
}
if (k.type_ == interp_rt::kInt) {
return table::TableHelper::GetTableInt(s, t, k.data_.i);
}
return table::TableHelper::GetTable(s, t, k);
}

#if defined(__GNUC__)
__attribute__((noinline))
#endif
void InterpSetTableSlow(State *s, CVar t, CVar k, CVar v) {
if (UNLIKELY(t.type_ != interp_rt::kTable)) {
InterpThrowIndexError(true);
}
if (UNLIKELY(k.type_ == interp_rt::kNil)) {
InterpThrowIndexError(false);
}
table::TableHelper::SetTable(s, t, k, v);
}

template<bool HasBoxes>
inline CVar InterpMakeMulti(State *s, CVar *stk, CVar **boxp, int base, int n) {
if constexpr (!HasBoxes) {
Expand Down Expand Up @@ -806,23 +847,108 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c
VM_DISPATCH_NEXT();
}
VM_CASE(GETTABLE) {
const CVar t = VM_R(inst.b);
const CVar k = VM_R(inst.c);
if (FL_VM_UNLIKELY(t.type_ != interp_rt::kTable)) {
ThrowFakeluaException("attempt to index a non-table value");
}
if (FL_VM_UNLIKELY(k.type_ == interp_rt::kNil)) {
ThrowFakeluaException("table index is nil");
}
if (k.type_ == interp_rt::kInt) {
VM_W(inst.a, table::TableHelper::GetTableInt(s, t, k.data_.i));
} else {
VM_W(inst.a, table::TableHelper::GetTable(s, t, k));
const CVar &t = VM_R(inst.b);
const CVar &k = VM_R(inst.c);
if (FL_VM_LIKELY(t.type_ == interp_rt::kTable && t.data_.t != nullptr)) {
const VarTable *const tbl = t.data_.t;
if (k.type_ == interp_rt::kInt) {
const uint64_t idx = static_cast<uint64_t>(k.data_.i) - 1;
if (FL_VM_LIKELY(idx < tbl->arr_size_)) {
VM_W(inst.a, tbl->arr_[idx]);
VM_DISPATCH_NEXT();
}
if (tbl->spec_get == nullptr && tbl->spec_count == 0 && tbl->bucket_count_ == 0) {
const int64_t ki = k.data_.i;
const uint32_t n = tbl->count_;
for (uint32_t i = 0; i < n; ++i) {
const auto &qd = tbl->quick_data_[i];
if (qd.key.type_ == interp_rt::kInt && qd.key.data_.i == ki) {
VM_W(inst.a, qd.val);
VM_DISPATCH_NEXT();
}
}
VM_W(inst.a, interp_rt::Nil());
VM_DISPATCH_NEXT();
}
} else if (k.type_ == interp_rt::kStringId && tbl->spec_get == nullptr && tbl->spec_count == 0 && tbl->bucket_count_ == 0) {
const int64_t sid = k.data_.i;
const uint32_t h = sid != 0 ? reinterpret_cast<const VarString *>(sid)->CachedHash() : 0u;
if (FL_VM_LIKELY(h != 0)) {
const uint32_t n = tbl->count_;
bool hash_miss = true;
for (uint32_t i = 0; i < n; ++i) {
const auto &qd = tbl->quick_data_[i];
if (qd.hash == h) {
if (FL_VM_LIKELY(qd.key.type_ == interp_rt::kStringId && qd.key.data_.i == sid)) {
VM_W(inst.a, qd.val);
VM_DISPATCH_NEXT();
}
hash_miss = false;
break;
}
}
if (FL_VM_LIKELY(hash_miss)) {
VM_W(inst.a, interp_rt::Nil());
VM_DISPATCH_NEXT();
}
}
}
}
VM_W(inst.a, InterpGetTableSlow(s, t, k));
VM_DISPATCH_NEXT();
}
VM_CASE(SETTABLE) {
interp_rt::SetTable(s, VM_R(inst.a), VM_R(inst.b), VM_R(inst.c));
const CVar &t = VM_R(inst.a);
const CVar &k = VM_R(inst.b);
const CVar &v = VM_R(inst.c);
if (FL_VM_LIKELY(t.type_ == interp_rt::kTable && (t.flag_ & interp_rt::kConstFlag) == 0 && t.data_.t != nullptr)) {
VarTable *const tbl = t.data_.t;
if (k.type_ == interp_rt::kInt) {
const uint64_t idx = static_cast<uint64_t>(k.data_.i) - 1;
if (FL_VM_LIKELY(idx < tbl->arr_size_)) {
if (FL_VM_LIKELY(v.type_ != interp_rt::kNil && tbl->arr_[idx].type_ != interp_rt::kNil)) {
tbl->arr_[idx] = v;
VM_DISPATCH_NEXT();
}
} else if (idx == tbl->arr_size_ && tbl->arr_size_ < tbl->arr_cap_ && tbl->count_ == 0 && v.type_ != interp_rt::kNil) {
// spec tables never allocate arr_, so arr_cap_ > 0 guarantees a plain table;
// count_ == 0 guarantees no hash entries follow arr_size_ for migration or #t.
tbl->arr_[idx] = v;
tbl->arr_size_ = static_cast<uint32_t>(idx) + 1;
if (tbl->seq_len_valid_ != 0 && tbl->seq_len_ == static_cast<int64_t>(idx)) {
tbl->seq_len_ = k.data_.i;
}
VM_DISPATCH_NEXT();
}
} else if (k.type_ == interp_rt::kStringId && v.type_ != interp_rt::kNil && tbl->spec_set == nullptr && tbl->spec_count == 0 && tbl->bucket_count_ == 0) {
const int64_t sid = k.data_.i;
const uint32_t h = sid != 0 ? reinterpret_cast<const VarString *>(sid)->CachedHash() : 0u;
if (FL_VM_LIKELY(h != 0)) {
const uint32_t n = tbl->count_;
bool hash_miss = true;
for (uint32_t i = 0; i < n; ++i) {
auto &qd = tbl->quick_data_[i];
if (qd.hash == h) {
if (FL_VM_LIKELY(qd.key.type_ == interp_rt::kStringId && qd.key.data_.i == sid)) {
static_cast<CVar &>(qd.val) = v;
VM_DISPATCH_NEXT();
}
hash_miss = false;
break;
}
}
if (FL_VM_LIKELY(hash_miss && n < VarTable::QUICK_DATA_SIZE)) {
auto &qd = tbl->quick_data_[n];
static_cast<CVar &>(qd.key) = k;
static_cast<CVar &>(qd.val) = v;
qd.hash = h;
tbl->count_ = n + 1;
VM_DISPATCH_NEXT();
}
}
}
}
InterpSetTableSlow(s, t, k, v);
VM_DISPATCH_NEXT();
}
VM_CASE(NEWTABLE) {
Expand Down
4 changes: 4 additions & 0 deletions src/var/var_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class VarString {
return (hash_val == 0) ? 1 : hash_val;
}

[[nodiscard]] uint32_t CachedHash() const {
return hash_;
}

[[nodiscard]] uint32_t Hash() const {
if (hash_ == 0) {
hash_ = HashOf(Str());
Expand Down
Loading
Loading