]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/lcode.c
2 ** $Id: lcode.c,v 2.101 2015/04/29 18:24:11 roberto Exp $
3 ** Code generator for Lua
4 ** See Copyright Notice in lua.h
32 /* Maximum number of registers in a Lua function (must fit in 8 bits) */
36 #define hasjumps(e) ((e)->t != (e)->f)
39 static int tonumeral(expdesc
*e
, TValue
*v
) {
40 if (e
->t
!= NO_JUMP
|| e
->f
!= NO_JUMP
)
41 return 0; /* not a numeral */
44 if (v
) setivalue(v
, e
->u
.ival
);
47 if (v
) setfltvalue(v
, e
->u
.nval
);
54 void luaK_nil (FuncState
*fs
, int from
, int n
) {
55 Instruction
*previous
;
56 int l
= from
+ n
- 1; /* last register to set nil */
57 if (fs
->pc
> fs
->lasttarget
) { /* no jumps to current position? */
58 previous
= &fs
->f
->code
[fs
->pc
-1];
59 if (GET_OPCODE(*previous
) == OP_LOADNIL
) {
60 int pfrom
= GETARG_A(*previous
);
61 int pl
= pfrom
+ GETARG_B(*previous
);
62 if ((pfrom
<= from
&& from
<= pl
+ 1) ||
63 (from
<= pfrom
&& pfrom
<= l
+ 1)) { /* can connect both? */
64 if (pfrom
< from
) from
= pfrom
; /* from = min(from, pfrom) */
65 if (pl
> l
) l
= pl
; /* l = max(l, pl) */
66 SETARG_A(*previous
, from
);
67 SETARG_B(*previous
, l
- from
);
70 } /* else go through */
72 luaK_codeABC(fs
, OP_LOADNIL
, from
, n
- 1, 0); /* else no optimization */
76 int luaK_jump (FuncState
*fs
) {
77 int jpc
= fs
->jpc
; /* save list of jumps to here */
80 j
= luaK_codeAsBx(fs
, OP_JMP
, 0, NO_JUMP
);
81 luaK_concat(fs
, &j
, jpc
); /* keep them on hold */
86 void luaK_ret (FuncState
*fs
, int first
, int nret
) {
87 luaK_codeABC(fs
, OP_RETURN
, first
, nret
+1, 0);
91 static int condjump (FuncState
*fs
, OpCode op
, int A
, int B
, int C
) {
92 luaK_codeABC(fs
, op
, A
, B
, C
);
97 static void fixjump (FuncState
*fs
, int pc
, int dest
) {
98 Instruction
*jmp
= &fs
->f
->code
[pc
];
99 int offset
= dest
-(pc
+1);
100 lua_assert(dest
!= NO_JUMP
);
101 if (abs(offset
) > MAXARG_sBx
)
102 luaX_syntaxerror(fs
->ls
, "control structure too long");
103 SETARG_sBx(*jmp
, offset
);
108 ** returns current 'pc' and marks it as a jump target (to avoid wrong
109 ** optimizations with consecutive instructions not in the same basic block).
111 int luaK_getlabel (FuncState
*fs
) {
112 fs
->lasttarget
= fs
->pc
;
117 static int getjump (FuncState
*fs
, int pc
) {
118 int offset
= GETARG_sBx(fs
->f
->code
[pc
]);
119 if (offset
== NO_JUMP
) /* point to itself represents end of list */
120 return NO_JUMP
; /* end of list */
122 return (pc
+1)+offset
; /* turn offset into absolute position */
126 static Instruction
*getjumpcontrol (FuncState
*fs
, int pc
) {
127 Instruction
*pi
= &fs
->f
->code
[pc
];
128 if (pc
>= 1 && testTMode(GET_OPCODE(*(pi
-1))))
136 ** check whether list has any jump that do not produce a value
137 ** (or produce an inverted value)
139 static int need_value (FuncState
*fs
, int list
) {
140 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
)) {
141 Instruction i
= *getjumpcontrol(fs
, list
);
142 if (GET_OPCODE(i
) != OP_TESTSET
) return 1;
144 return 0; /* not found */
148 static int patchtestreg (FuncState
*fs
, int node
, int reg
) {
149 Instruction
*i
= getjumpcontrol(fs
, node
);
150 if (GET_OPCODE(*i
) != OP_TESTSET
)
151 return 0; /* cannot patch other instructions */
152 if (reg
!= NO_REG
&& reg
!= GETARG_B(*i
))
154 else /* no register to put value or register already has the value */
155 *i
= CREATE_ABC(OP_TEST
, GETARG_B(*i
), 0, GETARG_C(*i
));
161 static void removevalues (FuncState
*fs
, int list
) {
162 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
))
163 patchtestreg(fs
, list
, NO_REG
);
167 static void patchlistaux (FuncState
*fs
, int list
, int vtarget
, int reg
,
169 while (list
!= NO_JUMP
) {
170 int next
= getjump(fs
, list
);
171 if (patchtestreg(fs
, list
, reg
))
172 fixjump(fs
, list
, vtarget
);
174 fixjump(fs
, list
, dtarget
); /* jump to default target */
180 static void dischargejpc (FuncState
*fs
) {
181 patchlistaux(fs
, fs
->jpc
, fs
->pc
, NO_REG
, fs
->pc
);
186 void luaK_patchlist (FuncState
*fs
, int list
, int target
) {
187 if (target
== fs
->pc
)
188 luaK_patchtohere(fs
, list
);
190 lua_assert(target
< fs
->pc
);
191 patchlistaux(fs
, list
, target
, NO_REG
, target
);
196 void luaK_patchclose (FuncState
*fs
, int list
, int level
) {
197 level
++; /* argument is +1 to reserve 0 as non-op */
198 while (list
!= NO_JUMP
) {
199 int next
= getjump(fs
, list
);
200 lua_assert(GET_OPCODE(fs
->f
->code
[list
]) == OP_JMP
&&
201 (GETARG_A(fs
->f
->code
[list
]) == 0 ||
202 GETARG_A(fs
->f
->code
[list
]) >= level
));
203 SETARG_A(fs
->f
->code
[list
], level
);
209 void luaK_patchtohere (FuncState
*fs
, int list
) {
211 luaK_concat(fs
, &fs
->jpc
, list
);
215 void luaK_concat (FuncState
*fs
, int *l1
, int l2
) {
216 if (l2
== NO_JUMP
) return;
217 else if (*l1
== NO_JUMP
)
222 while ((next
= getjump(fs
, list
)) != NO_JUMP
) /* find last element */
224 fixjump(fs
, list
, l2
);
229 static int luaK_code (FuncState
*fs
, Instruction i
) {
231 dischargejpc(fs
); /* 'pc' will change */
232 /* put new instruction in code array */
233 luaM_growvector(fs
->ls
->L
, f
->code
, fs
->pc
, f
->sizecode
, Instruction
,
236 /* save corresponding line information */
237 luaM_growvector(fs
->ls
->L
, f
->lineinfo
, fs
->pc
, f
->sizelineinfo
, int,
239 f
->lineinfo
[fs
->pc
] = fs
->ls
->lastline
;
244 int luaK_codeABC (FuncState
*fs
, OpCode o
, int a
, int b
, int c
) {
245 lua_assert(getOpMode(o
) == iABC
);
246 lua_assert(getBMode(o
) != OpArgN
|| b
== 0);
247 lua_assert(getCMode(o
) != OpArgN
|| c
== 0);
248 lua_assert(a
<= MAXARG_A
&& b
<= MAXARG_B
&& c
<= MAXARG_C
);
249 return luaK_code(fs
, CREATE_ABC(o
, a
, b
, c
));
253 int luaK_codeABx (FuncState
*fs
, OpCode o
, int a
, unsigned int bc
) {
254 lua_assert(getOpMode(o
) == iABx
|| getOpMode(o
) == iAsBx
);
255 lua_assert(getCMode(o
) == OpArgN
);
256 lua_assert(a
<= MAXARG_A
&& bc
<= MAXARG_Bx
);
257 return luaK_code(fs
, CREATE_ABx(o
, a
, bc
));
261 static int codeextraarg (FuncState
*fs
, int a
) {
262 lua_assert(a
<= MAXARG_Ax
);
263 return luaK_code(fs
, CREATE_Ax(OP_EXTRAARG
, a
));
267 int luaK_codek (FuncState
*fs
, int reg
, int k
) {
269 return luaK_codeABx(fs
, OP_LOADK
, reg
, k
);
271 int p
= luaK_codeABx(fs
, OP_LOADKX
, reg
, 0);
278 void luaK_checkstack (FuncState
*fs
, int n
) {
279 int newstack
= fs
->freereg
+ n
;
280 if (newstack
> fs
->f
->maxstacksize
) {
281 if (newstack
>= MAXREGS
)
282 luaX_syntaxerror(fs
->ls
,
283 "function or expression needs too many registers");
284 fs
->f
->maxstacksize
= cast_byte(newstack
);
289 void luaK_reserveregs (FuncState
*fs
, int n
) {
290 luaK_checkstack(fs
, n
);
295 static void freereg (FuncState
*fs
, int reg
) {
296 if (!ISK(reg
) && reg
>= fs
->nactvar
) {
298 lua_assert(reg
== fs
->freereg
);
303 static void freeexp (FuncState
*fs
, expdesc
*e
) {
304 if (e
->k
== VNONRELOC
)
305 freereg(fs
, e
->u
.info
);
310 ** Use scanner's table to cache position of constants in constant list
311 ** and try to reuse constants
313 static int addk (FuncState
*fs
, TValue
*key
, TValue
*v
) {
314 lua_State
*L
= fs
->ls
->L
;
316 TValue
*idx
= luaH_set(L
, fs
->ls
->h
, key
); /* index scanner table */
318 if (ttisinteger(idx
)) { /* is there an index there? */
319 k
= cast_int(ivalue(idx
));
320 /* correct value? (warning: must distinguish floats from integers!) */
321 if (k
< fs
->nk
&& ttype(&f
->k
[k
]) == ttype(v
) &&
322 luaV_rawequalobj(&f
->k
[k
], v
))
323 return k
; /* reuse index */
325 /* constant not found; create a new entry */
328 /* numerical value does not need GC barrier;
329 table has no metatable, so it does not need to invalidate cache */
331 luaM_growvector(L
, f
->k
, k
, f
->sizek
, TValue
, MAXARG_Ax
, "constants");
332 while (oldsize
< f
->sizek
) setnilvalue(&f
->k
[oldsize
++]);
333 setobj(L
, &f
->k
[k
], v
);
335 luaC_barrier(L
, f
, v
);
340 int luaK_stringK (FuncState
*fs
, TString
*s
) {
342 setsvalue(fs
->ls
->L
, &o
, s
);
343 return addk(fs
, &o
, &o
);
348 ** Integers use userdata as keys to avoid collision with floats with same
349 ** value; conversion to 'void*' used only for hashing, no "precision"
352 int luaK_intK (FuncState
*fs
, lua_Integer n
) {
354 setpvalue(&k
, cast(void*, cast(size_t, n
)));
356 return addk(fs
, &k
, &o
);
360 static int luaK_numberK (FuncState
*fs
, lua_Number r
) {
363 return addk(fs
, &o
, &o
);
367 static int boolK (FuncState
*fs
, int b
) {
370 return addk(fs
, &o
, &o
);
374 static int nilK (FuncState
*fs
) {
377 /* cannot use nil as key; instead use table itself to represent nil */
378 sethvalue(fs
->ls
->L
, &k
, fs
->ls
->h
);
379 return addk(fs
, &k
, &v
);
383 void luaK_setreturns (FuncState
*fs
, expdesc
*e
, int nresults
) {
384 if (e
->k
== VCALL
) { /* expression is an open function call? */
385 SETARG_C(getcode(fs
, e
), nresults
+1);
387 else if (e
->k
== VVARARG
) {
388 SETARG_B(getcode(fs
, e
), nresults
+1);
389 SETARG_A(getcode(fs
, e
), fs
->freereg
);
390 luaK_reserveregs(fs
, 1);
395 void luaK_setoneret (FuncState
*fs
, expdesc
*e
) {
396 if (e
->k
== VCALL
) { /* expression is an open function call? */
398 e
->u
.info
= GETARG_A(getcode(fs
, e
));
400 else if (e
->k
== VVARARG
) {
401 SETARG_B(getcode(fs
, e
), 2);
402 e
->k
= VRELOCABLE
; /* can relocate its simple result */
407 void luaK_dischargevars (FuncState
*fs
, expdesc
*e
) {
414 e
->u
.info
= luaK_codeABC(fs
, OP_GETUPVAL
, 0, e
->u
.info
, 0);
419 OpCode op
= OP_GETTABUP
; /* assume 't' is in an upvalue */
420 freereg(fs
, e
->u
.ind
.idx
);
421 if (e
->u
.ind
.vt
== VLOCAL
) { /* 't' is in a register? */
422 freereg(fs
, e
->u
.ind
.t
);
425 e
->u
.info
= luaK_codeABC(fs
, op
, 0, e
->u
.ind
.t
, e
->u
.ind
.idx
);
431 luaK_setoneret(fs
, e
);
434 default: break; /* there is one value available (somewhere) */
439 static int code_label (FuncState
*fs
, int A
, int b
, int jump
) {
440 luaK_getlabel(fs
); /* those instructions may be jump targets */
441 return luaK_codeABC(fs
, OP_LOADBOOL
, A
, b
, jump
);
445 static void discharge2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
446 luaK_dischargevars(fs
, e
);
449 luaK_nil(fs
, reg
, 1);
452 case VFALSE
: case VTRUE
: {
453 luaK_codeABC(fs
, OP_LOADBOOL
, reg
, e
->k
== VTRUE
, 0);
457 luaK_codek(fs
, reg
, e
->u
.info
);
461 luaK_codek(fs
, reg
, luaK_numberK(fs
, e
->u
.nval
));
465 luaK_codek(fs
, reg
, luaK_intK(fs
, e
->u
.ival
));
469 Instruction
*pc
= &getcode(fs
, e
);
474 if (reg
!= e
->u
.info
)
475 luaK_codeABC(fs
, OP_MOVE
, reg
, e
->u
.info
, 0);
479 lua_assert(e
->k
== VVOID
|| e
->k
== VJMP
);
480 return; /* nothing to do... */
488 static void discharge2anyreg (FuncState
*fs
, expdesc
*e
) {
489 if (e
->k
!= VNONRELOC
) {
490 luaK_reserveregs(fs
, 1);
491 discharge2reg(fs
, e
, fs
->freereg
-1);
496 static void exp2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
497 discharge2reg(fs
, e
, reg
);
499 luaK_concat(fs
, &e
->t
, e
->u
.info
); /* put this jump in 't' list */
501 int final
; /* position after whole expression */
502 int p_f
= NO_JUMP
; /* position of an eventual LOAD false */
503 int p_t
= NO_JUMP
; /* position of an eventual LOAD true */
504 if (need_value(fs
, e
->t
) || need_value(fs
, e
->f
)) {
505 int fj
= (e
->k
== VJMP
) ? NO_JUMP
: luaK_jump(fs
);
506 p_f
= code_label(fs
, reg
, 0, 1);
507 p_t
= code_label(fs
, reg
, 1, 0);
508 luaK_patchtohere(fs
, fj
);
510 final
= luaK_getlabel(fs
);
511 patchlistaux(fs
, e
->f
, final
, reg
, p_f
);
512 patchlistaux(fs
, e
->t
, final
, reg
, p_t
);
514 e
->f
= e
->t
= NO_JUMP
;
520 void luaK_exp2nextreg (FuncState
*fs
, expdesc
*e
) {
521 luaK_dischargevars(fs
, e
);
523 luaK_reserveregs(fs
, 1);
524 exp2reg(fs
, e
, fs
->freereg
- 1);
528 int luaK_exp2anyreg (FuncState
*fs
, expdesc
*e
) {
529 luaK_dischargevars(fs
, e
);
530 if (e
->k
== VNONRELOC
) {
531 if (!hasjumps(e
)) return e
->u
.info
; /* exp is already in a register */
532 if (e
->u
.info
>= fs
->nactvar
) { /* reg. is not a local? */
533 exp2reg(fs
, e
, e
->u
.info
); /* put value on it */
537 luaK_exp2nextreg(fs
, e
); /* default */
542 void luaK_exp2anyregup (FuncState
*fs
, expdesc
*e
) {
543 if (e
->k
!= VUPVAL
|| hasjumps(e
))
544 luaK_exp2anyreg(fs
, e
);
548 void luaK_exp2val (FuncState
*fs
, expdesc
*e
) {
550 luaK_exp2anyreg(fs
, e
);
552 luaK_dischargevars(fs
, e
);
556 int luaK_exp2RK (FuncState
*fs
, expdesc
*e
) {
562 if (fs
->nk
<= MAXINDEXRK
) { /* constant fits in RK operand? */
563 e
->u
.info
= (e
->k
== VNIL
) ? nilK(fs
) : boolK(fs
, (e
->k
== VTRUE
));
565 return RKASK(e
->u
.info
);
570 e
->u
.info
= luaK_intK(fs
, e
->u
.ival
);
575 e
->u
.info
= luaK_numberK(fs
, e
->u
.nval
);
581 if (e
->u
.info
<= MAXINDEXRK
) /* constant fits in 'argC'? */
582 return RKASK(e
->u
.info
);
587 /* not a constant in the right range: put it in a register */
588 return luaK_exp2anyreg(fs
, e
);
592 void luaK_storevar (FuncState
*fs
, expdesc
*var
, expdesc
*ex
) {
596 exp2reg(fs
, ex
, var
->u
.info
);
600 int e
= luaK_exp2anyreg(fs
, ex
);
601 luaK_codeABC(fs
, OP_SETUPVAL
, e
, var
->u
.info
, 0);
605 OpCode op
= (var
->u
.ind
.vt
== VLOCAL
) ? OP_SETTABLE
: OP_SETTABUP
;
606 int e
= luaK_exp2RK(fs
, ex
);
607 luaK_codeABC(fs
, op
, var
->u
.ind
.t
, var
->u
.ind
.idx
, e
);
611 lua_assert(0); /* invalid var kind to store */
619 void luaK_self (FuncState
*fs
, expdesc
*e
, expdesc
*key
) {
621 luaK_exp2anyreg(fs
, e
);
622 ereg
= e
->u
.info
; /* register where 'e' was placed */
624 e
->u
.info
= fs
->freereg
; /* base register for op_self */
626 luaK_reserveregs(fs
, 2); /* function and 'self' produced by op_self */
627 luaK_codeABC(fs
, OP_SELF
, e
->u
.info
, ereg
, luaK_exp2RK(fs
, key
));
632 static void invertjump (FuncState
*fs
, expdesc
*e
) {
633 Instruction
*pc
= getjumpcontrol(fs
, e
->u
.info
);
634 lua_assert(testTMode(GET_OPCODE(*pc
)) && GET_OPCODE(*pc
) != OP_TESTSET
&&
635 GET_OPCODE(*pc
) != OP_TEST
);
636 SETARG_A(*pc
, !(GETARG_A(*pc
)));
640 static int jumponcond (FuncState
*fs
, expdesc
*e
, int cond
) {
641 if (e
->k
== VRELOCABLE
) {
642 Instruction ie
= getcode(fs
, e
);
643 if (GET_OPCODE(ie
) == OP_NOT
) {
644 fs
->pc
--; /* remove previous OP_NOT */
645 return condjump(fs
, OP_TEST
, GETARG_B(ie
), 0, !cond
);
647 /* else go through */
649 discharge2anyreg(fs
, e
);
651 return condjump(fs
, OP_TESTSET
, NO_REG
, e
->u
.info
, cond
);
655 void luaK_goiftrue (FuncState
*fs
, expdesc
*e
) {
656 int pc
; /* pc of last jump */
657 luaK_dischargevars(fs
, e
);
664 case VK
: case VKFLT
: case VKINT
: case VTRUE
: {
665 pc
= NO_JUMP
; /* always true; do nothing */
669 pc
= jumponcond(fs
, e
, 0);
673 luaK_concat(fs
, &e
->f
, pc
); /* insert last jump in 'f' list */
674 luaK_patchtohere(fs
, e
->t
);
679 void luaK_goiffalse (FuncState
*fs
, expdesc
*e
) {
680 int pc
; /* pc of last jump */
681 luaK_dischargevars(fs
, e
);
687 case VNIL
: case VFALSE
: {
688 pc
= NO_JUMP
; /* always false; do nothing */
692 pc
= jumponcond(fs
, e
, 1);
696 luaK_concat(fs
, &e
->t
, pc
); /* insert last jump in 't' list */
697 luaK_patchtohere(fs
, e
->f
);
702 static void codenot (FuncState
*fs
, expdesc
*e
) {
703 luaK_dischargevars(fs
, e
);
705 case VNIL
: case VFALSE
: {
709 case VK
: case VKFLT
: case VKINT
: case VTRUE
: {
719 discharge2anyreg(fs
, e
);
721 e
->u
.info
= luaK_codeABC(fs
, OP_NOT
, 0, e
->u
.info
, 0);
726 lua_assert(0); /* cannot happen */
730 /* interchange true and false lists */
731 { int temp
= e
->f
; e
->f
= e
->t
; e
->t
= temp
; }
732 removevalues(fs
, e
->f
);
733 removevalues(fs
, e
->t
);
737 void luaK_indexed (FuncState
*fs
, expdesc
*t
, expdesc
*k
) {
738 lua_assert(!hasjumps(t
));
739 t
->u
.ind
.t
= t
->u
.info
;
740 t
->u
.ind
.idx
= luaK_exp2RK(fs
, k
);
741 t
->u
.ind
.vt
= (t
->k
== VUPVAL
) ? VUPVAL
742 : check_exp(vkisinreg(t
->k
), VLOCAL
);
748 ** return false if folding can raise an error
750 static int validop (int op
, TValue
*v1
, TValue
*v2
) {
752 case LUA_OPBAND
: case LUA_OPBOR
: case LUA_OPBXOR
:
753 case LUA_OPSHL
: case LUA_OPSHR
: case LUA_OPBNOT
: { /* conversion errors */
755 return (tointeger(v1
, &i
) && tointeger(v2
, &i
));
757 case LUA_OPDIV
: case LUA_OPIDIV
: case LUA_OPMOD
: /* division by 0 */
758 return (nvalue(v2
) != 0);
759 default: return 1; /* everything else is valid */
765 ** Try to "constant-fold" an operation; return 1 iff successful
767 static int constfolding (FuncState
*fs
, int op
, expdesc
*e1
, expdesc
*e2
) {
769 if (!tonumeral(e1
, &v1
) || !tonumeral(e2
, &v2
) || !validop(op
, &v1
, &v2
))
770 return 0; /* non-numeric operands or not safe to fold */
771 luaO_arith(fs
->ls
->L
, op
, &v1
, &v2
, &res
); /* does operation */
772 if (ttisinteger(&res
)) {
774 e1
->u
.ival
= ivalue(&res
);
776 else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */
777 lua_Number n
= fltvalue(&res
);
778 if (luai_numisnan(n
) || n
== 0)
788 ** Code for binary and unary expressions that "produce values"
789 ** (arithmetic operations, bitwise operations, concat, length). First
790 ** try to do constant folding (only for numeric [arithmetic and
791 ** bitwise] operations, which is what 'lua_arith' accepts).
792 ** Expression to produce final result will be encoded in 'e1'.
794 static void codeexpval (FuncState
*fs
, OpCode op
,
795 expdesc
*e1
, expdesc
*e2
, int line
) {
796 lua_assert(op
>= OP_ADD
);
797 if (op
<= OP_BNOT
&& constfolding(fs
, (op
- OP_ADD
) + LUA_OPADD
, e1
, e2
))
798 return; /* result has been folded */
801 /* move operands to registers (if needed) */
802 if (op
== OP_UNM
|| op
== OP_BNOT
|| op
== OP_LEN
) { /* unary op? */
803 o2
= 0; /* no second expression */
804 o1
= luaK_exp2anyreg(fs
, e1
); /* cannot operate on constants */
806 else { /* regular case (binary operators) */
807 o2
= luaK_exp2RK(fs
, e2
); /* both operands are "RK" */
808 o1
= luaK_exp2RK(fs
, e1
);
810 if (o1
> o2
) { /* free registers in proper order */
818 e1
->u
.info
= luaK_codeABC(fs
, op
, 0, o1
, o2
); /* generate opcode */
819 e1
->k
= VRELOCABLE
; /* all those operations are relocable */
820 luaK_fixline(fs
, line
);
825 static void codecomp (FuncState
*fs
, OpCode op
, int cond
, expdesc
*e1
,
827 int o1
= luaK_exp2RK(fs
, e1
);
828 int o2
= luaK_exp2RK(fs
, e2
);
831 if (cond
== 0 && op
!= OP_EQ
) {
832 int temp
; /* exchange args to replace by '<' or '<=' */
833 temp
= o1
; o1
= o2
; o2
= temp
; /* o1 <==> o2 */
836 e1
->u
.info
= condjump(fs
, op
, cond
, o1
, o2
);
841 void luaK_prefix (FuncState
*fs
, UnOpr op
, expdesc
*e
, int line
) {
843 e2
.t
= e2
.f
= NO_JUMP
; e2
.k
= VKINT
; e2
.u
.ival
= 0;
845 case OPR_MINUS
: case OPR_BNOT
: case OPR_LEN
: {
846 codeexpval(fs
, cast(OpCode
, (op
- OPR_MINUS
) + OP_UNM
), e
, &e2
, line
);
849 case OPR_NOT
: codenot(fs
, e
); break;
850 default: lua_assert(0);
855 void luaK_infix (FuncState
*fs
, BinOpr op
, expdesc
*v
) {
858 luaK_goiftrue(fs
, v
);
862 luaK_goiffalse(fs
, v
);
866 luaK_exp2nextreg(fs
, v
); /* operand must be on the 'stack' */
869 case OPR_ADD
: case OPR_SUB
:
870 case OPR_MUL
: case OPR_DIV
: case OPR_IDIV
:
871 case OPR_MOD
: case OPR_POW
:
872 case OPR_BAND
: case OPR_BOR
: case OPR_BXOR
:
873 case OPR_SHL
: case OPR_SHR
: {
874 if (!tonumeral(v
, NULL
)) luaK_exp2RK(fs
, v
);
885 void luaK_posfix (FuncState
*fs
, BinOpr op
,
886 expdesc
*e1
, expdesc
*e2
, int line
) {
889 lua_assert(e1
->t
== NO_JUMP
); /* list must be closed */
890 luaK_dischargevars(fs
, e2
);
891 luaK_concat(fs
, &e2
->f
, e1
->f
);
896 lua_assert(e1
->f
== NO_JUMP
); /* list must be closed */
897 luaK_dischargevars(fs
, e2
);
898 luaK_concat(fs
, &e2
->t
, e1
->t
);
903 luaK_exp2val(fs
, e2
);
904 if (e2
->k
== VRELOCABLE
&& GET_OPCODE(getcode(fs
, e2
)) == OP_CONCAT
) {
905 lua_assert(e1
->u
.info
== GETARG_B(getcode(fs
, e2
))-1);
907 SETARG_B(getcode(fs
, e2
), e1
->u
.info
);
908 e1
->k
= VRELOCABLE
; e1
->u
.info
= e2
->u
.info
;
911 luaK_exp2nextreg(fs
, e2
); /* operand must be on the 'stack' */
912 codeexpval(fs
, OP_CONCAT
, e1
, e2
, line
);
916 case OPR_ADD
: case OPR_SUB
: case OPR_MUL
: case OPR_DIV
:
917 case OPR_IDIV
: case OPR_MOD
: case OPR_POW
:
918 case OPR_BAND
: case OPR_BOR
: case OPR_BXOR
:
919 case OPR_SHL
: case OPR_SHR
: {
920 codeexpval(fs
, cast(OpCode
, (op
- OPR_ADD
) + OP_ADD
), e1
, e2
, line
);
923 case OPR_EQ
: case OPR_LT
: case OPR_LE
: {
924 codecomp(fs
, cast(OpCode
, (op
- OPR_EQ
) + OP_EQ
), 1, e1
, e2
);
927 case OPR_NE
: case OPR_GT
: case OPR_GE
: {
928 codecomp(fs
, cast(OpCode
, (op
- OPR_NE
) + OP_EQ
), 0, e1
, e2
);
931 default: lua_assert(0);
936 void luaK_fixline (FuncState
*fs
, int line
) {
937 fs
->f
->lineinfo
[fs
->pc
- 1] = line
;
941 void luaK_setlist (FuncState
*fs
, int base
, int nelems
, int tostore
) {
942 int c
= (nelems
- 1)/LFIELDS_PER_FLUSH
+ 1;
943 int b
= (tostore
== LUA_MULTRET
) ? 0 : tostore
;
944 lua_assert(tostore
!= 0);
946 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, c
);
947 else if (c
<= MAXARG_Ax
) {
948 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, 0);
952 luaX_syntaxerror(fs
->ls
, "constructor too long");
953 fs
->freereg
= base
+ 1; /* free registers with list values */