00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009
00010 #define lcode_c
00011 #define LUA_CORE
00012
00013 #include "lua.h"
00014
00015 #include "lcode.h"
00016 #include "ldebug.h"
00017 #include "ldo.h"
00018 #include "lgc.h"
00019 #include "llex.h"
00020 #include "lmem.h"
00021 #include "lobject.h"
00022 #include "lopcodes.h"
00023 #include "lparser.h"
00024 #include "ltable.h"
00025
00026
00027 #define hasjumps(e) ((e)->t != (e)->f)
00028
00029
00030 static int isnumeral(expdesc *e) {
00031 return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);
00032 }
00033
00034
00035 void luaK_nil (FuncState *fs, int from, int n) {
00036 Instruction *previous;
00037 if (fs->pc > fs->lasttarget) {
00038 if (fs->pc == 0) {
00039 if (from >= fs->nactvar)
00040 return;
00041 }
00042 else {
00043 previous = &fs->f->code[fs->pc-1];
00044 if (GET_OPCODE(*previous) == OP_LOADNIL) {
00045 int pfrom = GETARG_A(*previous);
00046 int pto = GETARG_B(*previous);
00047 if (pfrom <= from && from <= pto+1) {
00048 if (from+n-1 > pto)
00049 SETARG_B(*previous, from+n-1);
00050 return;
00051 }
00052 }
00053 }
00054 }
00055 luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0);
00056 }
00057
00058
00059 int luaK_jump (FuncState *fs) {
00060 int jpc = fs->jpc;
00061 int j;
00062 fs->jpc = NO_JUMP;
00063 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
00064 luaK_concat(fs, &j, jpc);
00065 return j;
00066 }
00067
00068
00069 void luaK_ret (FuncState *fs, int first, int nret) {
00070 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);
00071 }
00072
00073
00074 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {
00075 luaK_codeABC(fs, op, A, B, C);
00076 return luaK_jump(fs);
00077 }
00078
00079
00080 static void fixjump (FuncState *fs, int pc, int dest) {
00081 Instruction *jmp = &fs->f->code[pc];
00082 int offset = dest-(pc+1);
00083 lua_assert(dest != NO_JUMP);
00084 if (abs(offset) > MAXARG_sBx)
00085 luaX_syntaxerror(fs->ls, "control structure too long");
00086 SETARG_sBx(*jmp, offset);
00087 }
00088
00089
00090
00091
00092
00093
00094 int luaK_getlabel (FuncState *fs) {
00095 fs->lasttarget = fs->pc;
00096 return fs->pc;
00097 }
00098
00099
00100 static int getjump (FuncState *fs, int pc) {
00101 int offset = GETARG_sBx(fs->f->code[pc]);
00102 if (offset == NO_JUMP)
00103 return NO_JUMP;
00104 else
00105 return (pc+1)+offset;
00106 }
00107
00108
00109 static Instruction *getjumpcontrol (FuncState *fs, int pc) {
00110 Instruction *pi = &fs->f->code[pc];
00111 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
00112 return pi-1;
00113 else
00114 return pi;
00115 }
00116
00117
00118
00119
00120
00121
00122 static int need_value (FuncState *fs, int list) {
00123 for (; list != NO_JUMP; list = getjump(fs, list)) {
00124 Instruction i = *getjumpcontrol(fs, list);
00125 if (GET_OPCODE(i) != OP_TESTSET) return 1;
00126 }
00127 return 0;
00128 }
00129
00130
00131 static int patchtestreg (FuncState *fs, int node, int reg) {
00132 Instruction *i = getjumpcontrol(fs, node);
00133 if (GET_OPCODE(*i) != OP_TESTSET)
00134 return 0;
00135 if (reg != NO_REG && reg != GETARG_B(*i))
00136 SETARG_A(*i, reg);
00137 else
00138 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));
00139
00140 return 1;
00141 }
00142
00143
00144 static void removevalues (FuncState *fs, int list) {
00145 for (; list != NO_JUMP; list = getjump(fs, list))
00146 patchtestreg(fs, list, NO_REG);
00147 }
00148
00149
00150 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
00151 int dtarget) {
00152 while (list != NO_JUMP) {
00153 int next = getjump(fs, list);
00154 if (patchtestreg(fs, list, reg))
00155 fixjump(fs, list, vtarget);
00156 else
00157 fixjump(fs, list, dtarget);
00158 list = next;
00159 }
00160 }
00161
00162
00163 static void dischargejpc (FuncState *fs) {
00164 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);
00165 fs->jpc = NO_JUMP;
00166 }
00167
00168
00169 void luaK_patchlist (FuncState *fs, int list, int target) {
00170 if (target == fs->pc)
00171 luaK_patchtohere(fs, list);
00172 else {
00173 lua_assert(target < fs->pc);
00174 patchlistaux(fs, list, target, NO_REG, target);
00175 }
00176 }
00177
00178
00179 void luaK_patchtohere (FuncState *fs, int list) {
00180 luaK_getlabel(fs);
00181 luaK_concat(fs, &fs->jpc, list);
00182 }
00183
00184
00185 void luaK_concat (FuncState *fs, int *l1, int l2) {
00186 if (l2 == NO_JUMP) return;
00187 else if (*l1 == NO_JUMP)
00188 *l1 = l2;
00189 else {
00190 int list = *l1;
00191 int next;
00192 while ((next = getjump(fs, list)) != NO_JUMP)
00193 list = next;
00194 fixjump(fs, list, l2);
00195 }
00196 }
00197
00198
00199 void luaK_checkstack (FuncState *fs, int n) {
00200 int newstack = fs->freereg + n;
00201 if (newstack > fs->f->maxstacksize) {
00202 if (newstack >= MAXSTACK)
00203 luaX_syntaxerror(fs->ls, "function or expression too complex");
00204 fs->f->maxstacksize = cast_byte(newstack);
00205 }
00206 }
00207
00208
00209 void luaK_reserveregs (FuncState *fs, int n) {
00210 luaK_checkstack(fs, n);
00211 fs->freereg += n;
00212 }
00213
00214
00215 static void freereg (FuncState *fs, int reg) {
00216 if (!ISK(reg) && reg >= fs->nactvar) {
00217 fs->freereg--;
00218 lua_assert(reg == fs->freereg);
00219 }
00220 }
00221
00222
00223 static void freeexp (FuncState *fs, expdesc *e) {
00224 if (e->k == VNONRELOC)
00225 freereg(fs, e->u.s.info);
00226 }
00227
00228
00229 static int addk (FuncState *fs, TValue *k, TValue *v) {
00230 lua_State *L = fs->L;
00231 TValue *idx = luaH_set(L, fs->h, k);
00232 Proto *f = fs->f;
00233 int oldsize = f->sizek;
00234 if (ttisnumber(idx)) {
00235 lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));
00236 return cast_int(nvalue(idx));
00237 }
00238 else {
00239 setnvalue(idx, cast_num(fs->nk));
00240 luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,
00241 MAXARG_Bx, "constant table overflow");
00242 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
00243 setobj(L, &f->k[fs->nk], v);
00244 luaC_barrier(L, f, v);
00245 return fs->nk++;
00246 }
00247 }
00248
00249
00250 int luaK_stringK (FuncState *fs, TString *s) {
00251 TValue o;
00252 setsvalue(fs->L, &o, s);
00253 return addk(fs, &o, &o);
00254 }
00255
00256
00257 int luaK_numberK (FuncState *fs, lua_Number r) {
00258 TValue o;
00259 setnvalue(&o, r);
00260 return addk(fs, &o, &o);
00261 }
00262
00263
00264 static int boolK (FuncState *fs, int b) {
00265 TValue o;
00266 setbvalue(&o, b);
00267 return addk(fs, &o, &o);
00268 }
00269
00270
00271 static int nilK (FuncState *fs) {
00272 TValue k, v;
00273 setnilvalue(&v);
00274
00275 sethvalue(fs->L, &k, fs->h);
00276 return addk(fs, &k, &v);
00277 }
00278
00279
00280 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
00281 if (e->k == VCALL) {
00282 SETARG_C(getcode(fs, e), nresults+1);
00283 }
00284 else if (e->k == VVARARG) {
00285 SETARG_B(getcode(fs, e), nresults+1);
00286 SETARG_A(getcode(fs, e), fs->freereg);
00287 luaK_reserveregs(fs, 1);
00288 }
00289 }
00290
00291
00292 void luaK_setoneret (FuncState *fs, expdesc *e) {
00293 if (e->k == VCALL) {
00294 e->k = VNONRELOC;
00295 e->u.s.info = GETARG_A(getcode(fs, e));
00296 }
00297 else if (e->k == VVARARG) {
00298 SETARG_B(getcode(fs, e), 2);
00299 e->k = VRELOCABLE;
00300 }
00301 }
00302
00303
00304 void luaK_dischargevars (FuncState *fs, expdesc *e) {
00305 switch (e->k) {
00306 case VLOCAL: {
00307 e->k = VNONRELOC;
00308 break;
00309 }
00310 case VUPVAL: {
00311 e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);
00312 e->k = VRELOCABLE;
00313 break;
00314 }
00315 case VGLOBAL: {
00316 e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);
00317 e->k = VRELOCABLE;
00318 break;
00319 }
00320 case VINDEXED: {
00321 freereg(fs, e->u.s.aux);
00322 freereg(fs, e->u.s.info);
00323 e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);
00324 e->k = VRELOCABLE;
00325 break;
00326 }
00327 case VVARARG:
00328 case VCALL: {
00329 luaK_setoneret(fs, e);
00330 break;
00331 }
00332 default: break;
00333 }
00334 }
00335
00336
00337 static int code_label (FuncState *fs, int A, int b, int jump) {
00338 luaK_getlabel(fs);
00339 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
00340 }
00341
00342
00343 static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
00344 luaK_dischargevars(fs, e);
00345 switch (e->k) {
00346 case VNIL: {
00347 luaK_nil(fs, reg, 1);
00348 break;
00349 }
00350 case VFALSE: case VTRUE: {
00351 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
00352 break;
00353 }
00354 case VK: {
00355 luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
00356 break;
00357 }
00358 case VKNUM: {
00359 luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
00360 break;
00361 }
00362 case VRELOCABLE: {
00363 Instruction *pc = &getcode(fs, e);
00364 SETARG_A(*pc, reg);
00365 break;
00366 }
00367 case VNONRELOC: {
00368 if (reg != e->u.s.info)
00369 luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);
00370 break;
00371 }
00372 default: {
00373 lua_assert(e->k == VVOID || e->k == VJMP);
00374 return;
00375 }
00376 }
00377 e->u.s.info = reg;
00378 e->k = VNONRELOC;
00379 }
00380
00381
00382 static void discharge2anyreg (FuncState *fs, expdesc *e) {
00383 if (e->k != VNONRELOC) {
00384 luaK_reserveregs(fs, 1);
00385 discharge2reg(fs, e, fs->freereg-1);
00386 }
00387 }
00388
00389
00390 static void exp2reg (FuncState *fs, expdesc *e, int reg) {
00391 discharge2reg(fs, e, reg);
00392 if (e->k == VJMP)
00393 luaK_concat(fs, &e->t, e->u.s.info);
00394 if (hasjumps(e)) {
00395 int final;
00396 int p_f = NO_JUMP;
00397 int p_t = NO_JUMP;
00398 if (need_value(fs, e->t) || need_value(fs, e->f)) {
00399 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
00400 p_f = code_label(fs, reg, 0, 1);
00401 p_t = code_label(fs, reg, 1, 0);
00402 luaK_patchtohere(fs, fj);
00403 }
00404 final = luaK_getlabel(fs);
00405 patchlistaux(fs, e->f, final, reg, p_f);
00406 patchlistaux(fs, e->t, final, reg, p_t);
00407 }
00408 e->f = e->t = NO_JUMP;
00409 e->u.s.info = reg;
00410 e->k = VNONRELOC;
00411 }
00412
00413
00414 void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
00415 luaK_dischargevars(fs, e);
00416 freeexp(fs, e);
00417 luaK_reserveregs(fs, 1);
00418 exp2reg(fs, e, fs->freereg - 1);
00419 }
00420
00421
00422 int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
00423 luaK_dischargevars(fs, e);
00424 if (e->k == VNONRELOC) {
00425 if (!hasjumps(e)) return e->u.s.info;
00426 if (e->u.s.info >= fs->nactvar) {
00427 exp2reg(fs, e, e->u.s.info);
00428 return e->u.s.info;
00429 }
00430 }
00431 luaK_exp2nextreg(fs, e);
00432 return e->u.s.info;
00433 }
00434
00435
00436 void luaK_exp2val (FuncState *fs, expdesc *e) {
00437 if (hasjumps(e))
00438 luaK_exp2anyreg(fs, e);
00439 else
00440 luaK_dischargevars(fs, e);
00441 }
00442
00443
00444 int luaK_exp2RK (FuncState *fs, expdesc *e) {
00445 luaK_exp2val(fs, e);
00446 switch (e->k) {
00447 case VKNUM:
00448 case VTRUE:
00449 case VFALSE:
00450 case VNIL: {
00451 if (fs->nk <= MAXINDEXRK) {
00452 e->u.s.info = (e->k == VNIL) ? nilK(fs) :
00453 (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :
00454 boolK(fs, (e->k == VTRUE));
00455 e->k = VK;
00456 return RKASK(e->u.s.info);
00457 }
00458 else break;
00459 }
00460 case VK: {
00461 if (e->u.s.info <= MAXINDEXRK)
00462 return RKASK(e->u.s.info);
00463 else break;
00464 }
00465 default: break;
00466 }
00467
00468 return luaK_exp2anyreg(fs, e);
00469 }
00470
00471
00472 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
00473 switch (var->k) {
00474 case VLOCAL: {
00475 freeexp(fs, ex);
00476 exp2reg(fs, ex, var->u.s.info);
00477 return;
00478 }
00479 case VUPVAL: {
00480 int e = luaK_exp2anyreg(fs, ex);
00481 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);
00482 break;
00483 }
00484 case VGLOBAL: {
00485 int e = luaK_exp2anyreg(fs, ex);
00486 luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);
00487 break;
00488 }
00489 case VINDEXED: {
00490 int e = luaK_exp2RK(fs, ex);
00491 luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);
00492 break;
00493 }
00494 default: {
00495 lua_assert(0);
00496 break;
00497 }
00498 }
00499 freeexp(fs, ex);
00500 }
00501
00502
00503 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
00504 int func;
00505 luaK_exp2anyreg(fs, e);
00506 freeexp(fs, e);
00507 func = fs->freereg;
00508 luaK_reserveregs(fs, 2);
00509 luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
00510 freeexp(fs, key);
00511 e->u.s.info = func;
00512 e->k = VNONRELOC;
00513 }
00514
00515
00516 static void invertjump (FuncState *fs, expdesc *e) {
00517 Instruction *pc = getjumpcontrol(fs, e->u.s.info);
00518 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
00519 GET_OPCODE(*pc) != OP_TEST);
00520 SETARG_A(*pc, !(GETARG_A(*pc)));
00521 }
00522
00523
00524 static int jumponcond (FuncState *fs, expdesc *e, int cond) {
00525 if (e->k == VRELOCABLE) {
00526 Instruction ie = getcode(fs, e);
00527 if (GET_OPCODE(ie) == OP_NOT) {
00528 fs->pc--;
00529 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);
00530 }
00531
00532 }
00533 discharge2anyreg(fs, e);
00534 freeexp(fs, e);
00535 return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);
00536 }
00537
00538
00539 void luaK_goiftrue (FuncState *fs, expdesc *e) {
00540 int pc;
00541 luaK_dischargevars(fs, e);
00542 switch (e->k) {
00543 case VK: case VKNUM: case VTRUE: {
00544 pc = NO_JUMP;
00545 break;
00546 }
00547 case VJMP: {
00548 invertjump(fs, e);
00549 pc = e->u.s.info;
00550 break;
00551 }
00552 case VFALSE: {
00553 if (!hasjumps(e)) {
00554 pc = luaK_jump(fs);
00555 break;
00556 }
00557
00558 }
00559 default: {
00560 pc = jumponcond(fs, e, 0);
00561 break;
00562 }
00563 }
00564 luaK_concat(fs, &e->f, pc);
00565 luaK_patchtohere(fs, e->t);
00566 e->t = NO_JUMP;
00567 }
00568
00569
00570 static void luaK_goiffalse (FuncState *fs, expdesc *e) {
00571 int pc;
00572 luaK_dischargevars(fs, e);
00573 switch (e->k) {
00574 case VNIL: case VFALSE: {
00575 pc = NO_JUMP;
00576 break;
00577 }
00578 case VJMP: {
00579 pc = e->u.s.info;
00580 break;
00581 }
00582 case VTRUE: {
00583 if (!hasjumps(e)) {
00584 pc = luaK_jump(fs);
00585 break;
00586 }
00587
00588 }
00589 default: {
00590 pc = jumponcond(fs, e, 1);
00591 break;
00592 }
00593 }
00594 luaK_concat(fs, &e->t, pc);
00595 luaK_patchtohere(fs, e->f);
00596 e->f = NO_JUMP;
00597 }
00598
00599
00600 static void codenot (FuncState *fs, expdesc *e) {
00601 luaK_dischargevars(fs, e);
00602 switch (e->k) {
00603 case VNIL: case VFALSE: {
00604 e->k = VTRUE;
00605 break;
00606 }
00607 case VK: case VKNUM: case VTRUE: {
00608 e->k = VFALSE;
00609 break;
00610 }
00611 case VJMP: {
00612 invertjump(fs, e);
00613 break;
00614 }
00615 case VRELOCABLE:
00616 case VNONRELOC: {
00617 discharge2anyreg(fs, e);
00618 freeexp(fs, e);
00619 e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);
00620 e->k = VRELOCABLE;
00621 break;
00622 }
00623 default: {
00624 lua_assert(0);
00625 break;
00626 }
00627 }
00628
00629 { int temp = e->f; e->f = e->t; e->t = temp; }
00630 removevalues(fs, e->f);
00631 removevalues(fs, e->t);
00632 }
00633
00634
00635 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
00636 t->u.s.aux = luaK_exp2RK(fs, k);
00637 t->k = VINDEXED;
00638 }
00639
00640
00641 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
00642 lua_Number v1, v2, r;
00643 if (!isnumeral(e1) || !isnumeral(e2)) return 0;
00644 v1 = e1->u.nval;
00645 v2 = e2->u.nval;
00646 switch (op) {
00647 case OP_ADD: r = luai_numadd(v1, v2); break;
00648 case OP_SUB: r = luai_numsub(v1, v2); break;
00649 case OP_MUL: r = luai_nummul(v1, v2); break;
00650 case OP_DIV:
00651 if (v2 == 0) return 0;
00652 r = luai_numdiv(v1, v2); break;
00653 case OP_MOD:
00654 if (v2 == 0) return 0;
00655 r = luai_nummod(v1, v2); break;
00656 case OP_POW: r = luai_numpow(v1, v2); break;
00657 case OP_UNM: r = luai_numunm(v1); break;
00658 case OP_LEN: return 0;
00659 default: lua_assert(0); r = 0; break;
00660 }
00661 if (luai_numisnan(r)) return 0;
00662 e1->u.nval = r;
00663 return 1;
00664 }
00665
00666
00667 static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
00668 if (constfolding(op, e1, e2))
00669 return;
00670 else {
00671 int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;
00672 int o1 = luaK_exp2RK(fs, e1);
00673 if (o1 > o2) {
00674 freeexp(fs, e1);
00675 freeexp(fs, e2);
00676 }
00677 else {
00678 freeexp(fs, e2);
00679 freeexp(fs, e1);
00680 }
00681 e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
00682 e1->k = VRELOCABLE;
00683 }
00684 }
00685
00686
00687 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
00688 expdesc *e2) {
00689 int o1 = luaK_exp2RK(fs, e1);
00690 int o2 = luaK_exp2RK(fs, e2);
00691 freeexp(fs, e2);
00692 freeexp(fs, e1);
00693 if (cond == 0 && op != OP_EQ) {
00694 int temp;
00695 temp = o1; o1 = o2; o2 = temp;
00696 cond = 1;
00697 }
00698 e1->u.s.info = condjump(fs, op, cond, o1, o2);
00699 e1->k = VJMP;
00700 }
00701
00702
00703 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
00704 expdesc e2;
00705 e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
00706 switch (op) {
00707 case OPR_MINUS: {
00708 if (!isnumeral(e))
00709 luaK_exp2anyreg(fs, e);
00710 codearith(fs, OP_UNM, e, &e2);
00711 break;
00712 }
00713 case OPR_NOT: codenot(fs, e); break;
00714 case OPR_LEN: {
00715 luaK_exp2anyreg(fs, e);
00716 codearith(fs, OP_LEN, e, &e2);
00717 break;
00718 }
00719 default: lua_assert(0);
00720 }
00721 }
00722
00723
00724 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
00725 switch (op) {
00726 case OPR_AND: {
00727 luaK_goiftrue(fs, v);
00728 break;
00729 }
00730 case OPR_OR: {
00731 luaK_goiffalse(fs, v);
00732 break;
00733 }
00734 case OPR_CONCAT: {
00735 luaK_exp2nextreg(fs, v);
00736 break;
00737 }
00738 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
00739 case OPR_MOD: case OPR_POW: {
00740 if (!isnumeral(v)) luaK_exp2RK(fs, v);
00741 break;
00742 }
00743 default: {
00744 luaK_exp2RK(fs, v);
00745 break;
00746 }
00747 }
00748 }
00749
00750
00751 void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
00752 switch (op) {
00753 case OPR_AND: {
00754 lua_assert(e1->t == NO_JUMP);
00755 luaK_dischargevars(fs, e2);
00756 luaK_concat(fs, &e2->f, e1->f);
00757 *e1 = *e2;
00758 break;
00759 }
00760 case OPR_OR: {
00761 lua_assert(e1->f == NO_JUMP);
00762 luaK_dischargevars(fs, e2);
00763 luaK_concat(fs, &e2->t, e1->t);
00764 *e1 = *e2;
00765 break;
00766 }
00767 case OPR_CONCAT: {
00768 luaK_exp2val(fs, e2);
00769 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {
00770 lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);
00771 freeexp(fs, e1);
00772 SETARG_B(getcode(fs, e2), e1->u.s.info);
00773 e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;
00774 }
00775 else {
00776 luaK_exp2nextreg(fs, e2);
00777 codearith(fs, OP_CONCAT, e1, e2);
00778 }
00779 break;
00780 }
00781 case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
00782 case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
00783 case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
00784 case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
00785 case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
00786 case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
00787 case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
00788 case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
00789 case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
00790 case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;
00791 case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;
00792 case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;
00793 default: lua_assert(0);
00794 }
00795 }
00796
00797
00798 void luaK_fixline (FuncState *fs, int line) {
00799 fs->f->lineinfo[fs->pc - 1] = line;
00800 }
00801
00802
00803 static int luaK_code (FuncState *fs, Instruction i, int line) {
00804 Proto *f = fs->f;
00805 dischargejpc(fs);
00806
00807 luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
00808 MAX_INT, "code size overflow");
00809 f->code[fs->pc] = i;
00810
00811 luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,
00812 MAX_INT, "code size overflow");
00813 f->lineinfo[fs->pc] = line;
00814 return fs->pc++;
00815 }
00816
00817
00818 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {
00819 lua_assert(getOpMode(o) == iABC);
00820 lua_assert(getBMode(o) != OpArgN || b == 0);
00821 lua_assert(getCMode(o) != OpArgN || c == 0);
00822 return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);
00823 }
00824
00825
00826 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
00827 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);
00828 lua_assert(getCMode(o) == OpArgN);
00829 return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);
00830 }
00831
00832
00833 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
00834 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;
00835 int b = (tostore == LUA_MULTRET) ? 0 : tostore;
00836 lua_assert(tostore != 0);
00837 if (c <= MAXARG_C)
00838 luaK_codeABC(fs, OP_SETLIST, base, b, c);
00839 else {
00840 luaK_codeABC(fs, OP_SETLIST, base, b, 0);
00841 luaK_code(fs, cast(Instruction, c), fs->ls->lastline);
00842 }
00843 fs->freereg = base + 1;
00844 }