lcode.c 19 KB

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