opcode.c 21 KB

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