Skip to content

Commit 15231d4

Browse files
committed
'nresults' moved into 'callstatus'
That gives us more free bits in 'callstatus', for future use.
1 parent f407b3c commit 15231d4

File tree

6 files changed

+79
-62
lines changed

6 files changed

+79
-62
lines changed

lapi.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
11031103
ci->u2.funcidx = cast_int(savestack(L, c.func));
11041104
ci->u.c.old_errfunc = L->errfunc;
11051105
L->errfunc = func;
1106-
setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
1106+
setoah(ci, L->allowhook); /* save value of 'allowhook' */
11071107
ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
11081108
luaD_call(L, c.func, nresults); /* do the call */
11091109
ci->callstatus &= ~CIST_YPCALL;
@@ -1280,11 +1280,9 @@ LUA_API int lua_next (lua_State *L, int idx) {
12801280

12811281

12821282
LUA_API void lua_toclose (lua_State *L, int idx) {
1283-
int nresults;
12841283
StkId o;
12851284
lua_lock(L);
12861285
o = index2stack(L, idx);
1287-
nresults = L->ci->nresults;
12881286
api_check(L, L->tbclist.p < o, "given index below or equal a marked one");
12891287
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
12901288
L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */

ldebug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
346346
ar->nparams = 0;
347347
}
348348
else {
349-
ar->isvararg = f->l.p->flag & PF_ISVARARG;
349+
ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0;
350350
ar->nparams = f->l.p->numparams;
351351
}
352352
break;
353353
}
354354
case 't': {
355-
ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0;
355+
ar->istailcall = (ci != NULL && (ci->callstatus & CIST_TAIL));
356356
break;
357357
}
358358
case 'n': {

ldo.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,31 @@ static StkId tryfuncTM (lua_State *L, StkId func) {
452452
}
453453

454454

455+
/* Generic case for 'moveresult */
456+
l_sinline void genmoveresults (lua_State *L, StkId res, int nres,
457+
int wanted) {
458+
StkId firstresult = L->top.p - nres; /* index of first result */
459+
int i;
460+
if (nres > wanted) /* extra results? */
461+
nres = wanted; /* don't need them */
462+
for (i = 0; i < nres; i++) /* move all results to correct place */
463+
setobjs2s(L, res + i, firstresult + i);
464+
for (; i < wanted; i++) /* complete wanted number of results */
465+
setnilvalue(s2v(res + i));
466+
L->top.p = res + wanted; /* top points after the last result */
467+
}
468+
469+
455470
/*
456-
** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'.
457-
** Handle most typical cases (zero results for commands, one result for
458-
** expressions, multiple results for tail calls/single parameters)
459-
** separated.
471+
** Given 'nres' results at 'firstResult', move 'fwanted-1' of them
472+
** to 'res'. Handle most typical cases (zero results for commands,
473+
** one result for expressions, multiple results for tail calls/single
474+
** parameters) separated. The flag CIST_CLSRET in 'fwanted', if set,
475+
** forces the swicth to go to the default case.
460476
*/
461-
l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
462-
StkId firstresult;
463-
int i;
464-
switch (wanted) { /* handle typical cases separately */
477+
l_sinline void moveresults (lua_State *L, StkId res, int nres,
478+
l_uint32 fwanted) {
479+
switch (fwanted) { /* handle typical cases separately */
465480
case 0 + 1: /* no values needed */
466481
L->top.p = res;
467482
return;
@@ -473,12 +488,11 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
473488
L->top.p = res + 1;
474489
return;
475490
case LUA_MULTRET + 1:
476-
wanted = nres; /* we want all results */
491+
genmoveresults(L, res, nres, nres); /* we want all results */
477492
break;
478-
default: /* two/more results and/or to-be-closed variables */
479-
if (!(wanted & CIST_CLSRET))
480-
wanted--;
481-
else { /* to-be-closed variables? */
493+
default: { /* two/more results and/or to-be-closed variables */
494+
int wanted = get_nresults(fwanted);
495+
if (fwanted & CIST_CLSRET) { /* to-be-closed variables? */
482496
L->ci->u2.nres = nres;
483497
res = luaF_close(L, res, CLOSEKTOP, 1);
484498
L->ci->callstatus &= ~CIST_CLSRET;
@@ -487,21 +501,13 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
487501
rethook(L, L->ci, nres);
488502
res = restorestack(L, savedres); /* hook can move stack */
489503
}
490-
wanted = (wanted & ~CIST_CLSRET) - 1;
491504
if (wanted == LUA_MULTRET)
492505
wanted = nres; /* we want all results */
493506
}
507+
genmoveresults(L, res, nres, wanted);
494508
break;
509+
}
495510
}
496-
/* generic case */
497-
firstresult = L->top.p - nres; /* index of first result */
498-
if (nres > wanted) /* extra results? */
499-
nres = wanted; /* don't need them */
500-
for (i = 0; i < nres; i++) /* move all results to correct place */
501-
setobjs2s(L, res + i, firstresult + i);
502-
for (; i < wanted; i++) /* complete wanted number of results */
503-
setnilvalue(s2v(res + i));
504-
L->top.p = res + wanted; /* top points after the last result */
505511
}
506512

507513

@@ -512,13 +518,11 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) {
512518
** that.
513519
*/
514520
void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
515-
int wanted = ci->nresults + 1;
516-
if (ci->callstatus & CIST_CLSRET)
517-
wanted |= CIST_CLSRET; /* don't check hook in this case */
518-
else if (l_unlikely(L->hookmask))
521+
l_uint32 fwanted = ci->callstatus & (CIST_CLSRET | CIST_NRESULTS);
522+
if (l_unlikely(L->hookmask) && !(fwanted & CIST_CLSRET))
519523
rethook(L, ci, nres);
520524
/* move results to proper place */
521-
moveresults(L, ci->func.p, nres, wanted);
525+
moveresults(L, ci->func.p, nres, fwanted);
522526
/* function cannot be in any of these cases when returning */
523527
lua_assert(!(ci->callstatus &
524528
(CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET)));
@@ -530,12 +534,12 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
530534
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
531535

532536

533-
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret,
534-
int mask, StkId top) {
537+
l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults,
538+
l_uint32 mask, StkId top) {
535539
CallInfo *ci = L->ci = next_ci(L); /* new frame */
536540
ci->func.p = func;
537-
ci->nresults = nret;
538-
ci->callstatus = mask;
541+
lua_assert(((nresults + 1) & ~CIST_NRESULTS) == 0);
542+
ci->callstatus = mask | cast(l_uint32, nresults + 1);
539543
ci->top.p = top;
540544
return ci;
541545
}
@@ -664,7 +668,7 @@ l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) {
664668
luaE_checkcstack(L);
665669
}
666670
if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */
667-
ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */
671+
ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */
668672
luaV_execute(L, ci); /* call it */
669673
}
670674
L->nCcalls -= inc;
@@ -709,7 +713,7 @@ static int finishpcallk (lua_State *L, CallInfo *ci) {
709713
status = LUA_YIELD; /* was interrupted by an yield */
710714
else { /* error */
711715
StkId func = restorestack(L, ci->u2.funcidx);
712-
L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */
716+
L->allowhook = getoah(ci); /* restore 'allowhook' */
713717
func = luaF_close(L, func, status, 1); /* can yield or raise an error */
714718
luaD_seterrorobj(L, status, func);
715719
luaD_shrinkstack(L); /* restore stack size in case of overflow */

lstate.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ static void stack_init (lua_State *L1, lua_State *L) {
177177
ci->callstatus = CIST_C;
178178
ci->func.p = L1->top.p;
179179
ci->u.c.k = NULL;
180-
ci->nresults = 0;
181180
setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
182181
L1->top.p++;
183182
ci->top.p = L1->top.p + LUA_MINSTACK;

lstate.h

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,45 @@ struct CallInfo {
211211
int ntransfer; /* number of values transferred */
212212
} transferinfo;
213213
} u2;
214-
short nresults; /* expected number of results from this function */
215-
unsigned short callstatus;
214+
l_uint32 callstatus;
216215
};
217216

218217

219218
/*
220219
** Bits in CallInfo status
221220
*/
222-
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
223-
#define CIST_C (1<<1) /* call is running a C function */
224-
#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */
225-
#define CIST_HOOKED (1<<3) /* call is running a debug hook */
226-
#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */
227-
#define CIST_TAIL (1<<5) /* call was tail called */
228-
#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
229-
#define CIST_FIN (1<<7) /* function "called" a finalizer */
230-
#define CIST_TRAN (1<<8) /* 'ci' has transfer information */
231-
#define CIST_CLSRET (1<<9) /* function is closing tbc variables */
232-
/* Bits 10-12 are used for CIST_RECST (see below) */
233-
#define CIST_RECST 10
221+
/* bits 0-7 are the expected number of results from this function + 1 */
222+
#define CIST_NRESULTS 0xff
223+
/* original value of 'allowhook' */
224+
#define CIST_OAH (cast(l_uint32, 1) << 8)
225+
/* call is running a C function */
226+
#define CIST_C (cast(l_uint32, 1) << 9)
227+
/* call is on a fresh "luaV_execute" frame */
228+
#define CIST_FRESH (cast(l_uint32, 1) << 10)
229+
/* call is running a debug hook */
230+
#define CIST_HOOKED (cast(l_uint32, 1) << 11)
231+
/* doing a yieldable protected call */
232+
#define CIST_YPCALL (cast(l_uint32, 1) << 12)
233+
/* call was tail called */
234+
#define CIST_TAIL (cast(l_uint32, 1) << 13)
235+
/* last hook called yielded */
236+
#define CIST_HOOKYIELD (cast(l_uint32, 1) << 14)
237+
/* function "called" a finalizer */
238+
#define CIST_FIN (cast(l_uint32, 1) << 15)
239+
/* 'ci' has transfer information */
240+
#define CIST_TRAN (cast(l_uint32, 1) << 16)
241+
/* function is closing tbc variables */
242+
#define CIST_CLSRET (cast(l_uint32, 1) << 17)
243+
/* Bits 18-20 are used for CIST_RECST (see below) */
244+
#define CIST_RECST 18 /* the offset, not the mask */
234245
#if defined(LUA_COMPAT_LT_LE)
235-
#define CIST_LEQ (1<<13) /* using __lt for __le */
246+
/* using __lt for __le */
247+
#define CIST_LEQ (cast(l_uint32, 1) << 21)
236248
#endif
237249

238250

251+
#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1)
252+
239253
/*
240254
** Field CIST_RECST stores the "recover status", used to keep the error
241255
** status while closing to-be-closed variables in coroutines, so that
@@ -246,7 +260,7 @@ struct CallInfo {
246260
#define setcistrecst(ci,st) \
247261
check_exp(((st) & 7) == (st), /* status must fit in three bits */ \
248262
((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \
249-
| ((st) << CIST_RECST)))
263+
| (cast(l_uint32, st) << CIST_RECST)))
250264

251265

252266
/* active function is a Lua function */
@@ -255,9 +269,11 @@ struct CallInfo {
255269
/* call is running Lua code (not a hook) */
256270
#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
257271

258-
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
259-
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
260-
#define getoah(st) ((st) & CIST_OAH)
272+
273+
#define setoah(ci,v) \
274+
((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \
275+
: (ci)->callstatus & ~CIST_OAH))
276+
#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0)
261277

262278

263279
/*

lvm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,10 +1742,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
17421742
trap = 1;
17431743
}
17441744
else { /* do the 'poscall' here */
1745-
int nres;
1745+
int nres = get_nresults(ci->callstatus);
17461746
L->ci = ci->previous; /* back to caller */
17471747
L->top.p = base - 1;
1748-
for (nres = ci->nresults; l_unlikely(nres > 0); nres--)
1748+
for (; l_unlikely(nres > 0); nres--)
17491749
setnilvalue(s2v(L->top.p++)); /* all results are nil */
17501750
}
17511751
goto ret;
@@ -1759,7 +1759,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
17591759
trap = 1;
17601760
}
17611761
else { /* do the 'poscall' here */
1762-
int nres = ci->nresults;
1762+
int nres = get_nresults(ci->callstatus);
17631763
L->ci = ci->previous; /* back to caller */
17641764
if (nres == 0)
17651765
L->top.p = base - 1; /* asked for no results */

0 commit comments

Comments
 (0)