opcode.c 22 KB

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