lvm.c 16 KB

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