opcode.c 22 KB

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