opcode.c 21 KB

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