opcode.c 21 KB

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