]>
git.proxmox.com Git - mirror_zfs.git/blob - module/lua/lvm.c
3 ** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $
5 ** See Copyright Notice in lua.h
12 #include <sys/lua/lua.h>
27 #define strcoll(l,r) (strcmp((l),(r)))
30 /* limit for table tag-method chains (to avoid loops) */
31 #define MAXTAGLOOP 100
34 const TValue
*luaV_tonumber (const TValue
*obj
, TValue
*n
) {
36 if (ttisnumber(obj
)) return obj
;
37 if (ttisstring(obj
) && luaO_str2d(svalue(obj
), tsvalue(obj
)->len
, &num
)) {
46 int luaV_tostring (lua_State
*L
, StkId obj
) {
50 char s
[LUAI_MAXNUMBER2STR
];
51 lua_Number n
= nvalue(obj
);
52 int l
= lua_number2str(s
, n
);
53 setsvalue2s(L
, obj
, luaS_newlstr(L
, s
, l
));
59 static void traceexec (lua_State
*L
) {
61 lu_byte mask
= L
->hookmask
;
62 int counthook
= ((mask
& LUA_MASKCOUNT
) && L
->hookcount
== 0);
64 resethookcount(L
); /* reset count */
65 if (ci
->callstatus
& CIST_HOOKYIELD
) { /* called hook last time? */
66 ci
->callstatus
&= ~CIST_HOOKYIELD
; /* erase mark */
67 return; /* do not call hook again (VM yielded, so it did not move) */
70 luaD_hook(L
, LUA_HOOKCOUNT
, -1); /* call count hook */
71 if (mask
& LUA_MASKLINE
) {
72 Proto
*p
= ci_func(ci
)->p
;
73 int npc
= pcRel(ci
->u
.l
.savedpc
, p
);
74 int newline
= getfuncline(p
, npc
);
75 if (npc
== 0 || /* call linehook when enter a new function, */
76 ci
->u
.l
.savedpc
<= L
->oldpc
|| /* when jump back (loop), or when */
77 newline
!= getfuncline(p
, pcRel(L
->oldpc
, p
))) /* enter a new line */
78 luaD_hook(L
, LUA_HOOKLINE
, newline
); /* call line hook */
80 L
->oldpc
= ci
->u
.l
.savedpc
;
81 if (L
->status
== LUA_YIELD
) { /* did hook yield? */
83 L
->hookcount
= 1; /* undo decrement to zero */
84 ci
->u
.l
.savedpc
--; /* undo increment (resume will increment it again) */
85 ci
->callstatus
|= CIST_HOOKYIELD
; /* mark that it yielded */
86 ci
->func
= L
->top
- 1; /* protect stack below results */
87 luaD_throw(L
, LUA_YIELD
);
92 static void callTM (lua_State
*L
, const TValue
*f
, const TValue
*p1
,
93 const TValue
*p2
, TValue
*p3
, int hasres
) {
94 if (L
== NULL
) return;
96 ptrdiff_t result
= savestack(L
, p3
);
97 setobj2s(L
, L
->top
++, f
); /* push function */
98 setobj2s(L
, L
->top
++, p1
); /* 1st argument */
99 setobj2s(L
, L
->top
++, p2
); /* 2nd argument */
100 if (!hasres
) /* no result? 'p3' is third argument */
101 setobj2s(L
, L
->top
++, p3
); /* 3rd argument */
102 /* metamethod may yield only when called from Lua code */
103 luaD_call(L
, L
->top
- (4 - hasres
), hasres
, isLua(L
->ci
));
104 if (hasres
) { /* if has result, move it to its place */
105 p3
= restorestack(L
, result
);
106 setobjs2s(L
, p3
, --L
->top
);
111 void luaV_gettable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
113 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
115 if (ttistable(t
)) { /* `t' is a table? */
116 Table
*h
= hvalue(t
);
117 const TValue
*res
= luaH_get(h
, key
); /* do a primitive get */
118 if (!ttisnil(res
) || /* result is not nil? */
119 (tm
= fasttm(L
, h
->metatable
, TM_INDEX
)) == NULL
) { /* or no TM? */
120 setobj2s(L
, val
, res
);
123 /* else will try the tag method */
125 else if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_INDEX
)))
126 luaG_typeerror(L
, t
, "index");
127 if (ttisfunction(tm
)) {
128 callTM(L
, tm
, t
, key
, val
, 1);
131 t
= tm
; /* else repeat with 'tm' */
133 luaG_runerror(L
, "loop in gettable");
137 void luaV_settable (lua_State
*L
, const TValue
*t
, TValue
*key
, StkId val
) {
139 for (loop
= 0; loop
< MAXTAGLOOP
; loop
++) {
141 if (ttistable(t
)) { /* `t' is a table? */
142 Table
*h
= hvalue(t
);
143 TValue
*oldval
= cast(TValue
*, luaH_get(h
, key
));
144 /* if previous value is not nil, there must be a previous entry
145 in the table; moreover, a metamethod has no relevance */
146 if (!ttisnil(oldval
) ||
147 /* previous value is nil; must check the metamethod */
148 ((tm
= fasttm(L
, h
->metatable
, TM_NEWINDEX
)) == NULL
&&
149 /* no metamethod; is there a previous entry in the table? */
150 (oldval
!= luaO_nilobject
||
151 /* no previous entry; must create one. (The next test is
152 always true; we only need the assignment.) */
153 (oldval
= luaH_newkey(L
, h
, key
), 1)))) {
154 /* no metamethod and (now) there is an entry with given key */
155 setobj2t(L
, oldval
, val
); /* assign new value to that entry */
156 invalidateTMcache(h
);
157 luaC_barrierback(L
, obj2gco(h
), val
);
160 /* else will try the metamethod */
162 else /* not a table; check metamethod */
163 if (ttisnil(tm
= luaT_gettmbyobj(L
, t
, TM_NEWINDEX
)))
164 luaG_typeerror(L
, t
, "index");
165 /* there is a metamethod */
166 if (ttisfunction(tm
)) {
167 callTM(L
, tm
, t
, key
, val
, 0);
170 t
= tm
; /* else repeat with 'tm' */
172 luaG_runerror(L
, "loop in settable");
176 static int call_binTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
177 StkId res
, TMS event
) {
178 const TValue
*tm
= luaT_gettmbyobj(L
, p1
, event
); /* try first operand */
180 tm
= luaT_gettmbyobj(L
, p2
, event
); /* try second operand */
181 if (ttisnil(tm
)) return 0;
182 callTM(L
, tm
, p1
, p2
, res
, 1);
187 static const TValue
*get_equalTM (lua_State
*L
, Table
*mt1
, Table
*mt2
,
189 const TValue
*tm1
= fasttm(L
, mt1
, event
);
191 if (tm1
== NULL
) return NULL
; /* no metamethod */
192 if (mt1
== mt2
) return tm1
; /* same metatables => same metamethods */
193 tm2
= fasttm(L
, mt2
, event
);
194 if (tm2
== NULL
) return NULL
; /* no metamethod */
195 if (luaV_rawequalobj(tm1
, tm2
)) /* same metamethods? */
201 static int call_orderTM (lua_State
*L
, const TValue
*p1
, const TValue
*p2
,
203 if (!call_binTM(L
, p1
, p2
, L
->top
, event
))
204 return -1; /* no metamethod */
206 return !l_isfalse(L
->top
);
210 static int l_strcmp (const TString
*ls
, const TString
*rs
) {
211 const char *l
= getstr(ls
);
212 size_t ll
= ls
->tsv
.len
;
213 const char *r
= getstr(rs
);
214 size_t lr
= rs
->tsv
.len
;
216 int temp
= strcoll(l
, r
);
217 if (temp
!= 0) return temp
;
218 else { /* strings are equal up to a `\0' */
219 size_t len
= strlen(l
); /* index of first `\0' in both strings */
220 if (len
== lr
) /* r is finished? */
221 return (len
== ll
) ? 0 : 1;
222 else if (len
== ll
) /* l is finished? */
223 return -1; /* l is smaller than r (because r is not finished) */
224 /* both strings longer than `len'; go on comparing (after the `\0') */
226 l
+= len
; ll
-= len
; r
+= len
; lr
-= len
;
232 int luaV_lessthan (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
234 if (ttisnumber(l
) && ttisnumber(r
))
235 return luai_numlt(L
, nvalue(l
), nvalue(r
));
236 else if (ttisstring(l
) && ttisstring(r
))
237 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) < 0;
238 else if ((res
= call_orderTM(L
, l
, r
, TM_LT
)) < 0)
239 luaG_ordererror(L
, l
, r
);
244 int luaV_lessequal (lua_State
*L
, const TValue
*l
, const TValue
*r
) {
246 if (ttisnumber(l
) && ttisnumber(r
))
247 return luai_numle(L
, nvalue(l
), nvalue(r
));
248 else if (ttisstring(l
) && ttisstring(r
))
249 return l_strcmp(rawtsvalue(l
), rawtsvalue(r
)) <= 0;
250 else if ((res
= call_orderTM(L
, l
, r
, TM_LE
)) >= 0) /* first try `le' */
252 else if ((res
= call_orderTM(L
, r
, l
, TM_LT
)) < 0) /* else try `lt' */
253 luaG_ordererror(L
, l
, r
);
259 ** equality of Lua values. L == NULL means raw equality (no metamethods)
261 int luaV_equalobj_ (lua_State
*L
, const TValue
*t1
, const TValue
*t2
) {
263 lua_assert(ttisequal(t1
, t2
));
265 case LUA_TNIL
: return 1;
266 case LUA_TNUMBER
: return luai_numeq(nvalue(t1
), nvalue(t2
));
267 case LUA_TBOOLEAN
: return bvalue(t1
) == bvalue(t2
); /* true must be 1 !! */
268 case LUA_TLIGHTUSERDATA
: return pvalue(t1
) == pvalue(t2
);
269 case LUA_TLCF
: return fvalue(t1
) == fvalue(t2
);
270 case LUA_TSHRSTR
: return eqshrstr(rawtsvalue(t1
), rawtsvalue(t2
));
271 case LUA_TLNGSTR
: return luaS_eqlngstr(rawtsvalue(t1
), rawtsvalue(t2
));
272 case LUA_TUSERDATA
: {
273 if (uvalue(t1
) == uvalue(t2
)) return 1;
274 else if (L
== NULL
) return 0;
275 tm
= get_equalTM(L
, uvalue(t1
)->metatable
, uvalue(t2
)->metatable
, TM_EQ
);
276 break; /* will try TM */
279 if (hvalue(t1
) == hvalue(t2
)) return 1;
280 else if (L
== NULL
) return 0;
281 tm
= get_equalTM(L
, hvalue(t1
)->metatable
, hvalue(t2
)->metatable
, TM_EQ
);
282 break; /* will try TM */
285 lua_assert(iscollectable(t1
));
286 return gcvalue(t1
) == gcvalue(t2
);
288 if (tm
== NULL
|| L
== NULL
) return 0; /* no TM? */
289 callTM(L
, tm
, t1
, t2
, L
->top
, 1); /* call TM */
290 return !l_isfalse(L
->top
);
294 void luaV_concat (lua_State
*L
, int total
) {
295 lua_assert(total
>= 2);
298 int n
= 2; /* number of elements handled in this pass (at least 2) */
299 if (!(ttisstring(top
-2) || ttisnumber(top
-2)) || !tostring(L
, top
-1)) {
300 if (!call_binTM(L
, top
-2, top
-1, top
-2, TM_CONCAT
))
301 luaG_concaterror(L
, top
-2, top
-1);
303 else if (tsvalue(top
-1)->len
== 0) /* second operand is empty? */
304 (void)tostring(L
, top
- 2); /* result is first operand */
305 else if (ttisstring(top
-2) && tsvalue(top
-2)->len
== 0) {
306 setobjs2s(L
, top
- 2, top
- 1); /* result is second op. */
309 /* at least two non-empty string values; get as many as possible */
310 size_t tl
= tsvalue(top
-1)->len
;
313 /* collect total length */
314 for (i
= 1; i
< total
&& tostring(L
, top
-i
-1); i
++) {
315 size_t l
= tsvalue(top
-i
-1)->len
;
316 if (l
>= (MAX_SIZET
/sizeof(char)) - tl
)
317 luaG_runerror(L
, "string length overflow");
320 buffer
= luaZ_openspace(L
, &G(L
)->buff
, tl
);
323 do { /* concat all strings */
324 size_t l
= tsvalue(top
-i
)->len
;
325 memcpy(buffer
+tl
, svalue(top
-i
), l
* sizeof(char));
328 setsvalue2s(L
, top
-n
, luaS_newlstr(L
, buffer
, tl
));
330 total
-= n
-1; /* got 'n' strings to create 1 new */
331 L
->top
-= n
-1; /* popped 'n' strings and pushed one */
332 } while (total
> 1); /* repeat until only 1 result left */
336 void luaV_objlen (lua_State
*L
, StkId ra
, const TValue
*rb
) {
338 switch (ttypenv(rb
)) {
340 Table
*h
= hvalue(rb
);
341 tm
= fasttm(L
, h
->metatable
, TM_LEN
);
342 if (tm
) break; /* metamethod? break switch to call it */
343 setnvalue(ra
, cast_num(luaH_getn(h
))); /* else primitive len */
347 setnvalue(ra
, cast_num(tsvalue(rb
)->len
));
350 default: { /* try metamethod */
351 tm
= luaT_gettmbyobj(L
, rb
, TM_LEN
);
352 if (ttisnil(tm
)) /* no metamethod? */
353 luaG_typeerror(L
, rb
, "get length of");
357 callTM(L
, tm
, rb
, rb
, ra
, 1);
361 * luaV_div and luaV_mod patched in from Lua 5.3.2 in order to properly handle
362 * div/mod by zero (instead of crashing, which is the default behavior in
367 ** Integer division; return 'm // n', that is, floor(m/n).
368 ** C division truncates its result (rounds towards zero).
369 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
370 ** otherwise 'floor(q) == trunc(q) - 1'.
372 static lua_Number
luaV_div (lua_State
*L
, lua_Number m
, lua_Number n
) {
373 if ((lua_Unsigned
)(n
) + 1u <= 1u) { /* special cases: -1 or 0 */
375 luaG_runerror(L
, "attempt to divide by zero");
376 return (0 - m
); /* n==-1; avoid overflow with 0x80000...//-1 */
379 lua_Number q
= m
/ n
; /* perform C division */
380 if ((m
^ n
) < 0 && m
% n
!= 0) /* 'm/n' would be negative non-integer? */
381 q
-= 1; /* correct result for different rounding */
388 ** Integer modulus; return 'm % n'. (Assume that C '%' with
389 ** negative operands follows C99 behavior. See previous comment
392 static lua_Number
luaV_mod (lua_State
*L
, lua_Number m
, lua_Number n
) {
393 if ((lua_Unsigned
)(n
) + 1u <= 1u) { /* special cases: -1 or 0 */
395 luaG_runerror(L
, "attempt to perform 'n%%0'");
396 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
399 lua_Number r
= m
% n
;
400 if (r
!= 0 && (m
^ n
) < 0) /* 'm/n' would be non-integer negative? */
401 r
+= n
; /* correct result for different rounding */
407 * End patch from 5.3.2
410 void luaV_arith (lua_State
*L
, StkId ra
, const TValue
*rb
,
411 const TValue
*rc
, TMS op
) {
414 if ((b
= luaV_tonumber(rb
, &tempb
)) != NULL
&&
415 (c
= luaV_tonumber(rc
, &tempc
)) != NULL
) {
417 * Patched: if dividing or modding, use patched functions from 5.3
420 int lop
= op
- TM_ADD
+ LUA_OPADD
;
421 if (lop
== LUA_OPDIV
) {
422 res
= luaV_div(L
, nvalue(b
), nvalue(c
));
423 } else if (lop
== LUA_OPMOD
) {
424 res
= luaV_mod(L
, nvalue(b
), nvalue(c
));
426 res
= luaO_arith(op
- TM_ADD
+ LUA_OPADD
, nvalue(b
), nvalue(c
));
430 else if (!call_binTM(L
, rb
, rc
, ra
, op
))
431 luaG_aritherror(L
, rb
, rc
);
436 ** check whether cached closure in prototype 'p' may be reused, that is,
437 ** whether there is a cached closure with the same upvalues needed by
438 ** new closure to be created.
440 static Closure
*getcached (Proto
*p
, UpVal
**encup
, StkId base
) {
441 Closure
*c
= p
->cache
;
442 if (c
!= NULL
) { /* is there a cached closure? */
443 int nup
= p
->sizeupvalues
;
444 Upvaldesc
*uv
= p
->upvalues
;
446 for (i
= 0; i
< nup
; i
++) { /* check whether it has right upvalues */
447 TValue
*v
= uv
[i
].instack
? base
+ uv
[i
].idx
: encup
[uv
[i
].idx
]->v
;
448 if (c
->l
.upvals
[i
]->v
!= v
)
449 return NULL
; /* wrong upvalue; cannot reuse closure */
452 return c
; /* return cached closure (or NULL if no cached closure) */
457 ** create a new Lua closure, push it in the stack, and initialize
458 ** its upvalues. Note that the call to 'luaC_barrierproto' must come
459 ** before the assignment to 'p->cache', as the function needs the
460 ** original value of that field.
462 static void pushclosure (lua_State
*L
, Proto
*p
, UpVal
**encup
, StkId base
,
464 int nup
= p
->sizeupvalues
;
465 Upvaldesc
*uv
= p
->upvalues
;
467 Closure
*ncl
= luaF_newLclosure(L
, nup
);
469 setclLvalue(L
, ra
, ncl
); /* anchor new closure in stack */
470 for (i
= 0; i
< nup
; i
++) { /* fill in its upvalues */
471 if (uv
[i
].instack
) /* upvalue refers to local variable? */
472 ncl
->l
.upvals
[i
] = luaF_findupval(L
, base
+ uv
[i
].idx
);
473 else /* get upvalue from enclosing function */
474 ncl
->l
.upvals
[i
] = encup
[uv
[i
].idx
];
476 luaC_barrierproto(L
, p
, ncl
);
477 p
->cache
= ncl
; /* save it on cache for reuse */
482 ** finish execution of an opcode interrupted by an yield
484 void luaV_finishOp (lua_State
*L
) {
485 CallInfo
*ci
= L
->ci
;
486 StkId base
= ci
->u
.l
.base
;
487 Instruction inst
= *(ci
->u
.l
.savedpc
- 1); /* interrupted instruction */
488 OpCode op
= GET_OPCODE(inst
);
489 switch (op
) { /* finish its execution */
490 case OP_ADD
: case OP_SUB
: case OP_MUL
: case OP_DIV
:
491 case OP_MOD
: case OP_POW
: case OP_UNM
: case OP_LEN
:
492 case OP_GETTABUP
: case OP_GETTABLE
: case OP_SELF
: {
493 setobjs2s(L
, base
+ GETARG_A(inst
), --L
->top
);
496 case OP_LE
: case OP_LT
: case OP_EQ
: {
497 int res
= !l_isfalse(L
->top
- 1);
499 /* metamethod should not be called when operand is K */
500 lua_assert(!ISK(GETARG_B(inst
)));
501 if (op
== OP_LE
&& /* "<=" using "<" instead? */
502 ttisnil(luaT_gettmbyobj(L
, base
+ GETARG_B(inst
), TM_LE
)))
503 res
= !res
; /* invert result */
504 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_JMP
);
505 if (res
!= GETARG_A(inst
)) /* condition failed? */
506 ci
->u
.l
.savedpc
++; /* skip jump instruction */
510 StkId top
= L
->top
- 1; /* top when 'call_binTM' was called */
511 int b
= GETARG_B(inst
); /* first element to concatenate */
512 int total
= cast_int(top
- 1 - (base
+ b
)); /* yet to concatenate */
513 setobj2s(L
, top
- 2, top
); /* put TM result in proper position */
514 if (total
> 1) { /* are there elements to concat? */
515 L
->top
= top
- 1; /* top is one after last element (at top-2) */
516 luaV_concat(L
, total
); /* concat them (may yield again) */
518 /* move final result to final position */
519 setobj2s(L
, ci
->u
.l
.base
+ GETARG_A(inst
), L
->top
- 1);
520 L
->top
= ci
->top
; /* restore top */
524 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_TFORLOOP
);
525 L
->top
= ci
->top
; /* correct top */
529 if (GETARG_C(inst
) - 1 >= 0) /* nresults >= 0? */
530 L
->top
= ci
->top
; /* adjust results */
533 case OP_TAILCALL
: case OP_SETTABUP
: case OP_SETTABLE
:
535 default: lua_assert(0);
542 ** some macros for common tasks in `luaV_execute'
545 #if !defined luai_runtimecheck
546 #define luai_runtimecheck(L, c) /* void */
550 #define RA(i) (base+GETARG_A(i))
551 /* to be used after possible stack reallocation */
552 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
553 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
554 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
555 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
556 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
557 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
559 (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
562 /* execute a jump instruction */
563 #define dojump(ci,i,e) \
564 { int a = GETARG_A(i); \
565 if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
566 ci->u.l.savedpc += GETARG_sBx(i) + e; }
568 /* for test instructions, execute the jump instruction that follows it */
569 #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
572 #define Protect(x) { {x;}; base = ci->u.l.base; }
574 #define checkGC(L,c) \
575 Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
577 L->top = ci->top;}) /* restore top */ \
578 luai_threadyield(L); )
581 #define arith_op(op,tm) { \
582 TValue *rb = RKB(i); \
583 TValue *rc = RKC(i); \
584 if (ttisnumber(rb) && ttisnumber(rc)) { \
585 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
586 setnvalue(ra, op(L, nb, nc)); \
588 else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
591 #define vmdispatch(o) switch(o)
592 #define vmcase(l,b) case l: {b} break;
593 #define vmcasenb(l,b) case l: {b} /* nb = no break */
595 void luaV_execute (lua_State
*L
) {
596 CallInfo
*ci
= L
->ci
;
600 newframe
: /* reentry point when frame changes (call/return) */
601 lua_assert(ci
== L
->ci
);
602 cl
= clLvalue(ci
->func
);
605 /* main loop of interpreter */
607 Instruction i
= *(ci
->u
.l
.savedpc
++);
609 if ((L
->hookmask
& (LUA_MASKLINE
| LUA_MASKCOUNT
)) &&
610 (--L
->hookcount
== 0 || L
->hookmask
& LUA_MASKLINE
)) {
611 Protect(traceexec(L
));
613 /* WARNING: several calls may realloc the stack and invalidate `ra' */
615 lua_assert(base
== ci
->u
.l
.base
);
616 lua_assert(base
<= L
->top
&& L
->top
< L
->stack
+ L
->stacksize
);
617 vmdispatch (GET_OPCODE(i
)) {
619 setobjs2s(L
, ra
, RB(i
));
622 TValue
*rb
= k
+ GETARG_Bx(i
);
627 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
628 rb
= k
+ GETARG_Ax(*ci
->u
.l
.savedpc
++);
632 setbvalue(ra
, GETARG_B(i
));
633 if (GETARG_C(i
)) ci
->u
.l
.savedpc
++; /* skip next instruction (if C) */
643 setobj2s(L
, ra
, cl
->upvals
[b
]->v
);
647 Protect(luaV_gettable(L
, cl
->upvals
[b
]->v
, RKC(i
), ra
));
650 Protect(luaV_gettable(L
, RB(i
), RKC(i
), ra
));
654 Protect(luaV_settable(L
, cl
->upvals
[a
]->v
, RKB(i
), RKC(i
)));
657 UpVal
*uv
= cl
->upvals
[GETARG_B(i
)];
658 setobj(L
, uv
->v
, ra
);
659 luaC_barrier(L
, uv
, ra
);
662 Protect(luaV_settable(L
, ra
, RKB(i
), RKC(i
)));
667 Table
*t
= luaH_new(L
);
669 if (b
!= 0 || c
!= 0)
670 luaH_resize(L
, t
, luaO_fb2int(b
), luaO_fb2int(c
));
675 setobjs2s(L
, ra
+1, rb
);
676 Protect(luaV_gettable(L
, rb
, RKC(i
), ra
));
679 arith_op(luai_numadd
, TM_ADD
);
682 arith_op(luai_numsub
, TM_SUB
);
685 arith_op(luai_nummul
, TM_MUL
);
688 * Patched: use luaV_* instead of luai_* to handle div/mod by 0
691 arith_op(luaV_div
, TM_DIV
);
694 arith_op(luaV_mod
, TM_MOD
);
697 arith_op(luai_numpow
, TM_POW
);
701 if (ttisnumber(rb
)) {
702 lua_Number nb
= nvalue(rb
);
703 setnvalue(ra
, luai_numunm(L
, nb
));
706 Protect(luaV_arith(L
, ra
, rb
, rb
, TM_UNM
));
711 int res
= l_isfalse(rb
); /* next assignment may change this value */
715 Protect(luaV_objlen(L
, ra
, RB(i
)));
721 L
->top
= base
+ c
+ 1; /* mark the end of concat operands */
722 Protect(luaV_concat(L
, c
- b
+ 1));
723 ra
= RA(i
); /* 'luav_concat' may invoke TMs and move the stack */
725 setobjs2s(L
, ra
, rb
);
726 checkGC(L
, (ra
>= rb
? ra
+ 1 : rb
));
727 L
->top
= ci
->top
; /* restore top */
736 if (cast_int(equalobj(L
, rb
, rc
)) != GETARG_A(i
))
744 if (luaV_lessthan(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
752 if (luaV_lessequal(L
, RKB(i
), RKC(i
)) != GETARG_A(i
))
759 if (GETARG_C(i
) ? l_isfalse(ra
) : !l_isfalse(ra
))
766 if (GETARG_C(i
) ? l_isfalse(rb
) : !l_isfalse(rb
))
769 setobjs2s(L
, ra
, rb
);
775 int nresults
= GETARG_C(i
) - 1;
776 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
777 if (luaD_precall(L
, ra
, nresults
)) { /* C function? */
778 if (nresults
>= 0) L
->top
= ci
->top
; /* adjust results */
781 else { /* Lua function */
783 ci
->callstatus
|= CIST_REENTRY
;
784 goto newframe
; /* restart luaV_execute over new Lua function */
789 if (b
!= 0) L
->top
= ra
+b
; /* else previous instruction set top */
790 lua_assert(GETARG_C(i
) - 1 == LUA_MULTRET
);
791 if (luaD_precall(L
, ra
, LUA_MULTRET
)) /* C function? */
794 /* tail call: put called frame (n) in place of caller one (o) */
795 CallInfo
*nci
= L
->ci
; /* called frame */
796 CallInfo
*oci
= nci
->previous
; /* caller frame */
797 StkId nfunc
= nci
->func
; /* called function */
798 StkId ofunc
= oci
->func
; /* caller function */
799 /* last stack slot filled by 'precall' */
800 StkId lim
= nci
->u
.l
.base
+ getproto(nfunc
)->numparams
;
802 /* close all upvalues from previous call */
803 if (cl
->p
->sizep
> 0) luaF_close(L
, oci
->u
.l
.base
);
804 /* move new frame into old one */
805 for (aux
= 0; nfunc
+ aux
< lim
; aux
++)
806 setobjs2s(L
, ofunc
+ aux
, nfunc
+ aux
);
807 oci
->u
.l
.base
= ofunc
+ (nci
->u
.l
.base
- nfunc
); /* correct base */
808 oci
->top
= L
->top
= ofunc
+ (L
->top
- nfunc
); /* correct top */
809 oci
->u
.l
.savedpc
= nci
->u
.l
.savedpc
;
810 oci
->callstatus
|= CIST_TAIL
; /* function was tail called */
811 ci
= L
->ci
= oci
; /* remove new frame */
812 lua_assert(L
->top
== oci
->u
.l
.base
+ getproto(ofunc
)->maxstacksize
);
813 goto newframe
; /* restart luaV_execute over new Lua function */
818 if (b
!= 0) L
->top
= ra
+b
-1;
819 if (cl
->p
->sizep
> 0) luaF_close(L
, base
);
820 b
= luaD_poscall(L
, ra
);
821 if (!(ci
->callstatus
& CIST_REENTRY
)) /* 'ci' still the called one */
822 return; /* external invocation: return */
823 else { /* invocation via reentry: continue execution */
825 if (b
) L
->top
= ci
->top
;
826 lua_assert(isLua(ci
));
827 lua_assert(GET_OPCODE(*((ci
)->u
.l
.savedpc
- 1)) == OP_CALL
);
828 goto newframe
; /* restart luaV_execute over new Lua function */
832 lua_Number step
= nvalue(ra
+2);
833 lua_Number idx
= luai_numadd(L
, nvalue(ra
), step
); /* increment index */
834 lua_Number limit
= nvalue(ra
+1);
835 if (luai_numlt(L
, 0, step
) ? luai_numle(L
, idx
, limit
)
836 : luai_numle(L
, limit
, idx
)) {
837 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
838 setnvalue(ra
, idx
); /* update internal index... */
839 setnvalue(ra
+3, idx
); /* ...and external index */
843 const TValue
*init
= ra
;
844 const TValue
*plimit
= ra
+1;
845 const TValue
*pstep
= ra
+2;
846 if (!tonumber(init
, ra
))
847 luaG_runerror(L
, LUA_QL("for") " initial value must be a number");
848 else if (!tonumber(plimit
, ra
+1))
849 luaG_runerror(L
, LUA_QL("for") " limit must be a number");
850 else if (!tonumber(pstep
, ra
+2))
851 luaG_runerror(L
, LUA_QL("for") " step must be a number");
852 setnvalue(ra
, luai_numsub(L
, nvalue(ra
), nvalue(pstep
)));
853 ci
->u
.l
.savedpc
+= GETARG_sBx(i
);
855 vmcasenb(OP_TFORCALL
,
856 StkId cb
= ra
+ 3; /* call base */
857 setobjs2s(L
, cb
+2, ra
+2);
858 setobjs2s(L
, cb
+1, ra
+1);
859 setobjs2s(L
, cb
, ra
);
860 L
->top
= cb
+ 3; /* func. + 2 args (state and index) */
861 Protect(luaD_call(L
, cb
, GETARG_C(i
), 1));
863 i
= *(ci
->u
.l
.savedpc
++); /* go to next instruction */
865 lua_assert(GET_OPCODE(i
) == OP_TFORLOOP
);
870 if (!ttisnil(ra
+ 1)) { /* continue loop? */
871 setobjs2s(L
, ra
, ra
+ 1); /* save control variable */
872 ci
->u
.l
.savedpc
+= GETARG_sBx(i
); /* jump back */
880 if (n
== 0) n
= cast_int(L
->top
- ra
) - 1;
882 lua_assert(GET_OPCODE(*ci
->u
.l
.savedpc
) == OP_EXTRAARG
);
883 c
= GETARG_Ax(*ci
->u
.l
.savedpc
++);
885 luai_runtimecheck(L
, ttistable(ra
));
887 last
= ((c
-1)*LFIELDS_PER_FLUSH
) + n
;
888 if (last
> h
->sizearray
) /* needs more space? */
889 luaH_resizearray(L
, h
, last
); /* pre-allocate it at once */
892 luaH_setint(L
, h
, last
--, val
);
893 luaC_barrierback(L
, obj2gco(h
), val
);
895 L
->top
= ci
->top
; /* correct top (in case of previous open call) */
898 Proto
*p
= cl
->p
->p
[GETARG_Bx(i
)];
899 Closure
*ncl
= getcached(p
, cl
->upvals
, base
); /* cached closure */
900 if (ncl
== NULL
) /* no match? */
901 pushclosure(L
, p
, cl
->upvals
, base
, ra
); /* create a new one */
903 setclLvalue(L
, ra
, ncl
); /* push cashed closure */
907 int b
= GETARG_B(i
) - 1;
909 int n
= cast_int(base
- ci
->func
) - cl
->p
->numparams
- 1;
910 if (b
< 0) { /* B == 0? */
911 b
= n
; /* get all var. arguments */
912 Protect(luaD_checkstack(L
, n
));
913 ra
= RA(i
); /* previous call may change the stack */
916 for (j
= 0; j
< b
; j
++) {
918 setobjs2s(L
, ra
+ j
, base
- n
+ j
);
933 * this can live in SPL
935 #if BITS_PER_LONG == 32
936 #if defined(_KERNEL) && !defined(SPL_HAS_MODDI3)
937 extern uint64_t __umoddi3(uint64_t dividend
, uint64_t divisor
);
939 /* 64-bit signed modulo for 32-bit machines. */
941 __moddi3(int64_t n
, int64_t d
)
944 boolean_t nn
= B_FALSE
;
955 return (nn
? -q
: q
);
957 EXPORT_SYMBOL(__moddi3
);