opcode.c 22 KB

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