lcode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. ** $Id: lcode.c,v 1.50 2000/08/31 14:08:27 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. Proto *f = fs->f;
  357. LexState *ls = fs->ls;
  358. if (ls->lastline > fs->lastline) {
  359. luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, 2, int,
  360. "line info overflow", MAX_INT);
  361. if (ls->lastline > fs->lastline+1)
  362. f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  363. f->lineinfo[f->nlineinfo++] = fs->pc;
  364. fs->lastline = ls->lastline;
  365. }
  366. }
  367. int luaK_code0 (FuncState *fs, OpCode o) {
  368. return luaK_code2(fs, o, 0, 0);
  369. }
  370. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  371. return luaK_code2(fs, o, arg1, 0);
  372. }
  373. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  374. Instruction i = previous_instruction(fs);
  375. int delta = luaK_opproperties[o].push - luaK_opproperties[o].pop;
  376. int optm = 0; /* 1 when there is an optimization */
  377. switch (o) {
  378. case OP_CLOSURE: {
  379. delta = -arg2+1;
  380. break;
  381. }
  382. case OP_SETTABLE: {
  383. delta = -arg2;
  384. break;
  385. }
  386. case OP_SETLIST: {
  387. if (arg2 == 0) return NO_JUMP; /* nothing to do */
  388. delta = -arg2;
  389. break;
  390. }
  391. case OP_SETMAP: {
  392. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  393. delta = -2*arg1;
  394. break;
  395. }
  396. case OP_RETURN: {
  397. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
  398. SET_OPCODE(i, OP_TAILCALL);
  399. SETARG_B(i, arg1);
  400. optm = 1;
  401. }
  402. break;
  403. }
  404. case OP_PUSHNIL: {
  405. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  406. delta = arg1;
  407. switch(GET_OPCODE(i)) {
  408. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break;
  409. default: break;
  410. }
  411. break;
  412. }
  413. case OP_POP: {
  414. if (arg1 == 0) return NO_JUMP; /* nothing to do */
  415. delta = -arg1;
  416. switch(GET_OPCODE(i)) {
  417. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break;
  418. default: break;
  419. }
  420. break;
  421. }
  422. case OP_GETTABLE: {
  423. switch(GET_OPCODE(i)) {
  424. case OP_PUSHSTRING: /* `t.x' */
  425. SET_OPCODE(i, OP_GETDOTTED);
  426. optm = 1;
  427. break;
  428. case OP_GETLOCAL: /* `t[i]' */
  429. SET_OPCODE(i, OP_GETINDEXED);
  430. optm = 1;
  431. break;
  432. default: break;
  433. }
  434. break;
  435. }
  436. case OP_ADD: {
  437. switch(GET_OPCODE(i)) {
  438. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); optm = 1; break; /* `a+k' */
  439. default: break;
  440. }
  441. break;
  442. }
  443. case OP_SUB: {
  444. switch(GET_OPCODE(i)) {
  445. case OP_PUSHINT: /* `a-k' */
  446. i = CREATE_S(OP_ADDI, -GETARG_S(i));
  447. optm = 1;
  448. break;
  449. default: break;
  450. }
  451. break;
  452. }
  453. case OP_CONCAT: {
  454. delta = -arg1+1;
  455. switch(GET_OPCODE(i)) {
  456. case OP_CONCAT: /* `a..b..c' */
  457. SETARG_U(i, GETARG_U(i)+1);
  458. optm = 1;
  459. break;
  460. default: break;
  461. }
  462. break;
  463. }
  464. case OP_MINUS: {
  465. switch(GET_OPCODE(i)) {
  466. case OP_PUSHINT: /* `-k' */
  467. SETARG_S(i, -GETARG_S(i));
  468. optm = 1;
  469. break;
  470. case OP_PUSHNUM: /* `-k' */
  471. SET_OPCODE(i, OP_PUSHNEGNUM);
  472. optm = 1;
  473. break;
  474. default: break;
  475. }
  476. break;
  477. }
  478. case OP_JMPNE: {
  479. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a~=nil' */
  480. i = CREATE_S(OP_JMPT, NO_JUMP);
  481. optm = 1;
  482. }
  483. break;
  484. }
  485. case OP_JMPEQ: {
  486. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  487. i = CREATE_0(OP_NOT);
  488. delta = -1; /* just undo effect of previous PUSHNIL */
  489. optm = 1;
  490. }
  491. break;
  492. }
  493. case OP_JMPT:
  494. case OP_JMPONT: {
  495. switch (GET_OPCODE(i)) {
  496. case OP_NOT: {
  497. i = CREATE_S(OP_JMPF, NO_JUMP);
  498. optm = 1;
  499. break;
  500. }
  501. case OP_PUSHINT: {
  502. if (o == OP_JMPT) { /* JMPONT must keep original integer value */
  503. i = CREATE_S(OP_JMP, NO_JUMP);
  504. optm = 1;
  505. }
  506. break;
  507. }
  508. case OP_PUSHNIL: {
  509. if (GETARG_U(i) == 1) {
  510. fs->pc--; /* erase previous instruction */
  511. luaK_deltastack(fs, -1); /* correct stack */
  512. return NO_JUMP;
  513. }
  514. break;
  515. }
  516. default: break;
  517. }
  518. break;
  519. }
  520. case OP_JMPF:
  521. case OP_JMPONF: {
  522. switch (GET_OPCODE(i)) {
  523. case OP_NOT: {
  524. i = CREATE_S(OP_JMPT, NO_JUMP);
  525. optm = 1;
  526. break;
  527. }
  528. case OP_PUSHINT: { /* `while 1 do ...' */
  529. fs->pc--; /* erase previous instruction */
  530. luaK_deltastack(fs, -1); /* correct stack */
  531. return NO_JUMP;
  532. }
  533. case OP_PUSHNIL: { /* `repeat ... until nil' */
  534. if (GETARG_U(i) == 1) {
  535. i = CREATE_S(OP_JMP, NO_JUMP);
  536. optm = 1;
  537. }
  538. break;
  539. }
  540. default: break;
  541. }
  542. break;
  543. }
  544. case OP_GETDOTTED:
  545. case OP_GETINDEXED:
  546. case OP_TAILCALL:
  547. case OP_ADDI: {
  548. LUA_INTERNALERROR("instruction used only for optimizations");
  549. break;
  550. }
  551. default: {
  552. LUA_ASSERT(delta != VD, "invalid delta");
  553. break;
  554. }
  555. }
  556. luaK_deltastack(fs, delta);
  557. if (optm) { /* optimize: put instruction in place of last one */
  558. fs->f->code[fs->pc-1] = i; /* change previous instruction */
  559. return fs->pc-1; /* do not generate new instruction */
  560. }
  561. /* else build new instruction */
  562. switch ((enum Mode)luaK_opproperties[o].mode) {
  563. case iO: i = CREATE_0(o); break;
  564. case iU: i = CREATE_U(o, arg1); break;
  565. case iS: i = CREATE_S(o, arg1); break;
  566. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  567. }
  568. codelineinfo(fs);
  569. /* put new instruction in code array */
  570. luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction,
  571. "code size overflow", MAX_INT);
  572. fs->f->code[fs->pc] = i;
  573. return fs->pc++;
  574. }
  575. const struct OpProperties luaK_opproperties[NUM_OPCODES] = {
  576. {iO, 0, 0}, /* OP_END */
  577. {iU, 0, 0}, /* OP_RETURN */
  578. {iAB, 0, 0}, /* OP_CALL */
  579. {iAB, 0, 0}, /* OP_TAILCALL */
  580. {iU, VD, 0}, /* OP_PUSHNIL */
  581. {iU, VD, 0}, /* OP_POP */
  582. {iS, 1, 0}, /* OP_PUSHINT */
  583. {iU, 1, 0}, /* OP_PUSHSTRING */
  584. {iU, 1, 0}, /* OP_PUSHNUM */
  585. {iU, 1, 0}, /* OP_PUSHNEGNUM */
  586. {iU, 1, 0}, /* OP_PUSHUPVALUE */
  587. {iU, 1, 0}, /* OP_GETLOCAL */
  588. {iU, 1, 0}, /* OP_GETGLOBAL */
  589. {iO, 1, 2}, /* OP_GETTABLE */
  590. {iU, 1, 1}, /* OP_GETDOTTED */
  591. {iU, 1, 1}, /* OP_GETINDEXED */
  592. {iU, 2, 1}, /* OP_PUSHSELF */
  593. {iU, 1, 0}, /* OP_CREATETABLE */
  594. {iU, 0, 1}, /* OP_SETLOCAL */
  595. {iU, 0, 1}, /* OP_SETGLOBAL */
  596. {iAB, VD, 0}, /* OP_SETTABLE */
  597. {iAB, VD, 0}, /* OP_SETLIST */
  598. {iU, VD, 0}, /* OP_SETMAP */
  599. {iO, 1, 2}, /* OP_ADD */
  600. {iS, 1, 1}, /* OP_ADDI */
  601. {iO, 1, 2}, /* OP_SUB */
  602. {iO, 1, 2}, /* OP_MULT */
  603. {iO, 1, 2}, /* OP_DIV */
  604. {iO, 1, 2}, /* OP_POW */
  605. {iU, VD, 0}, /* OP_CONCAT */
  606. {iO, 1, 1}, /* OP_MINUS */
  607. {iO, 1, 1}, /* OP_NOT */
  608. {iS, 0, 2}, /* OP_JMPNE */
  609. {iS, 0, 2}, /* OP_JMPEQ */
  610. {iS, 0, 2}, /* OP_JMPLT */
  611. {iS, 0, 2}, /* OP_JMPLE */
  612. {iS, 0, 2}, /* OP_JMPGT */
  613. {iS, 0, 2}, /* OP_JMPGE */
  614. {iS, 0, 1}, /* OP_JMPT */
  615. {iS, 0, 1}, /* OP_JMPF */
  616. {iS, 0, 1}, /* OP_JMPONT */
  617. {iS, 0, 1}, /* OP_JMPONF */
  618. {iS, 0, 0}, /* OP_JMP */
  619. {iO, 0, 0}, /* OP_PUSHNILJMP */
  620. {iS, 0, 0}, /* OP_FORPREP */
  621. {iS, 0, 3}, /* OP_FORLOOP */
  622. {iS, 2, 0}, /* OP_LFORPREP */
  623. {iS, 0, 3}, /* OP_LFORLOOP */
  624. {iAB, VD, 0} /* OP_CLOSURE */
  625. };