lcode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. ** $Id: lcode.c,v 1.23 2000/04/07 19:35:20 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include "stdlib.h"
  7. #define LUA_REENTRANT
  8. #include "lcode.h"
  9. #include "ldo.h"
  10. #include "llex.h"
  11. #include "lmem.h"
  12. #include "lobject.h"
  13. #include "lopcodes.h"
  14. #include "lparser.h"
  15. #include "lstring.h"
  16. void luaK_error (LexState *ls, const char *msg) {
  17. luaX_error(ls, msg, ls->token);
  18. }
  19. /*
  20. ** Returns the the previous instruction, for optimizations.
  21. ** If there is a jump target between this and the current instruction,
  22. ** returns a dummy instruction to avoid wrong optimizations.
  23. */
  24. static Instruction previous_instruction (FuncState *fs) {
  25. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  26. return fs->f->code[fs->pc-1]; /* returns previous instruction */
  27. else
  28. return CREATE_0(OP_END); /* no optimizations after an `END' */
  29. }
  30. int luaK_jump (FuncState *fs) {
  31. int j = luaK_code1(fs, OP_JMP, NO_JUMP);
  32. if (j == fs->lasttarget) { /* possible jumps to this jump? */
  33. luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
  34. fs->jlt = NO_JUMP;
  35. }
  36. return j;
  37. }
  38. static void luaK_fixjump (FuncState *fs, int pc, int dest) {
  39. Instruction *jmp = &fs->f->code[pc];
  40. if (dest == NO_JUMP)
  41. SETARG_S(*jmp, NO_JUMP); /* point to itself to represent end of list */
  42. else { /* jump is relative to position following jump instruction */
  43. int offset = dest-(pc+1);
  44. if (abs(offset) > MAXARG_S)
  45. luaK_error(fs->ls, "control structure too long");
  46. SETARG_S(*jmp, offset);
  47. }
  48. }
  49. static int luaK_getjump (FuncState *fs, int pc) {
  50. int offset = GETARG_S(fs->f->code[pc]);
  51. if (offset == NO_JUMP) /* point to itself represents end of list */
  52. return NO_JUMP; /* end of list */
  53. else
  54. return (pc+1)+offset; /* turn offset into absolute position */
  55. }
  56. /*
  57. ** discharge list of jumps to last target.
  58. ** returns current `pc' and marks it as a jump target (to avoid wrong
  59. ** optimizations with consecutive instructions not in the same basic block).
  60. */
  61. int luaK_getlabel (FuncState *fs) {
  62. if (fs->pc != fs->lasttarget) {
  63. int lasttarget = fs->lasttarget;
  64. fs->lasttarget = fs->pc;
  65. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  66. fs->jlt = NO_JUMP; /* nobody jumps to this new label (till now) */
  67. }
  68. return fs->pc;
  69. }
  70. void luaK_deltastack (FuncState *fs, int delta) {
  71. fs->stacklevel += delta;
  72. if (delta > 0 && fs->stacklevel > fs->f->maxstacksize) {
  73. if (fs->stacklevel > MAXSTACK)
  74. luaK_error(fs->ls, "function or expression too complex");
  75. fs->f->maxstacksize = fs->stacklevel;
  76. }
  77. }
  78. void luaK_kstr (LexState *ls, int c) {
  79. luaK_code1(ls->fs, OP_PUSHSTRING, c);
  80. }
  81. static int real_constant (FuncState *fs, Number r) {
  82. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  83. Proto *f = fs->f;
  84. int c = f->nknum;
  85. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  86. while (--c >= lim)
  87. if (f->knum[c] == r) return c;
  88. /* not found; create a new entry */
  89. luaM_growvector(fs->L, f->knum, f->nknum, 1, Number, constantEM, MAXARG_U);
  90. c = f->nknum++;
  91. f->knum[c] = r;
  92. return c;
  93. }
  94. void luaK_number (FuncState *fs, Number f) {
  95. if (f <= (Number)MAXARG_S && (int)f == f)
  96. luaK_code1(fs, OP_PUSHINT, (int)f); /* f has a short integer value */
  97. else
  98. luaK_code1(fs, OP_PUSHNUM, real_constant(fs, f));
  99. }
  100. void luaK_adjuststack (FuncState *fs, int n) {
  101. if (n > 0)
  102. luaK_code1(fs, OP_POP, n);
  103. else if (n < 0)
  104. luaK_code1(fs, OP_PUSHNIL, -n);
  105. }
  106. int luaK_lastisopen (FuncState *fs) {
  107. /* check whether last instruction is an open function call */
  108. Instruction i = previous_instruction(fs);
  109. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
  110. return 1;
  111. else return 0;
  112. }
  113. void luaK_setcallreturns (FuncState *fs, int nresults) {
  114. if (luaK_lastisopen(fs)) { /* expression is an open function call? */
  115. SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
  116. luaK_deltastack(fs, nresults); /* push results */
  117. }
  118. }
  119. static void assertglobal (FuncState *fs, int index) {
  120. luaS_assertglobal(fs->L, fs->f->kstr[index]);
  121. }
  122. static int discharge (FuncState *fs, expdesc *var) {
  123. switch (var->k) {
  124. case VLOCAL:
  125. luaK_code1(fs, OP_GETLOCAL, var->u.index);
  126. break;
  127. case VGLOBAL:
  128. luaK_code1(fs, OP_GETGLOBAL, var->u.index);
  129. assertglobal(fs, var->u.index); /* make sure that there is a global */
  130. break;
  131. case VINDEXED:
  132. luaK_code0(fs, OP_GETTABLE);
  133. break;
  134. case VEXP:
  135. return 0; /* nothing to do */
  136. }
  137. var->k = VEXP;
  138. var->u.l.t = var->u.l.f = NO_JUMP;
  139. return 1;
  140. }
  141. static void discharge1 (FuncState *fs, expdesc *var) {
  142. discharge(fs, var);
  143. /* if it has jumps it is already discharged */
  144. if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
  145. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  146. }
  147. void luaK_storevar (LexState *ls, const expdesc *var) {
  148. FuncState *fs = ls->fs;
  149. switch (var->k) {
  150. case VLOCAL:
  151. luaK_code1(fs, OP_SETLOCAL, var->u.index);
  152. break;
  153. case VGLOBAL:
  154. luaK_code1(fs, OP_SETGLOBAL, var->u.index);
  155. assertglobal(fs, var->u.index); /* make sure that there is a global */
  156. break;
  157. case VINDEXED: /* table is at top-3; pop 3 elements after operation */
  158. luaK_code2(fs, OP_SETTABLE, 3, 3);
  159. break;
  160. default:
  161. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  162. }
  163. }
  164. static OpCode invertjump (OpCode op) {
  165. switch (op) {
  166. case OP_JMPNE: return OP_JMPEQ;
  167. case OP_JMPEQ: return OP_JMPNE;
  168. case OP_JMPLT: return OP_JMPGE;
  169. case OP_JMPLE: return OP_JMPGT;
  170. case OP_JMPGT: return OP_JMPLE;
  171. case OP_JMPGE: return OP_JMPLT;
  172. case OP_JMPT: case OP_JMPONT: return OP_JMPF;
  173. case OP_JMPF: case OP_JMPONF: return OP_JMPT;
  174. default:
  175. LUA_INTERNALERROR(NULL, "invalid jump instruction");
  176. return OP_END; /* to avoid warnings */
  177. }
  178. }
  179. static void luaK_patchlistaux (FuncState *fs, int list, int target,
  180. OpCode special, int special_target) {
  181. Instruction *code = fs->f->code;
  182. while (list != NO_JUMP) {
  183. int next = luaK_getjump(fs, list);
  184. Instruction *i = &code[list];
  185. OpCode op = GET_OPCODE(*i);
  186. if (op == special) /* this `op' already has a value */
  187. luaK_fixjump(fs, list, special_target);
  188. else {
  189. luaK_fixjump(fs, list, target); /* do the patch */
  190. if (op == OP_JMPONT) /* remove eventual values */
  191. SET_OPCODE(*i, OP_JMPT);
  192. else if (op == OP_JMPONF)
  193. SET_OPCODE(*i, OP_JMPF);
  194. }
  195. list = next;
  196. }
  197. }
  198. void luaK_patchlist (FuncState *fs, int list, int target) {
  199. if (target == fs->lasttarget) /* same target that list `jlt'? */
  200. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  201. else
  202. luaK_patchlistaux(fs, list, target, OP_END, 0);
  203. }
  204. static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  205. /* check whether list has a jump without a value */
  206. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  207. if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  208. return 0; /* not found */
  209. }
  210. void luaK_concat (FuncState *fs, int *l1, int l2) {
  211. if (*l1 == NO_JUMP)
  212. *l1 = l2;
  213. else {
  214. int list = *l1;
  215. for (;;) { /* traverse `l1' */
  216. int next = luaK_getjump(fs, list);
  217. if (next == NO_JUMP) { /* end of list? */
  218. luaK_fixjump(fs, list, l2);
  219. return;
  220. }
  221. list = next;
  222. }
  223. }
  224. }
  225. static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  226. Instruction *previous;
  227. int *golist = &v->u.l.f;
  228. int *exitlist = &v->u.l.t;
  229. if (invert) { /* interchange `golist' and `exitlist' */
  230. int *temp = golist; golist = exitlist; exitlist = temp;
  231. }
  232. discharge1(fs, v);
  233. previous = &fs->f->code[fs->pc-1];
  234. LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
  235. if (ISJUMP(GET_OPCODE(*previous))) {
  236. if (invert)
  237. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  238. }
  239. else
  240. luaK_code0(fs, jump);
  241. luaK_concat(fs, exitlist, fs->pc-1); /* insert last jump in `exitlist' */
  242. luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  243. *golist = NO_JUMP;
  244. }
  245. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  246. luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF);
  247. }
  248. void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  249. luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT);
  250. }
  251. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  252. FuncState *fs = ls->fs;
  253. if (!discharge(fs, v)) { /* `v' is an expression? */
  254. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  255. LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
  256. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  257. /* it is an expression without jumps */
  258. if (onlyone)
  259. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  260. }
  261. else { /* expression has jumps... */
  262. int p_nil = 0; /* position of an eventual PUSHNIL */
  263. int p_1 = 0; /* position of an eventual PUSHINT */
  264. int final; /* position after whole expression */
  265. if (ISJUMP(previous)) {
  266. luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in true list */
  267. p_nil = luaK_code0(fs, OP_PUSHNILJMP);
  268. p_1 = luaK_code1(fs, OP_PUSHINT, 1);
  269. }
  270. else { /* still may need a PUSHNIL or a PUSHINT */
  271. int need_nil = need_value(fs, v->u.l.f, OP_JMPONF);
  272. int need_1 = need_value(fs, v->u.l.t, OP_JMPONT);
  273. if (need_nil && need_1) {
  274. luaK_code1(fs, OP_JMP, 2); /* skip both pushes */
  275. p_nil = luaK_code0(fs, OP_PUSHNILJMP);
  276. p_1 = luaK_code1(fs, OP_PUSHINT, 1);
  277. luaK_deltastack(fs, -1); /* previous PUSHINT may be skipped */
  278. }
  279. else if (need_nil || need_1) {
  280. luaK_code1(fs, OP_JMP, 1); /* skip one push */
  281. if (need_nil)
  282. p_nil = luaK_code1(fs, OP_PUSHNIL, 1);
  283. else /* need_1 */
  284. p_1 = luaK_code1(fs, OP_PUSHINT, 1);
  285. luaK_deltastack(fs, -1); /* previous PUSHs may be skipped */
  286. }
  287. }
  288. final = luaK_getlabel(fs);
  289. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final);
  290. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final);
  291. v->u.l.f = v->u.l.t = NO_JUMP;
  292. }
  293. }
  294. }
  295. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  296. FuncState *fs = ls->fs;
  297. if (op == '-') {
  298. luaK_tostack(ls, v, 1);
  299. luaK_code0(fs, OP_MINUS);
  300. }
  301. else { /* op == NOT */
  302. Instruction *previous;
  303. discharge1(fs, v);
  304. previous = &fs->f->code[fs->pc-1];
  305. if (ISJUMP(GET_OPCODE(*previous)))
  306. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  307. else
  308. luaK_code0(fs, OP_NOT);
  309. /* interchange true and false lists */
  310. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  311. }
  312. }
  313. void luaK_infix (LexState *ls, int op, expdesc *v) {
  314. FuncState *fs = ls->fs;
  315. if (op == TK_AND)
  316. luaK_goiftrue(fs, v, 1);
  317. else if (op == TK_OR)
  318. luaK_goiffalse(fs, v, 1);
  319. else
  320. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  321. }
  322. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  323. FuncState *fs = ls->fs;
  324. if (op == TK_AND) {
  325. LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
  326. discharge1(fs, v2);
  327. v1->u.l.t = v2->u.l.t;
  328. luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
  329. }
  330. else if (op == TK_OR) {
  331. LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
  332. discharge1(fs, v2);
  333. v1->u.l.f = v2->u.l.f;
  334. luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
  335. }
  336. else {
  337. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  338. switch (op) {
  339. case '+': luaK_code0(fs, OP_ADD); break;
  340. case '-': luaK_code0(fs, OP_SUB); break;
  341. case '*': luaK_code0(fs, OP_MULT); break;
  342. case '/': luaK_code0(fs, OP_DIV); break;
  343. case '^': luaK_code0(fs, OP_POW); break;
  344. case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break;
  345. case TK_EQ: luaK_code0(fs, OP_JMPEQ); break;
  346. case TK_NE: luaK_code0(fs, OP_JMPNE); break;
  347. case '>': luaK_code0(fs, OP_JMPGT); break;
  348. case '<': luaK_code0(fs, OP_JMPLT); break;
  349. case TK_GE: luaK_code0(fs, OP_JMPGE); break;
  350. case TK_LE: luaK_code0(fs, OP_JMPLE); break;
  351. }
  352. }
  353. }
  354. int luaK_code0 (FuncState *fs, OpCode o) {
  355. return luaK_code2(fs, o, 0, 0);
  356. }
  357. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  358. return luaK_code2(fs, o, arg1, 0);
  359. }
  360. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  361. Instruction i = previous_instruction(fs);
  362. int delta = 0;
  363. enum {iO, iU, iS, iAB, iP} mode; /* instruction format (or iP to optimize) */
  364. mode = iP;
  365. switch (o) {
  366. case OP_CLOSURE: delta = -arg2+1; mode = iAB; break;
  367. case OP_SETLINE: mode = iU; break;
  368. case OP_CALL: mode = iAB; break;
  369. case OP_PUSHINT: delta = 1; mode = iS; break;
  370. case OP_SETGLOBAL: delta = -1; mode = iU; break;
  371. case OP_SETTABLE: delta = -arg2; mode = iAB; break;
  372. case OP_SETLIST: delta = -(arg2+1); mode = iAB; break;
  373. case OP_SETMAP: delta = -2*(arg1+1); mode = iU; break;
  374. case OP_FORLOOP: delta = -3; arg1 = NO_JUMP; mode = iS; break;
  375. case OP_FORPREP: arg1 = NO_JUMP; /* go through */
  376. case OP_JMP: mode = iS; break;
  377. case OP_END: case OP_PUSHNILJMP: case OP_NOT:
  378. mode = iO; break;
  379. case OP_PUSHSTRING: case OP_PUSHNUM:
  380. case OP_PUSHNEGNUM: case OP_PUSHUPVALUE:
  381. case OP_GETGLOBAL: case OP_PUSHSELF: case OP_CREATETABLE:
  382. delta = 1; mode = iU; break;
  383. case OP_JMPLT: case OP_JMPLE: case OP_JMPGT: case OP_JMPGE:
  384. delta = -2; arg1 = NO_JUMP; mode = iS; break;
  385. case OP_MULT: case OP_DIV: case OP_POW:
  386. delta = -1; mode = iO; break;
  387. case OP_RETURN:
  388. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
  389. SET_OPCODE(i, OP_TAILCALL);
  390. SETARG_B(i, arg1);
  391. }
  392. else mode = iU;
  393. break;
  394. case OP_PUSHNIL:
  395. delta = arg1;
  396. switch(GET_OPCODE(i)) {
  397. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); break;
  398. default: mode = iU; break;
  399. }
  400. break;
  401. case OP_POP:
  402. delta = -arg1;
  403. switch(GET_OPCODE(i)) {
  404. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); break;
  405. case OP_SETLOCAL: SETARG_B(i, GETARG_B(i)+arg1); break;
  406. default: mode = iU; break;
  407. }
  408. break;
  409. case OP_GETLOCAL:
  410. delta = 1;
  411. if (i == CREATE_AB(OP_SETLOCAL, arg1, 1))
  412. SETARG_B(i, 0);
  413. else mode = iU;
  414. break;
  415. case OP_GETTABLE:
  416. delta = -1;
  417. switch(GET_OPCODE(i)) {
  418. case OP_PUSHSTRING: SET_OPCODE(i, OP_GETDOTTED); break; /* `t.x' */
  419. case OP_GETLOCAL: SET_OPCODE(i, OP_GETINDEXED); break; /* `t[i]' */
  420. default: mode = iO; break;
  421. }
  422. break;
  423. case OP_SETLOCAL: {
  424. int pc = fs->pc;
  425. Instruction *code = fs->f->code;
  426. delta = -1;
  427. if (pc-1 > fs->lasttarget && /* no jumps in-between instructions? */
  428. code[pc-2] == CREATE_U(OP_GETLOCAL, arg1) &&
  429. GET_OPCODE(i) == OP_ADDI && abs(GETARG_S(i)) <= MAXARG_sA) {
  430. /* `local=local+k' */
  431. fs->pc = pc-1;
  432. code[pc-2] = CREATE_sAB(OP_INCLOCAL, GETARG_S(i), arg1);
  433. luaK_deltastack(fs, delta);
  434. return pc-1;
  435. }
  436. else {
  437. arg2 = 1; /* `setlocal' default pops one value */
  438. mode = iAB;
  439. }
  440. break;
  441. }
  442. case OP_ADD:
  443. delta = -1;
  444. switch(GET_OPCODE(i)) {
  445. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); break; /* `a+k' */
  446. default: mode = iO; break;
  447. }
  448. break;
  449. case OP_SUB:
  450. delta = -1;
  451. switch(GET_OPCODE(i)) {
  452. case OP_PUSHINT: i = CREATE_S(OP_ADDI, -GETARG_S(i)); break; /* `a-k' */
  453. default: mode = iO; break;
  454. }
  455. break;
  456. case OP_CONCAT:
  457. delta = -arg1+1;
  458. switch(GET_OPCODE(i)) {
  459. case OP_CONCAT: SETARG_U(i, GETARG_U(i)+1); break; /* `a..b..c' */
  460. default: mode = iU; break;
  461. }
  462. break;
  463. case OP_MINUS:
  464. switch(GET_OPCODE(i)) {
  465. case OP_PUSHINT: SETARG_S(i, -GETARG_S(i)); break; /* `-k' */
  466. case OP_PUSHNUM: SET_OPCODE(i, OP_PUSHNEGNUM); break; /* `-k' */
  467. default: mode = iO; break;
  468. }
  469. break;
  470. case OP_JMPNE:
  471. delta = -2;
  472. if (i == CREATE_U(OP_PUSHNIL, 1)) /* `a~=nil' */
  473. i = CREATE_S(OP_JMPT, NO_JUMP);
  474. else {
  475. arg1 = NO_JUMP;
  476. mode = iS;
  477. }
  478. break;
  479. case OP_JMPEQ:
  480. delta = -2;
  481. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  482. i = CREATE_0(OP_NOT);
  483. delta = -1; /* just undo effect of previous PUSHNIL */
  484. }
  485. else {
  486. arg1 = NO_JUMP;
  487. mode = iS;
  488. }
  489. break;
  490. case OP_JMPT: case OP_JMPF: case OP_JMPONT: case OP_JMPONF:
  491. delta = -1;
  492. arg1 = NO_JUMP;
  493. switch (GET_OPCODE(i)) {
  494. case OP_NOT: i = CREATE_S(invertjump(o), NO_JUMP); break;
  495. default: mode = iS; break;
  496. }
  497. break;
  498. case OP_GETDOTTED: case OP_GETINDEXED:
  499. case OP_TAILCALL: case OP_INCLOCAL:
  500. case OP_ADDI:
  501. LUA_INTERNALERROR(L, "instruction used only for optimizations");
  502. return 0; /* to avoid warnings */
  503. }
  504. luaK_deltastack(fs, delta);
  505. switch (mode) { /* handle instruction formats */
  506. case iO: i = CREATE_0(o); break;
  507. case iU: i = CREATE_U(o, arg1); break;
  508. case iS: i = CREATE_S(o, arg1); break;
  509. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  510. case iP: { /* optimize: put instruction in place of last one */
  511. fs->f->code[fs->pc-1] = i; /* change previous instruction */
  512. return fs->pc-1;
  513. }
  514. }
  515. /* actually create the new instruction */
  516. luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
  517. fs->f->code[fs->pc] = i;
  518. return fs->pc++;
  519. }