lcode.c 20 KB

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