lvm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. ** $Id: lvm.c,v 1.69 1999/12/01 19:50:08 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. switch (ttype(value)) {
  145. /* only userdata, tables and nil can have getglobal tag methods */
  146. case LUA_T_USERDATA: case LUA_T_ARRAY: case LUA_T_NIL: {
  147. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  148. if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */
  149. ttype(L->top) = LUA_T_STRING;
  150. tsvalue(L->top) = gv->name; /* global name */
  151. L->top++;
  152. *L->top++ = *value;
  153. luaD_callTM(L, im, 2, 1);
  154. return;
  155. }
  156. /* else no tag method: go through to default behavior */
  157. }
  158. default: *L->top++ = *value; /* default behavior */
  159. }
  160. }
  161. void luaV_setglobal (lua_State *L, GlobalVar *gv) {
  162. const TObject *oldvalue = &gv->value;
  163. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  164. if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
  165. gv->value = *(--L->top);
  166. else {
  167. /* WARNING: caller must assure stack space */
  168. TObject newvalue;
  169. newvalue = *(L->top-1);
  170. ttype(L->top-1) = LUA_T_STRING;
  171. tsvalue(L->top-1) = gv->name;
  172. *L->top++ = *oldvalue;
  173. *L->top++ = newvalue;
  174. luaD_callTM(L, im, 3, 0);
  175. }
  176. }
  177. static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
  178. /* try first operand */
  179. const TObject *im = luaT_getimbyObj(L, top-2, event);
  180. L->top = top;
  181. if (ttype(im) == LUA_T_NIL) {
  182. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  183. if (ttype(im) == LUA_T_NIL) {
  184. im = luaT_getim(L, 0, event); /* try a 'global' i.m. */
  185. if (ttype(im) == LUA_T_NIL)
  186. lua_error(L, msg);
  187. }
  188. }
  189. lua_pushstring(L, luaT_eventname[event]);
  190. luaD_callTM(L, im, 3, 1);
  191. }
  192. static void call_arith (lua_State *L, StkId top, IMS event) {
  193. call_binTM(L, top, event, "unexpected type in arithmetic operation");
  194. }
  195. static int luaV_strcomp (const char *l, long ll, const char *r, long lr) {
  196. for (;;) {
  197. long temp = strcoll(l, r);
  198. if (temp != 0) return temp;
  199. /* strings are equal up to a '\0' */
  200. temp = strlen(l); /* index of first '\0' in both strings */
  201. if (temp == ll) /* l is finished? */
  202. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  203. else if (temp == lr) /* r is finished? */
  204. return 1; /* l is greater than r (because l is not finished) */
  205. /* both strings longer than temp; go on comparing (after the '\0') */
  206. temp++;
  207. l += temp; ll -= temp; r += temp; lr -= temp;
  208. }
  209. }
  210. void luaV_comparison (lua_State *L, StkId top, lua_Type ttype_less,
  211. lua_Type ttype_equal, lua_Type ttype_great, IMS op) {
  212. const TObject *l = top-2;
  213. const TObject *r = top-1;
  214. real result;
  215. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  216. result = nvalue(l)-nvalue(r);
  217. else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
  218. result = luaV_strcomp(svalue(l), tsvalue(l)->u.s.len,
  219. svalue(r), tsvalue(r)->u.s.len);
  220. else {
  221. call_binTM(L, top, op, "unexpected type in comparison");
  222. return;
  223. }
  224. nvalue(top-2) = 1;
  225. ttype(top-2) = (result < 0) ? ttype_less :
  226. (result == 0) ? ttype_equal : ttype_great;
  227. }
  228. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  229. int i;
  230. Hash *htab;
  231. htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  232. ttype(tab) = LUA_T_ARRAY;
  233. for (i=0; i<nvararg; i++)
  234. luaH_setint(L, htab, i+1, firstelem+i);
  235. luaV_setn(L, htab, nvararg); /* store counter in field `n' */
  236. }
  237. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  238. TObject arg;
  239. int nvararg = (L->top-base) - nfixargs;
  240. if (nvararg < 0) {
  241. luaV_pack(L, base, 0, &arg);
  242. luaD_adjusttop(L, base, nfixargs);
  243. }
  244. else {
  245. luaV_pack(L, base+nfixargs, nvararg, &arg);
  246. L->top = base+nfixargs;
  247. }
  248. *L->top++ = arg;
  249. }
  250. /*
  251. ** Execute the given opcode, until a RET. Parameters are between
  252. ** [stack+base,top). Returns n such that the the results are between
  253. ** [stack+n,top).
  254. */
  255. StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
  256. StkId base) {
  257. register StkId top; /* keep top local, for performance */
  258. register const Byte *pc = tf->code;
  259. const TObject *consts = tf->consts;
  260. if (L->callhook)
  261. luaD_callHook(L, base, tf, 0);
  262. luaD_checkstack(L, (*pc++)+EXTRA_STACK);
  263. if (*pc < ZEROVARARG)
  264. luaD_adjusttop(L, base, *(pc++));
  265. else { /* varargs */
  266. adjust_varargs(L, base, (*pc++)-ZEROVARARG);
  267. luaC_checkGC(L);
  268. }
  269. top = L->top;
  270. for (;;) {
  271. register int aux = 0;
  272. switchentry:
  273. switch ((OpCode)*pc++) {
  274. case ENDCODE:
  275. top = base;
  276. goto ret;
  277. case RETCODE:
  278. base += *pc++;
  279. goto ret;
  280. case CALL: aux = *pc++;
  281. L->top = top;
  282. luaD_call(L, base+(*pc++), aux);
  283. top = L->top;
  284. break;
  285. case TAILCALL: aux = *pc++;
  286. L->top = top;
  287. luaD_call(L, base+(*pc++), MULT_RET);
  288. top = L->top;
  289. base += aux;
  290. goto ret;
  291. case PUSHNIL: aux = *pc++;
  292. do {
  293. ttype(top++) = LUA_T_NIL;
  294. } while (aux--);
  295. break;
  296. case POP: aux = *pc++;
  297. top -= aux;
  298. break;
  299. case PUSHNUMBERW: aux += highbyte(L, *pc++);
  300. case PUSHNUMBER: aux += *pc++;
  301. ttype(top) = LUA_T_NUMBER;
  302. nvalue(top) = aux;
  303. top++;
  304. break;
  305. case PUSHNUMBERNEGW: aux += highbyte(L, *pc++);
  306. case PUSHNUMBERNEG: aux += *pc++;
  307. ttype(top) = LUA_T_NUMBER;
  308. nvalue(top) = -aux;
  309. top++;
  310. break;
  311. case PUSHCONSTANTW: aux += highbyte(L, *pc++);
  312. case PUSHCONSTANT: aux += *pc++;
  313. *top++ = consts[aux];
  314. break;
  315. case PUSHUPVALUE: aux = *pc++;
  316. *top++ = cl->consts[aux+1];
  317. break;
  318. case PUSHLOCAL: aux = *pc++;
  319. *top++ = *(base+aux);
  320. break;
  321. case GETGLOBALW: aux += highbyte(L, *pc++);
  322. case GETGLOBAL: aux += *pc++;
  323. L->top = top;
  324. luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv);
  325. top++;
  326. break;
  327. case GETTABLE:
  328. L->top = top;
  329. luaV_gettable(L);
  330. top--;
  331. break;
  332. case GETDOTTEDW: aux += highbyte(L, *pc++);
  333. case GETDOTTED: aux += *pc++;
  334. *top++ = consts[aux];
  335. L->top = top;
  336. luaV_gettable(L);
  337. top--;
  338. break;
  339. case PUSHSELFW: aux += highbyte(L, *pc++);
  340. case PUSHSELF: aux += *pc++; {
  341. TObject receiver;
  342. receiver = *(top-1);
  343. *top++ = consts[aux];
  344. L->top = top;
  345. luaV_gettable(L);
  346. *(top-1) = receiver;
  347. break;
  348. }
  349. case CREATEARRAYW: aux += highbyte(L, *pc++);
  350. case CREATEARRAY: aux += *pc++;
  351. L->top = top;
  352. luaC_checkGC(L);
  353. avalue(top) = luaH_new(L, aux);
  354. ttype(top) = LUA_T_ARRAY;
  355. top++;
  356. break;
  357. case SETLOCAL: aux = *pc++;
  358. *(base+aux) = *(--top);
  359. break;
  360. case SETGLOBALW: aux += highbyte(L, *pc++);
  361. case SETGLOBAL: aux += *pc++;
  362. L->top = top;
  363. luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv);
  364. top--;
  365. break;
  366. case SETTABLEPOP:
  367. L->top = top;
  368. luaV_settable(L, top-3);
  369. top -= 3; /* pop table, index, and value */
  370. break;
  371. case SETTABLE:
  372. L->top = top;
  373. luaV_settable(L, top-3-(*pc++));
  374. top--; /* pop value */
  375. break;
  376. case SETLISTW: aux += highbyte(L, *pc++);
  377. case SETLIST: aux += *pc++; {
  378. int n = *(pc++);
  379. Hash *arr = avalue(top-n-1);
  380. L->top = top-n; /* final value of `top' (in case of errors) */
  381. aux *= LFIELDS_PER_FLUSH;
  382. for (; n; n--)
  383. luaH_setint(L, arr, n+aux, --top);
  384. break;
  385. }
  386. case SETMAP: aux = *pc++; {
  387. StkId finaltop = top-2*(aux+1);
  388. Hash *arr = avalue(finaltop-1);
  389. L->top = finaltop; /* final value of `top' (in case of errors) */
  390. do {
  391. luaH_set(L, arr, top-2, top-1);
  392. top-=2;
  393. } while (aux--);
  394. break;
  395. }
  396. case NEQOP: aux = 1;
  397. case EQOP: {
  398. int res = luaO_equalObj(top-2, top-1);
  399. if (aux) res = !res;
  400. top--;
  401. ttype(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  402. nvalue(top-1) = 1;
  403. break;
  404. }
  405. case LTOP:
  406. luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  407. top--;
  408. break;
  409. case LEOP:
  410. luaV_comparison(L, top, LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  411. top--;
  412. break;
  413. case GTOP:
  414. luaV_comparison(L, top, LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  415. top--;
  416. break;
  417. case GEOP:
  418. luaV_comparison(L, top, LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  419. top--;
  420. break;
  421. case ADDOP:
  422. if (tonumber(top-1) || tonumber(top-2))
  423. call_arith(L, top, IM_ADD);
  424. else
  425. nvalue(top-2) += nvalue(top-1);
  426. top--;
  427. break;
  428. case SUBOP:
  429. if (tonumber(top-1) || tonumber(top-2))
  430. call_arith(L, top, IM_SUB);
  431. else
  432. nvalue(top-2) -= nvalue(top-1);
  433. top--;
  434. break;
  435. case MULTOP:
  436. if (tonumber(top-1) || tonumber(top-2))
  437. call_arith(L, top, IM_MUL);
  438. else
  439. nvalue(top-2) *= nvalue(top-1);
  440. top--;
  441. break;
  442. case DIVOP:
  443. if (tonumber(top-1) || tonumber(top-2))
  444. call_arith(L, top, IM_DIV);
  445. else
  446. nvalue(top-2) /= nvalue(top-1);
  447. top--;
  448. break;
  449. case POWOP:
  450. call_binTM(L, top, IM_POW, "undefined operation");
  451. top--;
  452. break;
  453. case CONCOP:
  454. if (tostring(L, top-2) || tostring(L, top-1))
  455. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  456. else
  457. tsvalue(top-2) = strconc(L, tsvalue(top-2), tsvalue(top-1));
  458. top--;
  459. L->top = top;
  460. luaC_checkGC(L);
  461. break;
  462. case MINUSOP:
  463. if (tonumber(top-1)) {
  464. ttype(top) = LUA_T_NIL;
  465. call_arith(L, top+1, IM_UNM);
  466. }
  467. else
  468. nvalue(top-1) = - nvalue(top-1);
  469. break;
  470. case NOTOP:
  471. ttype(top-1) =
  472. (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  473. nvalue(top-1) = 1;
  474. break;
  475. case ONTJMPW: aux += highbyte(L, *pc++);
  476. case ONTJMP: aux += *pc++;
  477. if (ttype(top-1) != LUA_T_NIL) pc += aux;
  478. else top--;
  479. break;
  480. case ONFJMPW: aux += highbyte(L, *pc++);
  481. case ONFJMP: aux += *pc++;
  482. if (ttype(top-1) == LUA_T_NIL) pc += aux;
  483. else top--;
  484. break;
  485. case JMPW: aux += highbyte(L, *pc++);
  486. case JMP: aux += *pc++;
  487. pc += aux;
  488. break;
  489. case IFFJMPW: aux += highbyte(L, *pc++);
  490. case IFFJMP: aux += *pc++;
  491. if (ttype(--top) == LUA_T_NIL) pc += aux;
  492. break;
  493. case IFTUPJMPW: aux += highbyte(L, *pc++);
  494. case IFTUPJMP: aux += *pc++;
  495. if (ttype(--top) != LUA_T_NIL) pc -= aux;
  496. break;
  497. case IFFUPJMPW: aux += highbyte(L, *pc++);
  498. case IFFUPJMP: aux += *pc++;
  499. if (ttype(--top) == LUA_T_NIL) pc -= aux;
  500. break;
  501. case CLOSUREW: aux += highbyte(L, *pc++);
  502. case CLOSURE: aux += *pc++;
  503. *top++ = consts[aux];
  504. L->top = top;
  505. aux = *pc++; /* number of upvalues */
  506. luaV_closure(L, aux);
  507. luaC_checkGC(L);
  508. top -= aux;
  509. break;
  510. case SETLINEW: aux += highbyte(L, *pc++);
  511. case SETLINE: aux += *pc++;
  512. L->top = top;
  513. if ((base-1)->ttype != LUA_T_LINE) {
  514. /* open space for LINE value */
  515. luaD_openstack(L, base);
  516. base->ttype = LUA_T_LINE;
  517. base++;
  518. top++;
  519. }
  520. (base-1)->value.i = aux;
  521. if (L->linehook)
  522. luaD_lineHook(L, aux);
  523. break;
  524. case LONGARGW: aux += highbyte(L, *pc++);
  525. case LONGARG: aux += *pc++;
  526. aux = highbyte(L, highbyte(L, aux));
  527. goto switchentry; /* do not reset "aux" */
  528. }
  529. } ret:
  530. L->top = top;
  531. if (L->callhook)
  532. luaD_callHook(L, 0, NULL, 1);
  533. return base;
  534. }