opcode.c 21 KB

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