lvm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. ** $Id: lvm.c,v 1.70 1999/12/06 11:40:55 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, signal, 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. ** Execute the given opcode, until a RET. Parameters are between
  246. ** [stack+base,top). Returns n such that the the results are between
  247. ** [stack+n,top).
  248. */
  249. StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
  250. StkId base) {
  251. register StkId top; /* keep top local, for performance */
  252. register const Byte *pc = tf->code;
  253. const TObject *consts = tf->consts;
  254. if (L->callhook)
  255. luaD_callHook(L, base, tf, 0);
  256. luaD_checkstack(L, (*pc++)+EXTRA_STACK);
  257. if (*pc < ZEROVARARG)
  258. luaD_adjusttop(L, base, *(pc++));
  259. else { /* varargs */
  260. adjust_varargs(L, base, (*pc++)-ZEROVARARG);
  261. luaC_checkGC(L);
  262. }
  263. top = L->top;
  264. for (;;) {
  265. register int aux = 0;
  266. switchentry:
  267. switch ((OpCode)*pc++) {
  268. case ENDCODE:
  269. top = base;
  270. goto ret;
  271. case RETCODE:
  272. base += *pc++;
  273. goto ret;
  274. case CALL: aux = *pc++;
  275. L->top = top;
  276. luaD_call(L, base+(*pc++), aux);
  277. top = L->top;
  278. break;
  279. case TAILCALL: aux = *pc++;
  280. L->top = top;
  281. luaD_call(L, base+(*pc++), MULT_RET);
  282. top = L->top;
  283. base += aux;
  284. goto ret;
  285. case PUSHNIL: aux = *pc++;
  286. do {
  287. ttype(top++) = LUA_T_NIL;
  288. } while (aux--);
  289. break;
  290. case POP: aux = *pc++;
  291. top -= aux;
  292. break;
  293. case PUSHNUMBERW: aux += highbyte(L, *pc++);
  294. case PUSHNUMBER: aux += *pc++;
  295. ttype(top) = LUA_T_NUMBER;
  296. nvalue(top) = aux;
  297. top++;
  298. break;
  299. case PUSHNUMBERNEGW: aux += highbyte(L, *pc++);
  300. case PUSHNUMBERNEG: aux += *pc++;
  301. ttype(top) = LUA_T_NUMBER;
  302. nvalue(top) = -aux;
  303. top++;
  304. break;
  305. case PUSHCONSTANTW: aux += highbyte(L, *pc++);
  306. case PUSHCONSTANT: aux += *pc++;
  307. *top++ = consts[aux];
  308. break;
  309. case PUSHUPVALUE: aux = *pc++;
  310. *top++ = cl->consts[aux+1];
  311. break;
  312. case PUSHLOCAL: aux = *pc++;
  313. *top++ = *(base+aux);
  314. break;
  315. case GETGLOBALW: aux += highbyte(L, *pc++);
  316. case GETGLOBAL: aux += *pc++;
  317. L->top = top;
  318. luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
  319. top++;
  320. break;
  321. case GETTABLE:
  322. L->top = top;
  323. luaV_gettable(L);
  324. top--;
  325. break;
  326. case GETDOTTEDW: aux += highbyte(L, *pc++);
  327. case GETDOTTED: aux += *pc++;
  328. *top++ = consts[aux];
  329. L->top = top;
  330. luaV_gettable(L);
  331. top--;
  332. break;
  333. case PUSHSELFW: aux += highbyte(L, *pc++);
  334. case PUSHSELF: aux += *pc++; {
  335. TObject receiver;
  336. receiver = *(top-1);
  337. *top++ = consts[aux];
  338. L->top = top;
  339. luaV_gettable(L);
  340. *(top-1) = receiver;
  341. break;
  342. }
  343. case CREATEARRAYW: aux += highbyte(L, *pc++);
  344. case CREATEARRAY: aux += *pc++;
  345. L->top = top;
  346. luaC_checkGC(L);
  347. avalue(top) = luaH_new(L, aux);
  348. ttype(top) = LUA_T_ARRAY;
  349. top++;
  350. break;
  351. case SETLOCAL: aux = *pc++;
  352. *(base+aux) = *(--top);
  353. break;
  354. case SETGLOBALW: aux += highbyte(L, *pc++);
  355. case SETGLOBAL: aux += *pc++;
  356. L->top = top;
  357. luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
  358. top--;
  359. break;
  360. case SETTABLEPOP:
  361. L->top = top;
  362. luaV_settable(L, top-3);
  363. top -= 3; /* pop table, index, and value */
  364. break;
  365. case SETTABLE:
  366. L->top = top;
  367. luaV_settable(L, top-3-(*pc++));
  368. top--; /* pop value */
  369. break;
  370. case SETLISTW: aux += highbyte(L, *pc++);
  371. case SETLIST: aux += *pc++; {
  372. int n = *(pc++);
  373. Hash *arr = avalue(top-n-1);
  374. L->top = top-n; /* final value of `top' (in case of errors) */
  375. aux *= LFIELDS_PER_FLUSH;
  376. for (; n; n--)
  377. luaH_setint(L, arr, n+aux, --top);
  378. break;
  379. }
  380. case SETMAP: aux = *pc++; {
  381. StkId finaltop = top-2*(aux+1);
  382. Hash *arr = avalue(finaltop-1);
  383. L->top = finaltop; /* final value of `top' (in case of errors) */
  384. do {
  385. luaH_set(L, arr, top-2, top-1);
  386. top-=2;
  387. } while (aux--);
  388. break;
  389. }
  390. case NEQOP: aux = 1;
  391. case EQOP: {
  392. int res = luaO_equalObj(top-2, top-1);
  393. if (aux) res = !res;
  394. top--;
  395. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  396. nvalue(top-1) = 1;
  397. break;
  398. }
  399. case LTOP:
  400. luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  401. top--;
  402. break;
  403. case LEOP:
  404. luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  405. top--;
  406. break;
  407. case GTOP:
  408. luaV_comparison(L, top, LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  409. top--;
  410. break;
  411. case GEOP:
  412. luaV_comparison(L, top, LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  413. top--;
  414. break;
  415. case ADDOP:
  416. if (tonumber(top-1) || tonumber(top-2))
  417. call_arith(L, top, IM_ADD);
  418. else
  419. nvalue(top-2) += nvalue(top-1);
  420. top--;
  421. break;
  422. case SUBOP:
  423. if (tonumber(top-1) || tonumber(top-2))
  424. call_arith(L, top, IM_SUB);
  425. else
  426. nvalue(top-2) -= nvalue(top-1);
  427. top--;
  428. break;
  429. case MULTOP:
  430. if (tonumber(top-1) || tonumber(top-2))
  431. call_arith(L, top, IM_MUL);
  432. else
  433. nvalue(top-2) *= nvalue(top-1);
  434. top--;
  435. break;
  436. case DIVOP:
  437. if (tonumber(top-1) || tonumber(top-2))
  438. call_arith(L, top, IM_DIV);
  439. else
  440. nvalue(top-2) /= nvalue(top-1);
  441. top--;
  442. break;
  443. case POWOP:
  444. call_binTM(L, top, IM_POW, "undefined operation");
  445. top--;
  446. break;
  447. case CONCOP:
  448. if (tostring(L, top-2) || tostring(L, top-1))
  449. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  450. else
  451. tsvalue(top-2) = strconc(L, tsvalue(top-2), tsvalue(top-1));
  452. top--;
  453. L->top = top;
  454. luaC_checkGC(L);
  455. break;
  456. case MINUSOP:
  457. if (tonumber(top-1)) {
  458. ttype(top) = LUA_T_NIL;
  459. call_arith(L, top+1, IM_UNM);
  460. }
  461. else
  462. nvalue(top-1) = - nvalue(top-1);
  463. break;
  464. case NOTOP:
  465. ttype(top-1) =
  466. (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  467. nvalue(top-1) = 1;
  468. break;
  469. case ONTJMPW: aux += highbyte(L, *pc++);
  470. case ONTJMP: aux += *pc++;
  471. if (ttype(top-1) != LUA_T_NIL) pc += aux;
  472. else top--;
  473. break;
  474. case ONFJMPW: aux += highbyte(L, *pc++);
  475. case ONFJMP: aux += *pc++;
  476. if (ttype(top-1) == LUA_T_NIL) pc += aux;
  477. else top--;
  478. break;
  479. case JMPW: aux += highbyte(L, *pc++);
  480. case JMP: aux += *pc++;
  481. pc += aux;
  482. break;
  483. case IFFJMPW: aux += highbyte(L, *pc++);
  484. case IFFJMP: aux += *pc++;
  485. if (ttype(--top) == LUA_T_NIL) pc += aux;
  486. break;
  487. case IFTUPJMPW: aux += highbyte(L, *pc++);
  488. case IFTUPJMP: aux += *pc++;
  489. if (ttype(--top) != LUA_T_NIL) pc -= aux;
  490. break;
  491. case IFFUPJMPW: aux += highbyte(L, *pc++);
  492. case IFFUPJMP: aux += *pc++;
  493. if (ttype(--top) == LUA_T_NIL) pc -= aux;
  494. break;
  495. case CLOSUREW: aux += highbyte(L, *pc++);
  496. case CLOSURE: aux += *pc++;
  497. *top++ = consts[aux];
  498. L->top = top;
  499. aux = *pc++; /* number of upvalues */
  500. luaV_closure(L, aux);
  501. luaC_checkGC(L);
  502. top -= aux;
  503. break;
  504. case SETLINEW: aux += highbyte(L, *pc++);
  505. case SETLINE: aux += *pc++;
  506. L->top = top;
  507. if ((base-1)->ttype != LUA_T_LINE) {
  508. /* open space for LINE value */
  509. luaD_openstack(L, base);
  510. base->ttype = LUA_T_LINE;
  511. base++;
  512. top++;
  513. }
  514. (base-1)->value.i = aux;
  515. if (L->linehook)
  516. luaD_lineHook(L, aux);
  517. break;
  518. case LONGARGW: aux += highbyte(L, *pc++);
  519. case LONGARG: aux += *pc++;
  520. aux = highbyte(L, highbyte(L, aux));
  521. goto switchentry; /* do not reset "aux" */
  522. }
  523. } ret:
  524. L->top = top;
  525. if (L->callhook)
  526. luaD_callHook(L, 0, NULL, 1);
  527. return base;
  528. }