opcode.c 22 KB

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