lcode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. ** $Id: lcode.c,v 1.25 2000/04/13 16:51:01 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. static int code_label (FuncState *fs, OpCode op, int arg) {
  252. int j = luaK_getlabel(fs);
  253. luaK_code1(fs, op, arg);
  254. return j;
  255. }
  256. static void jump_to_value (FuncState *fs, expdesc *v, Instruction previous) {
  257. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  258. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  259. int final; /* position after whole expression */
  260. if (ISJUMP(previous)) {
  261. luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in true list */
  262. p_nil = code_label(fs, OP_PUSHNILJMP, 0);
  263. p_1 = code_label(fs, OP_PUSHINT, 1);
  264. }
  265. else { /* still may need a PUSHNIL or a PUSHINT */
  266. int need_nil = need_value(fs, v->u.l.f, OP_JMPONF);
  267. int need_1 = need_value(fs, v->u.l.t, OP_JMPONT);
  268. if (need_nil && need_1) {
  269. int j = code_label(fs, OP_JMP, 0); /* skip both pushes */
  270. p_nil = code_label(fs, OP_PUSHNILJMP, 0);
  271. p_1 = code_label(fs, OP_PUSHINT, 1);
  272. luaK_patchlist(fs, j, luaK_getlabel(fs));
  273. luaK_deltastack(fs, -1); /* previous PUSHINT may be skipped */
  274. }
  275. else if (need_nil || need_1) {
  276. int j = code_label(fs, OP_JMP, 0); /* skip one push */
  277. if (need_nil)
  278. p_nil = code_label(fs, OP_PUSHNIL, 1);
  279. else /* need_1 */
  280. p_1 = code_label(fs, OP_PUSHINT, 1);
  281. luaK_patchlist(fs, j, luaK_getlabel(fs));
  282. luaK_deltastack(fs, -1); /* previous PUSHs may be skipped */
  283. }
  284. }
  285. final = luaK_getlabel(fs);
  286. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final);
  287. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final);
  288. v->u.l.f = v->u.l.t = NO_JUMP;
  289. }
  290. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  291. FuncState *fs = ls->fs;
  292. if (!discharge(fs, v)) { /* `v' is an expression? */
  293. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  294. LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
  295. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  296. /* it is an expression without jumps */
  297. if (onlyone)
  298. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  299. }
  300. else /* expression has jumps... */
  301. jump_to_value(fs, v, previous);
  302. }
  303. }
  304. void luaK_prefix (LexState *ls, int op, expdesc *v) {
  305. FuncState *fs = ls->fs;
  306. if (op == '-') {
  307. luaK_tostack(ls, v, 1);
  308. luaK_code0(fs, OP_MINUS);
  309. }
  310. else { /* op == NOT */
  311. Instruction *previous;
  312. discharge1(fs, v);
  313. previous = &fs->f->code[fs->pc-1];
  314. if (ISJUMP(GET_OPCODE(*previous)))
  315. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  316. else
  317. luaK_code0(fs, OP_NOT);
  318. /* interchange true and false lists */
  319. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  320. }
  321. }
  322. void luaK_infix (LexState *ls, int op, expdesc *v) {
  323. FuncState *fs = ls->fs;
  324. if (op == TK_AND)
  325. luaK_goiftrue(fs, v, 1);
  326. else if (op == TK_OR)
  327. luaK_goiffalse(fs, v, 1);
  328. else
  329. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  330. }
  331. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  332. FuncState *fs = ls->fs;
  333. if (op == TK_AND) {
  334. LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
  335. discharge1(fs, v2);
  336. v1->u.l.t = v2->u.l.t;
  337. luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
  338. }
  339. else if (op == TK_OR) {
  340. LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
  341. discharge1(fs, v2);
  342. v1->u.l.f = v2->u.l.f;
  343. luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
  344. }
  345. else {
  346. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  347. switch (op) {
  348. case '+': luaK_code0(fs, OP_ADD); break;
  349. case '-': luaK_code0(fs, OP_SUB); break;
  350. case '*': luaK_code0(fs, OP_MULT); break;
  351. case '/': luaK_code0(fs, OP_DIV); break;
  352. case '^': luaK_code0(fs, OP_POW); break;
  353. case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break;
  354. case TK_EQ: luaK_code0(fs, OP_JMPEQ); break;
  355. case TK_NE: luaK_code0(fs, OP_JMPNE); break;
  356. case '>': luaK_code0(fs, OP_JMPGT); break;
  357. case '<': luaK_code0(fs, OP_JMPLT); break;
  358. case TK_GE: luaK_code0(fs, OP_JMPGE); break;
  359. case TK_LE: luaK_code0(fs, OP_JMPLE); break;
  360. }
  361. }
  362. }
  363. int luaK_code0 (FuncState *fs, OpCode o) {
  364. return luaK_code2(fs, o, 0, 0);
  365. }
  366. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  367. return luaK_code2(fs, o, arg1, 0);
  368. }
  369. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  370. Instruction i = previous_instruction(fs);
  371. int delta = 0;
  372. enum {iO, iU, iS, iAB, iP} mode; /* instruction format (or iP to optimize) */
  373. mode = iP;
  374. switch (o) {
  375. case OP_CLOSURE:
  376. delta = -arg2+1;
  377. mode = iAB;
  378. break;
  379. case OP_SETLINE:
  380. mode = iU;
  381. break;
  382. case OP_CALL:
  383. mode = iAB;
  384. break;
  385. case OP_PUSHINT:
  386. delta = 1;
  387. mode = iS;
  388. break;
  389. case OP_SETTABLE:
  390. delta = -arg2;
  391. mode = iAB;
  392. break;
  393. case OP_SETLIST:
  394. delta = -(arg2+1);
  395. mode = iAB;
  396. break;
  397. case OP_SETMAP:
  398. delta = -2*(arg1+1);
  399. mode = iU;
  400. break;
  401. case OP_FORLOOP:
  402. delta = -3;
  403. arg1 = NO_JUMP;
  404. mode = iS;
  405. break;
  406. case OP_SETLOCAL:
  407. case OP_SETGLOBAL:
  408. delta = -1;
  409. mode = iU;
  410. break;
  411. case OP_FORPREP:
  412. case OP_JMP:
  413. arg1 = NO_JUMP;
  414. mode = iS;
  415. break;
  416. case OP_END:
  417. case OP_PUSHNILJMP:
  418. case OP_NOT:
  419. mode = iO;
  420. break;
  421. case OP_PUSHSTRING:
  422. case OP_PUSHNUM:
  423. case OP_PUSHNEGNUM:
  424. case OP_PUSHUPVALUE:
  425. case OP_GETLOCAL:
  426. case OP_GETGLOBAL:
  427. case OP_PUSHSELF:
  428. case OP_CREATETABLE:
  429. delta = 1;
  430. mode = iU;
  431. break;
  432. case OP_JMPLT:
  433. case OP_JMPLE:
  434. case OP_JMPGT:
  435. case OP_JMPGE:
  436. delta = -2;
  437. arg1 = NO_JUMP;
  438. mode = iS;
  439. break;
  440. case OP_MULT:
  441. case OP_DIV:
  442. case OP_POW:
  443. delta = -1;
  444. mode = iO;
  445. break;
  446. case OP_RETURN:
  447. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
  448. SET_OPCODE(i, OP_TAILCALL);
  449. SETARG_B(i, arg1);
  450. }
  451. else mode = iU;
  452. break;
  453. case OP_PUSHNIL:
  454. delta = arg1;
  455. switch(GET_OPCODE(i)) {
  456. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); break;
  457. default: mode = iU; break;
  458. }
  459. break;
  460. case OP_POP:
  461. delta = -arg1;
  462. switch(GET_OPCODE(i)) {
  463. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); break;
  464. default: mode = iU; break;
  465. }
  466. break;
  467. case OP_GETTABLE:
  468. delta = -1;
  469. switch(GET_OPCODE(i)) {
  470. case OP_PUSHSTRING: SET_OPCODE(i, OP_GETDOTTED); break; /* `t.x' */
  471. case OP_GETLOCAL: SET_OPCODE(i, OP_GETINDEXED); break; /* `t[i]' */
  472. default: mode = iO; break;
  473. }
  474. break;
  475. case OP_ADD:
  476. delta = -1;
  477. switch(GET_OPCODE(i)) {
  478. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); break; /* `a+k' */
  479. default: mode = iO; break;
  480. }
  481. break;
  482. case OP_SUB:
  483. delta = -1;
  484. switch(GET_OPCODE(i)) {
  485. case OP_PUSHINT: i = CREATE_S(OP_ADDI, -GETARG_S(i)); break; /* `a-k' */
  486. default: mode = iO; break;
  487. }
  488. break;
  489. case OP_CONCAT:
  490. delta = -arg1+1;
  491. switch(GET_OPCODE(i)) {
  492. case OP_CONCAT: SETARG_U(i, GETARG_U(i)+1); break; /* `a..b..c' */
  493. default: mode = iU; break;
  494. }
  495. break;
  496. case OP_MINUS:
  497. switch(GET_OPCODE(i)) {
  498. case OP_PUSHINT: SETARG_S(i, -GETARG_S(i)); break; /* `-k' */
  499. case OP_PUSHNUM: SET_OPCODE(i, OP_PUSHNEGNUM); break; /* `-k' */
  500. default: mode = iO; break;
  501. }
  502. break;
  503. case OP_JMPNE:
  504. delta = -2;
  505. if (i == CREATE_U(OP_PUSHNIL, 1)) /* `a~=nil' */
  506. i = CREATE_S(OP_JMPT, NO_JUMP);
  507. else {
  508. arg1 = NO_JUMP;
  509. mode = iS;
  510. }
  511. break;
  512. case OP_JMPEQ:
  513. delta = -2;
  514. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  515. i = CREATE_0(OP_NOT);
  516. delta = -1; /* just undo effect of previous PUSHNIL */
  517. }
  518. else {
  519. arg1 = NO_JUMP;
  520. mode = iS;
  521. }
  522. break;
  523. case OP_JMPT:
  524. case OP_JMPF:
  525. case OP_JMPONT:
  526. case OP_JMPONF:
  527. delta = -1;
  528. arg1 = NO_JUMP;
  529. switch (GET_OPCODE(i)) {
  530. case OP_NOT: i = CREATE_S(invertjump(o), NO_JUMP); break;
  531. default: mode = iS; break;
  532. }
  533. break;
  534. case OP_GETDOTTED:
  535. case OP_GETINDEXED:
  536. case OP_TAILCALL:
  537. case OP_ADDI:
  538. LUA_INTERNALERROR(L, "instruction used only for optimizations");
  539. return 0; /* to avoid warnings */
  540. }
  541. luaK_deltastack(fs, delta);
  542. switch (mode) { /* handle instruction formats */
  543. case iO: i = CREATE_0(o); break;
  544. case iU: i = CREATE_U(o, arg1); break;
  545. case iS: i = CREATE_S(o, arg1); break;
  546. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  547. case iP: { /* optimize: put instruction in place of last one */
  548. fs->f->code[fs->pc-1] = i; /* change previous instruction */
  549. return fs->pc-1;
  550. }
  551. }
  552. /* actually create the new instruction */
  553. luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
  554. fs->f->code[fs->pc] = i;
  555. return fs->pc++;
  556. }