lcode.c 19 KB

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