opcode.c 23 KB

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