lvm.c 16 KB

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