opcode.c 22 KB

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