lcode.c 19 KB

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