]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.3/src/lcode.c
2 ** $Id: lcode.c,v 2.109 2016/05/13 19:09:21 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)
40 ** If expression is a numeric constant, fills 'v' with its value
41 ** and returns 1. Otherwise, returns 0.
43 static int tonumeral(expdesc
*e
, TValue
*v
) {
45 return 0; /* not a numeral */
48 if (v
) setivalue(v
, e
->u
.ival
);
51 if (v
) setfltvalue(v
, e
->u
.nval
);
59 ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
60 ** instruction is also OP_LOADNIL and ranges are compatible, adjust
61 ** range of previous instruction instead of emitting a new one. (For
62 ** instance, 'local a; local b' will generate a single opcode.)
64 void luaK_nil (FuncState
*fs
, int from
, int n
) {
65 Instruction
*previous
;
66 int l
= from
+ n
- 1; /* last register to set nil */
67 if (fs
->pc
> fs
->lasttarget
) { /* no jumps to current position? */
68 previous
= &fs
->f
->code
[fs
->pc
-1];
69 if (GET_OPCODE(*previous
) == OP_LOADNIL
) { /* previous is LOADNIL? */
70 int pfrom
= GETARG_A(*previous
); /* get previous range */
71 int pl
= pfrom
+ GETARG_B(*previous
);
72 if ((pfrom
<= from
&& from
<= pl
+ 1) ||
73 (from
<= pfrom
&& pfrom
<= l
+ 1)) { /* can connect both? */
74 if (pfrom
< from
) from
= pfrom
; /* from = min(from, pfrom) */
75 if (pl
> l
) l
= pl
; /* l = max(l, pl) */
76 SETARG_A(*previous
, from
);
77 SETARG_B(*previous
, l
- from
);
80 } /* else go through */
82 luaK_codeABC(fs
, OP_LOADNIL
, from
, n
- 1, 0); /* else no optimization */
87 ** Gets the destination address of a jump instruction. Used to traverse
90 static int getjump (FuncState
*fs
, int pc
) {
91 int offset
= GETARG_sBx(fs
->f
->code
[pc
]);
92 if (offset
== NO_JUMP
) /* point to itself represents end of list */
93 return NO_JUMP
; /* end of list */
95 return (pc
+1)+offset
; /* turn offset into absolute position */
100 ** Fix jump instruction at position 'pc' to jump to 'dest'.
101 ** (Jump addresses are relative in Lua)
103 static void fixjump (FuncState
*fs
, int pc
, int dest
) {
104 Instruction
*jmp
= &fs
->f
->code
[pc
];
105 int offset
= dest
- (pc
+ 1);
106 lua_assert(dest
!= NO_JUMP
);
107 if (abs(offset
) > MAXARG_sBx
)
108 luaX_syntaxerror(fs
->ls
, "control structure too long");
109 SETARG_sBx(*jmp
, offset
);
114 ** Concatenate jump-list 'l2' into jump-list 'l1'
116 void luaK_concat (FuncState
*fs
, int *l1
, int l2
) {
117 if (l2
== NO_JUMP
) return; /* nothing to concatenate? */
118 else if (*l1
== NO_JUMP
) /* no original list? */
119 *l1
= l2
; /* 'l1' points to 'l2' */
123 while ((next
= getjump(fs
, list
)) != NO_JUMP
) /* find last element */
125 fixjump(fs
, list
, l2
); /* last element links to 'l2' */
131 ** Create a jump instruction and return its position, so its destination
132 ** can be fixed later (with 'fixjump'). If there are jumps to
133 ** this position (kept in 'jpc'), link them all together so that
134 ** 'patchlistaux' will fix all them directly to the final destination.
136 int luaK_jump (FuncState
*fs
) {
137 int jpc
= fs
->jpc
; /* save list of jumps to here */
139 fs
->jpc
= NO_JUMP
; /* no more jumps to here */
140 j
= luaK_codeAsBx(fs
, OP_JMP
, 0, NO_JUMP
);
141 luaK_concat(fs
, &j
, jpc
); /* keep them on hold */
147 ** Code a 'return' instruction
149 void luaK_ret (FuncState
*fs
, int first
, int nret
) {
150 luaK_codeABC(fs
, OP_RETURN
, first
, nret
+1, 0);
155 ** Code a "conditional jump", that is, a test or comparison opcode
156 ** followed by a jump. Return jump position.
158 static int condjump (FuncState
*fs
, OpCode op
, int A
, int B
, int C
) {
159 luaK_codeABC(fs
, op
, A
, B
, C
);
160 return luaK_jump(fs
);
165 ** returns current 'pc' and marks it as a jump target (to avoid wrong
166 ** optimizations with consecutive instructions not in the same basic block).
168 int luaK_getlabel (FuncState
*fs
) {
169 fs
->lasttarget
= fs
->pc
;
175 ** Returns the position of the instruction "controlling" a given
176 ** jump (that is, its condition), or the jump itself if it is
179 static Instruction
*getjumpcontrol (FuncState
*fs
, int pc
) {
180 Instruction
*pi
= &fs
->f
->code
[pc
];
181 if (pc
>= 1 && testTMode(GET_OPCODE(*(pi
-1))))
189 ** Patch destination register for a TESTSET instruction.
190 ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
191 ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
192 ** register. Otherwise, change instruction to a simple 'TEST' (produces
193 ** no register value)
195 static int patchtestreg (FuncState
*fs
, int node
, int reg
) {
196 Instruction
*i
= getjumpcontrol(fs
, node
);
197 if (GET_OPCODE(*i
) != OP_TESTSET
)
198 return 0; /* cannot patch other instructions */
199 if (reg
!= NO_REG
&& reg
!= GETARG_B(*i
))
202 /* no register to put value or register already has the value;
203 change instruction to simple test */
204 *i
= CREATE_ABC(OP_TEST
, GETARG_B(*i
), 0, GETARG_C(*i
));
211 ** Traverse a list of tests ensuring no one produces a value
213 static void removevalues (FuncState
*fs
, int list
) {
214 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
))
215 patchtestreg(fs
, list
, NO_REG
);
220 ** Traverse a list of tests, patching their destination address and
221 ** registers: tests producing values jump to 'vtarget' (and put their
222 ** values in 'reg'), other tests jump to 'dtarget'.
224 static void patchlistaux (FuncState
*fs
, int list
, int vtarget
, int reg
,
226 while (list
!= NO_JUMP
) {
227 int next
= getjump(fs
, list
);
228 if (patchtestreg(fs
, list
, reg
))
229 fixjump(fs
, list
, vtarget
);
231 fixjump(fs
, list
, dtarget
); /* jump to default target */
238 ** Ensure all pending jumps to current position are fixed (jumping
239 ** to current position with no values) and reset list of pending
242 static void dischargejpc (FuncState
*fs
) {
243 patchlistaux(fs
, fs
->jpc
, fs
->pc
, NO_REG
, fs
->pc
);
249 ** Add elements in 'list' to list of pending jumps to "here"
250 ** (current position)
252 void luaK_patchtohere (FuncState
*fs
, int list
) {
253 luaK_getlabel(fs
); /* mark "here" as a jump target */
254 luaK_concat(fs
, &fs
->jpc
, list
);
259 ** Path all jumps in 'list' to jump to 'target'.
260 ** (The assert means that we cannot fix a jump to a forward address
261 ** because we only know addresses once code is generated.)
263 void luaK_patchlist (FuncState
*fs
, int list
, int target
) {
264 if (target
== fs
->pc
) /* 'target' is current position? */
265 luaK_patchtohere(fs
, list
); /* add list to pending jumps */
267 lua_assert(target
< fs
->pc
);
268 patchlistaux(fs
, list
, target
, NO_REG
, target
);
274 ** Path all jumps in 'list' to close upvalues up to given 'level'
275 ** (The assertion checks that jumps either were closing nothing
276 ** or were closing higher levels, from inner blocks.)
278 void luaK_patchclose (FuncState
*fs
, int list
, int level
) {
279 level
++; /* argument is +1 to reserve 0 as non-op */
280 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
)) {
281 lua_assert(GET_OPCODE(fs
->f
->code
[list
]) == OP_JMP
&&
282 (GETARG_A(fs
->f
->code
[list
]) == 0 ||
283 GETARG_A(fs
->f
->code
[list
]) >= level
));
284 SETARG_A(fs
->f
->code
[list
], level
);
290 ** Emit instruction 'i', checking for array sizes and saving also its
291 ** line information. Return 'i' position.
293 static int luaK_code (FuncState
*fs
, Instruction i
) {
295 dischargejpc(fs
); /* 'pc' will change */
296 /* put new instruction in code array */
297 luaM_growvector(fs
->ls
->L
, f
->code
, fs
->pc
, f
->sizecode
, Instruction
,
300 /* save corresponding line information */
301 luaM_growvector(fs
->ls
->L
, f
->lineinfo
, fs
->pc
, f
->sizelineinfo
, int,
303 f
->lineinfo
[fs
->pc
] = fs
->ls
->lastline
;
309 ** Format and emit an 'iABC' instruction. (Assertions check consistency
310 ** of parameters versus opcode.)
312 int luaK_codeABC (FuncState
*fs
, OpCode o
, int a
, int b
, int c
) {
313 lua_assert(getOpMode(o
) == iABC
);
314 lua_assert(getBMode(o
) != OpArgN
|| b
== 0);
315 lua_assert(getCMode(o
) != OpArgN
|| c
== 0);
316 lua_assert(a
<= MAXARG_A
&& b
<= MAXARG_B
&& c
<= MAXARG_C
);
317 return luaK_code(fs
, CREATE_ABC(o
, a
, b
, c
));
322 ** Format and emit an 'iABx' instruction.
324 int luaK_codeABx (FuncState
*fs
, OpCode o
, int a
, unsigned int bc
) {
325 lua_assert(getOpMode(o
) == iABx
|| getOpMode(o
) == iAsBx
);
326 lua_assert(getCMode(o
) == OpArgN
);
327 lua_assert(a
<= MAXARG_A
&& bc
<= MAXARG_Bx
);
328 return luaK_code(fs
, CREATE_ABx(o
, a
, bc
));
333 ** Emit an "extra argument" instruction (format 'iAx')
335 static int codeextraarg (FuncState
*fs
, int a
) {
336 lua_assert(a
<= MAXARG_Ax
);
337 return luaK_code(fs
, CREATE_Ax(OP_EXTRAARG
, a
));
342 ** Emit a "load constant" instruction, using either 'OP_LOADK'
343 ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
344 ** instruction with "extra argument".
346 int luaK_codek (FuncState
*fs
, int reg
, int k
) {
348 return luaK_codeABx(fs
, OP_LOADK
, reg
, k
);
350 int p
= luaK_codeABx(fs
, OP_LOADKX
, reg
, 0);
358 ** Check register-stack level, keeping track of its maximum size
359 ** in field 'maxstacksize'
361 void luaK_checkstack (FuncState
*fs
, int n
) {
362 int newstack
= fs
->freereg
+ n
;
363 if (newstack
> fs
->f
->maxstacksize
) {
364 if (newstack
>= MAXREGS
)
365 luaX_syntaxerror(fs
->ls
,
366 "function or expression needs too many registers");
367 fs
->f
->maxstacksize
= cast_byte(newstack
);
373 ** Reserve 'n' registers in register stack
375 void luaK_reserveregs (FuncState
*fs
, int n
) {
376 luaK_checkstack(fs
, n
);
382 ** Free register 'reg', if it is neither a constant index nor
386 static void freereg (FuncState
*fs
, int reg
) {
387 if (!ISK(reg
) && reg
>= fs
->nactvar
) {
389 lua_assert(reg
== fs
->freereg
);
395 ** Free register used by expression 'e' (if any)
397 static void freeexp (FuncState
*fs
, expdesc
*e
) {
398 if (e
->k
== VNONRELOC
)
399 freereg(fs
, e
->u
.info
);
404 ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
407 static void freeexps (FuncState
*fs
, expdesc
*e1
, expdesc
*e2
) {
408 int r1
= (e1
->k
== VNONRELOC
) ? e1
->u
.info
: -1;
409 int r2
= (e2
->k
== VNONRELOC
) ? e2
->u
.info
: -1;
422 ** Add constant 'v' to prototype's list of constants (field 'k').
423 ** Use scanner's table to cache position of constants in constant list
424 ** and try to reuse constants. Because some values should not be used
425 ** as keys (nil cannot be a key, integer keys can collapse with float
426 ** keys), the caller must provide a useful 'key' for indexing the cache.
428 static int addk (FuncState
*fs
, TValue
*key
, TValue
*v
) {
429 lua_State
*L
= fs
->ls
->L
;
431 TValue
*idx
= luaH_set(L
, fs
->ls
->h
, key
); /* index scanner table */
433 if (ttisinteger(idx
)) { /* is there an index there? */
434 k
= cast_int(ivalue(idx
));
435 /* correct value? (warning: must distinguish floats from integers!) */
436 if (k
< fs
->nk
&& ttype(&f
->k
[k
]) == ttype(v
) &&
437 luaV_rawequalobj(&f
->k
[k
], v
))
438 return k
; /* reuse index */
440 /* constant not found; create a new entry */
443 /* numerical value does not need GC barrier;
444 table has no metatable, so it does not need to invalidate cache */
446 luaM_growvector(L
, f
->k
, k
, f
->sizek
, TValue
, MAXARG_Ax
, "constants");
447 while (oldsize
< f
->sizek
) setnilvalue(&f
->k
[oldsize
++]);
448 setobj(L
, &f
->k
[k
], v
);
450 luaC_barrier(L
, f
, v
);
456 ** Add a string to list of constants and return its index.
458 int luaK_stringK (FuncState
*fs
, TString
*s
) {
460 setsvalue(fs
->ls
->L
, &o
, s
);
461 return addk(fs
, &o
, &o
); /* use string itself as key */
466 ** Add an integer to list of constants and return its index.
467 ** Integers use userdata as keys to avoid collision with floats with
468 ** same value; conversion to 'void*' is used only for hashing, so there
469 ** are no "precision" problems.
471 int luaK_intK (FuncState
*fs
, lua_Integer n
) {
473 setpvalue(&k
, cast(void*, cast(size_t, n
)));
475 return addk(fs
, &k
, &o
);
479 ** Add a float to list of constants and return its index.
481 static int luaK_numberK (FuncState
*fs
, lua_Number r
) {
484 return addk(fs
, &o
, &o
); /* use number itself as key */
489 ** Add a boolean to list of constants and return its index.
491 static int boolK (FuncState
*fs
, int b
) {
494 return addk(fs
, &o
, &o
); /* use boolean itself as key */
499 ** Add nil to list of constants and return its index.
501 static int nilK (FuncState
*fs
) {
504 /* cannot use nil as key; instead use table itself to represent nil */
505 sethvalue(fs
->ls
->L
, &k
, fs
->ls
->h
);
506 return addk(fs
, &k
, &v
);
511 ** Fix an expression to return the number of results 'nresults'.
512 ** Either 'e' is a multi-ret expression (function call or vararg)
513 ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that).
515 void luaK_setreturns (FuncState
*fs
, expdesc
*e
, int nresults
) {
516 if (e
->k
== VCALL
) { /* expression is an open function call? */
517 SETARG_C(getinstruction(fs
, e
), nresults
+ 1);
519 else if (e
->k
== VVARARG
) {
520 Instruction
*pc
= &getinstruction(fs
, e
);
521 SETARG_B(*pc
, nresults
+ 1);
522 SETARG_A(*pc
, fs
->freereg
);
523 luaK_reserveregs(fs
, 1);
525 else lua_assert(nresults
== LUA_MULTRET
);
530 ** Fix an expression to return one result.
531 ** If expression is not a multi-ret expression (function call or
532 ** vararg), it already returns one result, so nothing needs to be done.
533 ** Function calls become VNONRELOC expressions (as its result comes
534 ** fixed in the base register of the call), while vararg expressions
535 ** become VRELOCABLE (as OP_VARARG puts its results where it wants).
536 ** (Calls are created returning one result, so that does not need
539 void luaK_setoneret (FuncState
*fs
, expdesc
*e
) {
540 if (e
->k
== VCALL
) { /* expression is an open function call? */
541 /* already returns 1 value */
542 lua_assert(GETARG_C(getinstruction(fs
, e
)) == 2);
543 e
->k
= VNONRELOC
; /* result has fixed position */
544 e
->u
.info
= GETARG_A(getinstruction(fs
, e
));
546 else if (e
->k
== VVARARG
) {
547 SETARG_B(getinstruction(fs
, e
), 2);
548 e
->k
= VRELOCABLE
; /* can relocate its simple result */
554 ** Ensure that expression 'e' is not a variable.
556 void luaK_dischargevars (FuncState
*fs
, expdesc
*e
) {
558 case VLOCAL
: { /* already in a register */
559 e
->k
= VNONRELOC
; /* becomes a non-relocatable value */
562 case VUPVAL
: { /* move value to some (pending) register */
563 e
->u
.info
= luaK_codeABC(fs
, OP_GETUPVAL
, 0, e
->u
.info
, 0);
569 freereg(fs
, e
->u
.ind
.idx
);
570 if (e
->u
.ind
.vt
== VLOCAL
) { /* is 't' in a register? */
571 freereg(fs
, e
->u
.ind
.t
);
575 lua_assert(e
->u
.ind
.vt
== VUPVAL
);
576 op
= OP_GETTABUP
; /* 't' is in an upvalue */
578 e
->u
.info
= luaK_codeABC(fs
, op
, 0, e
->u
.ind
.t
, e
->u
.ind
.idx
);
582 case VVARARG
: case VCALL
: {
583 luaK_setoneret(fs
, e
);
586 default: break; /* there is one value available (somewhere) */
592 ** Ensures expression value is in register 'reg' (and therefore
593 ** 'e' will become a non-relocatable expression).
595 static void discharge2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
596 luaK_dischargevars(fs
, e
);
599 luaK_nil(fs
, reg
, 1);
602 case VFALSE
: case VTRUE
: {
603 luaK_codeABC(fs
, OP_LOADBOOL
, reg
, e
->k
== VTRUE
, 0);
607 luaK_codek(fs
, reg
, e
->u
.info
);
611 luaK_codek(fs
, reg
, luaK_numberK(fs
, e
->u
.nval
));
615 luaK_codek(fs
, reg
, luaK_intK(fs
, e
->u
.ival
));
619 Instruction
*pc
= &getinstruction(fs
, e
);
620 SETARG_A(*pc
, reg
); /* instruction will put result in 'reg' */
624 if (reg
!= e
->u
.info
)
625 luaK_codeABC(fs
, OP_MOVE
, reg
, e
->u
.info
, 0);
629 lua_assert(e
->k
== VJMP
);
630 return; /* nothing to do... */
639 ** Ensures expression value is in any register.
641 static void discharge2anyreg (FuncState
*fs
, expdesc
*e
) {
642 if (e
->k
!= VNONRELOC
) { /* no fixed register yet? */
643 luaK_reserveregs(fs
, 1); /* get a register */
644 discharge2reg(fs
, e
, fs
->freereg
-1); /* put value there */
649 static int code_loadbool (FuncState
*fs
, int A
, int b
, int jump
) {
650 luaK_getlabel(fs
); /* those instructions may be jump targets */
651 return luaK_codeABC(fs
, OP_LOADBOOL
, A
, b
, jump
);
656 ** check whether list has any jump that do not produce a value
657 ** or produce an inverted value
659 static int need_value (FuncState
*fs
, int list
) {
660 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
)) {
661 Instruction i
= *getjumpcontrol(fs
, list
);
662 if (GET_OPCODE(i
) != OP_TESTSET
) return 1;
664 return 0; /* not found */
669 ** Ensures final expression result (including results from its jump
670 ** lists) is in register 'reg'.
671 ** If expression has jumps, need to patch these jumps either to
672 ** its final position or to "load" instructions (for those tests
673 ** that do not produce values).
675 static void exp2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
676 discharge2reg(fs
, e
, reg
);
677 if (e
->k
== VJMP
) /* expression itself is a test? */
678 luaK_concat(fs
, &e
->t
, e
->u
.info
); /* put this jump in 't' list */
680 int final
; /* position after whole expression */
681 int p_f
= NO_JUMP
; /* position of an eventual LOAD false */
682 int p_t
= NO_JUMP
; /* position of an eventual LOAD true */
683 if (need_value(fs
, e
->t
) || need_value(fs
, e
->f
)) {
684 int fj
= (e
->k
== VJMP
) ? NO_JUMP
: luaK_jump(fs
);
685 p_f
= code_loadbool(fs
, reg
, 0, 1);
686 p_t
= code_loadbool(fs
, reg
, 1, 0);
687 luaK_patchtohere(fs
, fj
);
689 final
= luaK_getlabel(fs
);
690 patchlistaux(fs
, e
->f
, final
, reg
, p_f
);
691 patchlistaux(fs
, e
->t
, final
, reg
, p_t
);
693 e
->f
= e
->t
= NO_JUMP
;
700 ** Ensures final expression result (including results from its jump
701 ** lists) is in next available register.
703 void luaK_exp2nextreg (FuncState
*fs
, expdesc
*e
) {
704 luaK_dischargevars(fs
, e
);
706 luaK_reserveregs(fs
, 1);
707 exp2reg(fs
, e
, fs
->freereg
- 1);
712 ** Ensures final expression result (including results from its jump
713 ** lists) is in some (any) register and return that register.
715 int luaK_exp2anyreg (FuncState
*fs
, expdesc
*e
) {
716 luaK_dischargevars(fs
, e
);
717 if (e
->k
== VNONRELOC
) { /* expression already has a register? */
718 if (!hasjumps(e
)) /* no jumps? */
719 return e
->u
.info
; /* result is already in a register */
720 if (e
->u
.info
>= fs
->nactvar
) { /* reg. is not a local? */
721 exp2reg(fs
, e
, e
->u
.info
); /* put final result in it */
725 luaK_exp2nextreg(fs
, e
); /* otherwise, use next available register */
731 ** Ensures final expression result is either in a register or in an
734 void luaK_exp2anyregup (FuncState
*fs
, expdesc
*e
) {
735 if (e
->k
!= VUPVAL
|| hasjumps(e
))
736 luaK_exp2anyreg(fs
, e
);
741 ** Ensures final expression result is either in a register or it is
744 void luaK_exp2val (FuncState
*fs
, expdesc
*e
) {
746 luaK_exp2anyreg(fs
, e
);
748 luaK_dischargevars(fs
, e
);
753 ** Ensures final expression result is in a valid R/K index
754 ** (that is, it is either in a register or in 'k' with an index
755 ** in the range of R/K indices).
756 ** Returns R/K index.
758 int luaK_exp2RK (FuncState
*fs
, expdesc
*e
) {
760 switch (e
->k
) { /* move constants to 'k' */
761 case VTRUE
: e
->u
.info
= boolK(fs
, 1); goto vk
;
762 case VFALSE
: e
->u
.info
= boolK(fs
, 0); goto vk
;
763 case VNIL
: e
->u
.info
= nilK(fs
); goto vk
;
764 case VKINT
: e
->u
.info
= luaK_intK(fs
, e
->u
.ival
); goto vk
;
765 case VKFLT
: e
->u
.info
= luaK_numberK(fs
, e
->u
.nval
); goto vk
;
769 if (e
->u
.info
<= MAXINDEXRK
) /* constant fits in 'argC'? */
770 return RKASK(e
->u
.info
);
774 /* not a constant in the right range: put it in a register */
775 return luaK_exp2anyreg(fs
, e
);
780 ** Generate code to store result of expression 'ex' into variable 'var'.
782 void luaK_storevar (FuncState
*fs
, expdesc
*var
, expdesc
*ex
) {
786 exp2reg(fs
, ex
, var
->u
.info
); /* compute 'ex' into proper place */
790 int e
= luaK_exp2anyreg(fs
, ex
);
791 luaK_codeABC(fs
, OP_SETUPVAL
, e
, var
->u
.info
, 0);
795 OpCode op
= (var
->u
.ind
.vt
== VLOCAL
) ? OP_SETTABLE
: OP_SETTABUP
;
796 int e
= luaK_exp2RK(fs
, ex
);
797 luaK_codeABC(fs
, op
, var
->u
.ind
.t
, var
->u
.ind
.idx
, e
);
800 default: lua_assert(0); /* invalid var kind to store */
807 ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
809 void luaK_self (FuncState
*fs
, expdesc
*e
, expdesc
*key
) {
811 luaK_exp2anyreg(fs
, e
);
812 ereg
= e
->u
.info
; /* register where 'e' was placed */
814 e
->u
.info
= fs
->freereg
; /* base register for op_self */
815 e
->k
= VNONRELOC
; /* self expression has a fixed register */
816 luaK_reserveregs(fs
, 2); /* function and 'self' produced by op_self */
817 luaK_codeABC(fs
, OP_SELF
, e
->u
.info
, ereg
, luaK_exp2RK(fs
, key
));
823 ** Negate condition 'e' (where 'e' is a comparison).
825 static void negatecondition (FuncState
*fs
, expdesc
*e
) {
826 Instruction
*pc
= getjumpcontrol(fs
, e
->u
.info
);
827 lua_assert(testTMode(GET_OPCODE(*pc
)) && GET_OPCODE(*pc
) != OP_TESTSET
&&
828 GET_OPCODE(*pc
) != OP_TEST
);
829 SETARG_A(*pc
, !(GETARG_A(*pc
)));
834 ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
835 ** is true, code will jump if 'e' is true.) Return jump position.
836 ** Optimize when 'e' is 'not' something, inverting the condition
837 ** and removing the 'not'.
839 static int jumponcond (FuncState
*fs
, expdesc
*e
, int cond
) {
840 if (e
->k
== VRELOCABLE
) {
841 Instruction ie
= getinstruction(fs
, e
);
842 if (GET_OPCODE(ie
) == OP_NOT
) {
843 fs
->pc
--; /* remove previous OP_NOT */
844 return condjump(fs
, OP_TEST
, GETARG_B(ie
), 0, !cond
);
846 /* else go through */
848 discharge2anyreg(fs
, e
);
850 return condjump(fs
, OP_TESTSET
, NO_REG
, e
->u
.info
, cond
);
855 ** Emit code to go through if 'e' is true, jump otherwise.
857 void luaK_goiftrue (FuncState
*fs
, expdesc
*e
) {
858 int pc
; /* pc of new jump */
859 luaK_dischargevars(fs
, e
);
861 case VJMP
: { /* condition? */
862 negatecondition(fs
, e
); /* jump when it is false */
863 pc
= e
->u
.info
; /* save jump position */
866 case VK
: case VKFLT
: case VKINT
: case VTRUE
: {
867 pc
= NO_JUMP
; /* always true; do nothing */
871 pc
= jumponcond(fs
, e
, 0); /* jump when false */
875 luaK_concat(fs
, &e
->f
, pc
); /* insert new jump in false list */
876 luaK_patchtohere(fs
, e
->t
); /* true list jumps to here (to go through) */
882 ** Emit code to go through if 'e' is false, jump otherwise.
884 void luaK_goiffalse (FuncState
*fs
, expdesc
*e
) {
885 int pc
; /* pc of new jump */
886 luaK_dischargevars(fs
, e
);
889 pc
= e
->u
.info
; /* already jump if true */
892 case VNIL
: case VFALSE
: {
893 pc
= NO_JUMP
; /* always false; do nothing */
897 pc
= jumponcond(fs
, e
, 1); /* jump if true */
901 luaK_concat(fs
, &e
->t
, pc
); /* insert new jump in 't' list */
902 luaK_patchtohere(fs
, e
->f
); /* false list jumps to here (to go through) */
908 ** Code 'not e', doing constant folding.
910 static void codenot (FuncState
*fs
, expdesc
*e
) {
911 luaK_dischargevars(fs
, e
);
913 case VNIL
: case VFALSE
: {
914 e
->k
= VTRUE
; /* true == not nil == not false */
917 case VK
: case VKFLT
: case VKINT
: case VTRUE
: {
918 e
->k
= VFALSE
; /* false == not "x" == not 0.5 == not 1 == not true */
922 negatecondition(fs
, e
);
927 discharge2anyreg(fs
, e
);
929 e
->u
.info
= luaK_codeABC(fs
, OP_NOT
, 0, e
->u
.info
, 0);
933 default: lua_assert(0); /* cannot happen */
935 /* interchange true and false lists */
936 { int temp
= e
->f
; e
->f
= e
->t
; e
->t
= temp
; }
937 removevalues(fs
, e
->f
); /* values are useless when negated */
938 removevalues(fs
, e
->t
);
943 ** Create expression 't[k]'. 't' must have its final result already in a
944 ** register or upvalue.
946 void luaK_indexed (FuncState
*fs
, expdesc
*t
, expdesc
*k
) {
947 lua_assert(!hasjumps(t
) && (vkisinreg(t
->k
) || t
->k
== VUPVAL
));
948 t
->u
.ind
.t
= t
->u
.info
; /* register or upvalue index */
949 t
->u
.ind
.idx
= luaK_exp2RK(fs
, k
); /* R/K index for key */
950 t
->u
.ind
.vt
= (t
->k
== VUPVAL
) ? VUPVAL
: VLOCAL
;
956 ** Return false if folding can raise an error.
957 ** Bitwise operations need operands convertible to integers; division
958 ** operations cannot have 0 as divisor.
960 static int validop (int op
, TValue
*v1
, TValue
*v2
) {
962 case LUA_OPBAND
: case LUA_OPBOR
: case LUA_OPBXOR
:
963 case LUA_OPSHL
: case LUA_OPSHR
: case LUA_OPBNOT
: { /* conversion errors */
965 return (tointeger(v1
, &i
) && tointeger(v2
, &i
));
967 case LUA_OPDIV
: case LUA_OPIDIV
: case LUA_OPMOD
: /* division by 0 */
968 return (nvalue(v2
) != 0);
969 default: return 1; /* everything else is valid */
975 ** Try to "constant-fold" an operation; return 1 iff successful.
976 ** (In this case, 'e1' has the final result.)
978 static int constfolding (FuncState
*fs
, int op
, expdesc
*e1
, expdesc
*e2
) {
980 if (!tonumeral(e1
, &v1
) || !tonumeral(e2
, &v2
) || !validop(op
, &v1
, &v2
))
981 return 0; /* non-numeric operands or not safe to fold */
982 luaO_arith(fs
->ls
->L
, op
, &v1
, &v2
, &res
); /* does operation */
983 if (ttisinteger(&res
)) {
985 e1
->u
.ival
= ivalue(&res
);
987 else { /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
988 lua_Number n
= fltvalue(&res
);
989 if (luai_numisnan(n
) || n
== 0)
999 ** Emit code for unary expressions that "produce values"
1000 ** (everything but 'not').
1001 ** Expression to produce final result will be encoded in 'e'.
1003 static void codeunexpval (FuncState
*fs
, OpCode op
, expdesc
*e
, int line
) {
1004 int r
= luaK_exp2anyreg(fs
, e
); /* opcodes operate only on registers */
1006 e
->u
.info
= luaK_codeABC(fs
, op
, 0, r
, 0); /* generate opcode */
1007 e
->k
= VRELOCABLE
; /* all those operations are relocatable */
1008 luaK_fixline(fs
, line
);
1013 ** Emit code for binary expressions that "produce values"
1014 ** (everything but logical operators 'and'/'or' and comparison
1016 ** Expression to produce final result will be encoded in 'e1'.
1018 static void codebinexpval (FuncState
*fs
, OpCode op
,
1019 expdesc
*e1
, expdesc
*e2
, int line
) {
1020 int rk1
= luaK_exp2RK(fs
, e1
); /* both operands are "RK" */
1021 int rk2
= luaK_exp2RK(fs
, e2
);
1022 freeexps(fs
, e1
, e2
);
1023 e1
->u
.info
= luaK_codeABC(fs
, op
, 0, rk1
, rk2
); /* generate opcode */
1024 e1
->k
= VRELOCABLE
; /* all those operations are relocatable */
1025 luaK_fixline(fs
, line
);
1030 ** Emit code for comparisons.
1031 ** 'e1' was already put in R/K form by 'luaK_infix'.
1033 static void codecomp (FuncState
*fs
, BinOpr opr
, expdesc
*e1
, expdesc
*e2
) {
1034 int rk1
= (e1
->k
== VK
) ? RKASK(e1
->u
.info
)
1035 : check_exp(e1
->k
== VNONRELOC
, e1
->u
.info
);
1036 int rk2
= luaK_exp2RK(fs
, e2
);
1037 freeexps(fs
, e1
, e2
);
1039 case OPR_NE
: { /* '(a ~= b)' ==> 'not (a == b)' */
1040 e1
->u
.info
= condjump(fs
, OP_EQ
, 0, rk1
, rk2
);
1043 case OPR_GT
: case OPR_GE
: {
1044 /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */
1045 OpCode op
= cast(OpCode
, (opr
- OPR_NE
) + OP_EQ
);
1046 e1
->u
.info
= condjump(fs
, op
, 1, rk2
, rk1
); /* invert operands */
1049 default: { /* '==', '<', '<=' use their own opcodes */
1050 OpCode op
= cast(OpCode
, (opr
- OPR_EQ
) + OP_EQ
);
1051 e1
->u
.info
= condjump(fs
, op
, 1, rk1
, rk2
);
1060 ** Aplly prefix operation 'op' to expression 'e'.
1062 void luaK_prefix (FuncState
*fs
, UnOpr op
, expdesc
*e
, int line
) {
1063 static expdesc ef
= {VKINT
, {0}, NO_JUMP
, NO_JUMP
}; /* fake 2nd operand */
1065 case OPR_MINUS
: case OPR_BNOT
:
1066 if (constfolding(fs
, op
+ LUA_OPUNM
, e
, &ef
))
1070 codeunexpval(fs
, cast(OpCode
, op
+ OP_UNM
), e
, line
);
1072 case OPR_NOT
: codenot(fs
, e
); break;
1073 default: lua_assert(0);
1079 ** Process 1st operand 'v' of binary operation 'op' before reading
1082 void luaK_infix (FuncState
*fs
, BinOpr op
, expdesc
*v
) {
1085 luaK_goiftrue(fs
, v
); /* go ahead only if 'v' is true */
1089 luaK_goiffalse(fs
, v
); /* go ahead only if 'v' is false */
1093 luaK_exp2nextreg(fs
, v
); /* operand must be on the 'stack' */
1096 case OPR_ADD
: case OPR_SUB
:
1097 case OPR_MUL
: case OPR_DIV
: case OPR_IDIV
:
1098 case OPR_MOD
: case OPR_POW
:
1099 case OPR_BAND
: case OPR_BOR
: case OPR_BXOR
:
1100 case OPR_SHL
: case OPR_SHR
: {
1101 if (!tonumeral(v
, NULL
))
1103 /* else keep numeral, which may be folded with 2nd operand */
1115 ** Finalize code for binary operation, after reading 2nd operand.
1116 ** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
1117 ** concatenation is right associative), merge second CONCAT into first
1120 void luaK_posfix (FuncState
*fs
, BinOpr op
,
1121 expdesc
*e1
, expdesc
*e2
, int line
) {
1124 lua_assert(e1
->t
== NO_JUMP
); /* list closed by 'luK_infix' */
1125 luaK_dischargevars(fs
, e2
);
1126 luaK_concat(fs
, &e2
->f
, e1
->f
);
1131 lua_assert(e1
->f
== NO_JUMP
); /* list closed by 'luK_infix' */
1132 luaK_dischargevars(fs
, e2
);
1133 luaK_concat(fs
, &e2
->t
, e1
->t
);
1138 luaK_exp2val(fs
, e2
);
1139 if (e2
->k
== VRELOCABLE
&&
1140 GET_OPCODE(getinstruction(fs
, e2
)) == OP_CONCAT
) {
1141 lua_assert(e1
->u
.info
== GETARG_B(getinstruction(fs
, e2
))-1);
1143 SETARG_B(getinstruction(fs
, e2
), e1
->u
.info
);
1144 e1
->k
= VRELOCABLE
; e1
->u
.info
= e2
->u
.info
;
1147 luaK_exp2nextreg(fs
, e2
); /* operand must be on the 'stack' */
1148 codebinexpval(fs
, OP_CONCAT
, e1
, e2
, line
);
1152 case OPR_ADD
: case OPR_SUB
: case OPR_MUL
: case OPR_DIV
:
1153 case OPR_IDIV
: case OPR_MOD
: case OPR_POW
:
1154 case OPR_BAND
: case OPR_BOR
: case OPR_BXOR
:
1155 case OPR_SHL
: case OPR_SHR
: {
1156 if (!constfolding(fs
, op
+ LUA_OPADD
, e1
, e2
))
1157 codebinexpval(fs
, cast(OpCode
, op
+ OP_ADD
), e1
, e2
, line
);
1160 case OPR_EQ
: case OPR_LT
: case OPR_LE
:
1161 case OPR_NE
: case OPR_GT
: case OPR_GE
: {
1162 codecomp(fs
, op
, e1
, e2
);
1165 default: lua_assert(0);
1171 ** Change line information associated with current position.
1173 void luaK_fixline (FuncState
*fs
, int line
) {
1174 fs
->f
->lineinfo
[fs
->pc
- 1] = line
;
1179 ** Emit a SETLIST instruction.
1180 ** 'base' is register that keeps table;
1181 ** 'nelems' is #table plus those to be stored now;
1182 ** 'tostore' is number of values (in registers 'base + 1',...) to add to
1183 ** table (or LUA_MULTRET to add up to stack top).
1185 void luaK_setlist (FuncState
*fs
, int base
, int nelems
, int tostore
) {
1186 int c
= (nelems
- 1)/LFIELDS_PER_FLUSH
+ 1;
1187 int b
= (tostore
== LUA_MULTRET
) ? 0 : tostore
;
1188 lua_assert(tostore
!= 0 && tostore
<= LFIELDS_PER_FLUSH
);
1190 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, c
);
1191 else if (c
<= MAXARG_Ax
) {
1192 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, 0);
1193 codeextraarg(fs
, c
);
1196 luaX_syntaxerror(fs
->ls
, "constructor too long");
1197 fs
->freereg
= base
+ 1; /* free registers with list values */