2
0

lvm.c 16 KB

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