diff options
Diffstat (limited to 'src/lj_obj.h')
-rw-r--r-- | src/lj_obj.h | 676 |
1 files changed, 676 insertions, 0 deletions
diff --git a/src/lj_obj.h b/src/lj_obj.h new file mode 100644 index 00000000..e5ea713d --- /dev/null +++ b/src/lj_obj.h | |||
@@ -0,0 +1,676 @@ | |||
1 | /* | ||
2 | ** LuaJIT VM tags, values and objects. | ||
3 | ** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h | ||
4 | ** | ||
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 | ||
7 | */ | ||
8 | |||
9 | #ifndef _LJ_OBJ_H | ||
10 | #define _LJ_OBJ_H | ||
11 | |||
12 | #include "lua.h" | ||
13 | #include "lj_def.h" | ||
14 | #include "lj_arch.h" | ||
15 | |||
16 | /* -- Memory references (32 bit address space) ---------------------------- */ | ||
17 | |||
18 | /* Memory size. */ | ||
19 | typedef uint32_t MSize; | ||
20 | |||
21 | /* Memory reference */ | ||
22 | typedef struct MRef { | ||
23 | uint32_t ptr32; /* Pseudo 32 bit pointer. */ | ||
24 | } MRef; | ||
25 | |||
26 | #define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32) | ||
27 | |||
28 | #define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p)) | ||
29 | #define setmrefr(r, v) ((r).ptr32 = (v).ptr32) | ||
30 | |||
31 | /* -- GC object references (32 bit address space) ------------------------- */ | ||
32 | |||
33 | /* GCobj reference */ | ||
34 | typedef struct GCRef { | ||
35 | uint32_t gcptr32; /* Pseudo 32 bit pointer. */ | ||
36 | } GCRef; | ||
37 | |||
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. */ | ||
41 | |||
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)) | ||
48 | |||
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) | ||
54 | |||
55 | /* IMPORTANT NOTE: | ||
56 | ** | ||
57 | ** All uses of the setgcref* macros MUST be accompanied with a write barrier. | ||
58 | ** | ||
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. | ||
62 | ** | ||
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). | ||
73 | ** | ||
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. | ||
76 | ** | ||
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. | ||
81 | */ | ||
82 | |||
83 | /* -- Common type definitions --------------------------------------------- */ | ||
84 | |||
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. */ | ||
90 | |||
91 | /* Internal assembler functions. Never call these directly from C. */ | ||
92 | typedef void (*ASMFunction)(void); | ||
93 | |||
94 | /* Resizable string buffer. Need this here, details in lj_str.h. */ | ||
95 | typedef struct SBuf { | ||
96 | char *buf; /* String buffer base. */ | ||
97 | MSize n; /* String buffer length. */ | ||
98 | MSize sz; /* String buffer size. */ | ||
99 | } SBuf; | ||
100 | |||
101 | /* -- Tags and values ----------------------------------------------------- */ | ||
102 | |||
103 | /* Frame link. */ | ||
104 | typedef union { | ||
105 | int32_t ftsz; /* Frame type and size of previous frame. */ | ||
106 | MRef pcr; /* Overlaps PC for Lua frames. */ | ||
107 | } FrameLink; | ||
108 | |||
109 | /* Tagged value. */ | ||
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. */ | ||
113 | struct { | ||
114 | LJ_ENDIAN_LOHI( | ||
115 | GCRef gcr; /* GCobj reference (if any). */ | ||
116 | , int32_t it; /* Internal object tag. Must overlap MSW of number. */ | ||
117 | ) | ||
118 | }; | ||
119 | struct { | ||
120 | LJ_ENDIAN_LOHI( | ||
121 | GCRef func; /* Function for next frame (or dummy L). */ | ||
122 | , FrameLink tp; /* Link to previous frame. */ | ||
123 | ) | ||
124 | } fr; | ||
125 | struct { | ||
126 | LJ_ENDIAN_LOHI( | ||
127 | uint32_t lo; /* Lower 32 bits of number. */ | ||
128 | , uint32_t hi; /* Upper 32 bits of number. */ | ||
129 | ) | ||
130 | } u32; | ||
131 | } TValue; | ||
132 | |||
133 | typedef const TValue cTValue; | ||
134 | |||
135 | #define tvref(r) (mref(r, TValue)) | ||
136 | |||
137 | /* More external and GCobj tags for internal objects. */ | ||
138 | #define LAST_TT LUA_TTHREAD | ||
139 | |||
140 | #define LUA_TPROTO (LAST_TT+1) | ||
141 | #define LUA_TUPVAL (LAST_TT+2) | ||
142 | #define LUA_TDEADKEY (LAST_TT+3) | ||
143 | |||
144 | /* Internal object tags. | ||
145 | ** | ||
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). | ||
151 | ** | ||
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------ | ||
158 | ** | ||
159 | ** ORDER LJ_T | ||
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. | ||
163 | */ | ||
164 | #define LJ_TNIL (-1) | ||
165 | #define LJ_TFALSE (-2) | ||
166 | #define LJ_TTRUE (-3) | ||
167 | #define LJ_TLIGHTUD (-4) | ||
168 | #define LJ_TSTR (-5) | ||
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) | ||
178 | |||
179 | #if LJ_64 | ||
180 | #define LJ_TISNUM ((uint32_t)0xfff80000) | ||
181 | #else | ||
182 | #define LJ_TISNUM ((uint32_t)LJ_TNUMX) | ||
183 | #endif | ||
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) | ||
188 | |||
189 | /* -- TValue getters/setters ---------------------------------------------- */ | ||
190 | |||
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)) | ||
198 | #if LJ_64 | ||
199 | #define tvislightud(o) ((itype(o) >> 16) == LJ_TLIGHTUD) | ||
200 | #else | ||
201 | #define tvislightud(o) (itype(o) == LJ_TLIGHTUD) | ||
202 | #endif | ||
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) | ||
210 | |||
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() */ | ||
214 | #define tvisgcv(o) \ | ||
215 | ((uitype(o) - LJ_TISGCV) > ((uint32_t)LJ_TNUMX - LJ_TISGCV)) | ||
216 | |||
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) | ||
223 | |||
224 | /* Macros to convert type ids. */ | ||
225 | #if LJ_64 | ||
226 | #define itypemap(o) \ | ||
227 | (tvisnum(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o)) | ||
228 | #else | ||
229 | #define itypemap(o) (tvisnum(o) ? ~LJ_TNUMX : ~itype(o)) | ||
230 | #endif | ||
231 | |||
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)) | ||
235 | #if LJ_64 | ||
236 | #define lightudV(o) check_exp(tvislightud(o), \ | ||
237 | (void *)((o)->u64 & U64x(0000ffff,ffffffff))) | ||
238 | #else | ||
239 | #define lightudV(o) check_exp(tvislightud(o), gcrefp((o)->gcr, void)) | ||
240 | #endif | ||
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) | ||
249 | |||
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)) | ||
254 | |||
255 | #if LJ_64 | ||
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) | ||
262 | #else | ||
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; } | ||
270 | #endif | ||
271 | |||
272 | #define tvchecklive(g, o) \ | ||
273 | lua_assert(!tvisgcv(o) || \ | ||
274 | ((~itype(o) == gcval(o)->gch.gct) && !isdead(g, gcval(o)))) | ||
275 | |||
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) | ||
286 | |||
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))) | ||
290 | |||
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); } | ||
295 | |||
296 | /* -- String object ------------------------------------------------------- */ | ||
297 | |||
298 | /* String object header. String payload follows. */ | ||
299 | typedef struct GCstr { | ||
300 | GCHeader; | ||
301 | uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */ | ||
302 | uint8_t unused; | ||
303 | MSize hash; /* Hash of string. */ | ||
304 | MSize len; /* Size of string. */ | ||
305 | } GCstr; | ||
306 | |||
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) | ||
312 | |||
313 | /* -- Userdata object ----------------------------------------------------- */ | ||
314 | |||
315 | /* Userdata object. Payload follows. */ | ||
316 | typedef struct GCudata { | ||
317 | GCHeader; | ||
318 | uint8_t unused1; | ||
319 | uint8_t unused2; | ||
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. */ | ||
324 | } GCudata; | ||
325 | |||
326 | #define uddata(u) ((void *)((u)+1)) | ||
327 | #define sizeudata(u) (sizeof(struct GCudata)+(u)->len) | ||
328 | |||
329 | /* -- Prototype object ---------------------------------------------------- */ | ||
330 | |||
331 | /* Split constant array. Collectables are below, numbers above pointer. */ | ||
332 | typedef union ProtoK { | ||
333 | lua_Number *n; /* Numbers. */ | ||
334 | GCRef *gc; /* Collectable objects (strings/table/proto). */ | ||
335 | } ProtoK; | ||
336 | |||
337 | #define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) | ||
338 | #define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) | ||
339 | |||
340 | typedef struct VarInfo { | ||
341 | GCstr *name; /* Local variable name. */ | ||
342 | BCPos startpc; /* First point where the local variable is active. */ | ||
343 | BCPos endpc; /* First point where the local variable is dead. */ | ||
344 | } VarInfo; | ||
345 | |||
346 | typedef struct GCproto { | ||
347 | GCHeader; | ||
348 | uint8_t numparams; /* Number of parameters. */ | ||
349 | uint8_t framesize; /* Fixed frame size. */ | ||
350 | MSize sizebc; /* Number of bytecode instructions. */ | ||
351 | GCRef gclist; | ||
352 | ProtoK k; /* Split constant array (points to the middle). */ | ||
353 | BCIns *bc; /* Array of bytecode instructions. */ | ||
354 | int16_t *uv; /* Upvalue list. local >= 0. parent uv < 0. */ | ||
355 | MSize sizekgc; /* Number of collectable constants. */ | ||
356 | MSize sizekn; /* Number of lua_Number constants. */ | ||
357 | uint8_t sizeuv; /* Number of upvalues. */ | ||
358 | uint8_t flags; /* Miscellaneous flags (see below). */ | ||
359 | uint16_t trace; /* Anchor for chain of root traces. */ | ||
360 | /* ------ The following fields are for debugging/tracebacks only ------ */ | ||
361 | MSize sizelineinfo; /* Size of lineinfo array (may be 0). */ | ||
362 | MSize sizevarinfo; /* Size of local var info array (may be 0). */ | ||
363 | MSize sizeuvname; /* Size of upvalue names array (may be 0). */ | ||
364 | BCLine linedefined; /* First line of the function definition. */ | ||
365 | BCLine lastlinedefined; /* Last line of the function definition. */ | ||
366 | BCLine *lineinfo; /* Map from bytecode instructions to source lines. */ | ||
367 | struct VarInfo *varinfo; /* Names and extents of local variables. */ | ||
368 | GCstr **uvname; /* Upvalue names. */ | ||
369 | GCstr *chunkname; /* Name of the chunk this function was defined in. */ | ||
370 | } GCproto; | ||
371 | |||
372 | #define PROTO_IS_VARARG 0x01 | ||
373 | #define PROTO_HAS_FNEW 0x02 | ||
374 | #define PROTO_HAS_RETURN 0x04 | ||
375 | #define PROTO_FIXUP_RETURN 0x08 | ||
376 | #define PROTO_NO_JIT 0x10 | ||
377 | #define PROTO_HAS_ILOOP 0x20 | ||
378 | |||
379 | /* -- Upvalue object ------------------------------------------------------ */ | ||
380 | |||
381 | typedef struct GCupval { | ||
382 | GCHeader; | ||
383 | uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */ | ||
384 | uint8_t unused; | ||
385 | union { | ||
386 | TValue tv; /* If closed: the value itself. */ | ||
387 | struct { /* If open: double linked list, anchored at thread. */ | ||
388 | GCRef prev; | ||
389 | GCRef next; | ||
390 | }; | ||
391 | }; | ||
392 | TValue *v; /* Points to stack slot (open) or above (closed). */ | ||
393 | #if LJ_32 | ||
394 | int32_t unusedv; /* For consistent alignment (32 bit only). */ | ||
395 | #endif | ||
396 | } GCupval; | ||
397 | |||
398 | #define uvprev(uv_) (&gcref((uv_)->prev)->uv) | ||
399 | #define uvnext(uv_) (&gcref((uv_)->next)->uv) | ||
400 | |||
401 | /* -- Function object (closures) ------------------------------------------ */ | ||
402 | |||
403 | /* Common header for functions. env should be at same offset in GCudata. */ | ||
404 | #define GCfuncHeader \ | ||
405 | GCHeader; uint8_t ffid; uint8_t nupvalues; \ | ||
406 | GCRef env; GCRef gclist; ASMFunction gate | ||
407 | |||
408 | typedef struct GCfuncC { | ||
409 | GCfuncHeader; | ||
410 | lua_CFunction f; /* C function to be called. */ | ||
411 | TValue upvalue[1]; /* Array of upvalues (TValue). */ | ||
412 | } GCfuncC; | ||
413 | |||
414 | typedef struct GCfuncL { | ||
415 | GCfuncHeader; | ||
416 | GCRef pt; /* Link to prototype this function is based on. */ | ||
417 | GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */ | ||
418 | } GCfuncL; | ||
419 | |||
420 | typedef union GCfunc { | ||
421 | GCfuncC c; | ||
422 | GCfuncL l; | ||
423 | } GCfunc; | ||
424 | |||
425 | #define FF_LUA 0 | ||
426 | #define FF_C 1 | ||
427 | #define isluafunc(fn) ((fn)->c.ffid == FF_LUA) | ||
428 | #define iscfunc(fn) ((fn)->c.ffid == FF_C) | ||
429 | #define isffunc(fn) ((fn)->c.ffid > FF_C) | ||
430 | #define funcproto(fn) check_exp(isluafunc(fn), &gcref((fn)->l.pt)->pt) | ||
431 | #define sizeCfunc(n) (sizeof(GCfuncC) + sizeof(TValue)*((n)-1)) | ||
432 | #define sizeLfunc(n) (sizeof(GCfuncL) + sizeof(TValue *)*((n)-1)) | ||
433 | |||
434 | /* -- Table object -------------------------------------------------------- */ | ||
435 | |||
436 | /* Hash node. */ | ||
437 | typedef struct Node { | ||
438 | TValue val; /* Value object. Must be first field. */ | ||
439 | TValue key; /* Key object. */ | ||
440 | MRef next; /* Hash chain. */ | ||
441 | int32_t unused; /* For consistent alignment. */ | ||
442 | } Node; | ||
443 | |||
444 | LJ_STATIC_ASSERT(offsetof(Node, val) == 0); | ||
445 | |||
446 | typedef struct GCtab { | ||
447 | GCHeader; | ||
448 | uint8_t nomm; /* Negative cache for fast metamethods. */ | ||
449 | int8_t colo; /* Array colocation. */ | ||
450 | MRef array; /* Array part. */ | ||
451 | GCRef gclist; | ||
452 | GCRef metatable; /* Must be at same offset in GCudata. */ | ||
453 | MRef node; /* Hash part. */ | ||
454 | uint32_t asize; /* Size of array part (keys [0, asize-1]). */ | ||
455 | uint32_t hmask; /* Hash part mask (size of hash part - 1). */ | ||
456 | MRef lastfree; /* Any free position is before this position. */ | ||
457 | } GCtab; | ||
458 | |||
459 | #define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab)) | ||
460 | #define tabref(r) (&gcref((r))->tab) | ||
461 | #define noderef(r) (mref((r), Node)) | ||
462 | #define nextnode(n) (mref((n)->next, Node)) | ||
463 | |||
464 | /* -- State objects ------------------------------------------------------- */ | ||
465 | |||
466 | /* VM states. */ | ||
467 | enum { | ||
468 | LJ_VMST_INTERP, /* Interpreter. */ | ||
469 | LJ_VMST_C, /* C function. */ | ||
470 | LJ_VMST_GC, /* Garbage collector. */ | ||
471 | LJ_VMST_EXIT, /* Trace exit handler. */ | ||
472 | LJ_VMST_RECORD, /* Trace recorder. */ | ||
473 | LJ_VMST_OPT, /* Optimizer. */ | ||
474 | LJ_VMST_ASM, /* Assembler. */ | ||
475 | LJ_VMST__MAX | ||
476 | }; | ||
477 | |||
478 | #define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st) | ||
479 | |||
480 | /* Metamethods. */ | ||
481 | #define MMDEF(_) \ | ||
482 | _(index) _(newindex) _(gc) _(mode) _(eq) \ | ||
483 | /* Only the above (fast) metamethods are negative cached (max. 8). */ \ | ||
484 | _(len) _(lt) _(le) _(concat) _(call) \ | ||
485 | /* The following must be in ORDER ARITH. */ \ | ||
486 | _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \ | ||
487 | /* The following are used in the standard libraries. */ \ | ||
488 | _(metatable) _(tostring) | ||
489 | |||
490 | typedef enum { | ||
491 | #define MMENUM(name) MM_##name, | ||
492 | MMDEF(MMENUM) | ||
493 | #undef MMENUM | ||
494 | MM_MAX, | ||
495 | MM____ = MM_MAX, | ||
496 | MM_FAST = MM_eq | ||
497 | } MMS; | ||
498 | |||
499 | #define BASEMT_MAX ((~LJ_TNUMX)+1) | ||
500 | |||
501 | typedef struct GCState { | ||
502 | MSize total; /* Memory currently allocated. */ | ||
503 | MSize threshold; /* Memory threshold. */ | ||
504 | uint8_t currentwhite; /* Current white color. */ | ||
505 | uint8_t state; /* GC state. */ | ||
506 | uint8_t unused1; | ||
507 | uint8_t unused2; | ||
508 | MSize sweepstr; /* Sweep position in string table. */ | ||
509 | GCRef root; /* List of all collectable objects. */ | ||
510 | GCRef *sweep; /* Sweep position in root list. */ | ||
511 | GCRef gray; /* List of gray objects. */ | ||
512 | GCRef grayagain; /* List of objects for atomic traversal. */ | ||
513 | GCRef weak; /* List of weak tables (to be cleared). */ | ||
514 | GCRef mmudata; /* List of userdata (to be finalized). */ | ||
515 | MSize stepmul; /* Incremental GC step granularity. */ | ||
516 | MSize debt; /* Debt (how much GC is behind schedule). */ | ||
517 | MSize estimate; /* Estimate of memory actually in use. */ | ||
518 | MSize pause; /* Pause between successive GC cycles. */ | ||
519 | } GCState; | ||
520 | |||
521 | /* Global state, shared by all threads of a Lua universe. */ | ||
522 | typedef struct global_State { | ||
523 | GCRef *strhash; /* String hash table (hash chain anchors). */ | ||
524 | MSize strmask; /* String hash mask (size of hash table - 1). */ | ||
525 | MSize strnum; /* Number of strings in hash table. */ | ||
526 | lua_Alloc allocf; /* Memory allocator. */ | ||
527 | void *allocd; /* Memory allocator data. */ | ||
528 | GCState gc; /* Garbage collector. */ | ||
529 | SBuf tmpbuf; /* Temporary buffer for string concatenation. */ | ||
530 | Node nilnode; /* Fallback 1-element hash part (nil key and value). */ | ||
531 | uint8_t hookmask; /* Hook mask. */ | ||
532 | uint8_t dispatchmode; /* Dispatch mode. */ | ||
533 | uint8_t vmevmask; /* VM event mask. */ | ||
534 | uint8_t unused1; | ||
535 | GCRef mainthref; /* Link to main thread. */ | ||
536 | TValue registrytv; /* Anchor for registry. */ | ||
537 | TValue tmptv; /* Temporary TValue. */ | ||
538 | GCupval uvhead; /* Head of double-linked list of all open upvalues. */ | ||
539 | int32_t hookcount; /* Instruction hook countdown. */ | ||
540 | int32_t hookcstart; /* Start count for instruction hook counter. */ | ||
541 | lua_Hook hookf; /* Hook function. */ | ||
542 | lua_CFunction panic; /* Called as a last resort for errors. */ | ||
543 | volatile int32_t vmstate; /* VM state or current JIT code trace number. */ | ||
544 | GCRef jit_L; /* Current JIT code lua_State or NULL. */ | ||
545 | MRef jit_base; /* Current JIT code L->base. */ | ||
546 | GCRef basemt[BASEMT_MAX]; /* Metatables for base types. */ | ||
547 | GCRef mmname[MM_MAX]; /* Array holding metamethod names. */ | ||
548 | } global_State; | ||
549 | |||
550 | #define mainthread(g) (&gcref(g->mainthref)->th) | ||
551 | #define niltv(L) \ | ||
552 | check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val) | ||
553 | #define niltvg(g) \ | ||
554 | check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val) | ||
555 | |||
556 | /* Hook management. Hook event masks are defined in lua.h. */ | ||
557 | #define HOOK_EVENTMASK 0x0f | ||
558 | #define HOOK_ACTIVE 0x10 | ||
559 | #define HOOK_VMEVENT 0x20 | ||
560 | #define HOOK_GC 0x40 | ||
561 | #define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) | ||
562 | #define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE) | ||
563 | #define hook_entergc(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC)) | ||
564 | #define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT)) | ||
565 | #define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE) | ||
566 | #define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) | ||
567 | #define hook_restore(g, h) \ | ||
568 | ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h)) | ||
569 | |||
570 | /* Per-thread state object. */ | ||
571 | struct lua_State { | ||
572 | GCHeader; | ||
573 | uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */ | ||
574 | uint8_t status; /* Thread status. */ | ||
575 | MRef glref; /* Link to global state. */ | ||
576 | GCRef gclist; /* GC chain. */ | ||
577 | TValue *base; /* Base of currently executing function. */ | ||
578 | TValue *top; /* First free slot in the stack. */ | ||
579 | TValue *maxstack; /* Last free slot in the stack. */ | ||
580 | TValue *stack; /* Stack base. */ | ||
581 | GCRef openupval; /* List of open upvalues in the stack. */ | ||
582 | GCRef env; /* Thread environment (table of globals). */ | ||
583 | void *cframe; /* End of C stack frame chain. */ | ||
584 | MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ | ||
585 | }; | ||
586 | |||
587 | #define G(L) (mref(L->glref, global_State)) | ||
588 | #define registry(L) (&G(L)->registrytv) | ||
589 | |||
590 | /* Macros to access the currently executing (Lua) function. */ | ||
591 | #define curr_func(L) (&gcref((L->base-1)->fr.func)->fn) | ||
592 | #define curr_funcisL(L) (isluafunc(curr_func(L))) | ||
593 | #define curr_proto(L) (funcproto(curr_func(L))) | ||
594 | #define curr_topL(L) (L->base + curr_proto(L)->framesize) | ||
595 | #define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top) | ||
596 | |||
597 | /* -- GC object definition and conversions -------------------------------- */ | ||
598 | |||
599 | /* GC header for generic access to common fields of GC objects. */ | ||
600 | typedef struct GChead { | ||
601 | GCHeader; | ||
602 | uint8_t unused1; | ||
603 | uint8_t unused2; | ||
604 | GCRef env; | ||
605 | GCRef gclist; | ||
606 | GCRef metatable; | ||
607 | } GChead; | ||
608 | |||
609 | /* The env field SHOULD be at the same offset for all GC objects. */ | ||
610 | LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env)); | ||
611 | LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env)); | ||
612 | |||
613 | /* The metatable field MUST be at the same offset for all GC objects. */ | ||
614 | LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable)); | ||
615 | LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable)); | ||
616 | |||
617 | /* The gclist field MUST be at the same offset for all GC objects. */ | ||
618 | LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist)); | ||
619 | LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist)); | ||
620 | LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist)); | ||
621 | LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist)); | ||
622 | |||
623 | typedef union GCobj { | ||
624 | GChead gch; | ||
625 | GCstr str; | ||
626 | GCupval uv; | ||
627 | lua_State th; | ||
628 | GCproto pt; | ||
629 | GCfunc fn; | ||
630 | GCtab tab; | ||
631 | GCudata ud; | ||
632 | } GCobj; | ||
633 | |||
634 | /* Macros to convert a GCobj pointer into a specific value. */ | ||
635 | #define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str) | ||
636 | #define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv) | ||
637 | #define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) | ||
638 | #define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) | ||
639 | #define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) | ||
640 | #define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) | ||
641 | #define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) | ||
642 | |||
643 | /* Macro to convert any collectable object into a GCobj pointer. */ | ||
644 | #define obj2gco(v) (cast(GCobj *, (v))) | ||
645 | |||
646 | /* -- Number to integer conversion ---------------------------------------- */ | ||
647 | |||
648 | static LJ_AINLINE int32_t lj_num2bit(lua_Number n) | ||
649 | { | ||
650 | TValue o; | ||
651 | o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ | ||
652 | return (int32_t)o.u32.lo; | ||
653 | } | ||
654 | |||
655 | #if (defined(__i386__) || defined(_M_IX86)) && !defined(__SSE2__) | ||
656 | #define lj_num2int(n) lj_num2bit((n)) | ||
657 | #else | ||
658 | #define lj_num2int(n) ((int32_t)(n)) | ||
659 | #endif | ||
660 | |||
661 | /* -- Miscellaneous object handling --------------------------------------- */ | ||
662 | |||
663 | /* Names and maps for internal and external object tags. */ | ||
664 | LJ_DATA const char *const lj_obj_typename[1+LUA_TUPVAL+1]; | ||
665 | LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; | ||
666 | |||
667 | #define typename(o) (lj_obj_itypename[itypemap(o)]) | ||
668 | |||
669 | /* Compare two objects without calling metamethods. */ | ||
670 | LJ_FUNC int lj_obj_equal(cTValue *o1, cTValue *o2); | ||
671 | |||
672 | #ifdef LUA_USE_ASSERT | ||
673 | #include "lj_gc.h" | ||
674 | #endif | ||
675 | |||
676 | #endif | ||