opcode.c 22 KB

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