opcode.c 21 KB

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