opcode.c 24 KB

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