opcode.c 21 KB

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