lcode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. ** $Id: lcode.c,v 1.67 2001/04/06 18:25:00 roberto Exp roberto $
  3. ** Code generator for Lua
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define LUA_PRIVATE
  8. #include "lua.h"
  9. #include "lcode.h"
  10. #include "ldo.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lopcodes.h"
  15. #include "lparser.h"
  16. void luaK_error (LexState *ls, const l_char *msg) {
  17. luaX_error(ls, msg, ls->t.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(-1); /* no optimizations after an invalid instruction */
  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, l_s("control structure too long"));
  46. SETARG_S(*jmp, offset);
  47. }
  48. }
  49. /*
  50. ** prep-for instructions (OP_FORPREP & OP_LFORPREP) have a negated jump,
  51. ** as they simulate the real jump...
  52. */
  53. void luaK_fixfor (FuncState *fs, int pc, int dest) {
  54. Instruction *jmp = &fs->f->code[pc];
  55. int offset = dest-(pc+1);
  56. SETARG_S(*jmp, -offset);
  57. }
  58. static int luaK_getjump (FuncState *fs, int pc) {
  59. int offset = GETARG_S(fs->f->code[pc]);
  60. if (offset == NO_JUMP) /* point to itself represents end of list */
  61. return NO_JUMP; /* end of list */
  62. else
  63. return (pc+1)+offset; /* turn offset into absolute position */
  64. }
  65. /*
  66. ** returns current `pc' and marks it as a jump target (to avoid wrong
  67. ** optimizations with consecutive instructions not in the same basic block).
  68. ** discharge list of jumps to last target.
  69. */
  70. int luaK_getlabel (FuncState *fs) {
  71. if (fs->pc != fs->lasttarget) {
  72. int lasttarget = fs->lasttarget;
  73. fs->lasttarget = fs->pc;
  74. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  75. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  76. }
  77. return fs->pc;
  78. }
  79. void luaK_deltastack (FuncState *fs, int delta) {
  80. fs->stacklevel += delta;
  81. if (fs->stacklevel > fs->f->maxstacksize) {
  82. if (fs->stacklevel > MAXSTACK)
  83. luaK_error(fs->ls, l_s("function or expression too complex"));
  84. fs->f->maxstacksize = (short)fs->stacklevel;
  85. }
  86. }
  87. void luaK_kstr (LexState *ls, int c) {
  88. luaK_code1(ls->fs, OP_PUSHSTRING, c);
  89. }
  90. static int number_constant (FuncState *fs, lua_Number r) {
  91. /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
  92. Proto *f = fs->f;
  93. int c = fs->nknum;
  94. int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
  95. while (--c >= lim)
  96. if (f->knum[c] == r) return c;
  97. /* not found; create a new entry */
  98. luaM_growvector(fs->L, f->knum, fs->nknum, f->sizeknum, lua_Number,
  99. MAXARG_U, l_s("constant table overflow"));
  100. c = fs->nknum++;
  101. f->knum[c] = r;
  102. return c;
  103. }
  104. void luaK_number (FuncState *fs, lua_Number f) {
  105. if (f <= (lua_Number)MAXARG_S && (lua_Number)(int)f == f)
  106. luaK_code1(fs, OP_PUSHINT, (int)f); /* f has a short integer value */
  107. else
  108. luaK_code1(fs, OP_PUSHNUM, number_constant(fs, f));
  109. }
  110. void luaK_adjuststack (FuncState *fs, int n) {
  111. if (n > 0)
  112. luaK_code1(fs, OP_POP, n);
  113. else
  114. luaK_code1(fs, OP_PUSHNIL, -n);
  115. }
  116. int luaK_lastisopen (FuncState *fs) {
  117. /* check whether last instruction is an open function call */
  118. Instruction i = previous_instruction(fs);
  119. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
  120. return 1;
  121. else return 0;
  122. }
  123. void luaK_setcallreturns (FuncState *fs, int nresults) {
  124. if (luaK_lastisopen(fs)) { /* expression is an open function call? */
  125. SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
  126. luaK_deltastack(fs, nresults); /* push results */
  127. }
  128. }
  129. static int discharge (FuncState *fs, expdesc *var) {
  130. switch (var->k) {
  131. case VLOCAL:
  132. luaK_code1(fs, OP_GETLOCAL, var->u.index);
  133. break;
  134. case VGLOBAL:
  135. luaK_code1(fs, OP_GETGLOBAL, var->u.index);
  136. break;
  137. case VINDEXED:
  138. luaK_code0(fs, OP_GETTABLE);
  139. break;
  140. case VEXP:
  141. return 0; /* nothing to do */
  142. }
  143. var->k = VEXP;
  144. var->u.l.t = var->u.l.f = NO_JUMP;
  145. return 1;
  146. }
  147. static void discharge1 (FuncState *fs, expdesc *var) {
  148. discharge(fs, var);
  149. /* if it has jumps then it is already discharged */
  150. if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
  151. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  152. }
  153. void luaK_storevar (LexState *ls, const expdesc *var) {
  154. FuncState *fs = ls->fs;
  155. switch (var->k) {
  156. case VLOCAL:
  157. luaK_code1(fs, OP_SETLOCAL, var->u.index);
  158. break;
  159. case VGLOBAL:
  160. luaK_code1(fs, OP_SETGLOBAL, var->u.index);
  161. break;
  162. case VINDEXED: /* table is at top-3; pop 3 elements after operation */
  163. luaK_code2(fs, OP_SETTABLE, 3, 3);
  164. break;
  165. default:
  166. lua_assert(0); /* invalid var kind to store */
  167. }
  168. }
  169. static OpCode invertjump (OpCode op) {
  170. switch (op) {
  171. case OP_JMPNE: return OP_JMPEQ;
  172. case OP_JMPEQ: return OP_JMPNE;
  173. case OP_JMPLT: return OP_JMPGE;
  174. case OP_JMPLE: return OP_JMPGT;
  175. case OP_JMPGT: return OP_JMPLE;
  176. case OP_JMPGE: return OP_JMPLT;
  177. case OP_JMPT: case OP_JMPONT: return OP_JMPF;
  178. case OP_JMPF: case OP_JMPONF: return OP_JMPT;
  179. default:
  180. lua_assert(0); /* invalid jump instruction */
  181. return OP_JMP; /* to avoid warnings */
  182. }
  183. }
  184. static void luaK_patchlistaux (FuncState *fs, int list, int target,
  185. OpCode special, int special_target) {
  186. Instruction *code = fs->f->code;
  187. while (list != NO_JUMP) {
  188. int next = luaK_getjump(fs, list);
  189. Instruction *i = &code[list];
  190. OpCode op = GET_OPCODE(*i);
  191. if (op == special) /* this `op' already has a value */
  192. luaK_fixjump(fs, list, special_target);
  193. else {
  194. luaK_fixjump(fs, list, target); /* do the patch */
  195. if (op == OP_JMPONT) /* remove eventual values */
  196. SET_OPCODE(*i, OP_JMPT);
  197. else if (op == OP_JMPONF)
  198. SET_OPCODE(*i, OP_JMPF);
  199. }
  200. list = next;
  201. }
  202. }
  203. void luaK_patchlist (FuncState *fs, int list, int target) {
  204. if (target == fs->lasttarget) /* same target that list `jlt'? */
  205. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  206. else
  207. luaK_patchlistaux(fs, list, target, OP_ADD, 0);
  208. }
  209. static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  210. /* check whether list has a jump without a value */
  211. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  212. if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  213. return 0; /* not found */
  214. }
  215. void luaK_concat (FuncState *fs, int *l1, int l2) {
  216. if (*l1 == NO_JUMP)
  217. *l1 = l2;
  218. else {
  219. int list = *l1;
  220. int next;
  221. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  222. list = next;
  223. luaK_fixjump(fs, list, l2);
  224. }
  225. }
  226. static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  227. int prevpos; /* position of last instruction */
  228. Instruction *previous;
  229. int *golist, *exitlist;
  230. if (!invert) {
  231. golist = &v->u.l.f; /* go if false */
  232. exitlist = &v->u.l.t; /* exit if true */
  233. }
  234. else {
  235. golist = &v->u.l.t; /* go if true */
  236. exitlist = &v->u.l.f; /* exit if false */
  237. }
  238. discharge1(fs, v);
  239. prevpos = fs->pc-1;
  240. previous = &fs->f->code[prevpos];
  241. lua_assert(*previous==previous_instruction(fs)); /* no jump allowed here */
  242. if (!ISJUMP(GET_OPCODE(*previous)))
  243. prevpos = luaK_code1(fs, jump, NO_JUMP);
  244. else { /* last instruction is already a jump */
  245. if (invert)
  246. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  247. }
  248. luaK_concat(fs, exitlist, prevpos); /* insert last jump in `exitlist' */
  249. luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  250. *golist = NO_JUMP;
  251. }
  252. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  253. luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF);
  254. }
  255. static void luaK_goiffalse (FuncState *fs, expdesc *v) {
  256. luaK_testgo(fs, v, 0, OP_JMPONT);
  257. }
  258. static int code_label (FuncState *fs, OpCode op, int arg) {
  259. luaK_getlabel(fs); /* those instructions may be jump targets */
  260. return luaK_code1(fs, op, arg);
  261. }
  262. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  263. FuncState *fs = ls->fs;
  264. if (!discharge(fs, v)) { /* `v' is an expression? */
  265. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  266. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  267. /* expression has no jumps */
  268. if (onlyone)
  269. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  270. }
  271. else { /* expression has jumps */
  272. int final; /* position after whole expression */
  273. int j = NO_JUMP; /* eventual jump over values */
  274. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  275. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  276. if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF)
  277. || need_value(fs, v->u.l.t, OP_JMPONT)) {
  278. /* expression needs values */
  279. if (ISJUMP(previous))
  280. luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in t. list */
  281. else {
  282. j = code_label(fs, OP_JMP, NO_JUMP); /* to jump over both pushes */
  283. /* correct stack for compiler and symbolic execution */
  284. luaK_adjuststack(fs, 1);
  285. }
  286. p_nil = code_label(fs, OP_PUSHNILJMP, 0);
  287. p_1 = code_label(fs, OP_PUSHINT, 1);
  288. luaK_patchlist(fs, j, luaK_getlabel(fs));
  289. }
  290. final = luaK_getlabel(fs);
  291. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final);
  292. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final);
  293. v->u.l.f = v->u.l.t = NO_JUMP;
  294. }
  295. }
  296. }
  297. void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) {
  298. FuncState *fs = ls->fs;
  299. if (op == OPR_MINUS) {
  300. luaK_tostack(ls, v, 1);
  301. luaK_code0(fs, OP_MINUS);
  302. }
  303. else { /* op == NOT */
  304. Instruction *previous;
  305. discharge1(fs, v);
  306. previous = &fs->f->code[fs->pc-1];
  307. if (ISJUMP(GET_OPCODE(*previous)))
  308. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  309. else
  310. luaK_code0(fs, OP_NOT);
  311. /* interchange true and false lists */
  312. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  313. }
  314. }
  315. void luaK_infix (LexState *ls, BinOpr op, expdesc *v) {
  316. FuncState *fs = ls->fs;
  317. switch (op) {
  318. case OPR_AND:
  319. luaK_goiftrue(fs, v, 1);
  320. break;
  321. case OPR_OR:
  322. luaK_goiffalse(fs, v);
  323. break;
  324. default:
  325. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  326. }
  327. }
  328. static const struct {
  329. OpCode opcode; /* opcode for each binary operator */
  330. int arg; /* default argument for the opcode */
  331. } codes[] = { /* ORDER OPR */
  332. {OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0},
  333. {OP_POW, 0}, {OP_CONCAT, 2},
  334. {OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP},
  335. {OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP},
  336. {OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP}
  337. };
  338. void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
  339. FuncState *fs = ls->fs;
  340. switch (op) {
  341. case OPR_AND: {
  342. lua_assert(v1->u.l.t == NO_JUMP); /* list must be closed */
  343. discharge1(fs, v2);
  344. v1->u.l.t = v2->u.l.t;
  345. luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
  346. break;
  347. }
  348. case OPR_OR: {
  349. lua_assert(v1->u.l.f == NO_JUMP); /* list must be closed */
  350. discharge1(fs, v2);
  351. v1->u.l.f = v2->u.l.f;
  352. luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
  353. break;
  354. }
  355. default: {
  356. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  357. luaK_code1(fs, codes[op].opcode, codes[op].arg);
  358. }
  359. }
  360. }
  361. static void codelineinfo (FuncState *fs) {
  362. Proto *f = fs->f;
  363. LexState *ls = fs->ls;
  364. if (ls->lastline > fs->lastline) {
  365. if (ls->lastline > fs->lastline+1) {
  366. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  367. MAX_INT, l_s("line info overflow"));
  368. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  369. }
  370. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  371. MAX_INT, l_s("line info overflow"));
  372. f->lineinfo[fs->nlineinfo++] = fs->pc;
  373. fs->lastline = ls->lastline;
  374. }
  375. }
  376. int luaK_code0 (FuncState *fs, OpCode o) {
  377. return luaK_code2(fs, o, 0, 0);
  378. }
  379. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  380. return luaK_code2(fs, o, arg1, 0);
  381. }
  382. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  383. Proto *f;
  384. Instruction i = previous_instruction(fs);
  385. int push = (int)luaK_opproperties[o].push;
  386. int pop = (int)luaK_opproperties[o].pop;
  387. int optm = 0; /* 1 when there is an optimization */
  388. switch (o) {
  389. case OP_CLOSURE: {
  390. pop = arg2;
  391. break;
  392. }
  393. case OP_SETTABLE: {
  394. pop = arg2;
  395. break;
  396. }
  397. case OP_SETLIST: {
  398. pop = fs->stacklevel - 1 - arg2;
  399. break;
  400. }
  401. case OP_SETMAP: {
  402. pop = fs->stacklevel - 1 - arg1;
  403. break;
  404. }
  405. case OP_PUSHNIL: {
  406. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  407. push = arg1;
  408. switch(GET_OPCODE(i)) {
  409. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break;
  410. default: break;
  411. }
  412. break;
  413. }
  414. case OP_POP: {
  415. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  416. pop = arg1;
  417. switch(GET_OPCODE(i)) {
  418. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break;
  419. default: break;
  420. }
  421. break;
  422. }
  423. case OP_GETTABLE: {
  424. switch(GET_OPCODE(i)) {
  425. case OP_PUSHSTRING: /* `t.x' */
  426. SET_OPCODE(i, OP_GETDOTTED);
  427. optm = 1;
  428. break;
  429. case OP_GETLOCAL: /* `t[i]' */
  430. SET_OPCODE(i, OP_GETINDEXED);
  431. optm = 1;
  432. break;
  433. default: break;
  434. }
  435. break;
  436. }
  437. case OP_ADD: {
  438. switch(GET_OPCODE(i)) {
  439. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); optm = 1; break; /* `a+k' */
  440. default: break;
  441. }
  442. break;
  443. }
  444. case OP_SUB: {
  445. switch(GET_OPCODE(i)) {
  446. case OP_PUSHINT: /* `a-k' */
  447. i = CREATE_S(OP_ADDI, -GETARG_S(i));
  448. optm = 1;
  449. break;
  450. default: break;
  451. }
  452. break;
  453. }
  454. case OP_CONCAT: {
  455. pop = arg1;
  456. switch(GET_OPCODE(i)) {
  457. case OP_CONCAT: /* `a..b..c' */
  458. SETARG_U(i, GETARG_U(i)+1);
  459. optm = 1;
  460. break;
  461. default: break;
  462. }
  463. break;
  464. }
  465. case OP_MINUS: {
  466. switch(GET_OPCODE(i)) {
  467. case OP_PUSHINT: /* `-k' */
  468. SETARG_S(i, -GETARG_S(i));
  469. optm = 1;
  470. break;
  471. case OP_PUSHNUM: /* `-k' */
  472. SET_OPCODE(i, OP_PUSHNEGNUM);
  473. optm = 1;
  474. break;
  475. default: break;
  476. }
  477. break;
  478. }
  479. case OP_JMPNE: {
  480. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a~=nil' */
  481. i = CREATE_S(OP_JMPT, NO_JUMP);
  482. optm = 1;
  483. }
  484. break;
  485. }
  486. case OP_JMPEQ: {
  487. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  488. i = CREATE_0(OP_NOT);
  489. pop = 1; /* just undo effect of previous PUSHNIL */
  490. optm = 1;
  491. }
  492. break;
  493. }
  494. case OP_JMPT:
  495. case OP_JMPONT: {
  496. switch (GET_OPCODE(i)) {
  497. case OP_NOT: {
  498. i = CREATE_S(OP_JMPF, NO_JUMP);
  499. optm = 1;
  500. break;
  501. }
  502. case OP_PUSHINT: {
  503. if (o == OP_JMPT) { /* JMPONT must keep original integer value */
  504. i = CREATE_S(OP_JMP, NO_JUMP);
  505. optm = 1;
  506. }
  507. break;
  508. }
  509. case OP_PUSHNIL: {
  510. if (GETARG_U(i) == 1) {
  511. fs->pc--; /* erase previous instruction */
  512. luaK_deltastack(fs, -1); /* correct stack */
  513. return NO_JUMP;
  514. }
  515. break;
  516. }
  517. default: break;
  518. }
  519. break;
  520. }
  521. case OP_JMPF:
  522. case OP_JMPONF: {
  523. switch (GET_OPCODE(i)) {
  524. case OP_NOT: {
  525. i = CREATE_S(OP_JMPT, NO_JUMP);
  526. optm = 1;
  527. break;
  528. }
  529. case OP_PUSHINT: { /* `while 1 do ...' */
  530. fs->pc--; /* erase previous instruction */
  531. luaK_deltastack(fs, -1); /* correct stack */
  532. return NO_JUMP;
  533. }
  534. case OP_PUSHNIL: { /* `repeat ... until nil' */
  535. if (GETARG_U(i) == 1) {
  536. i = CREATE_S(OP_JMP, NO_JUMP);
  537. optm = 1;
  538. }
  539. break;
  540. }
  541. default: break;
  542. }
  543. break;
  544. }
  545. case OP_GETDOTTED:
  546. case OP_GETINDEXED:
  547. case OP_ADDI: {
  548. lua_assert(0); /* instruction used only for optimizations */
  549. break;
  550. }
  551. default: {
  552. break;
  553. }
  554. }
  555. f = fs->f;
  556. lua_assert(push != VD);
  557. lua_assert(pop != VD);
  558. luaK_deltastack(fs, push);
  559. luaK_deltastack(fs, -pop);
  560. if (optm) { /* optimize: put instruction in place of last one */
  561. f->code[fs->pc-1] = i; /* change previous instruction */
  562. return fs->pc-1; /* do not generate new instruction */
  563. }
  564. /* else build new instruction */
  565. switch ((enum Mode)luaK_opproperties[o].mode) {
  566. case iO: i = CREATE_0(o); break;
  567. case iU: i = CREATE_U(o, arg1); break;
  568. case iS: i = CREATE_S(o, arg1); break;
  569. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  570. }
  571. codelineinfo(fs);
  572. /* put new instruction in code array */
  573. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  574. MAX_INT, l_s("code size overflow"));
  575. f->code[fs->pc] = i;
  576. return fs->pc++;
  577. }
  578. const OpProperties luaK_opproperties[] = {
  579. {iU, 0, 0}, /* OP_RETURN */
  580. {iAB, 0, 0}, /* OP_CALL */
  581. {iU, VD, 0}, /* OP_PUSHNIL */
  582. {iU, 0, VD}, /* OP_POP */
  583. {iS, 1, 0}, /* OP_PUSHINT */
  584. {iU, 1, 0}, /* OP_PUSHSTRING */
  585. {iU, 1, 0}, /* OP_PUSHNUM */
  586. {iU, 1, 0}, /* OP_PUSHNEGNUM */
  587. {iU, 1, 0}, /* OP_PUSHUPVALUE */
  588. {iU, 1, 0}, /* OP_GETLOCAL */
  589. {iU, 1, 0}, /* OP_GETGLOBAL */
  590. {iO, 1, 2}, /* OP_GETTABLE */
  591. {iU, 1, 1}, /* OP_GETDOTTED */
  592. {iU, 1, 1}, /* OP_GETINDEXED */
  593. {iU, 2, 1}, /* OP_PUSHSELF */
  594. {iU, 1, 0}, /* OP_CREATETABLE */
  595. {iU, 0, 1}, /* OP_SETLOCAL */
  596. {iU, 0, 1}, /* OP_SETGLOBAL */
  597. {iAB, 0, VD}, /* OP_SETTABLE */
  598. {iAB, 0, VD}, /* OP_SETLIST */
  599. {iU, 0, VD}, /* OP_SETMAP */
  600. {iO, 1, 2}, /* OP_ADD */
  601. {iS, 1, 1}, /* OP_ADDI */
  602. {iO, 1, 2}, /* OP_SUB */
  603. {iO, 1, 2}, /* OP_MULT */
  604. {iO, 1, 2}, /* OP_DIV */
  605. {iO, 1, 2}, /* OP_POW */
  606. {iU, 1, VD}, /* OP_CONCAT */
  607. {iO, 1, 1}, /* OP_MINUS */
  608. {iO, 1, 1}, /* OP_NOT */
  609. {iS, 0, 2}, /* OP_JMPNE */
  610. {iS, 0, 2}, /* OP_JMPEQ */
  611. {iS, 0, 2}, /* OP_JMPLT */
  612. {iS, 0, 2}, /* OP_JMPLE */
  613. {iS, 0, 2}, /* OP_JMPGT */
  614. {iS, 0, 2}, /* OP_JMPGE */
  615. {iS, 0, 1}, /* OP_JMPT */
  616. {iS, 0, 1}, /* OP_JMPF */
  617. {iS, 0, 1}, /* OP_JMPONT */
  618. {iS, 0, 1}, /* OP_JMPONF */
  619. {iS, 0, 0}, /* OP_JMP */
  620. {iO, 0, 0}, /* OP_PUSHNILJMP */
  621. {iS, 0, 0}, /* OP_FORPREP */
  622. {iS, 0, 3}, /* OP_FORLOOP */
  623. {iS, 3, 0}, /* OP_LFORPREP */
  624. {iS, 0, 4}, /* OP_LFORLOOP */
  625. {iAB, 1, VD} /* OP_CLOSURE */
  626. };