opcode.c 21 KB

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