lcode.c 50 KB

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