lcode.c 19 KB

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