xref: /illumos-gate/usr/src/uts/common/fs/zfs/lua/lobject.h (revision dfc11533)
1 /*
2 ** $Id: lobject.h,v 2.71.1.2 2014/05/07 14:14:58 roberto Exp $
3 ** Type definitions for Lua objects
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #ifndef lobject_h
9 #define lobject_h
10 
11 
12 #include <sys/zfs_context.h>
13 
14 #include "llimits.h"
15 #include "lua.h"
16 
17 
18 /*
19 ** Extra tags for non-values
20 */
21 #define LUA_TPROTO	LUA_NUMTAGS
22 #define LUA_TUPVAL	(LUA_NUMTAGS+1)
23 #define LUA_TDEADKEY	(LUA_NUMTAGS+2)
24 
25 /*
26 ** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
27 */
28 #define LUA_TOTALTAGS	(LUA_TUPVAL+2)
29 
30 
31 /*
32 ** tags for Tagged Values have the following use of bits:
33 ** bits 0-3: actual tag (a LUA_T* value)
34 ** bits 4-5: variant bits
35 ** bit 6: whether value is collectable
36 */
37 
38 #define VARBITS		(3 << 4)
39 
40 
41 /*
42 ** LUA_TFUNCTION variants:
43 ** 0 - Lua function
44 ** 1 - light C function
45 ** 2 - regular C function (closure)
46 */
47 
48 /* Variant tags for functions */
49 #define LUA_TLCL	(LUA_TFUNCTION | (0 << 4))  /* Lua closure */
50 #define LUA_TLCF	(LUA_TFUNCTION | (1 << 4))  /* light C function */
51 #define LUA_TCCL	(LUA_TFUNCTION | (2 << 4))  /* C closure */
52 
53 
54 /* Variant tags for strings */
55 #define LUA_TSHRSTR	(LUA_TSTRING | (0 << 4))  /* short strings */
56 #define LUA_TLNGSTR	(LUA_TSTRING | (1 << 4))  /* long strings */
57 
58 
59 /* Bit mark for collectable types */
60 #define BIT_ISCOLLECTABLE	(1 << 6)
61 
62 /* mark a tag as collectable */
63 #define ctb(t)			((t) | BIT_ISCOLLECTABLE)
64 
65 
66 /*
67 ** Union of all collectable objects
68 */
69 typedef union GCObject GCObject;
70 
71 
72 /*
73 ** Common Header for all collectable objects (in macro form, to be
74 ** included in other objects)
75 */
76 #define CommonHeader	GCObject *next; lu_byte tt; lu_byte marked
77 
78 
79 /*
80 ** Common header in struct form
81 */
82 typedef struct GCheader {
83   CommonHeader;
84 } GCheader;
85 
86 
87 
88 /*
89 ** Union of all Lua values
90 */
91 typedef union Value Value;
92 
93 
94 #define numfield	lua_Number n;    /* numbers */
95 
96 
97 
98 /*
99 ** Tagged Values. This is the basic representation of values in Lua,
100 ** an actual value plus a tag with its type.
101 */
102 
103 #define TValuefields	Value value_; int tt_
104 
105 typedef struct lua_TValue TValue;
106 
107 
108 /* macro defining a nil value */
109 #define NILCONSTANT	{NULL}, LUA_TNIL
110 
111 
112 #define val_(o)		((o)->value_)
113 #define num_(o)		(val_(o).n)
114 
115 
116 /* raw type tag of a TValue */
117 #define rttype(o)	((o)->tt_)
118 
119 /* tag with no variants (bits 0-3) */
120 #define novariant(x)	((x) & 0x0F)
121 
122 /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
123 #define ttype(o)	(rttype(o) & 0x3F)
124 
125 /* type tag of a TValue with no variants (bits 0-3) */
126 #define ttypenv(o)	(novariant(rttype(o)))
127 
128 
129 /* Macros to test type */
130 #define checktag(o,t)		(rttype(o) == (t))
131 #define checktype(o,t)		(ttypenv(o) == (t))
132 #define ttisnumber(o)		checktag((o), LUA_TNUMBER)
133 #define ttisnil(o)		checktag((o), LUA_TNIL)
134 #define ttisboolean(o)		checktag((o), LUA_TBOOLEAN)
135 #define ttislightuserdata(o)	checktag((o), LUA_TLIGHTUSERDATA)
136 #define ttisstring(o)		checktype((o), LUA_TSTRING)
137 #define ttisshrstring(o)	checktag((o), ctb(LUA_TSHRSTR))
138 #define ttislngstring(o)	checktag((o), ctb(LUA_TLNGSTR))
139 #define ttistable(o)		checktag((o), ctb(LUA_TTABLE))
140 #define ttisfunction(o)		checktype(o, LUA_TFUNCTION)
141 #define ttisclosure(o)		((rttype(o) & 0x1F) == LUA_TFUNCTION)
142 #define ttisCclosure(o)		checktag((o), ctb(LUA_TCCL))
143 #define ttisLclosure(o)		checktag((o), ctb(LUA_TLCL))
144 #define ttislcf(o)		checktag((o), LUA_TLCF)
145 #define ttisuserdata(o)		checktag((o), ctb(LUA_TUSERDATA))
146 #define ttisthread(o)		checktag((o), ctb(LUA_TTHREAD))
147 #define ttisdeadkey(o)		checktag((o), LUA_TDEADKEY)
148 
149 #define ttisequal(o1,o2)	(rttype(o1) == rttype(o2))
150 
151 /* Macros to access values */
152 #define nvalue(o)	check_exp(ttisnumber(o), num_(o))
153 #define gcvalue(o)	check_exp(iscollectable(o), val_(o).gc)
154 #define pvalue(o)	check_exp(ttislightuserdata(o), val_(o).p)
155 #define rawtsvalue(o)	check_exp(ttisstring(o), &val_(o).gc->ts)
156 #define tsvalue(o)	(&rawtsvalue(o)->tsv)
157 #define rawuvalue(o)	check_exp(ttisuserdata(o), &val_(o).gc->u)
158 #define uvalue(o)	(&rawuvalue(o)->uv)
159 #define clvalue(o)	check_exp(ttisclosure(o), &val_(o).gc->cl)
160 #define clLvalue(o)	check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
161 #define clCvalue(o)	check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
162 #define fvalue(o)	check_exp(ttislcf(o), val_(o).f)
163 #define hvalue(o)	check_exp(ttistable(o), &val_(o).gc->h)
164 #define bvalue(o)	check_exp(ttisboolean(o), val_(o).b)
165 #define thvalue(o)	check_exp(ttisthread(o), &val_(o).gc->th)
166 /* a dead value may get the 'gc' field, but cannot access its contents */
167 #define deadvalue(o)	check_exp(ttisdeadkey(o), cast(void *, val_(o).gc))
168 
169 #define l_isfalse(o)	(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
170 
171 
172 #define iscollectable(o)	(rttype(o) & BIT_ISCOLLECTABLE)
173 
174 
175 /* Macros for internal tests */
176 #define righttt(obj)		(ttype(obj) == gcvalue(obj)->gch.tt)
177 
178 #define checkliveness(g,obj) \
179 	lua_longassert(!iscollectable(obj) || \
180 			(righttt(obj) && !isdead(g,gcvalue(obj))))
181 
182 
183 /* Macros to set values */
184 #define settt_(o,t)	((o)->tt_=(t))
185 
186 #define setnvalue(obj,x) \
187   { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
188 
189 #define setnilvalue(obj) settt_(obj, LUA_TNIL)
190 
191 #define setfvalue(obj,x) \
192   { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
193 
194 #define setpvalue(obj,x) \
195   { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
196 
197 #define setbvalue(obj,x) \
198   { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
199 
200 #define setgcovalue(L,obj,x) \
201   { TValue *io=(obj); GCObject *i_g=(x); \
202     val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
203 
204 #define setsvalue(L,obj,x) \
205   { TValue *io=(obj); \
206     TString *x_ = (x); \
207     val_(io).gc=cast(GCObject *, x_); settt_(io, ctb(x_->tsv.tt)); \
208     checkliveness(G(L),io); }
209 
210 #define setuvalue(L,obj,x) \
211   { TValue *io=(obj); \
212     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
213     checkliveness(G(L),io); }
214 
215 #define setthvalue(L,obj,x) \
216   { TValue *io=(obj); \
217     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
218     checkliveness(G(L),io); }
219 
220 #define setclLvalue(L,obj,x) \
221   { TValue *io=(obj); \
222     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
223     checkliveness(G(L),io); }
224 
225 #define setclCvalue(L,obj,x) \
226   { TValue *io=(obj); \
227     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
228     checkliveness(G(L),io); }
229 
230 #define sethvalue(L,obj,x) \
231   { TValue *io=(obj); \
232     val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
233     checkliveness(G(L),io); }
234 
235 #define setdeadvalue(obj)	settt_(obj, LUA_TDEADKEY)
236 
237 
238 
239 #define setobj(L,obj1,obj2) \
240 	{ const TValue *io2=(obj2); TValue *io1=(obj1); \
241 	  io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
242 	  checkliveness(G(L),io1); }
243 
244 
245 /*
246 ** different types of assignments, according to destination
247 */
248 
249 /* from stack to (same) stack */
250 #define setobjs2s	setobj
251 /* to stack (not from same stack) */
252 #define setobj2s	setobj
253 #define setsvalue2s	setsvalue
254 #define sethvalue2s	sethvalue
255 #define setptvalue2s	setptvalue
256 /* from table to same table */
257 #define setobjt2t	setobj
258 /* to table */
259 #define setobj2t	setobj
260 /* to new object */
261 #define setobj2n	setobj
262 #define setsvalue2n	setsvalue
263 
264 
265 /* check whether a number is valid (useful only for NaN trick) */
266 #define luai_checknum(L,o,c)	{ /* empty */ }
267 
268 
269 /*
270 ** {======================================================
271 ** NaN Trick
272 ** =======================================================
273 */
274 #if defined(LUA_NANTRICK)
275 
276 /*
277 ** numbers are represented in the 'd_' field. All other values have the
278 ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be
279 ** a "signaled NaN", which is never generated by regular operations by
280 ** the CPU (nor by 'strtod')
281 */
282 
283 /* allows for external implementation for part of the trick */
284 #if !defined(NNMARK)	/* { */
285 
286 
287 #if !defined(LUA_IEEEENDIAN)
288 #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN'
289 #endif
290 
291 
292 #define NNMARK		0x7FF7A500
293 #define NNMASK		0x7FFFFF00
294 
295 #undef TValuefields
296 #undef NILCONSTANT
297 
298 #if (LUA_IEEEENDIAN == 0)	/* { */
299 
300 /* little endian */
301 #define TValuefields  \
302 	union { struct { Value v__; int tt__; } i; double d__; } u
303 #define NILCONSTANT	{{{NULL}, tag2tt(LUA_TNIL)}}
304 /* field-access macros */
305 #define v_(o)		((o)->u.i.v__)
306 #define d_(o)		((o)->u.d__)
307 #define tt_(o)		((o)->u.i.tt__)
308 
309 #else				/* }{ */
310 
311 /* big endian */
312 #define TValuefields  \
313 	union { struct { int tt__; Value v__; } i; double d__; } u
314 #define NILCONSTANT	{{tag2tt(LUA_TNIL), {NULL}}}
315 /* field-access macros */
316 #define v_(o)		((o)->u.i.v__)
317 #define d_(o)		((o)->u.d__)
318 #define tt_(o)		((o)->u.i.tt__)
319 
320 #endif				/* } */
321 
322 #endif			/* } */
323 
324 
325 /* correspondence with standard representation */
326 #undef val_
327 #define val_(o)		v_(o)
328 #undef num_
329 #define num_(o)		d_(o)
330 
331 
332 #undef numfield
333 #define numfield	/* no such field; numbers are the entire struct */
334 
335 /* basic check to distinguish numbers from non-numbers */
336 #undef ttisnumber
337 #define ttisnumber(o)	((tt_(o) & NNMASK) != NNMARK)
338 
339 #define tag2tt(t)	(NNMARK | (t))
340 
341 #undef rttype
342 #define rttype(o)	(ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff)
343 
344 #undef settt_
345 #define settt_(o,t)	(tt_(o) = tag2tt(t))
346 
347 #undef setnvalue
348 #define setnvalue(obj,x) \
349 	{ TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
350 
351 #undef setobj
352 #define setobj(L,obj1,obj2) \
353 	{ const TValue *o2_=(obj2); TValue *o1_=(obj1); \
354 	  o1_->u = o2_->u; \
355 	  checkliveness(G(L),o1_); }
356 
357 
358 /*
359 ** these redefinitions are not mandatory, but these forms are more efficient
360 */
361 
362 #undef checktag
363 #undef checktype
364 #define checktag(o,t)	(tt_(o) == tag2tt(t))
365 #define checktype(o,t)	(ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS))
366 
367 #undef ttisequal
368 #define ttisequal(o1,o2)  \
369 	(ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2)))
370 
371 
372 #undef luai_checknum
373 #define luai_checknum(L,o,c)	{ if (!ttisnumber(o)) c; }
374 
375 #endif
376 /* }====================================================== */
377 
378 
379 
380 /*
381 ** {======================================================
382 ** types and prototypes
383 ** =======================================================
384 */
385 
386 
387 union Value {
388   GCObject *gc;    /* collectable objects */
389   void *p;         /* light userdata */
390   int b;           /* booleans */
391   lua_CFunction f; /* light C functions */
392   numfield         /* numbers */
393 };
394 
395 
396 struct lua_TValue {
397   TValuefields;
398 };
399 
400 
401 typedef TValue *StkId;  /* index to stack elements */
402 
403 
404 
405 
406 /*
407 ** Header for string value; string bytes follow the end of this structure
408 */
409 typedef union TString {
410   L_Umaxalign dummy;  /* ensures maximum alignment for strings */
411   struct {
412     CommonHeader;
413     lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
414     unsigned int hash;
415     size_t len;  /* number of characters in string */
416   } tsv;
417 } TString;
418 
419 
420 /* get the actual string (array of bytes) from a TString */
421 #define getstr(ts)	cast(const char *, (ts) + 1)
422 
423 /* get the actual string (array of bytes) from a Lua value */
424 #define svalue(o)       getstr(rawtsvalue(o))
425 
426 
427 /*
428 ** Header for userdata; memory area follows the end of this structure
429 */
430 typedef union Udata {
431   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */
432   struct {
433     CommonHeader;
434     struct Table *metatable;
435     struct Table *env;
436     size_t len;  /* number of bytes */
437   } uv;
438 } Udata;
439 
440 
441 
442 /*
443 ** Description of an upvalue for function prototypes
444 */
445 typedef struct Upvaldesc {
446   TString *name;  /* upvalue name (for debug information) */
447   lu_byte instack;  /* whether it is in stack */
448   lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
449 } Upvaldesc;
450 
451 
452 /*
453 ** Description of a local variable for function prototypes
454 ** (used for debug information)
455 */
456 typedef struct LocVar {
457   TString *varname;
458   int startpc;  /* first point where variable is active */
459   int endpc;    /* first point where variable is dead */
460 } LocVar;
461 
462 
463 /*
464 ** Function Prototypes
465 */
466 typedef struct Proto {
467   CommonHeader;
468   TValue *k;  /* constants used by the function */
469   Instruction *code;
470   struct Proto **p;  /* functions defined inside the function */
471   int *lineinfo;  /* map from opcodes to source lines (debug information) */
472   LocVar *locvars;  /* information about local variables (debug information) */
473   Upvaldesc *upvalues;  /* upvalue information */
474   union Closure *cache;  /* last created closure with this prototype */
475   TString  *source;  /* used for debug information */
476   int sizeupvalues;  /* size of 'upvalues' */
477   int sizek;  /* size of `k' */
478   int sizecode;
479   int sizelineinfo;
480   int sizep;  /* size of `p' */
481   int sizelocvars;
482   int linedefined;
483   int lastlinedefined;
484   GCObject *gclist;
485   lu_byte numparams;  /* number of fixed parameters */
486   lu_byte is_vararg;
487   lu_byte maxstacksize;  /* maximum stack used by this function */
488 } Proto;
489 
490 
491 
492 /*
493 ** Lua Upvalues
494 */
495 typedef struct UpVal {
496   CommonHeader;
497   TValue *v;  /* points to stack or to its own value */
498   union {
499     TValue value;  /* the value (when closed) */
500     struct {  /* double linked list (when open) */
501       struct UpVal *prev;
502       struct UpVal *next;
503     } l;
504   } u;
505 } UpVal;
506 
507 
508 /*
509 ** Closures
510 */
511 
512 #define ClosureHeader \
513 	CommonHeader; lu_byte nupvalues; GCObject *gclist
514 
515 typedef struct CClosure {
516   ClosureHeader;
517   lua_CFunction f;
518   TValue upvalue[1];  /* list of upvalues */
519 } CClosure;
520 
521 
522 typedef struct LClosure {
523   ClosureHeader;
524   struct Proto *p;
525   UpVal *upvals[1];  /* list of upvalues */
526 } LClosure;
527 
528 
529 typedef union Closure {
530   CClosure c;
531   LClosure l;
532 } Closure;
533 
534 
535 #define isLfunction(o)	ttisLclosure(o)
536 
537 #define getproto(o)	(clLvalue(o)->p)
538 
539 
540 /*
541 ** Tables
542 */
543 
544 typedef union TKey {
545   struct {
546     TValuefields;
547     struct Node *next;  /* for chaining */
548   } nk;
549   TValue tvk;
550 } TKey;
551 
552 
553 typedef struct Node {
554   TValue i_val;
555   TKey i_key;
556 } Node;
557 
558 
559 typedef struct Table {
560   CommonHeader;
561   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
562   lu_byte lsizenode;  /* log2 of size of `node' array */
563   int sizearray;  /* size of `array' array */
564   TValue *array;  /* array part */
565   Node *node;
566   Node *lastfree;  /* any free position is before this position */
567   struct Table *metatable;
568   GCObject *gclist;
569 } Table;
570 
571 
572 
573 /*
574 ** `module' operation for hashing (size is always a power of 2)
575 */
576 #define lmod(s,size) \
577 	(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
578 
579 
580 #define twoto(x)	(1<<(x))
581 #define sizenode(t)	(twoto((t)->lsizenode))
582 
583 
584 /*
585 ** (address of) a fixed nil value
586 */
587 #define luaO_nilobject		(&luaO_nilobject_)
588 
589 
590 LUAI_DDEC const TValue luaO_nilobject_;
591 
592 
593 LUAI_FUNC int luaO_int2fb (unsigned int x);
594 LUAI_FUNC int luaO_fb2int (int x);
595 LUAI_FUNC int luaO_ceillog2 (unsigned int x);
596 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
597 LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
598 LUAI_FUNC int luaO_hexavalue (int c);
599 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
600                                                        va_list argp);
601 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
602 LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
603 
604 
605 #endif
606 
607