opcode.c 22 KB

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