opcode.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 2.12 1994/11/01 18:25:20 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <setjmp.h>
  10. #include <math.h>
  11. #ifdef __GNUC__
  12. #include <floatingpoint.h>
  13. #endif
  14. #include "opcode.h"
  15. #include "hash.h"
  16. #include "inout.h"
  17. #include "table.h"
  18. #include "lua.h"
  19. #define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
  20. #define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
  21. #define STACK_BUFFER (STACKGAP+128)
  22. static Long maxstack;
  23. static Object *stack=NULL;
  24. static Object *top;
  25. static int CBase; /* when Lua calls C or C calls Lua, points to the */
  26. /* first slot after the last parameter. */
  27. static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
  28. /* when C calls Lua, has the number of results. */
  29. static jmp_buf *errorJmp;
  30. static int lua_execute (Byte *pc, int base);
  31. /*
  32. ** Reports an error, and jumps up to the available recover label
  33. */
  34. void lua_error (char *s)
  35. {
  36. fprintf (stderr, "lua: %s\n", s);
  37. if (errorJmp)
  38. longjmp(*errorJmp, 1);
  39. else
  40. exit(1);
  41. }
  42. /*
  43. ** Init stack
  44. */
  45. static void lua_initstack (void)
  46. {
  47. maxstack = STACK_BUFFER;
  48. stack = (Object *)calloc(maxstack, sizeof(Object));
  49. if (stack == NULL)
  50. lua_error("stack - not enough memory");
  51. top = stack;
  52. }
  53. /*
  54. ** Check stack overflow and, if necessary, realloc vector
  55. */
  56. static void lua_checkstack (Word n)
  57. {
  58. if (stack == NULL)
  59. return lua_initstack();
  60. if (n > maxstack)
  61. {
  62. int t = top-stack;
  63. maxstack *= 2;
  64. stack = (Object *)realloc(stack, maxstack*sizeof(Object));
  65. if (stack == NULL)
  66. lua_error("stack - not enough memory");
  67. top = stack + t;
  68. }
  69. }
  70. /*
  71. ** Concatenate two given strings, creating a mark space at the beginning.
  72. ** Return the new string pointer.
  73. */
  74. static char *lua_strconc (char *l, char *r)
  75. {
  76. static char buffer[1024];
  77. int n = strlen(l)+strlen(r)+1;
  78. if (n > 1024)
  79. lua_error ("string too large");
  80. return strcat(strcpy(buffer,l),r);
  81. }
  82. static int ToReal (char* s, float* f)
  83. {
  84. char c;
  85. float t;
  86. if (sscanf(s,"%f %c",&t,&c) == 1) { *f=t; return 1; } else return 0;
  87. }
  88. /*
  89. ** Convert, if possible, to a number object.
  90. ** Return 0 if success, not 0 if error.
  91. */
  92. static int lua_tonumber (Object *obj)
  93. {
  94. if (tag(obj) != LUA_T_STRING)
  95. return 1;;
  96. if (!ToReal(svalue(obj), &nvalue(obj)))
  97. return 2;
  98. tag(obj) = LUA_T_NUMBER;
  99. return 0;
  100. }
  101. /*
  102. ** Test if it is possible to convert an object to a number object.
  103. ** If possible, return the converted object, otherwise return nil object.
  104. */
  105. static Object *lua_convtonumber (Object *obj)
  106. {
  107. static Object cvt;
  108. if (tag(obj) == LUA_T_NUMBER)
  109. {
  110. cvt = *obj;
  111. return &cvt;
  112. }
  113. if (tag(obj) == LUA_T_STRING && ToReal(svalue(obj), &nvalue(&cvt)))
  114. tag(&cvt) = LUA_T_NUMBER;
  115. else
  116. tag(&cvt) = LUA_T_NIL;
  117. return &cvt;
  118. }
  119. /*
  120. ** Convert, if possible, to a string tag
  121. ** Return 0 in success or not 0 on error.
  122. */
  123. static int lua_tostring (Object *obj)
  124. {
  125. static char s[256];
  126. if (tag(obj) != LUA_T_NUMBER)
  127. lua_reportbug ("unexpected type at conversion to string");
  128. if ((int) nvalue(obj) == nvalue(obj))
  129. sprintf (s, "%d", (int) nvalue(obj));
  130. else
  131. sprintf (s, "%g", nvalue(obj));
  132. svalue(obj) = lua_createstring(s);
  133. if (svalue(obj) == NULL)
  134. return 1;
  135. tag(obj) = LUA_T_STRING;
  136. return 0;
  137. }
  138. /*
  139. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  140. */
  141. static void adjust_top (Object *newtop)
  142. {
  143. while (top < newtop) tag(top++) = LUA_T_NIL;
  144. top = newtop; /* top could be bigger than newtop */
  145. }
  146. /*
  147. ** Call a C function. CBase will point to the top of the stack,
  148. ** and CnResults is the number of parameters. Returns an index
  149. ** to the first result from C.
  150. */
  151. static int callC (lua_CFunction func, int base)
  152. {
  153. int oldBase = CBase;
  154. int oldCnResults = CnResults;
  155. int firstResult;
  156. CnResults = (top-stack) - base;
  157. CBase = base+CnResults; /* incorporate parameters on the stack */
  158. (*func)();
  159. firstResult = CBase;
  160. CBase = oldBase;
  161. CnResults = oldCnResults;
  162. return firstResult;
  163. }
  164. /*
  165. ** Call a function (C or Lua). The parameters must be on the stack,
  166. ** between [stack+base,top). When returns, the results are on the stack,
  167. ** between [stack+whereRes,top). The number of results is nResults, unless
  168. ** nResults=MULT_RET.
  169. */
  170. static void do_call (Object *func, int base, int nResults, int whereRes)
  171. {
  172. int firstResult;
  173. if (tag(func) == LUA_T_CFUNCTION)
  174. firstResult = callC(fvalue(func), base);
  175. else if (tag(func) == LUA_T_FUNCTION)
  176. firstResult = lua_execute(bvalue(func), base);
  177. else
  178. lua_reportbug ("call expression not a function");
  179. /* adjust the number of results */
  180. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  181. adjust_top(stack+firstResult+nResults);
  182. /* move results to the given position */
  183. if (firstResult != whereRes)
  184. {
  185. int i = top - (stack+firstResult); /* number of results */
  186. top -= firstResult-whereRes;
  187. while (i--)
  188. *(stack+whereRes+i) = *(stack+firstResult+i);
  189. }
  190. }
  191. /*
  192. ** Function to index the values on the top
  193. */
  194. int lua_pushsubscript (void)
  195. {
  196. --top;
  197. if (tag(top-1) != LUA_T_ARRAY)
  198. {
  199. lua_reportbug ("indexed expression not a table");
  200. return 1;
  201. }
  202. {
  203. Object *h = lua_hashget (avalue(top-1), top);
  204. if (h == NULL) return 1;
  205. *(top-1) = *h;
  206. }
  207. return 0;
  208. }
  209. /*
  210. ** Function to store indexed based on values at the top
  211. */
  212. int lua_storesubscript (void)
  213. {
  214. if (tag(top-3) != LUA_T_ARRAY)
  215. {
  216. lua_reportbug ("indexed expression not a table");
  217. return 1;
  218. }
  219. {
  220. Object *h = lua_hashdefine (avalue(top-3), top-2);
  221. if (h == NULL) return 1;
  222. *h = *(top-1);
  223. }
  224. top -= 3;
  225. return 0;
  226. }
  227. /*
  228. ** Traverse all objects on stack
  229. */
  230. void lua_travstack (void (*fn)(Object *))
  231. {
  232. Object *o;
  233. for (o = top-1; o >= stack; o--)
  234. fn (o);
  235. }
  236. /*
  237. ** Executes a main procedure. Uses as Base the top of the stack, as it
  238. ** uses no parameters and left no results.
  239. */
  240. static void do_main (Byte *main)
  241. {
  242. if (main)
  243. {
  244. Object f;
  245. tag(&f) = LUA_T_FUNCTION; bvalue(&f) = main;
  246. do_call(&f, top-stack, 0, top-stack);
  247. free(main);
  248. }
  249. }
  250. /*
  251. ** Open file, generate opcode and execute global statement. Return 0 on
  252. ** success or 1 on error.
  253. */
  254. int lua_dofile (char *filename)
  255. {
  256. jmp_buf myErrorJmp;
  257. int status;
  258. jmp_buf *oldErr = errorJmp;
  259. errorJmp = &myErrorJmp;
  260. if (setjmp(myErrorJmp) == 0)
  261. {
  262. lua_openfile (filename);
  263. do_main(lua_parse());
  264. status = 0;
  265. }
  266. else
  267. status = 1;
  268. lua_closefile();
  269. errorJmp = oldErr;
  270. return status;
  271. }
  272. /*
  273. ** Generate opcode stored on string and execute global statement. Return 0 on
  274. ** success or 1 on error.
  275. */
  276. int lua_dostring (char *string)
  277. {
  278. jmp_buf myErrorJmp;
  279. int status;
  280. jmp_buf *oldErr = errorJmp;
  281. errorJmp = &myErrorJmp;
  282. if (setjmp(myErrorJmp) == 0)
  283. {
  284. lua_openstring(string);
  285. do_main(lua_parse());
  286. status = 0;
  287. }
  288. else
  289. status = 1;
  290. lua_closestring();
  291. errorJmp = oldErr;
  292. return status;
  293. }
  294. /*
  295. ** Execute the given lua function. Return 0 on success or 1 on error.
  296. */
  297. int lua_callfunction (Object *function)
  298. {
  299. jmp_buf myErrorJmp;
  300. int status;
  301. jmp_buf *oldErr = errorJmp;
  302. errorJmp = &myErrorJmp;
  303. if (setjmp(myErrorJmp) == 0)
  304. {
  305. do_call(function, CBase, MULT_RET, CBase);
  306. CnResults = (top-stack) - CBase; /* number of results */
  307. CBase += CnResults; /* incorporate results on the stack */
  308. status = 0;
  309. }
  310. else
  311. status = 1;
  312. errorJmp = oldErr;
  313. return status;
  314. }
  315. /*
  316. ** Get a parameter, returning the object handle or NULL on error.
  317. ** 'number' must be 1 to get the first parameter.
  318. */
  319. Object *lua_getparam (int number)
  320. {
  321. if (number <= 0 || number > CnResults) return NULL;
  322. return (stack+(CBase-CnResults+number-1));
  323. }
  324. /*
  325. ** Given an object handle, return its number value. On error, return 0.0.
  326. */
  327. real lua_getnumber (Object *object)
  328. {
  329. if (object == NULL || tag(object) == LUA_T_NIL) return 0.0;
  330. if (tonumber (object)) return 0.0;
  331. else return (nvalue(object));
  332. }
  333. /*
  334. ** Given an object handle, return its string pointer. On error, return NULL.
  335. */
  336. char *lua_getstring (Object *object)
  337. {
  338. if (object == NULL || tag(object) == LUA_T_NIL) return NULL;
  339. if (tostring (object)) return NULL;
  340. else return (svalue(object));
  341. }
  342. /*
  343. ** Given an object handle, return a copy of its string. On error, return NULL.
  344. */
  345. char *lua_copystring (Object *object)
  346. {
  347. if (object == NULL || tag(object) == LUA_T_NIL) return NULL;
  348. if (tostring (object)) return NULL;
  349. else return (strdup(svalue(object)));
  350. }
  351. /*
  352. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  353. */
  354. lua_CFunction lua_getcfunction (Object *object)
  355. {
  356. if (object == NULL) return NULL;
  357. if (tag(object) != LUA_T_CFUNCTION) return NULL;
  358. else return (fvalue(object));
  359. }
  360. /*
  361. ** Given an object handle, return its user data. On error, return NULL.
  362. */
  363. void *lua_getuserdata (Object *object)
  364. {
  365. if (object == NULL) return NULL;
  366. if (tag(object) != LUA_T_USERDATA) return NULL;
  367. else return (uvalue(object));
  368. }
  369. /*
  370. ** Given an object handle, return its table. On error, return NULL.
  371. */
  372. void *lua_gettable (Object *object)
  373. {
  374. if (object == NULL) return NULL;
  375. if (tag(object) != LUA_T_ARRAY) return NULL;
  376. else return (avalue(object));
  377. }
  378. /*
  379. ** Given an object handle and a field name, return its field object.
  380. ** On error, return NULL.
  381. */
  382. Object *lua_getfield (Object *object, char *field)
  383. {
  384. if (object == NULL) return NULL;
  385. if (tag(object) != LUA_T_ARRAY)
  386. return NULL;
  387. else
  388. {
  389. Object ref;
  390. tag(&ref) = LUA_T_STRING;
  391. svalue(&ref) = lua_constant[lua_findconstant(field)];
  392. return (lua_hashget(avalue(object), &ref));
  393. }
  394. }
  395. /*
  396. ** Given an object handle and an index, return its indexed object.
  397. ** On error, return NULL.
  398. */
  399. Object *lua_getindexed (Object *object, float index)
  400. {
  401. if (object == NULL) return NULL;
  402. if (tag(object) != LUA_T_ARRAY)
  403. return NULL;
  404. else
  405. {
  406. Object ref;
  407. tag(&ref) = LUA_T_NUMBER;
  408. nvalue(&ref) = index;
  409. return (lua_hashget(avalue(object), &ref));
  410. }
  411. }
  412. /*
  413. ** Get a global object. Return the object handle or NULL on error.
  414. */
  415. Object *lua_getglobal (char *name)
  416. {
  417. int n = lua_findsymbol(name);
  418. if (n < 0) return NULL;
  419. return &s_object(n);
  420. }
  421. /*
  422. ** Push a nil object
  423. */
  424. int lua_pushnil (void)
  425. {
  426. lua_checkstack(top-stack+1);
  427. tag(top++) = LUA_T_NIL;
  428. return 0;
  429. }
  430. /*
  431. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  432. */
  433. int lua_pushnumber (real n)
  434. {
  435. lua_checkstack(top-stack+1);
  436. tag(top) = LUA_T_NUMBER; nvalue(top++) = n;
  437. return 0;
  438. }
  439. /*
  440. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  441. */
  442. int lua_pushstring (char *s)
  443. {
  444. lua_checkstack(top-stack+1);
  445. tag(top) = LUA_T_STRING;
  446. svalue(top++) = lua_createstring(s);
  447. return 0;
  448. }
  449. /*
  450. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  451. */
  452. int lua_pushcfunction (lua_CFunction fn)
  453. {
  454. lua_checkstack(top-stack+1);
  455. tag(top) = LUA_T_CFUNCTION; fvalue(top++) = fn;
  456. return 0;
  457. }
  458. /*
  459. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  460. */
  461. int lua_pushuserdata (void *u)
  462. {
  463. lua_checkstack(top-stack+1);
  464. tag(top) = LUA_T_USERDATA; uvalue(top++) = u;
  465. return 0;
  466. }
  467. /*
  468. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  469. */
  470. int lua_pushtable (void *t)
  471. {
  472. lua_checkstack(top-stack+1);
  473. tag(top) = LUA_T_ARRAY; avalue(top++) = t;
  474. return 0;
  475. }
  476. /*
  477. ** Push an object to stack.
  478. */
  479. int lua_pushobject (Object *o)
  480. {
  481. lua_checkstack(top-stack+1);
  482. *top++ = *o;
  483. return 0;
  484. }
  485. /*
  486. ** Store top of the stack at a global variable array field.
  487. ** Return 1 on error, 0 on success.
  488. */
  489. int lua_storeglobal (char *name)
  490. {
  491. int n = lua_findsymbol (name);
  492. if (n < 0) return 1;
  493. if (tag(top-1) == LUA_T_MARK) return 1;
  494. s_object(n) = *(--top);
  495. return 0;
  496. }
  497. /*
  498. ** Store top of the stack at an array field. Return 1 on error, 0 on success.
  499. */
  500. int lua_storefield (lua_Object object, char *field)
  501. {
  502. if (tag(object) != LUA_T_ARRAY)
  503. return 1;
  504. else
  505. {
  506. Object ref, *h;
  507. tag(&ref) = LUA_T_STRING;
  508. svalue(&ref) = lua_createstring(field);
  509. h = lua_hashdefine(avalue(object), &ref);
  510. if (h == NULL) return 1;
  511. if (tag(top-1) == LUA_T_MARK) return 1;
  512. *h = *(--top);
  513. }
  514. return 0;
  515. }
  516. /*
  517. ** Store top of the stack at an array index. Return 1 on error, 0 on success.
  518. */
  519. int lua_storeindexed (lua_Object object, float index)
  520. {
  521. if (tag(object) != LUA_T_ARRAY)
  522. return 1;
  523. else
  524. {
  525. Object ref, *h;
  526. tag(&ref) = LUA_T_NUMBER;
  527. nvalue(&ref) = index;
  528. h = lua_hashdefine(avalue(object), &ref);
  529. if (h == NULL) return 1;
  530. if (tag(top-1) == LUA_T_MARK) return 1;
  531. *h = *(--top);
  532. }
  533. return 0;
  534. }
  535. /*
  536. ** Given an object handle, return if it is nil.
  537. */
  538. int lua_isnil (Object *object)
  539. {
  540. return (object != NULL && tag(object) == LUA_T_NIL);
  541. }
  542. /*
  543. ** Given an object handle, return if it is a number one.
  544. */
  545. int lua_isnumber (Object *object)
  546. {
  547. return (object != NULL && tag(object) == LUA_T_NUMBER);
  548. }
  549. /*
  550. ** Given an object handle, return if it is a string one.
  551. */
  552. int lua_isstring (Object *object)
  553. {
  554. return (object != NULL && tag(object) == LUA_T_STRING);
  555. }
  556. /*
  557. ** Given an object handle, return if it is an array one.
  558. */
  559. int lua_istable (Object *object)
  560. {
  561. return (object != NULL && tag(object) == LUA_T_ARRAY);
  562. }
  563. /*
  564. ** Given an object handle, return if it is a lua function.
  565. */
  566. int lua_isfunction (Object *object)
  567. {
  568. return (object != NULL && tag(object) == LUA_T_FUNCTION);
  569. }
  570. /*
  571. ** Given an object handle, return if it is a cfunction one.
  572. */
  573. int lua_iscfunction (Object *object)
  574. {
  575. return (object != NULL && tag(object) == LUA_T_CFUNCTION);
  576. }
  577. /*
  578. ** Given an object handle, return if it is an user data one.
  579. */
  580. int lua_isuserdata (Object *object)
  581. {
  582. return (object != NULL && tag(object) == LUA_T_USERDATA);
  583. }
  584. /*
  585. ** Internal function: convert an object to a number
  586. */
  587. void lua_obj2number (void)
  588. {
  589. Object *o = lua_getparam(1);
  590. lua_pushobject (lua_convtonumber(o));
  591. }
  592. /*
  593. ** Execute the given opcode, until a RET. Parameters are between
  594. ** [stack+base,top). Returns n such that the the results are between
  595. ** [stack+n,top).
  596. */
  597. static int lua_execute (Byte *pc, int base)
  598. {
  599. lua_debugline = 0; /* reset debug flag */
  600. if (stack == NULL)
  601. lua_initstack();
  602. while (1)
  603. {
  604. OpCode opcode;
  605. switch (opcode = (OpCode)*pc++)
  606. {
  607. case PUSHNIL: tag(top++) = LUA_T_NIL; break;
  608. case PUSH0: tag(top) = LUA_T_NUMBER; nvalue(top++) = 0; break;
  609. case PUSH1: tag(top) = LUA_T_NUMBER; nvalue(top++) = 1; break;
  610. case PUSH2: tag(top) = LUA_T_NUMBER; nvalue(top++) = 2; break;
  611. case PUSHBYTE: tag(top) = LUA_T_NUMBER; nvalue(top++) = *pc++; break;
  612. case PUSHWORD:
  613. {
  614. CodeWord code;
  615. get_word(code,pc);
  616. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.w;
  617. }
  618. break;
  619. case PUSHFLOAT:
  620. {
  621. CodeFloat code;
  622. get_float(code,pc);
  623. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.f;
  624. }
  625. break;
  626. case PUSHSTRING:
  627. {
  628. CodeWord code;
  629. get_word(code,pc);
  630. tag(top) = LUA_T_STRING; svalue(top++) = lua_constant[code.w];
  631. }
  632. break;
  633. case PUSHFUNCTION:
  634. {
  635. CodeCode code;
  636. get_code(code,pc);
  637. tag(top) = LUA_T_FUNCTION; bvalue(top++) = code.b;
  638. }
  639. break;
  640. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  641. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  642. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  643. case PUSHLOCAL9: *top++ = *((stack+base) + (int)(opcode-PUSHLOCAL0)); break;
  644. case PUSHLOCAL: *top++ = *((stack+base) + (*pc++)); break;
  645. case PUSHGLOBAL:
  646. {
  647. CodeWord code;
  648. get_word(code,pc);
  649. *top++ = s_object(code.w);
  650. }
  651. break;
  652. case PUSHINDEXED:
  653. {
  654. int s = lua_pushsubscript();
  655. if (s == 1) return 1;
  656. }
  657. break;
  658. case PUSHSELF:
  659. {
  660. Object receiver = *(top-2);
  661. if (lua_pushsubscript() == 1) return 1;
  662. *(top++) = receiver;
  663. break;
  664. }
  665. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  666. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  667. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  668. case STORELOCAL9:
  669. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  670. break;
  671. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  672. case STOREGLOBAL:
  673. {
  674. CodeWord code;
  675. get_word(code,pc);
  676. s_object(code.w) = *(--top);
  677. }
  678. break;
  679. case STOREINDEXED0:
  680. {
  681. int s = lua_storesubscript();
  682. if (s == 1) return 1;
  683. }
  684. break;
  685. case STOREINDEXED:
  686. {
  687. int n = *pc++;
  688. if (tag(top-3-n) != LUA_T_ARRAY)
  689. {
  690. lua_reportbug ("indexed expression not a table");
  691. return 1;
  692. }
  693. {
  694. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  695. if (h == NULL) return 1;
  696. *h = *(top-1);
  697. }
  698. top--;
  699. }
  700. break;
  701. case STORELIST0:
  702. case STORELIST:
  703. {
  704. int m, n;
  705. Object *arr;
  706. if (opcode == STORELIST0) m = 0;
  707. else m = *(pc++) * FIELDS_PER_FLUSH;
  708. n = *(pc++);
  709. arr = top-n-1;
  710. if (tag(arr) != LUA_T_ARRAY)
  711. {
  712. lua_reportbug ("internal error - table expected");
  713. return 1;
  714. }
  715. while (n)
  716. {
  717. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  718. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  719. top--;
  720. n--;
  721. }
  722. }
  723. break;
  724. case STORERECORD:
  725. {
  726. int n = *(pc++);
  727. Object *arr = top-n-1;
  728. if (tag(arr) != LUA_T_ARRAY)
  729. {
  730. lua_reportbug ("internal error - table expected");
  731. return 1;
  732. }
  733. while (n)
  734. {
  735. CodeWord code;
  736. get_word(code,pc);
  737. tag(top) = LUA_T_STRING; svalue(top) = lua_constant[code.w];
  738. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  739. top--;
  740. n--;
  741. }
  742. }
  743. break;
  744. case ADJUST0:
  745. adjust_top((stack+base));
  746. break;
  747. case ADJUST:
  748. adjust_top((stack+base) + *(pc++));
  749. break;
  750. case CREATEARRAY:
  751. {
  752. CodeWord size;
  753. get_word(size,pc);
  754. top++;
  755. avalue(top-1) = lua_createarray(size.w);
  756. if (avalue(top-1) == NULL)
  757. return 1;
  758. tag(top-1) = LUA_T_ARRAY;
  759. }
  760. break;
  761. case EQOP:
  762. {
  763. int res;
  764. Object *l = top-2;
  765. Object *r = top-1;
  766. --top;
  767. if (tag(l) != tag(r))
  768. res = 0;
  769. else
  770. {
  771. switch (tag(l))
  772. {
  773. case LUA_T_NIL:
  774. res = 0; break;
  775. case LUA_T_NUMBER:
  776. res = (nvalue(l) == nvalue(r)); break;
  777. case LUA_T_ARRAY:
  778. res = (avalue(l) == avalue(r)); break;
  779. case LUA_T_FUNCTION:
  780. res = (bvalue(l) == bvalue(r)); break;
  781. case LUA_T_CFUNCTION:
  782. res = (fvalue(l) == fvalue(r)); break;
  783. case LUA_T_STRING:
  784. res = (strcmp (svalue(l), svalue(r)) == 0); break;
  785. default:
  786. res = (uvalue(l) == uvalue(r)); break;
  787. }
  788. }
  789. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  790. nvalue(top-1) = 1;
  791. }
  792. break;
  793. case LTOP:
  794. {
  795. Object *l = top-2;
  796. Object *r = top-1;
  797. --top;
  798. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  799. tag(top-1) = (nvalue(l) < nvalue(r)) ? LUA_T_NUMBER : LUA_T_NIL;
  800. else
  801. {
  802. if (tostring(l) || tostring(r))
  803. return 1;
  804. tag(top-1) = (strcmp (svalue(l), svalue(r)) < 0) ? LUA_T_NUMBER : LUA_T_NIL;
  805. }
  806. nvalue(top-1) = 1;
  807. }
  808. break;
  809. case LEOP:
  810. {
  811. Object *l = top-2;
  812. Object *r = top-1;
  813. --top;
  814. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  815. tag(top-1) = (nvalue(l) <= nvalue(r)) ? LUA_T_NUMBER : LUA_T_NIL;
  816. else
  817. {
  818. if (tostring(l) || tostring(r))
  819. return 1;
  820. tag(top-1) = (strcmp (svalue(l), svalue(r)) <= 0) ? LUA_T_NUMBER : LUA_T_NIL;
  821. }
  822. nvalue(top-1) = 1;
  823. }
  824. break;
  825. case ADDOP:
  826. {
  827. Object *l = top-2;
  828. Object *r = top-1;
  829. if (tonumber(r) || tonumber(l))
  830. return 1;
  831. nvalue(l) += nvalue(r);
  832. --top;
  833. }
  834. break;
  835. case SUBOP:
  836. {
  837. Object *l = top-2;
  838. Object *r = top-1;
  839. if (tonumber(r) || tonumber(l))
  840. return 1;
  841. nvalue(l) -= nvalue(r);
  842. --top;
  843. }
  844. break;
  845. case MULTOP:
  846. {
  847. Object *l = top-2;
  848. Object *r = top-1;
  849. if (tonumber(r) || tonumber(l))
  850. return 1;
  851. nvalue(l) *= nvalue(r);
  852. --top;
  853. }
  854. break;
  855. case DIVOP:
  856. {
  857. Object *l = top-2;
  858. Object *r = top-1;
  859. if (tonumber(r) || tonumber(l))
  860. return 1;
  861. nvalue(l) /= nvalue(r);
  862. --top;
  863. }
  864. break;
  865. case POWOP:
  866. {
  867. Object *l = top-2;
  868. Object *r = top-1;
  869. if (tonumber(r) || tonumber(l))
  870. return 1;
  871. nvalue(l) = pow(nvalue(l), nvalue(r));
  872. --top;
  873. }
  874. break;
  875. case CONCOP:
  876. {
  877. Object *l = top-2;
  878. Object *r = top-1;
  879. if (tostring(r) || tostring(l))
  880. return 1;
  881. svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  882. if (svalue(l) == NULL)
  883. return 1;
  884. --top;
  885. }
  886. break;
  887. case MINUSOP:
  888. if (tonumber(top-1))
  889. return 1;
  890. nvalue(top-1) = - nvalue(top-1);
  891. break;
  892. case NOTOP:
  893. tag(top-1) = tag(top-1) == LUA_T_NIL ? LUA_T_NUMBER : LUA_T_NIL;
  894. break;
  895. case ONTJMP:
  896. {
  897. CodeWord code;
  898. get_word(code,pc);
  899. if (tag(top-1) != LUA_T_NIL) pc += code.w;
  900. }
  901. break;
  902. case ONFJMP:
  903. {
  904. CodeWord code;
  905. get_word(code,pc);
  906. if (tag(top-1) == LUA_T_NIL) pc += code.w;
  907. }
  908. break;
  909. case JMP:
  910. {
  911. CodeWord code;
  912. get_word(code,pc);
  913. pc += code.w;
  914. }
  915. break;
  916. case UPJMP:
  917. {
  918. CodeWord code;
  919. get_word(code,pc);
  920. pc -= code.w;
  921. }
  922. break;
  923. case IFFJMP:
  924. {
  925. CodeWord code;
  926. get_word(code,pc);
  927. top--;
  928. if (tag(top) == LUA_T_NIL) pc += code.w;
  929. }
  930. break;
  931. case IFFUPJMP:
  932. {
  933. CodeWord code;
  934. get_word(code,pc);
  935. top--;
  936. if (tag(top) == LUA_T_NIL) pc -= code.w;
  937. }
  938. break;
  939. case POP: --top; break;
  940. case CALLFUNC:
  941. {
  942. int nParams = *(pc++);
  943. int nResults = *(pc++);
  944. Object *func = top-1-nParams; /* function is below parameters */
  945. int newBase = (top-stack)-nParams;
  946. do_call(func, newBase, nResults, newBase-1);
  947. }
  948. break;
  949. case RETCODE0:
  950. return base;
  951. case RETCODE:
  952. return base+*pc;
  953. case SETFUNCTION:
  954. {
  955. CodeCode file;
  956. CodeWord func;
  957. get_code(file,pc);
  958. get_word(func,pc);
  959. if (lua_pushfunction ((char *)file.b, func.w))
  960. return 1;
  961. }
  962. break;
  963. case SETLINE:
  964. {
  965. CodeWord code;
  966. get_word(code,pc);
  967. lua_debugline = code.w;
  968. }
  969. break;
  970. case RESET:
  971. lua_popfunction ();
  972. break;
  973. default:
  974. lua_error ("internal error - opcode doesn't match");
  975. return 1;
  976. }
  977. }
  978. }