lcode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. ** $Id: lcode.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  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 "ldebug.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. #include "ltable.h"
  17. #define hasjumps(e) ((e)->t != (e)->f)
  18. #define getcode(fs,e) ((fs)->f->code[(e)->u.i.info])
  19. void luaK_error (LexState *ls, const char *msg) {
  20. luaX_error(ls, msg, ls->t.token);
  21. }
  22. /*
  23. ** Returns the the previous instruction, for optimizations.
  24. ** If there is a jump target between this and the current instruction,
  25. ** returns a dummy instruction to avoid wrong optimizations.
  26. */
  27. static Instruction previous_instruction (FuncState *fs) {
  28. if (fs->pc > fs->lasttarget) /* no jumps to current position? */
  29. return fs->f->code[fs->pc-1]; /* returns previous instruction */
  30. else
  31. return cast(Instruction, -1);/* invalid instruction avoids optimizations */
  32. }
  33. void luaK_nil (FuncState *fs, int from, int n) {
  34. Instruction previous = previous_instruction(fs);
  35. if (GET_OPCODE(previous) == OP_LOADNIL) {
  36. int pfrom = GETARG_A(previous);
  37. int pto = GETARG_B(previous);
  38. if (pfrom <= from && from <= pto+1) { /* can connect both? */
  39. if (from+n-1 > pto)
  40. SETARG_B(fs->f->code[fs->pc-1], from+n-1);
  41. return;
  42. }
  43. }
  44. luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
  45. }
  46. int luaK_jump (FuncState *fs) {
  47. int j = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  48. if (j == fs->lasttarget) { /* possible jumps to this jump? */
  49. luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
  50. fs->jlt = NO_JUMP;
  51. }
  52. return j;
  53. }
  54. static int luaK_condjump (FuncState *fs, OpCode op, int A, int B, int C) {
  55. luaK_codeABC(fs, op, A, B, C);
  56. return luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP);
  57. }
  58. static void luaK_fixjump (FuncState *fs, int pc, int dest) {
  59. Instruction *jmp = &fs->f->code[pc];
  60. if (dest == NO_JUMP)
  61. SETARG_sBc(*jmp, NO_JUMP); /* point to itself to represent end of list */
  62. else { /* jump is relative to position following jump instruction */
  63. int offset = dest-(pc+1);
  64. if (abs(offset) > MAXARG_sBc)
  65. luaK_error(fs->ls, "control structure too long");
  66. SETARG_sBc(*jmp, offset);
  67. }
  68. }
  69. /*
  70. ** prep-for instructions (OP_FORPREP & OP_TFORPREP) have a negated jump,
  71. ** as they simulate the real jump...
  72. */
  73. void luaK_fixfor (FuncState *fs, int pc, int dest) {
  74. Instruction *jmp = &fs->f->code[pc];
  75. int offset = dest-(pc+1);
  76. SETARG_sBc(*jmp, -offset);
  77. }
  78. /*
  79. ** returns current `pc' and marks it as a jump target (to avoid wrong
  80. ** optimizations with consecutive instructions not in the same basic block).
  81. ** discharge list of jumps to last target.
  82. */
  83. int luaK_getlabel (FuncState *fs) {
  84. if (fs->pc != fs->lasttarget) {
  85. int lasttarget = fs->lasttarget;
  86. fs->lasttarget = fs->pc;
  87. luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
  88. fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
  89. }
  90. return fs->pc;
  91. }
  92. static int luaK_getjump (FuncState *fs, int pc) {
  93. int offset = GETARG_sBc(fs->f->code[pc]);
  94. if (offset == NO_JUMP) /* point to itself represents end of list */
  95. return NO_JUMP; /* end of list */
  96. else
  97. return (pc+1)+offset; /* turn offset into absolute position */
  98. }
  99. static Instruction *getjumpcontrol (FuncState *fs, int pc) {
  100. Instruction *pi = &fs->f->code[pc];
  101. OpCode op = GET_OPCODE(*pi);
  102. lua_assert(op == OP_JMP || op == OP_FORLOOP || op == OP_TFORLOOP);
  103. if (pc >= 1 && testOpMode(GET_OPCODE(*(pi-1)), OpModeT))
  104. return pi-1;
  105. else
  106. return pi;
  107. }
  108. static int need_value (FuncState *fs, int list, OpCode op) {
  109. /* check whether list has any jump different from `op' */
  110. for (; list != NO_JUMP; list = luaK_getjump(fs, list))
  111. if (GET_OPCODE(*getjumpcontrol(fs, list)) != op) return 1;
  112. return 0; /* not found */
  113. }
  114. static void patchtestreg (Instruction *i, int reg) {
  115. if (reg == NO_REG) reg = GETARG_B(*i);
  116. SETARG_A(*i, reg);
  117. }
  118. static void luaK_patchlistaux (FuncState *fs, int list,
  119. int ttarget, int treg, int ftarget, int freg, int dtarget) {
  120. while (list != NO_JUMP) {
  121. int next = luaK_getjump(fs, list);
  122. Instruction *i = getjumpcontrol(fs, list);
  123. switch (GET_OPCODE(*i)) {
  124. case OP_TESTT: {
  125. patchtestreg(i, treg);
  126. luaK_fixjump(fs, list, ttarget);
  127. break;
  128. }
  129. case OP_TESTF: {
  130. patchtestreg(i, freg);
  131. luaK_fixjump(fs, list, ftarget);
  132. break;
  133. }
  134. default: {
  135. luaK_fixjump(fs, list, dtarget); /* jump to default target */
  136. break;
  137. }
  138. }
  139. list = next;
  140. }
  141. }
  142. void luaK_patchlist (FuncState *fs, int list, int target) {
  143. if (target == fs->lasttarget) /* same target that list `jlt'? */
  144. luaK_concat(fs, &fs->jlt, list); /* delay fixing */
  145. else
  146. luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target);
  147. }
  148. void luaK_concat (FuncState *fs, int *l1, int l2) {
  149. if (*l1 == NO_JUMP)
  150. *l1 = l2;
  151. else {
  152. int list = *l1;
  153. int next;
  154. while ((next = luaK_getjump(fs, list)) != NO_JUMP) /* find last element */
  155. list = next;
  156. luaK_fixjump(fs, list, l2);
  157. }
  158. }
  159. void luaK_reserveregs (FuncState *fs, int n) {
  160. fs->freereg += n;
  161. if (fs->freereg > fs->f->maxstacksize) {
  162. if (fs->freereg >= MAXSTACK)
  163. luaK_error(fs->ls, "function or expression too complex");
  164. fs->f->maxstacksize = cast(short, fs->freereg);
  165. }
  166. }
  167. static void freereg (FuncState *fs, int reg) {
  168. if (reg >= fs->nactloc && reg < MAXSTACK) {
  169. fs->freereg--;
  170. lua_assert(reg == fs->freereg);
  171. }
  172. }
  173. static void freeexp (FuncState *fs, expdesc *e) {
  174. if (e->k == VNONRELOC)
  175. freereg(fs, e->u.i.info);
  176. }
  177. static int addk (FuncState *fs, TObject *k, TObject *v) {
  178. const TObject *index = luaH_get(fs->h, k);
  179. if (ttype(index) == LUA_TNUMBER) {
  180. lua_assert(luaO_equalObj(&fs->f->k[cast(int, nvalue(index))], v));
  181. return cast(int, nvalue(index));
  182. }
  183. else { /* constant not found; create a new entry */
  184. TObject o;
  185. Proto *f = fs->f;
  186. luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
  187. MAXARG_Bc, "constant table overflow");
  188. setobj(&f->k[fs->nk], v);
  189. setnvalue(&o, fs->nk);
  190. luaH_set(fs->L, fs->h, k, &o);
  191. return fs->nk++;
  192. }
  193. }
  194. int luaK_stringk (FuncState *fs, TString *s) {
  195. TObject o;
  196. setsvalue(&o, s);
  197. return addk(fs, &o, &o);
  198. }
  199. static int number_constant (FuncState *fs, lua_Number r) {
  200. TObject o;
  201. setnvalue(&o, r);
  202. return addk(fs, &o, &o);
  203. }
  204. static int nil_constant (FuncState *fs) {
  205. TObject k, v;
  206. setnilvalue(&v);
  207. sethvalue(&k, fs->h); /* cannot use nil as key; instead use table itself */
  208. return addk(fs, &k, &v);
  209. }
  210. void luaK_setcallreturns (FuncState *fs, expdesc *e, int nresults) {
  211. if (e->k == VCALL) { /* expression is an open function call? */
  212. SETARG_C(getcode(fs, e), nresults+1);
  213. if (nresults == 1) { /* `regular' expression? */
  214. e->k = VNONRELOC;
  215. e->u.i.info = GETARG_A(getcode(fs, e));
  216. }
  217. }
  218. }
  219. void luaK_dischargevars (FuncState *fs, expdesc *e) {
  220. switch (e->k) {
  221. case VLOCAL: {
  222. e->k = VNONRELOC;
  223. break;
  224. }
  225. case VUPVAL: {
  226. e->u.i.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.i.info, 0);
  227. e->k = VRELOCABLE;
  228. break;
  229. }
  230. case VGLOBAL: {
  231. e->u.i.info = luaK_codeABc(fs, OP_GETGLOBAL, 0, e->u.i.info);
  232. e->k = VRELOCABLE;
  233. break;
  234. }
  235. case VINDEXED: {
  236. freereg(fs, e->u.i.aux);
  237. freereg(fs, e->u.i.info);
  238. e->u.i.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.i.info, e->u.i.aux);
  239. e->k = VRELOCABLE;
  240. break;
  241. }
  242. case VCALL: {
  243. luaK_setcallreturns(fs, e, 1);
  244. break;
  245. }
  246. default: break; /* there is one value available (somewhere) */
  247. }
  248. }
  249. static int code_label (FuncState *fs, int A, int b, int jump) {
  250. luaK_getlabel(fs); /* those instructions may be jump targets */
  251. return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
  252. }
  253. static void dischargejumps (FuncState *fs, expdesc *e, int reg) {
  254. if (e->k == VJMP || hasjumps(e)) {
  255. int final; /* position after whole expression */
  256. int p_f = NO_JUMP; /* position of an eventual PUSH false */
  257. int p_t = NO_JUMP; /* position of an eventual PUSH true */
  258. if (e->k == VJMP || need_value(fs, e->f, OP_TESTF) ||
  259. need_value(fs, e->t, OP_TESTT)) {
  260. /* expression needs values */
  261. if (e->k != VJMP) {
  262. luaK_getlabel(fs); /* these instruction may be jump target */
  263. luaK_codeAsBc(fs, OP_JMP, 0, 2); /* to jump over both pushes */
  264. }
  265. else { /* last expression is a conditional (test + jump) */
  266. fs->pc--; /* remove its jump */
  267. lua_assert(testOpMode(GET_OPCODE(fs->f->code[fs->pc - 1]), OpModeT));
  268. }
  269. p_t = code_label(fs, reg, 1, 1);
  270. p_f = code_label(fs, reg, 0, 0);
  271. }
  272. final = luaK_getlabel(fs);
  273. luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f);
  274. luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t);
  275. }
  276. e->f = e->t = NO_JUMP;
  277. }
  278. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
  279. luaK_dischargevars(fs, e);
  280. switch (e->k) {
  281. case VNIL: {
  282. luaK_nil(fs, reg, 1);
  283. break;
  284. }
  285. case VFALSE: case VTRUE: {
  286. luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
  287. break;
  288. }
  289. case VNUMBER: {
  290. lua_Number f = e->u.n;
  291. int i = cast(int, f);
  292. if ((lua_Number)i == f && -MAXARG_sBc <= i && i <= MAXARG_sBc)
  293. luaK_codeAsBc(fs, OP_LOADINT, reg, i); /* f has a small int value */
  294. else
  295. luaK_codeABc(fs, OP_LOADK, reg, number_constant(fs, f));
  296. break;
  297. }
  298. case VK: {
  299. luaK_codeABc(fs, OP_LOADK, reg, e->u.i.info);
  300. break;
  301. }
  302. case VRELOCABLE: {
  303. Instruction *pc = &getcode(fs, e);
  304. SETARG_A(*pc, reg);
  305. break;
  306. }
  307. default: return;
  308. }
  309. e->u.i.info = reg;
  310. e->k = VNONRELOC;
  311. }
  312. static void discharge2anyreg (FuncState *fs, expdesc *e) {
  313. if (e->k != VNONRELOC) {
  314. luaK_reserveregs(fs, 1);
  315. discharge2reg(fs, e, fs->freereg-1);
  316. }
  317. }
  318. static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) {
  319. discharge2reg(fs, e, reg);
  320. switch (e->k) {
  321. case VVOID: {
  322. return; /* nothing to do... */
  323. }
  324. case VNONRELOC: {
  325. if (reg != e->u.i.info)
  326. luaK_codeABC(fs, OP_MOVE, reg, e->u.i.info, 0);
  327. break;
  328. }
  329. case VJMP: {
  330. break;
  331. }
  332. default: {
  333. lua_assert(0); /* cannot happen */
  334. break;
  335. }
  336. }
  337. dischargejumps(fs, e, reg);
  338. e->u.i.info = reg;
  339. e->k = VNONRELOC;
  340. }
  341. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
  342. int reg;
  343. luaK_dischargevars(fs, e);
  344. freeexp(fs, e);
  345. reg = fs->freereg;
  346. luaK_reserveregs(fs, 1);
  347. luaK_exp2reg(fs, e, reg);
  348. }
  349. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
  350. luaK_dischargevars(fs, e);
  351. if (e->k == VNONRELOC) {
  352. if (!hasjumps(e)) return e->u.i.info; /* exp is already in a register */
  353. if (e->u.i.info >= fs->nactloc) { /* reg. is not a local? */
  354. dischargejumps(fs, e, e->u.i.info); /* put value on it */
  355. return e->u.i.info;
  356. }
  357. }
  358. luaK_exp2nextreg(fs, e); /* default */
  359. return e->u.i.info;
  360. }
  361. void luaK_exp2val (FuncState *fs, expdesc *e) {
  362. if (hasjumps(e))
  363. luaK_exp2anyreg(fs, e);
  364. else
  365. luaK_dischargevars(fs, e);
  366. }
  367. int luaK_exp2RK (FuncState *fs, expdesc *e) {
  368. luaK_exp2val(fs, e);
  369. switch (e->k) {
  370. case VNUMBER: case VNIL: {
  371. if (fs->nk + MAXSTACK <= MAXARG_C) { /* constant fit in argC? */
  372. e->u.i.info = (e->k == VNIL) ? nil_constant(fs) :
  373. number_constant(fs, e->u.n);
  374. e->k = VK;
  375. return e->u.i.info + MAXSTACK;
  376. }
  377. else break;
  378. }
  379. case VK: {
  380. if (e->u.i.info + MAXSTACK <= MAXARG_C) /* constant fit in argC? */
  381. return e->u.i.info + MAXSTACK;
  382. else break;
  383. }
  384. default: break;
  385. }
  386. /* not a constant in the right range: put in a register */
  387. return luaK_exp2anyreg(fs, e);
  388. }
  389. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *exp) {
  390. switch (var->k) {
  391. case VLOCAL: {
  392. freeexp(fs, exp);
  393. luaK_exp2reg(fs, exp, var->u.i.info);
  394. break;
  395. }
  396. case VUPVAL: {
  397. int e = luaK_exp2anyreg(fs, exp);
  398. freereg(fs, e);
  399. luaK_codeABC(fs, OP_SETUPVAL, e, var->u.i.info, 0);
  400. break;
  401. }
  402. case VGLOBAL: {
  403. int e = luaK_exp2anyreg(fs, exp);
  404. freereg(fs, e);
  405. luaK_codeABc(fs, OP_SETGLOBAL, e, var->u.i.info);
  406. break;
  407. }
  408. case VINDEXED: {
  409. int e = luaK_exp2anyreg(fs, exp);
  410. freereg(fs, e);
  411. luaK_codeABC(fs, OP_SETTABLE, e, var->u.i.info, var->u.i.aux);
  412. break;
  413. }
  414. default: {
  415. lua_assert(0); /* invalid var kind to store */
  416. break;
  417. }
  418. }
  419. }
  420. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
  421. int func;
  422. luaK_exp2anyreg(fs, e);
  423. freeexp(fs, e);
  424. func = fs->freereg;
  425. luaK_reserveregs(fs, 2);
  426. luaK_codeABC(fs, OP_SELF, func, e->u.i.info, luaK_exp2RK(fs, key));
  427. freeexp(fs, key);
  428. e->u.i.info = func;
  429. e->k = VNONRELOC;
  430. }
  431. static OpCode invertoperator (OpCode op) {
  432. switch (op) {
  433. case OP_TESTNE: return OP_TESTEQ;
  434. case OP_TESTEQ: return OP_TESTNE;
  435. case OP_TESTLT: return OP_TESTGE;
  436. case OP_TESTLE: return OP_TESTGT;
  437. case OP_TESTGT: return OP_TESTLE;
  438. case OP_TESTGE: return OP_TESTLT;
  439. case OP_TESTT: return OP_TESTF;
  440. case OP_TESTF: return OP_TESTT;
  441. default: lua_assert(0); return op; /* invalid jump instruction */
  442. }
  443. }
  444. static void invertjump (FuncState *fs, expdesc *e) {
  445. Instruction *pc = getjumpcontrol(fs, e->u.i.info);
  446. *pc = SET_OPCODE(*pc, invertoperator(GET_OPCODE(*pc)));
  447. }
  448. static int jumponcond (FuncState *fs, expdesc *e, OpCode op) {
  449. if (e->k == VRELOCABLE) {
  450. Instruction ie = getcode(fs, e);
  451. if (GET_OPCODE(ie) == OP_NOT) {
  452. op = invertoperator(op);
  453. fs->pc--; /* remove previous OP_NOT */
  454. return luaK_condjump(fs, op, NO_REG, GETARG_B(ie), 0);
  455. }
  456. /* else go through */
  457. }
  458. discharge2anyreg(fs, e);
  459. freeexp(fs, e);
  460. return luaK_condjump(fs, op, NO_REG, e->u.i.info, 0);
  461. }
  462. void luaK_goiftrue (FuncState *fs, expdesc *e) {
  463. int pc; /* pc of last jump */
  464. luaK_dischargevars(fs, e);
  465. switch (e->k) {
  466. case VK: case VNUMBER: case VTRUE: {
  467. pc = NO_JUMP; /* always true; do nothing */
  468. break;
  469. }
  470. case VFALSE: {
  471. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  472. break;
  473. }
  474. case VJMP: {
  475. invertjump(fs, e);
  476. pc = e->u.i.info;
  477. break;
  478. }
  479. default: {
  480. pc = jumponcond(fs, e, OP_TESTF);
  481. break;
  482. }
  483. }
  484. luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */
  485. luaK_patchlist(fs, e->t, luaK_getlabel(fs));
  486. e->t = NO_JUMP;
  487. }
  488. static void luaK_goiffalse (FuncState *fs, expdesc *e) {
  489. int pc; /* pc of last jump */
  490. luaK_dischargevars(fs, e);
  491. switch (e->k) {
  492. case VNIL: case VFALSE: {
  493. pc = NO_JUMP; /* always false; do nothing */
  494. break;
  495. }
  496. case VTRUE: {
  497. pc = luaK_codeAsBc(fs, OP_JMP, 0, NO_JUMP); /* always jump */
  498. break;
  499. }
  500. case VJMP: {
  501. pc = e->u.i.info;
  502. break;
  503. }
  504. default: {
  505. pc = jumponcond(fs, e, OP_TESTT);
  506. break;
  507. }
  508. }
  509. luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */
  510. luaK_patchlist(fs, e->f, luaK_getlabel(fs));
  511. e->f = NO_JUMP;
  512. }
  513. static void codenot (FuncState *fs, expdesc *e) {
  514. luaK_dischargevars(fs, e);
  515. switch (e->k) {
  516. case VNIL: case VFALSE: {
  517. e->k = VTRUE;
  518. break;
  519. }
  520. case VK: case VNUMBER: case VTRUE: {
  521. e->k = VFALSE;
  522. break;
  523. }
  524. case VJMP: {
  525. invertjump(fs, e);
  526. break;
  527. }
  528. case VRELOCABLE:
  529. case VNONRELOC: {
  530. discharge2anyreg(fs, e);
  531. freeexp(fs, e);
  532. e->u.i.info = luaK_codeABC(fs, OP_NOT, 0, e->u.i.info, 0);
  533. e->k = VRELOCABLE;
  534. break;
  535. }
  536. default: {
  537. lua_assert(0); /* cannot happen */
  538. break;
  539. }
  540. }
  541. /* interchange true and false lists */
  542. { int temp = e->f; e->f = e->t; e->t = temp; }
  543. }
  544. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
  545. t->u.i.aux = luaK_exp2RK(fs, k);
  546. t->k = VINDEXED;
  547. }
  548. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
  549. if (op == OPR_MINUS) {
  550. luaK_exp2val(fs, e);
  551. if (e->k == VNUMBER)
  552. e->u.n = -e->u.n;
  553. else {
  554. luaK_exp2anyreg(fs, e);
  555. freeexp(fs, e);
  556. e->u.i.info = luaK_codeABC(fs, OP_UNM, 0, e->u.i.info, 0);
  557. e->k = VRELOCABLE;
  558. }
  559. }
  560. else /* op == NOT */
  561. codenot(fs, e);
  562. }
  563. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
  564. switch (op) {
  565. case OPR_AND: {
  566. luaK_goiftrue(fs, v);
  567. break;
  568. }
  569. case OPR_OR: {
  570. luaK_goiffalse(fs, v);
  571. break;
  572. }
  573. case OPR_CONCAT: {
  574. luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
  575. break;
  576. }
  577. case OPR_SUB: case OPR_DIV: case OPR_POW: {
  578. /* non-comutative operators */
  579. luaK_exp2anyreg(fs, v); /* first operand must be a register */
  580. break;
  581. }
  582. default: {
  583. luaK_exp2RK(fs, v);
  584. break;
  585. }
  586. }
  587. }
  588. /* opcode for each binary operator */
  589. static const OpCode codes[] = { /* ORDER OPR */
  590. OP_ADD, OP_SUB, OP_MUL, OP_DIV,
  591. OP_POW, OP_CONCAT,
  592. OP_TESTNE, OP_TESTEQ,
  593. OP_TESTLT, OP_TESTLE, OP_TESTGT, OP_TESTGE
  594. };
  595. /* `inverted' opcode for each binary operator */
  596. /* ( -1 means operator has no inverse) */
  597. static const OpCode invcodes[] = { /* ORDER OPR */
  598. OP_ADD, (OpCode)-1, OP_MUL, (OpCode)-1,
  599. (OpCode)-1, (OpCode)-1,
  600. OP_TESTNE, OP_TESTEQ,
  601. OP_TESTGT, OP_TESTGE, OP_TESTLT, OP_TESTLE
  602. };
  603. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
  604. switch (op) {
  605. case OPR_AND: {
  606. lua_assert(e1->t == NO_JUMP); /* list must be closed */
  607. luaK_dischargevars(fs, e2);
  608. luaK_concat(fs, &e1->f, e2->f);
  609. e1->k = e2->k; e1->u = e2->u; e1->t = e2->t;
  610. break;
  611. }
  612. case OPR_OR: {
  613. lua_assert(e1->f == NO_JUMP); /* list must be closed */
  614. luaK_dischargevars(fs, e2);
  615. luaK_concat(fs, &e1->t, e2->t);
  616. e1->k = e2->k; e1->u = e2->u; e1->f = e2->f;
  617. break;
  618. }
  619. case OPR_CONCAT: {
  620. luaK_exp2val(fs, e2);
  621. if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
  622. lua_assert(e1->u.i.info == GETARG_B(getcode(fs, e2))-1);
  623. freeexp(fs, e1);
  624. SETARG_B(getcode(fs, e2), e1->u.i.info);
  625. e1->k = e2->k; e1->u.i.info = e2->u.i.info;
  626. }
  627. else {
  628. luaK_exp2nextreg(fs, e2);
  629. freeexp(fs, e2);
  630. freeexp(fs, e1);
  631. e1->u.i.info = luaK_codeABC(fs, codes[op], 0, e1->u.i.info,
  632. e2->u.i.info);
  633. e1->k = VRELOCABLE;
  634. }
  635. break;
  636. }
  637. default: {
  638. int o1, o2;
  639. OpCode opc;
  640. if (e1->k != VK) { /* not a constant operator? */
  641. o1 = e1->u.i.info;
  642. o2 = luaK_exp2RK(fs, e2); /* maybe other operator is constant... */
  643. opc = codes[op];
  644. }
  645. else { /* invert operands */
  646. o2 = luaK_exp2RK(fs, e1); /* constant must be 2nd operand */
  647. o1 = luaK_exp2anyreg(fs, e2); /* other operator must be in register */
  648. opc = invcodes[op]; /* use inverted operator */
  649. }
  650. freeexp(fs, e2);
  651. freeexp(fs, e1);
  652. if (op < OPR_NE) { /* ORDER OPR */
  653. e1->u.i.info = luaK_codeABC(fs, opc, 0, o1, o2);
  654. e1->k = VRELOCABLE;
  655. }
  656. else { /* jump */
  657. e1->u.i.info = luaK_condjump(fs, opc, o1, 0, o2);
  658. e1->k = VJMP;
  659. }
  660. }
  661. }
  662. }
  663. static void codelineinfo (FuncState *fs) {
  664. Proto *f = fs->f;
  665. LexState *ls = fs->ls;
  666. if (ls->lastline > fs->lastline) {
  667. if (ls->lastline > fs->lastline+1) {
  668. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  669. MAX_INT, "line info overflow");
  670. f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
  671. }
  672. luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
  673. MAX_INT, "line info overflow");
  674. f->lineinfo[fs->nlineinfo++] = fs->pc;
  675. fs->lastline = ls->lastline;
  676. }
  677. }
  678. static int luaK_code (FuncState *fs, Instruction i) {
  679. Proto *f;
  680. codelineinfo(fs);
  681. f = fs->f;
  682. /* put new instruction in code array */
  683. luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
  684. MAX_INT, "code size overflow");
  685. f->code[fs->pc] = i;
  686. /*printf("free: %d ", fs->freereg); printopcode(f, fs->pc);*/
  687. return fs->pc++;
  688. }
  689. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
  690. lua_assert(getOpMode(o) == iABC);
  691. return luaK_code(fs, CREATE_ABC(o, a, b, c));
  692. }
  693. int luaK_codeABc (FuncState *fs, OpCode o, int a, unsigned int bc) {
  694. lua_assert(getOpMode(o) == iABc || getOpMode(o) == iAsBc);
  695. return luaK_code(fs, CREATE_ABc(o, a, bc));
  696. }