root/lj_parse.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. expr_init
  2. expr_numiszero
  3. err_syntax
  4. err_token
  5. err_limit
  6. const_num
  7. const_gc
  8. const_str
  9. lj_parse_keepstr
  10. lj_parse_keepcdata
  11. jmp_next
  12. jmp_novalue
  13. jmp_patchtestreg
  14. jmp_dropval
  15. jmp_patchins
  16. jmp_append
  17. jmp_patchval
  18. jmp_tohere
  19. jmp_patch
  20. bcreg_bump
  21. bcreg_reserve
  22. bcreg_free
  23. expr_free
  24. bcemit_INS
  25. expr_discharge
  26. bcemit_nil
  27. expr_toreg_nobranch
  28. expr_toreg
  29. expr_tonextreg
  30. expr_toanyreg
  31. expr_toval
  32. bcemit_store
  33. bcemit_method
  34. bcemit_jmp
  35. invertcond
  36. bcemit_branch
  37. bcemit_branch_t
  38. bcemit_branch_f
  39. foldarith
  40. bcemit_arith
  41. bcemit_comp
  42. bcemit_binop_left
  43. bcemit_binop
  44. bcemit_unop
  45. lex_opt
  46. lex_check
  47. lex_match
  48. lex_str
  49. var_new
  50. var_add
  51. var_remove
  52. var_lookup_local
  53. var_lookup_uv
  54. var_lookup_
  55. gola_new
  56. gola_patch
  57. gola_close
  58. gola_resolve
  59. gola_fixup
  60. gola_findlabel
  61. fscope_begin
  62. fscope_end
  63. fscope_uvmark
  64. fs_fixup_bc
  65. fs_fixup_uv2
  66. fs_fixup_k
  67. fs_fixup_uv1
  68. fs_prep_line
  69. fs_fixup_line
  70. fs_prep_var
  71. fs_fixup_var
  72. bcopisret
  73. fs_fixup_ret
  74. fs_finish
  75. fs_init
  76. expr_str
  77. expr_index
  78. expr_field
  79. expr_bracket
  80. expr_kvalue
  81. expr_table
  82. parse_params
  83. parse_body
  84. expr_list
  85. parse_args
  86. expr_primary
  87. expr_simple
  88. synlevel_begin
  89. token2binop
  90. expr_unop
  91. expr_binop
  92. expr
  93. expr_next
  94. expr_cond
  95. assign_hazard
  96. assign_adjust
  97. parse_assignment
  98. parse_call_assign
  99. parse_local
  100. parse_func
  101. parse_isend
  102. parse_return
  103. parse_break
  104. parse_goto
  105. parse_label
  106. parse_block
  107. parse_while
  108. parse_repeat
  109. parse_for_num
  110. predict_next
  111. parse_for_iter
  112. parse_for
  113. parse_then
  114. parse_if
  115. parse_stmt
  116. parse_chunk
  117. lj_parse

   1 /*
   2 ** Lua parser (source code -> bytecode).
   3 ** Copyright (C) 2005-2017 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 
   9 #define lj_parse_c
  10 #define LUA_CORE
  11 
  12 #include "lj_obj.h"
  13 #include "lj_gc.h"
  14 #include "lj_err.h"
  15 #include "lj_debug.h"
  16 #include "lj_buf.h"
  17 #include "lj_str.h"
  18 #include "lj_tab.h"
  19 #include "lj_func.h"
  20 #include "lj_state.h"
  21 #include "lj_bc.h"
  22 #if LJ_HASFFI
  23 #include "lj_ctype.h"
  24 #endif
  25 #include "lj_strfmt.h"
  26 #include "lj_lex.h"
  27 #include "lj_parse.h"
  28 #include "lj_vm.h"
  29 #include "lj_vmevent.h"
  30 
  31 /* -- Parser structures and definitions ----------------------------------- */
  32 
  33 /* Expression kinds. */
  34 typedef enum {
  35   /* Constant expressions must be first and in this order: */
  36   VKNIL,
  37   VKFALSE,
  38   VKTRUE,
  39   VKSTR,        /* sval = string value */
  40   VKNUM,        /* nval = number value */
  41   VKLAST = VKNUM,
  42   VKCDATA,      /* nval = cdata value, not treated as a constant expression */
  43   /* Non-constant expressions follow: */
  44   VLOCAL,       /* info = local register, aux = vstack index */
  45   VUPVAL,       /* info = upvalue index, aux = vstack index */
  46   VGLOBAL,      /* sval = string value */
  47   VINDEXED,     /* info = table register, aux = index reg/byte/string const */
  48   VJMP,         /* info = instruction PC */
  49   VRELOCABLE,   /* info = instruction PC */
  50   VNONRELOC,    /* info = result register */
  51   VCALL,        /* info = instruction PC, aux = base */
  52   VVOID
  53 } ExpKind;
  54 
  55 /* Expression descriptor. */
  56 typedef struct ExpDesc {
  57   union {
  58     struct {
  59       uint32_t info;    /* Primary info. */
  60       uint32_t aux;     /* Secondary info. */
  61     } s;
  62     TValue nval;        /* Number value. */
  63     GCstr *sval;        /* String value. */
  64   } u;
  65   ExpKind k;
  66   BCPos t;              /* True condition jump list. */
  67   BCPos f;              /* False condition jump list. */
  68 } ExpDesc;
  69 
  70 /* Macros for expressions. */
  71 #define expr_hasjump(e)         ((e)->t != (e)->f)
  72 
  73 #define expr_isk(e)             ((e)->k <= VKLAST)
  74 #define expr_isk_nojump(e)      (expr_isk(e) && !expr_hasjump(e))
  75 #define expr_isnumk(e)          ((e)->k == VKNUM)
  76 #define expr_isnumk_nojump(e)   (expr_isnumk(e) && !expr_hasjump(e))
  77 #define expr_isstrk(e)          ((e)->k == VKSTR)
  78 
  79 #define expr_numtv(e)           check_exp(expr_isnumk((e)), &(e)->u.nval)
  80 #define expr_numberV(e)         numberVnum(expr_numtv((e)))
  81 
  82 /* Initialize expression. */
  83 static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info)
  84 {
  85   e->k = k;
  86   e->u.s.info = info;
  87   e->f = e->t = NO_JMP;
  88 }
  89 
  90 /* Check number constant for +-0. */
  91 static int expr_numiszero(ExpDesc *e)
  92 {
  93   TValue *o = expr_numtv(e);
  94   return tvisint(o) ? (intV(o) == 0) : tviszero(o);
  95 }
  96 
  97 /* Per-function linked list of scope blocks. */
  98 typedef struct FuncScope {
  99   struct FuncScope *prev;       /* Link to outer scope. */
 100   MSize vstart;                 /* Start of block-local variables. */
 101   uint8_t nactvar;              /* Number of active vars outside the scope. */
 102   uint8_t flags;                /* Scope flags. */
 103 } FuncScope;
 104 
 105 #define FSCOPE_LOOP             0x01    /* Scope is a (breakable) loop. */
 106 #define FSCOPE_BREAK            0x02    /* Break used in scope. */
 107 #define FSCOPE_GOLA             0x04    /* Goto or label used in scope. */
 108 #define FSCOPE_UPVAL            0x08    /* Upvalue in scope. */
 109 #define FSCOPE_NOCLOSE          0x10    /* Do not close upvalues. */
 110 
 111 #define NAME_BREAK              ((GCstr *)(uintptr_t)1)
 112 
 113 /* Index into variable stack. */
 114 typedef uint16_t VarIndex;
 115 #define LJ_MAX_VSTACK           (65536 - LJ_MAX_UPVAL)
 116 
 117 /* Variable/goto/label info. */
 118 #define VSTACK_VAR_RW           0x01    /* R/W variable. */
 119 #define VSTACK_GOTO             0x02    /* Pending goto. */
 120 #define VSTACK_LABEL            0x04    /* Label. */
 121 
 122 /* Per-function state. */
 123 typedef struct FuncState {
 124   GCtab *kt;                    /* Hash table for constants. */
 125   LexState *ls;                 /* Lexer state. */
 126   lua_State *L;                 /* Lua state. */
 127   FuncScope *bl;                /* Current scope. */
 128   struct FuncState *prev;       /* Enclosing function. */
 129   BCPos pc;                     /* Next bytecode position. */
 130   BCPos lasttarget;             /* Bytecode position of last jump target. */
 131   BCPos jpc;                    /* Pending jump list to next bytecode. */
 132   BCReg freereg;                /* First free register. */
 133   BCReg nactvar;                /* Number of active local variables. */
 134   BCReg nkn, nkgc;              /* Number of lua_Number/GCobj constants */
 135   BCLine linedefined;           /* First line of the function definition. */
 136   BCInsLine *bcbase;            /* Base of bytecode stack. */
 137   BCPos bclim;                  /* Limit of bytecode stack. */
 138   MSize vbase;                  /* Base of variable stack for this function. */
 139   uint8_t flags;                /* Prototype flags. */
 140   uint8_t numparams;            /* Number of parameters. */
 141   uint8_t framesize;            /* Fixed frame size. */
 142   uint8_t nuv;                  /* Number of upvalues */
 143   VarIndex varmap[LJ_MAX_LOCVAR];  /* Map from register to variable idx. */
 144   VarIndex uvmap[LJ_MAX_UPVAL]; /* Map from upvalue to variable idx. */
 145   VarIndex uvtmp[LJ_MAX_UPVAL]; /* Temporary upvalue map. */
 146 } FuncState;
 147 
 148 /* Binary and unary operators. ORDER OPR */
 149 typedef enum BinOpr {
 150   OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,  /* ORDER ARITH */
 151   OPR_CONCAT,
 152   OPR_NE, OPR_EQ,
 153   OPR_LT, OPR_GE, OPR_LE, OPR_GT,
 154   OPR_AND, OPR_OR,
 155   OPR_NOBINOPR
 156 } BinOpr;
 157 
 158 LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT);
 159 LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT);
 160 LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT);
 161 LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD);
 162 LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);
 163 LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);
 164 LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);
 165 
 166 /* -- Error handling ------------------------------------------------------ */
 167 
 168 LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)
 169 {
 170   lj_lex_error(ls, ls->tok, em);
 171 }
 172 
 173 LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken tok)
 174 {
 175   lj_lex_error(ls, ls->tok, LJ_ERR_XTOKEN, lj_lex_token2str(ls, tok));
 176 }
 177 
 178 LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)
 179 {
 180   if (fs->linedefined == 0)
 181     lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what);
 182   else
 183     lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what);
 184 }
 185 
 186 #define checklimit(fs, v, l, m)         if ((v) >= (l)) err_limit(fs, l, m)
 187 #define checklimitgt(fs, v, l, m)       if ((v) > (l)) err_limit(fs, l, m)
 188 #define checkcond(ls, c, em)            { if (!(c)) err_syntax(ls, em); }
 189 
 190 /* -- Management of constants --------------------------------------------- */
 191 
 192 /* Return bytecode encoding for primitive constant. */
 193 #define const_pri(e)            check_exp((e)->k <= VKTRUE, (e)->k)
 194 
 195 #define tvhaskslot(o)   ((o)->u32.hi == 0)
 196 #define tvkslot(o)      ((o)->u32.lo)
 197 
 198 /* Add a number constant. */
 199 static BCReg const_num(FuncState *fs, ExpDesc *e)
 200 {
 201   lua_State *L = fs->L;
 202   TValue *o;
 203   lua_assert(expr_isnumk(e));
 204   o = lj_tab_set(L, fs->kt, &e->u.nval);
 205   if (tvhaskslot(o))
 206     return tvkslot(o);
 207   o->u64 = fs->nkn;
 208   return fs->nkn++;
 209 }
 210 
 211 /* Add a GC object constant. */
 212 static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)
 213 {
 214   lua_State *L = fs->L;
 215   TValue key, *o;
 216   setgcV(L, &key, gc, itype);
 217   /* NOBARRIER: the key is new or kept alive. */
 218   o = lj_tab_set(L, fs->kt, &key);
 219   if (tvhaskslot(o))
 220     return tvkslot(o);
 221   o->u64 = fs->nkgc;
 222   return fs->nkgc++;
 223 }
 224 
 225 /* Add a string constant. */
 226 static BCReg const_str(FuncState *fs, ExpDesc *e)
 227 {
 228   lua_assert(expr_isstrk(e) || e->k == VGLOBAL);
 229   return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);
 230 }
 231 
 232 /* Anchor string constant to avoid GC. */
 233 GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len)
 234 {
 235   /* NOBARRIER: the key is new or kept alive. */
 236   lua_State *L = ls->L;
 237   GCstr *s = lj_str_new(L, str, len);
 238   TValue *tv = lj_tab_setstr(L, ls->fs->kt, s);
 239   if (tvisnil(tv)) setboolV(tv, 1);
 240   lj_gc_check(L);
 241   return s;
 242 }
 243 
 244 #if LJ_HASFFI
 245 /* Anchor cdata to avoid GC. */
 246 void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd)
 247 {
 248   /* NOBARRIER: the key is new or kept alive. */
 249   lua_State *L = ls->L;
 250   setcdataV(L, tv, cd);
 251   setboolV(lj_tab_set(L, ls->fs->kt, tv), 1);
 252 }
 253 #endif
 254 
 255 /* -- Jump list handling -------------------------------------------------- */
 256 
 257 /* Get next element in jump list. */
 258 static BCPos jmp_next(FuncState *fs, BCPos pc)
 259 {
 260   ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);
 261   if ((BCPos)delta == NO_JMP)
 262     return NO_JMP;
 263   else
 264     return (BCPos)(((ptrdiff_t)pc+1)+delta);
 265 }
 266 
 267 /* Check if any of the instructions on the jump list produce no value. */
 268 static int jmp_novalue(FuncState *fs, BCPos list)
 269 {
 270   for (; list != NO_JMP; list = jmp_next(fs, list)) {
 271     BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins;
 272     if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG))
 273       return 1;
 274   }
 275   return 0;
 276 }
 277 
 278 /* Patch register of test instructions. */
 279 static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg)
 280 {
 281   BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc];
 282   BCOp op = bc_op(ilp->ins);
 283   if (op == BC_ISTC || op == BC_ISFC) {
 284     if (reg != NO_REG && reg != bc_d(ilp->ins)) {
 285       setbc_a(&ilp->ins, reg);
 286     } else {  /* Nothing to store or already in the right register. */
 287       setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC));
 288       setbc_a(&ilp->ins, 0);
 289     }
 290   } else if (bc_a(ilp->ins) == NO_REG) {
 291     if (reg == NO_REG) {
 292       ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0);
 293     } else {
 294       setbc_a(&ilp->ins, reg);
 295       if (reg >= bc_a(ilp[1].ins))
 296         setbc_a(&ilp[1].ins, reg+1);
 297     }
 298   } else {
 299     return 0;  /* Cannot patch other instructions. */
 300   }
 301   return 1;
 302 }
 303 
 304 /* Drop values for all instructions on jump list. */
 305 static void jmp_dropval(FuncState *fs, BCPos list)
 306 {
 307   for (; list != NO_JMP; list = jmp_next(fs, list))
 308     jmp_patchtestreg(fs, list, NO_REG);
 309 }
 310 
 311 /* Patch jump instruction to target. */
 312 static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)
 313 {
 314   BCIns *jmp = &fs->bcbase[pc].ins;
 315   BCPos offset = dest-(pc+1)+BCBIAS_J;
 316   lua_assert(dest != NO_JMP);
 317   if (offset > BCMAX_D)
 318     err_syntax(fs->ls, LJ_ERR_XJUMP);
 319   setbc_d(jmp, offset);
 320 }
 321 
 322 /* Append to jump list. */
 323 static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)
 324 {
 325   if (l2 == NO_JMP) {
 326     return;
 327   } else if (*l1 == NO_JMP) {
 328     *l1 = l2;
 329   } else {
 330     BCPos list = *l1;
 331     BCPos next;
 332     while ((next = jmp_next(fs, list)) != NO_JMP)  /* Find last element. */
 333       list = next;
 334     jmp_patchins(fs, list, l2);
 335   }
 336 }
 337 
 338 /* Patch jump list and preserve produced values. */
 339 static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,
 340                          BCReg reg, BCPos dtarget)
 341 {
 342   while (list != NO_JMP) {
 343     BCPos next = jmp_next(fs, list);
 344     if (jmp_patchtestreg(fs, list, reg))
 345       jmp_patchins(fs, list, vtarget);  /* Jump to target with value. */
 346     else
 347       jmp_patchins(fs, list, dtarget);  /* Jump to default target. */
 348     list = next;
 349   }
 350 }
 351 
 352 /* Jump to following instruction. Append to list of pending jumps. */
 353 static void jmp_tohere(FuncState *fs, BCPos list)
 354 {
 355   fs->lasttarget = fs->pc;
 356   jmp_append(fs, &fs->jpc, list);
 357 }
 358 
 359 /* Patch jump list to target. */
 360 static void jmp_patch(FuncState *fs, BCPos list, BCPos target)
 361 {
 362   if (target == fs->pc) {
 363     jmp_tohere(fs, list);
 364   } else {
 365     lua_assert(target < fs->pc);
 366     jmp_patchval(fs, list, target, NO_REG, target);
 367   }
 368 }
 369 
 370 /* -- Bytecode register allocator ----------------------------------------- */
 371 
 372 /* Bump frame size. */
 373 static void bcreg_bump(FuncState *fs, BCReg n)
 374 {
 375   BCReg sz = fs->freereg + n;
 376   if (sz > fs->framesize) {
 377     if (sz >= LJ_MAX_SLOTS)
 378       err_syntax(fs->ls, LJ_ERR_XSLOTS);
 379     fs->framesize = (uint8_t)sz;
 380   }
 381 }
 382 
 383 /* Reserve registers. */
 384 static void bcreg_reserve(FuncState *fs, BCReg n)
 385 {
 386   bcreg_bump(fs, n);
 387   fs->freereg += n;
 388 }
 389 
 390 /* Free register. */
 391 static void bcreg_free(FuncState *fs, BCReg reg)
 392 {
 393   if (reg >= fs->nactvar) {
 394     fs->freereg--;
 395     lua_assert(reg == fs->freereg);
 396   }
 397 }
 398 
 399 /* Free register for expression. */
 400 static void expr_free(FuncState *fs, ExpDesc *e)
 401 {
 402   if (e->k == VNONRELOC)
 403     bcreg_free(fs, e->u.s.info);
 404 }
 405 
 406 /* -- Bytecode emitter ---------------------------------------------------- */
 407 
 408 /* Emit bytecode instruction. */
 409 static BCPos bcemit_INS(FuncState *fs, BCIns ins)
 410 {
 411   BCPos pc = fs->pc;
 412   LexState *ls = fs->ls;
 413   jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);
 414   fs->jpc = NO_JMP;
 415   if (LJ_UNLIKELY(pc >= fs->bclim)) {
 416     ptrdiff_t base = fs->bcbase - ls->bcstack;
 417     checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");
 418     lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);
 419     fs->bclim = (BCPos)(ls->sizebcstack - base);
 420     fs->bcbase = ls->bcstack + base;
 421   }
 422   fs->bcbase[pc].ins = ins;
 423   fs->bcbase[pc].line = ls->lastline;
 424   fs->pc = pc+1;
 425   return pc;
 426 }
 427 
 428 #define bcemit_ABC(fs, o, a, b, c)      bcemit_INS(fs, BCINS_ABC(o, a, b, c))
 429 #define bcemit_AD(fs, o, a, d)          bcemit_INS(fs, BCINS_AD(o, a, d))
 430 #define bcemit_AJ(fs, o, a, j)          bcemit_INS(fs, BCINS_AJ(o, a, j))
 431 
 432 #define bcptr(fs, e)                    (&(fs)->bcbase[(e)->u.s.info].ins)
 433 
 434 /* -- Bytecode emitter for expressions ------------------------------------ */
 435 
 436 /* Discharge non-constant expression to any register. */
 437 static void expr_discharge(FuncState *fs, ExpDesc *e)
 438 {
 439   BCIns ins;
 440   if (e->k == VUPVAL) {
 441     ins = BCINS_AD(BC_UGET, 0, e->u.s.info);
 442   } else if (e->k == VGLOBAL) {
 443     ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));
 444   } else if (e->k == VINDEXED) {
 445     BCReg rc = e->u.s.aux;
 446     if ((int32_t)rc < 0) {
 447       ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);
 448     } else if (rc > BCMAX_C) {
 449       ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));
 450     } else {
 451       bcreg_free(fs, rc);
 452       ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);
 453     }
 454     bcreg_free(fs, e->u.s.info);
 455   } else if (e->k == VCALL) {
 456     e->u.s.info = e->u.s.aux;
 457     e->k = VNONRELOC;
 458     return;
 459   } else if (e->k == VLOCAL) {
 460     e->k = VNONRELOC;
 461     return;
 462   } else {
 463     return;
 464   }
 465   e->u.s.info = bcemit_INS(fs, ins);
 466   e->k = VRELOCABLE;
 467 }
 468 
 469 /* Emit bytecode to set a range of registers to nil. */
 470 static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)
 471 {
 472   if (fs->pc > fs->lasttarget) {  /* No jumps to current position? */
 473     BCIns *ip = &fs->bcbase[fs->pc-1].ins;
 474     BCReg pto, pfrom = bc_a(*ip);
 475     switch (bc_op(*ip)) {  /* Try to merge with the previous instruction. */
 476     case BC_KPRI:
 477       if (bc_d(*ip) != ~LJ_TNIL) break;
 478       if (from == pfrom) {
 479         if (n == 1) return;
 480       } else if (from == pfrom+1) {
 481         from = pfrom;
 482         n++;
 483       } else {
 484         break;
 485       }
 486       *ip = BCINS_AD(BC_KNIL, from, from+n-1);  /* Replace KPRI. */
 487       return;
 488     case BC_KNIL:
 489       pto = bc_d(*ip);
 490       if (pfrom <= from && from <= pto+1) {  /* Can we connect both ranges? */
 491         if (from+n-1 > pto)
 492           setbc_d(ip, from+n-1);  /* Patch previous instruction range. */
 493         return;
 494       }
 495       break;
 496     default:
 497       break;
 498     }
 499   }
 500   /* Emit new instruction or replace old instruction. */
 501   bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :
 502                           BCINS_AD(BC_KNIL, from, from+n-1));
 503 }
 504 
 505 /* Discharge an expression to a specific register. Ignore branches. */
 506 static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)
 507 {
 508   BCIns ins;
 509   expr_discharge(fs, e);
 510   if (e->k == VKSTR) {
 511     ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));
 512   } else if (e->k == VKNUM) {
 513 #if LJ_DUALNUM
 514     cTValue *tv = expr_numtv(e);
 515     if (tvisint(tv) && checki16(intV(tv)))
 516       ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv));
 517     else
 518 #else
 519     lua_Number n = expr_numberV(e);
 520     int32_t k = lj_num2int(n);
 521     if (checki16(k) && n == (lua_Number)k)
 522       ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);
 523     else
 524 #endif
 525       ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));
 526 #if LJ_HASFFI
 527   } else if (e->k == VKCDATA) {
 528     fs->flags |= PROTO_FFI;
 529     ins = BCINS_AD(BC_KCDATA, reg,
 530                    const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));
 531 #endif
 532   } else if (e->k == VRELOCABLE) {
 533     setbc_a(bcptr(fs, e), reg);
 534     goto noins;
 535   } else if (e->k == VNONRELOC) {
 536     if (reg == e->u.s.info)
 537       goto noins;
 538     ins = BCINS_AD(BC_MOV, reg, e->u.s.info);
 539   } else if (e->k == VKNIL) {
 540     bcemit_nil(fs, reg, 1);
 541     goto noins;
 542   } else if (e->k <= VKTRUE) {
 543     ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
 544   } else {
 545     lua_assert(e->k == VVOID || e->k == VJMP);
 546     return;
 547   }
 548   bcemit_INS(fs, ins);
 549 noins:
 550   e->u.s.info = reg;
 551   e->k = VNONRELOC;
 552 }
 553 
 554 /* Forward declaration. */
 555 static BCPos bcemit_jmp(FuncState *fs);
 556 
 557 /* Discharge an expression to a specific register. */
 558 static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg)
 559 {
 560   expr_toreg_nobranch(fs, e, reg);
 561   if (e->k == VJMP)
 562     jmp_append(fs, &e->t, e->u.s.info);  /* Add it to the true jump list. */
 563   if (expr_hasjump(e)) {  /* Discharge expression with branches. */
 564     BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;
 565     if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {
 566       BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);
 567       jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);
 568       bcemit_AJ(fs, BC_JMP, fs->freereg, 1);
 569       jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);
 570       jmp_tohere(fs, jval);
 571     }
 572     jend = fs->pc;
 573     fs->lasttarget = jend;
 574     jmp_patchval(fs, e->f, jend, reg, jfalse);
 575     jmp_patchval(fs, e->t, jend, reg, jtrue);
 576   }
 577   e->f = e->t = NO_JMP;
 578   e->u.s.info = reg;
 579   e->k = VNONRELOC;
 580 }
 581 
 582 /* Discharge an expression to the next free register. */
 583 static void expr_tonextreg(FuncState *fs, ExpDesc *e)
 584 {
 585   expr_discharge(fs, e);
 586   expr_free(fs, e);
 587   bcreg_reserve(fs, 1);
 588   expr_toreg(fs, e, fs->freereg - 1);
 589 }
 590 
 591 /* Discharge an expression to any register. */
 592 static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)
 593 {
 594   expr_discharge(fs, e);
 595   if (e->k == VNONRELOC) {
 596     if (!expr_hasjump(e)) return e->u.s.info;  /* Already in a register. */
 597     if (e->u.s.info >= fs->nactvar) {
 598       expr_toreg(fs, e, e->u.s.info);  /* Discharge to temp. register. */
 599       return e->u.s.info;
 600     }
 601   }
 602   expr_tonextreg(fs, e);  /* Discharge to next register. */
 603   return e->u.s.info;
 604 }
 605 
 606 /* Partially discharge expression to a value. */
 607 static void expr_toval(FuncState *fs, ExpDesc *e)
 608 {
 609   if (expr_hasjump(e))
 610     expr_toanyreg(fs, e);
 611   else
 612     expr_discharge(fs, e);
 613 }
 614 
 615 /* Emit store for LHS expression. */
 616 static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
 617 {
 618   BCIns ins;
 619   if (var->k == VLOCAL) {
 620     fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
 621     expr_free(fs, e);
 622     expr_toreg(fs, e, var->u.s.info);
 623     return;
 624   } else if (var->k == VUPVAL) {
 625     fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
 626     expr_toval(fs, e);
 627     if (e->k <= VKTRUE)
 628       ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
 629     else if (e->k == VKSTR)
 630       ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));
 631     else if (e->k == VKNUM)
 632       ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));
 633     else
 634       ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));
 635   } else if (var->k == VGLOBAL) {
 636     BCReg ra = expr_toanyreg(fs, e);
 637     ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
 638   } else {
 639     BCReg ra, rc;
 640     lua_assert(var->k == VINDEXED);
 641     ra = expr_toanyreg(fs, e);
 642     rc = var->u.s.aux;
 643     if ((int32_t)rc < 0) {
 644       ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);
 645     } else if (rc > BCMAX_C) {
 646       ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
 647     } else {
 648       /* Free late alloced key reg to avoid assert on free of value reg. */
 649       /* This can only happen when called from expr_table(). */
 650       lua_assert(e->k != VNONRELOC || ra < fs->nactvar ||
 651                  rc < ra || (bcreg_free(fs, rc),1));
 652       ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);
 653     }
 654   }
 655   bcemit_INS(fs, ins);
 656   expr_free(fs, e);
 657 }
 658 
 659 /* Emit method lookup expression. */
 660 static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
 661 {
 662   BCReg idx, func, obj = expr_toanyreg(fs, e);
 663   expr_free(fs, e);
 664   func = fs->freereg;
 665   bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj);  /* Copy object to 1st argument. */
 666   lua_assert(expr_isstrk(key));
 667   idx = const_str(fs, key);
 668   if (idx <= BCMAX_C) {
 669     bcreg_reserve(fs, 2+LJ_FR2);
 670     bcemit_ABC(fs, BC_TGETS, func, obj, idx);
 671   } else {
 672     bcreg_reserve(fs, 3+LJ_FR2);
 673     bcemit_AD(fs, BC_KSTR, func+2+LJ_FR2, idx);
 674     bcemit_ABC(fs, BC_TGETV, func, obj, func+2+LJ_FR2);
 675     fs->freereg--;
 676   }
 677   e->u.s.info = func;
 678   e->k = VNONRELOC;
 679 }
 680 
 681 /* -- Bytecode emitter for branches --------------------------------------- */
 682 
 683 /* Emit unconditional branch. */
 684 static BCPos bcemit_jmp(FuncState *fs)
 685 {
 686   BCPos jpc = fs->jpc;
 687   BCPos j = fs->pc - 1;
 688   BCIns *ip = &fs->bcbase[j].ins;
 689   fs->jpc = NO_JMP;
 690   if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) {
 691     setbc_j(ip, NO_JMP);
 692     fs->lasttarget = j+1;
 693   } else {
 694     j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP);
 695   }
 696   jmp_append(fs, &j, jpc);
 697   return j;
 698 }
 699 
 700 /* Invert branch condition of bytecode instruction. */
 701 static void invertcond(FuncState *fs, ExpDesc *e)
 702 {
 703   BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins;
 704   setbc_op(ip, bc_op(*ip)^1);
 705 }
 706 
 707 /* Emit conditional branch. */
 708 static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)
 709 {
 710   BCPos pc;
 711   if (e->k == VRELOCABLE) {
 712     BCIns *ip = bcptr(fs, e);
 713     if (bc_op(*ip) == BC_NOT) {
 714       *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));
 715       return bcemit_jmp(fs);
 716     }
 717   }
 718   if (e->k != VNONRELOC) {
 719     bcreg_reserve(fs, 1);
 720     expr_toreg_nobranch(fs, e, fs->freereg-1);
 721   }
 722   bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);
 723   pc = bcemit_jmp(fs);
 724   expr_free(fs, e);
 725   return pc;
 726 }
 727 
 728 /* Emit branch on true condition. */
 729 static void bcemit_branch_t(FuncState *fs, ExpDesc *e)
 730 {
 731   BCPos pc;
 732   expr_discharge(fs, e);
 733   if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
 734     pc = NO_JMP;  /* Never jump. */
 735   else if (e->k == VJMP)
 736     invertcond(fs, e), pc = e->u.s.info;
 737   else if (e->k == VKFALSE || e->k == VKNIL)
 738     expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
 739   else
 740     pc = bcemit_branch(fs, e, 0);
 741   jmp_append(fs, &e->f, pc);
 742   jmp_tohere(fs, e->t);
 743   e->t = NO_JMP;
 744 }
 745 
 746 /* Emit branch on false condition. */
 747 static void bcemit_branch_f(FuncState *fs, ExpDesc *e)
 748 {
 749   BCPos pc;
 750   expr_discharge(fs, e);
 751   if (e->k == VKNIL || e->k == VKFALSE)
 752     pc = NO_JMP;  /* Never jump. */
 753   else if (e->k == VJMP)
 754     pc = e->u.s.info;
 755   else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
 756     expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
 757   else
 758     pc = bcemit_branch(fs, e, 1);
 759   jmp_append(fs, &e->t, pc);
 760   jmp_tohere(fs, e->f);
 761   e->f = NO_JMP;
 762 }
 763 
 764 /* -- Bytecode emitter for operators -------------------------------------- */
 765 
 766 /* Try constant-folding of arithmetic operators. */
 767 static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)
 768 {
 769   TValue o;
 770   lua_Number n;
 771   if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;
 772   n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);
 773   setnumV(&o, n);
 774   if (tvisnan(&o) || tvismzero(&o)) return 0;  /* Avoid NaN and -0 as consts. */
 775   if (LJ_DUALNUM) {
 776     int32_t k = lj_num2int(n);
 777     if ((lua_Number)k == n) {
 778       setintV(&e1->u.nval, k);
 779       return 1;
 780     }
 781   }
 782   setnumV(&e1->u.nval, n);
 783   return 1;
 784 }
 785 
 786 /* Emit arithmetic operator. */
 787 static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
 788 {
 789   BCReg rb, rc, t;
 790   uint32_t op;
 791   if (foldarith(opr, e1, e2))
 792     return;
 793   if (opr == OPR_POW) {
 794     op = BC_POW;
 795     rc = expr_toanyreg(fs, e2);
 796     rb = expr_toanyreg(fs, e1);
 797   } else {
 798     op = opr-OPR_ADD+BC_ADDVV;
 799     /* Must discharge 2nd operand first since VINDEXED might free regs. */
 800     expr_toval(fs, e2);
 801     if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)
 802       op -= BC_ADDVV-BC_ADDVN;
 803     else
 804       rc = expr_toanyreg(fs, e2);
 805     /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
 806     lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC);
 807     expr_toval(fs, e1);
 808     /* Avoid two consts to satisfy bytecode constraints. */
 809     if (expr_isnumk(e1) && !expr_isnumk(e2) &&
 810         (t = const_num(fs, e1)) <= BCMAX_B) {
 811       rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;
 812     } else {
 813       rb = expr_toanyreg(fs, e1);
 814     }
 815   }
 816   /* Using expr_free might cause asserts if the order is wrong. */
 817   if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
 818   if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
 819   e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);
 820   e1->k = VRELOCABLE;
 821 }
 822 
 823 /* Emit comparison operator. */
 824 static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
 825 {
 826   ExpDesc *eret = e1;
 827   BCIns ins;
 828   expr_toval(fs, e1);
 829   if (opr == OPR_EQ || opr == OPR_NE) {
 830     BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;
 831     BCReg ra;
 832     if (expr_isk(e1)) { e1 = e2; e2 = eret; }  /* Need constant in 2nd arg. */
 833     ra = expr_toanyreg(fs, e1);  /* First arg must be in a reg. */
 834     expr_toval(fs, e2);
 835     switch (e2->k) {
 836     case VKNIL: case VKFALSE: case VKTRUE:
 837       ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));
 838       break;
 839     case VKSTR:
 840       ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));
 841       break;
 842     case VKNUM:
 843       ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));
 844       break;
 845     default:
 846       ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));
 847       break;
 848     }
 849   } else {
 850     uint32_t op = opr-OPR_LT+BC_ISLT;
 851     BCReg ra, rd;
 852     if ((op-BC_ISLT) & 1) {  /* GT -> LT, GE -> LE */
 853       e1 = e2; e2 = eret;  /* Swap operands. */
 854       op = ((op-BC_ISLT)^3)+BC_ISLT;
 855       expr_toval(fs, e1);
 856     }
 857     rd = expr_toanyreg(fs, e2);
 858     ra = expr_toanyreg(fs, e1);
 859     ins = BCINS_AD(op, ra, rd);
 860   }
 861   /* Using expr_free might cause asserts if the order is wrong. */
 862   if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
 863   if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
 864   bcemit_INS(fs, ins);
 865   eret->u.s.info = bcemit_jmp(fs);
 866   eret->k = VJMP;
 867 }
 868 
 869 /* Fixup left side of binary operator. */
 870 static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)
 871 {
 872   if (op == OPR_AND) {
 873     bcemit_branch_t(fs, e);
 874   } else if (op == OPR_OR) {
 875     bcemit_branch_f(fs, e);
 876   } else if (op == OPR_CONCAT) {
 877     expr_tonextreg(fs, e);
 878   } else if (op == OPR_EQ || op == OPR_NE) {
 879     if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);
 880   } else {
 881     if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);
 882   }
 883 }
 884 
 885 /* Emit binary operator. */
 886 static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
 887 {
 888   if (op <= OPR_POW) {
 889     bcemit_arith(fs, op, e1, e2);
 890   } else if (op == OPR_AND) {
 891     lua_assert(e1->t == NO_JMP);  /* List must be closed. */
 892     expr_discharge(fs, e2);
 893     jmp_append(fs, &e2->f, e1->f);
 894     *e1 = *e2;
 895   } else if (op == OPR_OR) {
 896     lua_assert(e1->f == NO_JMP);  /* List must be closed. */
 897     expr_discharge(fs, e2);
 898     jmp_append(fs, &e2->t, e1->t);
 899     *e1 = *e2;
 900   } else if (op == OPR_CONCAT) {
 901     expr_toval(fs, e2);
 902     if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
 903       lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1);
 904       expr_free(fs, e1);
 905       setbc_b(bcptr(fs, e2), e1->u.s.info);
 906       e1->u.s.info = e2->u.s.info;
 907     } else {
 908       expr_tonextreg(fs, e2);
 909       expr_free(fs, e2);
 910       expr_free(fs, e1);
 911       e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);
 912     }
 913     e1->k = VRELOCABLE;
 914   } else {
 915     lua_assert(op == OPR_NE || op == OPR_EQ ||
 916                op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT);
 917     bcemit_comp(fs, op, e1, e2);
 918   }
 919 }
 920 
 921 /* Emit unary operator. */
 922 static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
 923 {
 924   if (op == BC_NOT) {
 925     /* Swap true and false lists. */
 926     { BCPos temp = e->f; e->f = e->t; e->t = temp; }
 927     jmp_dropval(fs, e->f);
 928     jmp_dropval(fs, e->t);
 929     expr_discharge(fs, e);
 930     if (e->k == VKNIL || e->k == VKFALSE) {
 931       e->k = VKTRUE;
 932       return;
 933     } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {
 934       e->k = VKFALSE;
 935       return;
 936     } else if (e->k == VJMP) {
 937       invertcond(fs, e);
 938       return;
 939     } else if (e->k == VRELOCABLE) {
 940       bcreg_reserve(fs, 1);
 941       setbc_a(bcptr(fs, e), fs->freereg-1);
 942       e->u.s.info = fs->freereg-1;
 943       e->k = VNONRELOC;
 944     } else {
 945       lua_assert(e->k == VNONRELOC);
 946     }
 947   } else {
 948     lua_assert(op == BC_UNM || op == BC_LEN);
 949     if (op == BC_UNM && !expr_hasjump(e)) {  /* Constant-fold negations. */
 950 #if LJ_HASFFI
 951       if (e->k == VKCDATA) {  /* Fold in-place since cdata is not interned. */
 952         GCcdata *cd = cdataV(&e->u.nval);
 953         int64_t *p = (int64_t *)cdataptr(cd);
 954         if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
 955           p[1] ^= (int64_t)U64x(80000000,00000000);
 956         else
 957           *p = -*p;
 958         return;
 959       } else
 960 #endif
 961       if (expr_isnumk(e) && !expr_numiszero(e)) {  /* Avoid folding to -0. */
 962         TValue *o = expr_numtv(e);
 963         if (tvisint(o)) {
 964           int32_t k = intV(o);
 965           if (k == -k)
 966             setnumV(o, -(lua_Number)k);
 967           else
 968             setintV(o, -k);
 969           return;
 970         } else {
 971           o->u64 ^= U64x(80000000,00000000);
 972           return;
 973         }
 974       }
 975     }
 976     expr_toanyreg(fs, e);
 977   }
 978   expr_free(fs, e);
 979   e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);
 980   e->k = VRELOCABLE;
 981 }
 982 
 983 /* -- Lexer support ------------------------------------------------------- */
 984 
 985 /* Check and consume optional token. */
 986 static int lex_opt(LexState *ls, LexToken tok)
 987 {
 988   if (ls->tok == tok) {
 989     lj_lex_next(ls);
 990     return 1;
 991   }
 992   return 0;
 993 }
 994 
 995 /* Check and consume token. */
 996 static void lex_check(LexState *ls, LexToken tok)
 997 {
 998   if (ls->tok != tok)
 999     err_token(ls, tok);
1000   lj_lex_next(ls);
1001 }
1002 
1003 /* Check for matching token. */
1004 static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)
1005 {
1006   if (!lex_opt(ls, what)) {
1007     if (line == ls->linenumber) {
1008       err_token(ls, what);
1009     } else {
1010       const char *swhat = lj_lex_token2str(ls, what);
1011       const char *swho = lj_lex_token2str(ls, who);
1012       lj_lex_error(ls, ls->tok, LJ_ERR_XMATCH, swhat, swho, line);
1013     }
1014   }
1015 }
1016 
1017 /* Check for string token. */
1018 static GCstr *lex_str(LexState *ls)
1019 {
1020   GCstr *s;
1021   if (ls->tok != TK_name && (LJ_52 || ls->tok != TK_goto))
1022     err_token(ls, TK_name);
1023   s = strV(&ls->tokval);
1024   lj_lex_next(ls);
1025   return s;
1026 }
1027 
1028 /* -- Variable handling --------------------------------------------------- */
1029 
1030 #define var_get(ls, fs, i)      ((ls)->vstack[(fs)->varmap[(i)]])
1031 
1032 /* Define a new local variable. */
1033 static void var_new(LexState *ls, BCReg n, GCstr *name)
1034 {
1035   FuncState *fs = ls->fs;
1036   MSize vtop = ls->vtop;
1037   checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables");
1038   if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
1039     if (ls->sizevstack >= LJ_MAX_VSTACK)
1040       lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
1041     lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
1042   }
1043   lua_assert((uintptr_t)name < VARNAME__MAX ||
1044              lj_tab_getstr(fs->kt, name) != NULL);
1045   /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1046   setgcref(ls->vstack[vtop].name, obj2gco(name));
1047   fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
1048   ls->vtop = vtop+1;
1049 }
1050 
1051 #define var_new_lit(ls, n, v) \
1052   var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1))
1053 
1054 #define var_new_fixed(ls, n, vn) \
1055   var_new(ls, (n), (GCstr *)(uintptr_t)(vn))
1056 
1057 /* Add local variables. */
1058 static void var_add(LexState *ls, BCReg nvars)
1059 {
1060   FuncState *fs = ls->fs;
1061   BCReg nactvar = fs->nactvar;
1062   while (nvars--) {
1063     VarInfo *v = &var_get(ls, fs, nactvar);
1064     v->startpc = fs->pc;
1065     v->slot = nactvar++;
1066     v->info = 0;
1067   }
1068   fs->nactvar = nactvar;
1069 }
1070 
1071 /* Remove local variables. */
1072 static void var_remove(LexState *ls, BCReg tolevel)
1073 {
1074   FuncState *fs = ls->fs;
1075   while (fs->nactvar > tolevel)
1076     var_get(ls, fs, --fs->nactvar).endpc = fs->pc;
1077 }
1078 
1079 /* Lookup local variable name. */
1080 static BCReg var_lookup_local(FuncState *fs, GCstr *n)
1081 {
1082   int i;
1083   for (i = fs->nactvar-1; i >= 0; i--) {
1084     if (n == strref(var_get(fs->ls, fs, i).name))
1085       return (BCReg)i;
1086   }
1087   return (BCReg)-1;  /* Not found. */
1088 }
1089 
1090 /* Lookup or add upvalue index. */
1091 static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)
1092 {
1093   MSize i, n = fs->nuv;
1094   for (i = 0; i < n; i++)
1095     if (fs->uvmap[i] == vidx)
1096       return i;  /* Already exists. */
1097   /* Otherwise create a new one. */
1098   checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");
1099   lua_assert(e->k == VLOCAL || e->k == VUPVAL);
1100   fs->uvmap[n] = (uint16_t)vidx;
1101   fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info);
1102   fs->nuv = n+1;
1103   return n;
1104 }
1105 
1106 /* Forward declaration. */
1107 static void fscope_uvmark(FuncState *fs, BCReg level);
1108 
1109 /* Recursively lookup variables in enclosing functions. */
1110 static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)
1111 {
1112   if (fs) {
1113     BCReg reg = var_lookup_local(fs, name);
1114     if ((int32_t)reg >= 0) {  /* Local in this function? */
1115       expr_init(e, VLOCAL, reg);
1116       if (!first)
1117         fscope_uvmark(fs, reg);  /* Scope now has an upvalue. */
1118       return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);
1119     } else {
1120       MSize vidx = var_lookup_(fs->prev, name, e, 0);  /* Var in outer func? */
1121       if ((int32_t)vidx >= 0) {  /* Yes, make it an upvalue here. */
1122         e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e);
1123         e->k = VUPVAL;
1124         return vidx;
1125       }
1126     }
1127   } else {  /* Not found in any function, must be a global. */
1128     expr_init(e, VGLOBAL, 0);
1129     e->u.sval = name;
1130   }
1131   return (MSize)-1;  /* Global. */
1132 }
1133 
1134 /* Lookup variable name. */
1135 #define var_lookup(ls, e) \
1136   var_lookup_((ls)->fs, lex_str(ls), (e), 1)
1137 
1138 /* -- Goto an label handling ---------------------------------------------- */
1139 
1140 /* Add a new goto or label. */
1141 static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc)
1142 {
1143   FuncState *fs = ls->fs;
1144   MSize vtop = ls->vtop;
1145   if (LJ_UNLIKELY(vtop >= ls->sizevstack)) {
1146     if (ls->sizevstack >= LJ_MAX_VSTACK)
1147       lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
1148     lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
1149   }
1150   lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL);
1151   /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1152   setgcref(ls->vstack[vtop].name, obj2gco(name));
1153   ls->vstack[vtop].startpc = pc;
1154   ls->vstack[vtop].slot = (uint8_t)fs->nactvar;
1155   ls->vstack[vtop].info = info;
1156   ls->vtop = vtop+1;
1157   return vtop;
1158 }
1159 
1160 #define gola_isgoto(v)          ((v)->info & VSTACK_GOTO)
1161 #define gola_islabel(v)         ((v)->info & VSTACK_LABEL)
1162 #define gola_isgotolabel(v)     ((v)->info & (VSTACK_GOTO|VSTACK_LABEL))
1163 
1164 /* Patch goto to jump to label. */
1165 static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl)
1166 {
1167   FuncState *fs = ls->fs;
1168   BCPos pc = vg->startpc;
1169   setgcrefnull(vg->name);  /* Invalidate pending goto. */
1170   setbc_a(&fs->bcbase[pc].ins, vl->slot);
1171   jmp_patch(fs, pc, vl->startpc);
1172 }
1173 
1174 /* Patch goto to close upvalues. */
1175 static void gola_close(LexState *ls, VarInfo *vg)
1176 {
1177   FuncState *fs = ls->fs;
1178   BCPos pc = vg->startpc;
1179   BCIns *ip = &fs->bcbase[pc].ins;
1180   lua_assert(gola_isgoto(vg));
1181   lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO);
1182   setbc_a(ip, vg->slot);
1183   if (bc_op(*ip) == BC_JMP) {
1184     BCPos next = jmp_next(fs, pc);
1185     if (next != NO_JMP) jmp_patch(fs, next, pc);  /* Jump to UCLO. */
1186     setbc_op(ip, BC_UCLO);  /* Turn into UCLO. */
1187     setbc_j(ip, NO_JMP);
1188   }
1189 }
1190 
1191 /* Resolve pending forward gotos for label. */
1192 static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx)
1193 {
1194   VarInfo *vg = ls->vstack + bl->vstart;
1195   VarInfo *vl = ls->vstack + idx;
1196   for (; vg < vl; vg++)
1197     if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) {
1198       if (vg->slot < vl->slot) {
1199         GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name);
1200         lua_assert((uintptr_t)name >= VARNAME__MAX);
1201         ls->linenumber = ls->fs->bcbase[vg->startpc].line;
1202         lua_assert(strref(vg->name) != NAME_BREAK);
1203         lj_lex_error(ls, 0, LJ_ERR_XGSCOPE,
1204                      strdata(strref(vg->name)), strdata(name));
1205       }
1206       gola_patch(ls, vg, vl);
1207     }
1208 }
1209 
1210 /* Fixup remaining gotos and labels for scope. */
1211 static void gola_fixup(LexState *ls, FuncScope *bl)
1212 {
1213   VarInfo *v = ls->vstack + bl->vstart;
1214   VarInfo *ve = ls->vstack + ls->vtop;
1215   for (; v < ve; v++) {
1216     GCstr *name = strref(v->name);
1217     if (name != NULL) {  /* Only consider remaining valid gotos/labels. */
1218       if (gola_islabel(v)) {
1219         VarInfo *vg;
1220         setgcrefnull(v->name);  /* Invalidate label that goes out of scope. */
1221         for (vg = v+1; vg < ve; vg++)  /* Resolve pending backward gotos. */
1222           if (strref(vg->name) == name && gola_isgoto(vg)) {
1223             if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot)
1224               gola_close(ls, vg);
1225             gola_patch(ls, vg, v);
1226           }
1227       } else if (gola_isgoto(v)) {
1228         if (bl->prev) {  /* Propagate goto or break to outer scope. */
1229           bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA;
1230           v->slot = bl->nactvar;
1231           if ((bl->flags & FSCOPE_UPVAL))
1232             gola_close(ls, v);
1233         } else {  /* No outer scope: undefined goto label or no loop. */
1234           ls->linenumber = ls->fs->bcbase[v->startpc].line;
1235           if (name == NAME_BREAK)
1236             lj_lex_error(ls, 0, LJ_ERR_XBREAK);
1237           else
1238             lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name));
1239         }
1240       }
1241     }
1242   }
1243 }
1244 
1245 /* Find existing label. */
1246 static VarInfo *gola_findlabel(LexState *ls, GCstr *name)
1247 {
1248   VarInfo *v = ls->vstack + ls->fs->bl->vstart;
1249   VarInfo *ve = ls->vstack + ls->vtop;
1250   for (; v < ve; v++)
1251     if (strref(v->name) == name && gola_islabel(v))
1252       return v;
1253   return NULL;
1254 }
1255 
1256 /* -- Scope handling ------------------------------------------------------ */
1257 
1258 /* Begin a scope. */
1259 static void fscope_begin(FuncState *fs, FuncScope *bl, int flags)
1260 {
1261   bl->nactvar = (uint8_t)fs->nactvar;
1262   bl->flags = flags;
1263   bl->vstart = fs->ls->vtop;
1264   bl->prev = fs->bl;
1265   fs->bl = bl;
1266   lua_assert(fs->freereg == fs->nactvar);
1267 }
1268 
1269 /* End a scope. */
1270 static void fscope_end(FuncState *fs)
1271 {
1272   FuncScope *bl = fs->bl;
1273   LexState *ls = fs->ls;
1274   fs->bl = bl->prev;
1275   var_remove(ls, bl->nactvar);
1276   fs->freereg = fs->nactvar;
1277   lua_assert(bl->nactvar == fs->nactvar);
1278   if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL)
1279     bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0);
1280   if ((bl->flags & FSCOPE_BREAK)) {
1281     if ((bl->flags & FSCOPE_LOOP)) {
1282       MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc);
1283       ls->vtop = idx;  /* Drop break label immediately. */
1284       gola_resolve(ls, bl, idx);
1285     } else {  /* Need the fixup step to propagate the breaks. */
1286       gola_fixup(ls, bl);
1287       return;
1288     }
1289   }
1290   if ((bl->flags & FSCOPE_GOLA)) {
1291     gola_fixup(ls, bl);
1292   }
1293 }
1294 
1295 /* Mark scope as having an upvalue. */
1296 static void fscope_uvmark(FuncState *fs, BCReg level)
1297 {
1298   FuncScope *bl;
1299   for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev)
1300     ;
1301   if (bl)
1302     bl->flags |= FSCOPE_UPVAL;
1303 }
1304 
1305 /* -- Function state management ------------------------------------------- */
1306 
1307 /* Fixup bytecode for prototype. */
1308 static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n)
1309 {
1310   BCInsLine *base = fs->bcbase;
1311   MSize i;
1312   pt->sizebc = n;
1313   bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF,
1314                    fs->framesize, 0);
1315   for (i = 1; i < n; i++)
1316     bc[i] = base[i].ins;
1317 }
1318 
1319 /* Fixup upvalues for child prototype, step #2. */
1320 static void fs_fixup_uv2(FuncState *fs, GCproto *pt)
1321 {
1322   VarInfo *vstack = fs->ls->vstack;
1323   uint16_t *uv = proto_uv(pt);
1324   MSize i, n = pt->sizeuv;
1325   for (i = 0; i < n; i++) {
1326     VarIndex vidx = uv[i];
1327     if (vidx >= LJ_MAX_VSTACK)
1328       uv[i] = vidx - LJ_MAX_VSTACK;
1329     else if ((vstack[vidx].info & VSTACK_VAR_RW))
1330       uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL;
1331     else
1332       uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE;
1333   }
1334 }
1335 
1336 /* Fixup constants for prototype. */
1337 static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)
1338 {
1339   GCtab *kt;
1340   TValue *array;
1341   Node *node;
1342   MSize i, hmask;
1343   checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");
1344   checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");
1345   setmref(pt->k, kptr);
1346   pt->sizekn = fs->nkn;
1347   pt->sizekgc = fs->nkgc;
1348   kt = fs->kt;
1349   array = tvref(kt->array);
1350   for (i = 0; i < kt->asize; i++)
1351     if (tvhaskslot(&array[i])) {
1352       TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])];
1353       if (LJ_DUALNUM)
1354         setintV(tv, (int32_t)i);
1355       else
1356         setnumV(tv, (lua_Number)i);
1357     }
1358   node = noderef(kt->node);
1359   hmask = kt->hmask;
1360   for (i = 0; i <= hmask; i++) {
1361     Node *n = &node[i];
1362     if (tvhaskslot(&n->val)) {
1363       ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);
1364       lua_assert(!tvisint(&n->key));
1365       if (tvisnum(&n->key)) {
1366         TValue *tv = &((TValue *)kptr)[kidx];
1367         if (LJ_DUALNUM) {
1368           lua_Number nn = numV(&n->key);
1369           int32_t k = lj_num2int(nn);
1370           lua_assert(!tvismzero(&n->key));
1371           if ((lua_Number)k == nn)
1372             setintV(tv, k);
1373           else
1374             *tv = n->key;
1375         } else {
1376           *tv = n->key;
1377         }
1378       } else {
1379         GCobj *o = gcV(&n->key);
1380         setgcref(((GCRef *)kptr)[~kidx], o);
1381         lj_gc_objbarrier(fs->L, pt, o);
1382         if (tvisproto(&n->key))
1383           fs_fixup_uv2(fs, gco2pt(o));
1384       }
1385     }
1386   }
1387 }
1388 
1389 /* Fixup upvalues for prototype, step #1. */
1390 static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv)
1391 {
1392   setmref(pt->uv, uv);
1393   pt->sizeuv = fs->nuv;
1394   memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex));
1395 }
1396 
1397 #ifndef LUAJIT_DISABLE_DEBUGINFO
1398 /* Prepare lineinfo for prototype. */
1399 static size_t fs_prep_line(FuncState *fs, BCLine numline)
1400 {
1401   return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
1402 }
1403 
1404 /* Fixup lineinfo for prototype. */
1405 static void fs_fixup_line(FuncState *fs, GCproto *pt,
1406                           void *lineinfo, BCLine numline)
1407 {
1408   BCInsLine *base = fs->bcbase + 1;
1409   BCLine first = fs->linedefined;
1410   MSize i = 0, n = fs->pc-1;
1411   pt->firstline = fs->linedefined;
1412   pt->numline = numline;
1413   setmref(pt->lineinfo, lineinfo);
1414   if (LJ_LIKELY(numline < 256)) {
1415     uint8_t *li = (uint8_t *)lineinfo;
1416     do {
1417       BCLine delta = base[i].line - first;
1418       lua_assert(delta >= 0 && delta < 256);
1419       li[i] = (uint8_t)delta;
1420     } while (++i < n);
1421   } else if (LJ_LIKELY(numline < 65536)) {
1422     uint16_t *li = (uint16_t *)lineinfo;
1423     do {
1424       BCLine delta = base[i].line - first;
1425       lua_assert(delta >= 0 && delta < 65536);
1426       li[i] = (uint16_t)delta;
1427     } while (++i < n);
1428   } else {
1429     uint32_t *li = (uint32_t *)lineinfo;
1430     do {
1431       BCLine delta = base[i].line - first;
1432       lua_assert(delta >= 0);
1433       li[i] = (uint32_t)delta;
1434     } while (++i < n);
1435   }
1436 }
1437 
1438 /* Prepare variable info for prototype. */
1439 static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)
1440 {
1441   VarInfo *vs =ls->vstack, *ve;
1442   MSize i, n;
1443   BCPos lastpc;
1444   lj_buf_reset(&ls->sb);  /* Copy to temp. string buffer. */
1445   /* Store upvalue names. */
1446   for (i = 0, n = fs->nuv; i < n; i++) {
1447     GCstr *s = strref(vs[fs->uvmap[i]].name);
1448     MSize len = s->len+1;
1449     char *p = lj_buf_more(&ls->sb, len);
1450     p = lj_buf_wmem(p, strdata(s), len);
1451     setsbufP(&ls->sb, p);
1452   }
1453   *ofsvar = sbuflen(&ls->sb);
1454   lastpc = 0;
1455   /* Store local variable names and compressed ranges. */
1456   for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) {
1457     if (!gola_isgotolabel(vs)) {
1458       GCstr *s = strref(vs->name);
1459       BCPos startpc;
1460       char *p;
1461       if ((uintptr_t)s < VARNAME__MAX) {
1462         p = lj_buf_more(&ls->sb, 1 + 2*5);
1463         *p++ = (char)(uintptr_t)s;
1464       } else {
1465         MSize len = s->len+1;
1466         p = lj_buf_more(&ls->sb, len + 2*5);
1467         p = lj_buf_wmem(p, strdata(s), len);
1468       }
1469       startpc = vs->startpc;
1470       p = lj_strfmt_wuleb128(p, startpc-lastpc);
1471       p = lj_strfmt_wuleb128(p, vs->endpc-startpc);
1472       setsbufP(&ls->sb, p);
1473       lastpc = startpc;
1474     }
1475   }
1476   lj_buf_putb(&ls->sb, '\0');  /* Terminator for varinfo. */
1477   return sbuflen(&ls->sb);
1478 }
1479 
1480 /* Fixup variable info for prototype. */
1481 static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar)
1482 {
1483   setmref(pt->uvinfo, p);
1484   setmref(pt->varinfo, (char *)p + ofsvar);
1485   memcpy(p, sbufB(&ls->sb), sbuflen(&ls->sb));  /* Copy from temp. buffer. */
1486 }
1487 #else
1488 
1489 /* Initialize with empty debug info, if disabled. */
1490 #define fs_prep_line(fs, numline)               (UNUSED(numline), 0)
1491 #define fs_fixup_line(fs, pt, li, numline) \
1492   pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL)
1493 #define fs_prep_var(ls, fs, ofsvar)             (UNUSED(ofsvar), 0)
1494 #define fs_fixup_var(ls, pt, p, ofsvar) \
1495   setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL)
1496 
1497 #endif
1498 
1499 /* Check if bytecode op returns. */
1500 static int bcopisret(BCOp op)
1501 {
1502   switch (op) {
1503   case BC_CALLMT: case BC_CALLT:
1504   case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1505     return 1;
1506   default:
1507     return 0;
1508   }
1509 }
1510 
1511 /* Fixup return instruction for prototype. */
1512 static void fs_fixup_ret(FuncState *fs)
1513 {
1514   BCPos lastpc = fs->pc;
1515   if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) {
1516     if ((fs->bl->flags & FSCOPE_UPVAL))
1517       bcemit_AJ(fs, BC_UCLO, 0, 0);
1518     bcemit_AD(fs, BC_RET0, 0, 1);  /* Need final return. */
1519   }
1520   fs->bl->flags |= FSCOPE_NOCLOSE;  /* Handled above. */
1521   fscope_end(fs);
1522   lua_assert(fs->bl == NULL);
1523   /* May need to fixup returns encoded before first function was created. */
1524   if (fs->flags & PROTO_FIXUP_RETURN) {
1525     BCPos pc;
1526     for (pc = 1; pc < lastpc; pc++) {
1527       BCIns ins = fs->bcbase[pc].ins;
1528       BCPos offset;
1529       switch (bc_op(ins)) {
1530       case BC_CALLMT: case BC_CALLT:
1531       case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1532         offset = bcemit_INS(fs, ins);  /* Copy original instruction. */
1533         fs->bcbase[offset].line = fs->bcbase[pc].line;
1534         offset = offset-(pc+1)+BCBIAS_J;
1535         if (offset > BCMAX_D)
1536           err_syntax(fs->ls, LJ_ERR_XFIXUP);
1537         /* Replace with UCLO plus branch. */
1538         fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset);
1539         break;
1540       case BC_UCLO:
1541         return;  /* We're done. */
1542       default:
1543         break;
1544       }
1545     }
1546   }
1547 }
1548 
1549 /* Finish a FuncState and return the new prototype. */
1550 static GCproto *fs_finish(LexState *ls, BCLine line)
1551 {
1552   lua_State *L = ls->L;
1553   FuncState *fs = ls->fs;
1554   BCLine numline = line - fs->linedefined;
1555   size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;
1556   GCproto *pt;
1557 
1558   /* Apply final fixups. */
1559   fs_fixup_ret(fs);
1560 
1561   /* Calculate total size of prototype including all colocated arrays. */
1562   sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
1563   sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);
1564   ofsk = sizept; sizept += fs->nkn*sizeof(TValue);
1565   ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;
1566   ofsli = sizept; sizept += fs_prep_line(fs, numline);
1567   ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);
1568 
1569   /* Allocate prototype and initialize its fields. */
1570   pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
1571   pt->gct = ~LJ_TPROTO;
1572   pt->sizept = (MSize)sizept;
1573   pt->trace = 0;
1574   pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN));
1575   pt->numparams = fs->numparams;
1576   pt->framesize = fs->framesize;
1577   setgcref(pt->chunkname, obj2gco(ls->chunkname));
1578 
1579   /* Close potentially uninitialized gap between bc and kgc. */
1580   *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0;
1581   fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc);
1582   fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk));
1583   fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv));
1584   fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline);
1585   fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar);
1586 
1587   lj_vmevent_send(L, BC,
1588     setprotoV(L, L->top++, pt);
1589   );
1590 
1591   L->top--;  /* Pop table of constants. */
1592   ls->vtop = fs->vbase;  /* Reset variable stack. */
1593   ls->fs = fs->prev;
1594   lua_assert(ls->fs != NULL || ls->tok == TK_eof);
1595   return pt;
1596 }
1597 
1598 /* Initialize a new FuncState. */
1599 static void fs_init(LexState *ls, FuncState *fs)
1600 {
1601   lua_State *L = ls->L;
1602   fs->prev = ls->fs; ls->fs = fs;  /* Append to list. */
1603   fs->ls = ls;
1604   fs->vbase = ls->vtop;
1605   fs->L = L;
1606   fs->pc = 0;
1607   fs->lasttarget = 0;
1608   fs->jpc = NO_JMP;
1609   fs->freereg = 0;
1610   fs->nkgc = 0;
1611   fs->nkn = 0;
1612   fs->nactvar = 0;
1613   fs->nuv = 0;
1614   fs->bl = NULL;
1615   fs->flags = 0;
1616   fs->framesize = 1;  /* Minimum frame size. */
1617   fs->kt = lj_tab_new(L, 0, 0);
1618   /* Anchor table of constants in stack to avoid being collected. */
1619   settabV(L, L->top, fs->kt);
1620   incr_top(L);
1621 }
1622 
1623 /* -- Expressions --------------------------------------------------------- */
1624 
1625 /* Forward declaration. */
1626 static void expr(LexState *ls, ExpDesc *v);
1627 
1628 /* Return string expression. */
1629 static void expr_str(LexState *ls, ExpDesc *e)
1630 {
1631   expr_init(e, VKSTR, 0);
1632   e->u.sval = lex_str(ls);
1633 }
1634 
1635 /* Return index expression. */
1636 static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e)
1637 {
1638   /* Already called: expr_toval(fs, e). */
1639   t->k = VINDEXED;
1640   if (expr_isnumk(e)) {
1641 #if LJ_DUALNUM
1642     if (tvisint(expr_numtv(e))) {
1643       int32_t k = intV(expr_numtv(e));
1644       if (checku8(k)) {
1645         t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1646         return;
1647       }
1648     }
1649 #else
1650     lua_Number n = expr_numberV(e);
1651     int32_t k = lj_num2int(n);
1652     if (checku8(k) && n == (lua_Number)k) {
1653       t->u.s.aux = BCMAX_C+1+(uint32_t)k;  /* 256..511: const byte key */
1654       return;
1655     }
1656 #endif
1657   } else if (expr_isstrk(e)) {
1658     BCReg idx = const_str(fs, e);
1659     if (idx <= BCMAX_C) {
1660       t->u.s.aux = ~idx;  /* -256..-1: const string key */
1661       return;
1662     }
1663   }
1664   t->u.s.aux = expr_toanyreg(fs, e);  /* 0..255: register */
1665 }
1666 
1667 /* Parse index expression with named field. */
1668 static void expr_field(LexState *ls, ExpDesc *v)
1669 {
1670   FuncState *fs = ls->fs;
1671   ExpDesc key;
1672   expr_toanyreg(fs, v);
1673   lj_lex_next(ls);  /* Skip dot or colon. */
1674   expr_str(ls, &key);
1675   expr_index(fs, v, &key);
1676 }
1677 
1678 /* Parse index expression with brackets. */
1679 static void expr_bracket(LexState *ls, ExpDesc *v)
1680 {
1681   lj_lex_next(ls);  /* Skip '['. */
1682   expr(ls, v);
1683   expr_toval(ls->fs, v);
1684   lex_check(ls, ']');
1685 }
1686 
1687 /* Get value of constant expression. */
1688 static void expr_kvalue(TValue *v, ExpDesc *e)
1689 {
1690   if (e->k <= VKTRUE) {
1691     setpriV(v, ~(uint32_t)e->k);
1692   } else if (e->k == VKSTR) {
1693     setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);
1694   } else {
1695     lua_assert(tvisnumber(expr_numtv(e)));
1696     *v = *expr_numtv(e);
1697   }
1698 }
1699 
1700 /* Parse table constructor expression. */
1701 static void expr_table(LexState *ls, ExpDesc *e)
1702 {
1703   FuncState *fs = ls->fs;
1704   BCLine line = ls->linenumber;
1705   GCtab *t = NULL;
1706   int vcall = 0, needarr = 0, fixt = 0;
1707   uint32_t narr = 1;  /* First array index. */
1708   uint32_t nhash = 0;  /* Number of hash entries. */
1709   BCReg freg = fs->freereg;
1710   BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0);
1711   expr_init(e, VNONRELOC, freg);
1712   bcreg_reserve(fs, 1);
1713   freg++;
1714   lex_check(ls, '{');
1715   while (ls->tok != '}') {
1716     ExpDesc key, val;
1717     vcall = 0;
1718     if (ls->tok == '[') {
1719       expr_bracket(ls, &key);  /* Already calls expr_toval. */
1720       if (!expr_isk(&key)) expr_index(fs, e, &key);
1721       if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++;
1722       lex_check(ls, '=');
1723     } else if ((ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) &&
1724                lj_lex_lookahead(ls) == '=') {
1725       expr_str(ls, &key);
1726       lex_check(ls, '=');
1727       nhash++;
1728     } else {
1729       expr_init(&key, VKNUM, 0);
1730       setintV(&key.u.nval, (int)narr);
1731       narr++;
1732       needarr = vcall = 1;
1733     }
1734     expr(ls, &val);
1735     if (expr_isk(&key) && key.k != VKNIL &&
1736         (key.k == VKSTR || expr_isk_nojump(&val))) {
1737       TValue k, *v;
1738       if (!t) {  /* Create template table on demand. */
1739         BCReg kidx;
1740         t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash));
1741         kidx = const_gc(fs, obj2gco(t), LJ_TTAB);
1742         fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);
1743       }
1744       vcall = 0;
1745       expr_kvalue(&k, &key);
1746       v = lj_tab_set(fs->L, t, &k);
1747       lj_gc_anybarriert(fs->L, t);
1748       if (expr_isk_nojump(&val)) {  /* Add const key/value to template table. */
1749         expr_kvalue(v, &val);
1750       } else {  /* Otherwise create dummy string key (avoids lj_tab_newkey). */
1751         settabV(fs->L, v, t);  /* Preserve key with table itself as value. */
1752         fixt = 1;   /* Fix this later, after all resizes. */
1753         goto nonconst;
1754       }
1755     } else {
1756     nonconst:
1757       if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; }
1758       if (expr_isk(&key)) expr_index(fs, e, &key);
1759       bcemit_store(fs, e, &val);
1760     }
1761     fs->freereg = freg;
1762     if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break;
1763   }
1764   lex_match(ls, '}', '{', line);
1765   if (vcall) {
1766     BCInsLine *ilp = &fs->bcbase[fs->pc-1];
1767     ExpDesc en;
1768     lua_assert(bc_a(ilp->ins) == freg &&
1769                bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB));
1770     expr_init(&en, VKNUM, 0);
1771     en.u.nval.u32.lo = narr-1;
1772     en.u.nval.u32.hi = 0x43300000;  /* Biased integer to avoid denormals. */
1773     if (narr > 256) { fs->pc--; ilp--; }
1774     ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en));
1775     setbc_b(&ilp[-1].ins, 0);
1776   }
1777   if (pc == fs->pc-1) {  /* Make expr relocable if possible. */
1778     e->u.s.info = pc;
1779     fs->freereg--;
1780     e->k = VRELOCABLE;
1781   } else {
1782     e->k = VNONRELOC;  /* May have been changed by expr_index. */
1783   }
1784   if (!t) {  /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */
1785     BCIns *ip = &fs->bcbase[pc].ins;
1786     if (!needarr) narr = 0;
1787     else if (narr < 3) narr = 3;
1788     else if (narr > 0x7ff) narr = 0x7ff;
1789     setbc_d(ip, narr|(hsize2hbits(nhash)<<11));
1790   } else {
1791     if (needarr && t->asize < narr)
1792       lj_tab_reasize(fs->L, t, narr-1);
1793     if (fixt) {  /* Fix value for dummy keys in template table. */
1794       Node *node = noderef(t->node);
1795       uint32_t i, hmask = t->hmask;
1796       for (i = 0; i <= hmask; i++) {
1797         Node *n = &node[i];
1798         if (tvistab(&n->val)) {
1799           lua_assert(tabV(&n->val) == t);
1800           setnilV(&n->val);  /* Turn value into nil. */
1801         }
1802       }
1803     }
1804     lj_gc_check(fs->L);
1805   }
1806 }
1807 
1808 /* Parse function parameters. */
1809 static BCReg parse_params(LexState *ls, int needself)
1810 {
1811   FuncState *fs = ls->fs;
1812   BCReg nparams = 0;
1813   lex_check(ls, '(');
1814   if (needself)
1815     var_new_lit(ls, nparams++, "self");
1816   if (ls->tok != ')') {
1817     do {
1818       if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
1819         var_new(ls, nparams++, lex_str(ls));
1820       } else if (ls->tok == TK_dots) {
1821         lj_lex_next(ls);
1822         fs->flags |= PROTO_VARARG;
1823         break;
1824       } else {
1825         err_syntax(ls, LJ_ERR_XPARAM);
1826       }
1827     } while (lex_opt(ls, ','));
1828   }
1829   var_add(ls, nparams);
1830   lua_assert(fs->nactvar == nparams);
1831   bcreg_reserve(fs, nparams);
1832   lex_check(ls, ')');
1833   return nparams;
1834 }
1835 
1836 /* Forward declaration. */
1837 static void parse_chunk(LexState *ls);
1838 
1839 /* Parse body of a function. */
1840 static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
1841 {
1842   FuncState fs, *pfs = ls->fs;
1843   FuncScope bl;
1844   GCproto *pt;
1845   ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
1846   fs_init(ls, &fs);
1847   fscope_begin(&fs, &bl, 0);
1848   fs.linedefined = line;
1849   fs.numparams = (uint8_t)parse_params(ls, needself);
1850   fs.bcbase = pfs->bcbase + pfs->pc;
1851   fs.bclim = pfs->bclim - pfs->pc;
1852   bcemit_AD(&fs, BC_FUNCF, 0, 0);  /* Placeholder. */
1853   parse_chunk(ls);
1854   if (ls->tok != TK_end) lex_match(ls, TK_end, TK_function, line);
1855   pt = fs_finish(ls, (ls->lastline = ls->linenumber));
1856   pfs->bcbase = ls->bcstack + oldbase;  /* May have been reallocated. */
1857   pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
1858   /* Store new prototype in the constant array of the parent. */
1859   expr_init(e, VRELOCABLE,
1860             bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));
1861 #if LJ_HASFFI
1862   pfs->flags |= (fs.flags & PROTO_FFI);
1863 #endif
1864   if (!(pfs->flags & PROTO_CHILD)) {
1865     if (pfs->flags & PROTO_HAS_RETURN)
1866       pfs->flags |= PROTO_FIXUP_RETURN;
1867     pfs->flags |= PROTO_CHILD;
1868   }
1869   lj_lex_next(ls);
1870 }
1871 
1872 /* Parse expression list. Last expression is left open. */
1873 static BCReg expr_list(LexState *ls, ExpDesc *v)
1874 {
1875   BCReg n = 1;
1876   expr(ls, v);
1877   while (lex_opt(ls, ',')) {
1878     expr_tonextreg(ls->fs, v);
1879     expr(ls, v);
1880     n++;
1881   }
1882   return n;
1883 }
1884 
1885 /* Parse function argument list. */
1886 static void parse_args(LexState *ls, ExpDesc *e)
1887 {
1888   FuncState *fs = ls->fs;
1889   ExpDesc args;
1890   BCIns ins;
1891   BCReg base;
1892   BCLine line = ls->linenumber;
1893   if (ls->tok == '(') {
1894 #if !LJ_52
1895     if (line != ls->lastline)
1896       err_syntax(ls, LJ_ERR_XAMBIG);
1897 #endif
1898     lj_lex_next(ls);
1899     if (ls->tok == ')') {  /* f(). */
1900       args.k = VVOID;
1901     } else {
1902       expr_list(ls, &args);
1903       if (args.k == VCALL)  /* f(a, b, g()) or f(a, b, ...). */
1904         setbc_b(bcptr(fs, &args), 0);  /* Pass on multiple results. */
1905     }
1906     lex_match(ls, ')', '(', line);
1907   } else if (ls->tok == '{') {
1908     expr_table(ls, &args);
1909   } else if (ls->tok == TK_string) {
1910     expr_init(&args, VKSTR, 0);
1911     args.u.sval = strV(&ls->tokval);
1912     lj_lex_next(ls);
1913   } else {
1914     err_syntax(ls, LJ_ERR_XFUNARG);
1915     return;  /* Silence compiler. */
1916   }
1917   lua_assert(e->k == VNONRELOC);
1918   base = e->u.s.info;  /* Base register for call. */
1919   if (args.k == VCALL) {
1920     ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);
1921   } else {
1922     if (args.k != VVOID)
1923       expr_tonextreg(fs, &args);
1924     ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base - LJ_FR2);
1925   }
1926   expr_init(e, VCALL, bcemit_INS(fs, ins));
1927   e->u.s.aux = base;
1928   fs->bcbase[fs->pc - 1].line = line;
1929   fs->freereg = base+1;  /* Leave one result by default. */
1930 }
1931 
1932 /* Parse primary expression. */
1933 static void expr_primary(LexState *ls, ExpDesc *v)
1934 {
1935   FuncState *fs = ls->fs;
1936   /* Parse prefix expression. */
1937   if (ls->tok == '(') {
1938     BCLine line = ls->linenumber;
1939     lj_lex_next(ls);
1940     expr(ls, v);
1941     lex_match(ls, ')', '(', line);
1942     expr_discharge(ls->fs, v);
1943   } else if (ls->tok == TK_name || (!LJ_52 && ls->tok == TK_goto)) {
1944     var_lookup(ls, v);
1945   } else {
1946     err_syntax(ls, LJ_ERR_XSYMBOL);
1947   }
1948   for (;;) {  /* Parse multiple expression suffixes. */
1949     if (ls->tok == '.') {
1950       expr_field(ls, v);
1951     } else if (ls->tok == '[') {
1952       ExpDesc key;
1953       expr_toanyreg(fs, v);
1954       expr_bracket(ls, &key);
1955       expr_index(fs, v, &key);
1956     } else if (ls->tok == ':') {
1957       ExpDesc key;
1958       lj_lex_next(ls);
1959       expr_str(ls, &key);
1960       bcemit_method(fs, v, &key);
1961       parse_args(ls, v);
1962     } else if (ls->tok == '(' || ls->tok == TK_string || ls->tok == '{') {
1963       expr_tonextreg(fs, v);
1964       if (LJ_FR2) bcreg_reserve(fs, 1);
1965       parse_args(ls, v);
1966     } else {
1967       break;
1968     }
1969   }
1970 }
1971 
1972 /* Parse simple expression. */
1973 static void expr_simple(LexState *ls, ExpDesc *v)
1974 {
1975   switch (ls->tok) {
1976   case TK_number:
1977     expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokval)) ? VKCDATA : VKNUM, 0);
1978     copyTV(ls->L, &v->u.nval, &ls->tokval);
1979     break;
1980   case TK_string:
1981     expr_init(v, VKSTR, 0);
1982     v->u.sval = strV(&ls->tokval);
1983     break;
1984   case TK_nil:
1985     expr_init(v, VKNIL, 0);
1986     break;
1987   case TK_true:
1988     expr_init(v, VKTRUE, 0);
1989     break;
1990   case TK_false:
1991     expr_init(v, VKFALSE, 0);
1992     break;
1993   case TK_dots: {  /* Vararg. */
1994     FuncState *fs = ls->fs;
1995     BCReg base;
1996     checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS);
1997     bcreg_reserve(fs, 1);
1998     base = fs->freereg-1;
1999     expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams));
2000     v->u.s.aux = base;
2001     break;
2002   }
2003   case '{':  /* Table constructor. */
2004     expr_table(ls, v);
2005     return;
2006   case TK_function:
2007     lj_lex_next(ls);
2008     parse_body(ls, v, 0, ls->linenumber);
2009     return;
2010   default:
2011     expr_primary(ls, v);
2012     return;
2013   }
2014   lj_lex_next(ls);
2015 }
2016 
2017 /* Manage syntactic levels to avoid blowing up the stack. */
2018 static void synlevel_begin(LexState *ls)
2019 {
2020   if (++ls->level >= LJ_MAX_XLEVEL)
2021     lj_lex_error(ls, 0, LJ_ERR_XLEVELS);
2022 }
2023 
2024 #define synlevel_end(ls)        ((ls)->level--)
2025 
2026 /* Convert token to binary operator. */
2027 static BinOpr token2binop(LexToken tok)
2028 {
2029   switch (tok) {
2030   case '+':     return OPR_ADD;
2031   case '-':     return OPR_SUB;
2032   case '*':     return OPR_MUL;
2033   case '/':     return OPR_DIV;
2034   case '%':     return OPR_MOD;
2035   case '^':     return OPR_POW;
2036   case TK_concat: return OPR_CONCAT;
2037   case TK_ne:   return OPR_NE;
2038   case TK_eq:   return OPR_EQ;
2039   case '<':     return OPR_LT;
2040   case TK_le:   return OPR_LE;
2041   case '>':     return OPR_GT;
2042   case TK_ge:   return OPR_GE;
2043   case TK_and:  return OPR_AND;
2044   case TK_or:   return OPR_OR;
2045   default:      return OPR_NOBINOPR;
2046   }
2047 }
2048 
2049 /* Priorities for each binary operator. ORDER OPR. */
2050 static const struct {
2051   uint8_t left;         /* Left priority. */
2052   uint8_t right;        /* Right priority. */
2053 } priority[] = {
2054   {6,6}, {6,6}, {7,7}, {7,7}, {7,7},    /* ADD SUB MUL DIV MOD */
2055   {10,9}, {5,4},                        /* POW CONCAT (right associative) */
2056   {3,3}, {3,3},                         /* EQ NE */
2057   {3,3}, {3,3}, {3,3}, {3,3},           /* LT GE GT LE */
2058   {2,2}, {1,1}                          /* AND OR */
2059 };
2060 
2061 #define UNARY_PRIORITY          8  /* Priority for unary operators. */
2062 
2063 /* Forward declaration. */
2064 static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit);
2065 
2066 /* Parse unary expression. */
2067 static void expr_unop(LexState *ls, ExpDesc *v)
2068 {
2069   BCOp op;
2070   if (ls->tok == TK_not) {
2071     op = BC_NOT;
2072   } else if (ls->tok == '-') {
2073     op = BC_UNM;
2074   } else if (ls->tok == '#') {
2075     op = BC_LEN;
2076   } else {
2077     expr_simple(ls, v);
2078     return;
2079   }
2080   lj_lex_next(ls);
2081   expr_binop(ls, v, UNARY_PRIORITY);
2082   bcemit_unop(ls->fs, op, v);
2083 }
2084 
2085 /* Parse binary expressions with priority higher than the limit. */
2086 static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit)
2087 {
2088   BinOpr op;
2089   synlevel_begin(ls);
2090   expr_unop(ls, v);
2091   op = token2binop(ls->tok);
2092   while (op != OPR_NOBINOPR && priority[op].left > limit) {
2093     ExpDesc v2;
2094     BinOpr nextop;
2095     lj_lex_next(ls);
2096     bcemit_binop_left(ls->fs, op, v);
2097     /* Parse binary expression with higher priority. */
2098     nextop = expr_binop(ls, &v2, priority[op].right);
2099     bcemit_binop(ls->fs, op, v, &v2);
2100     op = nextop;
2101   }
2102   synlevel_end(ls);
2103   return op;  /* Return unconsumed binary operator (if any). */
2104 }
2105 
2106 /* Parse expression. */
2107 static void expr(LexState *ls, ExpDesc *v)
2108 {
2109   expr_binop(ls, v, 0);  /* Priority 0: parse whole expression. */
2110 }
2111 
2112 /* Assign expression to the next register. */
2113 static void expr_next(LexState *ls)
2114 {
2115   ExpDesc e;
2116   expr(ls, &e);
2117   expr_tonextreg(ls->fs, &e);
2118 }
2119 
2120 /* Parse conditional expression. */
2121 static BCPos expr_cond(LexState *ls)
2122 {
2123   ExpDesc v;
2124   expr(ls, &v);
2125   if (v.k == VKNIL) v.k = VKFALSE;
2126   bcemit_branch_t(ls->fs, &v);
2127   return v.f;
2128 }
2129 
2130 /* -- Assignments --------------------------------------------------------- */
2131 
2132 /* List of LHS variables. */
2133 typedef struct LHSVarList {
2134   ExpDesc v;                    /* LHS variable. */
2135   struct LHSVarList *prev;      /* Link to previous LHS variable. */
2136 } LHSVarList;
2137 
2138 /* Eliminate write-after-read hazards for local variable assignment. */
2139 static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v)
2140 {
2141   FuncState *fs = ls->fs;
2142   BCReg reg = v->u.s.info;  /* Check against this variable. */
2143   BCReg tmp = fs->freereg;  /* Rename to this temp. register (if needed). */
2144   int hazard = 0;
2145   for (; lh; lh = lh->prev) {
2146     if (lh->v.k == VINDEXED) {
2147       if (lh->v.u.s.info == reg) {  /* t[i], t = 1, 2 */
2148         hazard = 1;
2149         lh->v.u.s.info = tmp;
2150       }
2151       if (lh->v.u.s.aux == reg) {  /* t[i], i = 1, 2 */
2152         hazard = 1;
2153         lh->v.u.s.aux = tmp;
2154       }
2155     }
2156   }
2157   if (hazard) {
2158     bcemit_AD(fs, BC_MOV, tmp, reg);  /* Rename conflicting variable. */
2159     bcreg_reserve(fs, 1);
2160   }
2161 }
2162 
2163 /* Adjust LHS/RHS of an assignment. */
2164 static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e)
2165 {
2166   FuncState *fs = ls->fs;
2167   int32_t extra = (int32_t)nvars - (int32_t)nexps;
2168   if (e->k == VCALL) {
2169     extra++;  /* Compensate for the VCALL itself. */
2170     if (extra < 0) extra = 0;
2171     setbc_b(bcptr(fs, e), extra+1);  /* Fixup call results. */
2172     if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1);
2173   } else {
2174     if (e->k != VVOID)
2175       expr_tonextreg(fs, e);  /* Close last expression. */
2176     if (extra > 0) {  /* Leftover LHS are set to nil. */
2177       BCReg reg = fs->freereg;
2178       bcreg_reserve(fs, (BCReg)extra);
2179       bcemit_nil(fs, reg, (BCReg)extra);
2180     }
2181   }
2182   if (nexps > nvars)
2183     ls->fs->freereg -= nexps - nvars;  /* Drop leftover regs. */
2184 }
2185 
2186 /* Recursively parse assignment statement. */
2187 static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars)
2188 {
2189   ExpDesc e;
2190   checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX);
2191   if (lex_opt(ls, ',')) {  /* Collect LHS list and recurse upwards. */
2192     LHSVarList vl;
2193     vl.prev = lh;
2194     expr_primary(ls, &vl.v);
2195     if (vl.v.k == VLOCAL)
2196       assign_hazard(ls, lh, &vl.v);
2197     checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names");
2198     parse_assignment(ls, &vl, nvars+1);
2199   } else {  /* Parse RHS. */
2200     BCReg nexps;
2201     lex_check(ls, '=');
2202     nexps = expr_list(ls, &e);
2203     if (nexps == nvars) {
2204       if (e.k == VCALL) {
2205         if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) {  /* Vararg assignment. */
2206           ls->fs->freereg--;
2207           e.k = VRELOCABLE;
2208         } else {  /* Multiple call results. */
2209           e.u.s.info = e.u.s.aux;  /* Base of call is not relocatable. */
2210           e.k = VNONRELOC;
2211         }
2212       }
2213       bcemit_store(ls->fs, &lh->v, &e);
2214       return;
2215     }
2216     assign_adjust(ls, nvars, nexps, &e);
2217   }
2218   /* Assign RHS to LHS and recurse downwards. */
2219   expr_init(&e, VNONRELOC, ls->fs->freereg-1);
2220   bcemit_store(ls->fs, &lh->v, &e);
2221 }
2222 
2223 /* Parse call statement or assignment. */
2224 static void parse_call_assign(LexState *ls)
2225 {
2226   FuncState *fs = ls->fs;
2227   LHSVarList vl;
2228   expr_primary(ls, &vl.v);
2229   if (vl.v.k == VCALL) {  /* Function call statement. */
2230     setbc_b(bcptr(fs, &vl.v), 1);  /* No results. */
2231   } else {  /* Start of an assignment. */
2232     vl.prev = NULL;
2233     parse_assignment(ls, &vl, 1);
2234   }
2235 }
2236 
2237 /* Parse 'local' statement. */
2238 static void parse_local(LexState *ls)
2239 {
2240   if (lex_opt(ls, TK_function)) {  /* Local function declaration. */
2241     ExpDesc v, b;
2242     FuncState *fs = ls->fs;
2243     var_new(ls, 0, lex_str(ls));
2244     expr_init(&v, VLOCAL, fs->freereg);
2245     v.u.s.aux = fs->varmap[fs->freereg];
2246     bcreg_reserve(fs, 1);
2247     var_add(ls, 1);
2248     parse_body(ls, &b, 0, ls->linenumber);
2249     /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */
2250     expr_free(fs, &b);
2251     expr_toreg(fs, &b, v.u.s.info);
2252     /* The upvalue is in scope, but the local is only valid after the store. */
2253     var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc;
2254   } else {  /* Local variable declaration. */
2255     ExpDesc e;
2256     BCReg nexps, nvars = 0;
2257     do {  /* Collect LHS. */
2258       var_new(ls, nvars++, lex_str(ls));
2259     } while (lex_opt(ls, ','));
2260     if (lex_opt(ls, '=')) {  /* Optional RHS. */
2261       nexps = expr_list(ls, &e);
2262     } else {  /* Or implicitly set to nil. */
2263       e.k = VVOID;
2264       nexps = 0;
2265     }
2266     assign_adjust(ls, nvars, nexps, &e);
2267     var_add(ls, nvars);
2268   }
2269 }
2270 
2271 /* Parse 'function' statement. */
2272 static void parse_func(LexState *ls, BCLine line)
2273 {
2274   FuncState *fs;
2275   ExpDesc v, b;
2276   int needself = 0;
2277   lj_lex_next(ls);  /* Skip 'function'. */
2278   /* Parse function name. */
2279   var_lookup(ls, &v);
2280   while (ls->tok == '.')  /* Multiple dot-separated fields. */
2281     expr_field(ls, &v);
2282   if (ls->tok == ':') {  /* Optional colon to signify method call. */
2283     needself = 1;
2284     expr_field(ls, &v);
2285   }
2286   parse_body(ls, &b, needself, line);
2287   fs = ls->fs;
2288   bcemit_store(fs, &v, &b);
2289   fs->bcbase[fs->pc - 1].line = line;  /* Set line for the store. */
2290 }
2291 
2292 /* -- Control transfer statements ----------------------------------------- */
2293 
2294 /* Check for end of block. */
2295 static int parse_isend(LexToken tok)
2296 {
2297   switch (tok) {
2298   case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof:
2299     return 1;
2300   default:
2301     return 0;
2302   }
2303 }
2304 
2305 /* Parse 'return' statement. */
2306 static void parse_return(LexState *ls)
2307 {
2308   BCIns ins;
2309   FuncState *fs = ls->fs;
2310   lj_lex_next(ls);  /* Skip 'return'. */
2311   fs->flags |= PROTO_HAS_RETURN;
2312   if (parse_isend(ls->tok) || ls->tok == ';') {  /* Bare return. */
2313     ins = BCINS_AD(BC_RET0, 0, 1);
2314   } else {  /* Return with one or more values. */
2315     ExpDesc e;  /* Receives the _last_ expression in the list. */
2316     BCReg nret = expr_list(ls, &e);
2317     if (nret == 1) {  /* Return one result. */
2318       if (e.k == VCALL) {  /* Check for tail call. */
2319         BCIns *ip = bcptr(fs, &e);
2320         /* It doesn't pay off to add BC_VARGT just for 'return ...'. */
2321         if (bc_op(*ip) == BC_VARG) goto notailcall;
2322         fs->pc--;
2323         ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip));
2324       } else {  /* Can return the result from any register. */
2325         ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2);
2326       }
2327     } else {
2328       if (e.k == VCALL) {  /* Append all results from a call. */
2329       notailcall:
2330         setbc_b(bcptr(fs, &e), 0);
2331         ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar);
2332       } else {
2333         expr_tonextreg(fs, &e);  /* Force contiguous registers. */
2334         ins = BCINS_AD(BC_RET, fs->nactvar, nret+1);
2335       }
2336     }
2337   }
2338   if (fs->flags & PROTO_CHILD)
2339     bcemit_AJ(fs, BC_UCLO, 0, 0);  /* May need to close upvalues first. */
2340   bcemit_INS(fs, ins);
2341 }
2342 
2343 /* Parse 'break' statement. */
2344 static void parse_break(LexState *ls)
2345 {
2346   ls->fs->bl->flags |= FSCOPE_BREAK;
2347   gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs));
2348 }
2349 
2350 /* Parse 'goto' statement. */
2351 static void parse_goto(LexState *ls)
2352 {
2353   FuncState *fs = ls->fs;
2354   GCstr *name = lex_str(ls);
2355   VarInfo *vl = gola_findlabel(ls, name);
2356   if (vl)  /* Treat backwards goto within same scope like a loop. */
2357     bcemit_AJ(fs, BC_LOOP, vl->slot, -1);  /* No BC range check. */
2358   fs->bl->flags |= FSCOPE_GOLA;
2359   gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs));
2360 }
2361 
2362 /* Parse label. */
2363 static void parse_label(LexState *ls)
2364 {
2365   FuncState *fs = ls->fs;
2366   GCstr *name;
2367   MSize idx;
2368   fs->lasttarget = fs->pc;
2369   fs->bl->flags |= FSCOPE_GOLA;
2370   lj_lex_next(ls);  /* Skip '::'. */
2371   name = lex_str(ls);
2372   if (gola_findlabel(ls, name))
2373     lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name));
2374   idx = gola_new(ls, name, VSTACK_LABEL, fs->pc);
2375   lex_check(ls, TK_label);
2376   /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */
2377   for (;;) {
2378     if (ls->tok == TK_label) {
2379       synlevel_begin(ls);
2380       parse_label(ls);
2381       synlevel_end(ls);
2382     } else if (LJ_52 && ls->tok == ';') {
2383       lj_lex_next(ls);
2384     } else {
2385       break;
2386     }
2387   }
2388   /* Trailing label is considered to be outside of scope. */
2389   if (parse_isend(ls->tok) && ls->tok != TK_until)
2390     ls->vstack[idx].slot = fs->bl->nactvar;
2391   gola_resolve(ls, fs->bl, idx);
2392 }
2393 
2394 /* -- Blocks, loops and conditional statements ---------------------------- */
2395 
2396 /* Parse a block. */
2397 static void parse_block(LexState *ls)
2398 {
2399   FuncState *fs = ls->fs;
2400   FuncScope bl;
2401   fscope_begin(fs, &bl, 0);
2402   parse_chunk(ls);
2403   fscope_end(fs);
2404 }
2405 
2406 /* Parse 'while' statement. */
2407 static void parse_while(LexState *ls, BCLine line)
2408 {
2409   FuncState *fs = ls->fs;
2410   BCPos start, loop, condexit;
2411   FuncScope bl;
2412   lj_lex_next(ls);  /* Skip 'while'. */
2413   start = fs->lasttarget = fs->pc;
2414   condexit = expr_cond(ls);
2415   fscope_begin(fs, &bl, FSCOPE_LOOP);
2416   lex_check(ls, TK_do);
2417   loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
2418   parse_block(ls);
2419   jmp_patch(fs, bcemit_jmp(fs), start);
2420   lex_match(ls, TK_end, TK_while, line);
2421   fscope_end(fs);
2422   jmp_tohere(fs, condexit);
2423   jmp_patchins(fs, loop, fs->pc);
2424 }
2425 
2426 /* Parse 'repeat' statement. */
2427 static void parse_repeat(LexState *ls, BCLine line)
2428 {
2429   FuncState *fs = ls->fs;
2430   BCPos loop = fs->lasttarget = fs->pc;
2431   BCPos condexit;
2432   FuncScope bl1, bl2;
2433   fscope_begin(fs, &bl1, FSCOPE_LOOP);  /* Breakable loop scope. */
2434   fscope_begin(fs, &bl2, 0);  /* Inner scope. */
2435   lj_lex_next(ls);  /* Skip 'repeat'. */
2436   bcemit_AD(fs, BC_LOOP, fs->nactvar, 0);
2437   parse_chunk(ls);
2438   lex_match(ls, TK_until, TK_repeat, line);
2439   condexit = expr_cond(ls);  /* Parse condition (still inside inner scope). */
2440   if (!(bl2.flags & FSCOPE_UPVAL)) {  /* No upvalues? Just end inner scope. */
2441     fscope_end(fs);
2442   } else {  /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */
2443     parse_break(ls);  /* Break from loop and close upvalues. */
2444     jmp_tohere(fs, condexit);
2445     fscope_end(fs);  /* End inner scope and close upvalues. */
2446     condexit = bcemit_jmp(fs);
2447   }
2448   jmp_patch(fs, condexit, loop);  /* Jump backwards if !cond. */
2449   jmp_patchins(fs, loop, fs->pc);
2450   fscope_end(fs);  /* End loop scope. */
2451 }
2452 
2453 /* Parse numeric 'for'. */
2454 static void parse_for_num(LexState *ls, GCstr *varname, BCLine line)
2455 {
2456   FuncState *fs = ls->fs;
2457   BCReg base = fs->freereg;
2458   FuncScope bl;
2459   BCPos loop, loopend;
2460   /* Hidden control variables. */
2461   var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);
2462   var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);
2463   var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);
2464   /* Visible copy of index variable. */
2465   var_new(ls, FORL_EXT, varname);
2466   lex_check(ls, '=');
2467   expr_next(ls);
2468   lex_check(ls, ',');
2469   expr_next(ls);
2470   if (lex_opt(ls, ',')) {
2471     expr_next(ls);
2472   } else {
2473     bcemit_AD(fs, BC_KSHORT, fs->freereg, 1);  /* Default step is 1. */
2474     bcreg_reserve(fs, 1);
2475   }
2476   var_add(ls, 3);  /* Hidden control variables. */
2477   lex_check(ls, TK_do);
2478   loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP);
2479   fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
2480   var_add(ls, 1);
2481   bcreg_reserve(fs, 1);
2482   parse_block(ls);
2483   fscope_end(fs);
2484   /* Perform loop inversion. Loop control instructions are at the end. */
2485   loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP);
2486   fs->bcbase[loopend].line = line;  /* Fix line for control ins. */
2487   jmp_patchins(fs, loopend, loop+1);
2488   jmp_patchins(fs, loop, fs->pc);
2489 }
2490 
2491 /* Try to predict whether the iterator is next() and specialize the bytecode.
2492 ** Detecting next() and pairs() by name is simplistic, but quite effective.
2493 ** The interpreter backs off if the check for the closure fails at runtime.
2494 */
2495 static int predict_next(LexState *ls, FuncState *fs, BCPos pc)
2496 {
2497   BCIns ins = fs->bcbase[pc].ins;
2498   GCstr *name;
2499   cTValue *o;
2500   switch (bc_op(ins)) {
2501   case BC_MOV:
2502     name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name));
2503     break;
2504   case BC_UGET:
2505     name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name));
2506     break;
2507   case BC_GGET:
2508     /* There's no inverse index (yet), so lookup the strings. */
2509     o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs"));
2510     if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
2511       return 1;
2512     o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next"));
2513     if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins))
2514       return 1;
2515     return 0;
2516   default:
2517     return 0;
2518   }
2519   return (name->len == 5 && !strcmp(strdata(name), "pairs")) ||
2520          (name->len == 4 && !strcmp(strdata(name), "next"));
2521 }
2522 
2523 /* Parse 'for' iterator. */
2524 static void parse_for_iter(LexState *ls, GCstr *indexname)
2525 {
2526   FuncState *fs = ls->fs;
2527   ExpDesc e;
2528   BCReg nvars = 0;
2529   BCLine line;
2530   BCReg base = fs->freereg + 3;
2531   BCPos loop, loopend, exprpc = fs->pc;
2532   FuncScope bl;
2533   int isnext;
2534   /* Hidden control variables. */
2535   var_new_fixed(ls, nvars++, VARNAME_FOR_GEN);
2536   var_new_fixed(ls, nvars++, VARNAME_FOR_STATE);
2537   var_new_fixed(ls, nvars++, VARNAME_FOR_CTL);
2538   /* Visible variables returned from iterator. */
2539   var_new(ls, nvars++, indexname);
2540   while (lex_opt(ls, ','))
2541     var_new(ls, nvars++, lex_str(ls));
2542   lex_check(ls, TK_in);
2543   line = ls->linenumber;
2544   assign_adjust(ls, 3, expr_list(ls, &e), &e);
2545   /* The iterator needs another 3 [4] slots (func [pc] | state ctl). */
2546   bcreg_bump(fs, 3+LJ_FR2);
2547   isnext = (nvars <= 5 && predict_next(ls, fs, exprpc));
2548   var_add(ls, 3);  /* Hidden control variables. */
2549   lex_check(ls, TK_do);
2550   loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP);
2551   fscope_begin(fs, &bl, 0);  /* Scope for visible variables. */
2552   var_add(ls, nvars-3);
2553   bcreg_reserve(fs, nvars-3);
2554   parse_block(ls);
2555   fscope_end(fs);
2556   /* Perform loop inversion. Loop control instructions are at the end. */
2557   jmp_patchins(fs, loop, fs->pc);
2558   bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1);
2559   loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP);
2560   fs->bcbase[loopend-1].line = line;  /* Fix line for control ins. */
2561   fs->bcbase[loopend].line = line;
2562   jmp_patchins(fs, loopend, loop+1);
2563 }
2564 
2565 /* Parse 'for' statement. */
2566 static void parse_for(LexState *ls, BCLine line)
2567 {
2568   FuncState *fs = ls->fs;
2569   GCstr *varname;
2570   FuncScope bl;
2571   fscope_begin(fs, &bl, FSCOPE_LOOP);
2572   lj_lex_next(ls);  /* Skip 'for'. */
2573   varname = lex_str(ls);  /* Get first variable name. */
2574   if (ls->tok == '=')
2575     parse_for_num(ls, varname, line);
2576   else if (ls->tok == ',' || ls->tok == TK_in)
2577     parse_for_iter(ls, varname);
2578   else
2579     err_syntax(ls, LJ_ERR_XFOR);
2580   lex_match(ls, TK_end, TK_for, line);
2581   fscope_end(fs);  /* Resolve break list. */
2582 }
2583 
2584 /* Parse condition and 'then' block. */
2585 static BCPos parse_then(LexState *ls)
2586 {
2587   BCPos condexit;
2588   lj_lex_next(ls);  /* Skip 'if' or 'elseif'. */
2589   condexit = expr_cond(ls);
2590   lex_check(ls, TK_then);
2591   parse_block(ls);
2592   return condexit;
2593 }
2594 
2595 /* Parse 'if' statement. */
2596 static void parse_if(LexState *ls, BCLine line)
2597 {
2598   FuncState *fs = ls->fs;
2599   BCPos flist;
2600   BCPos escapelist = NO_JMP;
2601   flist = parse_then(ls);
2602   while (ls->tok == TK_elseif) {  /* Parse multiple 'elseif' blocks. */
2603     jmp_append(fs, &escapelist, bcemit_jmp(fs));
2604     jmp_tohere(fs, flist);
2605     flist = parse_then(ls);
2606   }
2607   if (ls->tok == TK_else) {  /* Parse optional 'else' block. */
2608     jmp_append(fs, &escapelist, bcemit_jmp(fs));
2609     jmp_tohere(fs, flist);
2610     lj_lex_next(ls);  /* Skip 'else'. */
2611     parse_block(ls);
2612   } else {
2613     jmp_append(fs, &escapelist, flist);
2614   }
2615   jmp_tohere(fs, escapelist);
2616   lex_match(ls, TK_end, TK_if, line);
2617 }
2618 
2619 /* -- Parse statements ---------------------------------------------------- */
2620 
2621 /* Parse a statement. Returns 1 if it must be the last one in a chunk. */
2622 static int parse_stmt(LexState *ls)
2623 {
2624   BCLine line = ls->linenumber;
2625   switch (ls->tok) {
2626   case TK_if:
2627     parse_if(ls, line);
2628     break;
2629   case TK_while:
2630     parse_while(ls, line);
2631     break;
2632   case TK_do:
2633     lj_lex_next(ls);
2634     parse_block(ls);
2635     lex_match(ls, TK_end, TK_do, line);
2636     break;
2637   case TK_for:
2638     parse_for(ls, line);
2639     break;
2640   case TK_repeat:
2641     parse_repeat(ls, line);
2642     break;
2643   case TK_function:
2644     parse_func(ls, line);
2645     break;
2646   case TK_local:
2647     lj_lex_next(ls);
2648     parse_local(ls);
2649     break;
2650   case TK_return:
2651     parse_return(ls);
2652     return 1;  /* Must be last. */
2653   case TK_break:
2654     lj_lex_next(ls);
2655     parse_break(ls);
2656     return !LJ_52;  /* Must be last in Lua 5.1. */
2657 #if LJ_52
2658   case ';':
2659     lj_lex_next(ls);
2660     break;
2661 #endif
2662   case TK_label:
2663     parse_label(ls);
2664     break;
2665   case TK_goto:
2666     if (LJ_52 || lj_lex_lookahead(ls) == TK_name) {
2667       lj_lex_next(ls);
2668       parse_goto(ls);
2669       break;
2670     }  /* else: fallthrough */
2671   default:
2672     parse_call_assign(ls);
2673     break;
2674   }
2675   return 0;
2676 }
2677 
2678 /* A chunk is a list of statements optionally separated by semicolons. */
2679 static void parse_chunk(LexState *ls)
2680 {
2681   int islast = 0;
2682   synlevel_begin(ls);
2683   while (!islast && !parse_isend(ls->tok)) {
2684     islast = parse_stmt(ls);
2685     lex_opt(ls, ';');
2686     lua_assert(ls->fs->framesize >= ls->fs->freereg &&
2687                ls->fs->freereg >= ls->fs->nactvar);
2688     ls->fs->freereg = ls->fs->nactvar;  /* Free registers after each stmt. */
2689   }
2690   synlevel_end(ls);
2691 }
2692 
2693 /* Entry point of bytecode parser. */
2694 GCproto *lj_parse(LexState *ls)
2695 {
2696   FuncState fs;
2697   FuncScope bl;
2698   GCproto *pt;
2699   lua_State *L = ls->L;
2700 #ifdef LUAJIT_DISABLE_DEBUGINFO
2701   ls->chunkname = lj_str_newlit(L, "=");
2702 #else
2703   ls->chunkname = lj_str_newz(L, ls->chunkarg);
2704 #endif
2705   setstrV(L, L->top, ls->chunkname);  /* Anchor chunkname string. */
2706   incr_top(L);
2707   ls->level = 0;
2708   fs_init(ls, &fs);
2709   fs.linedefined = 0;
2710   fs.numparams = 0;
2711   fs.bcbase = NULL;
2712   fs.bclim = 0;
2713   fs.flags |= PROTO_VARARG;  /* Main chunk is always a vararg func. */
2714   fscope_begin(&fs, &bl, 0);
2715   bcemit_AD(&fs, BC_FUNCV, 0, 0);  /* Placeholder. */
2716   lj_lex_next(ls);  /* Read-ahead first token. */
2717   parse_chunk(ls);
2718   if (ls->tok != TK_eof)
2719     err_token(ls, TK_eof);
2720   pt = fs_finish(ls, ls->linenumber);
2721   L->top--;  /* Drop chunkname. */
2722   lua_assert(fs.prev == NULL);
2723   lua_assert(ls->fs == NULL);
2724   lua_assert(pt->sizeuv == 0);
2725   return pt;
2726 }
2727 

/* [<][>][^][v][top][bottom][index][help] */