opcode.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.26 1994/12/16 15:56:45 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. char c;
  112. if (tag(obj) != LUA_T_STRING)
  113. return 1;
  114. else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
  115. {
  116. nvalue(obj) = t;
  117. tag(obj) = LUA_T_NUMBER;
  118. return 0;
  119. }
  120. else
  121. return 2;
  122. }
  123. /*
  124. ** Convert, if possible, to a string tag
  125. ** Return 0 in success or not 0 on error.
  126. */
  127. static int lua_tostring (Object *obj)
  128. {
  129. static char s[256];
  130. if (tag(obj) != LUA_T_NUMBER)
  131. return 1;
  132. if ((int) nvalue(obj) == nvalue(obj))
  133. sprintf (s, "%d", (int) nvalue(obj));
  134. else
  135. sprintf (s, "%g", nvalue(obj));
  136. tsvalue(obj) = lua_createstring(s);
  137. if (tsvalue(obj) == NULL)
  138. return 1;
  139. tag(obj) = LUA_T_STRING;
  140. return 0;
  141. }
  142. /*
  143. ** Adjust stack. Set top to the given value, pushing NILs if needed.
  144. */
  145. static void adjust_top (int newtop)
  146. {
  147. Object *nt = stack+newtop;
  148. while (top < nt) tag(top++) = LUA_T_NIL;
  149. top = nt; /* top could be bigger than newtop */
  150. }
  151. static void adjustC (int nParams)
  152. {
  153. adjust_top(CBase+nParams);
  154. }
  155. /*
  156. ** Call a C function. CBase will point to the top of the stack,
  157. ** and CnResults is the number of parameters. Returns an index
  158. ** to the first result from C.
  159. */
  160. static int callC (lua_CFunction func, int base)
  161. {
  162. int oldBase = CBase;
  163. int oldCnResults = CnResults;
  164. int firstResult;
  165. CnResults = (top-stack) - base;
  166. /* incorporate parameters on the stack */
  167. CBase = base+CnResults;
  168. (*func)();
  169. firstResult = CBase;
  170. CBase = oldBase;
  171. CnResults = oldCnResults;
  172. return firstResult;
  173. }
  174. /*
  175. ** Call the fallback for invalid functions (see do_call)
  176. */
  177. static void call_funcFB (Object *func, int base, int nResults, int whereRes)
  178. {
  179. int i;
  180. /* open space for first parameter (func) */
  181. for (i=top-stack; i>base; i--)
  182. stack[i] = stack[i-1];
  183. top++;
  184. stack[base] = *func;
  185. do_call(&luaI_fallBacks[FB_FUNCTION].function, base, nResults, whereRes);
  186. }
  187. /*
  188. ** Call a function (C or Lua). The parameters must be on the stack,
  189. ** between [stack+base,top). When returns, the results are on the stack,
  190. ** between [stack+whereRes,top). The number of results is nResults, unless
  191. ** nResults=MULT_RET.
  192. */
  193. static void do_call (Object *func, int base, int nResults, int whereRes)
  194. {
  195. int firstResult;
  196. if (tag(func) == LUA_T_CFUNCTION)
  197. firstResult = callC(fvalue(func), base);
  198. else if (tag(func) == LUA_T_FUNCTION)
  199. firstResult = lua_execute(bvalue(func), base);
  200. else
  201. { /* func is not a function */
  202. call_funcFB(func, base, nResults, whereRes);
  203. return;
  204. }
  205. /* adjust the number of results */
  206. if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
  207. adjust_top(firstResult+nResults);
  208. /* move results to the given position */
  209. if (firstResult != whereRes)
  210. {
  211. int i;
  212. nResults = top - (stack+firstResult); /* actual number of results */
  213. for (i=0; i<nResults; i++)
  214. *(stack+whereRes+i) = *(stack+firstResult+i);
  215. top -= firstResult-whereRes;
  216. }
  217. }
  218. /*
  219. ** Function to index a table. Receives the table at top-2 and the index
  220. ** at top-1.
  221. */
  222. static void pushsubscript (void)
  223. {
  224. if (tag(top-2) != LUA_T_ARRAY)
  225. do_call(&luaI_fallBacks[FB_GETTABLE].function, (top-stack)-2, 1, (top-stack)-2);
  226. else
  227. {
  228. Object *h = lua_hashget(avalue(top-2), top-1);
  229. if (h == NULL || tag(h) == LUA_T_NIL)
  230. do_call(&luaI_fallBacks[FB_INDEX].function, (top-stack)-2, 1, (top-stack)-2);
  231. else
  232. {
  233. --top;
  234. *(top-1) = *h;
  235. }
  236. }
  237. }
  238. /*
  239. ** Function to store indexed based on values at the top
  240. */
  241. static void storesubscript (void)
  242. {
  243. if (tag(top-3) != LUA_T_ARRAY)
  244. do_call(&luaI_fallBacks[FB_SETTABLE].function, (top-stack)-3, 0, (top-stack)-3);
  245. else
  246. {
  247. Object *h = lua_hashdefine (avalue(top-3), top-2);
  248. *h = *(top-1);
  249. top -= 3;
  250. }
  251. }
  252. /*
  253. ** Traverse all objects on stack
  254. */
  255. void lua_travstack (void (*fn)(Object *))
  256. {
  257. Object *o;
  258. for (o = top-1; o >= stack; o--)
  259. fn (o);
  260. }
  261. /*
  262. ** Execute a protected call. If function is null compiles the pre-set input.
  263. ** Leave nResults on the stack.
  264. */
  265. static int do_protectedrun (Object *function, int nResults)
  266. {
  267. jmp_buf myErrorJmp;
  268. int status;
  269. int oldCBase = CBase;
  270. jmp_buf *oldErr = errorJmp;
  271. errorJmp = &myErrorJmp;
  272. if (setjmp(myErrorJmp) == 0)
  273. {
  274. do_call(function, CBase, nResults, CBase);
  275. CnResults = (top-stack) - CBase; /* number of results */
  276. CBase += CnResults; /* incorporate results on the stack */
  277. status = 0;
  278. }
  279. else
  280. {
  281. CBase = oldCBase;
  282. top = stack+CBase;
  283. status = 1;
  284. }
  285. errorJmp = oldErr;
  286. return status;
  287. }
  288. static int do_protectedmain (void)
  289. {
  290. Byte *code = NULL;
  291. int status;
  292. int oldCBase = CBase;
  293. jmp_buf myErrorJmp;
  294. jmp_buf *oldErr = errorJmp;
  295. errorJmp = &myErrorJmp;
  296. if (setjmp(myErrorJmp) == 0)
  297. {
  298. Object f;
  299. lua_parse(&code);
  300. tag(&f) = LUA_T_FUNCTION; bvalue(&f) = code;
  301. do_call(&f, CBase, 0, CBase);
  302. status = 0;
  303. }
  304. else
  305. status = 1;
  306. if (code)
  307. luaI_free(code);
  308. errorJmp = oldErr;
  309. CBase = oldCBase;
  310. top = stack+CBase;
  311. return status;
  312. }
  313. /*
  314. ** Execute the given lua function. Return 0 on success or 1 on error.
  315. */
  316. int lua_callfunction (lua_Object function)
  317. {
  318. if (function == NULL)
  319. return 1;
  320. else
  321. return do_protectedrun (Address(function), MULT_RET);
  322. }
  323. int lua_call (char *funcname)
  324. {
  325. int n = luaI_findsymbolbyname(funcname);
  326. return do_protectedrun(&s_object(n), MULT_RET);
  327. }
  328. /*
  329. ** Open file, generate opcode and execute global statement. Return 0 on
  330. ** success or 1 on error.
  331. */
  332. int lua_dofile (char *filename)
  333. {
  334. int status;
  335. char *message = lua_openfile (filename);
  336. if (message)
  337. {
  338. lua_message(message);
  339. return 1;
  340. }
  341. status = do_protectedmain();
  342. lua_closefile();
  343. return status;
  344. }
  345. /*
  346. ** Generate opcode stored on string and execute global statement. Return 0 on
  347. ** success or 1 on error.
  348. */
  349. int lua_dostring (char *string)
  350. {
  351. int status;
  352. char *message = lua_openstring(string);
  353. if (message)
  354. {
  355. lua_message(message);
  356. return 1;
  357. }
  358. status = do_protectedmain();
  359. lua_closestring();
  360. return status;
  361. }
  362. /*
  363. ** API: set a function as a fallback
  364. */
  365. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  366. {
  367. static Object func = {LUA_T_CFUNCTION, luaI_setfallback};
  368. adjustC(0);
  369. lua_pushstring(name);
  370. lua_pushcfunction(fallback);
  371. do_protectedrun(&func, 1);
  372. return (Ref(top-1));
  373. }
  374. /*
  375. ** API: receives on the stack the table and the index.
  376. ** returns the value.
  377. */
  378. lua_Object lua_getsubscript (void)
  379. {
  380. static Byte code[2] = {PUSHINDEXED, RETCODE0};
  381. int status;
  382. Object func;
  383. tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
  384. adjustC(2);
  385. status = do_protectedrun(&func, 1);
  386. if (status == 0)
  387. return (Ref(top-1));
  388. else
  389. return LUA_NOOBJECT;
  390. }
  391. #define MAX_C_BLOCKS 10
  392. static int numCblocks = 0;
  393. static int Cblocks[MAX_C_BLOCKS];
  394. /*
  395. ** API: starts a new block
  396. */
  397. void lua_beginblock (void)
  398. {
  399. if (numCblocks < MAX_C_BLOCKS)
  400. Cblocks[numCblocks] = CBase;
  401. numCblocks++;
  402. }
  403. /*
  404. ** API: ends a block
  405. */
  406. void lua_endblock (void)
  407. {
  408. --numCblocks;
  409. if (numCblocks < MAX_C_BLOCKS)
  410. {
  411. CBase = Cblocks[numCblocks];
  412. adjustC(0);
  413. }
  414. }
  415. /*
  416. ** API: receives on the stack the table, the index, and the new value.
  417. */
  418. int lua_storesubscript (void)
  419. {
  420. static Byte code[2] = {STOREINDEXED0, RETCODE0};
  421. Object func;
  422. tag(&func) = LUA_T_FUNCTION; bvalue(&func) = code;
  423. adjustC(3);
  424. return(do_protectedrun(&func, 0));
  425. }
  426. /*
  427. ** API: creates a new table
  428. */
  429. lua_Object lua_createtable (void)
  430. {
  431. adjustC(0);
  432. avalue(top) = lua_createarray(0);
  433. tag(top) = LUA_T_ARRAY;
  434. top++;
  435. CBase++; /* incorporate object in the stack */
  436. return Ref(top-1);
  437. }
  438. /*
  439. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  440. ** 'number' must be 1 to get the first parameter.
  441. */
  442. lua_Object lua_getparam (int number)
  443. {
  444. if (number <= 0 || number > CnResults) return LUA_NOOBJECT;
  445. /* Ref(stack+(CBase-CnResults+number-1)) ==
  446. stack+(CBase-CnResults+number-1)-stack+1 == */
  447. return CBase-CnResults+number;
  448. }
  449. /*
  450. ** Given an object handle, return its number value. On error, return 0.0.
  451. */
  452. real lua_getnumber (lua_Object object)
  453. {
  454. if (object == LUA_NOOBJECT || tag(Address(object)) == LUA_T_NIL) return 0.0;
  455. if (tonumber (Address(object))) return 0.0;
  456. else return (nvalue(Address(object)));
  457. }
  458. /*
  459. ** Given an object handle, return its string pointer. On error, return NULL.
  460. */
  461. char *lua_getstring (lua_Object object)
  462. {
  463. if (object == LUA_NOOBJECT || tag(Address(object)) == LUA_T_NIL) return NULL;
  464. if (tostring (Address(object))) return NULL;
  465. else return (svalue(Address(object)));
  466. }
  467. /*
  468. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  469. */
  470. lua_CFunction lua_getcfunction (lua_Object object)
  471. {
  472. if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_CFUNCTION)
  473. return NULL;
  474. else return (fvalue(Address(object)));
  475. }
  476. /*
  477. ** Given an object handle, return its user data. On error, return NULL.
  478. */
  479. void *lua_getuserdata (lua_Object object)
  480. {
  481. if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
  482. return NULL;
  483. else return (uvalue(Address(object)));
  484. }
  485. lua_Object lua_getlocked (int ref)
  486. {
  487. adjustC(0);
  488. *top = *luaI_getlocked(ref);
  489. top++;
  490. CBase++; /* incorporate object in the stack */
  491. return Ref(top-1);
  492. }
  493. int lua_lock (void)
  494. {
  495. adjustC(1);
  496. return luaI_lock(--top);
  497. }
  498. /*
  499. ** Get a global object. Return the object handle or NULL on error.
  500. */
  501. lua_Object lua_getglobal (char *name)
  502. {
  503. int n = luaI_findsymbolbyname(name);
  504. adjustC(0);
  505. *top = s_object(n);
  506. top++;
  507. CBase++; /* incorporate object in the stack */
  508. return Ref(top-1);
  509. }
  510. /*
  511. ** Store top of the stack at a global variable array field.
  512. ** Return 1 on error, 0 on success.
  513. */
  514. int lua_storeglobal (char *name)
  515. {
  516. int n = luaI_findsymbolbyname(name);
  517. if (n < 0) return 1;
  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 int lua_execute (Byte *pc, int 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. int 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. }