lj_func.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** Function handling (prototypes, functions and upvalues).
  3. ** Copyright (C) 2005-2021 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. #define lj_func_c
  9. #define LUA_CORE
  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_func.h"
  13. #include "lj_trace.h"
  14. #include "lj_vm.h"
  15. /* -- Prototypes ---------------------------------------------------------- */
  16. void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt)
  17. {
  18. lj_mem_free(g, pt, pt->sizept);
  19. }
  20. /* -- Upvalues ------------------------------------------------------------ */
  21. static void unlinkuv(global_State *g, GCupval *uv)
  22. {
  23. UNUSED(g);
  24. lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv,
  25. "broken upvalue chain");
  26. setgcrefr(uvnext(uv)->prev, uv->prev);
  27. setgcrefr(uvprev(uv)->next, uv->next);
  28. }
  29. /* Find existing open upvalue for a stack slot or create a new one. */
  30. static GCupval *func_finduv(lua_State *L, TValue *slot)
  31. {
  32. global_State *g = G(L);
  33. GCRef *pp = &L->openupval;
  34. GCupval *p;
  35. GCupval *uv;
  36. /* Search the sorted list of open upvalues. */
  37. while (gcref(*pp) != NULL && uvval((p = gco2uv(gcref(*pp)))) >= slot) {
  38. lj_assertG(!p->closed && uvval(p) != &p->tv, "closed upvalue in chain");
  39. if (uvval(p) == slot) { /* Found open upvalue pointing to same slot? */
  40. if (isdead(g, obj2gco(p))) /* Resurrect it, if it's dead. */
  41. flipwhite(obj2gco(p));
  42. return p;
  43. }
  44. pp = &p->nextgc;
  45. }
  46. /* No matching upvalue found. Create a new one. */
  47. uv = lj_mem_newt(L, sizeof(GCupval), GCupval);
  48. newwhite(g, uv);
  49. uv->gct = ~LJ_TUPVAL;
  50. uv->closed = 0; /* Still open. */
  51. setmref(uv->v, slot); /* Pointing to the stack slot. */
  52. /* NOBARRIER: The GCupval is new (marked white) and open. */
  53. setgcrefr(uv->nextgc, *pp); /* Insert into sorted list of open upvalues. */
  54. setgcref(*pp, obj2gco(uv));
  55. setgcref(uv->prev, obj2gco(&g->uvhead)); /* Insert into GC list, too. */
  56. setgcrefr(uv->next, g->uvhead.next);
  57. setgcref(uvnext(uv)->prev, obj2gco(uv));
  58. setgcref(g->uvhead.next, obj2gco(uv));
  59. lj_assertG(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv,
  60. "broken upvalue chain");
  61. return uv;
  62. }
  63. /* Create an empty and closed upvalue. */
  64. static GCupval *func_emptyuv(lua_State *L)
  65. {
  66. GCupval *uv = (GCupval *)lj_mem_newgco(L, sizeof(GCupval));
  67. uv->gct = ~LJ_TUPVAL;
  68. uv->closed = 1;
  69. setnilV(&uv->tv);
  70. setmref(uv->v, &uv->tv);
  71. return uv;
  72. }
  73. /* Close all open upvalues pointing to some stack level or above. */
  74. void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level)
  75. {
  76. GCupval *uv;
  77. global_State *g = G(L);
  78. while (gcref(L->openupval) != NULL &&
  79. uvval((uv = gco2uv(gcref(L->openupval)))) >= level) {
  80. GCobj *o = obj2gco(uv);
  81. lj_assertG(!isblack(o), "bad black upvalue");
  82. lj_assertG(!uv->closed && uvval(uv) != &uv->tv, "closed upvalue in chain");
  83. setgcrefr(L->openupval, uv->nextgc); /* No longer in open list. */
  84. if (isdead(g, o)) {
  85. lj_func_freeuv(g, uv);
  86. } else {
  87. unlinkuv(g, uv);
  88. lj_gc_closeuv(g, uv);
  89. }
  90. }
  91. }
  92. void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv)
  93. {
  94. if (!uv->closed)
  95. unlinkuv(g, uv);
  96. lj_mem_freet(g, uv);
  97. }
  98. /* -- Functions (closures) ------------------------------------------------ */
  99. GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env)
  100. {
  101. GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeCfunc(nelems));
  102. fn->c.gct = ~LJ_TFUNC;
  103. fn->c.ffid = FF_C;
  104. fn->c.nupvalues = (uint8_t)nelems;
  105. /* NOBARRIER: The GCfunc is new (marked white). */
  106. setmref(fn->c.pc, &G(L)->bc_cfunc_ext);
  107. setgcref(fn->c.env, obj2gco(env));
  108. return fn;
  109. }
  110. static GCfunc *func_newL(lua_State *L, GCproto *pt, GCtab *env)
  111. {
  112. uint32_t count;
  113. GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeLfunc((MSize)pt->sizeuv));
  114. fn->l.gct = ~LJ_TFUNC;
  115. fn->l.ffid = FF_LUA;
  116. fn->l.nupvalues = 0; /* Set to zero until upvalues are initialized. */
  117. /* NOBARRIER: Really a setgcref. But the GCfunc is new (marked white). */
  118. setmref(fn->l.pc, proto_bc(pt));
  119. setgcref(fn->l.env, obj2gco(env));
  120. /* Saturating 3 bit counter (0..7) for created closures. */
  121. count = (uint32_t)pt->flags + PROTO_CLCOUNT;
  122. pt->flags = (uint8_t)(count - ((count >> PROTO_CLC_BITS) & PROTO_CLCOUNT));
  123. return fn;
  124. }
  125. /* Create a new Lua function with empty upvalues. */
  126. GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env)
  127. {
  128. GCfunc *fn = func_newL(L, pt, env);
  129. MSize i, nuv = pt->sizeuv;
  130. /* NOBARRIER: The GCfunc is new (marked white). */
  131. for (i = 0; i < nuv; i++) {
  132. GCupval *uv = func_emptyuv(L);
  133. int32_t v = proto_uv(pt)[i];
  134. uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1);
  135. uv->dhash = (uint32_t)(uintptr_t)pt ^ (v << 24);
  136. setgcref(fn->l.uvptr[i], obj2gco(uv));
  137. }
  138. fn->l.nupvalues = (uint8_t)nuv;
  139. return fn;
  140. }
  141. /* Do a GC check and create a new Lua function with inherited upvalues. */
  142. GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent)
  143. {
  144. GCfunc *fn;
  145. GCRef *puv;
  146. MSize i, nuv;
  147. TValue *base;
  148. lj_gc_check_fixtop(L);
  149. fn = func_newL(L, pt, tabref(parent->env));
  150. /* NOBARRIER: The GCfunc is new (marked white). */
  151. puv = parent->uvptr;
  152. nuv = pt->sizeuv;
  153. base = L->base;
  154. for (i = 0; i < nuv; i++) {
  155. uint32_t v = proto_uv(pt)[i];
  156. GCupval *uv;
  157. if ((v & PROTO_UV_LOCAL)) {
  158. uv = func_finduv(L, base + (v & 0xff));
  159. uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1);
  160. uv->dhash = (uint32_t)(uintptr_t)mref(parent->pc, char) ^ (v << 24);
  161. } else {
  162. uv = &gcref(puv[v])->uv;
  163. }
  164. setgcref(fn->l.uvptr[i], obj2gco(uv));
  165. }
  166. fn->l.nupvalues = (uint8_t)nuv;
  167. return fn;
  168. }
  169. void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *fn)
  170. {
  171. MSize size = isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) :
  172. sizeCfunc((MSize)fn->c.nupvalues);
  173. lj_mem_free(g, fn, size);
  174. }