opcode.c 22 KB

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