opcode.c 22 KB

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