opcode.c 21 KB

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