lcode.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /*
  2. ** $Id: lcode.c $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lcode_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include "lua.h"
  13. #include "lcode.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lgc.h"
  17. #include "llex.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lopcodes.h"
  21. #include "lparser.h"
  22. #include "lstring.h"
  23. #include "ltable.h"
  24. #include "lvm.h"
  25. /* Maximum number of registers in a Lua function (must fit in 8 bits) */
  26. #define MAXREGS 255
  27. #define hasjumps(e) ((e)->t != (e)->f)
  28. static int codesJ (FuncState *fs, OpCode o, int sj, int k);
  29. /* semantic error */
  30. l_noret luaK_semerror (LexState *ls, const char *msg) {
  31. ls->t.token = 0; /* remove "near <token>" from final message */
  32. luaX_syntaxerror(ls, msg);
  33. }
  34. /*
  35. ** If expression is a numeric constant, fills 'v' with its value
  36. ** and returns 1. Otherwise, returns 0.
  37. */
  38. static int tonumeral (const expdesc *e, TValue *v) {
  39. if (hasjumps(e))
  40. return 0; /* not a numeral */
  41. switch (e->k) {
  42. case VKINT:
  43. if (v) setivalue(v, e->u.ival);
  44. return 1;
  45. case VKFLT:
  46. if (v) setfltvalue(v, e->u.nval);
  47. return 1;
  48. default: return 0;
  49. }
  50. }
  51. /*
  52. ** Get the constant value from a constant expression
  53. */
  54. static TValue *const2val (FuncState *fs, const expdesc *e) {
  55. lua_assert(e->k == VCONST);
  56. return &fs->ls->dyd->actvar.arr[e->u.info].k;
  57. }
  58. /*
  59. ** If expression is a constant, fills 'v' with its value
  60. ** and returns 1. Otherwise, returns 0.
  61. */
  62. int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
  63. if (hasjumps(e))
  64. return 0; /* not a constant */
  65. switch (e->k) {
  66. case VFALSE: case VTRUE:
  67. setbvalue(v, e->k == VTRUE);
  68. return 1;
  69. case VNIL:
  70. setnilvalue(v);
  71. return 1;
  72. case VKSTR: {
  73. setsvalue(fs->ls->L, v, e->u.strval);
  74. return 1;
  75. }
  76. case VCONST: {
  77. setobj(fs->ls->L, v, const2val(fs, e));
  78. return 1;
  79. }
  80. default: return tonumeral(e, v);
  81. }
  82. }
  83. /*
  84. ** Return the previous instruction of the current code. If there
  85. ** may be a jump target between the current instruction and the
  86. ** previous one, return an invalid instruction (to avoid wrong
  87. ** optimizations).
  88. */
  89. static Instruction *previousinstruction (FuncState *fs) {
  90. static const Instruction invalidinstruction = ~(Instruction)0;
  91. if (fs->pc > fs->lasttarget)
  92. return &fs->f->code[fs->pc - 1]; /* previous instruction */
  93. else
  94. return cast(Instruction*, &invalidinstruction);
  95. }
  96. /*
  97. ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
  98. ** instruction is also OP_LOADNIL and ranges are compatible, adjust
  99. ** range of previous instruction instead of emitting a new one. (For
  100. ** instance, 'local a; local b' will generate a single opcode.)
  101. */
  102. void luaK_nil (FuncState *fs, int from, int n) {
  103. int l = from + n - 1; /* last register to set nil */
  104. Instruction *previous = previousinstruction(fs);
  105. if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
  106. int pfrom = GETARG_A(*previous); /* get previous range */
  107. int pl = pfrom + GETARG_B(*previous);
  108. if ((pfrom <= from && from <= pl + 1) ||
  109. (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
  110. if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
  111. if (pl > l) l = pl; /* l = max(l, pl) */
  112. SETARG_A(*previous, from);
  113. SETARG_B(*previous, l - from);
  114. return;
  115. } /* else go through */
  116. }
  117. luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
  118. }
  119. /*
  120. ** Gets the destination address of a jump instruction. Used to traverse
  121. ** a list of jumps.
  122. */
  123. static int getjump (FuncState *fs, int pc) {
  124. int offset = GETARG_sJ(fs->f->code[pc]);
  125. if (offset == NO_JUMP) /* point to itself represents end of list */
  126. return NO_JUMP; /* end of list */
  127. else
  128. return (pc+1)+offset; /* turn offset into absolute position */
  129. }
  130. /*
  131. ** Fix jump instruction at position 'pc' to jump to 'dest'.
  132. ** (Jump addresses are relative in Lua)
  133. */
  134. static void fixjump (FuncState *fs, int pc, int dest) {
  135. Instruction *jmp = &fs->f->code[pc];
  136. int offset = dest - (pc + 1);
  137. lua_assert(dest != NO_JUMP);
  138. if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
  139. luaX_syntaxerror(fs->ls, "control structure too long");
  140. lua_assert(GET_OPCODE(*jmp) == OP_JMP);
  141. SETARG_sJ(*jmp, offset);
  142. }
  143. /*
  144. ** Concatenate jump-list 'l2' into jump-list 'l1'
  145. */
  146. void luaK_concat (FuncState *fs, int *l1, int l2) {
  147. if (l2 == NO_JUMP) return; /* nothing to concatenate? */
  148. else if (*l1 == NO_JUMP) /* no original list? */
  149. *l1 = l2; /* 'l1' points to 'l2' */
  150. else {
  151. int list = *l1;
  152. int next;
  153. while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */
  154. list = next;
  155. fixjump(fs, list, l2); /* last element links to 'l2' */
  156. }
  157. }
  158. /*
  159. ** Create a jump instruction and return its position, so its destination
  160. ** can be fixed later (with 'fixjump').
  161. */
  162. int luaK_jump (FuncState *fs) {
  163. return codesJ(fs, OP_JMP, NO_JUMP, 0);
  164. }
  165. /*
  166. ** Code a 'return' instruction
  167. */
  168. void luaK_ret (FuncState *fs, int first, int nret) {
  169. OpCode op;
  170. switch (nret) {
  171. case 0: op = OP_RETURN0; break;
  172. case 1: op = OP_RETURN1; break;
  173. default: op = OP_RETURN; break;
  174. }
  175. luaK_codeABC(fs, op, first, nret + 1, 0);
  176. }
  177. /*
  178. ** Code a "conditional jump", that is, a test or comparison opcode
  179. ** followed by a jump. Return jump position.
  180. */
  181. static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
  182. luaK_codeABCk(fs, op, A, B, C, k);
  183. return luaK_jump(fs);
  184. }
  185. /*
  186. ** returns current 'pc' and marks it as a jump target (to avoid wrong
  187. ** optimizations with consecutive instructions not in the same basic block).
  188. */
  189. int luaK_getlabel (FuncState *fs) {
  190. fs->lasttarget = fs->pc;
  191. return fs->pc;
  192. }
  193. /*
  194. ** Returns the position of the instruction "controlling" a given
  195. ** jump (that is, its condition), or the jump itself if it is
  196. ** unconditional.
  197. */
  198. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  199. Instruction *pi = &fs->f->code[pc];
  200. if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
  201. return pi-1;
  202. else
  203. return pi;
  204. }
  205. /*
  206. ** Patch destination register for a TESTSET instruction.
  207. ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
  208. ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
  209. ** register. Otherwise, change instruction to a simple 'TEST' (produces
  210. ** no register value)
  211. */
  212. static int patchtestreg (FuncState *fs, int node, int reg) {
  213. Instruction *i = getjumpcontrol(fs, node);
  214. if (GET_OPCODE(*i) != OP_TESTSET)
  215. return 0; /* cannot patch other instructions */
  216. if (reg != NO_REG && reg != GETARG_B(*i))
  217. SETARG_A(*i, reg);
  218. else {
  219. /* no register to put value or register already has the value;
  220. change instruction to simple test */
  221. *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
  222. }
  223. return 1;
  224. }
  225. /*
  226. ** Traverse a list of tests ensuring no one produces a value
  227. */
  228. static void removevalues (FuncState *fs, int list) {
  229. for (; list != NO_JUMP; list = getjump(fs, list))
  230. patchtestreg(fs, list, NO_REG);
  231. }
  232. /*
  233. ** Traverse a list of tests, patching their destination address and
  234. ** registers: tests producing values jump to 'vtarget' (and put their
  235. ** values in 'reg'), other tests jump to 'dtarget'.
  236. */
  237. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
  238. int dtarget) {
  239. while (list != NO_JUMP) {
  240. int next = getjump(fs, list);
  241. if (patchtestreg(fs, list, reg))
  242. fixjump(fs, list, vtarget);
  243. else
  244. fixjump(fs, list, dtarget); /* jump to default target */
  245. list = next;
  246. }
  247. }
  248. /*
  249. ** Path all jumps in 'list' to jump to 'target'.
  250. ** (The assert means that we cannot fix a jump to a forward address
  251. ** because we only know addresses once code is generated.)
  252. */
  253. void luaK_patchlist (FuncState *fs, int list, int target) {
  254. lua_assert(target <= fs->pc);
  255. patchlistaux(fs, list, target, NO_REG, target);
  256. }
  257. void luaK_patchtohere (FuncState *fs, int list) {
  258. int hr = luaK_getlabel(fs); /* mark "here" as a jump target */
  259. luaK_patchlist(fs, list, hr);
  260. }
  261. /*
  262. ** MAXimum number of successive Instructions WiTHout ABSolute line
  263. ** information.
  264. */
  265. #if !defined(MAXIWTHABS)
  266. #define MAXIWTHABS 120
  267. #endif
  268. /* limit for difference between lines in relative line info. */
  269. #define LIMLINEDIFF 0x80
  270. /*
  271. ** Save line info for a new instruction. If difference from last line
  272. ** does not fit in a byte, of after that many instructions, save a new
  273. ** absolute line info; (in that case, the special value 'ABSLINEINFO'
  274. ** in 'lineinfo' signals the existence of this absolute information.)
  275. ** Otherwise, store the difference from last line in 'lineinfo'.
  276. */
  277. static void savelineinfo (FuncState *fs, Proto *f, int line) {
  278. int linedif = line - fs->previousline;
  279. int pc = fs->pc - 1; /* last instruction coded */
  280. if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) {
  281. luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
  282. f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
  283. f->abslineinfo[fs->nabslineinfo].pc = pc;
  284. f->abslineinfo[fs->nabslineinfo++].line = line;
  285. linedif = ABSLINEINFO; /* signal that there is absolute information */
  286. fs->iwthabs = 0; /* restart counter */
  287. }
  288. luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
  289. MAX_INT, "opcodes");
  290. f->lineinfo[pc] = linedif;
  291. fs->previousline = line; /* last line saved */
  292. }
  293. /*
  294. ** Remove line information from the last instruction.
  295. ** If line information for that instruction is absolute, set 'iwthabs'
  296. ** above its max to force the new (replacing) instruction to have
  297. ** absolute line info, too.
  298. */
  299. static void removelastlineinfo (FuncState *fs) {
  300. Proto *f = fs->f;
  301. int pc = fs->pc - 1; /* last instruction coded */
  302. if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */
  303. fs->previousline -= f->lineinfo[pc]; /* correct last line saved */
  304. fs->iwthabs--; /* undo previous increment */
  305. }
  306. else { /* absolute line information */
  307. lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
  308. fs->nabslineinfo--; /* remove it */
  309. fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */
  310. }
  311. }
  312. /*
  313. ** Remove the last instruction created, correcting line information
  314. ** accordingly.
  315. */
  316. static void removelastinstruction (FuncState *fs) {
  317. removelastlineinfo(fs);
  318. fs->pc--;
  319. }
  320. /*
  321. ** Emit instruction 'i', checking for array sizes and saving also its
  322. ** line information. Return 'i' position.
  323. */
  324. int luaK_code (FuncState *fs, Instruction i) {
  325. Proto *f = fs->f;
  326. /* put new instruction in code array */
  327. luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
  328. MAX_INT, "opcodes");
  329. f->code[fs->pc++] = i;
  330. savelineinfo(fs, f, fs->ls->lastline);
  331. return fs->pc - 1; /* index of new instruction */
  332. }
  333. /*
  334. ** Format and emit an 'iABC' instruction. (Assertions check consistency
  335. ** of parameters versus opcode.)
  336. */
  337. int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
  338. lua_assert(getOpMode(o) == iABC);
  339. lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
  340. c <= MAXARG_C && (k & ~1) == 0);
  341. return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
  342. }
  343. /*
  344. ** Format and emit an 'iABx' instruction.
  345. */
  346. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
  347. lua_assert(getOpMode(o) == iABx);
  348. lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
  349. return luaK_code(fs, CREATE_ABx(o, a, bc));
  350. }
  351. /*
  352. ** Format and emit an 'iAsBx' instruction.
  353. */
  354. int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
  355. unsigned int b = bc + OFFSET_sBx;
  356. lua_assert(getOpMode(o) == iAsBx);
  357. lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
  358. return luaK_code(fs, CREATE_ABx(o, a, b));
  359. }
  360. /*
  361. ** Format and emit an 'isJ' instruction.
  362. */
  363. static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
  364. unsigned int j = sj + OFFSET_sJ;
  365. lua_assert(getOpMode(o) == isJ);
  366. lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
  367. return luaK_code(fs, CREATE_sJ(o, j, k));
  368. }
  369. /*
  370. ** Emit an "extra argument" instruction (format 'iAx')
  371. */
  372. static int codeextraarg (FuncState *fs, int a) {
  373. lua_assert(a <= MAXARG_Ax);
  374. return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
  375. }
  376. /*
  377. ** Emit a "load constant" instruction, using either 'OP_LOADK'
  378. ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
  379. ** instruction with "extra argument".
  380. */
  381. static int luaK_codek (FuncState *fs, int reg, int k) {
  382. if (k <= MAXARG_Bx)
  383. return luaK_codeABx(fs, OP_LOADK, reg, k);
  384. else {
  385. int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
  386. codeextraarg(fs, k);
  387. return p;
  388. }
  389. }
  390. /*
  391. ** Check register-stack level, keeping track of its maximum size
  392. ** in field 'maxstacksize'
  393. */
  394. void luaK_checkstack (FuncState *fs, int n) {
  395. int newstack = fs->freereg + n;
  396. if (newstack > fs->f->maxstacksize) {
  397. if (newstack >= MAXREGS)
  398. luaX_syntaxerror(fs->ls,
  399. "function or expression needs too many registers");
  400. fs->f->maxstacksize = cast_byte(newstack);
  401. }
  402. }
  403. /*
  404. ** Reserve 'n' registers in register stack
  405. */
  406. void luaK_reserveregs (FuncState *fs, int n) {
  407. luaK_checkstack(fs, n);
  408. fs->freereg += n;
  409. }
  410. /*
  411. ** Free register 'reg', if it is neither a constant index nor
  412. ** a local variable.
  413. )
  414. */
  415. static void freereg (FuncState *fs, int reg) {
  416. if (reg >= luaY_nvarstack(fs)) {
  417. fs->freereg--;
  418. lua_assert(reg == fs->freereg);
  419. }
  420. }
  421. /*
  422. ** Free two registers in proper order
  423. */
  424. static void freeregs (FuncState *fs, int r1, int r2) {
  425. if (r1 > r2) {
  426. freereg(fs, r1);
  427. freereg(fs, r2);
  428. }
  429. else {
  430. freereg(fs, r2);
  431. freereg(fs, r1);
  432. }
  433. }
  434. /*
  435. ** Free register used by expression 'e' (if any)
  436. */
  437. static void freeexp (FuncState *fs, expdesc *e) {
  438. if (e->k == VNONRELOC)
  439. freereg(fs, e->u.info);
  440. }
  441. /*
  442. ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
  443. ** order.
  444. */
  445. static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
  446. int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
  447. int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
  448. freeregs(fs, r1, r2);
  449. }
  450. /*
  451. ** Add constant 'v' to prototype's list of constants (field 'k').
  452. ** Use scanner's table to cache position of constants in constant list
  453. ** and try to reuse constants. Because some values should not be used
  454. ** as keys (nil cannot be a key, integer keys can collapse with float
  455. ** keys), the caller must provide a useful 'key' for indexing the cache.
  456. */
  457. static int addk (FuncState *fs, TValue *key, TValue *v) {
  458. lua_State *L = fs->ls->L;
  459. Proto *f = fs->f;
  460. TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
  461. int k, oldsize;
  462. if (ttisinteger(idx)) { /* is there an index there? */
  463. k = cast_int(ivalue(idx));
  464. /* correct value? (warning: must distinguish floats from integers!) */
  465. if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
  466. luaV_rawequalobj(&f->k[k], v))
  467. return k; /* reuse index */
  468. }
  469. /* constant not found; create a new entry */
  470. oldsize = f->sizek;
  471. k = fs->nk;
  472. /* numerical value does not need GC barrier;
  473. table has no metatable, so it does not need to invalidate cache */
  474. setivalue(idx, k);
  475. luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  476. while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  477. setobj(L, &f->k[k], v);
  478. fs->nk++;
  479. luaC_barrier(L, f, v);
  480. return k;
  481. }
  482. /*
  483. ** Add a string to list of constants and return its index.
  484. */
  485. static int stringK (FuncState *fs, TString *s) {
  486. TValue o;
  487. setsvalue(fs->ls->L, &o, s);
  488. return addk(fs, &o, &o); /* use string itself as key */
  489. }
  490. /*
  491. ** Add an integer to list of constants and return its index.
  492. ** Integers use userdata as keys to avoid collision with floats with
  493. ** same value; conversion to 'void*' is used only for hashing, so there
  494. ** are no "precision" problems.
  495. */
  496. static int luaK_intK (FuncState *fs, lua_Integer n) {
  497. TValue k, o;
  498. setpvalue(&k, cast_voidp(cast_sizet(n)));
  499. setivalue(&o, n);
  500. return addk(fs, &k, &o);
  501. }
  502. /*
  503. ** Add a float to list of constants and return its index.
  504. */
  505. static int luaK_numberK (FuncState *fs, lua_Number r) {
  506. TValue o;
  507. setfltvalue(&o, r);
  508. return addk(fs, &o, &o); /* use number itself as key */
  509. }
  510. /*
  511. ** Add a boolean to list of constants and return its index.
  512. */
  513. static int boolK (FuncState *fs, int b) {
  514. TValue o;
  515. setbvalue(&o, b);
  516. return addk(fs, &o, &o); /* use boolean itself as key */
  517. }
  518. /*
  519. ** Add nil to list of constants and return its index.
  520. */
  521. static int nilK (FuncState *fs) {
  522. TValue k, v;
  523. setnilvalue(&v);
  524. /* cannot use nil as key; instead use table itself to represent nil */
  525. sethvalue(fs->ls->L, &k, fs->ls->h);
  526. return addk(fs, &k, &v);
  527. }
  528. /*
  529. ** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
  530. ** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
  531. ** overflows in the hidden addition inside 'int2sC'.
  532. */
  533. static int fitsC (lua_Integer i) {
  534. return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
  535. }
  536. /*
  537. ** Check whether 'i' can be stored in an 'sBx' operand.
  538. */
  539. static int fitsBx (lua_Integer i) {
  540. return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
  541. }
  542. void luaK_int (FuncState *fs, int reg, lua_Integer i) {
  543. if (fitsBx(i))
  544. luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
  545. else
  546. luaK_codek(fs, reg, luaK_intK(fs, i));
  547. }
  548. static void luaK_float (FuncState *fs, int reg, lua_Number f) {
  549. lua_Integer fi;
  550. if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
  551. luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
  552. else
  553. luaK_codek(fs, reg, luaK_numberK(fs, f));
  554. }
  555. /*
  556. ** Convert a constant in 'v' into an expression description 'e'
  557. */
  558. static void const2exp (TValue *v, expdesc *e) {
  559. switch (ttypetag(v)) {
  560. case LUA_TNUMINT:
  561. e->k = VKINT; e->u.ival = ivalue(v);
  562. break;
  563. case LUA_TNUMFLT:
  564. e->k = VKFLT; e->u.nval = fltvalue(v);
  565. break;
  566. case LUA_TBOOLEAN:
  567. e->k = bvalue(v) ? VTRUE : VFALSE;
  568. break;
  569. case LUA_TNIL:
  570. e->k = VNIL;
  571. break;
  572. case LUA_TSHRSTR: case LUA_TLNGSTR:
  573. e->k = VKSTR; e->u.strval = tsvalue(v);
  574. break;
  575. default: lua_assert(0);
  576. }
  577. }
  578. /*
  579. ** Fix an expression to return the number of results 'nresults'.
  580. ** Either 'e' is a multi-ret expression (function call or vararg)
  581. ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
  582. */
  583. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
  584. Instruction *pc = &getinstruction(fs, e);
  585. if (e->k == VCALL) /* expression is an open function call? */
  586. SETARG_C(*pc, nresults + 1);
  587. else if (e->k == VVARARG) {
  588. SETARG_C(*pc, nresults + 1);
  589. SETARG_A(*pc, fs->freereg);
  590. luaK_reserveregs(fs, 1);
  591. }
  592. else lua_assert(nresults == LUA_MULTRET);
  593. }
  594. /*
  595. ** Convert a VKSTR to a VK
  596. */
  597. static void str2K (FuncState *fs, expdesc *e) {
  598. lua_assert(e->k == VKSTR);
  599. e->u.info = stringK(fs, e->u.strval);
  600. e->k = VK;
  601. }
  602. /*
  603. ** Fix an expression to return one result.
  604. ** If expression is not a multi-ret expression (function call or
  605. ** vararg), it already returns one result, so nothing needs to be done.
  606. ** Function calls become VNONRELOC expressions (as its result comes
  607. ** fixed in the base register of the call), while vararg expressions
  608. ** become VRELOC (as OP_VARARG puts its results where it wants).
  609. ** (Calls are created returning one result, so that does not need
  610. ** to be fixed.)
  611. */
  612. void luaK_setoneret (FuncState *fs, expdesc *e) {
  613. if (e->k == VCALL) { /* expression is an open function call? */
  614. /* already returns 1 value */
  615. lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
  616. e->k = VNONRELOC; /* result has fixed position */
  617. e->u.info = GETARG_A(getinstruction(fs, e));
  618. }
  619. else if (e->k == VVARARG) {
  620. SETARG_C(getinstruction(fs, e), 2);
  621. e->k = VRELOC; /* can relocate its simple result */
  622. }
  623. }
  624. /*
  625. ** Ensure that expression 'e' is not a variable (nor a constant).
  626. ** (Expression still may have jump lists.)
  627. */
  628. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  629. switch (e->k) {
  630. case VCONST: {
  631. const2exp(const2val(fs, e), e);
  632. break;
  633. }
  634. case VLOCAL: { /* already in a register */
  635. e->u.info = e->u.var.sidx;
  636. e->k = VNONRELOC; /* becomes a non-relocatable value */
  637. break;
  638. }
  639. case VUPVAL: { /* move value to some (pending) register */
  640. e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
  641. e->k = VRELOC;
  642. break;
  643. }
  644. case VINDEXUP: {
  645. e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
  646. e->k = VRELOC;
  647. break;
  648. }
  649. case VINDEXI: {
  650. freereg(fs, e->u.ind.t);
  651. e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
  652. e->k = VRELOC;
  653. break;
  654. }
  655. case VINDEXSTR: {
  656. freereg(fs, e->u.ind.t);
  657. e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
  658. e->k = VRELOC;
  659. break;
  660. }
  661. case VINDEXED: {
  662. freeregs(fs, e->u.ind.t, e->u.ind.idx);
  663. e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
  664. e->k = VRELOC;
  665. break;
  666. }
  667. case VVARARG: case VCALL: {
  668. luaK_setoneret(fs, e);
  669. break;
  670. }
  671. default: break; /* there is one value available (somewhere) */
  672. }
  673. }
  674. /*
  675. ** Ensures expression value is in register 'reg' (and therefore
  676. ** 'e' will become a non-relocatable expression).
  677. ** (Expression still may have jump lists.)
  678. */
  679. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  680. luaK_dischargevars(fs, e);
  681. switch (e->k) {
  682. case VNIL: {
  683. luaK_nil(fs, reg, 1);
  684. break;
  685. }
  686. case VFALSE: case VTRUE: {
  687. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  688. break;
  689. }
  690. case VKSTR: {
  691. str2K(fs, e);
  692. } /* FALLTHROUGH */
  693. case VK: {
  694. luaK_codek(fs, reg, e->u.info);
  695. break;
  696. }
  697. case VKFLT: {
  698. luaK_float(fs, reg, e->u.nval);
  699. break;
  700. }
  701. case VKINT: {
  702. luaK_int(fs, reg, e->u.ival);
  703. break;
  704. }
  705. case VRELOC: {
  706. Instruction *pc = &getinstruction(fs, e);
  707. SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
  708. break;
  709. }
  710. case VNONRELOC: {
  711. if (reg != e->u.info)
  712. luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
  713. break;
  714. }
  715. default: {
  716. lua_assert(e->k == VJMP);
  717. return; /* nothing to do... */
  718. }
  719. }
  720. e->u.info = reg;
  721. e->k = VNONRELOC;
  722. }
  723. /*
  724. ** Ensures expression value is in any register.
  725. ** (Expression still may have jump lists.)
  726. */
  727. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  728. if (e->k != VNONRELOC) { /* no fixed register yet? */
  729. luaK_reserveregs(fs, 1); /* get a register */
  730. discharge2reg(fs, e, fs->freereg-1); /* put value there */
  731. }
  732. }
  733. static int code_loadbool (FuncState *fs, int A, int b, int jump) {
  734. luaK_getlabel(fs); /* those instructions may be jump targets */
  735. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  736. }
  737. /*
  738. ** check whether list has any jump that do not produce a value
  739. ** or produce an inverted value
  740. */
  741. static int need_value (FuncState *fs, int list) {
  742. for (; list != NO_JUMP; list = getjump(fs, list)) {
  743. Instruction i = *getjumpcontrol(fs, list);
  744. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  745. }
  746. return 0; /* not found */
  747. }
  748. /*
  749. ** Ensures final expression result (which includes results from its
  750. ** jump lists) is in register 'reg'.
  751. ** If expression has jumps, need to patch these jumps either to
  752. ** its final position or to "load" instructions (for those tests
  753. ** that do not produce values).
  754. */
  755. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  756. discharge2reg(fs, e, reg);
  757. if (e->k == VJMP) /* expression itself is a test? */
  758. luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
  759. if (hasjumps(e)) {
  760. int final; /* position after whole expression */
  761. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  762. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  763. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  764. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  765. p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */
  766. p_t = code_loadbool(fs, reg, 1, 0); /* load true */
  767. /* jump around these booleans if 'e' is not a test */
  768. luaK_patchtohere(fs, fj);
  769. }
  770. final = luaK_getlabel(fs);
  771. patchlistaux(fs, e->f, final, reg, p_f);
  772. patchlistaux(fs, e->t, final, reg, p_t);
  773. }
  774. e->f = e->t = NO_JUMP;
  775. e->u.info = reg;
  776. e->k = VNONRELOC;
  777. }
  778. /*
  779. ** Ensures final expression result is in next available register.
  780. */
  781. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  782. luaK_dischargevars(fs, e);
  783. freeexp(fs, e);
  784. luaK_reserveregs(fs, 1);
  785. exp2reg(fs, e, fs->freereg - 1);
  786. }
  787. /*
  788. ** Ensures final expression result is in some (any) register
  789. ** and return that register.
  790. */
  791. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  792. luaK_dischargevars(fs, e);
  793. if (e->k == VNONRELOC) { /* expression already has a register? */
  794. if (!hasjumps(e)) /* no jumps? */
  795. return e->u.info; /* result is already in a register */
  796. if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
  797. exp2reg(fs, e, e->u.info); /* put final result in it */
  798. return e->u.info;
  799. }
  800. }
  801. luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
  802. return e->u.info;
  803. }
  804. /*
  805. ** Ensures final expression result is either in a register
  806. ** or in an upvalue.
  807. */
  808. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  809. if (e->k != VUPVAL || hasjumps(e))
  810. luaK_exp2anyreg(fs, e);
  811. }
  812. /*
  813. ** Ensures final expression result is either in a register
  814. ** or it is a constant.
  815. */
  816. void luaK_exp2val (FuncState *fs, expdesc *e) {
  817. if (hasjumps(e))
  818. luaK_exp2anyreg(fs, e);
  819. else
  820. luaK_dischargevars(fs, e);
  821. }
  822. /*
  823. ** Try to make 'e' a K expression with an index in the range of R/K
  824. ** indices. Return true iff succeeded.
  825. */
  826. static int luaK_exp2K (FuncState *fs, expdesc *e) {
  827. if (!hasjumps(e)) {
  828. int info;
  829. switch (e->k) { /* move constants to 'k' */
  830. case VTRUE: info = boolK(fs, 1); break;
  831. case VFALSE: info = boolK(fs, 0); break;
  832. case VNIL: info = nilK(fs); break;
  833. case VKINT: info = luaK_intK(fs, e->u.ival); break;
  834. case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
  835. case VKSTR: info = stringK(fs, e->u.strval); break;
  836. case VK: info = e->u.info; break;
  837. default: return 0; /* not a constant */
  838. }
  839. if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
  840. e->k = VK; /* make expression a 'K' expression */
  841. e->u.info = info;
  842. return 1;
  843. }
  844. }
  845. /* else, expression doesn't fit; leave it unchanged */
  846. return 0;
  847. }
  848. /*
  849. ** Ensures final expression result is in a valid R/K index
  850. ** (that is, it is either in a register or in 'k' with an index
  851. ** in the range of R/K indices).
  852. ** Returns 1 iff expression is K.
  853. */
  854. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  855. if (luaK_exp2K(fs, e))
  856. return 1;
  857. else { /* not a constant in the right range: put it in a register */
  858. luaK_exp2anyreg(fs, e);
  859. return 0;
  860. }
  861. }
  862. static void codeABRK (FuncState *fs, OpCode o, int a, int b,
  863. expdesc *ec) {
  864. int k = luaK_exp2RK(fs, ec);
  865. luaK_codeABCk(fs, o, a, b, ec->u.info, k);
  866. }
  867. /*
  868. ** Generate code to store result of expression 'ex' into variable 'var'.
  869. */
  870. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  871. switch (var->k) {
  872. case VLOCAL: {
  873. freeexp(fs, ex);
  874. exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */
  875. return;
  876. }
  877. case VUPVAL: {
  878. int e = luaK_exp2anyreg(fs, ex);
  879. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  880. break;
  881. }
  882. case VINDEXUP: {
  883. codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
  884. break;
  885. }
  886. case VINDEXI: {
  887. codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
  888. break;
  889. }
  890. case VINDEXSTR: {
  891. codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
  892. break;
  893. }
  894. case VINDEXED: {
  895. codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
  896. break;
  897. }
  898. default: lua_assert(0); /* invalid var kind to store */
  899. }
  900. freeexp(fs, ex);
  901. }
  902. /*
  903. ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
  904. */
  905. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  906. int ereg;
  907. luaK_exp2anyreg(fs, e);
  908. ereg = e->u.info; /* register where 'e' was placed */
  909. freeexp(fs, e);
  910. e->u.info = fs->freereg; /* base register for op_self */
  911. e->k = VNONRELOC; /* self expression has a fixed register */
  912. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  913. codeABRK(fs, OP_SELF, e->u.info, ereg, key);
  914. freeexp(fs, key);
  915. }
  916. /*
  917. ** Negate condition 'e' (where 'e' is a comparison).
  918. */
  919. static void negatecondition (FuncState *fs, expdesc *e) {
  920. Instruction *pc = getjumpcontrol(fs, e->u.info);
  921. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  922. GET_OPCODE(*pc) != OP_TEST);
  923. SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
  924. }
  925. /*
  926. ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
  927. ** is true, code will jump if 'e' is true.) Return jump position.
  928. ** Optimize when 'e' is 'not' something, inverting the condition
  929. ** and removing the 'not'.
  930. */
  931. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  932. if (e->k == VRELOC) {
  933. Instruction ie = getinstruction(fs, e);
  934. if (GET_OPCODE(ie) == OP_NOT) {
  935. removelastinstruction(fs); /* remove previous OP_NOT */
  936. return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
  937. }
  938. /* else go through */
  939. }
  940. discharge2anyreg(fs, e);
  941. freeexp(fs, e);
  942. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
  943. }
  944. /*
  945. ** Emit code to go through if 'e' is true, jump otherwise.
  946. */
  947. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  948. int pc; /* pc of new jump */
  949. luaK_dischargevars(fs, e);
  950. switch (e->k) {
  951. case VJMP: { /* condition? */
  952. negatecondition(fs, e); /* jump when it is false */
  953. pc = e->u.info; /* save jump position */
  954. break;
  955. }
  956. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  957. pc = NO_JUMP; /* always true; do nothing */
  958. break;
  959. }
  960. default: {
  961. pc = jumponcond(fs, e, 0); /* jump when false */
  962. break;
  963. }
  964. }
  965. luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
  966. luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
  967. e->t = NO_JUMP;
  968. }
  969. /*
  970. ** Emit code to go through if 'e' is false, jump otherwise.
  971. */
  972. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  973. int pc; /* pc of new jump */
  974. luaK_dischargevars(fs, e);
  975. switch (e->k) {
  976. case VJMP: {
  977. pc = e->u.info; /* already jump if true */
  978. break;
  979. }
  980. case VNIL: case VFALSE: {
  981. pc = NO_JUMP; /* always false; do nothing */
  982. break;
  983. }
  984. default: {
  985. pc = jumponcond(fs, e, 1); /* jump if true */
  986. break;
  987. }
  988. }
  989. luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
  990. luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
  991. e->f = NO_JUMP;
  992. }
  993. /*
  994. ** Code 'not e', doing constant folding.
  995. */
  996. static void codenot (FuncState *fs, expdesc *e) {
  997. switch (e->k) {
  998. case VNIL: case VFALSE: {
  999. e->k = VTRUE; /* true == not nil == not false */
  1000. break;
  1001. }
  1002. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  1003. e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
  1004. break;
  1005. }
  1006. case VJMP: {
  1007. negatecondition(fs, e);
  1008. break;
  1009. }
  1010. case VRELOC:
  1011. case VNONRELOC: {
  1012. discharge2anyreg(fs, e);
  1013. freeexp(fs, e);
  1014. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  1015. e->k = VRELOC;
  1016. break;
  1017. }
  1018. default: lua_assert(0); /* cannot happen */
  1019. }
  1020. /* interchange true and false lists */
  1021. { int temp = e->f; e->f = e->t; e->t = temp; }
  1022. removevalues(fs, e->f); /* values are useless when negated */
  1023. removevalues(fs, e->t);
  1024. }
  1025. /*
  1026. ** Check whether expression 'e' is a small literal string
  1027. */
  1028. static int isKstr (FuncState *fs, expdesc *e) {
  1029. return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
  1030. ttisshrstring(&fs->f->k[e->u.info]));
  1031. }
  1032. /*
  1033. ** Check whether expression 'e' is a literal integer.
  1034. */
  1035. int luaK_isKint (expdesc *e) {
  1036. return (e->k == VKINT && !hasjumps(e));
  1037. }
  1038. /*
  1039. ** Check whether expression 'e' is a literal integer in
  1040. ** proper range to fit in register C
  1041. */
  1042. static int isCint (expdesc *e) {
  1043. return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
  1044. }
  1045. /*
  1046. ** Check whether expression 'e' is a literal integer in
  1047. ** proper range to fit in register sC
  1048. */
  1049. static int isSCint (expdesc *e) {
  1050. return luaK_isKint(e) && fitsC(e->u.ival);
  1051. }
  1052. /*
  1053. ** Check whether expression 'e' is a literal integer or float in
  1054. ** proper range to fit in a register (sB or sC).
  1055. */
  1056. static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
  1057. lua_Integer i;
  1058. if (e->k == VKINT)
  1059. i = e->u.ival;
  1060. else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
  1061. *isfloat = 1;
  1062. else
  1063. return 0; /* not a number */
  1064. if (!hasjumps(e) && fitsC(i)) {
  1065. *pi = int2sC(cast_int(i));
  1066. return 1;
  1067. }
  1068. else
  1069. return 0;
  1070. }
  1071. /*
  1072. ** Create expression 't[k]'. 't' must have its final result already in a
  1073. ** register or upvalue. Upvalues can only be indexed by literal strings.
  1074. ** Keys can be literal strings in the constant table or arbitrary
  1075. ** values in registers.
  1076. */
  1077. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  1078. if (k->k == VKSTR)
  1079. str2K(fs, k);
  1080. lua_assert(!hasjumps(t) &&
  1081. (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
  1082. if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
  1083. luaK_exp2anyreg(fs, t); /* put it in a register */
  1084. if (t->k == VUPVAL) {
  1085. t->u.ind.t = t->u.info; /* upvalue index */
  1086. t->u.ind.idx = k->u.info; /* literal string */
  1087. t->k = VINDEXUP;
  1088. }
  1089. else {
  1090. /* register index of the table */
  1091. t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info;
  1092. if (isKstr(fs, k)) {
  1093. t->u.ind.idx = k->u.info; /* literal string */
  1094. t->k = VINDEXSTR;
  1095. }
  1096. else if (isCint(k)) {
  1097. t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
  1098. t->k = VINDEXI;
  1099. }
  1100. else {
  1101. t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
  1102. t->k = VINDEXED;
  1103. }
  1104. }
  1105. }
  1106. /*
  1107. ** Return false if folding can raise an error.
  1108. ** Bitwise operations need operands convertible to integers; division
  1109. ** operations cannot have 0 as divisor.
  1110. */
  1111. static int validop (int op, TValue *v1, TValue *v2) {
  1112. switch (op) {
  1113. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  1114. case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
  1115. lua_Integer i;
  1116. return (tointegerns(v1, &i) && tointegerns(v2, &i));
  1117. }
  1118. case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
  1119. return (nvalue(v2) != 0);
  1120. default: return 1; /* everything else is valid */
  1121. }
  1122. }
  1123. /*
  1124. ** Try to "constant-fold" an operation; return 1 iff successful.
  1125. ** (In this case, 'e1' has the final result.)
  1126. */
  1127. static int constfolding (FuncState *fs, int op, expdesc *e1,
  1128. const expdesc *e2) {
  1129. TValue v1, v2, res;
  1130. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  1131. return 0; /* non-numeric operands or not safe to fold */
  1132. luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
  1133. if (ttisinteger(&res)) {
  1134. e1->k = VKINT;
  1135. e1->u.ival = ivalue(&res);
  1136. }
  1137. else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
  1138. lua_Number n = fltvalue(&res);
  1139. if (luai_numisnan(n) || n == 0)
  1140. return 0;
  1141. e1->k = VKFLT;
  1142. e1->u.nval = n;
  1143. }
  1144. return 1;
  1145. }
  1146. /*
  1147. ** Emit code for unary expressions that "produce values"
  1148. ** (everything but 'not').
  1149. ** Expression to produce final result will be encoded in 'e'.
  1150. */
  1151. static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
  1152. int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
  1153. freeexp(fs, e);
  1154. e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
  1155. e->k = VRELOC; /* all those operations are relocatable */
  1156. luaK_fixline(fs, line);
  1157. }
  1158. /*
  1159. ** Emit code for binary expressions that "produce values"
  1160. ** (everything but logical operators 'and'/'or' and comparison
  1161. ** operators).
  1162. ** Expression to produce final result will be encoded in 'e1'.
  1163. */
  1164. static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
  1165. OpCode op, int v2, int flip, int line,
  1166. OpCode mmop, TMS event) {
  1167. int v1 = luaK_exp2anyreg(fs, e1);
  1168. int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
  1169. freeexps(fs, e1, e2);
  1170. e1->u.info = pc;
  1171. e1->k = VRELOC; /* all those operations are relocatable */
  1172. luaK_fixline(fs, line);
  1173. luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
  1174. luaK_fixline(fs, line);
  1175. }
  1176. /*
  1177. ** Emit code for binary expressions that "produce values" over
  1178. ** two registers.
  1179. */
  1180. static void codebinexpval (FuncState *fs, OpCode op,
  1181. expdesc *e1, expdesc *e2, int line) {
  1182. int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
  1183. lua_assert(OP_ADD <= op && op <= OP_SHR);
  1184. finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
  1185. cast(TMS, (op - OP_ADD) + TM_ADD));
  1186. }
  1187. /*
  1188. ** Code binary operators with immediate operands.
  1189. */
  1190. static void codebini (FuncState *fs, OpCode op,
  1191. expdesc *e1, expdesc *e2, int flip, int line,
  1192. TMS event) {
  1193. int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */
  1194. lua_assert(e2->k == VKINT);
  1195. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
  1196. }
  1197. /* Try to code a binary operator negating its second operand.
  1198. ** For the metamethod, 2nd operand must keep its original value.
  1199. */
  1200. static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
  1201. OpCode op, int line, TMS event) {
  1202. if (!luaK_isKint(e2))
  1203. return 0; /* not an integer constant */
  1204. else {
  1205. lua_Integer i2 = e2->u.ival;
  1206. if (!(fitsC(i2) && fitsC(-i2)))
  1207. return 0; /* not in the proper range */
  1208. else { /* operating a small integer constant */
  1209. int v2 = cast_int(i2);
  1210. finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
  1211. /* correct metamethod argument */
  1212. SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
  1213. return 1; /* successfully coded */
  1214. }
  1215. }
  1216. }
  1217. static void swapexps (expdesc *e1, expdesc *e2) {
  1218. expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
  1219. }
  1220. /*
  1221. ** Code arithmetic operators ('+', '-', ...). If second operand is a
  1222. ** constant in the proper range, use variant opcodes with K operands.
  1223. */
  1224. static void codearith (FuncState *fs, BinOpr opr,
  1225. expdesc *e1, expdesc *e2, int flip, int line) {
  1226. TMS event = cast(TMS, opr + TM_ADD);
  1227. if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
  1228. int v2 = e2->u.info; /* K index */
  1229. OpCode op = cast(OpCode, opr + OP_ADDK);
  1230. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
  1231. }
  1232. else { /* 'e2' is neither an immediate nor a K operand */
  1233. OpCode op = cast(OpCode, opr + OP_ADD);
  1234. if (flip)
  1235. swapexps(e1, e2); /* back to original order */
  1236. codebinexpval(fs, op, e1, e2, line); /* use standard operators */
  1237. }
  1238. }
  1239. /*
  1240. ** Code commutative operators ('+', '*'). If first operand is a
  1241. ** numeric constant, change order of operands to try to use an
  1242. ** immediate or K operator.
  1243. */
  1244. static void codecommutative (FuncState *fs, BinOpr op,
  1245. expdesc *e1, expdesc *e2, int line) {
  1246. int flip = 0;
  1247. if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
  1248. swapexps(e1, e2); /* change order */
  1249. flip = 1;
  1250. }
  1251. if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
  1252. codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
  1253. else
  1254. codearith(fs, op, e1, e2, flip, line);
  1255. }
  1256. /*
  1257. ** Code bitwise operations; they are all associative, so the function
  1258. ** tries to put an integer constant as the 2nd operand (a K operand).
  1259. */
  1260. static void codebitwise (FuncState *fs, BinOpr opr,
  1261. expdesc *e1, expdesc *e2, int line) {
  1262. int flip = 0;
  1263. int v2;
  1264. OpCode op;
  1265. if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
  1266. swapexps(e1, e2); /* 'e2' will be the constant operand */
  1267. flip = 1;
  1268. }
  1269. else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
  1270. op = cast(OpCode, opr + OP_ADD);
  1271. codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
  1272. return;
  1273. }
  1274. v2 = e2->u.info; /* index in K array */
  1275. op = cast(OpCode, opr + OP_ADDK);
  1276. lua_assert(ttisinteger(&fs->f->k[v2]));
  1277. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
  1278. cast(TMS, opr + TM_ADD));
  1279. }
  1280. /*
  1281. ** Emit code for order comparisons. When using an immediate operand,
  1282. ** 'isfloat' tells whether the original value was a float.
  1283. */
  1284. static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
  1285. int r1, r2;
  1286. int im;
  1287. int isfloat = 0;
  1288. if (isSCnumber(e2, &im, &isfloat)) {
  1289. /* use immediate operand */
  1290. r1 = luaK_exp2anyreg(fs, e1);
  1291. r2 = im;
  1292. op = cast(OpCode, (op - OP_LT) + OP_LTI);
  1293. }
  1294. else if (isSCnumber(e1, &im, &isfloat)) {
  1295. /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
  1296. r1 = luaK_exp2anyreg(fs, e2);
  1297. r2 = im;
  1298. op = (op == OP_LT) ? OP_GTI : OP_GEI;
  1299. }
  1300. else { /* regular case, compare two registers */
  1301. r1 = luaK_exp2anyreg(fs, e1);
  1302. r2 = luaK_exp2anyreg(fs, e2);
  1303. }
  1304. freeexps(fs, e1, e2);
  1305. e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
  1306. e1->k = VJMP;
  1307. }
  1308. /*
  1309. ** Emit code for equality comparisons ('==', '~=').
  1310. ** 'e1' was already put as RK by 'luaK_infix'.
  1311. */
  1312. static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
  1313. int r1, r2;
  1314. int im;
  1315. int isfloat = 0; /* not needed here, but kept for symmetry */
  1316. OpCode op;
  1317. if (e1->k != VNONRELOC) {
  1318. lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
  1319. swapexps(e1, e2);
  1320. }
  1321. r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
  1322. if (isSCnumber(e2, &im, &isfloat)) {
  1323. op = OP_EQI;
  1324. r2 = im; /* immediate operand */
  1325. }
  1326. else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */
  1327. op = OP_EQK;
  1328. r2 = e2->u.info; /* constant index */
  1329. }
  1330. else {
  1331. op = OP_EQ; /* will compare two registers */
  1332. r2 = luaK_exp2anyreg(fs, e2);
  1333. }
  1334. freeexps(fs, e1, e2);
  1335. e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
  1336. e1->k = VJMP;
  1337. }
  1338. /*
  1339. ** Apply prefix operation 'op' to expression 'e'.
  1340. */
  1341. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  1342. static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
  1343. luaK_dischargevars(fs, e);
  1344. switch (op) {
  1345. case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
  1346. if (constfolding(fs, op + LUA_OPUNM, e, &ef))
  1347. break;
  1348. /* else */ /* FALLTHROUGH */
  1349. case OPR_LEN:
  1350. codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
  1351. break;
  1352. case OPR_NOT: codenot(fs, e); break;
  1353. default: lua_assert(0);
  1354. }
  1355. }
  1356. /*
  1357. ** Process 1st operand 'v' of binary operation 'op' before reading
  1358. ** 2nd operand.
  1359. */
  1360. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  1361. luaK_dischargevars(fs, v);
  1362. switch (op) {
  1363. case OPR_AND: {
  1364. luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
  1365. break;
  1366. }
  1367. case OPR_OR: {
  1368. luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
  1369. break;
  1370. }
  1371. case OPR_CONCAT: {
  1372. luaK_exp2nextreg(fs, v); /* operand must be on the stack */
  1373. break;
  1374. }
  1375. case OPR_ADD: case OPR_SUB:
  1376. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  1377. case OPR_MOD: case OPR_POW:
  1378. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1379. case OPR_SHL: case OPR_SHR: {
  1380. if (!tonumeral(v, NULL))
  1381. luaK_exp2anyreg(fs, v);
  1382. /* else keep numeral, which may be folded with 2nd operand */
  1383. break;
  1384. }
  1385. case OPR_EQ: case OPR_NE: {
  1386. if (!tonumeral(v, NULL))
  1387. luaK_exp2RK(fs, v);
  1388. /* else keep numeral, which may be an immediate operand */
  1389. break;
  1390. }
  1391. case OPR_LT: case OPR_LE:
  1392. case OPR_GT: case OPR_GE: {
  1393. int dummy, dummy2;
  1394. if (!isSCnumber(v, &dummy, &dummy2))
  1395. luaK_exp2anyreg(fs, v);
  1396. /* else keep numeral, which may be an immediate operand */
  1397. break;
  1398. }
  1399. default: lua_assert(0);
  1400. }
  1401. }
  1402. /*
  1403. ** Create code for '(e1 .. e2)'.
  1404. ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
  1405. ** because concatenation is right associative), merge both CONCATs.
  1406. */
  1407. static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
  1408. Instruction *ie2 = previousinstruction(fs);
  1409. if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
  1410. int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
  1411. lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
  1412. freeexp(fs, e2);
  1413. SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
  1414. SETARG_B(*ie2, n + 1); /* will concatenate one more element */
  1415. }
  1416. else { /* 'e2' is not a concatenation */
  1417. luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
  1418. freeexp(fs, e2);
  1419. luaK_fixline(fs, line);
  1420. }
  1421. }
  1422. /*
  1423. ** Finalize code for binary operation, after reading 2nd operand.
  1424. */
  1425. void luaK_posfix (FuncState *fs, BinOpr opr,
  1426. expdesc *e1, expdesc *e2, int line) {
  1427. luaK_dischargevars(fs, e2);
  1428. if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
  1429. return; /* done by folding */
  1430. switch (opr) {
  1431. case OPR_AND: {
  1432. lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
  1433. luaK_concat(fs, &e2->f, e1->f);
  1434. *e1 = *e2;
  1435. break;
  1436. }
  1437. case OPR_OR: {
  1438. lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
  1439. luaK_concat(fs, &e2->t, e1->t);
  1440. *e1 = *e2;
  1441. break;
  1442. }
  1443. case OPR_CONCAT: { /* e1 .. e2 */
  1444. luaK_exp2nextreg(fs, e2);
  1445. codeconcat(fs, e1, e2, line);
  1446. break;
  1447. }
  1448. case OPR_ADD: case OPR_MUL: {
  1449. codecommutative(fs, opr, e1, e2, line);
  1450. break;
  1451. }
  1452. case OPR_SUB: {
  1453. if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
  1454. break; /* coded as (r1 + -I) */
  1455. /* ELSE */
  1456. } /* FALLTHROUGH */
  1457. case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
  1458. codearith(fs, opr, e1, e2, 0, line);
  1459. break;
  1460. }
  1461. case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
  1462. codebitwise(fs, opr, e1, e2, line);
  1463. break;
  1464. }
  1465. case OPR_SHL: {
  1466. if (isSCint(e1)) {
  1467. swapexps(e1, e2);
  1468. codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */
  1469. }
  1470. else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
  1471. /* coded as (r1 >> -I) */;
  1472. }
  1473. else /* regular case (two registers) */
  1474. codebinexpval(fs, OP_SHL, e1, e2, line);
  1475. break;
  1476. }
  1477. case OPR_SHR: {
  1478. if (isSCint(e2))
  1479. codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
  1480. else /* regular case (two registers) */
  1481. codebinexpval(fs, OP_SHR, e1, e2, line);
  1482. break;
  1483. }
  1484. case OPR_EQ: case OPR_NE: {
  1485. codeeq(fs, opr, e1, e2);
  1486. break;
  1487. }
  1488. case OPR_LT: case OPR_LE: {
  1489. OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
  1490. codeorder(fs, op, e1, e2);
  1491. break;
  1492. }
  1493. case OPR_GT: case OPR_GE: {
  1494. /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
  1495. OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
  1496. swapexps(e1, e2);
  1497. codeorder(fs, op, e1, e2);
  1498. break;
  1499. }
  1500. default: lua_assert(0);
  1501. }
  1502. }
  1503. /*
  1504. ** Change line information associated with current position, by removing
  1505. ** previous info and adding it again with new line.
  1506. */
  1507. void luaK_fixline (FuncState *fs, int line) {
  1508. removelastlineinfo(fs);
  1509. savelineinfo(fs, fs->f, line);
  1510. }
  1511. void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) {
  1512. Instruction *inst = &fs->f->code[pc];
  1513. int extra = 0;
  1514. int k = 0;
  1515. if (rb != 0)
  1516. rb = luaO_ceillog2(rb) + 1; /* hash size */
  1517. if (rc > MAXARG_C) { /* does it need the extra argument? */
  1518. extra = rc / (MAXARG_C + 1);
  1519. rc %= (MAXARG_C + 1);
  1520. k = 1;
  1521. }
  1522. *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
  1523. *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
  1524. }
  1525. /*
  1526. ** Emit a SETLIST instruction.
  1527. ** 'base' is register that keeps table;
  1528. ** 'nelems' is #table plus those to be stored now;
  1529. ** 'tostore' is number of values (in registers 'base + 1',...) to add to
  1530. ** table (or LUA_MULTRET to add up to stack top).
  1531. */
  1532. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  1533. lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
  1534. if (tostore == LUA_MULTRET)
  1535. tostore = 0;
  1536. if (nelems <= MAXARG_C)
  1537. luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
  1538. else {
  1539. int extra = nelems / (MAXARG_C + 1);
  1540. nelems %= (MAXARG_C + 1);
  1541. luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
  1542. codeextraarg(fs, extra);
  1543. }
  1544. fs->freereg = base + 1; /* free registers with list values */
  1545. }
  1546. /*
  1547. ** return the final target of a jump (skipping jumps to jumps)
  1548. */
  1549. static int finaltarget (Instruction *code, int i) {
  1550. int count;
  1551. for (count = 0; count < 100; count++) { /* avoid infinite loops */
  1552. Instruction pc = code[i];
  1553. if (GET_OPCODE(pc) != OP_JMP)
  1554. break;
  1555. else
  1556. i += GETARG_sJ(pc) + 1;
  1557. }
  1558. return i;
  1559. }
  1560. /*
  1561. ** Do a final pass over the code of a function, doing small peephole
  1562. ** optimizations and adjustments.
  1563. */
  1564. void luaK_finish (FuncState *fs) {
  1565. int i;
  1566. Proto *p = fs->f;
  1567. for (i = 0; i < fs->pc; i++) {
  1568. Instruction *pc = &p->code[i];
  1569. lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
  1570. switch (GET_OPCODE(*pc)) {
  1571. case OP_RETURN0: case OP_RETURN1: {
  1572. if (!(fs->needclose || p->is_vararg))
  1573. break; /* no extra work */
  1574. /* else use OP_RETURN to do the extra work */
  1575. SET_OPCODE(*pc, OP_RETURN);
  1576. } /* FALLTHROUGH */
  1577. case OP_RETURN: case OP_TAILCALL: {
  1578. if (fs->needclose)
  1579. SETARG_k(*pc, 1); /* signal that it needs to close */
  1580. if (p->is_vararg)
  1581. SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
  1582. break;
  1583. }
  1584. case OP_JMP: {
  1585. int target = finaltarget(p->code, i);
  1586. fixjump(fs, i, target);
  1587. break;
  1588. }
  1589. default: break;
  1590. }
  1591. }
  1592. }