opcode.c 21 KB

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