Imported from ../lua-2.2.tar.gz.
[lua.git] / src / opcode.c
blobe4089f013338c910bec8e63380f3e6a41738321a
1 /*
2 ** opcode.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_opcode="$Id: opcode.c,v 3.50 1995/11/16 20:46:24 roberto Exp $";
8 #include <setjmp.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
13 #include "luadebug.h"
14 #include "mem.h"
15 #include "opcode.h"
16 #include "hash.h"
17 #include "inout.h"
18 #include "table.h"
19 #include "lua.h"
20 #include "fallback.h"
22 #define tonumber(o) ((tag(o) != LUA_T_NUMBER) && (lua_tonumber(o) != 0))
23 #define tostring(o) ((tag(o) != LUA_T_STRING) && (lua_tostring(o) != 0))
26 #define STACK_SIZE 128
28 typedef int StkId; /* index to stack elements */
30 static Object initial_stack;
32 static Object *stackLimit = &initial_stack+1;
33 static Object *stack = &initial_stack;
34 static Object *top = &initial_stack;
37 /* macros to convert from lua_Object to (Object *) and back */
39 #define Address(lo) ((lo)+stack-1)
40 #define Ref(st) ((st)-stack+1)
43 /* macro to increment stack top. There must be always an empty slot in
44 * the stack
46 #define incr_top if (++top >= stackLimit) growstack()
48 static StkId CBase = 0; /* when Lua calls C or C calls Lua, points to */
49 /* the first slot after the last parameter. */
50 static int CnResults = 0; /* when Lua calls C, has the number of parameters; */
51 /* when C calls Lua, has the number of results. */
53 static jmp_buf *errorJmp = NULL; /* current error recover point */
56 static StkId lua_execute (Byte *pc, StkId base);
57 static void do_call (StkId base, int nResults);
61 Object *luaI_Address (lua_Object o)
63 return Address(o);
69 ** Init stack
71 static void lua_initstack (void)
73 Long maxstack = STACK_SIZE;
74 stack = newvector(maxstack, Object);
75 stackLimit = stack+maxstack;
76 top = stack;
77 *(top++) = initial_stack;
82 ** Check stack overflow and, if necessary, realloc vector
84 #define lua_checkstack(nt) if ((nt) >= stackLimit) growstack()
86 static void growstack (void)
88 if (stack == &initial_stack)
89 lua_initstack();
90 else
92 StkId t = top-stack;
93 Long maxstack = stackLimit - stack;
94 maxstack *= 2;
95 stack = growvector(stack, maxstack, Object);
96 stackLimit = stack+maxstack;
97 top = stack + t;
98 if (maxstack >= MAX_WORD/2)
99 lua_error("stack size overflow");
105 ** Concatenate two given strings. Return the new string pointer.
107 static char *lua_strconc (char *l, char *r)
109 static char *buffer = NULL;
110 static int buffer_size = 0;
111 int nl = strlen(l);
112 int n = nl+strlen(r)+1;
113 if (n > buffer_size)
115 buffer_size = n;
116 if (buffer != NULL)
117 luaI_free(buffer);
118 buffer = newvector(buffer_size, char);
120 strcpy(buffer,l);
121 strcpy(buffer+nl, r);
122 return buffer;
127 ** Convert, if possible, to a number object.
128 ** Return 0 if success, not 0 if error.
130 static int lua_tonumber (Object *obj)
132 float t;
133 char c;
134 if (tag(obj) != LUA_T_STRING)
135 return 1;
136 else if (sscanf(svalue(obj), "%f %c",&t, &c) == 1)
138 nvalue(obj) = t;
139 tag(obj) = LUA_T_NUMBER;
140 return 0;
142 else
143 return 2;
148 ** Convert, if possible, to a string tag
149 ** Return 0 in success or not 0 on error.
151 static int lua_tostring (Object *obj)
153 static char s[256];
154 if (tag(obj) != LUA_T_NUMBER)
155 return 1;
156 if ((int) nvalue(obj) == nvalue(obj))
157 sprintf (s, "%d", (int) nvalue(obj));
158 else
159 sprintf (s, "%g", nvalue(obj));
160 tsvalue(obj) = lua_createstring(s);
161 if (tsvalue(obj) == NULL)
162 return 1;
163 tag(obj) = LUA_T_STRING;
164 return 0;
169 ** Adjust stack. Set top to the given value, pushing NILs if needed.
171 static void adjust_top (StkId newtop)
173 Object *nt;
174 lua_checkstack(stack+newtop);
175 nt = stack+newtop; /* warning: previous call may change stack */
176 while (top < nt) tag(top++) = LUA_T_NIL;
177 top = nt; /* top could be bigger than newtop */
180 #define adjustC(nParams) adjust_top(CBase+nParams)
184 ** Open a hole below "nelems" from the top.
186 static void open_stack (int nelems)
188 int i;
189 for (i=0; i<nelems; i++)
190 *(top-i) = *(top-i-1);
191 incr_top;
196 ** Call a C function. CBase will point to the top of the stack,
197 ** and CnResults is the number of parameters. Returns an index
198 ** to the first result from C.
200 static StkId callC (lua_CFunction func, StkId base)
202 StkId oldBase = CBase;
203 int oldCnResults = CnResults;
204 StkId firstResult;
205 CnResults = (top-stack) - base;
206 /* incorporate parameters on the stack */
207 CBase = base+CnResults;
208 (*func)();
209 firstResult = CBase;
210 CBase = oldBase;
211 CnResults = oldCnResults;
212 return firstResult;
216 ** Call the specified fallback, putting it on the stack below its arguments
218 static void callFB (int fb)
220 int nParams = luaI_fallBacks[fb].nParams;
221 open_stack(nParams);
222 *(top-nParams-1) = luaI_fallBacks[fb].function;
223 do_call((top-stack)-nParams, luaI_fallBacks[fb].nResults);
228 ** Call a function (C or Lua). The parameters must be on the stack,
229 ** between [stack+base,top). The function to be called is at stack+base-1.
230 ** When returns, the results are on the stack, between [stack+base-1,top).
231 ** The number of results is nResults, unless nResults=MULT_RET.
233 static void do_call (StkId base, int nResults)
235 StkId firstResult;
236 Object *func = stack+base-1;
237 int i;
238 if (tag(func) == LUA_T_CFUNCTION)
240 tag(func) = LUA_T_CMARK;
241 firstResult = callC(fvalue(func), base);
243 else if (tag(func) == LUA_T_FUNCTION)
245 tag(func) = LUA_T_MARK;
246 firstResult = lua_execute(func->value.tf->code, base);
248 else
249 { /* func is not a function */
250 /* Call the fallback for invalid functions */
251 open_stack((top-stack)-(base-1));
252 stack[base-1] = luaI_fallBacks[FB_FUNCTION].function;
253 do_call(base, nResults);
254 return;
256 /* adjust the number of results */
257 if (nResults != MULT_RET && top - (stack+firstResult) != nResults)
258 adjust_top(firstResult+nResults);
259 /* move results to base-1 (to erase parameters and function) */
260 base--;
261 nResults = top - (stack+firstResult); /* actual number of results */
262 for (i=0; i<nResults; i++)
263 *(stack+base+i) = *(stack+firstResult+i);
264 top -= firstResult-base;
269 ** Function to index a table. Receives the table at top-2 and the index
270 ** at top-1.
272 static void pushsubscript (void)
274 if (tag(top-2) != LUA_T_ARRAY)
275 callFB(FB_GETTABLE);
276 else
278 Object *h = lua_hashget(avalue(top-2), top-1);
279 if (h == NULL || tag(h) == LUA_T_NIL)
280 callFB(FB_INDEX);
281 else
283 --top;
284 *(top-1) = *h;
291 ** Function to store indexed based on values at the top
293 static void storesubscript (void)
295 if (tag(top-3) != LUA_T_ARRAY)
296 callFB(FB_SETTABLE);
297 else
299 Object *h = lua_hashdefine (avalue(top-3), top-2);
300 *h = *(top-1);
301 top -= 3;
307 ** Traverse all objects on stack
309 void lua_travstack (int (*fn)(Object *))
311 Object *o;
312 for (o = top-1; o >= stack; o--)
313 fn (o);
318 ** Error messages and debug functions
321 static void lua_message (char *s)
323 lua_pushstring(s);
324 callFB(FB_ERROR);
328 ** Reports an error, and jumps up to the available recover label
330 void lua_error (char *s)
332 if (s) lua_message(s);
333 if (errorJmp)
334 longjmp(*errorJmp, 1);
335 else
337 fprintf (stderr, "lua: exit(1). Unable to recover\n");
338 exit(1);
343 lua_Object lua_stackedfunction (int level)
345 Object *p = top;
346 while (--p >= stack)
347 if (p->tag == LUA_T_MARK || p->tag == LUA_T_CMARK)
348 if (level-- == 0)
349 return Ref(p);
350 return LUA_NOOBJECT;
354 int lua_currentline (lua_Object func)
356 Object *f = Address(func);
357 return (f+1 < top && (f+1)->tag == LUA_T_LINE) ? (f+1)->value.i : -1;
362 ** Execute a protected call. Assumes that function is at CBase and
363 ** parameters are on top of it. Leave nResults on the stack.
365 static int do_protectedrun (int nResults)
367 jmp_buf myErrorJmp;
368 int status;
369 StkId oldCBase = CBase;
370 jmp_buf *oldErr = errorJmp;
371 errorJmp = &myErrorJmp;
372 if (setjmp(myErrorJmp) == 0)
374 do_call(CBase+1, nResults);
375 CnResults = (top-stack) - CBase; /* number of results */
376 CBase += CnResults; /* incorporate results on the stack */
377 status = 0;
379 else
380 { /* an error occurred: restore CBase and top */
381 CBase = oldCBase;
382 top = stack+CBase;
383 status = 1;
385 errorJmp = oldErr;
386 return status;
390 static int do_protectedmain (void)
392 TFunc tf;
393 int status;
394 jmp_buf myErrorJmp;
395 jmp_buf *oldErr = errorJmp;
396 errorJmp = &myErrorJmp;
397 adjustC(1); /* one slot for the pseudo-function */
398 stack[CBase].tag = LUA_T_FUNCTION;
399 stack[CBase].value.tf = &tf;
400 tf.lineDefined = 0;
401 tf.fileName = lua_parsedfile;
402 tf.code = NULL;
403 if (setjmp(myErrorJmp) == 0)
405 lua_parse(&tf);
406 status = do_protectedrun(0);
408 else
410 status = 1;
411 adjustC(0); /* erase extra slot */
413 errorJmp = oldErr;
414 if (tf.code)
415 luaI_free(tf.code);
416 return status;
421 ** Execute the given lua function. Return 0 on success or 1 on error.
423 int lua_callfunction (lua_Object function)
425 if (function == LUA_NOOBJECT)
426 return 1;
427 else
429 open_stack((top-stack)-CBase);
430 stack[CBase] = *Address(function);
431 return do_protectedrun (MULT_RET);
436 int lua_call (char *funcname)
438 Word n = luaI_findsymbolbyname(funcname);
439 open_stack((top-stack)-CBase);
440 stack[CBase] = s_object(n);
441 return do_protectedrun(MULT_RET);
446 ** Open file, generate opcode and execute global statement. Return 0 on
447 ** success or 1 on error.
449 int lua_dofile (char *filename)
451 int status;
452 char *message = lua_openfile (filename);
453 if (message)
455 lua_message(message);
456 return 1;
458 status = do_protectedmain();
459 lua_closefile();
460 return status;
464 ** Generate opcode stored on string and execute global statement. Return 0 on
465 ** success or 1 on error.
467 int lua_dostring (char *string)
469 int status;
470 lua_openstring(string);
471 status = do_protectedmain();
472 lua_closestring();
473 return status;
478 ** API: set a function as a fallback
480 lua_Object lua_setfallback (char *name, lua_CFunction fallback)
482 adjustC(1); /* one slot for the pseudo-function */
483 stack[CBase].tag = LUA_T_CFUNCTION;
484 stack[CBase].value.f = luaI_setfallback;
485 lua_pushstring(name);
486 lua_pushcfunction(fallback);
487 if (do_protectedrun(1) == 0)
488 return (Ref(top-1));
489 else
490 return LUA_NOOBJECT;
495 ** API: receives on the stack the table and the index.
496 ** returns the value.
498 lua_Object lua_getsubscript (void)
500 adjustC(2);
501 pushsubscript();
502 CBase++; /* incorporate object in the stack */
503 return (Ref(top-1));
507 #define MAX_C_BLOCKS 10
509 static int numCblocks = 0;
510 static StkId Cblocks[MAX_C_BLOCKS];
513 ** API: starts a new block
515 void lua_beginblock (void)
517 if (numCblocks < MAX_C_BLOCKS)
518 Cblocks[numCblocks] = CBase;
519 numCblocks++;
523 ** API: ends a block
525 void lua_endblock (void)
527 --numCblocks;
528 if (numCblocks < MAX_C_BLOCKS)
530 CBase = Cblocks[numCblocks];
531 adjustC(0);
536 ** API: receives on the stack the table, the index, and the new value.
538 void lua_storesubscript (void)
540 adjustC(3);
541 storesubscript();
545 ** API: creates a new table
547 lua_Object lua_createtable (void)
549 adjustC(0);
550 avalue(top) = lua_createarray(0);
551 tag(top) = LUA_T_ARRAY;
552 incr_top;
553 CBase++; /* incorporate object in the stack */
554 return Ref(top-1);
558 ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
559 ** 'number' must be 1 to get the first parameter.
561 lua_Object lua_getparam (int number)
563 if (number <= 0 || number > CnResults) return LUA_NOOBJECT;
564 /* Ref(stack+(CBase-CnResults+number-1)) ==
565 stack+(CBase-CnResults+number-1)-stack+1 == */
566 return CBase-CnResults+number;
570 ** Given an object handle, return its number value. On error, return 0.0.
572 real lua_getnumber (lua_Object object)
574 if (object == LUA_NOOBJECT) return 0.0;
575 if (tonumber (Address(object))) return 0.0;
576 else return (nvalue(Address(object)));
580 ** Given an object handle, return its string pointer. On error, return NULL.
582 char *lua_getstring (lua_Object object)
584 if (object == LUA_NOOBJECT) return NULL;
585 if (tostring (Address(object))) return NULL;
586 else return (svalue(Address(object)));
590 ** Given an object handle, return its cfuntion pointer. On error, return NULL.
592 lua_CFunction lua_getcfunction (lua_Object object)
594 if (object == LUA_NOOBJECT || tag(Address(object)) != LUA_T_CFUNCTION)
595 return NULL;
596 else return (fvalue(Address(object)));
600 ** Given an object handle, return its user data. On error, return NULL.
602 void *lua_getuserdata (lua_Object object)
604 if (object == LUA_NOOBJECT || tag(Address(object)) < LUA_T_USERDATA)
605 return NULL;
606 else return (uvalue(Address(object)));
610 lua_Object lua_getlocked (int ref)
612 adjustC(0);
613 *top = *luaI_getlocked(ref);
614 incr_top;
615 CBase++; /* incorporate object in the stack */
616 return Ref(top-1);
620 void lua_pushlocked (int ref)
622 *top = *luaI_getlocked(ref);
623 incr_top;
627 int lua_lock (void)
629 adjustC(1);
630 return luaI_lock(--top);
636 ** Get a global object.
638 lua_Object lua_getglobal (char *name)
640 Word n = luaI_findsymbolbyname(name);
641 adjustC(0);
642 *top = s_object(n);
643 incr_top;
644 CBase++; /* incorporate object in the stack */
645 return Ref(top-1);
649 ** Store top of the stack at a global variable array field.
651 void lua_storeglobal (char *name)
653 Word n = luaI_findsymbolbyname(name);
654 adjustC(1);
655 s_object(n) = *(--top);
659 ** Push a nil object
661 void lua_pushnil (void)
663 tag(top) = LUA_T_NIL;
664 incr_top;
668 ** Push an object (tag=number) to stack.
670 void lua_pushnumber (real n)
672 tag(top) = LUA_T_NUMBER; nvalue(top) = n;
673 incr_top;
677 ** Push an object (tag=string) to stack.
679 void lua_pushstring (char *s)
681 tsvalue(top) = lua_createstring(s);
682 tag(top) = LUA_T_STRING;
683 incr_top;
687 ** Push an object (tag=string) on stack and register it on the constant table.
689 void lua_pushliteral (char *s)
691 tsvalue(top) = lua_constant[luaI_findconstantbyname(s)];
692 tag(top) = LUA_T_STRING;
693 incr_top;
697 ** Push an object (tag=cfunction) to stack.
699 void lua_pushcfunction (lua_CFunction fn)
701 tag(top) = LUA_T_CFUNCTION; fvalue(top) = fn;
702 incr_top;
706 ** Push an object (tag=userdata) to stack.
708 void lua_pushusertag (void *u, int tag)
710 if (tag < LUA_T_USERDATA) return;
711 tag(top) = tag; uvalue(top) = u;
712 incr_top;
716 ** Push a lua_Object to stack.
718 void lua_pushobject (lua_Object o)
720 *top = *Address(o);
721 incr_top;
725 ** Push an object on the stack.
727 void luaI_pushobject (Object *o)
729 *top = *o;
730 incr_top;
733 int lua_type (lua_Object o)
735 if (o == LUA_NOOBJECT)
736 return LUA_T_NIL;
737 else
738 return tag(Address(o));
742 void luaI_gcFB (Object *o)
744 *top = *o;
745 incr_top;
746 callFB(FB_GC);
750 static void call_arith (char *op)
752 lua_pushstring(op);
753 callFB(FB_ARITH);
756 static void comparison (lua_Type tag_less, lua_Type tag_equal,
757 lua_Type tag_great, char *op)
759 Object *l = top-2;
760 Object *r = top-1;
761 int result;
762 if (tag(l) == LUA_T_NUMBER && tag(r) == LUA_T_NUMBER)
763 result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
764 else if (tostring(l) || tostring(r))
766 lua_pushstring(op);
767 callFB(FB_ORDER);
768 return;
770 else
771 result = strcmp(svalue(l), svalue(r));
772 top--;
773 nvalue(top-1) = 1;
774 tag(top-1) = (result < 0) ? tag_less : (result == 0) ? tag_equal : tag_great;
780 ** Execute the given opcode, until a RET. Parameters are between
781 ** [stack+base,top). Returns n such that the the results are between
782 ** [stack+n,top).
784 static StkId lua_execute (Byte *pc, StkId base)
786 while (1)
788 OpCode opcode;
789 switch (opcode = (OpCode)*pc++)
791 case PUSHNIL: tag(top) = LUA_T_NIL; incr_top; break;
793 case PUSH0: case PUSH1: case PUSH2:
794 tag(top) = LUA_T_NUMBER;
795 nvalue(top) = opcode-PUSH0;
796 incr_top;
797 break;
799 case PUSHBYTE:
800 tag(top) = LUA_T_NUMBER; nvalue(top) = *pc++; incr_top; break;
802 case PUSHWORD:
804 CodeWord code;
805 get_word(code,pc);
806 tag(top) = LUA_T_NUMBER; nvalue(top) = code.w;
807 incr_top;
809 break;
811 case PUSHFLOAT:
813 CodeFloat code;
814 get_float(code,pc);
815 tag(top) = LUA_T_NUMBER; nvalue(top) = code.f;
816 incr_top;
818 break;
820 case PUSHSTRING:
822 CodeWord code;
823 get_word(code,pc);
824 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
825 incr_top;
827 break;
829 case PUSHFUNCTION:
831 CodeCode code;
832 get_code(code,pc);
833 luaI_insertfunction(code.tf); /* may take part in GC */
834 top->tag = LUA_T_FUNCTION;
835 top->value.tf = code.tf;
836 incr_top;
838 break;
840 case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2:
841 case PUSHLOCAL3: case PUSHLOCAL4: case PUSHLOCAL5:
842 case PUSHLOCAL6: case PUSHLOCAL7: case PUSHLOCAL8:
843 case PUSHLOCAL9:
844 *top = *((stack+base) + (int)(opcode-PUSHLOCAL0)); incr_top; break;
846 case PUSHLOCAL: *top = *((stack+base) + (*pc++)); incr_top; break;
848 case PUSHGLOBAL:
850 CodeWord code;
851 get_word(code,pc);
852 *top = s_object(code.w);
853 incr_top;
855 break;
857 case PUSHINDEXED:
858 pushsubscript();
859 break;
861 case PUSHSELF:
863 Object receiver = *(top-1);
864 CodeWord code;
865 get_word(code,pc);
866 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
867 incr_top;
868 pushsubscript();
869 *top = receiver;
870 incr_top;
871 break;
874 case STORELOCAL0: case STORELOCAL1: case STORELOCAL2:
875 case STORELOCAL3: case STORELOCAL4: case STORELOCAL5:
876 case STORELOCAL6: case STORELOCAL7: case STORELOCAL8:
877 case STORELOCAL9:
878 *((stack+base) + (int)(opcode-STORELOCAL0)) = *(--top);
879 break;
881 case STORELOCAL: *((stack+base) + (*pc++)) = *(--top); break;
883 case STOREGLOBAL:
885 CodeWord code;
886 get_word(code,pc);
887 s_object(code.w) = *(--top);
889 break;
891 case STOREINDEXED0:
892 storesubscript();
893 break;
895 case STOREINDEXED:
897 int n = *pc++;
898 if (tag(top-3-n) != LUA_T_ARRAY)
900 lua_checkstack(top+2);
901 *(top+1) = *(top-1);
902 *(top) = *(top-2-n);
903 *(top-1) = *(top-3-n);
904 top += 2;
905 callFB(FB_SETTABLE);
907 else
909 Object *h = lua_hashdefine (avalue(top-3-n), top-2-n);
910 *h = *(top-1);
911 top--;
914 break;
916 case STORELIST0:
917 case STORELIST:
919 int m, n;
920 Object *arr;
921 if (opcode == STORELIST0) m = 0;
922 else m = *(pc++) * FIELDS_PER_FLUSH;
923 n = *(pc++);
924 arr = top-n-1;
925 while (n)
927 tag(top) = LUA_T_NUMBER; nvalue(top) = n+m;
928 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
929 top--;
930 n--;
933 break;
935 case STORERECORD:
937 int n = *(pc++);
938 Object *arr = top-n-1;
939 while (n)
941 CodeWord code;
942 get_word(code,pc);
943 tag(top) = LUA_T_STRING; tsvalue(top) = lua_constant[code.w];
944 *(lua_hashdefine (avalue(arr), top)) = *(top-1);
945 top--;
946 n--;
949 break;
951 case ADJUST0:
952 adjust_top(base);
953 break;
955 case ADJUST:
956 adjust_top(base + *(pc++));
957 break;
959 case CREATEARRAY:
961 CodeWord size;
962 get_word(size,pc);
963 avalue(top) = lua_createarray(size.w);
964 tag(top) = LUA_T_ARRAY;
965 incr_top;
967 break;
969 case EQOP:
971 int res = lua_equalObj(top-2, top-1);
972 --top;
973 tag(top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
974 nvalue(top-1) = 1;
976 break;
978 case LTOP:
979 comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
980 break;
982 case LEOP:
983 comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
984 break;
986 case GTOP:
987 comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
988 break;
990 case GEOP:
991 comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
992 break;
994 case ADDOP:
996 Object *l = top-2;
997 Object *r = top-1;
998 if (tonumber(r) || tonumber(l))
999 call_arith("add");
1000 else
1002 nvalue(l) += nvalue(r);
1003 --top;
1006 break;
1008 case SUBOP:
1010 Object *l = top-2;
1011 Object *r = top-1;
1012 if (tonumber(r) || tonumber(l))
1013 call_arith("sub");
1014 else
1016 nvalue(l) -= nvalue(r);
1017 --top;
1020 break;
1022 case MULTOP:
1024 Object *l = top-2;
1025 Object *r = top-1;
1026 if (tonumber(r) || tonumber(l))
1027 call_arith("mul");
1028 else
1030 nvalue(l) *= nvalue(r);
1031 --top;
1034 break;
1036 case DIVOP:
1038 Object *l = top-2;
1039 Object *r = top-1;
1040 if (tonumber(r) || tonumber(l))
1041 call_arith("div");
1042 else
1044 nvalue(l) /= nvalue(r);
1045 --top;
1048 break;
1050 case POWOP:
1051 call_arith("pow");
1052 break;
1054 case CONCOP:
1056 Object *l = top-2;
1057 Object *r = top-1;
1058 if (tostring(r) || tostring(l))
1059 callFB(FB_CONCAT);
1060 else
1062 tsvalue(l) = lua_createstring (lua_strconc(svalue(l),svalue(r)));
1063 --top;
1066 break;
1068 case MINUSOP:
1069 if (tonumber(top-1))
1071 tag(top) = LUA_T_NIL;
1072 incr_top;
1073 call_arith("unm");
1075 else
1076 nvalue(top-1) = - nvalue(top-1);
1077 break;
1079 case NOTOP:
1080 tag(top-1) = (tag(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
1081 nvalue(top-1) = 1;
1082 break;
1084 case ONTJMP:
1086 CodeWord code;
1087 get_word(code,pc);
1088 if (tag(top-1) != LUA_T_NIL) pc += code.w;
1090 break;
1092 case ONFJMP:
1094 CodeWord code;
1095 get_word(code,pc);
1096 if (tag(top-1) == LUA_T_NIL) pc += code.w;
1098 break;
1100 case JMP:
1102 CodeWord code;
1103 get_word(code,pc);
1104 pc += code.w;
1106 break;
1108 case UPJMP:
1110 CodeWord code;
1111 get_word(code,pc);
1112 pc -= code.w;
1114 break;
1116 case IFFJMP:
1118 CodeWord code;
1119 get_word(code,pc);
1120 top--;
1121 if (tag(top) == LUA_T_NIL) pc += code.w;
1123 break;
1125 case IFFUPJMP:
1127 CodeWord code;
1128 get_word(code,pc);
1129 top--;
1130 if (tag(top) == LUA_T_NIL) pc -= code.w;
1132 break;
1134 case POP: --top; break;
1136 case CALLFUNC:
1138 int nParams = *(pc++);
1139 int nResults = *(pc++);
1140 StkId newBase = (top-stack)-nParams;
1141 do_call(newBase, nResults);
1143 break;
1145 case RETCODE0:
1146 return base;
1148 case RETCODE:
1149 return base+*pc;
1151 case SETLINE:
1153 CodeWord code;
1154 get_word(code,pc);
1155 if ((stack+base-1)->tag != LUA_T_LINE)
1157 /* open space for LINE value */
1158 open_stack((top-stack)-base);
1159 base++;
1160 (stack+base-1)->tag = LUA_T_LINE;
1162 (stack+base-1)->value.i = code.w;
1163 break;
1166 default:
1167 lua_error ("internal error - opcode doesn't match");