src/lj_parse.c - luajit-2.0-src

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** Lua parser (source code -> bytecode).
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major 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_parse_c
  9. #define LUA_CORE

  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_debug.h"
  14. #include "lj_buf.h"
  15. #include "lj_str.h"
  16. #include "lj_tab.h"
  17. #include "lj_func.h"
  18. #include "lj_state.h"
  19. #include "lj_bc.h"
  20. #if LJ_HASFFI
  21. #include "lj_ctype.h"
  22. #endif
  23. #include "lj_strfmt.h"
  24. #include "lj_lex.h"
  25. #include "lj_parse.h"
  26. #include "lj_vm.h"
  27. #include "lj_vmevent.h"

  28. /* -- Parser structures and definitions ----------------------------------- */

  29. /* Expression kinds. */
  30. typedef enum {
  31.   /* Constant expressions must be first and in this order: */
  32.   VKNIL,
  33.   VKFALSE,
  34.   VKTRUE,
  35.   VKSTR,        /* sval = string value */
  36.   VKNUM,        /* nval = number value */
  37.   VKLAST = VKNUM,
  38.   VKCDATA,        /* nval = cdata value, not treated as a constant expression */
  39.   /* Non-constant expressions follow: */
  40.   VLOCAL,        /* info = local register, aux = vstack index */
  41.   VUPVAL,        /* info = upvalue index, aux = vstack index */
  42.   VGLOBAL,        /* sval = string value */
  43.   VINDEXED,        /* info = table register, aux = index reg/byte/string const */
  44.   VJMP,                /* info = instruction PC */
  45.   VRELOCABLE,        /* info = instruction PC */
  46.   VNONRELOC,        /* info = result register */
  47.   VCALL,        /* info = instruction PC, aux = base */
  48.   VVOID
  49. } ExpKind;

  50. /* Expression descriptor. */
  51. typedef struct ExpDesc {
  52.   union {
  53.     struct {
  54.       uint32_t info;        /* Primary info. */
  55.       uint32_t aux;        /* Secondary info. */
  56.     } s;
  57.     TValue nval;        /* Number value. */
  58.     GCstr *sval;        /* String value. */
  59.   } u;
  60.   ExpKind k;
  61.   BCPos t;                /* True condition jump list. */
  62.   BCPos f;                /* False condition jump list. */
  63. } ExpDesc;

  64. /* Macros for expressions. */
  65. #define expr_hasjump(e)                ((e)->t != (e)->f)

  66. #define expr_isk(e)                ((e)->k <= VKLAST)
  67. #define expr_isk_nojump(e)        (expr_isk(e) && !expr_hasjump(e))
  68. #define expr_isnumk(e)                ((e)->k == VKNUM)
  69. #define expr_isnumk_nojump(e)        (expr_isnumk(e) && !expr_hasjump(e))
  70. #define expr_isstrk(e)                ((e)->k == VKSTR)

  71. #define expr_numtv(e)                check_exp(expr_isnumk((e)), &(e)->u.nval)
  72. #define expr_numberV(e)                numberVnum(expr_numtv((e)))

  73. /* Initialize expression. */
  74. static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info)
  75. {
  76.   e->k = k;
  77.   e->u.s.info = info;
  78.   e->f = e->t = NO_JMP;
  79. }

  80. /* Check number constant for +-0. */
  81. static int expr_numiszero(ExpDesc *e)
  82. {
  83.   TValue *o = expr_numtv(e);
  84.   return tvisint(o) ? (intV(o) == 0) : tviszero(o);
  85. }

  86. /* Per-function linked list of scope blocks. */
  87. typedef struct FuncScope {
  88.   struct FuncScope *prev;        /* Link to outer scope. */
  89.   MSize vstart;                        /* Start of block-local variables. */
  90.   uint8_t nactvar;                /* Number of active vars outside the scope. */
  91.   uint8_t flags;                /* Scope flags. */
  92. } FuncScope;

  93. #define FSCOPE_LOOP                0x01        /* Scope is a (breakable) loop. */
  94. #define FSCOPE_BREAK                0x02        /* Break used in scope. */
  95. #define FSCOPE_GOLA                0x04        /* Goto or label used in scope. */
  96. #define FSCOPE_UPVAL                0x08        /* Upvalue in scope. */
  97. #define FSCOPE_NOCLOSE                0x10        /* Do not close upvalues. */

  98. #define NAME_BREAK                ((GCstr *)(uintptr_t)1)

  99. /* Index into variable stack. */
  100. typedef uint16_t VarIndex;
  101. #define LJ_MAX_VSTACK                (65536 - LJ_MAX_UPVAL)

  102. /* Variable/goto/label info. */
  103. #define VSTACK_VAR_RW                0x01        /* R/W variable. */
  104. #define VSTACK_GOTO                0x02        /* Pending goto. */
  105. #define VSTACK_LABEL                0x04        /* Label. */

  106. /* Per-function state. */
  107. typedef struct FuncState {
  108.   GCtab *kt;                        /* Hash table for constants. */
  109.   LexState *ls;                        /* Lexer state. */
  110.   lua_State *L;                        /* Lua state. */
  111.   FuncScope *bl;                /* Current scope. */
  112.   struct FuncState *prev;        /* Enclosing function. */
  113.   BCPos pc;                        /* Next bytecode position. */
  114.   BCPos lasttarget;                /* Bytecode position of last jump target. */
  115.   BCPos jpc;                        /* Pending jump list to next bytecode. */
  116.   BCReg freereg;                /* First free register. */
  117.   BCReg nactvar;                /* Number of active local variables. */
  118.   BCReg nkn, nkgc;                /* Number of lua_Number/GCobj constants */
  119.   BCLine linedefined;                /* First line of the function definition. */
  120.   BCInsLine *bcbase;                /* Base of bytecode stack. */
  121.   BCPos bclim;                        /* Limit of bytecode stack. */
  122.   MSize vbase;                        /* Base of variable stack for this function. */
  123.   uint8_t flags;                /* Prototype flags. */
  124.   uint8_t numparams;                /* Number of parameters. */
  125.   uint8_t framesize;                /* Fixed frame size. */
  126.   uint8_t nuv;                        /* Number of upvalues */
  127.   VarIndex varmap[LJ_MAX_LOCVAR];  /* Map from register to variable idx. */
  128.   VarIndex uvmap[LJ_MAX_UPVAL];        /* Map from upvalue to variable idx. */
  129.   VarIndex uvtmp[LJ_MAX_UPVAL];        /* Temporary upvalue map. */
  130. } FuncState;

  131. /* Binary and unary operators. ORDER OPR */
  132. typedef enum BinOpr {
  133.   OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,  /* ORDER ARITH */
  134.   OPR_CONCAT,
  135.   OPR_NE, OPR_EQ,
  136.   OPR_LT, OPR_GE, OPR_LE, OPR_GT,
  137.   OPR_AND, OPR_OR,
  138.   OPR_NOBINOPR
  139. } BinOpr;

  140. LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT);
  141. LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT);
  142. LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT);
  143. LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD);
  144. LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);
  145. LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);
  146. LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);

  147. /* -- Error handling ------------------------------------------------------ */

  148. LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)
  149. {
  150.   lj_lex_error(ls, ls->tok, em);
  151. }

  152. LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken tok)
  153. {
  154.   lj_lex_error(ls, ls->tok, LJ_ERR_XTOKEN, lj_lex_token2str(ls, tok));
  155. }

  156. LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)
  157. {
  158.   if (fs->linedefined == 0)
  159.     lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what);
  160.   else
  161.     lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what);
  162. }

  163. #define checklimit(fs, v, l, m)                if ((v) >= (l)) err_limit(fs, l, m)
  164. #define checklimitgt(fs, v, l, m)        if ((v) > (l)) err_limit(fs, l, m)
  165. #define checkcond(ls, c, em)                { if (!(c)) err_syntax(ls, em); }

  166. /* -- Management of constants --------------------------------------------- */

  167. /* Return bytecode encoding for primitive constant. */
  168. #define const_pri(e)                check_exp((e)->k <= VKTRUE, (e)->k)

  169. #define tvhaskslot(o)        ((o)->u32.hi == 0)
  170. #define tvkslot(o)        ((o)->u32.lo)

  171. /* Add a number constant. */
  172. static BCReg const_num(FuncState *fs, ExpDesc *e)
  173. {
  174.   lua_State *L = fs->L;
  175.   TValue *o;
  176.   lua_assert(expr_isnumk(e));
  177.   o = lj_tab_set(L, fs->kt, &e->u.nval);
  178.   if (tvhaskslot(o))
  179.     return tvkslot(o);
  180.   o->u64 = fs->nkn;
  181.   return fs->nkn++;
  182. }

  183. /* Add a GC object constant. */
  184. static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)
  185. {
  186.   lua_State *L = fs->L;
  187.   TValue key, *o;
  188.   setgcV(L, &key, gc, itype);
  189.   /* NOBARRIER: the key is new or kept alive. */
  190.   o = lj_tab_set(L, fs->kt, &key);
  191.   if (tvhaskslot(o))
  192.     return tvkslot(o);
  193.   o->u64 = fs->nkgc;
  194.   return fs->nkgc++;
  195. }

  196. /* Add a string constant. */
  197. static BCReg const_str(FuncState *fs, ExpDesc *e)
  198. {
  199.   lua_assert(expr_isstrk(e) || e->k == VGLOBAL);
  200.   return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);
  201. }

  202. /* Anchor string constant to avoid GC. */
  203. GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len)
  204. {
  205.   /* NOBARRIER: the key is new or kept alive. */
  206.   lua_State *L = ls->L;
  207.   GCstr *s = lj_str_new(L, str, len);
  208.   TValue *tv = lj_tab_setstr(L, ls->fs->kt, s);
  209.   if (tvisnil(tv)) setboolV(tv, 1);
  210.   lj_gc_check(L);
  211.   return s;
  212. }

  213. #if LJ_HASFFI
  214. /* Anchor cdata to avoid GC. */
  215. void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd)
  216. {
  217.   /* NOBARRIER: the key is new or kept alive. */
  218.   lua_State *L = ls->L;
  219.   setcdataV(L, tv, cd);
  220.   setboolV(lj_tab_set(L, ls->fs->kt, tv), 1);
  221. }
  222. #endif

  223. /* -- Jump list handling -------------------------------------------------- */

  224. /* Get next element in jump list. */
  225. static BCPos jmp_next(FuncState *fs, BCPos pc)
  226. {
  227.   ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);
  228.   if ((BCPos)delta == NO_JMP)
  229.     return NO_JMP;
  230.   else
  231.     return (BCPos)(((ptrdiff_t)pc+1)+delta);
  232. }

  233. /* Check if any of the instructions on the jump list produce no value. */
  234. static int jmp_novalue(FuncState *fs, BCPos list)
  235. {
  236.   for (; list != NO_JMP; list = jmp_next(fs, list)) {
  237.     BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins;
  238.     if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG))
  239.       return 1;
  240.   }
  241.   return 0;
  242. }

  243. /* Patch register of test instructions. */
  244. static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg)
  245. {
  246.   BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc];
  247.   BCOp op = bc_op(ilp->ins);
  248.   if (op == BC_ISTC || op == BC_ISFC) {
  249.     if (reg != NO_REG && reg != bc_d(ilp->ins)) {
  250.       setbc_a(&ilp->ins, reg);
  251.     } else/* Nothing to store or already in the right register. */
  252.       setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC));
  253.       setbc_a(&ilp->ins, 0);
  254.     }
  255.   } else if (bc_a(ilp->ins) == NO_REG) {
  256.     if (reg == NO_REG) {
  257.       ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0);
  258.     } else {
  259.       setbc_a(&ilp->ins, reg);
  260.       if (reg >= bc_a(ilp[1].ins))
  261.         setbc_a(&ilp[1].ins, reg+1);
  262.     }
  263.   } else {
  264.     return 0/* Cannot patch other instructions. */
  265.   }
  266.   return 1;
  267. }

  268. /* Drop values for all instructions on jump list. */
  269. static void jmp_dropval(FuncState *fs, BCPos list)
  270. {
  271.   for (; list != NO_JMP; list = jmp_next(fs, list))
  272.     jmp_patchtestreg(fs, list, NO_REG);
  273. }

  274. /* Patch jump instruction to target. */
  275. static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)
  276. {
  277.   BCIns *jmp = &fs->bcbase[pc].ins;
  278.   BCPos offset = dest-(pc+1)+BCBIAS_J;
  279.   lua_assert(dest != NO_JMP);
  280.   if (offset > BCMAX_D)
  281.     err_syntax(fs->ls, LJ_ERR_XJUMP);
  282.   setbc_d(jmp, offset);
  283. }

  284. /* Append to jump list. */
  285. static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)
  286. {
  287.   if (l2 == NO_JMP) {
  288.     return;
  289.   } else if (*l1 == NO_JMP) {
  290.     *l1 = l2;
  291.   } else {
  292.     BCPos list = *l1;
  293.     BCPos next;
  294.     while ((next = jmp_next(fs, list)) != NO_JMP/* Find last element. */
  295.       list = next;
  296.     jmp_patchins(fs, list, l2);
  297.   }
  298. }

  299. /* Patch jump list and preserve produced values. */
  300. static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,
  301.                          BCReg reg, BCPos dtarget)
  302. {
  303.   while (list != NO_JMP) {
  304.     BCPos next = jmp_next(fs, list);
  305.     if (jmp_patchtestreg(fs, list, reg))
  306.       jmp_patchins(fs, list, vtarget);  /* Jump to target with value. */
  307.     else
  308.       jmp_patchins(fs, list, dtarget);  /* Jump to default target. */
  309.     list = next;
  310.   }
  311. }

  312. /* Jump to following instruction. Append to list of pending jumps. */
  313. static void jmp_tohere(FuncState *fs, BCPos list)
  314. {
  315.   fs->lasttarget = fs->pc;
  316.   jmp_append(fs, &fs->jpc, list);
  317. }

  318. /* Patch jump list to target. */
  319. static void jmp_patch(FuncState *fs, BCPos list, BCPos target)
  320. {
  321.   if (target == fs->pc) {
  322.     jmp_tohere(fs, list);
  323.   } else {
  324.     lua_assert(target < fs->pc);
  325.     jmp_patchval(fs, list, target, NO_REG, target);
  326.   }
  327. }

  328. /* -- Bytecode register allocator ----------------------------------------- */

  329. /* Bump frame size. */
  330. static void bcreg_bump(FuncState *fs, BCReg n)
  331. {
  332.   BCReg sz = fs->freereg + n;
  333.   if (sz > fs->framesize) {
  334.     if (sz >= LJ_MAX_SLOTS)
  335.       err_syntax(fs->ls, LJ_ERR_XSLOTS);
  336.     fs->framesize = (uint8_t)sz;
  337.   }
  338. }

  339. /* Reserve registers. */
  340. static void bcreg_reserve(FuncState *fs, BCReg n)
  341. {
  342.   bcreg_bump(fs, n);
  343.   fs->freereg += n;
  344. }

  345. /* Free register. */
  346. static void bcreg_free(FuncState *fs, BCReg reg)
  347. {
  348.   if (reg >= fs->nactvar) {
  349.     fs->freereg--;
  350.     lua_assert(reg == fs->freereg);
  351.   }
  352. }

  353. /* Free register for expression. */
  354. static void expr_free(FuncState *fs, ExpDesc *e)
  355. {
  356.   if (e->k == VNONRELOC)
  357.     bcreg_free(fs, e->u.s.info);
  358. }

  359. /* -- Bytecode emitter ---------------------------------------------------- */

  360. /* Emit bytecode instruction. */
  361. static BCPos bcemit_INS(FuncState *fs, BCIns ins)
  362. {
  363.   BCPos pc = fs->pc;
  364.   LexState *ls = fs->ls;
  365.   jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);
  366.   fs->jpc = NO_JMP;
  367.   if (LJ_UNLIKELY(pc >= fs->bclim)) {
  368.     ptrdiff_t base = fs->bcbase - ls->bcstack;
  369.     checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");
  370.     lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);
  371.     fs->bclim = (BCPos)(ls->sizebcstack - base);
  372.     fs->bcbase = ls->bcstack + base;
  373.   }
  374.   fs->bcbase[pc].ins = ins;
  375.   fs->bcbase[pc].line = ls->lastline;
  376.   fs->pc = pc+1;
  377.   return pc;
  378. }

  379. #define bcemit_ABC(fs, o, a, b, c)        bcemit_INS(fs, BCINS_ABC(o, a, b, c))
  380. #define bcemit_AD(fs, o, a, d)                bcemit_INS(fs, BCINS_AD(o, a, d))
  381. #define bcemit_AJ(fs, o, a, j)                bcemit_INS(fs, BCINS_AJ(o, a, j))

  382. #define bcptr(fs, e)                        (&(fs)->bcbase[(e)->u.s.info].ins)

  383. /* -- Bytecode emitter for expressions ------------------------------------ */

  384. /* Discharge non-constant expression to any register. */
  385. static void expr_discharge(FuncState *fs, ExpDesc *e)
  386. {
  387.   BCIns ins;
  388.   if (e->k == VUPVAL) {
  389.     ins = BCINS_AD(BC_UGET, 0, e->u.s.info);
  390.   } else if (e->k == VGLOBAL) {
  391.     ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));
  392.   } else if (e->k == VINDEXED) {
  393.     BCReg rc = e->u.s.aux;
  394.     if ((int32_t)rc < 0) {
  395.       ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);
  396.     } else if (rc > BCMAX_C) {
  397.       ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));
  398.     } else {
  399.       bcreg_free(fs, rc);
  400.       ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);
  401.     }
  402.     bcreg_free(fs, e->u.s.info);
  403.   } else if (e->k == VCALL) {
  404.     e->u.s.info = e->u.s.aux;
  405.     e->k = VNONRELOC;
  406.     return;
  407.   } else if (e->k == VLOCAL) {
  408.     e->k = VNONRELOC;
  409.     return;
  410.   } else {
  411.     return;
  412.   }
  413.   e->u.s.info = bcemit_INS(fs, ins);
  414.   e->k = VRELOCABLE;
  415. }

  416. /* Emit bytecode to set a range of registers to nil. */
  417. static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)
  418. {
  419.   if (fs->pc > fs->lasttarget) {  /* No jumps to current position? */
  420.     BCIns *ip = &fs->bcbase[fs->pc-1].ins;
  421.     BCReg pto, pfrom = bc_a(*ip);
  422.     switch (bc_op(*ip)) {  /* Try to merge with the previous instruction. */
  423.     case BC_KPRI:
  424.       if (bc_d(*ip) != ~LJ_TNIL) break;
  425.       if (from == pfrom) {
  426.         if (n == 1) return;
  427.       } else if (from == pfrom+1) {
  428.         from = pfrom;
  429.         n++;
  430.       } else {
  431.         break;
  432.       }
  433.       *ip = BCINS_AD(BC_KNIL, from, from+n-1);  /* Replace KPRI. */
  434.       return;
  435.     case BC_KNIL:
  436.       pto = bc_d(*ip);
  437.       if (pfrom <= from && from <= pto+1) {  /* Can we connect both ranges? */
  438.         if (from+n-1 > pto)
  439.           setbc_d(ip, from+n-1);  /* Patch previous instruction range. */
  440.         return;
  441.       }
  442.       break;
  443.     default:
  444.       break;
  445.     }
  446.   }
  447.   /* Emit new instruction or replace old instruction. */
  448.   bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :
  449.                           BCINS_AD(BC_KNIL, from, from+n-1));
  450. }

  451. /* Discharge an expression to a specific register. Ignore branches. */
  452. static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)
  453. {
  454.   BCIns ins;
  455.   expr_discharge(fs, e);
  456.   if (e->k == VKSTR) {
  457.     ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));
  458.   } else if (e->k == VKNUM) {
  459. #if LJ_DUALNUM
  460.     cTValue *tv = expr_numtv(e);
  461.     if (tvisint(tv) && checki16(intV(tv)))
  462.       ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv));
  463.     else
  464. #else
  465.     lua_Number n = expr_numberV(e);
  466.     int32_t k = lj_num2int(n);
  467.     if (checki16(k) && n == (lua_Number)k)
  468.       ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);
  469.     else
  470. #endif
  471.       ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));
  472. #if LJ_HASFFI
  473.   } else if (e->k == VKCDATA) {
  474.     fs->flags |= PROTO_FFI;
  475.     ins = BCINS_AD(BC_KCDATA, reg,
  476.                    const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));
  477. #endif
  478.   } else if (e->k == VRELOCABLE) {
  479.     setbc_a(bcptr(fs, e), reg);
  480.     goto noins;
  481.   } else if (e->k == VNONRELOC) {
  482.     if (reg == e->u.s.info)
  483.       goto noins;
  484.     ins = BCINS_AD(BC_MOV, reg, e->u.s.info);
  485.   } else if (e->k == VKNIL) {
  486.     bcemit_nil(fs, reg, 1);
  487.     goto noins;
  488.   } else if (e->k <= VKTRUE) {
  489.     ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
  490.   } else {
  491.     lua_assert(e->k == VVOID || e->k == VJMP);
  492.     return;
  493.   }
  494.   bcemit_INS(fs, ins);
  495. noins:
  496.   e->u.s.info = reg;
  497.   e->k = VNONRELOC;
  498. }

  499. /* Forward declaration. */
  500. static BCPos bcemit_jmp(FuncState *fs);

  501. /* Discharge an expression to a specific register. */
  502. static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg)
  503. {
  504.   expr_toreg_nobranch(fs, e, reg);
  505.   if (e->k == VJMP)
  506.     jmp_append(fs, &e->t, e->u.s.info);  /* Add it to the true jump list. */
  507.   if (expr_hasjump(e)) {  /* Discharge expression with branches. */
  508.     BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;
  509.     if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {
  510.       BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);
  511.       jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);
  512.       bcemit_AJ(fs, BC_JMP, fs->freereg, 1);
  513.       jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);
  514.       jmp_tohere(fs, jval);
  515.     }
  516.     jend = fs->pc;
  517.     fs->lasttarget = jend;
  518.     jmp_patchval(fs, e->f, jend, reg, jfalse);
  519.     jmp_patchval(fs, e->t, jend, reg, jtrue);
  520.   }
  521.   e->f = e->t = NO_JMP;
  522.   e->u.s.info = reg;
  523.   e->k = VNONRELOC;
  524. }

  525. /* Discharge an expression to the next free register. */
  526. static void expr_tonextreg(FuncState *fs, ExpDesc *e)
  527. {
  528.   expr_discharge(fs, e);
  529.   expr_free(fs, e);
  530.   bcreg_reserve(fs, 1);
  531.   expr_toreg(fs, e, fs->freereg - 1);
  532. }

  533. /* Discharge an expression to any register. */
  534. static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)
  535. {
  536.   expr_discharge(fs, e);
  537.   if (e->k == VNONRELOC) {
  538.     if (!expr_hasjump(e)) return e->u.s.info;  /* Already in a register. */
  539.     if (e->u.s.info >= fs->nactvar) {
  540.       expr_toreg(fs, e, e->u.s.info);  /* Discharge to temp. register. */
  541.       return e->u.s.info;
  542.     }
  543.   }
  544.   expr_tonextreg(fs, e);  /* Discharge to next register. */
  545.   return e->u.s.info;
  546. }

  547. /* Partially discharge expression to a value. */
  548. static void expr_toval(FuncState *fs, ExpDesc *e)
  549. {
  550.   if (expr_hasjump(e))
  551.     expr_toanyreg(fs, e);
  552.   else
  553.     expr_discharge(fs, e);
  554. }

  555. /* Emit store for LHS expression. */
  556. static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
  557. {
  558.   BCIns ins;
  559.   if (var->k == VLOCAL) {
  560.     fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
  561.     expr_free(fs, e);
  562.     expr_toreg(fs, e, var->u.s.info);
  563.     return;
  564.   } else if (var->k == VUPVAL) {
  565.     fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
  566.     expr_toval(fs, e);
  567.     if (e->k <= VKTRUE)
  568.       ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
  569.     else if (e->k == VKSTR)
  570.       ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));
  571.     else if (e->k == VKNUM)
  572.       ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));
  573.     else
  574.       ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));
  575.   } else if (var->k == VGLOBAL) {
  576.     BCReg ra = expr_toanyreg(fs, e);
  577.     ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
  578.   } else {
  579.     BCReg ra, rc;
  580.     lua_assert(var->k == VINDEXED);
  581.     ra = expr_toanyreg(fs, e);
  582.     rc = var->u.s.aux;
  583.     if ((int32_t)rc < 0) {
  584.       ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);
  585.     } else if (rc > BCMAX_C) {
  586.       ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
  587.     } else {
  588.       /* Free late alloced key reg to avoid assert on free of value reg. */
  589.       /* This can only happen when called from expr_table(). */
  590.       lua_assert(e->k != VNONRELOC || ra < fs->nactvar ||
  591.                  rc < ra || (bcreg_free(fs, rc),1));
  592.       ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);
  593.     }
  594.   }
  595.   bcemit_INS(fs, ins);
  596.   expr_free(fs, e);
  597. }

  598. /* Emit method lookup expression. */
  599. static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
  600. {
  601.   BCReg idx, func, obj = expr_toanyreg(fs, e);
  602.   expr_free(fs, e);
  603.   func = fs->freereg;
  604.   bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj);  /* Copy object to 1st argument. */
  605.   lua_assert(expr_isstrk(key));
  606.   idx = const_str(fs, key);
  607.   if (idx <= BCMAX_C) {
  608.     bcreg_reserve(fs, 2+LJ_FR2);
  609.     bcemit_ABC(fs, BC_TGETS, func, obj, idx);
  610.   } else {
  611.     bcreg_reserve(fs, 3+LJ_FR2);
  612.     bcemit_AD(fs, BC_KSTR, func+2+LJ_FR2, idx);
  613.     bcemit_ABC(fs, BC_TGETV, func, obj, func+2+LJ_FR2);
  614.     fs->freereg--;
  615.   }
  616.   e->u.s.info = func;
  617.   e->k = VNONRELOC;
  618. }

  619. /* -- Bytecode emitter for branches --------------------------------------- */

  620. /* Emit unconditional branch. */
  621. static BCPos bcemit_jmp(FuncState *fs)
  622. {
  623.   BCPos jpc = fs->jpc;
  624.   BCPos j = fs->pc - 1;
  625.   BCIns *ip = &fs->bcbase[j].ins;
  626.   fs->jpc = NO_JMP;
  627.   if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) {
  628.     setbc_j(ip, NO_JMP);
  629.     fs->lasttarget = j+1;
  630.   } else {
  631.     j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP);
  632.   }
  633.   jmp_append(fs, &j, jpc);
  634.   return j;
  635. }

  636. /* Invert branch condition of bytecode instruction. */
  637. static void invertcond(FuncState *fs, ExpDesc *e)
  638. {
  639.   BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins;
  640.   setbc_op(ip, bc_op(*ip)^1);
  641. }

  642. /* Emit conditional branch. */
  643. static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)
  644. {
  645.   BCPos pc;
  646.   if (e->k == VRELOCABLE) {
  647.     BCIns *ip = bcptr(fs, e);
  648.     if (bc_op(*ip) == BC_NOT) {
  649.       *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));
  650.       return bcemit_jmp(fs);
  651.     }
  652.   }
  653.   if (e->k != VNONRELOC) {
  654.     bcreg_reserve(fs, 1);
  655.     expr_toreg_nobranch(fs, e, fs->freereg-1);
  656.   }
  657.   bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);
  658.   pc = bcemit_jmp(fs);
  659.   expr_free(fs, e);
  660.   return pc;
  661. }

  662. /* Emit branch on true condition. */
  663. static void bcemit_branch_t(FuncState *fs, ExpDesc *e)
  664. {
  665.   BCPos pc;
  666.   expr_discharge(fs, e);
  667.   if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
  668.     pc = NO_JMP/* Never jump. */
  669.   else if (e->k == VJMP)
  670.     invertcond(fs, e), pc = e->u.s.info;
  671.   else if (e->k == VKFALSE || e->k == VKNIL)
  672.     expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
  673.   else
  674.     pc = bcemit_branch(fs, e, 0);
  675.   jmp_append(fs, &e->f, pc);
  676.   jmp_tohere(fs, e->t);
  677.   e->t = NO_JMP;
  678. }

  679. /* Emit branch on false condition. */
  680. static void bcemit_branch_f(FuncState *fs, ExpDesc *e)
  681. {
  682.   BCPos pc;
  683.   expr_discharge(fs, e);
  684.   if (e->k == VKNIL || e->k == VKFALSE)
  685.     pc = NO_JMP/* Never jump. */
  686.   else if (e->k == VJMP)
  687.     pc = e->u.s.info;
  688.   else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
  689.     expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
  690.   else
  691.     pc = bcemit_branch(fs, e, 1);
  692.   jmp_append(fs, &e->t, pc);
  693.   jmp_tohere(fs, e->f);
  694.   e->f = NO_JMP;
  695. }

  696. /* -- Bytecode emitter for operators -------------------------------------- */

  697. /* Try constant-folding of arithmetic operators. */
  698. static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)
  699. {
  700.   TValue o;
  701.   lua_Number n;
  702.   if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;
  703.   n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);
  704.   setnumV(&o, n);
  705.   if (tvisnan(&o) || tvismzero(&o)) return 0/* Avoid NaN and -0 as consts. */
  706.   if (LJ_DUALNUM) {
  707.     int32_t k = lj_num2int(n);
  708.     if ((lua_Number)k == n) {
  709.       setintV(&e1->u.nval, k);
  710.       return 1;
  711.     }
  712.   }
  713.   setnumV(&e1->u.nval, n);
  714.   return 1;
  715. }

  716. /* Emit arithmetic operator. */
  717. static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
  718. {
  719.   BCReg rb, rc, t;
  720.   uint32_t op;
  721.   if (foldarith(opr, e1, e2))
  722.     return;
  723.   if (opr == OPR_POW) {
  724.     op = BC_POW;
  725.     rc = expr_toanyreg(fs, e2);
  726.     rb = expr_toanyreg(fs, e1);
  727.   } else {
  728.     op = opr-OPR_ADD+BC_ADDVV;
  729.     /* Must discharge 2nd operand first since VINDEXED might free regs. */
  730.     expr_toval(fs, e2);
  731.     if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)
  732.       op -= BC_ADDVV-BC_ADDVN;
  733.     else
  734.       rc = expr_toanyreg(fs, e2);
  735.     /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
  736.     lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC);
  737.     expr_toval(fs, e1);
  738.     /* Avoid two consts to satisfy bytecode constraints. */
  739.     if (expr_isnumk(e1) && !expr_isnumk(e2) &&
  740.         (t = const_num(fs, e1)) <= BCMAX_B) {
  741.       rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;
  742.     } else {
  743.       rb = expr_toanyreg(fs, e1);
  744.     }
  745.   }
  746.   /* Using expr_free might cause asserts if the order is wrong. */
  747.   if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
  748.   if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
  749.   e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);
  750.   e1->k = VRELOCABLE;
  751. }

  752. /* Emit comparison operator. */
  753. static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
  754. {
  755.   ExpDesc *eret = e1;
  756.   BCIns ins;
  757.   expr_toval(fs, e1);
  758.   if (opr == OPR_EQ || opr == OPR_NE) {
  759.     BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;
  760.     BCReg ra;
  761.     if (expr_isk(e1)) { e1 = e2; e2 = eret; }  /* Need constant in 2nd arg. */
  762.     ra = expr_toanyreg(fs, e1);  /* First arg must be in a reg. */
  763.     expr_toval(fs, e2);
  764.     switch (e2->k) {
  765.     case VKNIL: case VKFALSE: case VKTRUE:
  766.       ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));
  767.       break;
  768.     case VKSTR:
  769.       ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));
  770.       break;
  771.     case VKNUM:
  772.       ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));
  773.       break;
  774.     default:
  775.       ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));
  776.       break;
  777.     }
  778.   } else {
  779.     uint32_t op = opr-OPR_LT+BC_ISLT;
  780.     BCReg ra, rd;
  781.     if ((op-BC_ISLT) & 1) {  /* GT -> LT, GE -> LE */
  782.       e1 = e2; e2 = eret;  /* Swap operands. */
  783.       op = ((op-BC_ISLT)^3)+BC_ISLT;
  784.       expr_toval(fs, e1);
  785.     }
  786.     rd = expr_toanyreg(fs, e2);
  787.     ra = expr_toanyreg(fs, e1);
  788.     ins = BCINS_AD(op, ra, rd);
  789.   }
  790.   /* Using expr_free might cause asserts if the order is wrong. */
  791.   if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
  792.   if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
  793.   bcemit_INS(fs, ins);
  794.   eret->u.s.info = bcemit_jmp(fs);
  795.   eret->k = VJMP;
  796. }

  797. /* Fixup left side of binary operator. */
  798. static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)
  799. {
  800.   if (op == OPR_AND) {
  801.     bcemit_branch_t(fs, e);
  802.   } else if (op == OPR_OR) {
  803.     bcemit_branch_f(fs, e);
  804.   } else if (op == OPR_CONCAT) {
  805.     expr_tonextreg(fs, e);
  806.   } else if (op == OPR_EQ || op == OPR_NE) {
  807.     if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);
  808.   } else {
  809.     if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);
  810.   }
  811. }

  812. /* Emit binary operator. */
  813. static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
  814. {
  815.   if (op <= OPR_POW) {
  816.     bcemit_arith(fs, op, e1, e2);
  817.   } else if (op == OPR_AND) {
  818.     lua_assert(e1->t == NO_JMP);  /* List must be closed. */
  819.     expr_discharge(fs, e2);
  820.     jmp_append(fs, &e2->f, e1->f);
  821.     *e1 = *e2;
  822.   } else if (op == OPR_OR) {
  823.     lua_assert(e1->f == NO_JMP);  /* List must be closed. */
  824.     expr_discharge(fs, e2);
  825.     jmp_append(fs, &e2->t, e1->t);
  826.     *e1 = *e2;
  827.   } else if (op == OPR_CONCAT) {
  828.     expr_toval(fs, e2);
  829.     if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
  830.       lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1);
  831.       expr_free(fs, e1);
  832.       setbc_b(bcptr(fs, e2), e1->u.s.info);
  833.       e1->u.s.info = e2->u.s.info;
  834.     } else {
  835.       expr_tonextreg(fs, e2);
  836.       expr_free(fs, e2);
  837.       expr_free(fs, e1);
  838.       e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);
  839.     }
  840.     e1->k = VRELOCABLE;
  841.   } else {
  842.     lua_assert(op == OPR_NE || op == OPR_EQ ||
  843.                op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT);
  844.     bcemit_comp(fs, op, e1, e2);
  845.   }
  846. }

  847. /* Emit unary operator. */
  848. static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
  849. {
  850.   if (op == BC_NOT) {
  851.     /* Swap true and false lists. */
  852.     { BCPos temp = e->f; e->f = e->t; e->t = temp; }
  853.     jmp_dropval(fs, e->f);
  854.     jmp_dropval(fs, e->t);
  855.     expr_discharge(fs, e);
  856.     if (e->k == VKNIL || e->k == VKFALSE) {
  857.       e->k = VKTRUE;
  858.       return;
  859.     } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {
  860.       e->k = VKFALSE;
  861.       return;
  862.     } else if (e->k == VJMP) {
  863.       invertcond(fs, e);
  864.       return;
  865.     } else if (e->k == VRELOCABLE) {
  866.       bcreg_reserve(fs, 1);
  867.       setbc_a(bcptr(fs, e), fs->freereg-1);
  868.       e->u.s.info = fs->freereg-1;
  869.       e->k = VNONRELOC;
  870.     } else {
  871.       lua_assert(e->k == VNONRELOC);
  872.     }
  873.   } else {
  874.     lua_assert(op == BC_UNM || op == BC_LEN);
  875.     if (op == BC_UNM && !expr_hasjump(e)) {  /* Constant-fold negations. */
  876. #if LJ_HASFFI
  877.       if (e->k == VKCDATA) {  /* Fold in-place since cdata is not interned. */
  878.         GCcdata *cd = cdataV(&e->u.nval);
  879.         int64_t *p = (int64_t *)cdataptr(cd);
  880.         if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
  881.           p[1] ^= (int64_t)U64x(80000000,00000000);
  882.         else
  883.           *p = -*p;
  884.         return;
  885.       } else
  886. #endif
  887.       if (expr_isnumk(e) && !expr_numiszero(e)) {  /* Avoid folding to -0. */
  888.         TValue *o = expr_numtv(e);
  889.         if (tvisint(o)) {
  890.           int32_t k = intV(o);
  891.           if (k == -k)
  892.             setnumV(o, -(lua_Number)k);
  893.           else
  894.             setintV(o, -k);
  895.           return;
  896.         } else {
  897.           o->u64 ^= U64x(80000000,00000000);
  898.           return;
  899.         }
  900.       }
  901.     }
  902.     expr_toanyreg(fs, e);
  903.   }
  904.   expr_free(fs, e);
  905.   e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);
  906.   e->k = VRELOCABLE;
  907. }

  908. /* -- Lexer support ------------------------------------------------------- */

  909. /* Check and consume optional token. */
  910. static int lex_opt(LexState *ls, LexToken tok)
  911. {
  912.   if (ls->tok == tok) {
  913.     lj_lex_next(ls);
  914.     return 1;
  915.   }
  916.   return 0;
  917. }

  918. /* Check and consume token. */
  919. static void lex_check(LexState *ls, LexToken tok)
  920. {
  921.   if (ls->tok != tok)
  922.     err_token(ls, tok);
  923.   lj_lex_next(ls);
  924. }

  925. /* Check for matching token. */
  926. static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)
  927. {
  928.   if (!lex_opt(ls, what)) {
  929.     if (line == ls->linenumber) {
  930.       err_token(ls, what);
  931.     } else {
  932.       const char *swhat = lj_lex_token2str(ls, what);
  933.       const char *swho = lj_lex_token2str(ls, who);
  934.       lj_lex_error(ls, ls->tok, LJ_ERR_XMATCH, swhat, swho, line);
  935.     }
  936.   }
  937. }

  938. /* Check for string token. */
  939. static GCstr *lex_str(LexState *ls)
  940. {
  941.   GCstr *s;
  942.   if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto))
  943.     err_token(ls, TK_name);
  944.   s = strV(&ls->tokval);
  945.   lj_lex_next(ls);
  946.   return s;
  947. }

  948. /* -- Variable handling --------------------------------------------------- */

  949. #define var_get(ls, fs, i)        ((ls)->vstack[(fs)->varmap[(i)]])

  950. /* Define a new local variable. */
  951. static void var_new(LexState *ls, BCReg n, GCstr *name)
  952. {
  953.   FuncState *fs = ls->fs;
  954.   MSize vtop = ls->vtop;
  955.   checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables");
  956.   if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
  957.     if (ls->sizevstack >= LJ_MAX_VSTACK)
  958.       lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
  959.     lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
  960.   }
  961.   lua_assert((uintptr_t)name < VARNAME__MAX ||
  962.              lj_tab_getstr(fs->kt, name) != NULL);
  963.   /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
  964.   setgcref(ls->vstack[vtop].name, obj2gco(name));
  965.   fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
  966.   ls->vtop = vtop+1;
  967. }

  968. #define var_new_lit(ls, n, v) \
  969.   var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1))

  970. #define var_new_fixed(ls, n, vn) \
  971.   var_new(ls, (n), (GCstr *)(uintptr_t)(vn))

  972. /* Add local variables. */
  973. static void var_add(LexState *ls, BCReg nvars)
  974. {
  975.   FuncState *fs = ls->fs;
  976.   BCReg nactvar = fs->nactvar;
  977.   while (nvars--) {
  978.     VarInfo *v = &var_get(ls, fs, nactvar);
  979.     v->startpc = fs->pc;
  980.     v->slot = nactvar++;
  981.     v->info = 0;
  982.   }
  983.   fs->nactvar = nactvar;
  984. }

  985. /* Remove local variables. */
  986. static void var_remove(LexState *ls, BCReg tolevel)
  987. {
  988.   FuncState *fs = ls->fs;
  989.   while (fs->nactvar > tolevel)
  990.     var_get(ls, fs, --fs->nactvar).endpc = fs->pc;
  991. }

  992. /* Lookup local variable name. */
  993. static BCReg var_lookup_local(FuncState *fs, GCstr *n)
  994. {
  995.   int i;
  996.   for (i = fs->nactvar-1; i >= 0; i--) {
  997.     if (n == strref(var_get(fs->ls, fs, i).name))
  998.       return (BCReg)i;
  999.   }
  1000.   return (BCReg)-1/* Not found. */
  1001. }

  1002. /* Lookup or add upvalue index. */
  1003. static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)
  1004. {
  1005.   MSize i, n = fs->nuv;
  1006.   for (i = 0; i < n; i++)
  1007.     if (fs->uvmap[i] == vidx)
  1008.       return i;  /* Already exists. */
  1009.   /* Otherwise create a new one. */
  1010.   checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");
  1011.   lua_assert(e->k == VLOCAL || e->k == VUPVAL);
  1012.   fs->uvmap[n] = (uint16_t)vidx;
  1013.   fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info);
  1014.   fs->nuv = n+1;
  1015.   return n;
  1016. }

  1017. /* Forward declaration. */
  1018. static void fscope_uvmark(FuncState *fs, BCReg level);

  1019. /* Recursively lookup variables in enclosing functions. */
  1020. static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)
  1021. {
  1022.   if (fs) {
  1023.     BCReg reg = var_lookup_local(fs, name);
  1024.     if ((int32_t)reg >= 0) {  /* Local in this function? */
  1025.       expr_init(e, VLOCAL, reg);
  1026.       if (!first)
  1027.         fscope_uvmark(fs, reg);  /* Scope now has an upvalue. */
  1028.       return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);
  1029.     } else {
  1030.       MSize vidx = var_lookup_(fs->prev, name, e, 0);  /* Var in outer func? */
  1031.       if ((int32_t)vidx >= 0) {  /* Yes, make it an upvalue here. */
  1032.         e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e);
  1033.         e->k = VUPVAL;
  1034.         return vidx;
  1035.       }
  1036.     }
  1037.   } else/* Not found in any function, must be a global. */
  1038.     expr_init(e, VGLOBAL, 0);
  1039.     e->u.sval = name;
  1040.   }
  1041.   return (MSize)-1/* Global. */
  1042. }

  1043. /* Lookup variable name. */
  1044. #define var_lookup(ls, e) \
  1045.   var_lookup_((ls)->fs, lex_str(ls), (e), 1)

  1046. /* -- Goto an label handling ---------------------------------------------- */

  1047. /* Add a new goto or label. */
  1048. static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc)
  1049. {
  1050.   FuncState *fs = ls->fs;
  1051.   MSize vtop = ls->vtop;
  1052.   if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
  1053.     if (ls->sizevstack >= LJ_MAX_VSTACK)
  1054.       lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
  1055.     lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
  1056.   }
  1057.   lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL);
  1058.   /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
  1059.   setgcref(ls->vstack[vtop].name, obj2gco(name));
  1060.   ls->vstack[vtop].startpc = pc;
  1061.   ls->vstack[vtop].slot = (uint8_t)fs->nactvar;
  1062.   ls->vstack[vtop].info = info;
  1063.   ls->vtop = vtop+1;
  1064.   return vtop;
  1065. }

  1066. #define gola_isgoto(v)                ((v)->info & VSTACK_GOTO)
  1067. #define gola_islabel(v)                ((v)->info & VSTACK_LABEL)
  1068. #define gola_isgotolabel(v)        ((v)->info & (VSTACK_GOTO|VSTACK_LABEL))

  1069. /* Patch goto to jump to label. */
  1070. static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl)
  1071. {
  1072.   FuncState *fs = ls->fs;
  1073.   BCPos pc = vg->startpc;
  1074.   setgcrefnull(vg->name);  /* Invalidate pending goto. */
  1075.   setbc_a(&fs->bcbase[pc].ins, vl->slot);
  1076.   jmp_patch(fs, pc, vl->startpc);
  1077. }

  1078. /* Patch goto to close upvalues. */
  1079. static void gola_close(LexState *ls, VarInfo *vg)
  1080. {
  1081.   FuncState *fs = ls->fs;
  1082.   BCPos pc = vg->startpc;
  1083.   BCIns *ip = &fs->bcbase[pc].ins;
  1084.   lua_assert(gola_isgoto(vg));
  1085.   lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO);
  1086.   setbc_a(ip, vg->slot);
  1087.   if (bc_op(*ip) == BC_JMP) {
  1088.     BCPos next = jmp_next(fs, pc);
  1089.     if (next != NO_JMP) jmp_patch(fs, next, pc);  /* Jump to UCLO. */
  1090.     setbc_op(ip, BC_UCLO);  /* Turn into UCLO. */
  1091.     setbc_j(ip, NO_JMP);
  1092.   }
  1093. }

  1094. /* Resolve pending forward gotos for label. */
  1095. static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx)
  1096. {
  1097.   VarInfo *vg = ls->vstack + bl->vstart;
  1098.   VarInfo *vl = ls->vstack + idx;
  1099.   for (; vg < vl; vg++)
  1100.     if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) {
  1101.       if (vg->slot < vl->slot) {
  1102.         GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name);
  1103.         lua_assert((uintptr_t)name >= VARNAME__MAX);
  1104.         ls->linenumber = ls->fs->bcbase[vg->startpc].line;
  1105.         lua_assert(strref(vg->name) != NAME_BREAK);
  1106.         lj_lex_error(ls, 0, LJ_ERR_XGSCOPE,
  1107.                      strdata(strref(vg->name)), strdata(name));
  1108.       }
  1109.       gola_patch(ls, vg, vl);
  1110.     }
  1111. }

  1112. /* Fixup remaining gotos and labels for scope. */
  1113. static void gola_fixup(LexState *ls, FuncScope *bl)
  1114. {
  1115.   VarInfo *v = ls->vstack + bl->vstart;
  1116.   VarInfo *ve = ls->vstack + ls->vtop;
  1117.   for (; v < ve; v++) {
  1118.     GCstr *name = strref(v->name);
  1119.     if (name != NULL) {  /* Only consider remaining valid gotos/labels. */
  1120.       if (gola_islabel(v)) {
  1121.         VarInfo *vg;
  1122.         setgcrefnull(v->name);  /* Invalidate label that goes out of scope. */
  1123.         for (vg = v+1; vg < ve; vg++)  /* Resolve pending backward gotos. */
  1124.           if (strref(vg->name) == name && gola_isgoto(vg)) {
  1125.             if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot)
  1126.               gola_close(ls, vg);
  1127.             gola_patch(ls, vg, v);
  1128.           }
  1129.       } else if (gola_isgoto(v)) {
  1130.         if (bl->prev) {  /* Propagate goto or break to outer scope. */
  1131.           bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA;
  1132.           v->slot = bl->nactvar;
  1133.           if ((bl->flags & FSCOPE_UPVAL))
  1134.             gola_close(ls, v);
  1135.         } else/* No outer scope: undefined goto label or no loop. */
  1136.           ls->linenumber = ls->fs->bcbase[v->startpc].line;
  1137.           if (name == NAME_BREAK)
  1138.             lj_lex_error(ls, 0, LJ_ERR_XBREAK);
  1139.           else
  1140.             lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name));
  1141.         }
  1142.       }
  1143.     }
  1144.   }
  1145. }

  1146. /* Find existing label. */
  1147. static VarInfo *gola_findlabel(LexState *ls, GCstr *name)
  1148. {
  1149.   VarInfo *v = ls->vstack + ls->fs->bl->vstart;
  1150.   VarInfo *ve = ls->vstack + ls->vtop;
  1151.   for (; v < ve; v++)
  1152.     if (strref(v->name) == name && gola_islabel(v))
  1153.       return v;
  1154.   return NULL;
  1155. }

  1156. /* -- Scope handling ------------------------------------------------------ */

  1157. /* Begin a scope. */
  1158. static void fscope_begin(FuncState *fs, FuncScope *bl, int flags)
  1159. {
  1160.   bl->nactvar = (uint8_t)fs->nactvar;
  1161.   bl->flags = flags;
  1162.   bl->vstart = fs->ls->vtop;
  1163.   bl->prev = fs->bl;
  1164.   fs->bl = bl;
  1165.   lua_assert(fs->freereg == fs->nactvar);
  1166. }

  1167. /* End a scope. */
  1168. static void fscope_end(FuncState *fs)
  1169. {
  1170.   FuncScope *bl = fs->bl;
  1171.   LexState *ls = fs->ls;
  1172.   fs->bl = bl->prev;
  1173.   var_remove(ls, bl->nactvar);
  1174.   fs->freereg = fs->nactvar;
  1175.   lua_assert(bl->nactvar == fs->nactvar);
  1176.   if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL)
  1177.     bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0);
  1178.   if ((bl->flags & FSCOPE_BREAK)) {
  1179.     if ((bl->flags & FSCOPE_LOOP)) {
  1180.       MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc);
  1181.       ls->vtop = idx;  /* Drop break label immediately. */
  1182.       gola_resolve(ls, bl, idx);
  1183.       return;
  1184.     }  /* else: need the fixup step to propagate the breaks. */
  1185.   } else if (!(bl->flags & FSCOPE_GOLA)) {
  1186.     return;
  1187.   }
  1188.   gola_fixup(ls, bl);
  1189. }

  1190. /* Mark scope as having an upvalue. */
  1191. static void fscope_uvmark(FuncState *fs, BCReg level)
  1192. {
  1193.   FuncScope *bl;
  1194.   for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev)
  1195.     ;
  1196.   if (bl)
  1197.     bl->flags |= FSCOPE_UPVAL;
  1198. }

  1199. /* -- Function state management ------------------------------------------- */

  1200. /* Fixup bytecode for prototype. */
  1201. static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n)
  1202. {
  1203.   BCInsLine *base = fs->bcbase;
  1204.   MSize i;
  1205.   pt->sizebc = n;
  1206.   bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,
  1207.                    fs->framesize, 0);
  1208.   for (i = 1; i < n; i++)
  1209.     bc[i] = base[i].ins;
  1210. }

  1211. /* Fixup upvalues for child prototype, step #2. */
  1212. static void fs_fixup_uv2(FuncState *fs, GCproto *pt)
  1213. {
  1214.   VarInfo *vstack = fs->ls->vstack;
  1215.   uint16_t *uv = proto_uv(pt);
  1216.   MSize i, n = pt->sizeuv;
  1217.   for (i = 0; i < n; i++) {
  1218.     VarIndex vidx = uv[i];
  1219.     if (vidx >= LJ_MAX_VSTACK)
  1220.       uv[i] = vidx - LJ_MAX_VSTACK;
  1221.     else if ((vstack[vidx].info & VSTACK_VAR_RW))
  1222.       uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL;
  1223.     else
  1224.       uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE;
  1225.   }
  1226. }

  1227. /* Fixup constants for prototype. */
  1228. static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)
  1229. {
  1230.   GCtab *kt;
  1231.   TValue *array;
  1232.   Node *node;
  1233.   MSize i, hmask;
  1234.   checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");
  1235.   checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");
  1236.   setmref(pt->k, kptr);
  1237.   pt->sizekn = fs->nkn;
  1238.   pt->sizekgc = fs->nkgc;
  1239.   kt = fs->kt;
  1240.   array = tvref(kt->array);
  1241.   for (i = 0; i < kt->asize; i++)
  1242.     if (tvhaskslot(&array[i])) {
  1243.       TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])];
  1244.       if (LJ_DUALNUM)
  1245.         setintV(tv, (int32_t)i);
  1246.       else
  1247.         setnumV(tv, (lua_Number)i);
  1248.     }
  1249.   node = noderef(kt->node);
  1250.   hmask = kt->hmask;
  1251.   for (i = 0; i <= hmask; i++) {
  1252.     Node *n = &node[i];
  1253.     if (tvhaskslot(&n->val)) {
  1254.       ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);
  1255.       lua_assert(!tvisint(&n->key));
  1256.       if (tvisnum(&n->key)) {
  1257.         TValue *tv = &((TValue *)kptr)[kidx];
  1258.         if (LJ_DUALNUM) {
  1259.           lua_Number nn = numV(&n->key);
  1260.           int32_t k = lj_num2int(nn);
  1261.           lua_assert(!tvismzero(&n->key));
  1262.           if ((lua_Number)k == nn)
  1263.             setintV(tv, k);
  1264.           else
  1265.             *tv = n->key;
  1266.         } else {
  1267.           *tv = n->key;
  1268.         }
  1269.       } else {
  1270.         GCobj *o = gcV(&n->key);
  1271.         setgcref(((GCRef *)kptr)[~kidx], o);
  1272.         lj_gc_objbarrier(fs->L, pt, o);
  1273.         if (tvisproto(&n->key))
  1274.           fs_fixup_uv2(fs, gco2pt(o));
  1275.       }
  1276.     }
  1277.   }
  1278. }

  1279. /* Fixup upvalues for prototype, step #1. */
  1280. static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv)
  1281. {
  1282.   setmref(pt->uv, uv);
  1283.   pt->sizeuv = fs->nuv;
  1284.   memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex));
  1285. }

  1286. #ifndef LUAJIT_DISABLE_DEBUGINFO
  1287. /* Prepare lineinfo for prototype. */
  1288. static size_t fs_prep_line(FuncState *fs, BCLine numline)
  1289. {
  1290.   return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
  1291. }

  1292. /* Fixup lineinfo for prototype. */
  1293. static void fs_fixup_line(FuncState *fs, GCproto *pt,
  1294.                           void *lineinfo, BCLine numline)
  1295. {
  1296.   BCInsLine *base = fs->bcbase + 1;
  1297.   BCLine first = fs->linedefined;
  1298.   MSize i = 0, n = fs->pc-1;
  1299.   pt->firstline = fs->linedefined;
  1300.   pt->numline = numline;
  1301.   setmref(pt->lineinfo, lineinfo);
  1302.   if (LJ_LIKELY(numline < 256)) {
  1303.     uint8_t *li = (uint8_t *)lineinfo;
  1304.     do {
  1305.       BCLine delta = base[i].line - first;
  1306.       lua_assert(delta >= 0 && delta < 256);
  1307.       li[i] = (uint8_t)delta;
  1308.     } while (++i < n);
  1309.   } else if (LJ_LIKELY(numline < 65536)) {
  1310.     uint16_t *li = (uint16_t *)lineinfo;
  1311.     do {
  1312.       BCLine delta = base[i].line - first;
  1313.       lua_assert(delta >= 0 && delta < 65536);
  1314.       li[i] = (uint16_t)delta;
  1315.     } while (++i < n);
  1316.   } else {
  1317.     uint32_t *li = (uint32_t *)lineinfo;
  1318.     do {
  1319.       BCLine delta = base[i].line - first;
  1320.       lua_assert(delta >= 0);
  1321.       li[i] = (uint32_t)delta;
  1322.     } while (++i < n);
  1323.   }
  1324. }

  1325. /* Prepare variable info for prototype. */
  1326. static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)
  1327. {
  1328.   VarInfo *vs =ls->vstack, *ve;
  1329.   MSize i, n;
  1330.   BCPos lastpc;
  1331.   lj_buf_reset(&ls->sb);  /* Copy to temp. string buffer. */
  1332.   /* Store upvalue names. */
  1333.   for (i = 0, n = fs->nuv; i < n; i++) {
  1334.     GCstr *s = strref(vs[fs->uvmap[i]].name);
  1335.     MSize len = s->len+1;
  1336.     char *p = lj_buf_more(&ls->sb, len);
  1337.     p = lj_buf_wmem(p, strdata(s), len);
  1338.     setsbufP(&ls->sb, p);
  1339.   }
  1340.   *ofsvar = sbuflen(&ls->sb);
  1341.   lastpc = 0;
  1342.   /* Store local variable names and compressed ranges. */
  1343.   for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) {
  1344.     if (!gola_isgotolabel(vs)) {
  1345.       GCstr *s = strref(vs->name);
  1346.       BCPos startpc;
  1347.       char *p;
  1348.       if ((uintptr_t)s < VARNAME__MAX) {
  1349.         p = lj_buf_more(&ls->sb, 1 + 2*5);
  1350.         *p++ = (char)(uintptr_t)s;
  1351.       } else {
  1352.         MSize len = s->len+1;
  1353.         p = lj_buf_more(&ls->sb, len + 2*5);
  1354.         p = lj_buf_wmem(p, strdata(s), len);
  1355.       }
  1356.       startpc = vs->startpc;
  1357.       p = lj_strfmt_wuleb128(p, startpc-lastpc);
  1358.       p = lj_strfmt_wuleb128(p, vs->endpc-startpc);
  1359.       setsbufP(&ls->sb, p);
  1360.       lastpc = startpc;
  1361.     }
  1362.   }
  1363.   lj_buf_putb(&ls->sb, '\0');  /* Terminator for varinfo. */
  1364.   return sbuflen(&ls->sb);
  1365. }

  1366. /* Fixup variable info for prototype. */
  1367. static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar)
  1368. {
  1369.   setmref(pt->uvinfo, p);
  1370.   setmref(pt->varinfo, (char *)p + ofsvar);
  1371.   memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb));  /* Copy from temp. buffer. */
  1372. }
  1373. #else

  1374. /* Initialize with empty debug info, if disabled. */
  1375. #define fs_prep_line(fs, numline)                (UNUSED(numline), 0)
  1376. #define fs_fixup_line(fs, pt, li, numline) \
  1377.   pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL)
  1378. #define fs_prep_var(ls, fs, ofsvar)                (UNUSED(ofsvar), 0)
  1379. #define fs_fixup_var(ls, pt, p, ofsvar) \
  1380.   setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL)

  1381. #endif

  1382. /* Check if bytecode op returns. */
  1383. static int bcopisret(BCOp op)
  1384. {
  1385.   switch (op) {
  1386.   case BC_CALLMT: case BC_CALLT:
  1387.   case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
  1388.     return 1;
  1389.   default:
  1390.     return 0;
  1391.   }
  1392. }

  1393. /* Fixup return instruction for prototype. */
  1394. static void fs_fixup_ret(FuncState *fs)
  1395. {
  1396.   BCPos lastpc = fs->pc;
  1397.   if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) {
  1398.     if ((fs->bl->flags & FSCOPE_UPVAL))
  1399.       bcemit_AJ(fs, BC_UCLO, 0, 0);
  1400.     bcemit_AD(fs, BC_RET0, 0, 1);  /* Need final return. */
  1401.   }
  1402.   fs->bl->flags |= FSCOPE_NOCLOSE/* Handled above. */
  1403.   fscope_end(fs);
  1404.   lua_assert(fs->bl == NULL);
  1405.   /* May need to fixup returns encoded before first function was created. */
  1406.   if (fs->flags & PROTO_FIXUP_RETURN) {
  1407.     BCPos pc;
  1408.     for (pc = 1; pc < lastpc; pc++) {
  1409.       BCIns ins = fs->bcbase[pc].ins;
  1410.       BCPos offset;
  1411.       switch (bc_op(ins)) {
  1412.       case BC_CALLMT: case BC_CALLT:
  1413.       case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
  1414.         offset = bcemit_INS(fs, ins);  /* Copy original instruction. */
  1415.         fs->bcbase[offset].line = fs->bcbase[pc].line;
  1416.         offset = offset-(pc+1)+BCBIAS_J;
  1417.         if (offset > BCMAX_D)
  1418.           err_syntax(fs->ls, LJ_ERR_XFIXUP);
  1419.         /* Replace with UCLO plus branch. */
  1420.         fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset);
  1421.         break;
  1422.       case BC_UCLO:
  1423.         return/* We're done. */
  1424.       default:
  1425.         break;
  1426.       }
  1427.     }
  1428.   }
  1429. }

  1430. /* Finish a FuncState and return the new prototype. */
  1431. static GCproto *fs_finish(LexState *ls, BCLine line)
  1432. {
  1433.   lua_State *L = ls->L;
  1434.   FuncState *fs = ls->fs;
  1435.   BCLine numline = line - fs->linedefined;
  1436.   size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;
  1437.   GCproto *pt;

  1438.   /* Apply final fixups. */
  1439.   fs_fixup_ret(fs);

  1440.   /* Calculate total size of prototype including all colocated arrays. */
  1441.   sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
  1442.   sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);
  1443.   ofsk = sizept; sizept += fs->nkn*sizeof(TValue);
  1444.   ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;
  1445.   ofsli = sizept; sizept += fs_prep_line(fs, numline);
  1446.   ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);

  1447.   /* Allocate prototype and initialize its fields. */
  1448.   pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
  1449.   pt->gct = ~LJ_TPROTO;
  1450.   pt->sizept = (MSize)sizept;
  1451.   pt->trace = 0;
  1452.   pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN));
  1453.   pt->numparams = fs->numparams;
  1454.   pt->framesize = fs->framesize;
  1455.   setgcref(pt->chunkname, obj2gco(ls->chunkname));

  1456.   /* Close potentially uninitialized gap between bc and kgc. */
  1457.   *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0;
  1458.   fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc);
  1459.   fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk));
  1460.   fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv));
  1461.   fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline);
  1462.   fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar);

  1463.   lj_vmevent_send(L, BC,
  1464.     setprotoV(L, L->top++, pt);
  1465.   );

  1466.   L->top--;  /* Pop table of constants. */
  1467.   ls->vtop = fs->vbase;  /* Reset variable stack. */
  1468.   ls->fs = fs->prev;
  1469.   lua_assert(ls->fs != NULL || ls->tok == TK_eof);
  1470.   return pt;
  1471. }

  1472. /* Initialize a new FuncState. */
  1473. static void fs_init(LexState *ls, FuncState *fs)
  1474. {
  1475.   lua_State *L = ls->L;
  1476.   fs->prev = ls->fs; ls->fs = fs;  /* Append to list. */
  1477.   fs->ls = ls;
  1478.   fs->vbase = ls->vtop;
  1479.   fs->L = L;
  1480.   fs->pc = 0;
  1481.   fs->lasttarget = 0;
  1482.   fs->jpc = NO_JMP;
  1483.   fs->freereg = 0;
  1484.   fs->nkgc = 0;
  1485.   fs->nkn = 0;
  1486.   fs->nactvar = 0;
  1487.   fs->nuv = 0;
  1488.   fs->bl = NULL;
  1489.   fs->flags = 0;
  1490.   fs->framesize = 1/* Minimum frame size. */
  1491.   fs->kt = lj_tab_new(L, 0, 0);
  1492.   /* Anchor table of constants in stack to avoid being collected. */
  1493.   settabV(L, L->top, fs->kt);
  1494.   incr_top(L);
  1495. }

  1496. /* -- Expressions --------------------------------------------------------- */

  1497. /* Forward declaration. */
  1498. static void expr(LexState *ls, ExpDesc *v);

  1499. /* Return string expression. */
  1500. static void expr_str(LexState *ls, ExpDesc *e)
  1501. {
  1502.   expr_init(e, VKSTR, 0);
  1503.   e->u.sval = lex_str(ls);
  1504. }

  1505. /* Return index expression. */
  1506. static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e)
  1507. {
  1508.   /* Already called: expr_toval(fs, e). */
  1509.   t->k = VINDEXED;
  1510.   if (expr_isnumk(e)) {
  1511. #if LJ_DUALNUM
  1512.     if (tvisint(expr_numtv(e))) {
  1513.       int32_t k = intV(expr_numtv(e));
  1514.       if (checku8(k)) {
  1515.         t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
  1516.         return;
  1517.       }
  1518.     }
  1519. #else
  1520.     lua_Number n = expr_numberV(e);
  1521.     int32_t k = lj_num2int(n);
  1522.     if (checku8(k) && n == (lua_Number)k) {
  1523.       t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
  1524.       return;
  1525.     }
  1526. #endif
  1527.   } else if (expr_isstrk(e)) {
  1528.     BCReg idx = const_str(fs, e);
  1529.     if (idx <= BCMAX_C) {
  1530.       t->u.s.aux = ~idx;  /* -256..-1: const string key */
  1531.       return;
  1532.     }
  1533.   }
  1534.   t->u.s.aux = expr_toanyreg(fs, e);  /* 0..255: register */
  1535. }

  1536. /* Parse index expression with named field. */
  1537. static void expr_field(LexState *ls, ExpDesc *v)
  1538. {
  1539.   FuncState *fs = ls->fs;
  1540.   ExpDesc key;
  1541.   expr_toanyreg(fs, v);
  1542.   lj_lex_next(ls);  /* Skip dot or colon. */
  1543.   expr_str(ls, &key);
  1544.   expr_index(fs, v, &key);
  1545. }

  1546. /* Parse index expression with brackets. */
  1547. static void expr_bracket(LexState *ls, ExpDesc *v)
  1548. {
  1549.   lj_lex_next(ls);  /* Skip '['. */
  1550.   expr(ls, v);
  1551.   expr_toval(ls->fs, v);
  1552.   lex_check(ls, ']');
  1553. }

  1554. /* Get value of constant expression. */
  1555. static void expr_kvalue(TValue *v, ExpDesc *e)
  1556. {
  1557.   if (e->k <= VKTRUE) {
  1558.     setpriV(v, ~(uint32_t)e->k);
  1559.   } else if (e->k == VKSTR) {
  1560.     setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);
  1561.   } else {
  1562.     lua_assert(tvisnumber(expr_numtv(e)));
  1563.     *v = *expr_numtv(e);
  1564.   }
  1565. }

  1566. /* Parse table constructor expression. */
  1567. static void expr_table(LexState *ls, ExpDesc *e)
  1568. {
  1569.   FuncState *fs = ls->fs;
  1570.   BCLine line = ls->linenumber;
  1571.   GCtab *t = NULL;
  1572.   int vcall = 0, needarr = 0, fixt = 0;
  1573.   uint32_t narr = 1/* First array index. */
  1574.   uint32_t nhash = 0/* Number of hash entries. */
  1575.   BCReg freg = fs->freereg;
  1576.   BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0);
  1577.   expr_init(e, VNONRELOC, freg);
  1578.   bcreg_reserve(fs, 1);
  1579.   freg++;
  1580.   lex_check(ls, '{');
  1581.   while (ls->tok != '}') {
  1582.     ExpDesc key, val;
  1583.     vcall = 0;
  1584.     if (ls->tok == '[') {
  1585.       expr_bracket(ls, &key);  /* Already calls expr_toval. */
  1586.       if (!expr_isk(&key)) expr_index(fs, e, &key);
  1587.       if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++;
  1588.       lex_check(ls, '=');
  1589.     } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) &&
  1590.                lj_lex_lookahead(ls) == '=') {
  1591.       expr_str(ls, &key);
  1592.       lex_check(ls, '=');
  1593.       nhash++;
  1594.     } else {
  1595.       expr_init(&key, VKNUM, 0);
  1596.       setintV(&key.u.nval, (int)narr);
  1597.       narr++;
  1598.       needarr = vcall = 1;
  1599.     }
  1600.     expr(ls, &val);
  1601.     if (expr_isk(&key) && key.k != VKNIL &&
  1602.         (key.k == VKSTR || expr_isk_nojump(&val))) {
  1603.       TValue k, *v;
  1604.       if (!t) {  /* Create template table on demand. */
  1605.         BCReg kidx;
  1606.         t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash));
  1607.         kidx = const_gc(fs, obj2gco(t), LJ_TTAB);
  1608.         fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);
  1609.       }
  1610.       vcall = 0;
  1611.       expr_kvalue(&k, &key);
  1612.       v = lj_tab_set(fs->L, t, &k);
  1613.       lj_gc_anybarriert(fs->L, t);
  1614.       if (expr_isk_nojump(&val)) {  /* Add const key/value to template table. */
  1615.         expr_kvalue(v, &val);
  1616.       } else/* Otherwise create dummy string key (avoids lj_tab_newkey). */
  1617.         settabV(fs->L, v, t);  /* Preserve key with table itself as value. */
  1618.         fixt = 1;   /* Fix this later, after all resizes. */
  1619.         goto nonconst;
  1620.       }
  1621.     } else {
  1622.     nonconst:
  1623.       if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; }
  1624.       if (expr_isk(&key)) expr_index(fs, e, &key);
  1625.       bcemit_store(fs, e, &val);
  1626.     }
  1627.     fs->freereg = freg;
  1628.     if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break;
  1629.   }
  1630.   lex_match(ls, '}', '{', line);
  1631.   if (vcall) {
  1632.     BCInsLine *ilp = &fs->bcbase[fs->pc-1];
  1633.     ExpDesc en;
  1634.     lua_assert(bc_a(ilp->ins) == freg &&
  1635.                bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB));
  1636.     expr_init(&en, VKNUM, 0);
  1637.     en.u.nval.u32.lo = narr-1;
  1638.     en.u.nval.u32.hi = 0x43300000/* Biased integer to avoid denormals. */
  1639.     if (narr > 256) { fs->pc--; ilp--; }
  1640.     ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en));
  1641.     setbc_b(&ilp[-1].ins, 0);
  1642.   }
  1643.   if (pc == fs->pc-1) {  /* Make expr relocable if possible. */
  1644.     e->u.s.info = pc;
  1645.     fs->freereg--;
  1646.     e->k = VRELOCABLE;
  1647.   } else {
  1648.     e->k = VNONRELOC;  /* May have been changed by expr_index. */
  1649.   }
  1650.   if (!t) {  /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */
  1651.     BCIns *ip = &fs->bcbase[pc].ins;
  1652.     if (!needarr) narr = 0;
  1653.     else if (narr < 3) narr = 3;
  1654.     else if (narr > 0x7ff) narr = 0x7ff;
  1655.     setbc_d(ip, narr|(hsize2hbits(nhash)<<11));
  1656.   } else {
  1657.     if (needarr && t->asize < narr)
  1658.       lj_tab_reasize(fs->L, t, narr-1);
  1659.     if (fixt) {  /* Fix value for dummy keys in template table. */
  1660.       Node *node = noderef(t->node);
  1661.       uint32_t i, hmask = t->hmask;
  1662.       for (i = 0; i <= hmask; i++) {
  1663.         Node *n = &node[i];
  1664.         if (tvistab(&n->val)) {
  1665.           lua_assert(tabV(&n->val) == t);
  1666.           setnilV(&n->val);  /* Turn value into nil. */
  1667.         }
  1668.       }
  1669.     }
  1670.     lj_gc_check(fs->L);
  1671.   }
  1672. }

  1673. /* Parse function parameters. */
  1674. static BCReg parse_params(LexState *ls, int needself)
  1675. {
  1676.   FuncState *fs = ls->fs;
  1677.   BCReg nparams = 0;
  1678.   lex_check(ls, '(');
  1679.   if (needself)
  1680.     var_new_lit(ls, nparams++, "self");
  1681.   if (ls->tok != ')') {
  1682.     do {
  1683.       if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
  1684.         var_new(ls, nparams++, lex_str(ls));
  1685.       } else if (ls->tok == TK_dots) {
  1686.         lj_lex_next(ls);
  1687.         fs->flags |= PROTO_VARARG;
  1688.         break;
  1689.       } else {
  1690.         err_syntax(ls, LJ_ERR_XPARAM);
  1691.       }
  1692.     } while (lex_opt(ls, ','));
  1693.   }
  1694.   var_add(ls, nparams);
  1695.   lua_assert(fs->nactvar == nparams);
  1696.   bcreg_reserve(fs, nparams);
  1697.   lex_check(ls, ')');
  1698.   return nparams;
  1699. }

  1700. /* Forward declaration. */
  1701. static void parse_chunk(LexState *ls);

  1702. /* Parse body of a function. */
  1703. static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
  1704. {
  1705.   FuncState fs, *pfs = ls->fs;
  1706.   FuncScope bl;
  1707.   GCproto *pt;
  1708.   ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
  1709.   fs_init(ls, &fs);
  1710.   fscope_begin(&fs, &bl, 0);
  1711.   fs.linedefined = line;
  1712.   fs.numparams = (uint8_t)parse_params(ls, needself);
  1713.   fs.bcbase = pfs->bcbase + pfs->pc;
  1714.   fs.bclim = pfs->bclim - pfs->pc;
  1715.   bcemit_AD(&fs, BC_FUNCF, 0, 0);  /* Placeholder. */
  1716.   parse_chunk(ls);
  1717.   if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line);
  1718.   pt = fs_finish(ls, (ls->lastline = ls->linenumber));
  1719.   pfs->bcbase = ls->bcstack + oldbase;  /* May have been reallocated. */
  1720.   pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
  1721.   /* Store new prototype in the constant array of the parent. */
  1722.   expr_init(e, VRELOCABLE,
  1723.             bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));
  1724. #if LJ_HASFFI
  1725.   pfs->flags |= (fs.flags & PROTO_FFI);
  1726. #endif
  1727.   if (!(pfs->flags & PROTO_CHILD)) {
  1728.     if (pfs->flags & PROTO_HAS_RETURN)
  1729.       pfs->flags |= PROTO_FIXUP_RETURN;
  1730.     pfs->flags |= PROTO_CHILD;
  1731.   }
  1732.   lj_lex_next(ls);
  1733. }

  1734. /* Parse expression list. Last expression is left open. */
  1735. static BCReg expr_list(LexState *ls, ExpDesc *v)
  1736. {
  1737.   BCReg n = 1;
  1738.   expr(ls, v);
  1739.   while (lex_opt(ls, ',')) {
  1740.     expr_tonextreg(ls->fs, v);
  1741.     expr(ls, v);
  1742.     n++;
  1743.   }
  1744.   return n;
  1745. }

  1746. /* Parse function argument list. */
  1747. static void parse_args(LexState *ls, ExpDesc *e)
  1748. {
  1749.   FuncState *fs = ls->fs;
  1750.   ExpDesc args;
  1751.   BCIns ins;
  1752.   BCReg base;
  1753.   BCLine line = ls->linenumber;
  1754.   if (ls->tok == '(') {
  1755. #if !LJ_52
  1756.     if (line != ls->lastline)
  1757.       err_syntax(ls, LJ_ERR_XAMBIG);
  1758. #endif
  1759.     lj_lex_next(ls);
  1760.     if (ls->tok == ')') {  /* f(). */
  1761.       args.k = VVOID;
  1762.     } else {
  1763.       expr_list(ls, &args);
  1764.       if (args.k == VCALL)  /* f(a, b, g()) or f(a, b, ...). */
  1765.         setbc_b(bcptr(fs, &args), 0);  /* Pass on multiple results. */
  1766.     }
  1767.     lex_match(ls, ')', '(', line);
  1768.   } else if (ls->tok == '{') {
  1769.     expr_table(ls, &args);
  1770.   } else if (ls->tok == TK_string) {
  1771.     expr_init(&args, VKSTR, 0);
  1772.     args.u.sval = strV(&ls->tokval);
  1773.     lj_lex_next(ls);
  1774.   } else {
  1775.     err_syntax(ls, LJ_ERR_XFUNARG);
  1776.     return/* Silence compiler. */
  1777.   }
  1778.   lua_assert(e->k == VNONRELOC);
  1779.   base = e->u.s.info;  /* Base register for call. */
  1780.   if (args.k == VCALL) {
  1781.     ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);
  1782.   } else {
  1783.     if (args.k != VVOID)
  1784.       expr_tonextreg(fs, &args);
  1785.     ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2);
  1786.   }
  1787.   expr_init(e, VCALL, bcemit_INS(fs, ins));
  1788.   e->u.s.aux = base;
  1789.   fs->bcbase[fs->pc - 1].line = line;
  1790.   fs->freereg = base+1/* Leave one result by default. */
  1791. }

  1792. /* Parse primary expression. */
  1793. static void expr_primary(LexState *ls, ExpDesc *v)
  1794. {
  1795.   FuncState *fs = ls->fs;
  1796.   /* Parse prefix expression. */
  1797.   if (ls->tok == '(') {
  1798.     BCLine line = ls->linenumber;
  1799.     lj_lex_next(ls);
  1800.     expr(ls, v);
  1801.     lex_match(ls, ')', '(', line);
  1802.     expr_discharge(ls->fs, v);
  1803.   } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
  1804.     var_lookup(ls, v);
  1805.   } else {
  1806.     err_syntax(ls, LJ_ERR_XSYMBOL);
  1807.   }
  1808.   for (;;) {  /* Parse multiple expression suffixes. */
  1809.     if (ls->tok == '.') {
  1810.       expr_field(ls, v);
  1811.     } else if (ls->tok == '[') {
  1812.       ExpDesc key;
  1813.       expr_toanyreg(fs, v);
  1814.       expr_bracket(ls, &key);
  1815.       expr_index(fs, v, &key);
  1816.     } else if (ls->tok == ':') {
  1817.       ExpDesc key;
  1818.       lj_lex_next(ls);
  1819.       expr_str(ls, &key);
  1820.       bcemit_method(fs, v, &key);
  1821.       parse_args(ls, v);
  1822.     } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') {
  1823.       expr_tonextreg(fs, v);
  1824.       if (LJ_FR2) bcreg_reserve(fs, 1);
  1825.       parse_args(ls, v);
  1826.     } else {
  1827.       break;
  1828.     }
  1829.   }
  1830. }

  1831. /* Parse simple expression. */
  1832. static void expr_simple(LexState *ls, ExpDesc *v)
  1833. {
  1834.   switch (ls->tok) {
  1835.   case TK_number:
  1836.     expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0);
  1837.     copyTV(ls->L, &v->u.nval, &ls->tokval);
  1838.     break;
  1839.   case TK_string:
  1840.     expr_init(v, VKSTR, 0);
  1841.     v->u.sval = strV(&ls->tokval);
  1842.     break;
  1843.   case TK_nil:
  1844.     expr_init(v, VKNIL, 0);
  1845.     break;
  1846.   case TK_true:
  1847.     expr_init(v, VKTRUE, 0);
  1848.     break;
  1849.   case TK_false:
  1850.     expr_init(v, VKFALSE, 0);
  1851.     break;
  1852.   case TK_dots: {  /* Vararg. */
  1853.     FuncState *fs = ls->fs;
  1854.     BCReg base;
  1855.     checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS);
  1856.     bcreg_reserve(fs, 1);
  1857.     base = fs->freereg-1;
  1858.     expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams));
  1859.     v->u.s.aux = base;
  1860.     break;
  1861.   }
  1862.   case '{'/* Table constructor. */
  1863.     expr_table(ls, v);
  1864.     return;
  1865.   case TK_function:
  1866.     lj_lex_next(ls);
  1867.     parse_body(ls, v, 0, ls->linenumber);
  1868.     return;
  1869.   default:
  1870.     expr_primary(ls, v);
  1871.     return;
  1872.   }
  1873.   lj_lex_next(ls);
  1874. }

  1875. /* Manage syntactic levels to avoid blowing up the stack. */
  1876. static void synlevel_begin(LexState *ls)
  1877. {
  1878.   if (++ls->level >= LJ_MAX_XLEVEL)
  1879.     lj_lex_error(ls, 0, LJ_ERR_XLEVELS);
  1880. }

  1881. #define synlevel_end(ls)        ((ls)->level--)

  1882. /* Convert token to binary operator. */
  1883. static BinOpr token2binop(LexToken tok)
  1884. {
  1885.   switch (tok) {
  1886.   case '+':        return OPR_ADD;
  1887.   case '-':        return OPR_SUB;
  1888.   case '*':        return OPR_MUL;
  1889.   case '/':        return OPR_DIV;
  1890.   case '%':        return OPR_MOD;
  1891.   case '^':        return OPR_POW;
  1892.   case TK_concat: return OPR_CONCAT;
  1893.   case TK_ne:        return OPR_NE;
  1894.   case TK_eq:        return OPR_EQ;
  1895.   case '<':        return OPR_LT;
  1896.   case TK_le:        return OPR_LE;
  1897.   case '>':        return OPR_GT;
  1898.   case TK_ge:        return OPR_GE;
  1899.   case TK_and:        return OPR_AND;
  1900.   case TK_or:        return OPR_OR;
  1901.   default:        return OPR_NOBINOPR;
  1902.   }
  1903. }

  1904. /* Priorities for each binary operator. ORDER OPR. */
  1905. static const struct {
  1906.   uint8_t left;                /* Left priority. */
  1907.   uint8_t right;        /* Right priority. */
  1908. } priority[] = {
  1909.   {6,6}, {6,6}, {7,7}, {7,7}, {7,7},        /* ADD SUB MUL DIV MOD */
  1910.   {10,9}, {5,4},                        /* POW CONCAT (right associative) */
  1911.   {3,3}, {3,3},                                /* EQ NE */
  1912.   {3,3}, {3,3}, {3,3}, {3,3},                /* LT GE GT LE */
  1913.   {2,2}, {1,1}                                /* AND OR */
  1914. };

  1915. #define UNARY_PRIORITY                8  /* Priority for unary operators. */

  1916. /* Forward declaration. */
  1917. static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit);

  1918. /* Parse unary expression. */
  1919. static void expr_unop(LexState *ls, ExpDesc *v)
  1920. {
  1921.   BCOp op;
  1922.   if (ls->tok == TK_not) {
  1923.     op = BC_NOT;
  1924.   } else if (ls->tok == '-') {
  1925.     op = BC_UNM;
  1926.   } else if (ls->tok == '#') {
  1927.     op = BC_LEN;
  1928.   } else {
  1929.     expr_simple(ls, v);
  1930.     return;
  1931.   }
  1932.   lj_lex_next(ls);
  1933.   expr_binop(ls, v, UNARY_PRIORITY);
  1934.   bcemit_unop(ls->fs, op, v);
  1935. }

  1936. /* Parse binary expressions with priority higher than the limit. */
  1937. static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit)
  1938. {
  1939.   BinOpr op;
  1940.   synlevel_begin(ls);
  1941.   expr_unop(ls, v);
  1942.   op = token2binop(ls->tok);
  1943.   while (op != OPR_NOBINOPR && priority[op].left > limit) {
  1944.     ExpDesc v2;
  1945.     BinOpr nextop;
  1946.     lj_lex_next(ls);
  1947.     bcemit_binop_left(ls->fs, op, v);
  1948.     /* Parse binary expression with higher priority. */
  1949.     nextop = expr_binop(ls, &v2, priority[op].right);
  1950.     bcemit_binop(ls->fs, op, v, &v2);
  1951.     op = nextop;
  1952.   }
  1953.   synlevel_end(ls);
  1954.   return op;  /* Return unconsumed binary operator (if any). */
  1955. }

  1956. /* Parse expression. */
  1957. static void expr(LexState *ls, ExpDesc *v)
  1958. {
  1959.   expr_binop(ls, v, 0);  /* Priority 0: parse whole expression. */
  1960. }

  1961. /* Assign expression to the next register. */
  1962. static void expr_next(LexState *ls)
  1963. {
  1964.   ExpDesc e;
  1965.   expr(ls, &e);
  1966.   expr_tonextreg(ls->fs, &e);
  1967. }

  1968. /* Parse conditional expression. */
  1969. static BCPos expr_cond(LexState *ls)
  1970. {
  1971.   ExpDesc v;
  1972.   expr(ls, &v);
  1973.   if (v.k == VKNIL) v.k = VKFALSE;
  1974.   bcemit_branch_t(ls->fs, &v);
  1975.   return v.f;
  1976. }

  1977. /* -- Assignments --------------------------------------------------------- */

  1978. /* List of LHS variables. */
  1979. typedef struct LHSVarList {
  1980.   ExpDesc v;                        /* LHS variable. */
  1981.   struct LHSVarList *prev;        /* Link to previous LHS variable. */
  1982. } LHSVarList;

  1983. /* Eliminate write-after-read hazards for local variable assignment. */
  1984. static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v)
  1985. {
  1986.   FuncState *fs = ls->fs;
  1987.   BCReg reg = v->u.s.info;  /* Check against this variable. */
  1988.   BCReg tmp = fs->freereg/* Rename to this temp. register (if needed). */
  1989.   int hazard = 0;
  1990.   for (; lh; lh = lh->prev) {
  1991.     if (lh->v.k == VINDEXED) {
  1992.       if (lh->v.u.s.info == reg) {  /* t[i], t = 1, 2 */
  1993.         hazard = 1;
  1994.         lh->v.u.s.info = tmp;
  1995.       }
  1996.       if (lh->v.u.s.aux == reg) {  /* t[i], i = 1, 2 */
  1997.         hazard = 1;
  1998.         lh->v.u.s.aux = tmp;
  1999.       }
  2000.     }
  2001.   }
  2002.   if (hazard) {
  2003.     bcemit_AD(fs, BC_MOV, tmp, reg);  /* Rename conflicting variable. */
  2004.     bcreg_reserve(fs, 1);
  2005.   }
  2006. }

  2007. /* Adjust LHS/RHS of an assignment. */
  2008. static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e)
  2009. {
  2010.   FuncState *fs = ls->fs;
  2011.   int32_t extra = (int32_t)nvars - (int32_t)nexps;
  2012.   if (e->k == VCALL) {
  2013.     extra++;  /* Compensate for the VCALL itself. */
  2014.     if (extra < 0) extra = 0;
  2015.     setbc_b(bcptr(fs, e), extra+1);  /* Fixup call results. */
  2016.     if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1);
  2017.   } else {
  2018.     if (e->k != VVOID)
  2019.       expr_tonextreg(fs, e);  /* Close last expression. */
  2020.     if (extra > 0) {  /* Leftover LHS are set to nil. */
  2021.       BCReg reg = fs->freereg;
  2022.       bcreg_reserve(fs, (BCReg)extra);
  2023.       bcemit_nil(fs, reg, (BCReg)extra);
  2024.     }
  2025.   }
  2026. }

  2027. /* Recursively parse assignment statement. */
  2028. static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars)
  2029. {
  2030.   ExpDesc e;
  2031.   checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX);
  2032.   if (lex_opt(ls, ',')) {  /* Collect LHS list and recurse upwards. */
  2033.     LHSVarList vl;
  2034.     vl.prev = lh;
  2035.     expr_primary(ls, &vl.v);
  2036.     if (vl.v.k == VLOCAL)
  2037.       assign_hazard(ls, lh, &vl.v);
  2038.     checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names");
  2039.     parse_assignment(ls, &vl, nvars+1);
  2040.   } else/* Parse RHS. */
  2041.     BCReg nexps;
  2042.     lex_check(ls, '=');
  2043.     nexps = expr_list(ls, &e);
  2044.     if (nexps == nvars) {
  2045.       if (e.k == VCALL) {
  2046.         if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) {  /* Vararg assignment. */
  2047.           ls->fs->freereg--;
  2048.           e.k = VRELOCABLE;
  2049.         } else/* Multiple call results. */
  2050.           e.u.s.info = e.u.s.aux;  /* Base of call is not relocatable. */
  2051.           e.k = VNONRELOC;
  2052.         }
  2053.       }
  2054.       bcemit_store(ls->fs, &lh->v, &e);
  2055.       return;
  2056.     }
  2057.     assign_adjust(ls, nvars, nexps, &e);
  2058.     if (nexps > nvars)
  2059.       ls->fs->freereg -= nexps - nvars;  /* Drop leftover regs. */
  2060.   }
  2061.   /* Assign RHS to LHS and recurse downwards. */
  2062.   expr_init(&e, VNONRELOC, ls->fs->freereg-1);
  2063.   bcemit_store(ls->fs, &lh->v, &e);
  2064. }

  2065. /* Parse call statement or assignment. */
  2066. static void parse_call_assign(LexState *ls)
  2067. {
  2068.   FuncState *fs = ls->fs;
  2069.   LHSVarList vl;
  2070.   expr_primary(ls, &vl.v);
  2071.   if (vl.v.k == VCALL) {  /* Function call statement. */
  2072.     setbc_b(bcptr(fs, &vl.v), 1);  /* No results. */
  2073.   } else/* Start of an assignment. */
  2074.     vl.prev = NULL;
  2075.     parse_assignment(ls, &vl, 1);
  2076.   }
  2077. }

  2078. /* Parse 'local' statement. */
  2079. static void parse_local(LexState *ls)
  2080. {
  2081.   if (lex_opt(ls, TK_function)) {  /* Local function declaration. */
  2082.     ExpDesc v, b;
  2083.     FuncState *fs = ls->fs;
  2084.     var_new(ls, 0, lex_str(ls));
  2085.     expr_init(&v, VLOCAL, fs->freereg);
  2086.     v.u.s.aux = fs->varmap[fs->freereg];
  2087.     bcreg_reserve(fs, 1);
  2088.     var_add(ls, 1);
  2089.     parse_body(ls, &b, 0, ls->linenumber);
  2090.     /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */
  2091.     expr_free(fs, &b);
  2092.     expr_toreg(fs, &b, v.u.s.info);
  2093.     /* The upvalue is in scope, but the local is only valid after the store. */
  2094.     var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc;
  2095.   } else/* Local variable declaration. */
  2096.     ExpDesc e;
  2097.     BCReg nexps, nvars = 0;
  2098.     do/* Collect LHS. */
  2099.       var_new(ls, nvars++, lex_str(ls));
  2100.     } while (lex_opt(ls, ','));
  2101.     if (lex_opt(ls, '=')) {  /* Optional RHS. */
  2102.       nexps = expr_list(ls, &e);
  2103.     } else/* Or implicitly set to nil. */
  2104.       e.k = VVOID;
  2105.       nexps = 0;
  2106.     }
  2107.     assign_adjust(ls, nvars, nexps, &e);
  2108.     var_add(ls, nvars);
  2109.   }
  2110. }

  2111. /* Parse 'function' statement. */
  2112. static void parse_func(LexState *ls, BCLine line)
  2113. {
  2114.   FuncState *fs;
  2115.   ExpDesc v, b;
  2116.   int needself = 0;
  2117.   lj_lex_next(ls);  /* Skip 'function'. */
  2118.   /* Parse function name. */
  2119.   var_lookup(ls, &v);
  2120.   while (ls->tok == '.'/* Multiple dot-separated fields. */
  2121.     expr_field(ls, &v);
  2122.   if (ls->tok == ':') {  /* Optional colon to signify method call. */
  2123.     needself = 1;
  2124.     expr_field(ls, &v);
  2125.   }
  2126.   parse_body(ls, &b, needself, line);
  2127.   fs = ls->fs;
  2128.   bcemit_store(fs, &v, &b);
  2129.   fs->bcbase[fs->pc - 1].line = line;  /* Set line for the store. */
  2130. }

  2131. /* -- Control transfer statements ----------------------------------------- */

  2132. /* Check for end of block. */
  2133. static int parse_isend(LexToken tok)
  2134. {
  2135.   switch (tok) {
  2136.   case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof:
  2137.     return 1;
  2138.   default:
  2139.     return 0;
  2140.   }
  2141. }

  2142. /* Parse 'return' statement. */
  2143. static void parse_return(LexState *ls)
  2144. {
  2145.   BCIns ins;
  2146.   FuncState *fs = ls->fs;
  2147.   lj_lex_next(ls);  /* Skip 'return'. */
  2148.   fs->flags |= PROTO_HAS_RETURN;
  2149.   if (parse_isend(ls->tok) || ls->tok == ';') {  /* Bare return. */
  2150.     ins = BCINS_AD(BC_RET0, 0, 1);
  2151.   } else/* Return with one or more values. */
  2152.     ExpDesc e;  /* Receives the _last_ expression in the list. */
  2153.     BCReg nret = expr_list(ls, &e);
  2154.     if (nret == 1) {  /* Return one result. */
  2155.       if (e.k == VCALL) {  /* Check for tail call. */
  2156.         BCIns *ip = bcptr(fs, &e);
  2157.         /* It doesn't pay off to add BC_VARGT just for 'return ...'. */
  2158.         if (bc_op(*ip) == BC_VARG) goto notailcall;
  2159.         fs->pc--;
  2160.         ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip));
  2161.       } else/* Can return the result from any register. */
  2162.         ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2);
  2163.       }
  2164.     } else {
  2165.       if (e.k == VCALL) {  /* Append all results from a call. */
  2166.       notailcall:
  2167.         setbc_b(bcptr(fs, &e), 0);
  2168.         ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar);
  2169.       } else {
  2170.         expr_tonextreg(fs, &e);  /* Force contiguous registers. */
  2171.         ins = BCINS_AD(BC_RET, fs->nactvar, nret+1);
  2172.       }
  2173.     }
  2174.   }
  2175.   if (fs->flags & PROTO_CHILD)
  2176.     bcemit_AJ(fs, BC_UCLO, 0, 0);  /* May need to close upvalues first. */
  2177.   bcemit_INS(fs, ins);
  2178. }

  2179. /* Parse 'break' statement. */
  2180. static void parse_break(LexState *ls)
  2181. {
  2182.   ls->fs->bl->flags |= FSCOPE_BREAK;
  2183.   gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs));
  2184. }

  2185. /* Parse 'goto' statement. */
  2186. static void parse_goto(LexState *ls)
  2187. {
  2188.   FuncState *fs = ls->fs;
  2189.   GCstr *name = lex_str(ls);
  2190.   VarInfo *vl = gola_findlabel(ls, name);
  2191.   if (vl)  /* Treat backwards goto within same scope like a loop. */
  2192.     bcemit_AJ(fs, BC_LOOP, vl->slot, -1);  /* No BC range check. */
  2193.   fs->bl->flags |= FSCOPE_GOLA;
  2194.   gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs));
  2195. }

  2196. /* Parse label. */
  2197. static void parse_label(LexState *ls)
  2198. {
  2199.   FuncState *fs = ls->fs;
  2200.   GCstr *name;
  2201.   MSize idx;
  2202.   fs->lasttarget = fs->pc;
  2203.   fs->bl->flags |= FSCOPE_GOLA;
  2204.   lj_lex_next(ls);  /* Skip '::'. */
  2205.   name = lex_str(ls);
  2206.   if (gola_findlabel(ls, name))
  2207.     lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name));
  2208.   idx = gola_new(ls, name, VSTACK_LABEL, fs->pc);
  2209.   lex_check(ls, TK_label);
  2210.   /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */
  2211.   for (;;) {
  2212.     if (ls->tok == TK_label) {
  2213.       synlevel_begin(ls);
  2214.       parse_label(ls);
  2215.       synlevel_end(ls);
  2216.     } else if (LJ_52 && ls->tok == ';') {
  2217.       lj_lex_next(ls);
  2218.     } else {
  2219.       break;
  2220.     }
  2221.   }
  2222.   /* Trailing label is considered to be outside of scope. */
  2223.   if (parse_isend(ls->tok) && ls->tok != TK_until)
  2224.     ls->vstack[idx].slot = fs->bl->nactvar;
  2225.   gola_resolve(ls, fs->bl, idx);
  2226. }

  2227. /* -- Blocks, loops and conditional statements ---------------------------- */

  2228. /* Parse a block. */
  2229. static void parse_block(LexState *ls)
  2230. {
  2231.   FuncState *fs = ls->fs;
  2232.   FuncScope bl;
  2233.   fscope_begin(fs, &bl, 0);
  2234.   parse_chunk(ls);
  2235.   fscope_end(fs);
  2236. }

  2237. /* Parse 'while' statement. */
  2238. static void parse_while(LexState *ls, BCLine line)
  2239. {
  2240.   FuncState *fs = ls->fs;
  2241.   BCPos start, loop, condexit;
  2242.   FuncScope bl;
  2243.   lj_lex_next(ls);  /* Skip 'while'. */
  2244.   start = fs->lasttarget = fs->pc;
  2245.   condexit = expr_cond(ls);
  2246.   fscope_begin(fs, &bl, FSCOPE_LOOP);
  2247.   lex_check(ls, TK_do);
  2248.   loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
  2249.   parse_block(ls);
  2250.   jmp_patch(fs, bcemit_jmp(fs), start);
  2251.   lex_match(ls, TK_end, TK_while, line);
  2252.   fscope_end(fs);
  2253.   jmp_tohere(fs, condexit);
  2254.   jmp_patchins(fs, loop, fs->pc);
  2255. }

  2256. /* Parse 'repeat' statement. */
  2257. static void parse_repeat(LexState *ls, BCLine line)
  2258. {
  2259.   FuncState *fs = ls->fs;
  2260.   BCPos loop = fs->lasttarget = fs->pc;
  2261.   BCPos condexit;
  2262.   FuncScope bl1, bl2;
  2263.   fscope_begin(fs, &bl1, FSCOPE_LOOP);  /* Breakable loop scope. */
  2264.   fscope_begin(fs, &bl2, 0);  /* Inner scope. */
  2265.   lj_lex_next(ls);  /* Skip 'repeat'. */
  2266.   bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
  2267.   parse_chunk(ls);
  2268.   lex_match(ls, TK_until, TK_repeat, line);
  2269.   condexit = expr_cond(ls);  /* Parse condition (still inside inner scope). */
  2270.   if (!(bl2.flags & FSCOPE_UPVAL)) {  /* No upvalues? Just end inner scope. */
  2271.     fscope_end(fs);
  2272.   } else/* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */
  2273.     parse_break(ls);  /* Break from loop and close upvalues. */
  2274.     jmp_tohere(fs, condexit);
  2275.     fscope_end(fs);  /* End inner scope and close upvalues. */
  2276.     condexit = bcemit_jmp(fs);
  2277.   }
  2278.   jmp_patch(fs, condexit, loop);  /* Jump backwards if !cond. */
  2279.   jmp_patchins(fs, loop, fs->pc);
  2280.   fscope_end(fs);  /* End loop scope. */
  2281. }

  2282. /* Parse numeric 'for'. */
  2283. static void parse_for_num(LexState *ls, GCstr *varname, BCLine line)
  2284. {
  2285.   FuncState *fs = ls->fs;
  2286.   BCReg base = fs->freereg;
  2287.   FuncScope bl;
  2288.   BCPos loop, loopend;
  2289.   /* Hidden control variables. */
  2290.   var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);
  2291.   var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);
  2292.   var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);
  2293.   /* Visible copy of index variable. */
  2294.   var_new(ls, FORL_EXT, varname);
  2295.   lex_check(ls, '=');
  2296.   expr_next(ls);
  2297.   lex_check(ls, ',');
  2298.   expr_next(ls);
  2299.   if (lex_opt(ls, ',')) {
  2300.     expr_next(ls);
  2301.   } else {
  2302.     bcemit_AD(fs, BC_KSHORT, fs->freereg, 1);  /* Default step is 1. */
  2303.     bcreg_reserve(fs, 1);
  2304.   }
  2305.   var_add(ls, 3);  /* Hidden control variables. */
  2306.   lex_check(ls, TK_do);
  2307.   loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP);
  2308.   fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
  2309.   var_add(ls, 1);
  2310.   bcreg_reserve(fs, 1);
  2311.   parse_block(ls);
  2312.   fscope_end(fs);
  2313.   /* Perform loop inversion. Loop control instructions are at the end. */
  2314.   loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP);
  2315.   fs->bcbase[loopend].line = line;  /* Fix line for control ins. */
  2316.   jmp_patchins(fs, loopend, loop+1);
  2317.   jmp_patchins(fs, loop, fs->pc);
  2318. }

  2319. /* Try to predict whether the iterator is next() and specialize the bytecode.
  2320. ** Detecting next() and pairs() by name is simplistic, but quite effective.
  2321. ** The interpreter backs off if the check for the closure fails at runtime.
  2322. */
  2323. static int predict_next(LexState *ls, FuncState *fs, BCPos pc)
  2324. {
  2325.   BCIns ins = fs->bcbase[pc].ins;
  2326.   GCstr *name;
  2327.   cTValue *o;
  2328.   switch (bc_op(ins)) {
  2329.   case BC_MOV:
  2330.     name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name));
  2331.     break;
  2332.   case BC_UGET:
  2333.     name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name));
  2334.     break;
  2335.   case BC_GGET:
  2336.     /* There's no inverse index (yet), so lookup the strings. */
  2337.     o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs"));
  2338.     if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
  2339.       return 1;
  2340.     o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next"));
  2341.     if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
  2342.       return 1;
  2343.     return 0;
  2344.   default:
  2345.     return 0;
  2346.   }
  2347.   return (name->len == 5 && !strcmp(strdata(name), "pairs")) ||
  2348.          (name->len == 4 && !strcmp(strdata(name), "next"));
  2349. }

  2350. /* Parse 'for' iterator. */
  2351. static void parse_for_iter(LexState *ls, GCstr *indexname)
  2352. {
  2353.   FuncState *fs = ls->fs;
  2354.   ExpDesc e;
  2355.   BCReg nvars = 0;
  2356.   BCLine line;
  2357.   BCReg base = fs->freereg + 3;
  2358.   BCPos loop, loopend, exprpc = fs->pc;
  2359.   FuncScope bl;
  2360.   int isnext;
  2361.   /* Hidden control variables. */
  2362.   var_new_fixed(ls, nvars++, VARNAME_FOR_GEN);
  2363.   var_new_fixed(ls, nvars++, VARNAME_FOR_STATE);
  2364.   var_new_fixed(ls, nvars++, VARNAME_FOR_CTL);
  2365.   /* Visible variables returned from iterator. */
  2366.   var_new(ls, nvars++, indexname);
  2367.   while (lex_opt(ls, ','))
  2368.     var_new(ls, nvars++, lex_str(ls));
  2369.   lex_check(ls, TK_in);
  2370.   line = ls->linenumber;
  2371.   assign_adjust(ls, 3, expr_list(ls, &e), &e);
  2372.   /* The iterator needs another 3 [4] slots (func [pc] | state ctl). */
  2373.   bcreg_bump(fs, 3+LJ_FR2);
  2374.   isnext = (nvars <= 5 && predict_next(ls, fs, exprpc));
  2375.   var_add(ls, 3);  /* Hidden control variables. */
  2376.   lex_check(ls, TK_do);
  2377.   loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP);
  2378.   fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
  2379.   var_add(ls, nvars-3);
  2380.   bcreg_reserve(fs, nvars-3);
  2381.   parse_block(ls);
  2382.   fscope_end(fs);
  2383.   /* Perform loop inversion. Loop control instructions are at the end. */
  2384.   jmp_patchins(fs, loop, fs->pc);
  2385.   bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1);
  2386.   loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP);
  2387.   fs->bcbase[loopend-1].line = line;  /* Fix line for control ins. */
  2388.   fs->bcbase[loopend].line = line;
  2389.   jmp_patchins(fs, loopend, loop+1);
  2390. }

  2391. /* Parse 'for' statement. */
  2392. static void parse_for(LexState *ls, BCLine line)
  2393. {
  2394.   FuncState *fs = ls->fs;
  2395.   GCstr *varname;
  2396.   FuncScope bl;
  2397.   fscope_begin(fs, &bl, FSCOPE_LOOP);
  2398.   lj_lex_next(ls);  /* Skip 'for'. */
  2399.   varname = lex_str(ls);  /* Get first variable name. */
  2400.   if (ls->tok == '=')
  2401.     parse_for_num(ls, varname, line);
  2402.   else if (ls->tok == ',' || ls->tok == TK_in)
  2403.     parse_for_iter(ls, varname);
  2404.   else
  2405.     err_syntax(ls, LJ_ERR_XFOR);
  2406.   lex_match(ls, TK_end, TK_for, line);
  2407.   fscope_end(fs);  /* Resolve break list. */
  2408. }

  2409. /* Parse condition and 'then' block. */
  2410. static BCPos parse_then(LexState *ls)
  2411. {
  2412.   BCPos condexit;
  2413.   lj_lex_next(ls);  /* Skip 'if' or 'elseif'. */
  2414.   condexit = expr_cond(ls);
  2415.   lex_check(ls, TK_then);
  2416.   parse_block(ls);
  2417.   return condexit;
  2418. }

  2419. /* Parse 'if' statement. */
  2420. static void parse_if(LexState *ls, BCLine line)
  2421. {
  2422.   FuncState *fs = ls->fs;
  2423.   BCPos flist;
  2424.   BCPos escapelist = NO_JMP;
  2425.   flist = parse_then(ls);
  2426.   while (ls->tok == TK_elseif) {  /* Parse multiple 'elseif' blocks. */
  2427.     jmp_append(fs, &escapelist, bcemit_jmp(fs));
  2428.     jmp_tohere(fs, flist);
  2429.     flist = parse_then(ls);
  2430.   }
  2431.   if (ls->tok == TK_else) {  /* Parse optional 'else' block. */
  2432.     jmp_append(fs, &escapelist, bcemit_jmp(fs));
  2433.     jmp_tohere(fs, flist);
  2434.     lj_lex_next(ls);  /* Skip 'else'. */
  2435.     parse_block(ls);
  2436.   } else {
  2437.     jmp_append(fs, &escapelist, flist);
  2438.   }
  2439.   jmp_tohere(fs, escapelist);
  2440.   lex_match(ls, TK_end, TK_if, line);
  2441. }

  2442. /* -- Parse statements ---------------------------------------------------- */

  2443. /* Parse a statement. Returns 1 if it must be the last one in a chunk. */
  2444. static int parse_stmt(LexState *ls)
  2445. {
  2446.   BCLine line = ls->linenumber;
  2447.   switch (ls->tok) {
  2448.   case TK_if:
  2449.     parse_if(ls, line);
  2450.     break;
  2451.   case TK_while:
  2452.     parse_while(ls, line);
  2453.     break;
  2454.   case TK_do:
  2455.     lj_lex_next(ls);
  2456.     parse_block(ls);
  2457.     lex_match(ls, TK_end, TK_do, line);
  2458.     break;
  2459.   case TK_for:
  2460.     parse_for(ls, line);
  2461.     break;
  2462.   case TK_repeat:
  2463.     parse_repeat(ls, line);
  2464.     break;
  2465.   case TK_function:
  2466.     parse_func(ls, line);
  2467.     break;
  2468.   case TK_local:
  2469.     lj_lex_next(ls);
  2470.     parse_local(ls);
  2471.     break;
  2472.   case TK_return:
  2473.     parse_return(ls);
  2474.     return 1/* Must be last. */
  2475.   case TK_break:
  2476.     lj_lex_next(ls);
  2477.     parse_break(ls);
  2478.     return !LJ_52/* Must be last in Lua 5.1. */
  2479. #if LJ_52
  2480.   case ';':
  2481.     lj_lex_next(ls);
  2482.     break;
  2483. #endif
  2484.   case TK_label:
  2485.     parse_label(ls);
  2486.     break;
  2487.   case TK_goto:
  2488.     if (LJ_52 || lj_lex_lookahead(ls) == TK_name) {
  2489.       lj_lex_next(ls);
  2490.       parse_goto(ls);
  2491.       break;
  2492.     }  /* else: fallthrough */
  2493.   default:
  2494.     parse_call_assign(ls);
  2495.     break;
  2496.   }
  2497.   return 0;
  2498. }

  2499. /* A chunk is a list of statements optionally separated by semicolons. */
  2500. static void parse_chunk(LexState *ls)
  2501. {
  2502.   int islast = 0;
  2503.   synlevel_begin(ls);
  2504.   while (!islast && !parse_isend(ls->tok)) {
  2505.     islast = parse_stmt(ls);
  2506.     lex_opt(ls, ';');
  2507.     lua_assert(ls->fs->framesize >= ls->fs->freereg &&
  2508.                ls->fs->freereg >= ls->fs->nactvar);
  2509.     ls->fs->freereg = ls->fs->nactvar;  /* Free registers after each stmt. */
  2510.   }
  2511.   synlevel_end(ls);
  2512. }

  2513. /* Entry point of bytecode parser. */
  2514. GCproto *lj_parse(LexState *ls)
  2515. {
  2516.   FuncState fs;
  2517.   FuncScope bl;
  2518.   GCproto *pt;
  2519.   lua_State *L = ls->L;
  2520. #ifdef LUAJIT_DISABLE_DEBUGINFO
  2521.   ls->chunkname = lj_str_newlit(L, "=");
  2522. #else
  2523.   ls->chunkname = lj_str_newz(L, ls->chunkarg);
  2524. #endif
  2525.   setstrV(L, L->top, ls->chunkname);  /* Anchor chunkname string. */
  2526.   incr_top(L);
  2527.   ls->level = 0;
  2528.   fs_init(ls, &fs);
  2529.   fs.linedefined = 0;
  2530.   fs.numparams = 0;
  2531.   fs.bcbase = NULL;
  2532.   fs.bclim = 0;
  2533.   fs.flags |= PROTO_VARARG/* Main chunk is always a vararg func. */
  2534.   fscope_begin(&fs, &bl, 0);
  2535.   bcemit_AD(&fs, BC_FUNCV, 0, 0);  /* Placeholder. */
  2536.   lj_lex_next(ls);  /* Read-ahead first token. */
  2537.   parse_chunk(ls);
  2538.   if (ls->tok != TK_eof)
  2539.     err_token(ls, TK_eof);
  2540.   pt = fs_finish(ls, ls->linenumber);
  2541.   L->top--;  /* Drop chunkname. */
  2542.   lua_assert(fs.prev == NULL);
  2543.   lua_assert(ls->fs == NULL);
  2544.   lua_assert(pt->sizeuv == 0);
  2545.   return pt;
  2546. }