lvm.c 17 KB

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