opcode.c 24 KB

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