opcode.c 20 KB

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