lvm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. ** $Id: lvm.c,v 1.98 2000/03/29 20:19:20 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define LUA_REENTRANT
  10. #include "lauxlib.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lobject.h"
  16. #include "lopcodes.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lvm.h"
  22. #ifdef OLD_ANSI
  23. #define strcoll(a,b) strcmp(a,b)
  24. #endif
  25. /*
  26. ** Extra stack size to run a function:
  27. ** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
  28. */
  29. #define EXTRA_STACK 8
  30. int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
  31. if (ttype(obj) != TAG_STRING)
  32. return 1;
  33. else {
  34. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  35. return 2;
  36. ttype(obj) = TAG_NUMBER;
  37. return 0;
  38. }
  39. }
  40. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  41. if (ttype(obj) != TAG_NUMBER)
  42. return 1;
  43. else {
  44. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  45. sprintf(s, "%.16g", (double)nvalue(obj));
  46. tsvalue(obj) = luaS_new(L, s);
  47. ttype(obj) = TAG_STRING;
  48. return 0;
  49. }
  50. }
  51. void luaV_setn (lua_State *L, Hash *t, int val) {
  52. TObject index, value;
  53. ttype(&index) = TAG_STRING; tsvalue(&index) = luaS_new(L, "n");
  54. ttype(&value) = TAG_NUMBER; nvalue(&value) = val;
  55. luaH_set(L, t, &index, &value);
  56. }
  57. static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
  58. Closure *c = luaF_newclosure(L, nelems);
  59. L->top -= nelems;
  60. while (nelems--)
  61. c->consts[nelems] = *(L->top+nelems);
  62. ttype(L->top) = t;
  63. clvalue(L->top) = c;
  64. incr_top;
  65. return c;
  66. }
  67. void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
  68. Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
  69. cl->f.c = c;
  70. }
  71. void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
  72. Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
  73. cl->f.l = l;
  74. }
  75. /*
  76. ** Function to index a table.
  77. ** Receives the table at top-2 and the index at top-1.
  78. */
  79. void luaV_gettable (lua_State *L, StkId top) {
  80. TObject *table = top-2;
  81. const TObject *im;
  82. if (ttype(table) != TAG_TABLE) { /* not a table, get gettable TM */
  83. im = luaT_getimbyObj(L, table, IM_GETTABLE);
  84. if (ttype(im) == TAG_NIL) {
  85. L->top = top;
  86. luaG_indexerror(L, table);
  87. }
  88. }
  89. else { /* object is a table... */
  90. int tg = table->value.a->htag;
  91. im = luaT_getim(L, tg, IM_GETTABLE);
  92. if (ttype(im) == TAG_NIL) { /* and does not have a `gettable' TM */
  93. const TObject *h = luaH_get(L, avalue(table), table+1);
  94. if (ttype(h) == TAG_NIL &&
  95. (ttype(im=luaT_getim(L, tg, IM_INDEX)) != TAG_NIL)) {
  96. /* result is nil and there is an `index' tag method */
  97. L->top = top;
  98. luaD_callTM(L, im, 2, 1); /* calls it */
  99. }
  100. else
  101. *table = *h; /* `push' result into table position */
  102. return;
  103. }
  104. /* else it has a `gettable' TM, go through to next command */
  105. }
  106. /* object is not a table, or it has a `gettable' TM */
  107. L->top = top;
  108. luaD_callTM(L, im, 2, 1);
  109. }
  110. /*
  111. ** Receives table at *t, index at *(t+1) and value at `top'.
  112. */
  113. void luaV_settable (lua_State *L, StkId t, StkId top) {
  114. const TObject *im;
  115. if (ttype(t) != TAG_TABLE) { /* not a table, get `settable' method */
  116. L->top = top;
  117. im = luaT_getimbyObj(L, t, IM_SETTABLE);
  118. if (ttype(im) == TAG_NIL)
  119. luaG_indexerror(L, t);
  120. }
  121. else { /* object is a table... */
  122. im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
  123. if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */
  124. luaH_set(L, avalue(t), t+1, top-1);
  125. return;
  126. }
  127. /* else it has a `settable' method, go through to next command */
  128. }
  129. /* object is not a table, or it has a `settable' method */
  130. /* prepare arguments and call the tag method */
  131. luaD_checkstack(L, 3);
  132. *(top+2) = *(top-1);
  133. *(top+1) = *(t+1);
  134. *(top) = *t;
  135. *(top-1) = *im;
  136. L->top = top+3;
  137. luaD_call(L, top-1, 0);
  138. }
  139. void luaV_rawsettable (lua_State *L, StkId t) {
  140. if (ttype(t) != TAG_TABLE)
  141. lua_error(L, "indexed expression not a table");
  142. else {
  143. luaH_set(L, avalue(t), t+1, L->top-1);
  144. L->top -= 3;
  145. }
  146. }
  147. void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
  148. const TObject *value = &gv->value;
  149. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  150. if (ttype(im) == TAG_NIL) /* is there a tag method? */
  151. *top = *value; /* default behavior */
  152. else { /* tag method */
  153. luaD_checkstack(L, 3);
  154. *top = *im;
  155. ttype(top+1) = TAG_STRING;
  156. tsvalue(top+1) = gv->name; /* global name */
  157. *(top+2) = *value;
  158. L->top = top+3;
  159. luaD_call(L, top, 1);
  160. }
  161. }
  162. void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
  163. const TObject *oldvalue = &gv->value;
  164. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  165. if (ttype(im) == TAG_NIL) /* is there a tag method? */
  166. gv->value = *(top-1);
  167. else {
  168. luaD_checkstack(L, 3);
  169. *(top+2) = *(top-1); /* new value */
  170. *(top+1) = *oldvalue;
  171. ttype(top) = TAG_STRING;
  172. tsvalue(top) = gv->name;
  173. *(top-1) = *im;
  174. L->top = top+3;
  175. luaD_call(L, top-1, 0);
  176. }
  177. }
  178. static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
  179. /* try first operand */
  180. const TObject *im = luaT_getimbyObj(L, top-2, event);
  181. L->top = top;
  182. if (ttype(im) == TAG_NIL) {
  183. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  184. if (ttype(im) == TAG_NIL) {
  185. im = luaT_getim(L, 0, event); /* try a `global' method */
  186. if (ttype(im) == TAG_NIL)
  187. lua_error(L, msg);
  188. }
  189. }
  190. lua_pushstring(L, luaT_eventname[event]);
  191. luaD_callTM(L, im, 3, 1);
  192. }
  193. static void call_arith (lua_State *L, StkId top, IMS event) {
  194. call_binTM(L, top, event, "unexpected type in arithmetic operation");
  195. }
  196. static int luaV_strcomp (const TString *ls, const TString *rs) {
  197. const char *l = ls->str;
  198. long ll = ls->u.s.len;
  199. const char *r = rs->str;
  200. long lr = rs->u.s.len;
  201. for (;;) {
  202. long temp = strcoll(l, r);
  203. if (temp != 0) return temp;
  204. /* strings are equal up to a '\0' */
  205. temp = strlen(l); /* index of first '\0' in both strings */
  206. if (temp == ll) /* l is finished? */
  207. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  208. else if (temp == lr) /* r is finished? */
  209. return 1; /* l is greater than r (because l is not finished) */
  210. /* both strings longer than temp; go on comparing (after the '\0') */
  211. temp++;
  212. l += temp; ll -= temp; r += temp; lr -= temp;
  213. }
  214. }
  215. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
  216. if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
  217. return (nvalue(l) < nvalue(r));
  218. else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
  219. return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  220. else { /* call TM */
  221. luaD_checkstack(L, 2);
  222. *top++ = *l;
  223. *top++ = *r;
  224. call_binTM(L, top, IM_LT, "unexpected type in comparison");
  225. L->top--;
  226. return (ttype(L->top) != TAG_NIL);
  227. }
  228. }
  229. static void strconc (lua_State *L, int total, StkId top) {
  230. do {
  231. int n = 2; /* number of elements handled in this pass (at least 2) */
  232. if (tostring(L, top-2) || tostring(L, top-1))
  233. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  234. else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
  235. /* at least two string values; get as many as possible */
  236. long tl = tsvalue(top-1)->u.s.len + tsvalue(top-2)->u.s.len;
  237. char *buffer;
  238. int i;
  239. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  240. tl += tsvalue(top-n-1)->u.s.len;
  241. n++;
  242. }
  243. buffer = luaL_openspace(L, tl);
  244. tl = 0;
  245. for (i=n; i>0; i--) { /* concat all strings */
  246. long l = tsvalue(top-i)->u.s.len;
  247. memcpy(buffer+tl, tsvalue(top-i)->str, l);
  248. tl += l;
  249. }
  250. tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
  251. }
  252. total -= n-1; /* got `n' strings to create 1 new */
  253. top -= n-1;
  254. } while (total > 1); /* repeat until only 1 result left */
  255. }
  256. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  257. int i;
  258. Hash *htab;
  259. htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  260. ttype(tab) = TAG_TABLE;
  261. for (i=0; i<nvararg; i++)
  262. luaH_setint(L, htab, i+1, firstelem+i);
  263. luaV_setn(L, htab, nvararg); /* store counter in field `n' */
  264. }
  265. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  266. TObject arg;
  267. int nvararg = (L->top-base) - nfixargs;
  268. if (nvararg < 0) {
  269. luaV_pack(L, base, 0, &arg);
  270. luaD_adjusttop(L, base, nfixargs);
  271. }
  272. else {
  273. luaV_pack(L, base+nfixargs, nvararg, &arg);
  274. L->top = base+nfixargs;
  275. }
  276. *L->top++ = arg;
  277. }
  278. /*
  279. ** Executes the given Lua function. Parameters are between [base,top).
  280. ** Returns n such that the the results are between [n,top).
  281. */
  282. StkId luaV_execute (lua_State *L, const Closure *cl, register StkId base) {
  283. const Proto *tf = cl->f.l;
  284. register StkId top; /* keep top local, for performance */
  285. register const Instruction *pc = tf->code;
  286. TString **kstr = tf->kstr;
  287. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  288. if (tf->is_vararg) { /* varargs? */
  289. adjust_varargs(L, base, tf->numparams);
  290. luaC_checkGC(L);
  291. }
  292. else
  293. luaD_adjusttop(L, base, tf->numparams);
  294. top = L->top;
  295. for (;;) {
  296. register Instruction i = *pc++;
  297. switch (GET_OPCODE(i)) {
  298. case OP_END:
  299. return L->top; /* no results */
  300. case OP_RETURN:
  301. L->top = top;
  302. return base+GETARG_U(i);
  303. case OP_CALL:
  304. L->top = top;
  305. luaD_call(L, base+GETARG_A(i), GETARG_B(i));
  306. top = L->top;
  307. break;
  308. case OP_TAILCALL:
  309. L->top = top;
  310. luaD_call(L, base+GETARG_A(i), MULT_RET);
  311. return base+GETARG_B(i);
  312. case OP_PUSHNIL: {
  313. int n = GETARG_U(i);
  314. LUA_ASSERT(L, n>0, "invalid argument");
  315. do {
  316. ttype(top++) = TAG_NIL;
  317. } while (--n > 0);
  318. break;
  319. }
  320. case OP_POP:
  321. top -= GETARG_U(i);
  322. break;
  323. case OP_PUSHINT:
  324. ttype(top) = TAG_NUMBER;
  325. nvalue(top) = (Number)GETARG_S(i);
  326. top++;
  327. break;
  328. case OP_PUSHSTRING:
  329. ttype(top) = TAG_STRING;
  330. tsvalue(top) = kstr[GETARG_U(i)];
  331. top++;
  332. break;
  333. case OP_PUSHNUM:
  334. ttype(top) = TAG_NUMBER;
  335. nvalue(top) = tf->knum[GETARG_U(i)];
  336. top++;
  337. break;
  338. case OP_PUSHNEGNUM:
  339. ttype(top) = TAG_NUMBER;
  340. nvalue(top) = -tf->knum[GETARG_U(i)];
  341. top++;
  342. break;
  343. case OP_PUSHUPVALUE:
  344. *top++ = cl->consts[GETARG_U(i)];
  345. break;
  346. case OP_PUSHLOCAL:
  347. *top++ = *(base+GETARG_U(i));
  348. break;
  349. case OP_GETGLOBAL:
  350. luaV_getglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  351. top++;
  352. break;
  353. case OP_GETTABLE:
  354. luaV_gettable(L, top);
  355. top--;
  356. break;
  357. case OP_GETDOTTED:
  358. ttype(top) = TAG_STRING;
  359. tsvalue(top++) = kstr[GETARG_U(i)];
  360. luaV_gettable(L, top);
  361. top--;
  362. break;
  363. case OP_PUSHSELF: {
  364. TObject receiver;
  365. receiver = *(top-1);
  366. ttype(top) = TAG_STRING;
  367. tsvalue(top++) = kstr[GETARG_U(i)];
  368. luaV_gettable(L, top);
  369. *(top-1) = receiver;
  370. break;
  371. }
  372. case OP_CREATETABLE:
  373. L->top = top;
  374. luaC_checkGC(L);
  375. avalue(top) = luaH_new(L, GETARG_U(i));
  376. ttype(top) = TAG_TABLE;
  377. top++;
  378. break;
  379. case OP_SETLOCAL:
  380. *(base+GETARG_U(i)) = *(--top);
  381. break;
  382. case OP_SETGLOBAL:
  383. luaV_setglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  384. top--;
  385. break;
  386. case OP_SETTABLEPOP:
  387. luaV_settable(L, top-3, top);
  388. top -= 3; /* pop table, index, and value */
  389. break;
  390. case OP_SETTABLE:
  391. luaV_settable(L, top-3-GETARG_U(i), top);
  392. top--; /* pop value */
  393. break;
  394. case OP_SETLIST: {
  395. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  396. int n = GETARG_B(i)+1;
  397. Hash *arr = avalue(top-n-1);
  398. L->top = top-n; /* final value of `top' (in case of errors) */
  399. for (; n; n--)
  400. luaH_setint(L, arr, n+aux, --top);
  401. break;
  402. }
  403. case OP_SETMAP: {
  404. int n = GETARG_U(i);
  405. StkId finaltop = top-2*(n+1);
  406. Hash *arr = avalue(finaltop-1);
  407. L->top = finaltop; /* final value of `top' (in case of errors) */
  408. do {
  409. luaH_set(L, arr, top-2, top-1);
  410. top-=2;
  411. } while (n--);
  412. break;
  413. }
  414. case OP_ADD:
  415. if (tonumber(top-1) || tonumber(top-2))
  416. call_arith(L, top, IM_ADD);
  417. else
  418. nvalue(top-2) += nvalue(top-1);
  419. top--;
  420. break;
  421. case OP_INCLOCAL: {
  422. TObject *var = base+GETARG_B(i);
  423. int n = GETARG_sA(i);
  424. if (tonumber(var)) {
  425. *top = *var; /* PUSHLOCAL */
  426. ttype(top+1) = TAG_NUMBER;
  427. nvalue(top+1) = (Number)n; /* PUSHINT */
  428. call_arith(L, top+2, IM_ADD);
  429. *var = *top; /* SETLOCAL */
  430. }
  431. else
  432. nvalue(var) += (Number)n;
  433. break;
  434. }
  435. case OP_ADDI:
  436. if (tonumber(top-1)) {
  437. ttype(top) = TAG_NUMBER;
  438. nvalue(top) = (Number)GETARG_S(i);
  439. call_arith(L, top+1, IM_ADD);
  440. }
  441. else
  442. nvalue(top-1) += (Number)GETARG_S(i);
  443. break;
  444. case OP_SUB:
  445. if (tonumber(top-1) || tonumber(top-2))
  446. call_arith(L, top, IM_SUB);
  447. else
  448. nvalue(top-2) -= nvalue(top-1);
  449. top--;
  450. break;
  451. case OP_MULT:
  452. if (tonumber(top-1) || tonumber(top-2))
  453. call_arith(L, top, IM_MUL);
  454. else
  455. nvalue(top-2) *= nvalue(top-1);
  456. top--;
  457. break;
  458. case OP_DIV:
  459. if (tonumber(top-1) || tonumber(top-2))
  460. call_arith(L, top, IM_DIV);
  461. else
  462. nvalue(top-2) /= nvalue(top-1);
  463. top--;
  464. break;
  465. case OP_POW:
  466. call_binTM(L, top, IM_POW, "undefined operation");
  467. top--;
  468. break;
  469. case OP_CONC: {
  470. int n = GETARG_U(i);
  471. strconc(L, n, top);
  472. top -= n-1;
  473. L->top = top;
  474. luaC_checkGC(L);
  475. break;
  476. }
  477. case OP_MINUS:
  478. if (tonumber(top-1)) {
  479. ttype(top) = TAG_NIL;
  480. call_arith(L, top+1, IM_UNM);
  481. }
  482. else
  483. nvalue(top-1) = -nvalue(top-1);
  484. break;
  485. case OP_NOT:
  486. ttype(top-1) =
  487. (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
  488. nvalue(top-1) = 1;
  489. break;
  490. case OP_JMPNEQ:
  491. top -= 2;
  492. if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  493. break;
  494. case OP_JMPEQ:
  495. top -= 2;
  496. if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  497. break;
  498. case OP_JMPLT:
  499. top -= 2;
  500. if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  501. break;
  502. case OP_JMPLE: /* a <= b === !(b<a) */
  503. top -= 2;
  504. if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  505. break;
  506. case OP_JMPGT: /* a > b === (b<a) */
  507. top -= 2;
  508. if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  509. break;
  510. case OP_JMPGE: /* a >= b === !(a<b) */
  511. top -= 2;
  512. if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  513. break;
  514. case OP_JMPT:
  515. if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
  516. break;
  517. case OP_JMPF:
  518. if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
  519. break;
  520. case OP_JMPONT:
  521. if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
  522. else top--;
  523. break;
  524. case OP_JMPONF:
  525. if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
  526. else top--;
  527. break;
  528. case OP_JMP:
  529. pc += GETARG_S(i);
  530. break;
  531. case OP_PUSHNILJMP:
  532. ttype(top++) = TAG_NIL;
  533. pc++;
  534. break;
  535. case OP_CLOSURE:
  536. L->top = top;
  537. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
  538. top = L->top;
  539. luaC_checkGC(L);
  540. break;
  541. case OP_SETLINE:
  542. if ((base-1)->ttype != TAG_LINE) {
  543. /* open space for LINE value */
  544. int n = top-base;
  545. while (n--) base[n+1] = base[n];
  546. base++;
  547. top++;
  548. (base-1)->ttype = TAG_LINE;
  549. }
  550. (base-1)->value.i = GETARG_U(i);
  551. if (L->linehook) {
  552. L->top = top;
  553. luaD_lineHook(L, base-2, GETARG_U(i));
  554. }
  555. break;
  556. }
  557. }
  558. }