lvm.c 16 KB

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