lj_parse.c 79 KB

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