opcode.c 22 KB

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