xref: /freebsd/contrib/lua/src/ldo.h (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: ldo.h $
38e3e3a7aSWarner Losh ** Stack and Call structure of Lua
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #ifndef ldo_h
88e3e3a7aSWarner Losh #define ldo_h
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh 
11*a9490b81SWarner Losh #include "llimits.h"
128e3e3a7aSWarner Losh #include "lobject.h"
138e3e3a7aSWarner Losh #include "lstate.h"
148e3e3a7aSWarner Losh #include "lzio.h"
158e3e3a7aSWarner Losh 
168e3e3a7aSWarner Losh 
178e3e3a7aSWarner Losh /*
188e3e3a7aSWarner Losh ** Macro to check stack size and grow stack if needed.  Parameters
198e3e3a7aSWarner Losh ** 'pre'/'pos' allow the macro to preserve a pointer into the
208e3e3a7aSWarner Losh ** stack across reallocations, doing the work only when needed.
210495ed39SKyle Evans ** It also allows the running of one GC step when the stack is
220495ed39SKyle Evans ** reallocated.
238e3e3a7aSWarner Losh ** 'condmovestack' is used in heavy tests to force a stack reallocation
248e3e3a7aSWarner Losh ** at every check.
258e3e3a7aSWarner Losh */
268e3e3a7aSWarner Losh #define luaD_checkstackaux(L,n,pre,pos)  \
27*a9490b81SWarner Losh 	if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
280495ed39SKyle Evans 	  { pre; luaD_growstack(L, n, 1); pos; } \
290495ed39SKyle Evans         else { condmovestack(L,pre,pos); }
308e3e3a7aSWarner Losh 
318e3e3a7aSWarner Losh /* In general, 'pre'/'pos' are empty (nothing to save) */
328e3e3a7aSWarner Losh #define luaD_checkstack(L,n)	luaD_checkstackaux(L,n,(void)0,(void)0)
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh 
358e3e3a7aSWarner Losh 
36*a9490b81SWarner Losh #define savestack(L,pt)		(cast_charp(pt) - cast_charp(L->stack.p))
37*a9490b81SWarner Losh #define restorestack(L,n)	cast(StkId, cast_charp(L->stack.p) + (n))
380495ed39SKyle Evans 
390495ed39SKyle Evans 
400495ed39SKyle Evans /* macro to check stack size, preserving 'p' */
41*a9490b81SWarner Losh #define checkstackp(L,n,p)  \
42*a9490b81SWarner Losh   luaD_checkstackaux(L, n, \
43*a9490b81SWarner Losh     ptrdiff_t t__ = savestack(L, p),  /* save 'p' */ \
44*a9490b81SWarner Losh     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
45*a9490b81SWarner Losh 
46*a9490b81SWarner Losh 
47*a9490b81SWarner Losh /* macro to check stack size and GC, preserving 'p' */
480495ed39SKyle Evans #define checkstackGCp(L,n,p)  \
490495ed39SKyle Evans   luaD_checkstackaux(L, n, \
500495ed39SKyle Evans     ptrdiff_t t__ = savestack(L, p);  /* save 'p' */ \
510495ed39SKyle Evans     luaC_checkGC(L),  /* stack grow uses memory */ \
520495ed39SKyle Evans     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
530495ed39SKyle Evans 
540495ed39SKyle Evans 
550495ed39SKyle Evans /* macro to check stack size and GC */
560495ed39SKyle Evans #define checkstackGC(L,fsize)  \
570495ed39SKyle Evans 	luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
588e3e3a7aSWarner Losh 
598e3e3a7aSWarner Losh 
608e3e3a7aSWarner Losh /* type of protected functions, to be ran by 'runprotected' */
618e3e3a7aSWarner Losh typedef void (*Pfunc) (lua_State *L, void *ud);
628e3e3a7aSWarner Losh 
630495ed39SKyle Evans LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
648e3e3a7aSWarner Losh LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
658e3e3a7aSWarner Losh                                                   const char *mode);
660495ed39SKyle Evans LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
670495ed39SKyle Evans                                         int fTransfer, int nTransfer);
680495ed39SKyle Evans LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
69*a9490b81SWarner Losh LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
70*a9490b81SWarner Losh                                               int narg1, int delta);
710495ed39SKyle Evans LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
728e3e3a7aSWarner Losh LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
738e3e3a7aSWarner Losh LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
748c784bb8SWarner Losh LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func);
758c784bb8SWarner Losh LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
768e3e3a7aSWarner Losh LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
778e3e3a7aSWarner Losh                                         ptrdiff_t oldtop, ptrdiff_t ef);
780495ed39SKyle Evans LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
790495ed39SKyle Evans LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
800495ed39SKyle Evans LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
818e3e3a7aSWarner Losh LUAI_FUNC void luaD_shrinkstack (lua_State *L);
828e3e3a7aSWarner Losh LUAI_FUNC void luaD_inctop (lua_State *L);
838e3e3a7aSWarner Losh 
848e3e3a7aSWarner Losh LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
858e3e3a7aSWarner Losh LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
868e3e3a7aSWarner Losh 
878e3e3a7aSWarner Losh #endif
888e3e3a7aSWarner Losh 
89