2 ** $Id: lcode.c,v 2.25a 2006/03/21 19:28:49 roberto Exp $
3 ** Code generator for Lua
4 ** See Copyright Notice in lua.h
28 #define hasjumps(e) ((e)->t != (e)->f)
30 static lu_bool
isnumeral(expdesc
*e
) {
39 return (ek
&& e
->t
== NO_JUMP
&& e
->f
== NO_JUMP
);
43 void luaK_nil (FuncState
*fs
, int from
, int n
) {
44 Instruction
*previous
;
45 if (fs
->pc
> fs
->lasttarget
) { /* no jumps to current position? */
46 if (fs
->pc
== 0) { /* function start? */
47 if (from
>= fs
->nactvar
)
48 return; /* positions are already clean */
51 previous
= &fs
->f
->code
[fs
->pc
-1];
52 if (GET_OPCODE(*previous
) == OP_LOADNIL
) {
53 int pfrom
= GETARG_A(*previous
);
54 int pto
= GETARG_B(*previous
);
55 if (pfrom
<= from
&& from
<= pto
+1) { /* can connect both? */
57 SETARG_B(*previous
, from
+n
-1);
63 luaK_codeABC(fs
, OP_LOADNIL
, from
, from
+n
-1, 0); /* else no optimization */
67 int luaK_jump (FuncState
*fs
) {
68 int jpc
= fs
->jpc
; /* save list of jumps to here */
71 j
= luaK_codeAsBx(fs
, OP_JMP
, 0, NO_JUMP
);
72 luaK_concat(fs
, &j
, jpc
); /* keep them on hold */
77 void luaK_ret (FuncState
*fs
, int first
, int nret
) {
78 luaK_codeABC(fs
, OP_RETURN
, first
, nret
+1, 0);
82 static int condjump (FuncState
*fs
, OpCode op
, int A
, int B
, int C
) {
83 luaK_codeABC(fs
, op
, A
, B
, C
);
88 static void fixjump (FuncState
*fs
, int pc
, int dest
) {
89 Instruction
*jmp
= &fs
->f
->code
[pc
];
90 int offset
= dest
-(pc
+1);
91 lua_assert(dest
!= NO_JUMP
);
92 if (abs(offset
) > MAXARG_sBx
)
93 luaX_syntaxerror(fs
->ls
, "control structure too long");
94 SETARG_sBx(*jmp
, offset
);
99 ** returns current `pc' and marks it as a jump target (to avoid wrong
100 ** optimizations with consecutive instructions not in the same basic block).
102 int luaK_getlabel (FuncState
*fs
) {
103 fs
->lasttarget
= fs
->pc
;
108 static int getjump (FuncState
*fs
, int pc
) {
109 int offset
= GETARG_sBx(fs
->f
->code
[pc
]);
110 if (offset
== NO_JUMP
) /* point to itself represents end of list */
111 return NO_JUMP
; /* end of list */
113 return (pc
+1)+offset
; /* turn offset into absolute position */
117 static Instruction
*getjumpcontrol (FuncState
*fs
, int pc
) {
118 Instruction
*pi
= &fs
->f
->code
[pc
];
119 if (pc
>= 1 && testTMode(GET_OPCODE(*(pi
-1))))
127 ** check whether list has any jump that do not produce a value
128 ** (or produce an inverted value)
130 static lu_bool
need_value (FuncState
*fs
, int list
) {
131 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
)) {
132 Instruction i
= *getjumpcontrol(fs
, list
);
133 if (GET_OPCODE(i
) != OP_TESTSET
) return 1;
135 return 0; /* not found */
139 static lu_bool
patchtestreg (FuncState
*fs
, int node
, int reg
) {
140 Instruction
*i
= getjumpcontrol(fs
, node
);
141 if (GET_OPCODE(*i
) != OP_TESTSET
)
142 return 0; /* cannot patch other instructions */
143 if (reg
!= NO_REG
&& reg
!= GETARG_B(*i
))
145 else /* no register to put value or register already has the value */
146 *i
= CREATE_ABC(OP_TEST
, GETARG_B(*i
), 0, GETARG_C(*i
));
152 static void removevalues (FuncState
*fs
, int list
) {
153 for (; list
!= NO_JUMP
; list
= getjump(fs
, list
))
154 patchtestreg(fs
, list
, NO_REG
);
158 static void patchlistaux (FuncState
*fs
, int list
, int vtarget
, int reg
,
160 while (list
!= NO_JUMP
) {
161 int next
= getjump(fs
, list
);
162 if (patchtestreg(fs
, list
, reg
))
163 fixjump(fs
, list
, vtarget
);
165 fixjump(fs
, list
, dtarget
); /* jump to default target */
171 static void dischargejpc (FuncState
*fs
) {
172 patchlistaux(fs
, fs
->jpc
, fs
->pc
, NO_REG
, fs
->pc
);
177 void luaK_patchlist (FuncState
*fs
, int list
, int target
) {
178 if (target
== fs
->pc
)
179 luaK_patchtohere(fs
, list
);
181 lua_assert(target
< fs
->pc
);
182 patchlistaux(fs
, list
, target
, NO_REG
, target
);
187 void luaK_patchtohere (FuncState
*fs
, int list
) {
189 luaK_concat(fs
, &fs
->jpc
, list
);
193 void luaK_concat (FuncState
*fs
, int *l1
, int l2
) {
194 if (l2
== NO_JUMP
) return;
195 else if (*l1
== NO_JUMP
)
200 while ((next
= getjump(fs
, list
)) != NO_JUMP
) /* find last element */
202 fixjump(fs
, list
, l2
);
207 void luaK_checkstack (FuncState
*fs
, int n
) {
208 int newstack
= fs
->freereg
+ n
;
209 if (newstack
> fs
->f
->maxstacksize
) {
210 if (newstack
>= MAXSTACK
)
211 luaX_syntaxerror(fs
->ls
, "function or expression too complex");
212 fs
->f
->maxstacksize
= cast_byte(newstack
);
217 void luaK_reserveregs (FuncState
*fs
, int n
) {
218 luaK_checkstack(fs
, n
);
223 static void freereg (FuncState
*fs
, int reg
) {
224 if (!ISK(reg
) && reg
>= fs
->nactvar
) {
226 lua_assert(reg
== fs
->freereg
);
231 static void freeexp (FuncState
*fs
, expdesc
*e
) {
232 if (e
->k
== VNONRELOC
)
233 freereg(fs
, e
->u
.s
.info
);
237 static int addk (FuncState
*fs
, TValue
*k
, TValue
*v
) {
238 lua_State
*L
= fs
->L
;
239 TValue
*idx
= luaH_set(L
, fs
->h
, k
);
241 int oldsize
= f
->sizek
;
243 lua_assert( !ttisnumber_raw(idx
) );
244 if (ttisinteger(idx
)) {
245 lua_assert(luaO_rawequalObj(&fs
->f
->k
[ (int) ivalue(idx
) ], v
));
249 if (ttisnumber(idx
)) {
250 lua_assert(luaO_rawequalObj(&fs
->f
->k
[cast_int(nvalue(idx
))], v
));
251 return cast_int(nvalue(idx
));
254 else { /* constant not found; create a new entry */
255 setivalue(idx
, fs
->nk
);
256 luaM_growvector(L
, f
->k
, fs
->nk
, f
->sizek
, TValue
,
257 MAXARG_Bx
, "constant table overflow");
258 while (oldsize
< f
->sizek
) setnilvalue(&f
->k
[oldsize
++]);
259 setobj(L
, &f
->k
[fs
->nk
], v
);
260 luaC_barrier(L
, f
, v
);
266 int luaK_stringK (FuncState
*fs
, TString
*s
) {
268 setsvalue(fs
->L
, &o
, s
);
269 return addk(fs
, &o
, &o
);
273 int luaK_numberK (FuncState
*fs
, lua_Number r
) {
276 return addk(fs
, &o
, &o
);
281 int luaK_integerK (FuncState
*fs
, lua_Integer r
) {
284 return addk(fs
, &o
, &o
);
290 static int luaK_imagK (FuncState
*fs
, lua_Number r
) {
292 setnvalue_complex(&o
, r
*I
);
293 return addk(fs
, &o
, &o
);
297 static int boolK (FuncState
*fs
, int b
) {
300 return addk(fs
, &o
, &o
);
304 static int nilK (FuncState
*fs
) {
307 /* cannot use nil as key; instead use table itself to represent nil */
308 sethvalue(fs
->L
, &k
, fs
->h
);
309 return addk(fs
, &k
, &v
);
313 void luaK_setreturns (FuncState
*fs
, expdesc
*e
, int nresults
) {
314 if (e
->k
== VCALL
) { /* expression is an open function call? */
315 SETARG_C(getcode(fs
, e
), nresults
+1);
317 else if (e
->k
== VVARARG
) {
318 SETARG_B(getcode(fs
, e
), nresults
+1);
319 SETARG_A(getcode(fs
, e
), fs
->freereg
);
320 luaK_reserveregs(fs
, 1);
325 void luaK_setoneret (FuncState
*fs
, expdesc
*e
) {
326 if (e
->k
== VCALL
) { /* expression is an open function call? */
328 e
->u
.s
.info
= GETARG_A(getcode(fs
, e
));
330 else if (e
->k
== VVARARG
) {
331 SETARG_B(getcode(fs
, e
), 2);
332 e
->k
= VRELOCABLE
; /* can relocate its simple result */
337 void luaK_dischargevars (FuncState
*fs
, expdesc
*e
) {
344 e
->u
.s
.info
= luaK_codeABC(fs
, OP_GETUPVAL
, 0, e
->u
.s
.info
, 0);
349 e
->u
.s
.info
= luaK_codeABx(fs
, OP_GETGLOBAL
, 0, e
->u
.s
.info
);
354 freereg(fs
, e
->u
.s
.aux
);
355 freereg(fs
, e
->u
.s
.info
);
356 e
->u
.s
.info
= luaK_codeABC(fs
, OP_GETTABLE
, 0, e
->u
.s
.info
, e
->u
.s
.aux
);
362 luaK_setoneret(fs
, e
);
365 default: break; /* there is one value available (somewhere) */
370 static int code_label (FuncState
*fs
, int A
, int b
, int jump
) {
371 luaK_getlabel(fs
); /* those instructions may be jump targets */
372 return luaK_codeABC(fs
, OP_LOADBOOL
, A
, b
, jump
);
376 static void discharge2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
377 luaK_dischargevars(fs
, e
);
380 luaK_nil(fs
, reg
, 1);
383 case VFALSE
: case VTRUE
: {
384 luaK_codeABC(fs
, OP_LOADBOOL
, reg
, e
->k
== VTRUE
, 0);
388 luaK_codeABx(fs
, OP_LOADK
, reg
, e
->u
.s
.info
);
392 luaK_codeABx(fs
, OP_LOADK
, reg
, luaK_numberK(fs
, e
->u
.nval
));
397 luaK_codeABx(fs
, OP_LOADK
, reg
, luaK_integerK(fs
, e
->u
.ival
));
403 luaK_codeABx(fs
, OP_LOADK
, reg
, luaK_imagK(fs
, e
->u
.nval
));
408 Instruction
*pc
= &getcode(fs
, e
);
413 if (reg
!= e
->u
.s
.info
)
414 luaK_codeABC(fs
, OP_MOVE
, reg
, e
->u
.s
.info
, 0);
418 lua_assert(e
->k
== VVOID
|| e
->k
== VJMP
);
419 return; /* nothing to do... */
427 static void discharge2anyreg (FuncState
*fs
, expdesc
*e
) {
428 if (e
->k
!= VNONRELOC
) {
429 luaK_reserveregs(fs
, 1);
430 discharge2reg(fs
, e
, fs
->freereg
-1);
435 static void exp2reg (FuncState
*fs
, expdesc
*e
, int reg
) {
436 discharge2reg(fs
, e
, reg
);
438 luaK_concat(fs
, &e
->t
, e
->u
.s
.info
); /* put this jump in `t' list */
440 int final
; /* position after whole expression */
441 int p_f
= NO_JUMP
; /* position of an eventual LOAD false */
442 int p_t
= NO_JUMP
; /* position of an eventual LOAD true */
443 if (need_value(fs
, e
->t
) || need_value(fs
, e
->f
)) {
444 int fj
= (e
->k
== VJMP
) ? NO_JUMP
: luaK_jump(fs
);
445 p_f
= code_label(fs
, reg
, 0, 1);
446 p_t
= code_label(fs
, reg
, 1, 0);
447 luaK_patchtohere(fs
, fj
);
449 final
= luaK_getlabel(fs
);
450 patchlistaux(fs
, e
->f
, final
, reg
, p_f
);
451 patchlistaux(fs
, e
->t
, final
, reg
, p_t
);
453 e
->f
= e
->t
= NO_JUMP
;
459 void luaK_exp2nextreg (FuncState
*fs
, expdesc
*e
) {
460 luaK_dischargevars(fs
, e
);
462 luaK_reserveregs(fs
, 1);
463 exp2reg(fs
, e
, fs
->freereg
- 1);
467 int luaK_exp2anyreg (FuncState
*fs
, expdesc
*e
) {
468 luaK_dischargevars(fs
, e
);
469 if (e
->k
== VNONRELOC
) {
470 if (!hasjumps(e
)) return e
->u
.s
.info
; /* exp is already in a register */
471 if (e
->u
.s
.info
>= fs
->nactvar
) { /* reg. is not a local? */
472 exp2reg(fs
, e
, e
->u
.s
.info
); /* put value on it */
476 luaK_exp2nextreg(fs
, e
); /* default */
481 void luaK_exp2val (FuncState
*fs
, expdesc
*e
) {
483 luaK_exp2anyreg(fs
, e
);
485 luaK_dischargevars(fs
, e
);
489 int luaK_exp2RK (FuncState
*fs
, expdesc
*e
) {
502 if (fs
->nk
<= MAXINDEXRK
) { /* constant fit in RK operand? */
503 e
->u
.s
.info
= (e
->k
== VNIL
) ? nilK(fs
) :
504 (e
->k
== VKNUM
) ? luaK_numberK(fs
, e
->u
.nval
) :
506 (e
->k
== VKINT
) ? luaK_integerK(fs
, e
->u
.ival
) :
509 (e
->k
== VKNUM2
) ? luaK_imagK(fs
, e
->u
.nval
) :
511 boolK(fs
, (e
->k
== VTRUE
));
513 return RKASK(e
->u
.s
.info
);
518 if (e
->u
.s
.info
<= MAXINDEXRK
) /* constant fit in argC? */
519 return RKASK(e
->u
.s
.info
);
524 /* not a constant in the right range: put it in a register */
525 return luaK_exp2anyreg(fs
, e
);
529 void luaK_storevar (FuncState
*fs
, expdesc
*var
, expdesc
*ex
) {
533 exp2reg(fs
, ex
, var
->u
.s
.info
);
537 int e
= luaK_exp2anyreg(fs
, ex
);
538 luaK_codeABC(fs
, OP_SETUPVAL
, e
, var
->u
.s
.info
, 0);
542 int e
= luaK_exp2anyreg(fs
, ex
);
543 luaK_codeABx(fs
, OP_SETGLOBAL
, e
, var
->u
.s
.info
);
547 int e
= luaK_exp2RK(fs
, ex
);
548 luaK_codeABC(fs
, OP_SETTABLE
, var
->u
.s
.info
, var
->u
.s
.aux
, e
);
552 lua_assert(0); /* invalid var kind to store */
560 void luaK_self (FuncState
*fs
, expdesc
*e
, expdesc
*key
) {
562 luaK_exp2anyreg(fs
, e
);
565 luaK_reserveregs(fs
, 2);
566 luaK_codeABC(fs
, OP_SELF
, func
, e
->u
.s
.info
, luaK_exp2RK(fs
, key
));
573 static void invertjump (FuncState
*fs
, expdesc
*e
) {
574 Instruction
*pc
= getjumpcontrol(fs
, e
->u
.s
.info
);
575 lua_assert(testTMode(GET_OPCODE(*pc
)) && GET_OPCODE(*pc
) != OP_TESTSET
&&
576 GET_OPCODE(*pc
) != OP_TEST
);
577 SETARG_A(*pc
, !(GETARG_A(*pc
)));
581 static int jumponcond (FuncState
*fs
, expdesc
*e
, int cond
) {
582 if (e
->k
== VRELOCABLE
) {
583 Instruction ie
= getcode(fs
, e
);
584 if (GET_OPCODE(ie
) == OP_NOT
) {
585 fs
->pc
--; /* remove previous OP_NOT */
586 return condjump(fs
, OP_TEST
, GETARG_B(ie
), 0, !cond
);
588 /* else go through */
590 discharge2anyreg(fs
, e
);
592 return condjump(fs
, OP_TESTSET
, NO_REG
, e
->u
.s
.info
, cond
);
596 void luaK_goiftrue (FuncState
*fs
, expdesc
*e
) {
597 int pc
; /* pc of last jump */
598 luaK_dischargevars(fs
, e
);
606 case VK
: case VKNUM
: case VTRUE
: {
607 pc
= NO_JUMP
; /* always true; do nothing */
611 pc
= luaK_jump(fs
); /* always jump */
620 pc
= jumponcond(fs
, e
, 0);
624 luaK_concat(fs
, &e
->f
, pc
); /* insert last jump in `f' list */
625 luaK_patchtohere(fs
, e
->t
);
630 static void luaK_goiffalse (FuncState
*fs
, expdesc
*e
) {
631 int pc
; /* pc of last jump */
632 luaK_dischargevars(fs
, e
);
634 case VNIL
: case VFALSE
: {
635 pc
= NO_JUMP
; /* always false; do nothing */
639 pc
= luaK_jump(fs
); /* always jump */
647 pc
= jumponcond(fs
, e
, 1);
651 luaK_concat(fs
, &e
->t
, pc
); /* insert last jump in `t' list */
652 luaK_patchtohere(fs
, e
->f
);
657 static void codenot (FuncState
*fs
, expdesc
*e
) {
658 luaK_dischargevars(fs
, e
);
660 case VNIL
: case VFALSE
: {
670 case VK
: case VKNUM
: case VTRUE
: {
680 discharge2anyreg(fs
, e
);
682 e
->u
.s
.info
= luaK_codeABC(fs
, OP_NOT
, 0, e
->u
.s
.info
, 0);
687 lua_assert(0); /* cannot happen */
691 /* interchange true and false lists */
692 { int temp
= e
->f
; e
->f
= e
->t
; e
->t
= temp
; }
693 removevalues(fs
, e
->f
);
694 removevalues(fs
, e
->t
);
698 void luaK_indexed (FuncState
*fs
, expdesc
*t
, expdesc
*k
) {
699 t
->u
.s
.aux
= luaK_exp2RK(fs
, k
);
704 static lu_bool
constfolding (OpCode op
, expdesc
*e1
, expdesc
*e2
) {
705 lua_Number v1
, v2
, r
;
707 if (!isnumeral(e1
) || !isnumeral(e2
)) return 0;
709 /* real and imaginary parts don't mix. */
711 if (e1
->k
== VKNUM2
) {
712 if ((op
!= OP_UNM
) && (e2
->k
!= VKNUM2
)) return 0;
714 else if (e2
->k
== VKNUM2
) { return 0; }
717 if ((e1
->k
== VKINT
) && (e2
->k
== VKINT
)) {
718 lua_Integer i1
= e1
->u
.ival
, i2
= e2
->u
.ival
;
721 /* Integer/integer calculations (maybe end up producing floating point) */
723 case OP_ADD
: done
= try_addint( &rr
, i1
, i2
); break;
724 case OP_SUB
: done
= try_subint( &rr
, i1
, i2
); break;
725 case OP_MUL
: done
= try_mulint( &rr
, i1
, i2
); break;
726 case OP_DIV
: done
= try_divint( &rr
, i1
, i2
); break;
727 case OP_MOD
: done
= try_modint( &rr
, i1
, i2
); break;
728 case OP_POW
: done
= try_powint( &rr
, i1
, i2
); break;
729 case OP_UNM
: done
= try_unmint( &rr
, i1
); break;
730 default: done
= 0; break;
733 e1
->u
.ival
= rr
; /* We remained within integer range, and accuracy! */
737 v1
= (e1
->k
== VKINT
) ? ((lua_Number
)e1
->u
.ival
) : e1
->u
.nval
;
738 v2
= (e2
->k
== VKINT
) ? ((lua_Number
)e2
->u
.ival
) : e2
->u
.nval
;
744 case OP_ADD
: r
= luai_numadd(v1
, v2
); break;
745 case OP_SUB
: r
= luai_numsub(v1
, v2
); break;
748 if (vkres
==VKNUM2
) return 0; /* leave to runtime (could do here, but not worth it?) */
750 r
= luai_nummul(v1
, v2
); break;
752 if (v2
== 0) return 0; /* do not attempt to divide by 0 */
754 if (vkres
==VKNUM2
) return 0; /* leave to runtime */
756 r
= luai_numdiv(v1
, v2
); break;
758 if (v2
== 0) return 0; /* do not attempt to divide by 0 */
760 if (vkres
==VKNUM2
) return 0; /* leave to runtime */
762 r
= luai_nummod(v1
, v2
); break;
765 if (vkres
==VKNUM2
) return 0; /* leave to runtime */
767 r
= luai_numpow(v1
, v2
); break;
768 case OP_UNM
: r
= luai_numunm(v1
); break;
769 case OP_LEN
: return 0; /* no constant folding for 'len' */
770 default: lua_assert(0); r
= 0; break;
772 if (luai_numisnan(r
)) return 0; /* do not attempt to produce NaN */
773 e1
->k
= (expkind
) vkres
; // (Visual C++ 2005 needs the cast, on 64-bit ints)
779 static void codearith (FuncState
*fs
, OpCode op
, expdesc
*e1
, expdesc
*e2
) {
780 if (constfolding(op
, e1
, e2
))
783 int o2
= (op
!= OP_UNM
&& op
!= OP_LEN
) ? luaK_exp2RK(fs
, e2
) : 0;
784 int o1
= luaK_exp2RK(fs
, e1
);
793 e1
->u
.s
.info
= luaK_codeABC(fs
, op
, 0, o1
, o2
);
799 static void codecomp (FuncState
*fs
, OpCode op
, int cond
, expdesc
*e1
,
801 int o1
= luaK_exp2RK(fs
, e1
);
802 int o2
= luaK_exp2RK(fs
, e2
);
805 if (cond
== 0 && op
!= OP_EQ
) {
806 int temp
; /* exchange args to replace by `<' or `<=' */
807 temp
= o1
; o1
= o2
; o2
= temp
; /* o1 <==> o2 */
810 e1
->u
.s
.info
= condjump(fs
, op
, cond
, o1
, o2
);
815 void luaK_prefix (FuncState
*fs
, UnOpr op
, expdesc
*e
) {
816 /* Q: is the 'e2' really needed; couldn't we use 'e' for both params like
817 * when calling 'Arith()' at lvm.c */
819 e2
.t
= e2
.f
= NO_JUMP
;
821 e2
.k
= VKINT
; e2
.u
.ival
= 0;
823 e2
.k
= VKNUM
; e2
.u
.nval
= 0;
828 luaK_exp2anyreg(fs
, e
); /* cannot operate on non-numeric constants */
829 codearith(fs
, OP_UNM
, e
, &e2
);
832 case OPR_NOT
: codenot(fs
, e
); break;
834 luaK_exp2anyreg(fs
, e
); /* cannot operate on constants */
835 codearith(fs
, OP_LEN
, e
, &e2
);
838 default: lua_assert(0);
843 void luaK_infix (FuncState
*fs
, BinOpr op
, expdesc
*v
) {
846 luaK_goiftrue(fs
, v
);
850 luaK_goiffalse(fs
, v
);
854 luaK_exp2nextreg(fs
, v
); /* operand must be on the `stack' */
857 case OPR_ADD
: case OPR_SUB
: case OPR_MUL
: case OPR_DIV
:
858 case OPR_MOD
: case OPR_POW
: {
859 if (!isnumeral(v
)) luaK_exp2RK(fs
, v
);
870 void luaK_posfix (FuncState
*fs
, BinOpr op
, expdesc
*e1
, expdesc
*e2
) {
873 lua_assert(e1
->t
== NO_JUMP
); /* list must be closed */
874 luaK_dischargevars(fs
, e2
);
875 luaK_concat(fs
, &e2
->f
, e1
->f
);
880 lua_assert(e1
->f
== NO_JUMP
); /* list must be closed */
881 luaK_dischargevars(fs
, e2
);
882 luaK_concat(fs
, &e2
->t
, e1
->t
);
887 luaK_exp2val(fs
, e2
);
888 if (e2
->k
== VRELOCABLE
&& GET_OPCODE(getcode(fs
, e2
)) == OP_CONCAT
) {
889 lua_assert(e1
->u
.s
.info
== GETARG_B(getcode(fs
, e2
))-1);
891 SETARG_B(getcode(fs
, e2
), e1
->u
.s
.info
);
892 e1
->k
= VRELOCABLE
; e1
->u
.s
.info
= e2
->u
.s
.info
;
895 luaK_exp2nextreg(fs
, e2
); /* operand must be on the 'stack' */
896 codearith(fs
, OP_CONCAT
, e1
, e2
);
901 codearith(fs
, OP_ADD
, e1
, e2
); break;
902 case OPR_SUB
: codearith(fs
, OP_SUB
, e1
, e2
); break;
903 case OPR_MUL
: codearith(fs
, OP_MUL
, e1
, e2
); break;
904 case OPR_DIV
: codearith(fs
, OP_DIV
, e1
, e2
); break;
905 case OPR_MOD
: codearith(fs
, OP_MOD
, e1
, e2
); break;
906 case OPR_POW
: codearith(fs
, OP_POW
, e1
, e2
); break;
907 case OPR_EQ
: codecomp(fs
, OP_EQ
, 1, e1
, e2
); break;
908 case OPR_NE
: codecomp(fs
, OP_EQ
, 0, e1
, e2
); break;
909 case OPR_LT
: codecomp(fs
, OP_LT
, 1, e1
, e2
); break;
910 case OPR_LE
: codecomp(fs
, OP_LE
, 1, e1
, e2
); break;
911 case OPR_GT
: codecomp(fs
, OP_LT
, 0, e1
, e2
); break;
912 case OPR_GE
: codecomp(fs
, OP_LE
, 0, e1
, e2
); break;
913 default: lua_assert(0);
918 void luaK_fixline (FuncState
*fs
, int line
) {
919 fs
->f
->lineinfo
[fs
->pc
- 1] = line
;
923 static int luaK_code (FuncState
*fs
, Instruction i
, int line
) {
925 dischargejpc(fs
); /* `pc' will change */
926 /* put new instruction in code array */
927 luaM_growvector(fs
->L
, f
->code
, fs
->pc
, f
->sizecode
, Instruction
,
928 MAX_INT
, "code size overflow");
930 /* save corresponding line information */
931 luaM_growvector(fs
->L
, f
->lineinfo
, fs
->pc
, f
->sizelineinfo
, int,
932 MAX_INT
, "code size overflow");
933 f
->lineinfo
[fs
->pc
] = line
;
938 int luaK_codeABC (FuncState
*fs
, OpCode o
, int a
, int b
, int c
) {
939 lua_assert(getOpMode(o
) == iABC
);
940 lua_assert(getBMode(o
) != OpArgN
|| b
== 0);
941 lua_assert(getCMode(o
) != OpArgN
|| c
== 0);
942 return luaK_code(fs
, CREATE_ABC(o
, a
, b
, c
), fs
->ls
->lastline
);
946 int luaK_codeABx (FuncState
*fs
, OpCode o
, int a
, unsigned int bc
) {
947 lua_assert(getOpMode(o
) == iABx
|| getOpMode(o
) == iAsBx
);
948 lua_assert(getCMode(o
) == OpArgN
);
949 return luaK_code(fs
, CREATE_ABx(o
, a
, bc
), fs
->ls
->lastline
);
953 void luaK_setlist (FuncState
*fs
, int base
, int nelems
, int tostore
) {
954 int c
= (nelems
- 1)/LFIELDS_PER_FLUSH
+ 1;
955 int b
= (tostore
== LUA_MULTRET
) ? 0 : tostore
;
956 lua_assert(tostore
!= 0);
958 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, c
);
960 luaK_codeABC(fs
, OP_SETLIST
, base
, b
, 0);
961 luaK_code(fs
, cast(Instruction
, c
), fs
->ls
->lastline
);
963 fs
->freereg
= base
+ 1; /* free registers with list values */