From bdc142e948700feeafb217e34380e881e6194650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petri=20H=C3=A4kkinen?= Date: Tue, 7 Nov 2023 11:43:53 +0200 Subject: [PATCH] Internal tag for iterators --- CodeGen/src/CodeGenUtils.cpp | 8 ++++---- VM/src/lapi.cpp | 3 +-- VM/src/lobject.h | 7 ++++++- VM/src/lstate.h | 2 +- VM/src/lvmexecute.cpp | 10 +++++----- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CodeGen/src/CodeGenUtils.cpp b/CodeGen/src/CodeGenUtils.cpp index 3cdd20b3..267a912b 100644 --- a/CodeGen/src/CodeGenUtils.cpp +++ b/CodeGen/src/CodeGenUtils.cpp @@ -71,7 +71,7 @@ bool forgLoopTableIter(lua_State* L, Table* h, int index, TValue* ra) if (!ttisnil(e)) { - setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1)), LU_TAG_ITERATOR); setnvalue(ra + 3, double(index + 1)); setobj2s(L, ra + 4, e); @@ -90,7 +90,7 @@ bool forgLoopTableIter(lua_State* L, Table* h, int index, TValue* ra) if (!ttisnil(gval(n))) { - setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1)), LU_TAG_ITERATOR); getnodekey(L, ra + 3, n); setobj(L, ra + 4, gval(n)); @@ -115,7 +115,7 @@ bool forgLoopNodeIter(lua_State* L, Table* h, int index, TValue* ra) if (!ttisnil(gval(n))) { - setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1)), LU_TAG_ITERATOR); getnodekey(L, ra + 3, n); setobj(L, ra + 4, gval(n)); @@ -765,7 +765,7 @@ const Instruction* executeFORGPREP(lua_State* L, const Instruction* pc, StkId ba { // set up registers for builtin iteration setobj2s(L, ra + 1, ra); - setpvalue(ra + 2, reinterpret_cast(uintptr_t(0))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(0)), LU_TAG_ITERATOR); setnilvalue(ra); } else diff --git a/VM/src/lapi.cpp b/VM/src/lapi.cpp index 2b9d9849..58c767f1 100644 --- a/VM/src/lapi.cpp +++ b/VM/src/lapi.cpp @@ -682,8 +682,7 @@ void lua_pushboolean(lua_State* L, int b) void lua_pushlightuserdatatagged(lua_State* L, void* p, int tag) { api_check(L, unsigned(tag) < LUA_LUTAG_LIMIT); - setpvalue(L->top, p); - L->top->extra[0] = tag; + setpvalue(L->top, p, tag); api_incr_top(L); } diff --git a/VM/src/lobject.h b/VM/src/lobject.h index eb2975ff..4d0ca97b 100644 --- a/VM/src/lobject.h +++ b/VM/src/lobject.h @@ -82,6 +82,10 @@ typedef struct lua_TValue #define lightuserdatatag(o) check_exp(ttislightuserdata(o), (o)->extra[0]) +// Internal tags used by the VM +#define LU_TAG_ITERATOR LUA_UTAG_LIMIT +#define LU_TAG_COUNT (LUA_UTAG_LIMIT+1) + /* ** for internal debug only */ @@ -122,10 +126,11 @@ typedef struct lua_TValue } #endif -#define setpvalue(obj, x) \ +#define setpvalue(obj, x, tag) \ { \ TValue* i_o = (obj); \ i_o->value.p = (x); \ + i_o->extra[0] = (tag); \ i_o->tt = LUA_TLIGHTUSERDATA; \ } diff --git a/VM/src/lstate.h b/VM/src/lstate.h index 808f57b9..967418ab 100644 --- a/VM/src/lstate.h +++ b/VM/src/lstate.h @@ -197,7 +197,7 @@ typedef struct global_State struct Table* mt[LUA_T_COUNT]; // metatables for basic types TString* ttname[LUA_T_COUNT]; // names for basic types TString* tmname[TM_N]; // array with tag-method names - TString* lightuserdataname[LUA_LUTAG_LIMIT]; // names for tagged lightuserdata + TString* lightuserdataname[LU_TAG_COUNT]; // names for tagged lightuserdata TValue pseudotemp; // storage for temporary values used in pseudo2addr diff --git a/VM/src/lvmexecute.cpp b/VM/src/lvmexecute.cpp index 7778bf56..ac868795 100644 --- a/VM/src/lvmexecute.cpp +++ b/VM/src/lvmexecute.cpp @@ -2302,7 +2302,7 @@ reentry: { // set up registers for builtin iteration setobj2s(L, ra + 1, ra); - setpvalue(ra + 2, reinterpret_cast(uintptr_t(0))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(0)), LU_TAG_ITERATOR); setnilvalue(ra); } else @@ -2354,7 +2354,7 @@ reentry: if (!ttisnil(e)) { - setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1)), LU_TAG_ITERATOR); setnvalue(ra + 3, double(index + 1)); setobj2s(L, ra + 4, e); @@ -2375,7 +2375,7 @@ reentry: if (!ttisnil(gval(n))) { - setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(index + 1)), LU_TAG_ITERATOR); getnodekey(L, ra + 3, n); setobj2s(L, ra + 4, gval(n)); @@ -2427,7 +2427,7 @@ reentry: { setnilvalue(ra); // ra+1 is already the table - setpvalue(ra + 2, reinterpret_cast(uintptr_t(0))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(0)), LU_TAG_ITERATOR); } else if (!ttisfunction(ra)) { @@ -2456,7 +2456,7 @@ reentry: { setnilvalue(ra); // ra+1 is already the table - setpvalue(ra + 2, reinterpret_cast(uintptr_t(0))); + setpvalue(ra + 2, reinterpret_cast(uintptr_t(0)), LU_TAG_ITERATOR); } else if (!ttisfunction(ra)) {