lcode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. ** $Id: lcode.c,v 1.66 2001/03/26 14:31:49 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. for (;;) { /* traverse `l1' */
  221. int next = luaK_getjump(fs, list);
  222. if (next == NO_JUMP) { /* end of list? */
  223. luaK_fixjump(fs, list, l2);
  224. return;
  225. }
  226. list = next;
  227. }
  228. }
  229. }
  230. static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  231. int prevpos; /* position of last instruction */
  232. Instruction *previous;
  233. int *golist, *exitlist;
  234. if (!invert) {
  235. golist = &v->u.l.f; /* go if false */
  236. exitlist = &v->u.l.t; /* exit if true */
  237. }
  238. else {
  239. golist = &v->u.l.t; /* go if true */
  240. exitlist = &v->u.l.f; /* exit if false */
  241. }
  242. discharge1(fs, v);
  243. prevpos = fs->pc-1;
  244. previous = &fs->f->code[prevpos];
  245. lua_assert(*previous==previous_instruction(fs)); /* no jump allowed here */
  246. if (!ISJUMP(GET_OPCODE(*previous)))
  247. prevpos = luaK_code1(fs, jump, NO_JUMP);
  248. else { /* last instruction is already a jump */
  249. if (invert)
  250. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  251. }
  252. luaK_concat(fs, exitlist, prevpos); /* insert last jump in `exitlist' */
  253. luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  254. *golist = NO_JUMP;
  255. }
  256. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  257. luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF);
  258. }
  259. static void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  260. luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT);
  261. }
  262. static int code_label (FuncState *fs, OpCode op, int arg) {
  263. luaK_getlabel(fs); /* those instructions may be jump targets */
  264. return luaK_code1(fs, op, arg);
  265. }
  266. void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
  267. FuncState *fs = ls->fs;
  268. if (!discharge(fs, v)) { /* `v' is an expression? */
  269. OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
  270. if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
  271. /* expression has no jumps */
  272. if (onlyone)
  273. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  274. }
  275. else { /* expression has jumps */
  276. int final; /* position after whole expression */
  277. int j = NO_JUMP; /* eventual jump over values */
  278. int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
  279. int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
  280. if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF)
  281. || need_value(fs, v->u.l.t, OP_JMPONT)) {
  282. /* expression needs values */
  283. if (ISJUMP(previous))
  284. luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in t. list */
  285. else {
  286. j = code_label(fs, OP_JMP, NO_JUMP); /* to jump over both pushes */
  287. /* correct stack for compiler and symbolic execution */
  288. luaK_adjuststack(fs, 1);
  289. }
  290. p_nil = code_label(fs, OP_PUSHNILJMP, 0);
  291. p_1 = code_label(fs, OP_PUSHINT, 1);
  292. luaK_patchlist(fs, j, luaK_getlabel(fs));
  293. }
  294. final = luaK_getlabel(fs);
  295. luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final);
  296. luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final);
  297. v->u.l.f = v->u.l.t = NO_JUMP;
  298. }
  299. }
  300. }
  301. void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) {
  302. FuncState *fs = ls->fs;
  303. if (op == OPR_MINUS) {
  304. luaK_tostack(ls, v, 1);
  305. luaK_code0(fs, OP_MINUS);
  306. }
  307. else { /* op == NOT */
  308. Instruction *previous;
  309. discharge1(fs, v);
  310. previous = &fs->f->code[fs->pc-1];
  311. if (ISJUMP(GET_OPCODE(*previous)))
  312. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  313. else
  314. luaK_code0(fs, OP_NOT);
  315. /* interchange true and false lists */
  316. { int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
  317. }
  318. }
  319. void luaK_infix (LexState *ls, BinOpr op, expdesc *v) {
  320. FuncState *fs = ls->fs;
  321. switch (op) {
  322. case OPR_AND:
  323. luaK_goiftrue(fs, v, 1);
  324. break;
  325. case OPR_OR:
  326. luaK_goiffalse(fs, v, 1);
  327. break;
  328. default:
  329. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  330. }
  331. }
  332. static const struct {
  333. OpCode opcode; /* opcode for each binary operator */
  334. int arg; /* default argument for the opcode */
  335. } codes[] = { /* ORDER OPR */
  336. {OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0},
  337. {OP_POW, 0}, {OP_CONCAT, 2},
  338. {OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP},
  339. {OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP},
  340. {OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP}
  341. };
  342. void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
  343. FuncState *fs = ls->fs;
  344. switch (op) {
  345. case OPR_AND: {
  346. lua_assert(v1->u.l.t == NO_JUMP); /* list must be closed */
  347. discharge1(fs, v2);
  348. v1->u.l.t = v2->u.l.t;
  349. luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
  350. break;
  351. }
  352. case OPR_OR: {
  353. lua_assert(v1->u.l.f == NO_JUMP); /* list must be closed */
  354. discharge1(fs, v2);
  355. v1->u.l.f = v2->u.l.f;
  356. luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
  357. break;
  358. }
  359. default: {
  360. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  361. luaK_code1(fs, codes[op].opcode, codes[op].arg);
  362. }
  363. }
  364. }
  365. static void codelineinfo (FuncState *fs) {
  366. Proto *f = fs->f;
  367. LexState *ls = fs->ls;
  368. if (ls->lastline > fs->lastline) {
  369. if (ls->lastline > fs->lastline+1) {
  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++] = -(ls->lastline - (fs->lastline+1));
  373. }
  374. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  375. MAX_INT, l_s("line info overflow"));
  376. f->lineinfo[fs->nlineinfo++] = fs->pc;
  377. fs->lastline = ls->lastline;
  378. }
  379. }
  380. int luaK_code0 (FuncState *fs, OpCode o) {
  381. return luaK_code2(fs, o, 0, 0);
  382. }
  383. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  384. return luaK_code2(fs, o, arg1, 0);
  385. }
  386. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  387. Proto *f;
  388. Instruction i = previous_instruction(fs);
  389. int push = (int)luaK_opproperties[o].push;
  390. int pop = (int)luaK_opproperties[o].pop;
  391. int optm = 0; /* 1 when there is an optimization */
  392. switch (o) {
  393. case OP_CLOSURE: {
  394. pop = arg2;
  395. break;
  396. }
  397. case OP_SETTABLE: {
  398. pop = arg2;
  399. break;
  400. }
  401. case OP_SETLIST: {
  402. pop = fs->stacklevel - 1 - arg2;
  403. break;
  404. }
  405. case OP_SETMAP: {
  406. pop = fs->stacklevel - 1 - arg1;
  407. break;
  408. }
  409. case OP_PUSHNIL: {
  410. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  411. push = arg1;
  412. switch(GET_OPCODE(i)) {
  413. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break;
  414. default: break;
  415. }
  416. break;
  417. }
  418. case OP_POP: {
  419. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  420. pop = arg1;
  421. switch(GET_OPCODE(i)) {
  422. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break;
  423. default: break;
  424. }
  425. break;
  426. }
  427. case OP_GETTABLE: {
  428. switch(GET_OPCODE(i)) {
  429. case OP_PUSHSTRING: /* `t.x' */
  430. SET_OPCODE(i, OP_GETDOTTED);
  431. optm = 1;
  432. break;
  433. case OP_GETLOCAL: /* `t[i]' */
  434. SET_OPCODE(i, OP_GETINDEXED);
  435. optm = 1;
  436. break;
  437. default: break;
  438. }
  439. break;
  440. }
  441. case OP_ADD: {
  442. switch(GET_OPCODE(i)) {
  443. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); optm = 1; break; /* `a+k' */
  444. default: break;
  445. }
  446. break;
  447. }
  448. case OP_SUB: {
  449. switch(GET_OPCODE(i)) {
  450. case OP_PUSHINT: /* `a-k' */
  451. i = CREATE_S(OP_ADDI, -GETARG_S(i));
  452. optm = 1;
  453. break;
  454. default: break;
  455. }
  456. break;
  457. }
  458. case OP_CONCAT: {
  459. pop = arg1;
  460. switch(GET_OPCODE(i)) {
  461. case OP_CONCAT: /* `a..b..c' */
  462. SETARG_U(i, GETARG_U(i)+1);
  463. optm = 1;
  464. break;
  465. default: break;
  466. }
  467. break;
  468. }
  469. case OP_MINUS: {
  470. switch(GET_OPCODE(i)) {
  471. case OP_PUSHINT: /* `-k' */
  472. SETARG_S(i, -GETARG_S(i));
  473. optm = 1;
  474. break;
  475. case OP_PUSHNUM: /* `-k' */
  476. SET_OPCODE(i, OP_PUSHNEGNUM);
  477. optm = 1;
  478. break;
  479. default: break;
  480. }
  481. break;
  482. }
  483. case OP_JMPNE: {
  484. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a~=nil' */
  485. i = CREATE_S(OP_JMPT, NO_JUMP);
  486. optm = 1;
  487. }
  488. break;
  489. }
  490. case OP_JMPEQ: {
  491. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  492. i = CREATE_0(OP_NOT);
  493. pop = 1; /* just undo effect of previous PUSHNIL */
  494. optm = 1;
  495. }
  496. break;
  497. }
  498. case OP_JMPT:
  499. case OP_JMPONT: {
  500. switch (GET_OPCODE(i)) {
  501. case OP_NOT: {
  502. i = CREATE_S(OP_JMPF, NO_JUMP);
  503. optm = 1;
  504. break;
  505. }
  506. case OP_PUSHINT: {
  507. if (o == OP_JMPT) { /* JMPONT must keep original integer value */
  508. i = CREATE_S(OP_JMP, NO_JUMP);
  509. optm = 1;
  510. }
  511. break;
  512. }
  513. case OP_PUSHNIL: {
  514. if (GETARG_U(i) == 1) {
  515. fs->pc--; /* erase previous instruction */
  516. luaK_deltastack(fs, -1); /* correct stack */
  517. return NO_JUMP;
  518. }
  519. break;
  520. }
  521. default: break;
  522. }
  523. break;
  524. }
  525. case OP_JMPF:
  526. case OP_JMPONF: {
  527. switch (GET_OPCODE(i)) {
  528. case OP_NOT: {
  529. i = CREATE_S(OP_JMPT, NO_JUMP);
  530. optm = 1;
  531. break;
  532. }
  533. case OP_PUSHINT: { /* `while 1 do ...' */
  534. fs->pc--; /* erase previous instruction */
  535. luaK_deltastack(fs, -1); /* correct stack */
  536. return NO_JUMP;
  537. }
  538. case OP_PUSHNIL: { /* `repeat ... until nil' */
  539. if (GETARG_U(i) == 1) {
  540. i = CREATE_S(OP_JMP, NO_JUMP);
  541. optm = 1;
  542. }
  543. break;
  544. }
  545. default: break;
  546. }
  547. break;
  548. }
  549. case OP_GETDOTTED:
  550. case OP_GETINDEXED:
  551. case OP_ADDI: {
  552. lua_assert(0); /* instruction used only for optimizations */
  553. break;
  554. }
  555. default: {
  556. break;
  557. }
  558. }
  559. f = fs->f;
  560. lua_assert(push != VD);
  561. lua_assert(pop != VD);
  562. luaK_deltastack(fs, push);
  563. luaK_deltastack(fs, -pop);
  564. if (optm) { /* optimize: put instruction in place of last one */
  565. f->code[fs->pc-1] = i; /* change previous instruction */
  566. return fs->pc-1; /* do not generate new instruction */
  567. }
  568. /* else build new instruction */
  569. switch ((enum Mode)luaK_opproperties[o].mode) {
  570. case iO: i = CREATE_0(o); break;
  571. case iU: i = CREATE_U(o, arg1); break;
  572. case iS: i = CREATE_S(o, arg1); break;
  573. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  574. }
  575. codelineinfo(fs);
  576. /* put new instruction in code array */
  577. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  578. MAX_INT, l_s("code size overflow"));
  579. f->code[fs->pc] = i;
  580. return fs->pc++;
  581. }
  582. const OpProperties luaK_opproperties[] = {
  583. {iU, 0, 0}, /* OP_RETURN */
  584. {iAB, 0, 0}, /* OP_CALL */
  585. {iU, VD, 0}, /* OP_PUSHNIL */
  586. {iU, 0, VD}, /* OP_POP */
  587. {iS, 1, 0}, /* OP_PUSHINT */
  588. {iU, 1, 0}, /* OP_PUSHSTRING */
  589. {iU, 1, 0}, /* OP_PUSHNUM */
  590. {iU, 1, 0}, /* OP_PUSHNEGNUM */
  591. {iU, 1, 0}, /* OP_PUSHUPVALUE */
  592. {iU, 1, 0}, /* OP_GETLOCAL */
  593. {iU, 1, 0}, /* OP_GETGLOBAL */
  594. {iO, 1, 2}, /* OP_GETTABLE */
  595. {iU, 1, 1}, /* OP_GETDOTTED */
  596. {iU, 1, 1}, /* OP_GETINDEXED */
  597. {iU, 2, 1}, /* OP_PUSHSELF */
  598. {iU, 1, 0}, /* OP_CREATETABLE */
  599. {iU, 0, 1}, /* OP_SETLOCAL */
  600. {iU, 0, 1}, /* OP_SETGLOBAL */
  601. {iAB, 0, VD}, /* OP_SETTABLE */
  602. {iAB, 0, VD}, /* OP_SETLIST */
  603. {iU, 0, VD}, /* OP_SETMAP */
  604. {iO, 1, 2}, /* OP_ADD */
  605. {iS, 1, 1}, /* OP_ADDI */
  606. {iO, 1, 2}, /* OP_SUB */
  607. {iO, 1, 2}, /* OP_MULT */
  608. {iO, 1, 2}, /* OP_DIV */
  609. {iO, 1, 2}, /* OP_POW */
  610. {iU, 1, VD}, /* OP_CONCAT */
  611. {iO, 1, 1}, /* OP_MINUS */
  612. {iO, 1, 1}, /* OP_NOT */
  613. {iS, 0, 2}, /* OP_JMPNE */
  614. {iS, 0, 2}, /* OP_JMPEQ */
  615. {iS, 0, 2}, /* OP_JMPLT */
  616. {iS, 0, 2}, /* OP_JMPLE */
  617. {iS, 0, 2}, /* OP_JMPGT */
  618. {iS, 0, 2}, /* OP_JMPGE */
  619. {iS, 0, 1}, /* OP_JMPT */
  620. {iS, 0, 1}, /* OP_JMPF */
  621. {iS, 0, 1}, /* OP_JMPONT */
  622. {iS, 0, 1}, /* OP_JMPONF */
  623. {iS, 0, 0}, /* OP_JMP */
  624. {iO, 0, 0}, /* OP_PUSHNILJMP */
  625. {iS, 0, 0}, /* OP_FORPREP */
  626. {iS, 0, 3}, /* OP_FORLOOP */
  627. {iS, 3, 0}, /* OP_LFORPREP */
  628. {iS, 0, 4}, /* OP_LFORLOOP */
  629. {iAB, 1, VD} /* OP_CLOSURE */
  630. };