lvm.c 16 KB

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