opcode.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.12 1994/11/16 16:03:48 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. 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: receives on the stack the table, the index, and the new value.
  379. */
  380. int lua_storesubscript (void)
  381. {
  382. static Byte code[2] = {STOREINDEXED, RETCODE0};
  383. Object func;
  384. tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
  385. adjustC(3);
  386. return(do_protectedrun(&func, 0));
  387. }
  388. /*
  389. ** API: creates a new table
  390. */
  391. lua_Object lua_createTable (int initSize)
  392. {
  393. adjustC(0);
  394. tag(top) = LUA_T_ARRAY;
  395. avalue(top) = lua_createarray(initSize);
  396. top++;
  397. CBase++; /* incorporate object in the stack */
  398. return Ref(top-1);
  399. }
  400. /*
  401. ** Get a parameter, returning the object handle or 0 on error.
  402. ** 'number' must be 1 to get the first parameter.
  403. */
  404. lua_Object lua_getparam (int number)
  405. {
  406. if (number <= 0 || number > CnResults) return 0;
  407. /* Ref(stack+(CBase-CnResults+number-1)) ==
  408. stack+(CBase-CnResults+number-1)-stack+1 == */
  409. return CBase-CnResults+number;
  410. }
  411. /*
  412. ** Given an object handle, return its number value. On error, return 0.0.
  413. */
  414. real lua_getnumber (lua_Object object)
  415. {
  416. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return 0.0;
  417. if (tonumber (Address(object))) return 0.0;
  418. else return (nvalue(Address(object)));
  419. }
  420. /*
  421. ** Given an object handle, return its string pointer. On error, return NULL.
  422. */
  423. char *lua_getstring (lua_Object object)
  424. {
  425. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
  426. if (tostring (Address(object))) return NULL;
  427. else return (svalue(Address(object)));
  428. }
  429. /*
  430. ** Given an object handle, return a copy of its string. On error, return NULL.
  431. */
  432. char *lua_copystring (lua_Object object)
  433. {
  434. if (object == 0 || tag(Address(object)) == LUA_T_NIL) return NULL;
  435. if (tostring (Address(object))) return NULL;
  436. else return (strdup(svalue(Address(object))));
  437. }
  438. /*
  439. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  440. */
  441. lua_CFunction lua_getcfunction (lua_Object object)
  442. {
  443. if (object == 0) return NULL;
  444. if (tag(Address(object)) != LUA_T_CFUNCTION) return NULL;
  445. else return (fvalue(Address(object)));
  446. }
  447. /*
  448. ** Given an object handle, return its user data. On error, return NULL.
  449. */
  450. void *lua_getuserdata (lua_Object object)
  451. {
  452. if (object == 0) return NULL;
  453. if (tag(Address(object)) != LUA_T_USERDATA) return NULL;
  454. else return (uvalue(Address(object)));
  455. }
  456. lua_Object lua_getlocked (int ref)
  457. {
  458. adjustC(0);
  459. *top = *luaI_getlocked(ref);
  460. top++;
  461. CBase++; /* incorporate object in the stack */
  462. return Ref(top-1);
  463. }
  464. /*
  465. ** Get a global object. Return the object handle or NULL on error.
  466. */
  467. lua_Object lua_getglobal (char *name)
  468. {
  469. int n = luaI_findsymbolbyname(name);
  470. adjustC(0);
  471. *top = s_object(n);
  472. top++;
  473. CBase++; /* incorporate object in the stack */
  474. return Ref(top-1);
  475. }
  476. /*
  477. ** Store top of the stack at a global variable array field.
  478. ** Return 1 on error, 0 on success.
  479. */
  480. int lua_storeglobal (char *name)
  481. {
  482. int n = luaI_findsymbolbyname(name);
  483. if (n < 0) return 1;
  484. adjustC(1);
  485. s_object(n) = *(--top);
  486. return 0;
  487. }
  488. /*
  489. ** Push a nil object
  490. */
  491. int lua_pushnil (void)
  492. {
  493. lua_checkstack(top-stack+1);
  494. tag(top++) = LUA_T_NIL;
  495. return 0;
  496. }
  497. /*
  498. ** Push an object (tag=number) to stack. Return 0 on success or 1 on error.
  499. */
  500. int lua_pushnumber (real n)
  501. {
  502. lua_checkstack(top-stack+1);
  503. tag(top) = LUA_T_NUMBER; nvalue(top++) = n;
  504. return 0;
  505. }
  506. /*
  507. ** Push an object (tag=string) to stack. Return 0 on success or 1 on error.
  508. */
  509. int lua_pushstring (char *s)
  510. {
  511. lua_checkstack(top-stack+1);
  512. tag(top) = LUA_T_STRING;
  513. svalue(top++) = lua_createstring(s);
  514. return 0;
  515. }
  516. /*
  517. ** Push an object (tag=cfunction) to stack. Return 0 on success or 1 on error.
  518. */
  519. int lua_pushcfunction (lua_CFunction fn)
  520. {
  521. lua_checkstack(top-stack+1);
  522. tag(top) = LUA_T_CFUNCTION; fvalue(top++) = fn;
  523. return 0;
  524. }
  525. /*
  526. ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
  527. */
  528. int lua_pushuserdata (void *u)
  529. {
  530. lua_checkstack(top-stack+1);
  531. tag(top) = LUA_T_USERDATA; uvalue(top++) = u;
  532. return 0;
  533. }
  534. /*
  535. ** Push a lua_Object to stack.
  536. */
  537. int lua_pushobject (lua_Object o)
  538. {
  539. lua_checkstack(top-stack+1);
  540. *top++ = *Address(o);
  541. return 0;
  542. }
  543. /*
  544. ** Push an object on the stack.
  545. */
  546. void luaI_pushobject (Object *o)
  547. {
  548. lua_checkstack(top-stack+1);
  549. *top++ = *o;
  550. }
  551. int lua_type (lua_Object o)
  552. {
  553. if (o == 0)
  554. return LUA_T_NIL;
  555. else
  556. return tag(Address(o));
  557. }
  558. void luaI_gcFB (Object *o)
  559. {
  560. *(top++) = *o;
  561. do_call(&luaI_fallBacks[FB_GC].function, (top-stack)-1, 0, (top-stack)-1);
  562. }
  563. static void call_arith (char *op)
  564. {
  565. lua_pushstring(op);
  566. do_call(&luaI_fallBacks[FB_ARITH].function, (top-stack)-3, 1, (top-stack)-3);
  567. }
  568. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  569. lua_Type tag_great, char *op)
  570. {
  571. Object *l = top-2;
  572. Object *r = top-1;
  573. int result;
  574. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  575. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  576. else if (tostring(l) || tostring(r))
  577. {
  578. lua_pushstring(op);
  579. do_call(&luaI_fallBacks[FB_ORDER].function, (top-stack)-3, 1, (top-stack)-3);
  580. return;
  581. }
  582. else
  583. result = strcmp(svalue(l), svalue(r));
  584. top--;
  585. nvalue(top-1) = 1;
  586. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  587. }
  588. /*
  589. ** Execute the given opcode, until a RET. Parameters are between
  590. ** [stack+base,top). Returns n such that the the results are between
  591. ** [stack+n,top).
  592. */
  593. static int lua_execute (Byte *pc, int base)
  594. {
  595. lua_checkstack(STACKGAP+MAX_TEMPS+base);
  596. while (1)
  597. {
  598. OpCode opcode;
  599. switch (opcode = (OpCode)*pc++)
  600. {
  601. case PUSHNIL: tag(top++) = LUA_T_NIL; break;
  602. case PUSH0: case PUSH1: case PUSH2:
  603. tag(top) = LUA_T_NUMBER;
  604. nvalue(top++) = opcode-PUSH0;
  605. break;
  606. case PUSHBYTE: tag(top) = LUA_T_NUMBER; nvalue(top++) = *pc++; break;
  607. case PUSHWORD:
  608. {
  609. CodeWord code;
  610. get_word(code,pc);
  611. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.w;
  612. }
  613. break;
  614. case PUSHFLOAT:
  615. {
  616. CodeFloat code;
  617. get_float(code,pc);
  618. tag(top) = LUA_T_NUMBER; nvalue(top++) = code.f;
  619. }
  620. break;
  621. case PUSHSTRING:
  622. {
  623. CodeWord code;
  624. get_word(code,pc);
  625. tag(top) = LUA_T_STRING; svalue(top++) = lua_constant[code.w];
  626. }
  627. break;
  628. case PUSHFUNCTION:
  629. {
  630. CodeCode code;
  631. get_code(code,pc);
  632. tag(top) = LUA_T_FUNCTION; bvalue(top++) = code.b;
  633. }
  634. break;
  635. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  636. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  637. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  638. case PUSHLOCAL9: *top++ = *((stack+base) + (int)(opcode-PUSHLOCAL0)); break;
  639. case PUSHLOCAL: *top++ = *((stack+base) + (*pc++)); break;
  640. case PUSHGLOBAL:
  641. {
  642. CodeWord code;
  643. get_word(code,pc);
  644. *top++ = s_object(code.w);
  645. }
  646. break;
  647. case PUSHINDEXED:
  648. pushsubscript();
  649. break;
  650. case PUSHSELF:
  651. {
  652. Object receiver = *(top-2);
  653. pushsubscript();
  654. *(top++) = receiver;
  655. break;
  656. }
  657. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  658. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  659. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  660. case STORELOCAL9:
  661. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  662. break;
  663. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  664. case STOREGLOBAL:
  665. {
  666. CodeWord code;
  667. get_word(code,pc);
  668. s_object(code.w) = *(--top);
  669. }
  670. break;
  671. case STOREINDEXED0:
  672. storesubscript();
  673. break;
  674. case STOREINDEXED:
  675. {
  676. int n = *pc++;
  677. if (tag(top-3-n) != LUA_T_ARRAY)
  678. {
  679. *(top+1) = *(top-1);
  680. *(top) = *(top-2-n);
  681. *(top-1) = *(top-3-n);
  682. top += 2;
  683. do_call(&luaI_fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3);
  684. }
  685. else
  686. {
  687. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  688. *h = *(top-1);
  689. top--;
  690. }
  691. }
  692. break;
  693. case STORELIST0:
  694. case STORELIST:
  695. {
  696. int m, n;
  697. Object *arr;
  698. if (opcode == STORELIST0) m = 0;
  699. else m = *(pc++) * FIELDS_PER_FLUSH;
  700. n = *(pc++);
  701. arr = top-n-1;
  702. while (n)
  703. {
  704. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  705. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  706. top--;
  707. n--;
  708. }
  709. }
  710. break;
  711. case STORERECORD:
  712. {
  713. int n = *(pc++);
  714. Object *arr = top-n-1;
  715. while (n)
  716. {
  717. CodeWord code;
  718. get_word(code,pc);
  719. tag(top) = LUA_T_STRING; svalue(top) = lua_constant[code.w];
  720. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  721. top--;
  722. n--;
  723. }
  724. }
  725. break;
  726. case ADJUST0:
  727. adjust_top(base);
  728. break;
  729. case ADJUST:
  730. adjust_top(base + *(pc++));
  731. break;
  732. case CREATEARRAY:
  733. {
  734. CodeWord size;
  735. get_word(size,pc);
  736. tag(top) = LUA_T_ARRAY;
  737. avalue(top) = lua_createarray(size.w);
  738. top++;
  739. }
  740. break;
  741. case EQOP:
  742. {
  743. int res = lua_equalObj(top-2, top-1);
  744. --top;
  745. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  746. nvalue(top-1) = 1;
  747. }
  748. break;
  749. case LTOP:
  750. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "<");
  751. break;
  752. case LEOP:
  753. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "<=");
  754. break;
  755. case GTOP:
  756. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, ">");
  757. break;
  758. case GEOP:
  759. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, ">=");
  760. break;
  761. case ADDOP:
  762. {
  763. Object *l = top-2;
  764. Object *r = top-1;
  765. if (tonumber(r) || tonumber(l))
  766. call_arith("+");
  767. else
  768. {
  769. nvalue(l) += nvalue(r);
  770. --top;
  771. }
  772. }
  773. break;
  774. case SUBOP:
  775. {
  776. Object *l = top-2;
  777. Object *r = top-1;
  778. if (tonumber(r) || tonumber(l))
  779. call_arith("-");
  780. else
  781. {
  782. nvalue(l) -= nvalue(r);
  783. --top;
  784. }
  785. }
  786. break;
  787. case MULTOP:
  788. {
  789. Object *l = top-2;
  790. Object *r = top-1;
  791. if (tonumber(r) || tonumber(l))
  792. call_arith("*");
  793. else
  794. {
  795. nvalue(l) *= nvalue(r);
  796. --top;
  797. }
  798. }
  799. break;
  800. case DIVOP:
  801. {
  802. Object *l = top-2;
  803. Object *r = top-1;
  804. if (tonumber(r) || tonumber(l))
  805. call_arith("/");
  806. else
  807. {
  808. nvalue(l) /= nvalue(r);
  809. --top;
  810. }
  811. }
  812. break;
  813. case POWOP:
  814. {
  815. Object *l = top-2;
  816. Object *r = top-1;
  817. if (tonumber(r) || tonumber(l))
  818. call_arith("^");
  819. else
  820. {
  821. nvalue(l) = pow(nvalue(l), nvalue(r));
  822. --top;
  823. }
  824. }
  825. break;
  826. case CONCOP:
  827. {
  828. Object *l = top-2;
  829. Object *r = top-1;
  830. if (tostring(r) || tostring(l))
  831. do_call(&luaI_fallBacks[FB_CONCAT].function, (top-stack)-2, 1, (top-stack)-2);
  832. else
  833. {
  834. svalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  835. --top;
  836. }
  837. }
  838. break;
  839. case MINUSOP:
  840. if (tonumber(top-1))
  841. do_call(&luaI_fallBacks[FB_UNMINUS].function, (top-stack)-1, 1, (top-stack)-1);
  842. else
  843. nvalue(top-1) = - nvalue(top-1);
  844. break;
  845. case NOTOP:
  846. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  847. nvalue(top-1) = 1;
  848. break;
  849. case ONTJMP:
  850. {
  851. CodeWord code;
  852. get_word(code,pc);
  853. if (tag(top-1) != LUA_T_NIL) pc += code.w;
  854. }
  855. break;
  856. case ONFJMP:
  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 JMP:
  864. {
  865. CodeWord code;
  866. get_word(code,pc);
  867. pc += code.w;
  868. }
  869. break;
  870. case UPJMP:
  871. {
  872. CodeWord code;
  873. get_word(code,pc);
  874. pc -= code.w;
  875. }
  876. break;
  877. case IFFJMP:
  878. {
  879. CodeWord code;
  880. get_word(code,pc);
  881. top--;
  882. if (tag(top) == LUA_T_NIL) pc += code.w;
  883. }
  884. break;
  885. case IFFUPJMP:
  886. {
  887. CodeWord code;
  888. get_word(code,pc);
  889. top--;
  890. if (tag(top) == LUA_T_NIL) pc -= code.w;
  891. }
  892. break;
  893. case POP: --top; break;
  894. case CALLFUNC:
  895. {
  896. int nParams = *(pc++);
  897. int nResults = *(pc++);
  898. Object *func = top-1-nParams; /* function is below parameters */
  899. int newBase = (top-stack)-nParams;
  900. do_call(func, newBase, nResults, newBase-1);
  901. }
  902. break;
  903. case RETCODE0:
  904. return base;
  905. case RETCODE:
  906. return base+*pc;
  907. case SETFUNCTION:
  908. {
  909. CodeCode file;
  910. CodeWord func;
  911. get_code(file,pc);
  912. get_word(func,pc);
  913. lua_pushfunction ((char *)file.b, func.w);
  914. }
  915. break;
  916. case SETLINE:
  917. {
  918. CodeWord code;
  919. get_word(code,pc);
  920. lua_debugline = code.w;
  921. }
  922. break;
  923. case RESET:
  924. lua_popfunction ();
  925. break;
  926. default:
  927. lua_error ("internal error - opcode doesn't match");
  928. }
  929. }
  930. }