opcode.c 21 KB

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