lcode.c 17 KB

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