opcode.c 21 KB

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