2 ** LuaJIT VM tags, values and objects.
3 ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
5 ** Portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
16 /* -- Memory references (32 bit address space) ---------------------------- */
19 typedef uint32_t MSize
;
21 /* Memory reference */
23 uint32_t ptr32
; /* Pseudo 32 bit pointer. */
26 #define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32)
28 #define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p))
29 #define setmrefr(r, v) ((r).ptr32 = (v).ptr32)
31 /* -- GC object references (32 bit address space) ------------------------- */
34 typedef struct GCRef
{
35 uint32_t gcptr32
; /* Pseudo 32 bit pointer. */
38 /* Common GC header for all collectable objects. */
39 #define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct
40 /* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */
42 #define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32)
43 #define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32)
44 #define gcrefu(r) ((r).gcptr32)
45 #define gcrefi(r) ((int32_t)(r).gcptr32)
46 #define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32)
47 #define gcnext(gc) (gcref((gc)->gch.nextgc))
49 #define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch)
50 #define setgcrefi(r, i) ((r).gcptr32 = (uint32_t)(i))
51 #define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p))
52 #define setgcrefnull(r) ((r).gcptr32 = 0)
53 #define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32)
57 ** All uses of the setgcref* macros MUST be accompanied with a write barrier.
59 ** This is to ensure the integrity of the incremental GC. The invariant
60 ** to preserve is that a black object never points to a white object.
61 ** I.e. never store a white object into a field of a black object.
63 ** It's ok to LEAVE OUT the write barrier ONLY in the following cases:
64 ** - The source is not a GC object (NULL).
65 ** - The target is a GC root. I.e. everything in global_State.
66 ** - The target is a lua_State field (threads are never black).
67 ** - The target is a stack slot, see setgcV et al.
68 ** - The target is an open upvalue, i.e. pointing to a stack slot.
69 ** - The target is a newly created object (i.e. marked white). But make
70 ** sure nothing invokes the GC inbetween.
71 ** - The target and the source are the same object (self-reference).
72 ** - The target already contains the object (e.g. moving elements around).
74 ** The most common case is a store to a stack slot. All other cases where
75 ** a barrier has been omitted are annotated with a NOBARRIER comment.
77 ** The same logic applies for stores to table slots (array part or hash
78 ** part). ALL uses of lj_tab_set* require a barrier for the stored *value*
79 ** (if it's a GC object). The barrier for the *key* is already handled
80 ** internally by lj_tab_newkey.
83 /* -- Common type definitions --------------------------------------------- */
85 /* Types for handling bytecodes. Need this here, details in lj_bc.h. */
86 typedef uint32_t BCIns
; /* Bytecode instruction. */
87 typedef uint32_t BCPos
; /* Bytecode position. */
88 typedef uint32_t BCReg
; /* Bytecode register. */
89 typedef int32_t BCLine
; /* Bytecode line number. */
91 /* Internal assembler functions. Never call these directly from C. */
92 typedef void (*ASMFunction
)(void);
94 /* Resizable string buffer. Need this here, details in lj_str.h. */
96 char *buf
; /* String buffer base. */
97 MSize n
; /* String buffer length. */
98 MSize sz
; /* String buffer size. */
101 /* -- Tags and values ----------------------------------------------------- */
105 int32_t ftsz
; /* Frame type and size of previous frame. */
106 MRef pcr
; /* Overlaps PC for Lua frames. */
110 typedef LJ_ALIGN(8) union TValue
{
111 uint64_t u64
; /* 64 bit pattern overlaps number. */
112 lua_Number n
; /* Number object overlaps split tag/value object. */
115 GCRef gcr
; /* GCobj reference (if any). */
116 , int32_t it
; /* Internal object tag. Must overlap MSW of number. */
121 GCRef func
; /* Function for next frame (or dummy L). */
122 , FrameLink tp
; /* Link to previous frame. */
127 uint32_t lo
; /* Lower 32 bits of number. */
128 , uint32_t hi
; /* Upper 32 bits of number. */
133 typedef const TValue cTValue
;
135 #define tvref(r) (mref(r, TValue))
137 /* More external and GCobj tags for internal objects. */
138 #define LAST_TT LUA_TTHREAD
140 #define LUA_TPROTO (LAST_TT+1)
141 #define LUA_TUPVAL (LAST_TT+2)
142 #define LUA_TDEADKEY (LAST_TT+3)
144 /* Internal object tags.
146 ** Internal tags overlap the MSW of a number object (must be a double).
147 ** Interpreted as a double these are special NaNs. The FPU only generates
148 ** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available
149 ** for use as internal tags. Small negative numbers are used to shorten the
150 ** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate).
152 ** ---MSW---.---LSW---
153 ** primitive types | itype | |
154 ** lightuserdata | itype | void * | (32 bit platforms)
155 ** lightuserdata |fffc| void * | (64 bit platforms, 48 bit pointers)
156 ** GC objects | itype | GCRef |
157 ** number -------double------
160 ** Primitive types nil/false/true must be first, lightuserdata next.
161 ** GC objects are at the end, table/userdata must be lowest.
162 ** Also check lj_ir.h for similar ordering constraints.
165 #define LJ_TFALSE (-2)
166 #define LJ_TTRUE (-3)
167 #define LJ_TLIGHTUD (-4)
169 #define LJ_TUPVAL (-6)
170 #define LJ_TTHREAD (-7)
171 #define LJ_TPROTO (-8)
172 #define LJ_TFUNC (-9)
173 #define LJ_TDEADKEY (-10)
174 #define LJ_TTAB (-11)
175 #define LJ_TUDATA (-12)
176 /* This is just the canonical number type used in some places. */
177 #define LJ_TNUMX (-13)
180 #define LJ_TISNUM ((uint32_t)0xfff80000)
182 #define LJ_TISNUM ((uint32_t)LJ_TNUMX)
184 #define LJ_TISTRUECOND ((uint32_t)LJ_TFALSE)
185 #define LJ_TISPRI ((uint32_t)LJ_TTRUE)
186 #define LJ_TISGCV ((uint32_t)(LJ_TSTR+1))
187 #define LJ_TISTABUD ((uint32_t)LJ_TTAB)
189 /* -- TValue getters/setters ---------------------------------------------- */
191 /* Macros to test types. */
192 #define itype(o) ((o)->it)
193 #define uitype(o) ((uint32_t)itype(o))
194 #define tvisnil(o) (itype(o) == LJ_TNIL)
195 #define tvisfalse(o) (itype(o) == LJ_TFALSE)
196 #define tvistrue(o) (itype(o) == LJ_TTRUE)
197 #define tvisbool(o) (tvisfalse(o) || tvistrue(o))
199 #define tvislightud(o) ((itype(o) >> 16) == LJ_TLIGHTUD)
201 #define tvislightud(o) (itype(o) == LJ_TLIGHTUD)
203 #define tvisstr(o) (itype(o) == LJ_TSTR)
204 #define tvisfunc(o) (itype(o) == LJ_TFUNC)
205 #define tvisthread(o) (itype(o) == LJ_TTHREAD)
206 #define tvisproto(o) (itype(o) == LJ_TPROTO)
207 #define tvistab(o) (itype(o) == LJ_TTAB)
208 #define tvisudata(o) (itype(o) == LJ_TUDATA)
209 #define tvisnum(o) (uitype(o) <= LJ_TISNUM)
211 #define tvistruecond(o) (uitype(o) < LJ_TISTRUECOND)
212 #define tvispri(o) (uitype(o) >= LJ_TISPRI)
213 #define tvistabud(o) (uitype(o) <= LJ_TISTABUD) /* && !tvisnum() */
215 ((uitype(o) - LJ_TISGCV) > ((uint32_t)LJ_TNUMX - LJ_TISGCV))
217 /* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */
218 #define tvisnan(o) ((o)->n != (o)->n)
219 #define tvispzero(o) ((o)->u64 == 0)
220 #define tvismzero(o) ((o)->u64 == U64x(80000000,00000000))
221 #define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000))
222 #define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64)
224 /* Macros to convert type ids. */
226 #define itypemap(o) \
227 (tvisnum(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o))
229 #define itypemap(o) (tvisnum(o) ? ~LJ_TNUMX : ~itype(o))
232 /* Macros to get tagged values. */
233 #define gcval(o) (gcref((o)->gcr))
234 #define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - (o)->it))
236 #define lightudV(o) check_exp(tvislightud(o), \
237 (void *)((o)->u64 & U64x(0000ffff,ffffffff)))
239 #define lightudV(o) check_exp(tvislightud(o), gcrefp((o)->gcr, void))
241 #define gcV(o) check_exp(tvisgcv(o), gcval(o))
242 #define strV(o) check_exp(tvisstr(o), &gcval(o)->str)
243 #define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn)
244 #define threadV(o) check_exp(tvisthread(o), &gcval(o)->th)
245 #define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt)
246 #define tabV(o) check_exp(tvistab(o), &gcval(o)->tab)
247 #define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud)
248 #define numV(o) check_exp(tvisnum(o), (o)->n)
250 /* Macros to set tagged values. */
251 #define setitype(o, i) ((o)->it = (i))
252 #define setnilV(o) ((o)->it = LJ_TNIL)
253 #define setboolV(o, x) ((o)->it = LJ_TFALSE-(x))
256 #define checklightudptr(L, p) \
257 (((uint64_t)(p) >> 48) ? (lj_err_msg(L, LJ_ERR_BADLU), NULL) : (p))
258 #define setlightudV(o, x) \
259 ((o)->u64 = (uint64_t)(x) | (((uint64_t)LJ_TLIGHTUD) << 48))
260 #define setcont(o, x) \
261 ((o)->u64 = (uint64_t)(x) - (uint64_t)lj_vm_asm_begin)
263 #define checklightudptr(L, p) (p)
264 #define setlightudV(o, x) \
265 { TValue *i_o = (o); \
266 setgcrefp(i_o->gcr, (x)); i_o->it = LJ_TLIGHTUD; }
267 #define setcont(o, x) \
268 { TValue *i_o = (o); \
269 setgcrefp(i_o->gcr, (x)); i_o->it = LJ_TLIGHTUD; }
272 #define tvchecklive(g, o) \
273 lua_assert(!tvisgcv(o) || \
274 ((~itype(o) == gcval(o)->gch.gct) && !isdead(g, gcval(o))))
276 #define setgcV(L, o, x, itype) \
277 { TValue *i_o = (o); \
278 setgcrefp(i_o->gcr, &(x)->nextgc); i_o->it = itype; \
279 tvchecklive(G(L), i_o); }
280 #define setstrV(L, o, x) setgcV(L, o, x, LJ_TSTR)
281 #define setthreadV(L, o, x) setgcV(L, o, x, LJ_TTHREAD)
282 #define setprotoV(L, o, x) setgcV(L, o, x, LJ_TPROTO)
283 #define setfuncV(L, o, x) setgcV(L, o, &(x)->l, LJ_TFUNC)
284 #define settabV(L, o, x) setgcV(L, o, x, LJ_TTAB)
285 #define setudataV(L, o, x) setgcV(L, o, x, LJ_TUDATA)
287 #define setnumV(o, x) ((o)->n = (x))
288 #define setnanV(o) ((o)->u64 = U64x(fff80000,00000000))
289 #define setintV(o, i) ((o)->n = cast_num((int32_t)(i)))
291 /* Copy tagged values. */
292 #define copyTV(L, o1, o2) \
293 { cTValue *i_o2 = (o2); TValue *i_o1 = (o1); \
294 *i_o1 = *i_o2; tvchecklive(G(L), i_o1); }
296 /* -- String object ------------------------------------------------------- */
298 /* String object header. String payload follows. */
299 typedef struct GCstr
{
301 uint8_t reserved
; /* Used by lexer for fast lookup of reserved words. */
303 MSize hash
; /* Hash of string. */
304 MSize len
; /* Size of string. */
307 #define strref(r) (&gcref((r))->str)
308 #define strdata(s) ((const char *)((s)+1))
309 #define strdatawr(s) ((char *)((s)+1))
310 #define strVdata(o) strdata(strV(o))
311 #define sizestring(s) (sizeof(struct GCstr)+(s)->len+1)
313 /* -- Userdata object ----------------------------------------------------- */
315 /* Userdata object. Payload follows. */
316 typedef struct GCudata
{
318 uint8_t udtype
; /* Userdata type. */
320 GCRef env
; /* Should be at same offset in GCfunc. */
321 MSize len
; /* Size of payload. */
322 GCRef metatable
; /* Must be at same offset in GCtab. */
323 uint32_t align1
; /* To force 8 byte alignment of the payload. */
326 /* Userdata types. */
328 UDTYPE_USERDATA
, /* Regular userdata. */
329 UDTYPE_IO_FILE
, /* I/O library FILE. */
333 #define uddata(u) ((void *)((u)+1))
334 #define sizeudata(u) (sizeof(struct GCudata)+(u)->len)
336 /* -- Prototype object ---------------------------------------------------- */
338 /* Split constant array. Collectables are below, numbers above pointer. */
339 typedef union ProtoK
{
340 lua_Number
*n
; /* Numbers. */
341 GCRef
*gc
; /* Collectable objects (strings/table/proto). */
344 #define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
345 #define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))
347 typedef struct VarInfo
{
348 GCstr
*name
; /* Local variable name. */
349 BCPos startpc
; /* First point where the local variable is active. */
350 BCPos endpc
; /* First point where the local variable is dead. */
353 typedef struct GCproto
{
355 uint8_t numparams
; /* Number of parameters. */
356 uint8_t framesize
; /* Fixed frame size. */
357 MSize sizebc
; /* Number of bytecode instructions. */
359 ProtoK k
; /* Split constant array (points to the middle). */
360 BCIns
*bc
; /* Array of bytecode instructions. */
361 int16_t *uv
; /* Upvalue list. local >= 0. parent uv < 0. */
362 MSize sizekgc
; /* Number of collectable constants. */
363 MSize sizekn
; /* Number of lua_Number constants. */
364 uint8_t sizeuv
; /* Number of upvalues. */
365 uint8_t flags
; /* Miscellaneous flags (see below). */
366 uint16_t trace
; /* Anchor for chain of root traces. */
367 /* ------ The following fields are for debugging/tracebacks only ------ */
368 MSize sizelineinfo
; /* Size of lineinfo array (may be 0). */
369 MSize sizevarinfo
; /* Size of local var info array (may be 0). */
370 MSize sizeuvname
; /* Size of upvalue names array (may be 0). */
371 BCLine linedefined
; /* First line of the function definition. */
372 BCLine lastlinedefined
; /* Last line of the function definition. */
373 BCLine
*lineinfo
; /* Map from bytecode instructions to source lines. */
374 struct VarInfo
*varinfo
; /* Names and extents of local variables. */
375 GCstr
**uvname
; /* Upvalue names. */
376 GCstr
*chunkname
; /* Name of the chunk this function was defined in. */
379 #define PROTO_IS_VARARG 0x01
380 #define PROTO_HAS_FNEW 0x02
381 #define PROTO_HAS_RETURN 0x04
382 #define PROTO_FIXUP_RETURN 0x08
383 #define PROTO_NO_JIT 0x10
384 #define PROTO_HAS_ILOOP 0x20
386 /* -- Upvalue object ------------------------------------------------------ */
388 typedef struct GCupval
{
390 uint8_t closed
; /* Set if closed (i.e. uv->v == &uv->u.value). */
393 TValue tv
; /* If closed: the value itself. */
394 struct { /* If open: double linked list, anchored at thread. */
399 TValue
*v
; /* Points to stack slot (open) or above (closed). */
401 int32_t unusedv
; /* For consistent alignment (32 bit only). */
405 #define uvprev(uv_) (&gcref((uv_)->prev)->uv)
406 #define uvnext(uv_) (&gcref((uv_)->next)->uv)
408 /* -- Function object (closures) ------------------------------------------ */
410 /* Common header for functions. env should be at same offset in GCudata. */
411 #define GCfuncHeader \
412 GCHeader; uint8_t ffid; uint8_t nupvalues; \
413 GCRef env; GCRef gclist; ASMFunction gate
415 typedef struct GCfuncC
{
417 lua_CFunction f
; /* C function to be called. */
418 TValue upvalue
[1]; /* Array of upvalues (TValue). */
421 typedef struct GCfuncL
{
423 GCRef pt
; /* Link to prototype this function is based on. */
424 GCRef uvptr
[1]; /* Array of _pointers_ to upvalue objects (GCupval). */
427 typedef union GCfunc
{
434 #define isluafunc(fn) ((fn)->c.ffid == FF_LUA)
435 #define iscfunc(fn) ((fn)->c.ffid == FF_C)
436 #define isffunc(fn) ((fn)->c.ffid > FF_C)
437 #define funcproto(fn) check_exp(isluafunc(fn), &gcref((fn)->l.pt)->pt)
438 #define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n))
439 #define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n))
441 /* -- Table object -------------------------------------------------------- */
444 typedef struct Node
{
445 TValue val
; /* Value object. Must be first field. */
446 TValue key
; /* Key object. */
447 MRef next
; /* Hash chain. */
448 int32_t unused
; /* For consistent alignment. */
451 LJ_STATIC_ASSERT(offsetof(Node
, val
) == 0);
453 typedef struct GCtab
{
455 uint8_t nomm
; /* Negative cache for fast metamethods. */
456 int8_t colo
; /* Array colocation. */
457 MRef array
; /* Array part. */
459 GCRef metatable
; /* Must be at same offset in GCudata. */
460 MRef node
; /* Hash part. */
461 uint32_t asize
; /* Size of array part (keys [0, asize-1]). */
462 uint32_t hmask
; /* Hash part mask (size of hash part - 1). */
463 MRef lastfree
; /* Any free position is before this position. */
466 #define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab))
467 #define tabref(r) (&gcref((r))->tab)
468 #define noderef(r) (mref((r), Node))
469 #define nextnode(n) (mref((n)->next, Node))
471 /* -- State objects ------------------------------------------------------- */
475 LJ_VMST_INTERP
, /* Interpreter. */
476 LJ_VMST_C
, /* C function. */
477 LJ_VMST_GC
, /* Garbage collector. */
478 LJ_VMST_EXIT
, /* Trace exit handler. */
479 LJ_VMST_RECORD
, /* Trace recorder. */
480 LJ_VMST_OPT
, /* Optimizer. */
481 LJ_VMST_ASM
, /* Assembler. */
485 #define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st)
489 _(index) _(newindex) _(gc) _(mode) _(eq) \
490 /* Only the above (fast) metamethods are negative cached (max. 8). */ \
491 _(len) _(lt) _(le) _(concat) _(call) \
492 /* The following must be in ORDER ARITH. */ \
493 _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \
494 /* The following are used in the standard libraries. */ \
495 _(metatable) _(tostring)
498 #define MMENUM(name) MM_##name,
508 GCROOT_BASEMT
, /* Metatables for base types. */
509 GCROOT_BASEMT_NUM
= ~LJ_TNUMX
, /* Last base metatable. */
510 GCROOT_IO_INPUT
, /* Userdata for default I/O input file. */
511 GCROOT_IO_OUTPUT
, /* Userdata for default I/O output file. */
515 #define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)])
516 #define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)])
518 typedef struct GCState
{
519 MSize total
; /* Memory currently allocated. */
520 MSize threshold
; /* Memory threshold. */
521 uint8_t currentwhite
; /* Current white color. */
522 uint8_t state
; /* GC state. */
525 MSize sweepstr
; /* Sweep position in string table. */
526 GCRef root
; /* List of all collectable objects. */
527 GCRef
*sweep
; /* Sweep position in root list. */
528 GCRef gray
; /* List of gray objects. */
529 GCRef grayagain
; /* List of objects for atomic traversal. */
530 GCRef weak
; /* List of weak tables (to be cleared). */
531 GCRef mmudata
; /* List of userdata (to be finalized). */
532 MSize stepmul
; /* Incremental GC step granularity. */
533 MSize debt
; /* Debt (how much GC is behind schedule). */
534 MSize estimate
; /* Estimate of memory actually in use. */
535 MSize pause
; /* Pause between successive GC cycles. */
538 /* Global state, shared by all threads of a Lua universe. */
539 typedef struct global_State
{
540 GCRef
*strhash
; /* String hash table (hash chain anchors). */
541 MSize strmask
; /* String hash mask (size of hash table - 1). */
542 MSize strnum
; /* Number of strings in hash table. */
543 lua_Alloc allocf
; /* Memory allocator. */
544 void *allocd
; /* Memory allocator data. */
545 GCState gc
; /* Garbage collector. */
546 SBuf tmpbuf
; /* Temporary buffer for string concatenation. */
547 Node nilnode
; /* Fallback 1-element hash part (nil key and value). */
548 uint8_t hookmask
; /* Hook mask. */
549 uint8_t dispatchmode
; /* Dispatch mode. */
550 uint8_t vmevmask
; /* VM event mask. */
551 uint8_t wrapmode
; /* Wrap mode. */
552 GCRef mainthref
; /* Link to main thread. */
553 TValue registrytv
; /* Anchor for registry. */
554 TValue tmptv
; /* Temporary TValue. */
555 GCupval uvhead
; /* Head of double-linked list of all open upvalues. */
556 int32_t hookcount
; /* Instruction hook countdown. */
557 int32_t hookcstart
; /* Start count for instruction hook counter. */
558 lua_Hook hookf
; /* Hook function. */
559 lua_CFunction wrapf
; /* Wrapper for C function calls. */
560 lua_CFunction panic
; /* Called as a last resort for errors. */
561 volatile int32_t vmstate
; /* VM state or current JIT code trace number. */
562 GCRef jit_L
; /* Current JIT code lua_State or NULL. */
563 MRef jit_base
; /* Current JIT code L->base. */
564 GCRef gcroot
[GCROOT__MAX
]; /* GC roots. */
565 GCRef mmname
[MM_MAX
]; /* Array holding metamethod names. */
568 #define mainthread(g) (&gcref(g->mainthref)->th)
570 check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val)
572 check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val)
574 /* Hook management. Hook event masks are defined in lua.h. */
575 #define HOOK_EVENTMASK 0x0f
576 #define HOOK_ACTIVE 0x10
577 #define HOOK_VMEVENT 0x20
579 #define hook_active(g) ((g)->hookmask & HOOK_ACTIVE)
580 #define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE)
581 #define hook_entergc(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC))
582 #define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT))
583 #define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE)
584 #define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK)
585 #define hook_restore(g, h) \
586 ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h))
588 /* Per-thread state object. */
591 uint8_t dummy_ffid
; /* Fake FF_C for curr_funcisL() on dummy frames. */
592 uint8_t status
; /* Thread status. */
593 MRef glref
; /* Link to global state. */
594 GCRef gclist
; /* GC chain. */
595 TValue
*base
; /* Base of currently executing function. */
596 TValue
*top
; /* First free slot in the stack. */
597 TValue
*maxstack
; /* Last free slot in the stack. */
598 TValue
*stack
; /* Stack base. */
599 GCRef openupval
; /* List of open upvalues in the stack. */
600 GCRef env
; /* Thread environment (table of globals). */
601 void *cframe
; /* End of C stack frame chain. */
602 MSize stacksize
; /* True stack size (incl. LJ_STACK_EXTRA). */
605 #define G(L) (mref(L->glref, global_State))
606 #define registry(L) (&G(L)->registrytv)
608 /* Macros to access the currently executing (Lua) function. */
609 #define curr_func(L) (&gcref((L->base-1)->fr.func)->fn)
610 #define curr_funcisL(L) (isluafunc(curr_func(L)))
611 #define curr_proto(L) (funcproto(curr_func(L)))
612 #define curr_topL(L) (L->base + curr_proto(L)->framesize)
613 #define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top)
615 /* -- GC object definition and conversions -------------------------------- */
617 /* GC header for generic access to common fields of GC objects. */
618 typedef struct GChead
{
627 /* The env field SHOULD be at the same offset for all GC objects. */
628 LJ_STATIC_ASSERT(offsetof(GChead
, env
) == offsetof(GCfuncL
, env
));
629 LJ_STATIC_ASSERT(offsetof(GChead
, env
) == offsetof(GCudata
, env
));
631 /* The metatable field MUST be at the same offset for all GC objects. */
632 LJ_STATIC_ASSERT(offsetof(GChead
, metatable
) == offsetof(GCtab
, metatable
));
633 LJ_STATIC_ASSERT(offsetof(GChead
, metatable
) == offsetof(GCudata
, metatable
));
635 /* The gclist field MUST be at the same offset for all GC objects. */
636 LJ_STATIC_ASSERT(offsetof(GChead
, gclist
) == offsetof(lua_State
, gclist
));
637 LJ_STATIC_ASSERT(offsetof(GChead
, gclist
) == offsetof(GCproto
, gclist
));
638 LJ_STATIC_ASSERT(offsetof(GChead
, gclist
) == offsetof(GCfuncL
, gclist
));
639 LJ_STATIC_ASSERT(offsetof(GChead
, gclist
) == offsetof(GCtab
, gclist
));
641 typedef union GCobj
{
652 /* Macros to convert a GCobj pointer into a specific value. */
653 #define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str)
654 #define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv)
655 #define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th)
656 #define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt)
657 #define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn)
658 #define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab)
659 #define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud)
661 /* Macro to convert any collectable object into a GCobj pointer. */
662 #define obj2gco(v) (cast(GCobj *, (v)))
664 /* -- Number to integer conversion ---------------------------------------- */
666 static LJ_AINLINE
int32_t lj_num2bit(lua_Number n
)
669 o
.n
= n
+ 6755399441055744.0; /* 2^52 + 2^51 */
670 return (int32_t)o
.u32
.lo
;
673 #if (defined(__i386__) || defined(_M_IX86)) && !defined(__SSE2__)
674 #define lj_num2int(n) lj_num2bit((n))
676 #define lj_num2int(n) ((int32_t)(n))
679 /* -- Miscellaneous object handling --------------------------------------- */
681 /* Names and maps for internal and external object tags. */
682 LJ_DATA
const char *const lj_obj_typename
[1+LUA_TUPVAL
+1];
683 LJ_DATA
const char *const lj_obj_itypename
[~LJ_TNUMX
+1];
685 #define typename(o) (lj_obj_itypename[itypemap(o)])
687 /* Compare two objects without calling metamethods. */
688 LJ_FUNC
int lj_obj_equal(cTValue
*o1
, cTValue
*o2
);
690 #ifdef LUA_USE_ASSERT