refactoring

pull/167/head
Dibyendu Majumdar 6 years ago
parent b51868d20f
commit f62b1eb557

@ -303,9 +303,9 @@ typedef struct lua_TValue {
/* to new object */
#define setobj2n setobj
#define setsvalue2n setsvalue
/* to table */
#define setobj2t setobj
/* to table (define it as an expression to be used in macros) */
#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1)))

@ -50,10 +50,10 @@
/*
** fast track for 'gettable': if 't' is a table and 't[k]' is not nil,
** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise,
** return 0 (meaning it will have to check metamethod) with 'slot'
** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise).
** 'f' is the raw get function to use.
** return 1 with 'slot' pointing to 't[k]' (position of final result).
** Otherwise, return 0 (meaning it will have to check metamethod)
** with 'slot' pointing to a nil 't[k]' (if 't' is a table) or NULL
** (otherwise). 'f' is the raw get function to use.
*/
#define luaV_fastget(L,t,k,slot,f) \
(!ttistable(t) \
@ -61,41 +61,28 @@
: (slot = f(hvalue(t), k), /* else, do raw access */ \
!ttisnil(slot))) /* result not nil? */
/*
** standard implementation for 'gettable'
** RAVI change - renamed as we need luaV_gettable to be
** an exported function
** Special case of 'luaV_fastget' for integers, inlining the fast case
** of 'luaH_getint'.
*/
#define luaV_fastgettable(L,t,k,v) { const TValue *slot; \
if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \
else luaV_finishget(L,t,k,v,slot); }
#define luaV_fastgeti(L,t,k,slot) \
(!ttistable(t) \
? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \
: (slot = (l_castS2U(k) - 1u < hvalue(t)->sizearray) \
? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \
!ttisnil(slot))) /* result not nil? */
/*
** Fast track for set table. If 't' is a table and 't[k]' is not nil,
** call GC barrier, do a raw 't[k]=v', and return true; otherwise,
** return false with 'slot' equal to NULL (if 't' is not a table) or
** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro
** returns true, there is no need to 'invalidateTMcache', because the
** call is not creating a new entry.
** Finish a fast set operation (when fast get succeeds). In that case,
** 'slot' points to the place to put the value.
*/
#define luaV_fastset(L,t,k,slot,f,v) \
(!ttistable(t) \
? (slot = NULL, 0) \
: (slot = f(hvalue(t), k), \
ttisnil(slot) ? 0 \
: (luaC_barrierback(L, hvalue(t), v), \
setobj2t(L, cast(TValue *,slot), v), \
1)))
#define luaV_finishfastset(L,t,slot,v) \
{ setobj2t(L, cast(TValue *,slot), v); \
luaC_barrierback(L, hvalue(t), v); }
/*
** RAVI change - renamed as we need luaV_settable to be
** an exported function
*/
#define luaV_fastsettable(L,t,k,v) { const TValue *slot; \
if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \
luaV_finishset(L,t,k,v,slot); }
LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);

@ -670,14 +670,13 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
lua_lock(L);
t = index2addr(L, idx);
if (ttisLtable(t) || !ttistable(t)) {
if (luaV_fastget(L, t, n, slot, luaH_getint)) {
if (luaV_fastgeti(L, t, n, slot)) {
setobj2s(L, L->top, slot);
api_incr_top(L);
}
else {
setivalue(L->top, n);
api_incr_top(L);
luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
TValue aux;
setivalue(&aux, n);
luaV_finishget(L, t, &aux, L->top, slot);
}
}
else {
@ -694,8 +693,8 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
setnilvalue(L->top);
}
}
api_incr_top(L);
}
api_incr_top(L);
lua_unlock(L);
return ttnov(L->top - 1);
}
@ -964,8 +963,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
const TValue *slot;
TString *str = luaS_new(L, k);
api_checknelems(L, 1);
if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1))
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
luaV_finishfastset(L, t, slot, L->top - 1);
L->top--; /* pop value */
}
else {
setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
api_incr_top(L);
@ -1007,13 +1008,13 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
api_checknelems(L, 1);
t = index2addr(L, idx);
if (ttisLtable(t) || !ttistable(t)) {
if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1))
L->top--; /* pop value */
if (luaV_fastgeti(L, t, n, slot)) {
luaV_finishfastset(L, t, slot, L->top - 1);
}
else {
setivalue(L->top, n);
api_incr_top(L);
luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
L->top -= 2; /* pop value and key */
TValue aux;
setivalue(&aux, n);
luaV_finishset(L, t, &aux, L->top - 1, slot);
}
}
else {
@ -1041,8 +1042,8 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
luaG_runerror(L, "value cannot be converted to integer");
}
}
L->top--;
}
L->top--; /* pop value */
lua_unlock(L);
}

@ -196,7 +196,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
return;
}
t = tm; /* else try to access 'tm[key]' */
if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */
if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */
setobj2s(L, val, slot); /* done */
return;
}
@ -210,7 +210,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
** Finish a table assignment 't[key] = val'.
** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points
** to the entry 't[key]', or to 'luaO_nilobject' if there is no such
** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset'
** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastget'
** would have done the job.)
*/
void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
@ -243,9 +243,11 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
return;
}
t = tm; /* else repeat assignment over 'tm' */
if (luaV_fastset(L, t, key, slot, luaH_get, val))
if (luaV_fastget(L, t, key, slot, luaH_get)) {
luaV_finishfastset(L, t, slot, val);
return; /* done */
/* else loop */
}
/* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
}
luaG_runerror(L, "'__newindex' chain too long; possible loop");
}
@ -345,8 +347,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
else \
slot = luaH_get(hvalue(t), key); \
if (!ttisnil(slot)) { \
luaC_barrierback(L, hvalue(t), val); \
setobj2t(L, cast(TValue *, slot), val); \
luaC_barrierback(L, hvalue(t), val); \
} \
else { \
protect(luaV_finishset(L, t, key, val, slot)); \
@ -402,8 +404,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
else \
slot = luaH_getint(h, idx); \
if (!ttisnil(slot)) { \
luaC_barrierback(L, h, val); \
setobj2t(L, cast(TValue *, slot), val); \
luaC_barrierback(L, h, val); \
} \
else { \
protect(luaV_finishset(L, t, key, val, slot)); \
@ -452,8 +454,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
if (RAVI_LIKELY(ttisLtable(t))) { \
const TValue *slot = luaH_getshortstr(hvalue(t), tsvalue(key)); \
if (!ttisnil(slot)) { \
luaC_barrierback(L, hvalue(t), val); \
setobj2t(L, cast(TValue *, slot), val); \
luaC_barrierback(L, hvalue(t), val); \
} \
else { \
protect(luaV_finishset(L, t, key, val, slot)); \
@ -1048,6 +1050,7 @@ void luaV_finishOp (lua_State *L) {
L->top = ci->top); /* restore top */ \
luai_threadyield(L); }
#ifndef RAVI_USE_COMPUTED_GOTO
#if RAVI_BYTECODE_PROFILING_ENABLED
@ -2077,8 +2080,8 @@ int luaV_execute (lua_State *L) {
vmcase(OP_RAVI_GETTABLE_I) {
TValue *rb = RB(i);
TValue *rc = RKC(i);
GETTABLE_INLINE_PROTECTED_I(L, rb, rc, ra);
vmbreak;
GETTABLE_INLINE_PROTECTED_I(L, rb, rc, ra);
vmbreak;
}
/* This opcode is used when the key is known to be
short string but the variable may or may not be

@ -220,6 +220,7 @@ static const char Lua_header[] = ""
"#define setobjt2t setobj\n"
"#define setobj2n setobj\n"
"#define setsvalue2n setsvalue\n"
"#define setobj2t setobj\n"
"typedef TValue *StkId;\n"
"typedef struct TString {\n"
" CommonHeader;\n"

Loading…
Cancel
Save