opcode.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.4 1994/11/07 16:34:44 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. lua_reportbug ("call expression not a function");
  245. /* adjust the number of results */
  246. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  247. adjust_top(stack+firstResult+nResults);
  248. /* move results to the given position */
  249. if (firstResult != whereRes)
  250. {
  251. int i = top - (stack+firstResult); /* number of results */
  252. top -= firstResult-whereRes;
  253. while (i--)
  254. *(stack+whereRes+i) = *(stack+firstResult+i);
  255. }
  256. }
  257. /*
  258. ** Function to index a table. Receives the table at top-2 and the index
  259. ** at top-1.
  260. */
  261. static void pushsubscript (void)
  262. {
  263. if (tag(top-2) != LUA_T_ARRAY)
  264. do_call(&fallBacks[FB_GETTABLE].function, (top-stack)-2, 1, (top-stack)-2);
  265. else
  266. {
  267. Object *h = lua_hashget(avalue(top-2), top-1);
  268. if (h == NULL)
  269. do_call(&fallBacks[FB_INDEX].function, (top-stack)-2, 1, (top-stack)-2);
  270. else
  271. {
  272. --top;
  273. *(top-1) = *h;
  274. }
  275. }
  276. }
  277. /*
  278. ** Function to store indexed based on values at the top
  279. */
  280. static void storesubscript (void)
  281. {
  282. if (tag(top-3) != LUA_T_ARRAY)
  283. do_call(&fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3);
  284. else
  285. {
  286. Object *h = lua_hashdefine (avalue(top-3), top-2);
  287. *h = *(top-1);
  288. top -= 3;
  289. }
  290. }
  291. /*
  292. ** Traverse all objects on stack
  293. */
  294. void lua_travstack (void (*fn)(Object *))
  295. {
  296. Object *o;
  297. for (o = top-1; o >= stack; o--)
  298. fn (o);
  299. }
  300. /*
  301. ** Execute a protected call. If function is null compiles the pre-set input.
  302. ** Leave nResults on the stack.
  303. */
  304. static int do_protectedrun (Object *function, int nResults)
  305. {
  306. Object f;
  307. jmp_buf myErrorJmp;
  308. int status;
  309. int oldCBase = CBase;
  310. jmp_buf *oldErr = errorJmp;
  311. errorJmp = &myErrorJmp;
  312. if (setjmp(myErrorJmp) == 0)
  313. {
  314. if (function == NULL)
  315. {
  316. tag(&f) = LUA_T_FUNCTION;
  317. bvalue(&f) = lua_parse();
  318. function = &f;
  319. }
  320. else
  321. tag(&f) = LUA_T_NIL;
  322. do_call(function, CBase, nResults, CBase);
  323. CnResults = (top-stack) - CBase; /* number of results */
  324. CBase += CnResults; /* incorporate results on the stack */
  325. status = 0;
  326. }
  327. else
  328. {
  329. CBase = oldCBase;
  330. top = stack+CBase;
  331. status = 1;
  332. }
  333. if (tag(&f) == LUA_T_FUNCTION)
  334. free(bvalue(&f));
  335. errorJmp = oldErr;
  336. return status;
  337. }
  338. /*
  339. ** Execute the given lua function. Return 0 on success or 1 on error.
  340. */
  341. int lua_callfunction (lua_Object function)
  342. {
  343. if (function == NULL)
  344. return 1;
  345. else
  346. return do_protectedrun (Address(function), MULT_RET);
  347. }
  348. /*
  349. ** Open file, generate opcode and execute global statement. Return 0 on
  350. ** success or 1 on error.
  351. */
  352. int lua_dofile (char *filename)
  353. {
  354. int status;
  355. char *message = lua_openfile (filename);
  356. if (message)
  357. {
  358. lua_message(message);
  359. return 1;
  360. }
  361. status = do_protectedrun(NULL, 0);
  362. lua_closefile();
  363. return status;
  364. }
  365. /*
  366. ** Generate opcode stored on string and execute global statement. Return 0 on
  367. ** success or 1 on error.
  368. */
  369. int lua_dostring (char *string)
  370. {
  371. int status;
  372. char *message = lua_openstring(string);
  373. if (message)
  374. {
  375. lua_message(message);
  376. return 1;
  377. }
  378. status = do_protectedrun(NULL, 0);
  379. lua_closestring();
  380. return status;
  381. }
  382. /*
  383. ** API: set a function as a fallback
  384. */
  385. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  386. {
  387. static Object func = {LUA_T_CFUNCTION, luaI_setfallback};
  388. adjustC(0);
  389. lua_pushstring(name);
  390. lua_pushcfunction(fallback);
  391. do_protectedrun(&func, 1);
  392. return (Ref(top-1));
  393. }
  394. /*
  395. ** API: receives on the stack the table and the index.
  396. ** returns the value.
  397. */
  398. lua_Object lua_getsubscript (void)
  399. {
  400. static Byte code[2] = {PUSHINDEXED, RETCODE0};
  401. int status;
  402. Object func;
  403. tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
  404. adjustC(2);
  405. status = do_protectedrun(&func, 1);
  406. if (status == 0)
  407. return (Ref(top-1));
  408. else
  409. return 0;
  410. }
  411. /*
  412. ** API: receives on the stack the table, the index, and the new value.
  413. */
  414. int lua_storesubscript (void)
  415. {
  416. static Byte code[2] = {STOREINDEXED, RETCODE0};
  417. Object func;
  418. tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
  419. adjustC(3);
  420. return(do_protectedrun(&func, 0));
  421. }
  422. /*
  423. ** Get a parameter, returning the object handle or 0 on error.
  424. ** 'number' must be 1 to get the first parameter.
  425. */
  426. lua_Object lua_getparam (int number)
  427. {
  428. if (number <= 0 || number > CnResults) return 0;
  429. /* Ref(stack+(CBase-CnResults+number-1)) ==
  430. stack+(CBase-CnResults+number-1)-stack+1 == */
  431. return CBase-CnResults+number;
  432. }
  433. /*
  434. ** Given an object handle, return its number value. On error, return 0.0.
  435. */
  436. real lua_getnumber (lua_Object object)
  437. {
  438. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return 0.0;
  439. if (tonumber (Address(object))) return 0.0;
  440. else return (nvalue(Address(object)));
  441. }
  442. /*
  443. ** Given an object handle, return its string pointer. On error, return NULL.
  444. */
  445. char *lua_getstring (lua_Object object)
  446. {
  447. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
  448. if (tostring (Address(object))) return NULL;
  449. else return (svalue(Address(object)));
  450. }
  451. /*
  452. ** Given an object handle, return a copy of its string. On error, return NULL.
  453. */
  454. char *lua_copystring (lua_Object object)
  455. {
  456. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
  457. if (tostring (Address(object))) return NULL;
  458. else return (strdup(svalue(Address(object))));
  459. }
  460. /*
  461. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  462. */
  463. lua_CFunction lua_getcfunction (lua_Object object)
  464. {
  465. if (object == 0) return NULL;
  466. if (tag(Address(object)) != LUA_T_CFUNCTION) return NULL;
  467. else return (fvalue(Address(object)));
  468. }
  469. /*
  470. ** Given an object handle, return its user data. On error, return NULL.
  471. */
  472. void *lua_getuserdata (lua_Object object)
  473. {
  474. if (object == 0) return NULL;
  475. if (tag(Address(object)) != LUA_T_USERDATA) return NULL;
  476. else return (uvalue(Address(object)));
  477. }
  478. /*
  479. ** Get a global object. Return the object handle or NULL on error.
  480. */
  481. lua_Object lua_getglobal (char *name)
  482. {
  483. int n = lua_findsymbol(name);
  484. if (n < 0) return 0;
  485. *(top++) = s_object(n);
  486. return Ref(top-1);
  487. }
  488. /*
  489. ** Store top of the stack at a global variable array field.
  490. ** Return 1 on error, 0 on success.
  491. */
  492. int lua_storeglobal (char *name)
  493. {
  494. int n = lua_findsymbol (name);
  495. if (n < 0) return 1;
  496. adjustC(1);
  497. s_object(n) = *(--top);
  498. return 0;
  499. }
  500. /*
  501. ** Push a nil object
  502. */
  503. int lua_pushnil (void)
  504. {
  505. lua_checkstack(top-stack+1);
  506. tag(top++) = LUA_T_NIL;
  507. return 0;
  508. }
  509. /*
  510. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  511. */
  512. int lua_pushnumber (real n)
  513. {
  514. lua_checkstack(top-stack+1);
  515. tag(top) = LUA_T_NUMBER; nvalue(top++) = n;
  516. return 0;
  517. }
  518. /*
  519. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  520. */
  521. int lua_pushstring (char *s)
  522. {
  523. lua_checkstack(top-stack+1);
  524. tag(top) = LUA_T_STRING;
  525. svalue(top++) = lua_createstring(s);
  526. return 0;
  527. }
  528. /*
  529. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  530. */
  531. int lua_pushcfunction (lua_CFunction fn)
  532. {
  533. lua_checkstack(top-stack+1);
  534. tag(top) = LUA_T_CFUNCTION; fvalue(top++) = fn;
  535. return 0;
  536. }
  537. /*
  538. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  539. */
  540. int lua_pushuserdata (void *u)
  541. {
  542. lua_checkstack(top-stack+1);
  543. tag(top) = LUA_T_USERDATA; uvalue(top++) = u;
  544. return 0;
  545. }
  546. /*
  547. ** Push a lua_Object to stack.
  548. */
  549. int lua_pushobject (lua_Object o)
  550. {
  551. lua_checkstack(top-stack+1);
  552. *top++ = *Address(o);
  553. return 0;
  554. }
  555. /*
  556. ** Push an object on the stack.
  557. */
  558. void luaI_pushobject (Object *o)
  559. {
  560. lua_checkstack(top-stack+1);
  561. *top++ = *o;
  562. }
  563. int lua_type (lua_Object o)
  564. {
  565. if (o == 0)
  566. return LUA_T_NIL;
  567. else
  568. return tag(Address(o));
  569. }
  570. static void call_arith (char *op)
  571. {
  572. lua_pushstring(op);
  573. do_call(&fallBacks[FB_ARITH].function, (top-stack)-3, 1, (top-stack)-3);
  574. }
  575. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  576. lua_Type tag_great, char *op)
  577. {
  578. Object *l = top-2;
  579. Object *r = top-1;
  580. int result;
  581. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  582. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  583. else if (tostring(l) || tostring(r))
  584. {
  585. lua_pushstring(op);
  586. do_call(&fallBacks[FB_ORDER].function, (top-stack)-3, 1, (top-stack)-3);
  587. return;
  588. }
  589. else
  590. result = strcmp(svalue(l), svalue(r));
  591. top--;
  592. nvalue(top-1) = 1;
  593. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  594. }
  595. /*
  596. ** Execute the given opcode, until a RET. Parameters are between
  597. ** [stack+base,top). Returns n such that the the results are between
  598. ** [stack+n,top).
  599. */
  600. static int lua_execute (Byte *pc, int base)
  601. {
  602. lua_debugline = 0; /* reset debug flag */
  603. lua_checkstack(STACKGAP+MAX_TEMPS+base);
  604. while (1)
  605. {
  606. OpCode opcode;
  607. switch (opcode = (OpCode)*pc++)
  608. {
  609. case PUSHNIL: tag(top++) = LUA_T_NIL; break;
  610. case PUSH0: tag(top) = LUA_T_NUMBER; nvalue(top++) = 0; break;
  611. case PUSH1: tag(top) = LUA_T_NUMBER; nvalue(top++) = 1; break;
  612. case PUSH2: tag(top) = LUA_T_NUMBER; nvalue(top++) = 2; break;
  613. case PUSHBYTE: tag(top) = LUA_T_NUMBER; nvalue(top++) = *pc++; break;
  614. case PUSHWORD:
  615. {
  616. CodeWord code;
  617. get_word(code,pc);
  618. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.w;
  619. }
  620. break;
  621. case PUSHFLOAT:
  622. {
  623. CodeFloat code;
  624. get_float(code,pc);
  625. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.f;
  626. }
  627. break;
  628. case PUSHSTRING:
  629. {
  630. CodeWord code;
  631. get_word(code,pc);
  632. tag(top) = LUA_T_STRING; svalue(top++) = lua_constant[code.w];
  633. }
  634. break;
  635. case PUSHFUNCTION:
  636. {
  637. CodeCode code;
  638. get_code(code,pc);
  639. tag(top) = LUA_T_FUNCTION; bvalue(top++) = code.b;
  640. }
  641. break;
  642. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  643. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  644. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  645. case PUSHLOCAL9: *top++ = *((stack+base) + (int)(opcode-PUSHLOCAL0)); break;
  646. case PUSHLOCAL: *top++ = *((stack+base) + (*pc++)); break;
  647. case PUSHGLOBAL:
  648. {
  649. CodeWord code;
  650. get_word(code,pc);
  651. *top++ = s_object(code.w);
  652. }
  653. break;
  654. case PUSHINDEXED:
  655. pushsubscript();
  656. break;
  657. case PUSHSELF:
  658. {
  659. Object receiver = *(top-2);
  660. pushsubscript();
  661. *(top++) = receiver;
  662. break;
  663. }
  664. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  665. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  666. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  667. case STORELOCAL9:
  668. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  669. break;
  670. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  671. case STOREGLOBAL:
  672. {
  673. CodeWord code;
  674. get_word(code,pc);
  675. s_object(code.w) = *(--top);
  676. }
  677. break;
  678. case STOREINDEXED0:
  679. storesubscript();
  680. break;
  681. case STOREINDEXED:
  682. {
  683. int n = *pc++;
  684. if (tag(top-3-n) != LUA_T_ARRAY)
  685. {
  686. *(top+1) = *(top-1);
  687. *(top) = *(top-2-n);
  688. *(top-1) = *(top-3-n);
  689. top += 2;
  690. do_call(&fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3);
  691. }
  692. else
  693. {
  694. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  695. *h = *(top-1);
  696. top--;
  697. }
  698. }
  699. break;
  700. case STORELIST0:
  701. case STORELIST:
  702. {
  703. int m, n;
  704. Object *arr;
  705. if (opcode == STORELIST0) m = 0;
  706. else m = *(pc++) * FIELDS_PER_FLUSH;
  707. n = *(pc++);
  708. arr = top-n-1;
  709. if (tag(arr) != LUA_T_ARRAY)
  710. lua_reportbug ("internal error - table expected");
  711. while (n)
  712. {
  713. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  714. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  715. top--;
  716. n--;
  717. }
  718. }
  719. break;
  720. case STORERECORD:
  721. {
  722. int n = *(pc++);
  723. Object *arr = top-n-1;
  724. if (tag(arr) != LUA_T_ARRAY)
  725. lua_reportbug ("internal error - table expected");
  726. while (n)
  727. {
  728. CodeWord code;
  729. get_word(code,pc);
  730. tag(top) = LUA_T_STRING; svalue(top) = lua_constant[code.w];
  731. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  732. top--;
  733. n--;
  734. }
  735. }
  736. break;
  737. case ADJUST0:
  738. adjust_top((stack+base));
  739. break;
  740. case ADJUST:
  741. adjust_top((stack+base) + *(pc++));
  742. break;
  743. case CREATEARRAY:
  744. {
  745. CodeWord size;
  746. get_word(size,pc);
  747. top++;
  748. avalue(top-1) = lua_createarray(size.w);
  749. tag(top-1) = LUA_T_ARRAY;
  750. }
  751. break;
  752. case EQOP:
  753. {
  754. int res;
  755. Object *l = top-2;
  756. Object *r = top-1;
  757. --top;
  758. if (tag(l) != tag(r))
  759. res = 0;
  760. else
  761. {
  762. switch (tag(l))
  763. {
  764. case LUA_T_NIL:
  765. res = 1; break;
  766. case LUA_T_NUMBER:
  767. res = (nvalue(l) == nvalue(r)); break;
  768. case LUA_T_ARRAY:
  769. res = (avalue(l) == avalue(r)); break;
  770. case LUA_T_FUNCTION:
  771. res = (bvalue(l) == bvalue(r)); break;
  772. case LUA_T_CFUNCTION:
  773. res = (fvalue(l) == fvalue(r)); break;
  774. case LUA_T_STRING:
  775. res = (strcmp (svalue(l), svalue(r)) == 0); break;
  776. default:
  777. res = (uvalue(l) == uvalue(r)); break;
  778. }
  779. }
  780. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  781. nvalue(top-1) = 1;
  782. }
  783. break;
  784. case LTOP:
  785. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "<");
  786. break;
  787. case LEOP:
  788. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "<=");
  789. break;
  790. case GTOP:
  791. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, ">");
  792. break;
  793. case GEOP:
  794. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, ">=");
  795. break;
  796. case ADDOP:
  797. {
  798. Object *l = top-2;
  799. Object *r = top-1;
  800. if (tonumber(r) || tonumber(l))
  801. call_arith("+");
  802. else
  803. {
  804. nvalue(l) += nvalue(r);
  805. --top;
  806. }
  807. }
  808. break;
  809. case SUBOP:
  810. {
  811. Object *l = top-2;
  812. Object *r = top-1;
  813. if (tonumber(r) || tonumber(l))
  814. call_arith("-");
  815. else
  816. {
  817. nvalue(l) -= nvalue(r);
  818. --top;
  819. }
  820. }
  821. break;
  822. case MULTOP:
  823. {
  824. Object *l = top-2;
  825. Object *r = top-1;
  826. if (tonumber(r) || tonumber(l))
  827. call_arith("*");
  828. else
  829. {
  830. nvalue(l) *= nvalue(r);
  831. --top;
  832. }
  833. }
  834. break;
  835. case DIVOP:
  836. {
  837. Object *l = top-2;
  838. Object *r = top-1;
  839. if (tonumber(r) || tonumber(l))
  840. call_arith("/");
  841. else
  842. {
  843. nvalue(l) /= nvalue(r);
  844. --top;
  845. }
  846. }
  847. break;
  848. case POWOP:
  849. {
  850. Object *l = top-2;
  851. Object *r = top-1;
  852. if (tonumber(r) || tonumber(l))
  853. call_arith("^");
  854. else
  855. {
  856. nvalue(l) = pow(nvalue(l), nvalue(r));
  857. --top;
  858. }
  859. }
  860. break;
  861. case CONCOP:
  862. {
  863. Object *l = top-2;
  864. Object *r = top-1;
  865. if (tostring(r) || tostring(l))
  866. do_call(&fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2);
  867. else
  868. {
  869. svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  870. --top;
  871. }
  872. }
  873. break;
  874. case MINUSOP:
  875. if (tonumber(top-1))
  876. do_call(&fallBacks[FB_UNMINUS].function, (top-stack)-1, 1, (top-stack)-1);
  877. else
  878. nvalue(top-1) = - nvalue(top-1);
  879. break;
  880. case NOTOP:
  881. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  882. break;
  883. case ONTJMP:
  884. {
  885. CodeWord code;
  886. get_word(code,pc);
  887. if (tag(top-1) != LUA_T_NIL) pc += code.w;
  888. }
  889. break;
  890. case ONFJMP:
  891. {
  892. CodeWord code;
  893. get_word(code,pc);
  894. if (tag(top-1) == LUA_T_NIL) pc += code.w;
  895. }
  896. break;
  897. case JMP:
  898. {
  899. CodeWord code;
  900. get_word(code,pc);
  901. pc += code.w;
  902. }
  903. break;
  904. case UPJMP:
  905. {
  906. CodeWord code;
  907. get_word(code,pc);
  908. pc -= code.w;
  909. }
  910. break;
  911. case IFFJMP:
  912. {
  913. CodeWord code;
  914. get_word(code,pc);
  915. top--;
  916. if (tag(top) == LUA_T_NIL) pc += code.w;
  917. }
  918. break;
  919. case IFFUPJMP:
  920. {
  921. CodeWord code;
  922. get_word(code,pc);
  923. top--;
  924. if (tag(top) == LUA_T_NIL) pc -= code.w;
  925. }
  926. break;
  927. case POP: --top; break;
  928. case CALLFUNC:
  929. {
  930. int nParams = *(pc++);
  931. int nResults = *(pc++);
  932. Object *func = top-1-nParams; /* function is below parameters */
  933. int newBase = (top-stack)-nParams;
  934. do_call(func, newBase, nResults, newBase-1);
  935. }
  936. break;
  937. case RETCODE0:
  938. return base;
  939. case RETCODE:
  940. return base+*pc;
  941. case SETFUNCTION:
  942. {
  943. CodeCode file;
  944. CodeWord func;
  945. get_code(file,pc);
  946. get_word(func,pc);
  947. lua_pushfunction ((char *)file.b, func.w);
  948. }
  949. break;
  950. case SETLINE:
  951. {
  952. CodeWord code;
  953. get_word(code,pc);
  954. lua_debugline = code.w;
  955. }
  956. break;
  957. case RESET:
  958. lua_popfunction ();
  959. break;
  960. default:
  961. lua_error ("internal error - opcode doesn't match");
  962. }
  963. }
  964. }