lcode.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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 constant).
  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. ** Ensures expression value is in register 'reg' (and therefore
  689. ** 'e' will become a 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. ** Ensures expression value is in any register.
  742. ** (Expression still may have jump lists.)
  743. */
  744. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  745. if (e->k != VNONRELOC) { /* no fixed register yet? */
  746. luaK_reserveregs(fs, 1); /* get a register */
  747. discharge2reg(fs, e, fs->freereg-1); /* put value there */
  748. }
  749. }
  750. static int code_loadbool (FuncState *fs, int A, OpCode op) {
  751. luaK_getlabel(fs); /* those instructions may be jump targets */
  752. return luaK_codeABC(fs, op, A, 0, 0);
  753. }
  754. /*
  755. ** check whether list has any jump that do not produce a value
  756. ** or produce an inverted value
  757. */
  758. static int need_value (FuncState *fs, int list) {
  759. for (; list != NO_JUMP; list = getjump(fs, list)) {
  760. Instruction i = *getjumpcontrol(fs, list);
  761. if (GET_OPCODE(i) != OP_TESTSET) return 1;
  762. }
  763. return 0; /* not found */
  764. }
  765. /*
  766. ** Ensures final expression result (which includes results from its
  767. ** jump lists) is in register 'reg'.
  768. ** If expression has jumps, need to patch these jumps either to
  769. ** its final position or to "load" instructions (for those tests
  770. ** that do not produce values).
  771. */
  772. static void exp2reg (FuncState *fs, expdesc *e, int reg) {
  773. discharge2reg(fs, e, reg);
  774. if (e->k == VJMP) /* expression itself is a test? */
  775. luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
  776. if (hasjumps(e)) {
  777. int final; /* position after whole expression */
  778. int p_f = NO_JUMP; /* position of an eventual LOAD false */
  779. int p_t = NO_JUMP; /* position of an eventual LOAD true */
  780. if (need_value(fs, e->t) || need_value(fs, e->f)) {
  781. int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
  782. p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */
  783. p_t = code_loadbool(fs, reg, OP_LOADTRUE);
  784. /* jump around these booleans if 'e' is not a test */
  785. luaK_patchtohere(fs, fj);
  786. }
  787. final = luaK_getlabel(fs);
  788. patchlistaux(fs, e->f, final, reg, p_f);
  789. patchlistaux(fs, e->t, final, reg, p_t);
  790. }
  791. e->f = e->t = NO_JUMP;
  792. e->u.info = reg;
  793. e->k = VNONRELOC;
  794. }
  795. /*
  796. ** Ensures final expression result is in next available register.
  797. */
  798. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  799. luaK_dischargevars(fs, e);
  800. freeexp(fs, e);
  801. luaK_reserveregs(fs, 1);
  802. exp2reg(fs, e, fs->freereg - 1);
  803. }
  804. /*
  805. ** Ensures final expression result is in some (any) register
  806. ** and return that register.
  807. */
  808. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  809. luaK_dischargevars(fs, e);
  810. if (e->k == VNONRELOC) { /* expression already has a register? */
  811. if (!hasjumps(e)) /* no jumps? */
  812. return e->u.info; /* result is already in a register */
  813. if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
  814. exp2reg(fs, e, e->u.info); /* put final result in it */
  815. return e->u.info;
  816. }
  817. }
  818. luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
  819. return e->u.info;
  820. }
  821. /*
  822. ** Ensures final expression result is either in a register
  823. ** or in an upvalue.
  824. */
  825. void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
  826. if (e->k != VUPVAL || hasjumps(e))
  827. luaK_exp2anyreg(fs, e);
  828. }
  829. /*
  830. ** Ensures final expression result is either in a register
  831. ** or it is a constant.
  832. */
  833. void luaK_exp2val (FuncState *fs, expdesc *e) {
  834. if (hasjumps(e))
  835. luaK_exp2anyreg(fs, e);
  836. else
  837. luaK_dischargevars(fs, e);
  838. }
  839. /*
  840. ** Try to make 'e' a K expression with an index in the range of R/K
  841. ** indices. Return true iff succeeded.
  842. */
  843. static int luaK_exp2K (FuncState *fs, expdesc *e) {
  844. if (!hasjumps(e)) {
  845. int info;
  846. switch (e->k) { /* move constants to 'k' */
  847. case VTRUE: info = boolT(fs); break;
  848. case VFALSE: info = boolF(fs); break;
  849. case VNIL: info = nilK(fs); break;
  850. case VKINT: info = luaK_intK(fs, e->u.ival); break;
  851. case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
  852. case VKSTR: info = stringK(fs, e->u.strval); break;
  853. case VK: info = e->u.info; break;
  854. default: return 0; /* not a constant */
  855. }
  856. if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
  857. e->k = VK; /* make expression a 'K' expression */
  858. e->u.info = info;
  859. return 1;
  860. }
  861. }
  862. /* else, expression doesn't fit; leave it unchanged */
  863. return 0;
  864. }
  865. /*
  866. ** Ensures final expression result is in a valid R/K index
  867. ** (that is, it is either in a register or in 'k' with an index
  868. ** in the range of R/K indices).
  869. ** Returns 1 iff expression is K.
  870. */
  871. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  872. if (luaK_exp2K(fs, e))
  873. return 1;
  874. else { /* not a constant in the right range: put it in a register */
  875. luaK_exp2anyreg(fs, e);
  876. return 0;
  877. }
  878. }
  879. static void codeABRK (FuncState *fs, OpCode o, int a, int b,
  880. expdesc *ec) {
  881. int k = luaK_exp2RK(fs, ec);
  882. luaK_codeABCk(fs, o, a, b, ec->u.info, k);
  883. }
  884. /*
  885. ** Generate code to store result of expression 'ex' into variable 'var'.
  886. */
  887. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
  888. switch (var->k) {
  889. case VLOCAL: {
  890. freeexp(fs, ex);
  891. exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */
  892. return;
  893. }
  894. case VUPVAL: {
  895. int e = luaK_exp2anyreg(fs, ex);
  896. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
  897. break;
  898. }
  899. case VINDEXUP: {
  900. codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
  901. break;
  902. }
  903. case VINDEXI: {
  904. codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
  905. break;
  906. }
  907. case VINDEXSTR: {
  908. codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
  909. break;
  910. }
  911. case VINDEXED: {
  912. codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
  913. break;
  914. }
  915. default: lua_assert(0); /* invalid var kind to store */
  916. }
  917. freeexp(fs, ex);
  918. }
  919. /*
  920. ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
  921. */
  922. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  923. int ereg;
  924. luaK_exp2anyreg(fs, e);
  925. ereg = e->u.info; /* register where 'e' was placed */
  926. freeexp(fs, e);
  927. e->u.info = fs->freereg; /* base register for op_self */
  928. e->k = VNONRELOC; /* self expression has a fixed register */
  929. luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
  930. codeABRK(fs, OP_SELF, e->u.info, ereg, key);
  931. freeexp(fs, key);
  932. }
  933. /*
  934. ** Negate condition 'e' (where 'e' is a comparison).
  935. */
  936. static void negatecondition (FuncState *fs, expdesc *e) {
  937. Instruction *pc = getjumpcontrol(fs, e->u.info);
  938. lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
  939. GET_OPCODE(*pc) != OP_TEST);
  940. SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
  941. }
  942. /*
  943. ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
  944. ** is true, code will jump if 'e' is true.) Return jump position.
  945. ** Optimize when 'e' is 'not' something, inverting the condition
  946. ** and removing the 'not'.
  947. */
  948. static int jumponcond (FuncState *fs, expdesc *e, int cond) {
  949. if (e->k == VRELOC) {
  950. Instruction ie = getinstruction(fs, e);
  951. if (GET_OPCODE(ie) == OP_NOT) {
  952. removelastinstruction(fs); /* remove previous OP_NOT */
  953. return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
  954. }
  955. /* else go through */
  956. }
  957. discharge2anyreg(fs, e);
  958. freeexp(fs, e);
  959. return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
  960. }
  961. /*
  962. ** Emit code to go through if 'e' is true, jump otherwise.
  963. */
  964. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  965. int pc; /* pc of new jump */
  966. luaK_dischargevars(fs, e);
  967. switch (e->k) {
  968. case VJMP: { /* condition? */
  969. negatecondition(fs, e); /* jump when it is false */
  970. pc = e->u.info; /* save jump position */
  971. break;
  972. }
  973. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  974. pc = NO_JUMP; /* always true; do nothing */
  975. break;
  976. }
  977. default: {
  978. pc = jumponcond(fs, e, 0); /* jump when false */
  979. break;
  980. }
  981. }
  982. luaK_concat(fs, &e->f, pc); /* insert new jump in false list */
  983. luaK_patchtohere(fs, e->t); /* true list jumps to here (to go through) */
  984. e->t = NO_JUMP;
  985. }
  986. /*
  987. ** Emit code to go through if 'e' is false, jump otherwise.
  988. */
  989. void luaK_goiffalse (FuncState *fs, expdesc *e) {
  990. int pc; /* pc of new jump */
  991. luaK_dischargevars(fs, e);
  992. switch (e->k) {
  993. case VJMP: {
  994. pc = e->u.info; /* already jump if true */
  995. break;
  996. }
  997. case VNIL: case VFALSE: {
  998. pc = NO_JUMP; /* always false; do nothing */
  999. break;
  1000. }
  1001. default: {
  1002. pc = jumponcond(fs, e, 1); /* jump if true */
  1003. break;
  1004. }
  1005. }
  1006. luaK_concat(fs, &e->t, pc); /* insert new jump in 't' list */
  1007. luaK_patchtohere(fs, e->f); /* false list jumps to here (to go through) */
  1008. e->f = NO_JUMP;
  1009. }
  1010. /*
  1011. ** Code 'not e', doing constant folding.
  1012. */
  1013. static void codenot (FuncState *fs, expdesc *e) {
  1014. switch (e->k) {
  1015. case VNIL: case VFALSE: {
  1016. e->k = VTRUE; /* true == not nil == not false */
  1017. break;
  1018. }
  1019. case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
  1020. e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */
  1021. break;
  1022. }
  1023. case VJMP: {
  1024. negatecondition(fs, e);
  1025. break;
  1026. }
  1027. case VRELOC:
  1028. case VNONRELOC: {
  1029. discharge2anyreg(fs, e);
  1030. freeexp(fs, e);
  1031. e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
  1032. e->k = VRELOC;
  1033. break;
  1034. }
  1035. default: lua_assert(0); /* cannot happen */
  1036. }
  1037. /* interchange true and false lists */
  1038. { int temp = e->f; e->f = e->t; e->t = temp; }
  1039. removevalues(fs, e->f); /* values are useless when negated */
  1040. removevalues(fs, e->t);
  1041. }
  1042. /*
  1043. ** Check whether expression 'e' is a small literal string
  1044. */
  1045. static int isKstr (FuncState *fs, expdesc *e) {
  1046. return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
  1047. ttisshrstring(&fs->f->k[e->u.info]));
  1048. }
  1049. /*
  1050. ** Check whether expression 'e' is a literal integer.
  1051. */
  1052. int luaK_isKint (expdesc *e) {
  1053. return (e->k == VKINT && !hasjumps(e));
  1054. }
  1055. /*
  1056. ** Check whether expression 'e' is a literal integer in
  1057. ** proper range to fit in register C
  1058. */
  1059. static int isCint (expdesc *e) {
  1060. return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
  1061. }
  1062. /*
  1063. ** Check whether expression 'e' is a literal integer in
  1064. ** proper range to fit in register sC
  1065. */
  1066. static int isSCint (expdesc *e) {
  1067. return luaK_isKint(e) && fitsC(e->u.ival);
  1068. }
  1069. /*
  1070. ** Check whether expression 'e' is a literal integer or float in
  1071. ** proper range to fit in a register (sB or sC).
  1072. */
  1073. static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
  1074. lua_Integer i;
  1075. if (e->k == VKINT)
  1076. i = e->u.ival;
  1077. else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
  1078. *isfloat = 1;
  1079. else
  1080. return 0; /* not a number */
  1081. if (!hasjumps(e) && fitsC(i)) {
  1082. *pi = int2sC(cast_int(i));
  1083. return 1;
  1084. }
  1085. else
  1086. return 0;
  1087. }
  1088. /*
  1089. ** Create expression 't[k]'. 't' must have its final result already in a
  1090. ** register or upvalue. Upvalues can only be indexed by literal strings.
  1091. ** Keys can be literal strings in the constant table or arbitrary
  1092. ** values in registers.
  1093. */
  1094. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  1095. if (k->k == VKSTR)
  1096. str2K(fs, k);
  1097. lua_assert(!hasjumps(t) &&
  1098. (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
  1099. if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
  1100. luaK_exp2anyreg(fs, t); /* put it in a register */
  1101. if (t->k == VUPVAL) {
  1102. t->u.ind.t = t->u.info; /* upvalue index */
  1103. t->u.ind.idx = k->u.info; /* literal string */
  1104. t->k = VINDEXUP;
  1105. }
  1106. else {
  1107. /* register index of the table */
  1108. t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info;
  1109. if (isKstr(fs, k)) {
  1110. t->u.ind.idx = k->u.info; /* literal string */
  1111. t->k = VINDEXSTR;
  1112. }
  1113. else if (isCint(k)) {
  1114. t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
  1115. t->k = VINDEXI;
  1116. }
  1117. else {
  1118. t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
  1119. t->k = VINDEXED;
  1120. }
  1121. }
  1122. }
  1123. /*
  1124. ** Return false if folding can raise an error.
  1125. ** Bitwise operations need operands convertible to integers; division
  1126. ** operations cannot have 0 as divisor.
  1127. */
  1128. static int validop (int op, TValue *v1, TValue *v2) {
  1129. switch (op) {
  1130. case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
  1131. case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
  1132. lua_Integer i;
  1133. return (tointegerns(v1, &i) && tointegerns(v2, &i));
  1134. }
  1135. case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
  1136. return (nvalue(v2) != 0);
  1137. default: return 1; /* everything else is valid */
  1138. }
  1139. }
  1140. /*
  1141. ** Try to "constant-fold" an operation; return 1 iff successful.
  1142. ** (In this case, 'e1' has the final result.)
  1143. */
  1144. static int constfolding (FuncState *fs, int op, expdesc *e1,
  1145. const expdesc *e2) {
  1146. TValue v1, v2, res;
  1147. if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
  1148. return 0; /* non-numeric operands or not safe to fold */
  1149. luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
  1150. if (ttisinteger(&res)) {
  1151. e1->k = VKINT;
  1152. e1->u.ival = ivalue(&res);
  1153. }
  1154. else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
  1155. lua_Number n = fltvalue(&res);
  1156. if (luai_numisnan(n) || n == 0)
  1157. return 0;
  1158. e1->k = VKFLT;
  1159. e1->u.nval = n;
  1160. }
  1161. return 1;
  1162. }
  1163. /*
  1164. ** Emit code for unary expressions that "produce values"
  1165. ** (everything but 'not').
  1166. ** Expression to produce final result will be encoded in 'e'.
  1167. */
  1168. static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
  1169. int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */
  1170. freeexp(fs, e);
  1171. e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */
  1172. e->k = VRELOC; /* all those operations are relocatable */
  1173. luaK_fixline(fs, line);
  1174. }
  1175. /*
  1176. ** Emit code for binary expressions that "produce values"
  1177. ** (everything but logical operators 'and'/'or' and comparison
  1178. ** operators).
  1179. ** Expression to produce final result will be encoded in 'e1'.
  1180. */
  1181. static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
  1182. OpCode op, int v2, int flip, int line,
  1183. OpCode mmop, TMS event) {
  1184. int v1 = luaK_exp2anyreg(fs, e1);
  1185. int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
  1186. freeexps(fs, e1, e2);
  1187. e1->u.info = pc;
  1188. e1->k = VRELOC; /* all those operations are relocatable */
  1189. luaK_fixline(fs, line);
  1190. luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */
  1191. luaK_fixline(fs, line);
  1192. }
  1193. /*
  1194. ** Emit code for binary expressions that "produce values" over
  1195. ** two registers.
  1196. */
  1197. static void codebinexpval (FuncState *fs, OpCode op,
  1198. expdesc *e1, expdesc *e2, int line) {
  1199. int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
  1200. lua_assert(OP_ADD <= op && op <= OP_SHR);
  1201. finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
  1202. cast(TMS, (op - OP_ADD) + TM_ADD));
  1203. }
  1204. /*
  1205. ** Code binary operators with immediate operands.
  1206. */
  1207. static void codebini (FuncState *fs, OpCode op,
  1208. expdesc *e1, expdesc *e2, int flip, int line,
  1209. TMS event) {
  1210. int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */
  1211. lua_assert(e2->k == VKINT);
  1212. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
  1213. }
  1214. /* Try to code a binary operator negating its second operand.
  1215. ** For the metamethod, 2nd operand must keep its original value.
  1216. */
  1217. static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
  1218. OpCode op, int line, TMS event) {
  1219. if (!luaK_isKint(e2))
  1220. return 0; /* not an integer constant */
  1221. else {
  1222. lua_Integer i2 = e2->u.ival;
  1223. if (!(fitsC(i2) && fitsC(-i2)))
  1224. return 0; /* not in the proper range */
  1225. else { /* operating a small integer constant */
  1226. int v2 = cast_int(i2);
  1227. finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
  1228. /* correct metamethod argument */
  1229. SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
  1230. return 1; /* successfully coded */
  1231. }
  1232. }
  1233. }
  1234. static void swapexps (expdesc *e1, expdesc *e2) {
  1235. expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */
  1236. }
  1237. /*
  1238. ** Code arithmetic operators ('+', '-', ...). If second operand is a
  1239. ** constant in the proper range, use variant opcodes with K operands.
  1240. */
  1241. static void codearith (FuncState *fs, BinOpr opr,
  1242. expdesc *e1, expdesc *e2, int flip, int line) {
  1243. TMS event = cast(TMS, opr + TM_ADD);
  1244. if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
  1245. int v2 = e2->u.info; /* K index */
  1246. OpCode op = cast(OpCode, opr + OP_ADDK);
  1247. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
  1248. }
  1249. else { /* 'e2' is neither an immediate nor a K operand */
  1250. OpCode op = cast(OpCode, opr + OP_ADD);
  1251. if (flip)
  1252. swapexps(e1, e2); /* back to original order */
  1253. codebinexpval(fs, op, e1, e2, line); /* use standard operators */
  1254. }
  1255. }
  1256. /*
  1257. ** Code commutative operators ('+', '*'). If first operand is a
  1258. ** numeric constant, change order of operands to try to use an
  1259. ** immediate or K operator.
  1260. */
  1261. static void codecommutative (FuncState *fs, BinOpr op,
  1262. expdesc *e1, expdesc *e2, int line) {
  1263. int flip = 0;
  1264. if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
  1265. swapexps(e1, e2); /* change order */
  1266. flip = 1;
  1267. }
  1268. if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
  1269. codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
  1270. else
  1271. codearith(fs, op, e1, e2, flip, line);
  1272. }
  1273. /*
  1274. ** Code bitwise operations; they are all associative, so the function
  1275. ** tries to put an integer constant as the 2nd operand (a K operand).
  1276. */
  1277. static void codebitwise (FuncState *fs, BinOpr opr,
  1278. expdesc *e1, expdesc *e2, int line) {
  1279. int flip = 0;
  1280. int v2;
  1281. OpCode op;
  1282. if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
  1283. swapexps(e1, e2); /* 'e2' will be the constant operand */
  1284. flip = 1;
  1285. }
  1286. else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
  1287. op = cast(OpCode, opr + OP_ADD);
  1288. codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
  1289. return;
  1290. }
  1291. v2 = e2->u.info; /* index in K array */
  1292. op = cast(OpCode, opr + OP_ADDK);
  1293. lua_assert(ttisinteger(&fs->f->k[v2]));
  1294. finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
  1295. cast(TMS, opr + TM_ADD));
  1296. }
  1297. /*
  1298. ** Emit code for order comparisons. When using an immediate operand,
  1299. ** 'isfloat' tells whether the original value was a float.
  1300. */
  1301. static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
  1302. int r1, r2;
  1303. int im;
  1304. int isfloat = 0;
  1305. if (isSCnumber(e2, &im, &isfloat)) {
  1306. /* use immediate operand */
  1307. r1 = luaK_exp2anyreg(fs, e1);
  1308. r2 = im;
  1309. op = cast(OpCode, (op - OP_LT) + OP_LTI);
  1310. }
  1311. else if (isSCnumber(e1, &im, &isfloat)) {
  1312. /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
  1313. r1 = luaK_exp2anyreg(fs, e2);
  1314. r2 = im;
  1315. op = (op == OP_LT) ? OP_GTI : OP_GEI;
  1316. }
  1317. else { /* regular case, compare two registers */
  1318. r1 = luaK_exp2anyreg(fs, e1);
  1319. r2 = luaK_exp2anyreg(fs, e2);
  1320. }
  1321. freeexps(fs, e1, e2);
  1322. e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
  1323. e1->k = VJMP;
  1324. }
  1325. /*
  1326. ** Emit code for equality comparisons ('==', '~=').
  1327. ** 'e1' was already put as RK by 'luaK_infix'.
  1328. */
  1329. static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
  1330. int r1, r2;
  1331. int im;
  1332. int isfloat = 0; /* not needed here, but kept for symmetry */
  1333. OpCode op;
  1334. if (e1->k != VNONRELOC) {
  1335. lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
  1336. swapexps(e1, e2);
  1337. }
  1338. r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
  1339. if (isSCnumber(e2, &im, &isfloat)) {
  1340. op = OP_EQI;
  1341. r2 = im; /* immediate operand */
  1342. }
  1343. else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */
  1344. op = OP_EQK;
  1345. r2 = e2->u.info; /* constant index */
  1346. }
  1347. else {
  1348. op = OP_EQ; /* will compare two registers */
  1349. r2 = luaK_exp2anyreg(fs, e2);
  1350. }
  1351. freeexps(fs, e1, e2);
  1352. e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
  1353. e1->k = VJMP;
  1354. }
  1355. /*
  1356. ** Apply prefix operation 'op' to expression 'e'.
  1357. */
  1358. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
  1359. static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
  1360. luaK_dischargevars(fs, e);
  1361. switch (op) {
  1362. case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
  1363. if (constfolding(fs, op + LUA_OPUNM, e, &ef))
  1364. break;
  1365. /* else */ /* FALLTHROUGH */
  1366. case OPR_LEN:
  1367. codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
  1368. break;
  1369. case OPR_NOT: codenot(fs, e); break;
  1370. default: lua_assert(0);
  1371. }
  1372. }
  1373. /*
  1374. ** Process 1st operand 'v' of binary operation 'op' before reading
  1375. ** 2nd operand.
  1376. */
  1377. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  1378. luaK_dischargevars(fs, v);
  1379. switch (op) {
  1380. case OPR_AND: {
  1381. luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */
  1382. break;
  1383. }
  1384. case OPR_OR: {
  1385. luaK_goiffalse(fs, v); /* go ahead only if 'v' is false */
  1386. break;
  1387. }
  1388. case OPR_CONCAT: {
  1389. luaK_exp2nextreg(fs, v); /* operand must be on the stack */
  1390. break;
  1391. }
  1392. case OPR_ADD: case OPR_SUB:
  1393. case OPR_MUL: case OPR_DIV: case OPR_IDIV:
  1394. case OPR_MOD: case OPR_POW:
  1395. case OPR_BAND: case OPR_BOR: case OPR_BXOR:
  1396. case OPR_SHL: case OPR_SHR: {
  1397. if (!tonumeral(v, NULL))
  1398. luaK_exp2anyreg(fs, v);
  1399. /* else keep numeral, which may be folded with 2nd operand */
  1400. break;
  1401. }
  1402. case OPR_EQ: case OPR_NE: {
  1403. if (!tonumeral(v, NULL))
  1404. luaK_exp2RK(fs, v);
  1405. /* else keep numeral, which may be an immediate operand */
  1406. break;
  1407. }
  1408. case OPR_LT: case OPR_LE:
  1409. case OPR_GT: case OPR_GE: {
  1410. int dummy, dummy2;
  1411. if (!isSCnumber(v, &dummy, &dummy2))
  1412. luaK_exp2anyreg(fs, v);
  1413. /* else keep numeral, which may be an immediate operand */
  1414. break;
  1415. }
  1416. default: lua_assert(0);
  1417. }
  1418. }
  1419. /*
  1420. ** Create code for '(e1 .. e2)'.
  1421. ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
  1422. ** because concatenation is right associative), merge both CONCATs.
  1423. */
  1424. static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
  1425. Instruction *ie2 = previousinstruction(fs);
  1426. if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
  1427. int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
  1428. lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
  1429. freeexp(fs, e2);
  1430. SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
  1431. SETARG_B(*ie2, n + 1); /* will concatenate one more element */
  1432. }
  1433. else { /* 'e2' is not a concatenation */
  1434. luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
  1435. freeexp(fs, e2);
  1436. luaK_fixline(fs, line);
  1437. }
  1438. }
  1439. /*
  1440. ** Finalize code for binary operation, after reading 2nd operand.
  1441. */
  1442. void luaK_posfix (FuncState *fs, BinOpr opr,
  1443. expdesc *e1, expdesc *e2, int line) {
  1444. luaK_dischargevars(fs, e2);
  1445. if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
  1446. return; /* done by folding */
  1447. switch (opr) {
  1448. case OPR_AND: {
  1449. lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */
  1450. luaK_concat(fs, &e2->f, e1->f);
  1451. *e1 = *e2;
  1452. break;
  1453. }
  1454. case OPR_OR: {
  1455. lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */
  1456. luaK_concat(fs, &e2->t, e1->t);
  1457. *e1 = *e2;
  1458. break;
  1459. }
  1460. case OPR_CONCAT: { /* e1 .. e2 */
  1461. luaK_exp2nextreg(fs, e2);
  1462. codeconcat(fs, e1, e2, line);
  1463. break;
  1464. }
  1465. case OPR_ADD: case OPR_MUL: {
  1466. codecommutative(fs, opr, e1, e2, line);
  1467. break;
  1468. }
  1469. case OPR_SUB: {
  1470. if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
  1471. break; /* coded as (r1 + -I) */
  1472. /* ELSE */
  1473. } /* FALLTHROUGH */
  1474. case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
  1475. codearith(fs, opr, e1, e2, 0, line);
  1476. break;
  1477. }
  1478. case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
  1479. codebitwise(fs, opr, e1, e2, line);
  1480. break;
  1481. }
  1482. case OPR_SHL: {
  1483. if (isSCint(e1)) {
  1484. swapexps(e1, e2);
  1485. codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */
  1486. }
  1487. else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
  1488. /* coded as (r1 >> -I) */;
  1489. }
  1490. else /* regular case (two registers) */
  1491. codebinexpval(fs, OP_SHL, e1, e2, line);
  1492. break;
  1493. }
  1494. case OPR_SHR: {
  1495. if (isSCint(e2))
  1496. codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
  1497. else /* regular case (two registers) */
  1498. codebinexpval(fs, OP_SHR, e1, e2, line);
  1499. break;
  1500. }
  1501. case OPR_EQ: case OPR_NE: {
  1502. codeeq(fs, opr, e1, e2);
  1503. break;
  1504. }
  1505. case OPR_LT: case OPR_LE: {
  1506. OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
  1507. codeorder(fs, op, e1, e2);
  1508. break;
  1509. }
  1510. case OPR_GT: case OPR_GE: {
  1511. /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
  1512. OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
  1513. swapexps(e1, e2);
  1514. codeorder(fs, op, e1, e2);
  1515. break;
  1516. }
  1517. default: lua_assert(0);
  1518. }
  1519. }
  1520. /*
  1521. ** Change line information associated with current position, by removing
  1522. ** previous info and adding it again with new line.
  1523. */
  1524. void luaK_fixline (FuncState *fs, int line) {
  1525. removelastlineinfo(fs);
  1526. savelineinfo(fs, fs->f, line);
  1527. }
  1528. void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
  1529. Instruction *inst = &fs->f->code[pc];
  1530. int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
  1531. int extra = asize / (MAXARG_C + 1); /* higher bits of array size */
  1532. int rc = asize % (MAXARG_C + 1); /* lower bits of array size */
  1533. int k = (extra > 0); /* true iff needs extra argument */
  1534. *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
  1535. *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
  1536. }
  1537. /*
  1538. ** Emit a SETLIST instruction.
  1539. ** 'base' is register that keeps table;
  1540. ** 'nelems' is #table plus those to be stored now;
  1541. ** 'tostore' is number of values (in registers 'base + 1',...) to add to
  1542. ** table (or LUA_MULTRET to add up to stack top).
  1543. */
  1544. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
  1545. lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
  1546. if (tostore == LUA_MULTRET)
  1547. tostore = 0;
  1548. if (nelems <= MAXARG_C)
  1549. luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
  1550. else {
  1551. int extra = nelems / (MAXARG_C + 1);
  1552. nelems %= (MAXARG_C + 1);
  1553. luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
  1554. codeextraarg(fs, extra);
  1555. }
  1556. fs->freereg = base + 1; /* free registers with list values */
  1557. }
  1558. /*
  1559. ** return the final target of a jump (skipping jumps to jumps)
  1560. */
  1561. static int finaltarget (Instruction *code, int i) {
  1562. int count;
  1563. for (count = 0; count < 100; count++) { /* avoid infinite loops */
  1564. Instruction pc = code[i];
  1565. if (GET_OPCODE(pc) != OP_JMP)
  1566. break;
  1567. else
  1568. i += GETARG_sJ(pc) + 1;
  1569. }
  1570. return i;
  1571. }
  1572. /*
  1573. ** Do a final pass over the code of a function, doing small peephole
  1574. ** optimizations and adjustments.
  1575. */
  1576. void luaK_finish (FuncState *fs) {
  1577. int i;
  1578. Proto *p = fs->f;
  1579. for (i = 0; i < fs->pc; i++) {
  1580. Instruction *pc = &p->code[i];
  1581. lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
  1582. switch (GET_OPCODE(*pc)) {
  1583. case OP_RETURN0: case OP_RETURN1: {
  1584. if (!(fs->needclose || p->is_vararg))
  1585. break; /* no extra work */
  1586. /* else use OP_RETURN to do the extra work */
  1587. SET_OPCODE(*pc, OP_RETURN);
  1588. } /* FALLTHROUGH */
  1589. case OP_RETURN: case OP_TAILCALL: {
  1590. if (fs->needclose)
  1591. SETARG_k(*pc, 1); /* signal that it needs to close */
  1592. if (p->is_vararg)
  1593. SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
  1594. break;
  1595. }
  1596. case OP_JMP: {
  1597. int target = finaltarget(p->code, i);
  1598. fixjump(fs, i, target);
  1599. break;
  1600. }
  1601. default: break;
  1602. }
  1603. }
  1604. }