opcode.c 22 KB

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