opcode.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. ** opcode.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_opcode="$Id: opcode.c,v 3.52 1996/01/09 20:22:44 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. /*
  321. ** Traverse all objects on stack
  322. */
  323. void lua_travstack (int (*fn)(Object *))
  324. {
  325. Object *o;
  326. for (o = top-1; o >= stack; o--)
  327. fn (o);
  328. }
  329. /*
  330. ** Error messages and debug functions
  331. */
  332. static void lua_message (char *s)
  333. {
  334. lua_pushstring(s);
  335. callFB(FB_ERROR);
  336. }
  337. /*
  338. ** Reports an error, and jumps up to the available recover label
  339. */
  340. void lua_error (char *s)
  341. {
  342. if (s) lua_message(s);
  343. if (errorJmp)
  344. longjmp(*errorJmp, 1);
  345. else
  346. {
  347. fprintf (stderr, "lua: exit(1). Unable to recover\n");
  348. exit(1);
  349. }
  350. }
  351. lua_Object lua_stackedfunction (int level)
  352. {
  353. Object *p = top;
  354. while (--p >= stack)
  355. if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)
  356. if (level-- == 0)
  357. return Ref(p);
  358. return LUA_NOOBJECT;
  359. }
  360. int lua_currentline (lua_Object func)
  361. {
  362. Object *f = Address(func);
  363. return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
  364. }
  365. /*
  366. ** Execute a protected call. Assumes that function is at CBase and
  367. ** parameters are on top of it. Leave nResults on the stack.
  368. */
  369. static int do_protectedrun (int nResults)
  370. {
  371. jmp_buf myErrorJmp;
  372. int status;
  373. StkId oldCBase = CBase;
  374. jmp_buf *oldErr = errorJmp;
  375. errorJmp = &myErrorJmp;
  376. if (setjmp(myErrorJmp) == 0)
  377. {
  378. do_call(CBase+1, nResults);
  379. CnResults = (top-stack) - CBase; /* number of results */
  380. CBase += CnResults; /* incorporate results on the stack */
  381. status = 0;
  382. }
  383. else
  384. { /* an error occurred: restore CBase and top */
  385. CBase = oldCBase;
  386. top = stack+CBase;
  387. status = 1;
  388. }
  389. errorJmp = oldErr;
  390. return status;
  391. }
  392. static int do_protectedmain (void)
  393. {
  394. TFunc tf;
  395. int status;
  396. jmp_buf myErrorJmp;
  397. jmp_buf *oldErr = errorJmp;
  398. errorJmp = &myErrorJmp;
  399. adjustC(1); /* one slot for the pseudo-function */
  400. stack[CBase].tag = LUA_T_FUNCTION;
  401. stack[CBase].value.tf = &tf;
  402. tf.lineDefined = 0;
  403. tf.fileName = lua_parsedfile;
  404. tf.code = NULL;
  405. if (setjmp(myErrorJmp) == 0)
  406. {
  407. lua_parse(&tf);
  408. status = do_protectedrun(0);
  409. }
  410. else
  411. {
  412. status = 1;
  413. adjustC(0); /* erase extra slot */
  414. }
  415. errorJmp = oldErr;
  416. if (tf.code)
  417. luaI_free(tf.code);
  418. return status;
  419. }
  420. /*
  421. ** Execute the given lua function. Return 0 on success or 1 on error.
  422. */
  423. int lua_callfunction (lua_Object function)
  424. {
  425. if (function == LUA_NOOBJECT)
  426. return 1;
  427. else
  428. {
  429. open_stack((top-stack)-CBase);
  430. stack[CBase] = *Address(function);
  431. return do_protectedrun (MULT_RET);
  432. }
  433. }
  434. int lua_call (char *funcname)
  435. {
  436. Word n = luaI_findsymbolbyname(funcname);
  437. open_stack((top-stack)-CBase);
  438. stack[CBase] = s_object(n);
  439. return do_protectedrun(MULT_RET);
  440. }
  441. /*
  442. ** Open file, generate opcode and execute global statement. Return 0 on
  443. ** success or 1 on error.
  444. */
  445. int lua_dofile (char *filename)
  446. {
  447. int status;
  448. if (lua_openfile(filename))
  449. return 1;
  450. status = do_protectedmain();
  451. lua_closefile();
  452. return status;
  453. }
  454. /*
  455. ** Generate opcode stored on string and execute global statement. Return 0 on
  456. ** success or 1 on error.
  457. */
  458. int lua_dostring (char *string)
  459. {
  460. int status;
  461. lua_openstring(string);
  462. status = do_protectedmain();
  463. lua_closestring();
  464. return status;
  465. }
  466. /*
  467. ** API: set a function as a fallback
  468. */
  469. lua_Object lua_setfallback (char *name, lua_CFunction fallback)
  470. {
  471. adjustC(1); /* one slot for the pseudo-function */
  472. stack[CBase].tag = LUA_T_CFUNCTION;
  473. stack[CBase].value.f = luaI_setfallback;
  474. lua_pushstring(name);
  475. lua_pushcfunction(fallback);
  476. if (do_protectedrun(1) == 0)
  477. return (Ref(top-1));
  478. else
  479. return LUA_NOOBJECT;
  480. }
  481. /*
  482. ** API: receives on the stack the table and the index.
  483. ** returns the value.
  484. */
  485. lua_Object lua_getsubscript (void)
  486. {
  487. adjustC(2);
  488. pushsubscript();
  489. CBase++; /* incorporate object in the stack */
  490. return (Ref(top-1));
  491. }
  492. #define MAX_C_BLOCKS 10
  493. static int numCblocks = 0;
  494. static StkId Cblocks[MAX_C_BLOCKS];
  495. /*
  496. ** API: starts a new block
  497. */
  498. void lua_beginblock (void)
  499. {
  500. if (numCblocks < MAX_C_BLOCKS)
  501. Cblocks[numCblocks] = CBase;
  502. numCblocks++;
  503. }
  504. /*
  505. ** API: ends a block
  506. */
  507. void lua_endblock (void)
  508. {
  509. --numCblocks;
  510. if (numCblocks < MAX_C_BLOCKS)
  511. {
  512. CBase = Cblocks[numCblocks];
  513. adjustC(0);
  514. }
  515. }
  516. /*
  517. ** API: receives on the stack the table, the index, and the new value.
  518. */
  519. void lua_storesubscript (void)
  520. {
  521. adjustC(3);
  522. storesubscript();
  523. }
  524. /*
  525. ** API: creates a new table
  526. */
  527. lua_Object lua_createtable (void)
  528. {
  529. adjustC(0);
  530. avalue(top) = lua_createarray(0);
  531. tag(top) = LUA_T_ARRAY;
  532. incr_top;
  533. CBase++; /* incorporate object in the stack */
  534. return Ref(top-1);
  535. }
  536. /*
  537. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  538. ** 'number' must be 1 to get the first parameter.
  539. */
  540. lua_Object lua_getparam (int number)
  541. {
  542. if (number <= 0 || number > CnResults) return LUA_NOOBJECT;
  543. /* Ref(stack+(CBase-CnResults+number-1)) ==
  544. stack+(CBase-CnResults+number-1)-stack+1 == */
  545. return CBase-CnResults+number;
  546. }
  547. /*
  548. ** Given an object handle, return its number value. On error, return 0.0.
  549. */
  550. real lua_getnumber (lua_Object object)
  551. {
  552. if (object == LUA_NOOBJECT) return 0.0;
  553. if (tonumber (Address(object))) return 0.0;
  554. else return (nvalue(Address(object)));
  555. }
  556. /*
  557. ** Given an object handle, return its string pointer. On error, return NULL.
  558. */
  559. char *lua_getstring (lua_Object object)
  560. {
  561. if (object == LUA_NOOBJECT) return NULL;
  562. if (tostring (Address(object))) return NULL;
  563. else return (svalue(Address(object)));
  564. }
  565. /*
  566. ** Given an object handle, return its cfuntion pointer. On error, return NULL.
  567. */
  568. lua_CFunction lua_getcfunction (lua_Object object)
  569. {
  570. if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_CFUNCTION)
  571. return NULL;
  572. else return (fvalue(Address(object)));
  573. }
  574. /*
  575. ** Given an object handle, return its user data. On error, return NULL.
  576. */
  577. void *lua_getuserdata (lua_Object object)
  578. {
  579. if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
  580. return NULL;
  581. else return (uvalue(Address(object)));
  582. }
  583. lua_Object lua_getlocked (int ref)
  584. {
  585. adjustC(0);
  586. *top = *luaI_getlocked(ref);
  587. incr_top;
  588. CBase++; /* incorporate object in the stack */
  589. return Ref(top-1);
  590. }
  591. void lua_pushlocked (int ref)
  592. {
  593. *top = *luaI_getlocked(ref);
  594. incr_top;
  595. }
  596. int lua_lock (void)
  597. {
  598. adjustC(1);
  599. return luaI_lock(--top);
  600. }
  601. /*
  602. ** Get a global object.
  603. */
  604. lua_Object lua_getglobal (char *name)
  605. {
  606. Word n = luaI_findsymbolbyname(name);
  607. adjustC(0);
  608. *top = s_object(n);
  609. incr_top;
  610. CBase++; /* incorporate object in the stack */
  611. return Ref(top-1);
  612. }
  613. /*
  614. ** Store top of the stack at a global variable array field.
  615. */
  616. void lua_storeglobal (char *name)
  617. {
  618. Word n = luaI_findsymbolbyname(name);
  619. adjustC(1);
  620. s_object(n) = *(--top);
  621. }
  622. /*
  623. ** Push a nil object
  624. */
  625. void lua_pushnil (void)
  626. {
  627. tag(top) = LUA_T_NIL;
  628. incr_top;
  629. }
  630. /*
  631. ** Push an object (tag=number) to stack.
  632. */
  633. void lua_pushnumber (real n)
  634. {
  635. tag(top) = LUA_T_NUMBER; nvalue(top) = n;
  636. incr_top;
  637. }
  638. /*
  639. ** Push an object (tag=string) to stack.
  640. */
  641. void lua_pushstring (char *s)
  642. {
  643. tsvalue(top) = lua_createstring(s);
  644. tag(top) = LUA_T_STRING;
  645. incr_top;
  646. }
  647. /*
  648. ** Push an object (tag=string) on stack and register it on the constant table.
  649. */
  650. void lua_pushliteral (char *s)
  651. {
  652. Word ct = luaI_findconstantbyname(s); /* this call may change lua_constant */
  653. tsvalue(top) = lua_constant[ct];
  654. tag(top) = LUA_T_STRING;
  655. incr_top;
  656. }
  657. /*
  658. ** Push an object (tag=cfunction) to stack.
  659. */
  660. void lua_pushcfunction (lua_CFunction fn)
  661. {
  662. tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
  663. incr_top;
  664. }
  665. /*
  666. ** Push an object (tag=userdata) to stack.
  667. */
  668. void lua_pushusertag (void *u, int tag)
  669. {
  670. if (tag < LUA_T_USERDATA) return;
  671. tag(top) = tag; uvalue(top) = u;
  672. incr_top;
  673. }
  674. /*
  675. ** Push a lua_Object to stack.
  676. */
  677. void lua_pushobject (lua_Object o)
  678. {
  679. *top = *Address(o);
  680. incr_top;
  681. }
  682. /*
  683. ** Push an object on the stack.
  684. */
  685. void luaI_pushobject (Object *o)
  686. {
  687. *top = *o;
  688. incr_top;
  689. }
  690. int lua_type (lua_Object o)
  691. {
  692. if (o == LUA_NOOBJECT)
  693. return LUA_T_NIL;
  694. else
  695. return tag(Address(o));
  696. }
  697. void luaI_gcFB (Object *o)
  698. {
  699. *top = *o;
  700. incr_top;
  701. callFB(FB_GC);
  702. }
  703. static void call_arith (char *op)
  704. {
  705. lua_pushstring(op);
  706. callFB(FB_ARITH);
  707. }
  708. static void comparison (lua_Type tag_less, lua_Type tag_equal,
  709. lua_Type tag_great, char *op)
  710. {
  711. Object *l = top-2;
  712. Object *r = top-1;
  713. int result;
  714. if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
  715. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  716. else if (tostring(l) || tostring(r))
  717. {
  718. lua_pushstring(op);
  719. callFB(FB_ORDER);
  720. return;
  721. }
  722. else
  723. result = strcmp(svalue(l), svalue(r));
  724. top--;
  725. nvalue(top-1) = 1;
  726. tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
  727. }
  728. /*
  729. ** Execute the given opcode, until a RET. Parameters are between
  730. ** [stack+base,top). Returns n such that the the results are between
  731. ** [stack+n,top).
  732. */
  733. static StkId lua_execute (Byte *pc, StkId base)
  734. {
  735. if (call_hook)
  736. callHook (base, LUA_T_MARK, 0);
  737. while (1)
  738. {
  739. OpCode opcode;
  740. switch (opcode = (OpCode)*pc++)
  741. {
  742. case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
  743. case PUSH0: case PUSH1: case PUSH2:
  744. tag(top) = LUA_T_NUMBER;
  745. nvalue(top) = opcode-PUSH0;
  746. incr_top;
  747. break;
  748. case PUSHBYTE:
  749. tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
  750. case PUSHWORD:
  751. {
  752. CodeWord code;
  753. get_word(code,pc);
  754. tag(top) = LUA_T_NUMBER; nvalue(top) = code.w;
  755. incr_top;
  756. }
  757. break;
  758. case PUSHFLOAT:
  759. {
  760. CodeFloat code;
  761. get_float(code,pc);
  762. tag(top) = LUA_T_NUMBER; nvalue(top) = code.f;
  763. incr_top;
  764. }
  765. break;
  766. case PUSHSTRING:
  767. {
  768. CodeWord code;
  769. get_word(code,pc);
  770. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  771. incr_top;
  772. }
  773. break;
  774. case PUSHFUNCTION:
  775. {
  776. CodeCode code;
  777. get_code(code,pc);
  778. luaI_insertfunction(code.tf); /* may take part in GC */
  779. top->tag = LUA_T_FUNCTION;
  780. top->value.tf = code.tf;
  781. incr_top;
  782. }
  783. break;
  784. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
  785. case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
  786. case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
  787. case PUSHLOCAL9:
  788. *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
  789. case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
  790. case PUSHGLOBAL:
  791. {
  792. CodeWord code;
  793. get_word(code,pc);
  794. *top = s_object(code.w);
  795. incr_top;
  796. }
  797. break;
  798. case PUSHINDEXED:
  799. pushsubscript();
  800. break;
  801. case PUSHSELF:
  802. {
  803. Object receiver = *(top-1);
  804. CodeWord code;
  805. get_word(code,pc);
  806. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  807. incr_top;
  808. pushsubscript();
  809. *top = receiver;
  810. incr_top;
  811. break;
  812. }
  813. case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
  814. case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
  815. case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
  816. case STORELOCAL9:
  817. *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
  818. break;
  819. case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
  820. case STOREGLOBAL:
  821. {
  822. CodeWord code;
  823. get_word(code,pc);
  824. s_object(code.w) = *(--top);
  825. }
  826. break;
  827. case STOREINDEXED0:
  828. storesubscript();
  829. break;
  830. case STOREINDEXED:
  831. {
  832. int n = *pc++;
  833. if (tag(top-3-n) != LUA_T_ARRAY)
  834. {
  835. lua_checkstack(top+2);
  836. *(top+1) = *(top-1);
  837. *(top) = *(top-2-n);
  838. *(top-1) = *(top-3-n);
  839. top += 2;
  840. callFB(FB_SETTABLE);
  841. }
  842. else
  843. {
  844. Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
  845. *h = *(top-1);
  846. top--;
  847. }
  848. }
  849. break;
  850. case STORELIST0:
  851. case STORELIST:
  852. {
  853. int m, n;
  854. Object *arr;
  855. if (opcode == STORELIST0) m = 0;
  856. else m = *(pc++) * FIELDS_PER_FLUSH;
  857. n = *(pc++);
  858. arr = top-n-1;
  859. while (n)
  860. {
  861. tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
  862. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  863. top--;
  864. n--;
  865. }
  866. }
  867. break;
  868. case STORERECORD:
  869. {
  870. int n = *(pc++);
  871. Object *arr = top-n-1;
  872. while (n)
  873. {
  874. CodeWord code;
  875. get_word(code,pc);
  876. tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
  877. *(lua_hashdefine (avalue(arr), top)) = *(top-1);
  878. top--;
  879. n--;
  880. }
  881. }
  882. break;
  883. case ADJUST0:
  884. adjust_top(base);
  885. break;
  886. case ADJUST:
  887. adjust_top(base + *(pc++));
  888. break;
  889. case CREATEARRAY:
  890. {
  891. CodeWord size;
  892. get_word(size,pc);
  893. avalue(top) = lua_createarray(size.w);
  894. tag(top) = LUA_T_ARRAY;
  895. incr_top;
  896. }
  897. break;
  898. case EQOP:
  899. {
  900. int res = lua_equalObj(top-2, top-1);
  901. --top;
  902. tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  903. nvalue(top-1) = 1;
  904. }
  905. break;
  906. case LTOP:
  907. comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
  908. break;
  909. case LEOP:
  910. comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
  911. break;
  912. case GTOP:
  913. comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
  914. break;
  915. case GEOP:
  916. comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
  917. break;
  918. case ADDOP:
  919. {
  920. Object *l = top-2;
  921. Object *r = top-1;
  922. if (tonumber(r) || tonumber(l))
  923. call_arith("add");
  924. else
  925. {
  926. nvalue(l) += nvalue(r);
  927. --top;
  928. }
  929. }
  930. break;
  931. case SUBOP:
  932. {
  933. Object *l = top-2;
  934. Object *r = top-1;
  935. if (tonumber(r) || tonumber(l))
  936. call_arith("sub");
  937. else
  938. {
  939. nvalue(l) -= nvalue(r);
  940. --top;
  941. }
  942. }
  943. break;
  944. case MULTOP:
  945. {
  946. Object *l = top-2;
  947. Object *r = top-1;
  948. if (tonumber(r) || tonumber(l))
  949. call_arith("mul");
  950. else
  951. {
  952. nvalue(l) *= nvalue(r);
  953. --top;
  954. }
  955. }
  956. break;
  957. case DIVOP:
  958. {
  959. Object *l = top-2;
  960. Object *r = top-1;
  961. if (tonumber(r) || tonumber(l))
  962. call_arith("div");
  963. else
  964. {
  965. nvalue(l) /= nvalue(r);
  966. --top;
  967. }
  968. }
  969. break;
  970. case POWOP:
  971. call_arith("pow");
  972. break;
  973. case CONCOP:
  974. {
  975. Object *l = top-2;
  976. Object *r = top-1;
  977. if (tostring(r) || tostring(l))
  978. callFB(FB_CONCAT);
  979. else
  980. {
  981. tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
  982. --top;
  983. }
  984. }
  985. break;
  986. case MINUSOP:
  987. if (tonumber(top-1))
  988. {
  989. tag(top) = LUA_T_NIL;
  990. incr_top;
  991. call_arith("unm");
  992. }
  993. else
  994. nvalue(top-1) = - nvalue(top-1);
  995. break;
  996. case NOTOP:
  997. tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  998. nvalue(top-1) = 1;
  999. break;
  1000. case ONTJMP:
  1001. {
  1002. CodeWord code;
  1003. get_word(code,pc);
  1004. if (tag(top-1) != LUA_T_NIL) pc += code.w;
  1005. }
  1006. break;
  1007. case ONFJMP:
  1008. {
  1009. CodeWord code;
  1010. get_word(code,pc);
  1011. if (tag(top-1) == LUA_T_NIL) pc += code.w;
  1012. }
  1013. break;
  1014. case JMP:
  1015. {
  1016. CodeWord code;
  1017. get_word(code,pc);
  1018. pc += code.w;
  1019. }
  1020. break;
  1021. case UPJMP:
  1022. {
  1023. CodeWord code;
  1024. get_word(code,pc);
  1025. pc -= code.w;
  1026. }
  1027. break;
  1028. case IFFJMP:
  1029. {
  1030. CodeWord code;
  1031. get_word(code,pc);
  1032. top--;
  1033. if (tag(top) == LUA_T_NIL) pc += code.w;
  1034. }
  1035. break;
  1036. case IFFUPJMP:
  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 POP: --top; break;
  1045. case CALLFUNC:
  1046. {
  1047. int nParams = *(pc++);
  1048. int nResults = *(pc++);
  1049. StkId newBase = (top-stack)-nParams;
  1050. do_call(newBase, nResults);
  1051. }
  1052. break;
  1053. case RETCODE0:
  1054. case RETCODE:
  1055. if (call_hook)
  1056. callHook (base, LUA_T_MARK, 1);
  1057. return (base + ((opcode==RETCODE0) ? 0 : *pc));
  1058. case SETLINE:
  1059. {
  1060. CodeWord code;
  1061. get_word(code,pc);
  1062. if ((stack+base-1)->tag != LUA_T_LINE)
  1063. {
  1064. /* open space for LINE value */
  1065. open_stack((top-stack)-base);
  1066. base++;
  1067. (stack+base-1)->tag = LUA_T_LINE;
  1068. }
  1069. (stack+base-1)->value.i = code.w;
  1070. if (line_hook)
  1071. lineHook (code.w);
  1072. break;
  1073. }
  1074. default:
  1075. lua_error ("internal error - opcode doesn't match");
  1076. }
  1077. }
  1078. }