lvm.c 16 KB

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