opcode.c 22 KB

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