2
0

lcode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. ** $Id: lcode.c,v 1.27 2000/04/17 14:05:34 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 "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. #include "lstring.h"
  16. void luaK_error (LexState *ls, const char *msg) {
  17. luaX_error(ls, msg, ls->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_code0(fs, OP_JMP);
  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. ** discharge list of jumps to last target.
  58. ** returns current `pc' and marks it as a jump target (to avoid wrong
  59. ** optimizations with consecutive instructions not in the same basic block).
  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 (till now) */
  67. }
  68. return fs->pc;
  69. }
  70. void luaK_deltastack (FuncState *fs, int delta) {
  71. fs->stacklevel += delta;
  72. if (delta > 0 && 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 real_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, constantEM, 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 && (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, real_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 if (n < 0)
  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 void assertglobal (FuncState *fs, int index) {
  120. luaS_assertglobal(fs->L, fs->f->kstr[index]);
  121. }
  122. static int discharge (FuncState *fs, expdesc *var) {
  123. switch (var->k) {
  124. case VLOCAL:
  125. luaK_code1(fs, OP_GETLOCAL, var->u.index);
  126. break;
  127. case VGLOBAL:
  128. luaK_code1(fs, OP_GETGLOBAL, var->u.index);
  129. assertglobal(fs, var->u.index); /* make sure that there is a global */
  130. break;
  131. case VINDEXED:
  132. luaK_code0(fs, OP_GETTABLE);
  133. break;
  134. case VEXP:
  135. return 0; /* nothing to do */
  136. }
  137. var->k = VEXP;
  138. var->u.l.t = var->u.l.f = NO_JUMP;
  139. return 1;
  140. }
  141. static void discharge1 (FuncState *fs, expdesc *var) {
  142. discharge(fs, var);
  143. /* if it has jumps it is already discharged */
  144. if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
  145. luaK_setcallreturns(fs, 1); /* call must return 1 value */
  146. }
  147. void luaK_storevar (LexState *ls, const expdesc *var) {
  148. FuncState *fs = ls->fs;
  149. switch (var->k) {
  150. case VLOCAL:
  151. luaK_code1(fs, OP_SETLOCAL, var->u.index);
  152. break;
  153. case VGLOBAL:
  154. luaK_code1(fs, OP_SETGLOBAL, var->u.index);
  155. assertglobal(fs, var->u.index); /* make sure that there is a global */
  156. break;
  157. case VINDEXED: /* table is at top-3; pop 3 elements after operation */
  158. luaK_code2(fs, OP_SETTABLE, 3, 3);
  159. break;
  160. default:
  161. LUA_INTERNALERROR(ls->L, "invalid var kind to store");
  162. }
  163. }
  164. static OpCode invertjump (OpCode op) {
  165. switch (op) {
  166. case OP_JMPNE: return OP_JMPEQ;
  167. case OP_JMPEQ: return OP_JMPNE;
  168. case OP_JMPLT: return OP_JMPGE;
  169. case OP_JMPLE: return OP_JMPGT;
  170. case OP_JMPGT: return OP_JMPLE;
  171. case OP_JMPGE: return OP_JMPLT;
  172. case OP_JMPT: case OP_JMPONT: return OP_JMPF;
  173. case OP_JMPF: case OP_JMPONF: return OP_JMPT;
  174. default:
  175. LUA_INTERNALERROR(NULL, "invalid jump instruction");
  176. return OP_END; /* to avoid warnings */
  177. }
  178. }
  179. static void luaK_patchlistaux (FuncState *fs, int list, int target,
  180. OpCode special, int special_target) {
  181. Instruction *code = fs->f->code;
  182. while (list != NO_JUMP) {
  183. int next = luaK_getjump(fs, list);
  184. Instruction *i = &code[list];
  185. OpCode op = GET_OPCODE(*i);
  186. if (op == special) /* this `op' already has a value */
  187. luaK_fixjump(fs, list, special_target);
  188. else {
  189. luaK_fixjump(fs, list, target); /* do the patch */
  190. if (op == OP_JMPONT) /* remove eventual values */
  191. SET_OPCODE(*i, OP_JMPT);
  192. else if (op == OP_JMPONF)
  193. SET_OPCODE(*i, OP_JMPF);
  194. }
  195. list = next;
  196. }
  197. }
  198. void luaK_patchlist (FuncState *fs, int list, int target) {
  199. if (target == fs->lasttarget) /* same target that list `jlt'? */
  200. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  201. else
  202. luaK_patchlistaux(fs, list, target, OP_END, 0);
  203. }
  204. static int need_value (FuncState *fs, int list, OpCode hasvalue) {
  205. /* check whether list has a jump without a value */
  206. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  207. if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
  208. return 0; /* not found */
  209. }
  210. void luaK_concat (FuncState *fs, int *l1, int l2) {
  211. if (*l1 == NO_JUMP)
  212. *l1 = l2;
  213. else {
  214. int list = *l1;
  215. for (;;) { /* traverse `l1' */
  216. int next = luaK_getjump(fs, list);
  217. if (next == NO_JUMP) { /* end of list? */
  218. luaK_fixjump(fs, list, l2);
  219. return;
  220. }
  221. list = next;
  222. }
  223. }
  224. }
  225. static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
  226. Instruction *previous;
  227. int *golist = &v->u.l.f;
  228. int *exitlist = &v->u.l.t;
  229. if (invert) { /* interchange `golist' and `exitlist' */
  230. int *temp = golist; golist = exitlist; exitlist = temp;
  231. }
  232. discharge1(fs, v);
  233. previous = &fs->f->code[fs->pc-1];
  234. LUA_ASSERT(L, GET_OPCODE(*previous) != OP_SETLINE, "bad place to set line");
  235. if (ISJUMP(GET_OPCODE(*previous))) {
  236. if (invert)
  237. SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
  238. }
  239. else
  240. luaK_code0(fs, jump);
  241. luaK_concat(fs, exitlist, fs->pc-1); /* insert last jump in `exitlist' */
  242. luaK_patchlist(fs, *golist, luaK_getlabel(fs));
  243. *golist = NO_JUMP;
  244. }
  245. void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
  246. luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF);
  247. }
  248. void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
  249. luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT);
  250. }
  251. static int code_label (FuncState *fs, OpCode op, int arg) {
  252. int j = luaK_getlabel(fs);
  253. luaK_code1(fs, op, arg);
  254. return j;
  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. LUA_ASSERT(L, previous != OP_SETLINE, "bad place to set line");
  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, 0); /* 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. 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, int op, expdesc *v) {
  292. FuncState *fs = ls->fs;
  293. if (op == '-') {
  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, int op, expdesc *v) {
  310. FuncState *fs = ls->fs;
  311. if (op == TK_AND)
  312. luaK_goiftrue(fs, v, 1);
  313. else if (op == TK_OR)
  314. luaK_goiffalse(fs, v, 1);
  315. else
  316. luaK_tostack(ls, v, 1); /* all other binary operators need a value */
  317. }
  318. void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
  319. FuncState *fs = ls->fs;
  320. if (op == TK_AND) {
  321. LUA_ASSERT(ls->L, v1->u.l.t == NO_JUMP, "list must be closed");
  322. discharge1(fs, v2);
  323. v1->u.l.t = v2->u.l.t;
  324. luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
  325. }
  326. else if (op == TK_OR) {
  327. LUA_ASSERT(ls->L, v1->u.l.f == NO_JUMP, "list must be closed");
  328. discharge1(fs, v2);
  329. v1->u.l.f = v2->u.l.f;
  330. luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
  331. }
  332. else {
  333. luaK_tostack(ls, v2, 1); /* `v2' must be a value */
  334. switch (op) {
  335. case '+': luaK_code0(fs, OP_ADD); break;
  336. case '-': luaK_code0(fs, OP_SUB); break;
  337. case '*': luaK_code0(fs, OP_MULT); break;
  338. case '/': luaK_code0(fs, OP_DIV); break;
  339. case '^': luaK_code0(fs, OP_POW); break;
  340. case TK_CONCAT: luaK_code1(fs, OP_CONCAT, 2); break;
  341. case TK_EQ: luaK_code0(fs, OP_JMPEQ); break;
  342. case TK_NE: luaK_code0(fs, OP_JMPNE); break;
  343. case '>': luaK_code0(fs, OP_JMPGT); break;
  344. case '<': luaK_code0(fs, OP_JMPLT); break;
  345. case TK_GE: luaK_code0(fs, OP_JMPGE); break;
  346. case TK_LE: luaK_code0(fs, OP_JMPLE); break;
  347. }
  348. }
  349. }
  350. int luaK_code0 (FuncState *fs, OpCode o) {
  351. return luaK_code2(fs, o, 0, 0);
  352. }
  353. int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
  354. return luaK_code2(fs, o, arg1, 0);
  355. }
  356. int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
  357. Instruction i = previous_instruction(fs);
  358. int delta = 0;
  359. enum {iO, iU, iS, iAB, iP} mode; /* instruction format (or iP to optimize) */
  360. mode = iP;
  361. switch (o) {
  362. case OP_CLOSURE:
  363. delta = -arg2+1;
  364. mode = iAB;
  365. break;
  366. case OP_SETLINE:
  367. mode = iU;
  368. break;
  369. case OP_CALL:
  370. mode = iAB;
  371. break;
  372. case OP_PUSHINT:
  373. delta = 1;
  374. mode = iS;
  375. break;
  376. case OP_SETTABLE:
  377. delta = -arg2;
  378. mode = iAB;
  379. break;
  380. case OP_SETLIST:
  381. delta = -(arg2+1);
  382. mode = iAB;
  383. break;
  384. case OP_SETMAP:
  385. delta = -2*(arg1+1);
  386. mode = iU;
  387. break;
  388. case OP_FORLOOP:
  389. delta = -3;
  390. arg1 = NO_JUMP;
  391. mode = iS;
  392. break;
  393. case OP_SETLOCAL:
  394. case OP_SETGLOBAL:
  395. delta = -1;
  396. mode = iU;
  397. break;
  398. case OP_FORPREP:
  399. case OP_JMP:
  400. arg1 = NO_JUMP;
  401. mode = iS;
  402. break;
  403. case OP_END:
  404. case OP_PUSHNILJMP:
  405. case OP_NOT:
  406. mode = iO;
  407. break;
  408. case OP_PUSHSTRING:
  409. case OP_PUSHNUM:
  410. case OP_PUSHNEGNUM:
  411. case OP_PUSHUPVALUE:
  412. case OP_GETLOCAL:
  413. case OP_GETGLOBAL:
  414. case OP_PUSHSELF:
  415. case OP_CREATETABLE:
  416. delta = 1;
  417. mode = iU;
  418. break;
  419. case OP_JMPLT:
  420. case OP_JMPLE:
  421. case OP_JMPGT:
  422. case OP_JMPGE:
  423. delta = -2;
  424. arg1 = NO_JUMP;
  425. mode = iS;
  426. break;
  427. case OP_MULT:
  428. case OP_DIV:
  429. case OP_POW:
  430. delta = -1;
  431. mode = iO;
  432. break;
  433. case OP_RETURN:
  434. if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
  435. SET_OPCODE(i, OP_TAILCALL);
  436. SETARG_B(i, arg1);
  437. }
  438. else mode = iU;
  439. break;
  440. case OP_PUSHNIL:
  441. delta = arg1;
  442. switch(GET_OPCODE(i)) {
  443. case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); break;
  444. default: mode = iU; break;
  445. }
  446. break;
  447. case OP_POP:
  448. delta = -arg1;
  449. switch(GET_OPCODE(i)) {
  450. case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); break;
  451. default: mode = iU; break;
  452. }
  453. break;
  454. case OP_GETTABLE:
  455. delta = -1;
  456. switch(GET_OPCODE(i)) {
  457. case OP_PUSHSTRING: SET_OPCODE(i, OP_GETDOTTED); break; /* `t.x' */
  458. case OP_GETLOCAL: SET_OPCODE(i, OP_GETINDEXED); break; /* `t[i]' */
  459. default: mode = iO; break;
  460. }
  461. break;
  462. case OP_ADD:
  463. delta = -1;
  464. switch(GET_OPCODE(i)) {
  465. case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); break; /* `a+k' */
  466. default: mode = iO; break;
  467. }
  468. break;
  469. case OP_SUB:
  470. delta = -1;
  471. switch(GET_OPCODE(i)) {
  472. case OP_PUSHINT: i = CREATE_S(OP_ADDI, -GETARG_S(i)); break; /* `a-k' */
  473. default: mode = iO; break;
  474. }
  475. break;
  476. case OP_CONCAT:
  477. delta = -arg1+1;
  478. switch(GET_OPCODE(i)) {
  479. case OP_CONCAT: SETARG_U(i, GETARG_U(i)+1); break; /* `a..b..c' */
  480. default: mode = iU; break;
  481. }
  482. break;
  483. case OP_MINUS:
  484. switch(GET_OPCODE(i)) {
  485. case OP_PUSHINT: SETARG_S(i, -GETARG_S(i)); break; /* `-k' */
  486. case OP_PUSHNUM: SET_OPCODE(i, OP_PUSHNEGNUM); break; /* `-k' */
  487. default: mode = iO; break;
  488. }
  489. break;
  490. case OP_JMPNE:
  491. delta = -2;
  492. if (i == CREATE_U(OP_PUSHNIL, 1)) /* `a~=nil' */
  493. i = CREATE_S(OP_JMPT, NO_JUMP);
  494. else {
  495. arg1 = NO_JUMP;
  496. mode = iS;
  497. }
  498. break;
  499. case OP_JMPEQ:
  500. delta = -2;
  501. if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
  502. i = CREATE_0(OP_NOT);
  503. delta = -1; /* just undo effect of previous PUSHNIL */
  504. }
  505. else {
  506. arg1 = NO_JUMP;
  507. mode = iS;
  508. }
  509. break;
  510. case OP_JMPT:
  511. case OP_JMPF:
  512. case OP_JMPONT:
  513. case OP_JMPONF:
  514. delta = -1;
  515. arg1 = NO_JUMP;
  516. switch (GET_OPCODE(i)) {
  517. case OP_NOT: i = CREATE_S(invertjump(o), NO_JUMP); break;
  518. default: mode = iS; break;
  519. }
  520. break;
  521. case OP_GETDOTTED:
  522. case OP_GETINDEXED:
  523. case OP_TAILCALL:
  524. case OP_ADDI:
  525. LUA_INTERNALERROR(L, "instruction used only for optimizations");
  526. return 0; /* to avoid warnings */
  527. }
  528. luaK_deltastack(fs, delta);
  529. switch (mode) { /* handle instruction formats */
  530. case iO: i = CREATE_0(o); break;
  531. case iU: i = CREATE_U(o, arg1); break;
  532. case iS: i = CREATE_S(o, arg1); break;
  533. case iAB: i = CREATE_AB(o, arg1, arg2); break;
  534. case iP: { /* optimize: put instruction in place of last one */
  535. fs->f->code[fs->pc-1] = i; /* change previous instruction */
  536. return fs->pc-1;
  537. }
  538. }
  539. /* actually create the new instruction */
  540. luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction, codeEM, MAX_INT);
  541. fs->f->code[fs->pc] = i;
  542. return fs->pc++;
  543. }