lvm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. ** $Id: lvm.c,v 1.90 2000/03/03 14:58:26 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. ** LUA_T_LINE(1), NAME(1), TM calls(3) (plus some extra...)
  28. */
  29. #define EXTRA_STACK 8
  30. static TaggedString *strconc (lua_State *L, const TaggedString *l,
  31. const TaggedString *r) {
  32. long nl = l->u.s.len;
  33. long nr = r->u.s.len;
  34. char *buffer = luaL_openspace(L, nl+nr);
  35. memcpy(buffer, l->str, nl);
  36. memcpy(buffer+nl, r->str, nr);
  37. return luaS_newlstr(L, buffer, nl+nr);
  38. }
  39. int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
  40. if (ttype(obj) != LUA_T_STRING)
  41. return 1;
  42. else {
  43. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  44. return 2;
  45. ttype(obj) = LUA_T_NUMBER;
  46. return 0;
  47. }
  48. }
  49. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  50. if (ttype(obj) != LUA_T_NUMBER)
  51. return 1;
  52. else {
  53. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  54. sprintf(s, "%.16g", (double)nvalue(obj));
  55. tsvalue(obj) = luaS_new(L, s);
  56. ttype(obj) = LUA_T_STRING;
  57. return 0;
  58. }
  59. }
  60. void luaV_setn (lua_State *L, Hash *t, int val) {
  61. TObject index, value;
  62. ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new(L, "n");
  63. ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
  64. luaH_set(L, t, &index, &value);
  65. }
  66. void luaV_closure (lua_State *L, int nelems) {
  67. if (nelems > 0) {
  68. Closure *c = luaF_newclosure(L, nelems);
  69. c->consts[0] = *(L->top-1);
  70. L->top -= nelems;
  71. while (nelems--)
  72. c->consts[nelems+1] = *(L->top-1+nelems);
  73. ttype(L->top-1) = (ttype(&c->consts[0]) == LUA_T_CPROTO) ?
  74. LUA_T_CCLOSURE : LUA_T_LCLOSURE;
  75. (L->top-1)->value.cl = c;
  76. }
  77. }
  78. /*
  79. ** Function to index a table.
  80. ** Receives the table at top-2 and the index at top-1.
  81. */
  82. void luaV_gettable (lua_State *L, StkId top) {
  83. TObject *table = top-2;
  84. const TObject *im;
  85. if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable TM */
  86. im = luaT_getimbyObj(L, table, IM_GETTABLE);
  87. if (ttype(im) == LUA_T_NIL) {
  88. L->top = top;
  89. luaG_indexerror(L, table);
  90. }
  91. }
  92. else { /* object is a table... */
  93. int tg = table->value.a->htag;
  94. im = luaT_getim(L, tg, IM_GETTABLE);
  95. if (ttype(im) == LUA_T_NIL) { /* and does not have a `gettable' TM */
  96. const TObject *h = luaH_get(L, avalue(table), table+1);
  97. if (ttype(h) == LUA_T_NIL &&
  98. (ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
  99. /* result is nil and there is an `index' tag method */
  100. L->top = top;
  101. luaD_callTM(L, im, 2, 1); /* calls it */
  102. }
  103. else
  104. *table = *h; /* `push' result into table position */
  105. return;
  106. }
  107. /* else it has a `gettable' TM, go through to next command */
  108. }
  109. /* object is not a table, or it has a `gettable' TM */
  110. L->top = top;
  111. luaD_callTM(L, im, 2, 1);
  112. }
  113. /*
  114. ** Receives table at *t, index at *(t+1) and value at `top'.
  115. */
  116. void luaV_settable (lua_State *L, StkId t, StkId top) {
  117. const TObject *im;
  118. if (ttype(t) != LUA_T_ARRAY) { /* not a table, get `settable' method */
  119. L->top = top;
  120. im = luaT_getimbyObj(L, t, IM_SETTABLE);
  121. if (ttype(im) == LUA_T_NIL)
  122. luaG_indexerror(L, t);
  123. }
  124. else { /* object is a table... */
  125. im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
  126. if (ttype(im) == LUA_T_NIL) { /* and does not have a `settable' method */
  127. luaH_set(L, avalue(t), t+1, top-1);
  128. return;
  129. }
  130. /* else it has a `settable' method, go through to next command */
  131. }
  132. /* object is not a table, or it has a `settable' method */
  133. /* prepare arguments and call the tag method */
  134. luaD_checkstack(L, 3);
  135. *(top+2) = *(top-1);
  136. *(top+1) = *(t+1);
  137. *(top) = *t;
  138. *(top-1) = *im;
  139. L->top = top+3;
  140. luaD_call(L, top-1, 0);
  141. }
  142. void luaV_rawsettable (lua_State *L, StkId t) {
  143. if (ttype(t) != LUA_T_ARRAY)
  144. lua_error(L, "indexed expression not a table");
  145. else {
  146. luaH_set(L, avalue(t), t+1, L->top-1);
  147. L->top -= 3;
  148. }
  149. }
  150. void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
  151. const TObject *value = &gv->value;
  152. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  153. if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
  154. *top = *value; /* default behavior */
  155. else { /* tag method */
  156. luaD_checkstack(L, 3);
  157. *top = *im;
  158. ttype(top+1) = LUA_T_STRING;
  159. tsvalue(top+1) = gv->name; /* global name */
  160. *(top+2) = *value;
  161. L->top = top+3;
  162. luaD_call(L, top, 1);
  163. }
  164. }
  165. void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
  166. const TObject *oldvalue = &gv->value;
  167. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  168. if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
  169. gv->value = *(top-1);
  170. else {
  171. luaD_checkstack(L, 3);
  172. *(top+2) = *(top-1); /* new value */
  173. *(top+1) = *oldvalue;
  174. ttype(top) = LUA_T_STRING;
  175. tsvalue(top) = gv->name;
  176. *(top-1) = *im;
  177. L->top = top+3;
  178. luaD_call(L, top-1, 0);
  179. }
  180. }
  181. static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
  182. /* try first operand */
  183. const TObject *im = luaT_getimbyObj(L, top-2, event);
  184. L->top = top;
  185. if (ttype(im) == LUA_T_NIL) {
  186. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  187. if (ttype(im) == LUA_T_NIL) {
  188. im = luaT_getim(L, 0, event); /* try a `global' method */
  189. if (ttype(im) == LUA_T_NIL)
  190. lua_error(L, msg);
  191. }
  192. }
  193. lua_pushstring(L, luaT_eventname[event]);
  194. luaD_callTM(L, im, 3, 1);
  195. }
  196. static void call_arith (lua_State *L, StkId top, IMS event) {
  197. call_binTM(L, top, event, "unexpected type in arithmetic operation");
  198. }
  199. static int luaV_strcomp (const TaggedString *ls, const TaggedString *rs) {
  200. const char *l = ls->str;
  201. long ll = ls->u.s.len;
  202. const char *r = rs->str;
  203. long lr = rs->u.s.len;
  204. for (;;) {
  205. long temp = strcoll(l, r);
  206. if (temp != 0) return temp;
  207. /* strings are equal up to a '\0' */
  208. temp = strlen(l); /* index of first '\0' in both strings */
  209. if (temp == ll) /* l is finished? */
  210. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  211. else if (temp == lr) /* r is finished? */
  212. return 1; /* l is greater than r (because l is not finished) */
  213. /* both strings longer than temp; go on comparing (after the '\0') */
  214. temp++;
  215. l += temp; ll -= temp; r += temp; lr -= temp;
  216. }
  217. }
  218. int luaV_lessthan (lua_State *L, TObject *l, TObject *r) {
  219. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  220. return (nvalue(l) < nvalue(r));
  221. else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
  222. return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  223. else {
  224. /* update top and put arguments in correct order to call TM */
  225. if (l<r) /* are arguments in correct order? */
  226. L->top = r+1; /* yes; 2nd is on top */
  227. else { /* no; exchange them */
  228. TObject temp = *r;
  229. *r = *l;
  230. *l = temp;
  231. L->top = l+1; /* 1st is on top */
  232. }
  233. call_binTM(L, L->top, IM_LT, "unexpected type in comparison");
  234. L->top--;
  235. return (ttype(L->top) != LUA_T_NIL);
  236. }
  237. }
  238. #define setbool(o,cond) if (cond) { \
  239. ttype(o) = LUA_T_NUMBER; nvalue(o) = 1.0; } \
  240. else ttype(o) = LUA_T_NIL
  241. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  242. int i;
  243. Hash *htab;
  244. htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  245. ttype(tab) = LUA_T_ARRAY;
  246. for (i=0; i<nvararg; i++)
  247. luaH_setint(L, htab, i+1, firstelem+i);
  248. luaV_setn(L, htab, nvararg); /* store counter in field `n' */
  249. }
  250. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  251. TObject arg;
  252. int nvararg = (L->top-base) - nfixargs;
  253. if (nvararg < 0) {
  254. luaV_pack(L, base, 0, &arg);
  255. luaD_adjusttop(L, base, nfixargs);
  256. }
  257. else {
  258. luaV_pack(L, base+nfixargs, nvararg, &arg);
  259. L->top = base+nfixargs;
  260. }
  261. *L->top++ = arg;
  262. }
  263. /*
  264. ** Executes the given Lua function. Parameters are between [base,top).
  265. ** Returns n such that the the results are between [n,top).
  266. */
  267. StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
  268. register StkId base) {
  269. register StkId top; /* keep top local, for performance */
  270. register const Instruction *pc = tf->code;
  271. TaggedString **kstr = tf->kstr;
  272. if (L->callhook)
  273. luaD_callHook(L, base-1, L->callhook, "call");
  274. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  275. if (tf->is_vararg) { /* varargs? */
  276. adjust_varargs(L, base, tf->numparams);
  277. luaC_checkGC(L);
  278. }
  279. else
  280. luaD_adjusttop(L, base, tf->numparams);
  281. top = L->top;
  282. for (;;) {
  283. register Instruction i = *pc++;
  284. switch (GET_OPCODE(i)) {
  285. case ENDCODE:
  286. return L->top; /* no results */
  287. case RETCODE:
  288. L->top = top;
  289. return base+GETARG_U(i);
  290. case CALL:
  291. L->top = top;
  292. luaD_call(L, base+GETARG_A(i), GETARG_B(i));
  293. top = L->top;
  294. break;
  295. case TAILCALL:
  296. L->top = top;
  297. luaD_call(L, base+GETARG_A(i), MULT_RET);
  298. return base+GETARG_B(i);
  299. case PUSHNIL: {
  300. int n = GETARG_U(i);
  301. do {
  302. ttype(top++) = LUA_T_NIL;
  303. } while (--n > 0);
  304. break;
  305. }
  306. case POP:
  307. top -= GETARG_U(i);
  308. break;
  309. case PUSHINT:
  310. ttype(top) = LUA_T_NUMBER;
  311. nvalue(top) = (real)GETARG_S(i);
  312. top++;
  313. break;
  314. case PUSHSTRING:
  315. ttype(top) = LUA_T_STRING;
  316. tsvalue(top) = kstr[GETARG_U(i)];
  317. top++;
  318. break;
  319. case PUSHNUM:
  320. ttype(top) = LUA_T_NUMBER;
  321. nvalue(top) = tf->knum[GETARG_U(i)];
  322. top++;
  323. break;
  324. case PUSHNEGNUM:
  325. ttype(top) = LUA_T_NUMBER;
  326. nvalue(top) = -tf->knum[GETARG_U(i)];
  327. top++;
  328. break;
  329. case PUSHUPVALUE:
  330. *top++ = cl->consts[GETARG_U(i)+1];
  331. break;
  332. case PUSHLOCAL:
  333. *top++ = *(base+GETARG_U(i));
  334. break;
  335. case GETGLOBAL:
  336. luaV_getglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  337. top++;
  338. break;
  339. case GETTABLE:
  340. luaV_gettable(L, top);
  341. top--;
  342. break;
  343. case GETDOTTED:
  344. ttype(top) = LUA_T_STRING;
  345. tsvalue(top++) = kstr[GETARG_U(i)];
  346. luaV_gettable(L, top);
  347. top--;
  348. break;
  349. case PUSHSELF: {
  350. TObject receiver;
  351. receiver = *(top-1);
  352. ttype(top) = LUA_T_STRING;
  353. tsvalue(top++) = kstr[GETARG_U(i)];
  354. luaV_gettable(L, top);
  355. *(top-1) = receiver;
  356. break;
  357. }
  358. case CREATETABLE:
  359. L->top = top;
  360. luaC_checkGC(L);
  361. avalue(top) = luaH_new(L, GETARG_U(i));
  362. ttype(top) = LUA_T_ARRAY;
  363. top++;
  364. break;
  365. case SETLOCAL:
  366. *(base+GETARG_U(i)) = *(--top);
  367. break;
  368. case SETGLOBAL:
  369. luaV_setglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  370. top--;
  371. break;
  372. case SETTABLEPOP:
  373. luaV_settable(L, top-3, top);
  374. top -= 3; /* pop table, index, and value */
  375. break;
  376. case SETTABLE:
  377. luaV_settable(L, top-3-GETARG_U(i), top);
  378. top--; /* pop value */
  379. break;
  380. case SETLIST: {
  381. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  382. int n = GETARG_B(i)+1;
  383. Hash *arr = avalue(top-n-1);
  384. L->top = top-n; /* final value of `top' (in case of errors) */
  385. for (; n; n--)
  386. luaH_setint(L, arr, n+aux, --top);
  387. break;
  388. }
  389. case SETMAP: {
  390. int n = GETARG_U(i);
  391. StkId finaltop = top-2*(n+1);
  392. Hash *arr = avalue(finaltop-1);
  393. L->top = finaltop; /* final value of `top' (in case of errors) */
  394. do {
  395. luaH_set(L, arr, top-2, top-1);
  396. top-=2;
  397. } while (n--);
  398. break;
  399. }
  400. case NEQOP:
  401. top--;
  402. setbool(top-1, !luaO_equalObj(top-1, top));
  403. break;
  404. case EQOP:
  405. top--;
  406. setbool(top-1, luaO_equalObj(top-1, top));
  407. break;
  408. case LTOP:
  409. top--;
  410. setbool(top-1, luaV_lessthan(L, top-1, top));
  411. break;
  412. case LEOP: /* a <= b === !(b<a) */
  413. top--;
  414. setbool(top-1, !luaV_lessthan(L, top, top-1));
  415. break;
  416. case GTOP: /* a > b === (b<a) */
  417. top--;
  418. setbool(top-1, luaV_lessthan(L, top, top-1));
  419. break;
  420. case GEOP: /* a >= b === !(a<b) */
  421. top--;
  422. setbool(top-1, !luaV_lessthan(L, top-1, top));
  423. break;
  424. case ADDOP:
  425. if (tonumber(top-1) || tonumber(top-2))
  426. call_arith(L, top, IM_ADD);
  427. else
  428. nvalue(top-2) += nvalue(top-1);
  429. top--;
  430. break;
  431. case ADDI:
  432. if (tonumber(top-1)) {
  433. ttype(top) = LUA_T_NUMBER;
  434. nvalue(top) = (real)GETARG_S(i);
  435. call_arith(L, top+1, IM_ADD);
  436. }
  437. else
  438. nvalue(top-1) += (real)GETARG_S(i);
  439. break;
  440. case SUBOP:
  441. if (tonumber(top-1) || tonumber(top-2))
  442. call_arith(L, top, IM_SUB);
  443. else
  444. nvalue(top-2) -= nvalue(top-1);
  445. top--;
  446. break;
  447. case MULTOP:
  448. if (tonumber(top-1) || tonumber(top-2))
  449. call_arith(L, top, IM_MUL);
  450. else
  451. nvalue(top-2) *= nvalue(top-1);
  452. top--;
  453. break;
  454. case DIVOP:
  455. if (tonumber(top-1) || tonumber(top-2))
  456. call_arith(L, top, IM_DIV);
  457. else
  458. nvalue(top-2) /= nvalue(top-1);
  459. top--;
  460. break;
  461. case POWOP:
  462. call_binTM(L, top, IM_POW, "undefined operation");
  463. top--;
  464. break;
  465. case CONCOP:
  466. if (tostring(L, top-2) || tostring(L, top-1))
  467. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  468. else
  469. tsvalue(top-2) = strconc(L, tsvalue(top-2), tsvalue(top-1));
  470. top--;
  471. L->top = top;
  472. luaC_checkGC(L);
  473. break;
  474. case MINUSOP:
  475. if (tonumber(top-1)) {
  476. ttype(top) = LUA_T_NIL;
  477. call_arith(L, top+1, IM_UNM);
  478. }
  479. else
  480. nvalue(top-1) = -nvalue(top-1);
  481. break;
  482. case NOTOP:
  483. ttype(top-1) =
  484. (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  485. nvalue(top-1) = 1;
  486. break;
  487. case ONTJMP:
  488. if (ttype(top-1) != LUA_T_NIL) pc += GETARG_S(i);
  489. else top--;
  490. break;
  491. case ONFJMP:
  492. if (ttype(top-1) == LUA_T_NIL) pc += GETARG_S(i);
  493. else top--;
  494. break;
  495. case JMP:
  496. pc += GETARG_S(i);
  497. break;
  498. case IFTJMP:
  499. if (ttype(--top) != LUA_T_NIL) pc += GETARG_S(i);
  500. break;
  501. case IFFJMP:
  502. if (ttype(--top) == LUA_T_NIL) pc += GETARG_S(i);
  503. break;
  504. case CLOSURE:
  505. ttype(top) = LUA_T_LPROTO;
  506. tfvalue(top) = tf->kproto[GETARG_A(i)];
  507. L->top = ++top;
  508. luaV_closure(L, GETARG_B(i));
  509. top -= GETARG_B(i);
  510. luaC_checkGC(L);
  511. break;
  512. case SETLINE:
  513. if ((base-1)->ttype != LUA_T_LINE) {
  514. /* open space for LINE value */
  515. int n = top-base;
  516. while (n--) base[n+1] = base[n];
  517. base++;
  518. top++;
  519. (base-1)->ttype = LUA_T_LINE;
  520. }
  521. (base-1)->value.i = GETARG_U(i);
  522. if (L->linehook) {
  523. L->top = top;
  524. luaD_lineHook(L, base-2, GETARG_U(i));
  525. }
  526. break;
  527. }
  528. }
  529. }