lj_obj.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /*
  2. ** LuaJIT VM tags, values and objects.
  3. ** Copyright (C) 2005-2023 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. #ifndef _LJ_OBJ_H
  9. #define _LJ_OBJ_H
  10. #include "lua.h"
  11. #include "lj_def.h"
  12. #include "lj_arch.h"
  13. /* -- Memory references --------------------------------------------------- */
  14. /* Memory and GC object sizes. */
  15. typedef uint32_t MSize;
  16. #if LJ_GC64
  17. typedef uint64_t GCSize;
  18. #else
  19. typedef uint32_t GCSize;
  20. #endif
  21. /* Memory reference */
  22. typedef struct MRef {
  23. #if LJ_GC64
  24. uint64_t ptr64; /* True 64 bit pointer. */
  25. #else
  26. uint32_t ptr32; /* Pseudo 32 bit pointer. */
  27. #endif
  28. } MRef;
  29. #if LJ_GC64
  30. #define mref(r, t) ((t *)(void *)(r).ptr64)
  31. #define mrefu(r) ((r).ptr64)
  32. #define setmref(r, p) ((r).ptr64 = (uint64_t)(void *)(p))
  33. #define setmrefu(r, u) ((r).ptr64 = (uint64_t)(u))
  34. #define setmrefr(r, v) ((r).ptr64 = (v).ptr64)
  35. #else
  36. #define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32)
  37. #define mrefu(r) ((r).ptr32)
  38. #define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p))
  39. #define setmrefu(r, u) ((r).ptr32 = (uint32_t)(u))
  40. #define setmrefr(r, v) ((r).ptr32 = (v).ptr32)
  41. #endif
  42. /* -- GC object references ------------------------------------------------ */
  43. /* GCobj reference */
  44. typedef struct GCRef {
  45. #if LJ_GC64
  46. uint64_t gcptr64; /* True 64 bit pointer. */
  47. #else
  48. uint32_t gcptr32; /* Pseudo 32 bit pointer. */
  49. #endif
  50. } GCRef;
  51. /* Common GC header for all collectable objects. */
  52. #define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct
  53. /* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */
  54. #if LJ_GC64
  55. #define gcref(r) ((GCobj *)(r).gcptr64)
  56. #define gcrefp(r, t) ((t *)(void *)(r).gcptr64)
  57. #define gcrefu(r) ((r).gcptr64)
  58. #define gcrefeq(r1, r2) ((r1).gcptr64 == (r2).gcptr64)
  59. #define setgcref(r, gc) ((r).gcptr64 = (uint64_t)&(gc)->gch)
  60. #define setgcreft(r, gc, it) \
  61. (r).gcptr64 = (uint64_t)&(gc)->gch | (((uint64_t)(it)) << 47)
  62. #define setgcrefp(r, p) ((r).gcptr64 = (uint64_t)(p))
  63. #define setgcrefnull(r) ((r).gcptr64 = 0)
  64. #define setgcrefr(r, v) ((r).gcptr64 = (v).gcptr64)
  65. #else
  66. #define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32)
  67. #define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32)
  68. #define gcrefu(r) ((r).gcptr32)
  69. #define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32)
  70. #define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch)
  71. #define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p))
  72. #define setgcrefnull(r) ((r).gcptr32 = 0)
  73. #define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32)
  74. #endif
  75. #define gcnext(gc) (gcref((gc)->gch.nextgc))
  76. /* IMPORTANT NOTE:
  77. **
  78. ** All uses of the setgcref* macros MUST be accompanied with a write barrier.
  79. **
  80. ** This is to ensure the integrity of the incremental GC. The invariant
  81. ** to preserve is that a black object never points to a white object.
  82. ** I.e. never store a white object into a field of a black object.
  83. **
  84. ** It's ok to LEAVE OUT the write barrier ONLY in the following cases:
  85. ** - The source is not a GC object (NULL).
  86. ** - The target is a GC root. I.e. everything in global_State.
  87. ** - The target is a lua_State field (threads are never black).
  88. ** - The target is a stack slot, see setgcV et al.
  89. ** - The target is an open upvalue, i.e. pointing to a stack slot.
  90. ** - The target is a newly created object (i.e. marked white). But make
  91. ** sure nothing invokes the GC inbetween.
  92. ** - The target and the source are the same object (self-reference).
  93. ** - The target already contains the object (e.g. moving elements around).
  94. **
  95. ** The most common case is a store to a stack slot. All other cases where
  96. ** a barrier has been omitted are annotated with a NOBARRIER comment.
  97. **
  98. ** The same logic applies for stores to table slots (array part or hash
  99. ** part). ALL uses of lj_tab_set* require a barrier for the stored value
  100. ** *and* the stored key, based on the above rules. In practice this means
  101. ** a barrier is needed if *either* of the key or value are a GC object.
  102. **
  103. ** It's ok to LEAVE OUT the write barrier in the following special cases:
  104. ** - The stored value is nil. The key doesn't matter because it's either
  105. ** not resurrected or lj_tab_newkey() will take care of the key barrier.
  106. ** - The key doesn't matter if the *previously* stored value is guaranteed
  107. ** to be non-nil (because the key is kept alive in the table).
  108. ** - The key doesn't matter if it's guaranteed not to be part of the table,
  109. ** since lj_tab_newkey() takes care of the key barrier. This applies
  110. ** trivially to new tables, but watch out for resurrected keys. Storing
  111. ** a nil value leaves the key in the table!
  112. **
  113. ** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used
  114. ** by the interpreter for all table stores.
  115. **
  116. ** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark
  117. ** dead keys in tables. The reference is left in, but it's guaranteed to
  118. ** be never dereferenced as long as the value is nil. It's ok if the key is
  119. ** freed or if any object subsequently gets the same address.
  120. **
  121. ** Not destroying dead keys helps to keep key hash slots stable. This avoids
  122. ** specialization back-off for HREFK when a value flips between nil and
  123. ** non-nil and the GC gets in the way. It also allows safely hoisting
  124. ** HREF/HREFK across GC steps. Dead keys are only removed if a table is
  125. ** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize.
  126. **
  127. ** The trade-off is that a write barrier for tables must take the key into
  128. ** account, too. Implicitly resurrecting the key by storing a non-nil value
  129. ** may invalidate the incremental GC invariant.
  130. */
  131. /* -- Common type definitions --------------------------------------------- */
  132. /* Types for handling bytecodes. Need this here, details in lj_bc.h. */
  133. typedef uint32_t BCIns; /* Bytecode instruction. */
  134. typedef uint32_t BCPos; /* Bytecode position. */
  135. typedef uint32_t BCReg; /* Bytecode register. */
  136. typedef int32_t BCLine; /* Bytecode line number. */
  137. /* Internal assembler functions. Never call these directly from C. */
  138. typedef void (*ASMFunction)(void);
  139. /* Resizable string buffer. Need this here, details in lj_buf.h. */
  140. #define SBufHeader char *w, *e, *b; MRef L
  141. typedef struct SBuf {
  142. SBufHeader;
  143. } SBuf;
  144. /* -- Tags and values ----------------------------------------------------- */
  145. /* Frame link. */
  146. typedef union {
  147. int32_t ftsz; /* Frame type and size of previous frame. */
  148. MRef pcr; /* Or PC for Lua frames. */
  149. } FrameLink;
  150. /* Tagged value. */
  151. typedef LJ_ALIGN(8) union TValue {
  152. uint64_t u64; /* 64 bit pattern overlaps number. */
  153. lua_Number n; /* Number object overlaps split tag/value object. */
  154. #if LJ_GC64
  155. GCRef gcr; /* GCobj reference with tag. */
  156. int64_t it64;
  157. struct {
  158. LJ_ENDIAN_LOHI(
  159. int32_t i; /* Integer value. */
  160. , uint32_t it; /* Internal object tag. Must overlap MSW of number. */
  161. )
  162. };
  163. #else
  164. struct {
  165. LJ_ENDIAN_LOHI(
  166. union {
  167. GCRef gcr; /* GCobj reference (if any). */
  168. int32_t i; /* Integer value. */
  169. };
  170. , uint32_t it; /* Internal object tag. Must overlap MSW of number. */
  171. )
  172. };
  173. #endif
  174. #if LJ_FR2
  175. int64_t ftsz; /* Frame type and size of previous frame, or PC. */
  176. #else
  177. struct {
  178. LJ_ENDIAN_LOHI(
  179. GCRef func; /* Function for next frame (or dummy L). */
  180. , FrameLink tp; /* Link to previous frame. */
  181. )
  182. } fr;
  183. #endif
  184. struct {
  185. LJ_ENDIAN_LOHI(
  186. uint32_t lo; /* Lower 32 bits of number. */
  187. , uint32_t hi; /* Upper 32 bits of number. */
  188. )
  189. } u32;
  190. } TValue;
  191. typedef const TValue cTValue;
  192. #define tvref(r) (mref(r, TValue))
  193. /* More external and GCobj tags for internal objects. */
  194. #define LAST_TT LUA_TTHREAD
  195. #define LUA_TPROTO (LAST_TT+1)
  196. #define LUA_TCDATA (LAST_TT+2)
  197. /* Internal object tags.
  198. **
  199. ** Format for 32 bit GC references (!LJ_GC64):
  200. **
  201. ** Internal tags overlap the MSW of a number object (must be a double).
  202. ** Interpreted as a double these are special NaNs. The FPU only generates
  203. ** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available
  204. ** for use as internal tags. Small negative numbers are used to shorten the
  205. ** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate).
  206. **
  207. ** ---MSW---.---LSW---
  208. ** primitive types | itype | |
  209. ** lightuserdata | itype | void * | (32 bit platforms)
  210. ** lightuserdata |ffff|seg| ofs | (64 bit platforms)
  211. ** GC objects | itype | GCRef |
  212. ** int (LJ_DUALNUM)| itype | int |
  213. ** number -------double------
  214. **
  215. ** Format for 64 bit GC references (LJ_GC64):
  216. **
  217. ** The upper 13 bits must be 1 (0xfff8...) for a special NaN. The next
  218. ** 4 bits hold the internal tag. The lowest 47 bits either hold a pointer,
  219. ** a zero-extended 32 bit integer or all bits set to 1 for primitive types.
  220. **
  221. ** ------MSW------.------LSW------
  222. ** primitive types |1..1|itype|1..................1|
  223. ** GC objects |1..1|itype|-------GCRef--------|
  224. ** lightuserdata |1..1|itype|seg|------ofs-------|
  225. ** int (LJ_DUALNUM) |1..1|itype|0..0|-----int-------|
  226. ** number ------------double-------------
  227. **
  228. ** ORDER LJ_T
  229. ** Primitive types nil/false/true must be first, lightuserdata next.
  230. ** GC objects are at the end, table/userdata must be lowest.
  231. ** Also check lj_ir.h for similar ordering constraints.
  232. */
  233. #define LJ_TNIL (~0u)
  234. #define LJ_TFALSE (~1u)
  235. #define LJ_TTRUE (~2u)
  236. #define LJ_TLIGHTUD (~3u)
  237. #define LJ_TSTR (~4u)
  238. #define LJ_TUPVAL (~5u)
  239. #define LJ_TTHREAD (~6u)
  240. #define LJ_TPROTO (~7u)
  241. #define LJ_TFUNC (~8u)
  242. #define LJ_TTRACE (~9u)
  243. #define LJ_TCDATA (~10u)
  244. #define LJ_TTAB (~11u)
  245. #define LJ_TUDATA (~12u)
  246. /* This is just the canonical number type used in some places. */
  247. #define LJ_TNUMX (~13u)
  248. /* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */
  249. #if LJ_64 && !LJ_GC64
  250. #define LJ_TISNUM 0xfffeffffu
  251. #else
  252. #define LJ_TISNUM LJ_TNUMX
  253. #endif
  254. #define LJ_TISTRUECOND LJ_TFALSE
  255. #define LJ_TISPRI LJ_TTRUE
  256. #define LJ_TISGCV (LJ_TSTR+1)
  257. #define LJ_TISTABUD LJ_TTAB
  258. /* Type marker for slot holding a traversal index. Must be lightuserdata. */
  259. #define LJ_KEYINDEX 0xfffe7fffu
  260. #if LJ_GC64
  261. #define LJ_GCVMASK (((uint64_t)1 << 47) - 1)
  262. #endif
  263. #if LJ_64
  264. /* To stay within 47 bits, lightuserdata is segmented. */
  265. #define LJ_LIGHTUD_BITS_SEG 8
  266. #define LJ_LIGHTUD_BITS_LO (47 - LJ_LIGHTUD_BITS_SEG)
  267. #endif
  268. /* -- String object ------------------------------------------------------- */
  269. typedef uint32_t StrHash; /* String hash value. */
  270. typedef uint32_t StrID; /* String ID. */
  271. /* String object header. String payload follows. */
  272. typedef struct GCstr {
  273. GCHeader;
  274. uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */
  275. uint8_t hashalg; /* Hash algorithm. */
  276. StrID sid; /* Interned string ID. */
  277. StrHash hash; /* Hash of string. */
  278. MSize len; /* Size of string. */
  279. } GCstr;
  280. #define strref(r) (&gcref((r))->str)
  281. #define strdata(s) ((const char *)((s)+1))
  282. #define strdatawr(s) ((char *)((s)+1))
  283. #define strVdata(o) strdata(strV(o))
  284. /* -- Userdata object ----------------------------------------------------- */
  285. /* Userdata object. Payload follows. */
  286. typedef struct GCudata {
  287. GCHeader;
  288. uint8_t udtype; /* Userdata type. */
  289. uint8_t unused2;
  290. GCRef env; /* Should be at same offset in GCfunc. */
  291. MSize len; /* Size of payload. */
  292. GCRef metatable; /* Must be at same offset in GCtab. */
  293. uint32_t align1; /* To force 8 byte alignment of the payload. */
  294. } GCudata;
  295. /* Userdata types. */
  296. enum {
  297. UDTYPE_USERDATA, /* Regular userdata. */
  298. UDTYPE_IO_FILE, /* I/O library FILE. */
  299. UDTYPE_FFI_CLIB, /* FFI C library namespace. */
  300. UDTYPE_BUFFER, /* String buffer. */
  301. UDTYPE__MAX
  302. };
  303. #define uddata(u) ((void *)((u)+1))
  304. #define sizeudata(u) (sizeof(struct GCudata)+(u)->len)
  305. /* -- C data object ------------------------------------------------------- */
  306. /* C data object. Payload follows. */
  307. typedef struct GCcdata {
  308. GCHeader;
  309. uint16_t ctypeid; /* C type ID. */
  310. } GCcdata;
  311. /* Prepended to variable-sized or realigned C data objects. */
  312. typedef struct GCcdataVar {
  313. uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */
  314. uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */
  315. MSize len; /* Size of payload. */
  316. } GCcdataVar;
  317. #define cdataptr(cd) ((void *)((cd)+1))
  318. #define cdataisv(cd) ((cd)->marked & 0x80)
  319. #define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar)))
  320. #define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len)
  321. #define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra)
  322. #define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset))
  323. /* -- Prototype object ---------------------------------------------------- */
  324. #define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
  325. #define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))
  326. typedef struct GCproto {
  327. GCHeader;
  328. uint8_t numparams; /* Number of parameters. */
  329. uint8_t framesize; /* Fixed frame size. */
  330. MSize sizebc; /* Number of bytecode instructions. */
  331. #if LJ_GC64
  332. uint32_t unused_gc64;
  333. #endif
  334. GCRef gclist;
  335. MRef k; /* Split constant array (points to the middle). */
  336. MRef uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */
  337. MSize sizekgc; /* Number of collectable constants. */
  338. MSize sizekn; /* Number of lua_Number constants. */
  339. MSize sizept; /* Total size including colocated arrays. */
  340. uint8_t sizeuv; /* Number of upvalues. */
  341. uint8_t flags; /* Miscellaneous flags (see below). */
  342. uint16_t trace; /* Anchor for chain of root traces. */
  343. /* ------ The following fields are for debugging/tracebacks only ------ */
  344. GCRef chunkname; /* Name of the chunk this function was defined in. */
  345. BCLine firstline; /* First line of the function definition. */
  346. BCLine numline; /* Number of lines for the function definition. */
  347. MRef lineinfo; /* Compressed map from bytecode ins. to source line. */
  348. MRef uvinfo; /* Upvalue names. */
  349. MRef varinfo; /* Names and compressed extents of local variables. */
  350. } GCproto;
  351. /* Flags for prototype. */
  352. #define PROTO_CHILD 0x01 /* Has child prototypes. */
  353. #define PROTO_VARARG 0x02 /* Vararg function. */
  354. #define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */
  355. #define PROTO_NOJIT 0x08 /* JIT disabled for this function. */
  356. #define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */
  357. /* Only used during parsing. */
  358. #define PROTO_HAS_RETURN 0x20 /* Already emitted a return. */
  359. #define PROTO_FIXUP_RETURN 0x40 /* Need to fixup emitted returns. */
  360. /* Top bits used for counting created closures. */
  361. #define PROTO_CLCOUNT 0x20 /* Base of saturating 3 bit counter. */
  362. #define PROTO_CLC_BITS 3
  363. #define PROTO_CLC_POLY (3*PROTO_CLCOUNT) /* Polymorphic threshold. */
  364. #define PROTO_UV_LOCAL 0x8000 /* Upvalue for local slot. */
  365. #define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */
  366. #define proto_kgc(pt, idx) \
  367. check_exp((uintptr_t)(intptr_t)(idx) >= ~(uintptr_t)(pt)->sizekgc+1u, \
  368. gcref(mref((pt)->k, GCRef)[(idx)]))
  369. #define proto_knumtv(pt, idx) \
  370. check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)])
  371. #define proto_bc(pt) ((BCIns *)((char *)(pt) + sizeof(GCproto)))
  372. #define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt)))
  373. #define proto_uv(pt) (mref((pt)->uv, uint16_t))
  374. #define proto_chunkname(pt) (strref((pt)->chunkname))
  375. #define proto_chunknamestr(pt) (strdata(proto_chunkname((pt))))
  376. #define proto_lineinfo(pt) (mref((pt)->lineinfo, const void))
  377. #define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t))
  378. #define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t))
  379. /* -- Upvalue object ------------------------------------------------------ */
  380. typedef struct GCupval {
  381. GCHeader;
  382. uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */
  383. uint8_t immutable; /* Immutable value. */
  384. union {
  385. TValue tv; /* If closed: the value itself. */
  386. struct { /* If open: double linked list, anchored at thread. */
  387. GCRef prev;
  388. GCRef next;
  389. };
  390. };
  391. MRef v; /* Points to stack slot (open) or above (closed). */
  392. uint32_t dhash; /* Disambiguation hash: dh1 != dh2 => cannot alias. */
  393. } GCupval;
  394. #define uvprev(uv_) (&gcref((uv_)->prev)->uv)
  395. #define uvnext(uv_) (&gcref((uv_)->next)->uv)
  396. #define uvval(uv_) (mref((uv_)->v, TValue))
  397. /* -- Function object (closures) ------------------------------------------ */
  398. /* Common header for functions. env should be at same offset in GCudata. */
  399. #define GCfuncHeader \
  400. GCHeader; uint8_t ffid; uint8_t nupvalues; \
  401. GCRef env; GCRef gclist; MRef pc
  402. typedef struct GCfuncC {
  403. GCfuncHeader;
  404. lua_CFunction f; /* C function to be called. */
  405. TValue upvalue[1]; /* Array of upvalues (TValue). */
  406. } GCfuncC;
  407. typedef struct GCfuncL {
  408. GCfuncHeader;
  409. GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */
  410. } GCfuncL;
  411. typedef union GCfunc {
  412. GCfuncC c;
  413. GCfuncL l;
  414. } GCfunc;
  415. #define FF_LUA 0
  416. #define FF_C 1
  417. #define isluafunc(fn) ((fn)->c.ffid == FF_LUA)
  418. #define iscfunc(fn) ((fn)->c.ffid == FF_C)
  419. #define isffunc(fn) ((fn)->c.ffid > FF_C)
  420. #define funcproto(fn) \
  421. check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto)))
  422. #define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n))
  423. #define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n))
  424. /* -- Table object -------------------------------------------------------- */
  425. /* Hash node. */
  426. typedef struct Node {
  427. TValue val; /* Value object. Must be first field. */
  428. TValue key; /* Key object. */
  429. MRef next; /* Hash chain. */
  430. #if !LJ_GC64
  431. MRef freetop; /* Top of free elements (stored in t->node[0]). */
  432. #endif
  433. } Node;
  434. LJ_STATIC_ASSERT(offsetof(Node, val) == 0);
  435. typedef struct GCtab {
  436. GCHeader;
  437. uint8_t nomm; /* Negative cache for fast metamethods. */
  438. int8_t colo; /* Array colocation. */
  439. MRef array; /* Array part. */
  440. GCRef gclist;
  441. GCRef metatable; /* Must be at same offset in GCudata. */
  442. MRef node; /* Hash part. */
  443. uint32_t asize; /* Size of array part (keys [0, asize-1]). */
  444. uint32_t hmask; /* Hash part mask (size of hash part - 1). */
  445. #if LJ_GC64
  446. MRef freetop; /* Top of free elements. */
  447. #endif
  448. } GCtab;
  449. #define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab))
  450. #define tabref(r) ((GCtab *)gcref((r)))
  451. #define noderef(r) (mref((r), Node))
  452. #define nextnode(n) (mref((n)->next, Node))
  453. #if LJ_GC64
  454. #define getfreetop(t, n) (noderef((t)->freetop))
  455. #define setfreetop(t, n, v) (setmref((t)->freetop, (v)))
  456. #else
  457. #define getfreetop(t, n) (noderef((n)->freetop))
  458. #define setfreetop(t, n, v) (setmref((n)->freetop, (v)))
  459. #endif
  460. /* -- State objects ------------------------------------------------------- */
  461. /* VM states. */
  462. enum {
  463. LJ_VMST_INTERP, /* Interpreter. */
  464. LJ_VMST_C, /* C function. */
  465. LJ_VMST_GC, /* Garbage collector. */
  466. LJ_VMST_EXIT, /* Trace exit handler. */
  467. LJ_VMST_RECORD, /* Trace recorder. */
  468. LJ_VMST_OPT, /* Optimizer. */
  469. LJ_VMST_ASM, /* Assembler. */
  470. LJ_VMST__MAX
  471. };
  472. #define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st)
  473. /* Metamethods. ORDER MM */
  474. #ifdef LJ_HASFFI
  475. #define MMDEF_FFI(_) _(new)
  476. #else
  477. #define MMDEF_FFI(_)
  478. #endif
  479. #if LJ_52 || LJ_HASFFI
  480. #define MMDEF_PAIRS(_) _(pairs) _(ipairs)
  481. #else
  482. #define MMDEF_PAIRS(_)
  483. #define MM_pairs 255
  484. #define MM_ipairs 255
  485. #endif
  486. #define MMDEF(_) \
  487. _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \
  488. /* Only the above (fast) metamethods are negative cached (max. 8). */ \
  489. _(lt) _(le) _(concat) _(call) \
  490. /* The following must be in ORDER ARITH. */ \
  491. _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \
  492. /* The following are used in the standard libraries. */ \
  493. _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_)
  494. typedef enum {
  495. #define MMENUM(name) MM_##name,
  496. MMDEF(MMENUM)
  497. #undef MMENUM
  498. MM__MAX,
  499. MM____ = MM__MAX,
  500. MM_FAST = MM_len
  501. } MMS;
  502. /* GC root IDs. */
  503. typedef enum {
  504. GCROOT_MMNAME, /* Metamethod names. */
  505. GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1,
  506. GCROOT_BASEMT, /* Metatables for base types. */
  507. GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX,
  508. GCROOT_IO_INPUT, /* Userdata for default I/O input file. */
  509. GCROOT_IO_OUTPUT, /* Userdata for default I/O output file. */
  510. GCROOT_MAX
  511. } GCRootID;
  512. #define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)])
  513. #define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)])
  514. #define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)]))
  515. /* Garbage collector state. */
  516. typedef struct GCState {
  517. GCSize total; /* Memory currently allocated. */
  518. GCSize threshold; /* Memory threshold. */
  519. uint8_t currentwhite; /* Current white color. */
  520. uint8_t state; /* GC state. */
  521. uint8_t nocdatafin; /* No cdata finalizer called. */
  522. #if LJ_64
  523. uint8_t lightudnum; /* Number of lightuserdata segments - 1. */
  524. #else
  525. uint8_t unused1;
  526. #endif
  527. MSize sweepstr; /* Sweep position in string table. */
  528. GCRef root; /* List of all collectable objects. */
  529. MRef sweep; /* Sweep position in root list. */
  530. GCRef gray; /* List of gray objects. */
  531. GCRef grayagain; /* List of objects for atomic traversal. */
  532. GCRef weak; /* List of weak tables (to be cleared). */
  533. GCRef mmudata; /* List of userdata (to be finalized). */
  534. GCSize debt; /* Debt (how much GC is behind schedule). */
  535. GCSize estimate; /* Estimate of memory actually in use. */
  536. MSize stepmul; /* Incremental GC step granularity. */
  537. MSize pause; /* Pause between successive GC cycles. */
  538. #if LJ_64
  539. MRef lightudseg; /* Upper bits of lightuserdata segments. */
  540. #endif
  541. } GCState;
  542. /* String interning state. */
  543. typedef struct StrInternState {
  544. GCRef *tab; /* String hash table anchors. */
  545. MSize mask; /* String hash mask (size of hash table - 1). */
  546. MSize num; /* Number of strings in hash table. */
  547. StrID id; /* Next string ID. */
  548. uint8_t idreseed; /* String ID reseed counter. */
  549. uint8_t second; /* String interning table uses secondary hashing. */
  550. uint8_t unused1;
  551. uint8_t unused2;
  552. LJ_ALIGN(8) uint64_t seed; /* Random string seed. */
  553. } StrInternState;
  554. /* Global state, shared by all threads of a Lua universe. */
  555. typedef struct global_State {
  556. lua_Alloc allocf; /* Memory allocator. */
  557. void *allocd; /* Memory allocator data. */
  558. GCState gc; /* Garbage collector. */
  559. GCstr strempty; /* Empty string. */
  560. uint8_t stremptyz; /* Zero terminator of empty string. */
  561. uint8_t hookmask; /* Hook mask. */
  562. uint8_t dispatchmode; /* Dispatch mode. */
  563. uint8_t vmevmask; /* VM event mask. */
  564. StrInternState str; /* String interning. */
  565. volatile int32_t vmstate; /* VM state or current JIT code trace number. */
  566. GCRef mainthref; /* Link to main thread. */
  567. SBuf tmpbuf; /* Temporary string buffer. */
  568. TValue tmptv, tmptv2; /* Temporary TValues. */
  569. Node nilnode; /* Fallback 1-element hash part (nil key and value). */
  570. TValue registrytv; /* Anchor for registry. */
  571. GCupval uvhead; /* Head of double-linked list of all open upvalues. */
  572. int32_t hookcount; /* Instruction hook countdown. */
  573. int32_t hookcstart; /* Start count for instruction hook counter. */
  574. lua_Hook hookf; /* Hook function. */
  575. lua_CFunction wrapf; /* Wrapper for C function calls. */
  576. lua_CFunction panic; /* Called as a last resort for errors. */
  577. BCIns bc_cfunc_int; /* Bytecode for internal C function calls. */
  578. BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */
  579. GCRef cur_L; /* Currently executing lua_State. */
  580. MRef jit_base; /* Current JIT code L->base or NULL. */
  581. MRef ctype_state; /* Pointer to C type state. */
  582. PRNGState prng; /* Global PRNG state. */
  583. GCRef gcroot[GCROOT_MAX]; /* GC roots. */
  584. } global_State;
  585. #define mainthread(g) (&gcref(g->mainthref)->th)
  586. #define niltv(L) \
  587. check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val)
  588. #define niltvg(g) \
  589. check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val)
  590. /* Hook management. Hook event masks are defined in lua.h. */
  591. #define HOOK_EVENTMASK 0x0f
  592. #define HOOK_ACTIVE 0x10
  593. #define HOOK_ACTIVE_SHIFT 4
  594. #define HOOK_VMEVENT 0x20
  595. #define HOOK_GC 0x40
  596. #define HOOK_PROFILE 0x80
  597. #define hook_active(g) ((g)->hookmask & HOOK_ACTIVE)
  598. #define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE)
  599. #define hook_entergc(g) \
  600. ((g)->hookmask = ((g)->hookmask | (HOOK_ACTIVE|HOOK_GC)) & ~HOOK_PROFILE)
  601. #define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT))
  602. #define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE)
  603. #define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK)
  604. #define hook_restore(g, h) \
  605. ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h))
  606. /* Per-thread state object. */
  607. struct lua_State {
  608. GCHeader;
  609. uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */
  610. uint8_t status; /* Thread status. */
  611. MRef glref; /* Link to global state. */
  612. GCRef gclist; /* GC chain. */
  613. TValue *base; /* Base of currently executing function. */
  614. TValue *top; /* First free slot in the stack. */
  615. MRef maxstack; /* Last free slot in the stack. */
  616. MRef stack; /* Stack base. */
  617. GCRef openupval; /* List of open upvalues in the stack. */
  618. GCRef env; /* Thread environment (table of globals). */
  619. void *cframe; /* End of C stack frame chain. */
  620. MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */
  621. };
  622. #define G(L) (mref(L->glref, global_State))
  623. #define registry(L) (&G(L)->registrytv)
  624. /* Macros to access the currently executing (Lua) function. */
  625. #if LJ_GC64
  626. #define curr_func(L) (&gcval(L->base-2)->fn)
  627. #elif LJ_FR2
  628. #define curr_func(L) (&gcref((L->base-2)->gcr)->fn)
  629. #else
  630. #define curr_func(L) (&gcref((L->base-1)->fr.func)->fn)
  631. #endif
  632. #define curr_funcisL(L) (isluafunc(curr_func(L)))
  633. #define curr_proto(L) (funcproto(curr_func(L)))
  634. #define curr_topL(L) (L->base + curr_proto(L)->framesize)
  635. #define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top)
  636. #if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK)
  637. LJ_FUNC_NORET void lj_assert_fail(global_State *g, const char *file, int line,
  638. const char *func, const char *fmt, ...);
  639. #endif
  640. /* -- GC object definition and conversions -------------------------------- */
  641. /* GC header for generic access to common fields of GC objects. */
  642. typedef struct GChead {
  643. GCHeader;
  644. uint8_t unused1;
  645. uint8_t unused2;
  646. GCRef env;
  647. GCRef gclist;
  648. GCRef metatable;
  649. } GChead;
  650. /* The env field SHOULD be at the same offset for all GC objects. */
  651. LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env));
  652. LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env));
  653. /* The metatable field MUST be at the same offset for all GC objects. */
  654. LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable));
  655. LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable));
  656. /* The gclist field MUST be at the same offset for all GC objects. */
  657. LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist));
  658. LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist));
  659. LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist));
  660. LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist));
  661. typedef union GCobj {
  662. GChead gch;
  663. GCstr str;
  664. GCupval uv;
  665. lua_State th;
  666. GCproto pt;
  667. GCfunc fn;
  668. GCcdata cd;
  669. GCtab tab;
  670. GCudata ud;
  671. } GCobj;
  672. /* Macros to convert a GCobj pointer into a specific value. */
  673. #define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str)
  674. #define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv)
  675. #define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th)
  676. #define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt)
  677. #define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn)
  678. #define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd)
  679. #define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab)
  680. #define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud)
  681. /* Macro to convert any collectable object into a GCobj pointer. */
  682. #define obj2gco(v) ((GCobj *)(v))
  683. /* -- TValue getters/setters ---------------------------------------------- */
  684. /* Macros to test types. */
  685. #if LJ_GC64
  686. #define itype(o) ((uint32_t)((o)->it64 >> 47))
  687. #define tvisnil(o) ((o)->it64 == -1)
  688. #else
  689. #define itype(o) ((o)->it)
  690. #define tvisnil(o) (itype(o) == LJ_TNIL)
  691. #endif
  692. #define tvisfalse(o) (itype(o) == LJ_TFALSE)
  693. #define tvistrue(o) (itype(o) == LJ_TTRUE)
  694. #define tvisbool(o) (tvisfalse(o) || tvistrue(o))
  695. #if LJ_64 && !LJ_GC64
  696. #define tvislightud(o) (((int32_t)itype(o) >> 15) == -2)
  697. #else
  698. #define tvislightud(o) (itype(o) == LJ_TLIGHTUD)
  699. #endif
  700. #define tvisstr(o) (itype(o) == LJ_TSTR)
  701. #define tvisfunc(o) (itype(o) == LJ_TFUNC)
  702. #define tvisthread(o) (itype(o) == LJ_TTHREAD)
  703. #define tvisproto(o) (itype(o) == LJ_TPROTO)
  704. #define tviscdata(o) (itype(o) == LJ_TCDATA)
  705. #define tvistab(o) (itype(o) == LJ_TTAB)
  706. #define tvisudata(o) (itype(o) == LJ_TUDATA)
  707. #define tvisnumber(o) (itype(o) <= LJ_TISNUM)
  708. #define tvisint(o) (LJ_DUALNUM && itype(o) == LJ_TISNUM)
  709. #define tvisnum(o) (itype(o) < LJ_TISNUM)
  710. #define tvistruecond(o) (itype(o) < LJ_TISTRUECOND)
  711. #define tvispri(o) (itype(o) >= LJ_TISPRI)
  712. #define tvistabud(o) (itype(o) <= LJ_TISTABUD) /* && !tvisnum() */
  713. #define tvisgcv(o) ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV))
  714. /* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */
  715. #define tvisnan(o) ((o)->n != (o)->n)
  716. #if LJ_64
  717. #define tviszero(o) (((o)->u64 << 1) == 0)
  718. #else
  719. #define tviszero(o) (((o)->u32.lo | ((o)->u32.hi << 1)) == 0)
  720. #endif
  721. #define tvispzero(o) ((o)->u64 == 0)
  722. #define tvismzero(o) ((o)->u64 == U64x(80000000,00000000))
  723. #define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000))
  724. #define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64)
  725. /* Macros to convert type ids. */
  726. #if LJ_64 && !LJ_GC64
  727. #define itypemap(o) \
  728. (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o))
  729. #else
  730. #define itypemap(o) (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o))
  731. #endif
  732. /* Macros to get tagged values. */
  733. #if LJ_GC64
  734. #define gcval(o) ((GCobj *)(gcrefu((o)->gcr) & LJ_GCVMASK))
  735. #else
  736. #define gcval(o) (gcref((o)->gcr))
  737. #endif
  738. #define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - itype(o)))
  739. #if LJ_64
  740. #define lightudseg(u) \
  741. (((u) >> LJ_LIGHTUD_BITS_LO) & ((1 << LJ_LIGHTUD_BITS_SEG)-1))
  742. #define lightudlo(u) \
  743. ((u) & (((uint64_t)1 << LJ_LIGHTUD_BITS_LO) - 1))
  744. #define lightudup(p) \
  745. ((uint32_t)(((p) >> LJ_LIGHTUD_BITS_LO) << (LJ_LIGHTUD_BITS_LO-32)))
  746. static LJ_AINLINE void *lightudV(global_State *g, cTValue *o)
  747. {
  748. uint64_t u = o->u64;
  749. uint64_t seg = lightudseg(u);
  750. uint32_t *segmap = mref(g->gc.lightudseg, uint32_t);
  751. lj_assertG(tvislightud(o), "lightuserdata expected");
  752. if (seg == (1 << LJ_LIGHTUD_BITS_SEG)-1) return NULL;
  753. lj_assertG(seg <= g->gc.lightudnum, "bad lightuserdata segment %d", seg);
  754. return (void *)(((uint64_t)segmap[seg] << 32) | lightudlo(u));
  755. }
  756. #else
  757. #define lightudV(g, o) check_exp(tvislightud(o), gcrefp((o)->gcr, void))
  758. #endif
  759. #define gcV(o) check_exp(tvisgcv(o), gcval(o))
  760. #define strV(o) check_exp(tvisstr(o), &gcval(o)->str)
  761. #define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn)
  762. #define threadV(o) check_exp(tvisthread(o), &gcval(o)->th)
  763. #define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt)
  764. #define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd)
  765. #define tabV(o) check_exp(tvistab(o), &gcval(o)->tab)
  766. #define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud)
  767. #define numV(o) check_exp(tvisnum(o), (o)->n)
  768. #define intV(o) check_exp(tvisint(o), (int32_t)(o)->i)
  769. /* Macros to set tagged values. */
  770. #if LJ_GC64
  771. #define setitype(o, i) ((o)->it = ((i) << 15))
  772. #define setnilV(o) ((o)->it64 = -1)
  773. #define setpriV(o, x) ((o)->it64 = (int64_t)~((uint64_t)~(x)<<47))
  774. #define setboolV(o, x) ((o)->it64 = (int64_t)~((uint64_t)((x)+1)<<47))
  775. #else
  776. #define setitype(o, i) ((o)->it = (i))
  777. #define setnilV(o) ((o)->it = LJ_TNIL)
  778. #define setboolV(o, x) ((o)->it = LJ_TFALSE-(uint32_t)(x))
  779. #define setpriV(o, i) (setitype((o), (i)))
  780. #endif
  781. static LJ_AINLINE void setrawlightudV(TValue *o, void *p)
  782. {
  783. #if LJ_GC64
  784. o->u64 = (uint64_t)p | (((uint64_t)LJ_TLIGHTUD) << 47);
  785. #elif LJ_64
  786. o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48);
  787. #else
  788. setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD);
  789. #endif
  790. }
  791. #if LJ_FR2 || LJ_32
  792. #define contptr(f) ((void *)(f))
  793. #define setcont(o, f) ((o)->u64 = (uint64_t)(uintptr_t)contptr(f))
  794. #else
  795. #define contptr(f) \
  796. ((void *)(uintptr_t)(uint32_t)((intptr_t)(f) - (intptr_t)lj_vm_asm_begin))
  797. #define setcont(o, f) \
  798. ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin)
  799. #endif
  800. static LJ_AINLINE void checklivetv(lua_State *L, TValue *o, const char *msg)
  801. {
  802. UNUSED(L); UNUSED(o); UNUSED(msg);
  803. #if LUA_USE_ASSERT
  804. if (tvisgcv(o)) {
  805. lj_assertL(~itype(o) == gcval(o)->gch.gct,
  806. "mismatch of TValue type %d vs GC type %d",
  807. ~itype(o), gcval(o)->gch.gct);
  808. /* Copy of isdead check from lj_gc.h to avoid circular include. */
  809. lj_assertL(!(gcval(o)->gch.marked & (G(L)->gc.currentwhite ^ 3) & 3), msg);
  810. }
  811. #endif
  812. }
  813. static LJ_AINLINE void setgcVraw(TValue *o, GCobj *v, uint32_t itype)
  814. {
  815. #if LJ_GC64
  816. setgcreft(o->gcr, v, itype);
  817. #else
  818. setgcref(o->gcr, v); setitype(o, itype);
  819. #endif
  820. }
  821. static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t it)
  822. {
  823. setgcVraw(o, v, it);
  824. checklivetv(L, o, "store to dead GC object");
  825. }
  826. #define define_setV(name, type, tag) \
  827. static LJ_AINLINE void name(lua_State *L, TValue *o, const type *v) \
  828. { \
  829. setgcV(L, o, obj2gco(v), tag); \
  830. }
  831. define_setV(setstrV, GCstr, LJ_TSTR)
  832. define_setV(setthreadV, lua_State, LJ_TTHREAD)
  833. define_setV(setprotoV, GCproto, LJ_TPROTO)
  834. define_setV(setfuncV, GCfunc, LJ_TFUNC)
  835. define_setV(setcdataV, GCcdata, LJ_TCDATA)
  836. define_setV(settabV, GCtab, LJ_TTAB)
  837. define_setV(setudataV, GCudata, LJ_TUDATA)
  838. #define setnumV(o, x) ((o)->n = (x))
  839. #define setnanV(o) ((o)->u64 = U64x(fff80000,00000000))
  840. #define setpinfV(o) ((o)->u64 = U64x(7ff00000,00000000))
  841. #define setminfV(o) ((o)->u64 = U64x(fff00000,00000000))
  842. static LJ_AINLINE void setintV(TValue *o, int32_t i)
  843. {
  844. #if LJ_DUALNUM
  845. o->i = (uint32_t)i; setitype(o, LJ_TISNUM);
  846. #else
  847. o->n = (lua_Number)i;
  848. #endif
  849. }
  850. static LJ_AINLINE void setint64V(TValue *o, int64_t i)
  851. {
  852. if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i))
  853. setintV(o, (int32_t)i);
  854. else
  855. setnumV(o, (lua_Number)i);
  856. }
  857. #if LJ_64
  858. #define setintptrV(o, i) setint64V((o), (i))
  859. #else
  860. #define setintptrV(o, i) setintV((o), (i))
  861. #endif
  862. /* Copy tagged values. */
  863. static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2)
  864. {
  865. *o1 = *o2;
  866. checklivetv(L, o1, "copy of dead GC object");
  867. }
  868. /* -- Number to integer conversion ---------------------------------------- */
  869. #if LJ_SOFTFP
  870. LJ_ASMF int32_t lj_vm_tobit(double x);
  871. #if LJ_TARGET_MIPS64
  872. LJ_ASMF int32_t lj_vm_tointg(double x);
  873. #endif
  874. #endif
  875. static LJ_AINLINE int32_t lj_num2bit(lua_Number n)
  876. {
  877. #if LJ_SOFTFP
  878. return lj_vm_tobit(n);
  879. #else
  880. TValue o;
  881. o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */
  882. return (int32_t)o.u32.lo;
  883. #endif
  884. }
  885. #define lj_num2int(n) ((int32_t)(n))
  886. /*
  887. ** This must match the JIT backend behavior. In particular for archs
  888. ** that don't have a common hardware instruction for this conversion.
  889. ** Note that signed FP to unsigned int conversions have an undefined
  890. ** result and should never be relied upon in portable FFI code.
  891. ** See also: C99 or C11 standard, 6.3.1.4, footnote of (1).
  892. */
  893. static LJ_AINLINE uint64_t lj_num2u64(lua_Number n)
  894. {
  895. #if LJ_TARGET_X86ORX64 || LJ_TARGET_MIPS
  896. int64_t i = (int64_t)n;
  897. if (i < 0) i = (int64_t)(n - 18446744073709551616.0);
  898. return (uint64_t)i;
  899. #else
  900. return (uint64_t)n;
  901. #endif
  902. }
  903. static LJ_AINLINE int32_t numberVint(cTValue *o)
  904. {
  905. if (LJ_LIKELY(tvisint(o)))
  906. return intV(o);
  907. else
  908. return lj_num2int(numV(o));
  909. }
  910. static LJ_AINLINE lua_Number numberVnum(cTValue *o)
  911. {
  912. if (LJ_UNLIKELY(tvisint(o)))
  913. return (lua_Number)intV(o);
  914. else
  915. return numV(o);
  916. }
  917. /* -- Miscellaneous object handling --------------------------------------- */
  918. /* Names and maps for internal and external object tags. */
  919. LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1];
  920. LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1];
  921. #define lj_typename(o) (lj_obj_itypename[itypemap(o)])
  922. /* Compare two objects without calling metamethods. */
  923. LJ_FUNC int LJ_FASTCALL lj_obj_equal(cTValue *o1, cTValue *o2);
  924. LJ_FUNC const void * LJ_FASTCALL lj_obj_ptr(global_State *g, cTValue *o);
  925. #if LJ_ABI_PAUTH
  926. #if LJ_TARGET_ARM64
  927. #include <ptrauth.h>
  928. #define lj_ptr_sign(ptr, ctx) \
  929. ptrauth_sign_unauthenticated((ptr), ptrauth_key_function_pointer, (ctx))
  930. #define lj_ptr_strip(ptr) ptrauth_strip((ptr), ptrauth_key_function_pointer)
  931. #else
  932. #error "No support for pointer authentication for this architecture"
  933. #endif
  934. #else
  935. #define lj_ptr_sign(ptr, ctx) (ptr)
  936. #define lj_ptr_strip(ptr) (ptr)
  937. #endif
  938. #endif