Path: blob/main/sys/contrib/openzfs/module/lua/lcode.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lcode.c,v 2.62.1.1 2013/04/12 18:48:47 roberto Exp $3** Code generator for Lua4** See Copyright Notice in lua.h5*/67#define lcode_c8#define LUA_CORE910#if defined(HAVE_IMPLICIT_FALLTHROUGH)11#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"12#endif1314#include <sys/lua/lua.h>1516#include "lcode.h"17#include "ldebug.h"18#include "ldo.h"19#include "lgc.h"20#include "llex.h"21#include "lmem.h"22#include "lobject.h"23#include "lopcodes.h"24#include "lparser.h"25#include "lstring.h"26#include "ltable.h"27#include "lvm.h"282930#define hasjumps(e) ((e)->t != (e)->f)313233static int isnumeral(expdesc *e) {34return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);35}363738void luaK_nil (FuncState *fs, int from, int n) {39Instruction *previous;40int l = from + n - 1; /* last register to set nil */41if (fs->pc > fs->lasttarget) { /* no jumps to current position? */42previous = &fs->f->code[fs->pc-1];43if (GET_OPCODE(*previous) == OP_LOADNIL) {44int pfrom = GETARG_A(*previous);45int pl = pfrom + GETARG_B(*previous);46if ((pfrom <= from && from <= pl + 1) ||47(from <= pfrom && pfrom <= l + 1)) { /* can connect both? */48if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */49if (pl > l) l = pl; /* l = max(l, pl) */50SETARG_A(*previous, from);51SETARG_B(*previous, l - from);52return;53}54} /* else go through */55}56luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */57}585960int luaK_jump (FuncState *fs) {61int jpc = fs->jpc; /* save list of jumps to here */62int j;63fs->jpc = NO_JUMP;64j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);65luaK_concat(fs, &j, jpc); /* keep them on hold */66return j;67}686970void luaK_ret (FuncState *fs, int first, int nret) {71luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);72}737475static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {76luaK_codeABC(fs, op, A, B, C);77return luaK_jump(fs);78}798081static void fixjump (FuncState *fs, int pc, int dest) {82Instruction *jmp = &fs->f->code[pc];83int offset = dest-(pc+1);84lua_assert(dest != NO_JUMP);85if (abs(offset) > MAXARG_sBx)86luaX_syntaxerror(fs->ls, "control structure too long");87SETARG_sBx(*jmp, offset);88}899091/*92** returns current `pc' and marks it as a jump target (to avoid wrong93** optimizations with consecutive instructions not in the same basic block).94*/95int luaK_getlabel (FuncState *fs) {96fs->lasttarget = fs->pc;97return fs->pc;98}99100101static int getjump (FuncState *fs, int pc) {102int offset = GETARG_sBx(fs->f->code[pc]);103if (offset == NO_JUMP) /* point to itself represents end of list */104return NO_JUMP; /* end of list */105else106return (pc+1)+offset; /* turn offset into absolute position */107}108109110static Instruction *getjumpcontrol (FuncState *fs, int pc) {111Instruction *pi = &fs->f->code[pc];112if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))113return pi-1;114else115return pi;116}117118119/*120** check whether list has any jump that do not produce a value121** (or produce an inverted value)122*/123static int need_value (FuncState *fs, int list) {124for (; list != NO_JUMP; list = getjump(fs, list)) {125Instruction i = *getjumpcontrol(fs, list);126if (GET_OPCODE(i) != OP_TESTSET) return 1;127}128return 0; /* not found */129}130131132static int patchtestreg (FuncState *fs, int node, int reg) {133Instruction *i = getjumpcontrol(fs, node);134if (GET_OPCODE(*i) != OP_TESTSET)135return 0; /* cannot patch other instructions */136if (reg != NO_REG && reg != GETARG_B(*i))137SETARG_A(*i, reg);138else /* no register to put value or register already has the value */139*i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));140141return 1;142}143144145static void removevalues (FuncState *fs, int list) {146for (; list != NO_JUMP; list = getjump(fs, list))147patchtestreg(fs, list, NO_REG);148}149150151static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,152int dtarget) {153while (list != NO_JUMP) {154int next = getjump(fs, list);155if (patchtestreg(fs, list, reg))156fixjump(fs, list, vtarget);157else158fixjump(fs, list, dtarget); /* jump to default target */159list = next;160}161}162163164static void dischargejpc (FuncState *fs) {165patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);166fs->jpc = NO_JUMP;167}168169170void luaK_patchlist (FuncState *fs, int list, int target) {171if (target == fs->pc)172luaK_patchtohere(fs, list);173else {174lua_assert(target < fs->pc);175patchlistaux(fs, list, target, NO_REG, target);176}177}178179180LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {181level++; /* argument is +1 to reserve 0 as non-op */182while (list != NO_JUMP) {183int next = getjump(fs, list);184lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&185(GETARG_A(fs->f->code[list]) == 0 ||186GETARG_A(fs->f->code[list]) >= level));187SETARG_A(fs->f->code[list], level);188list = next;189}190}191192193void luaK_patchtohere (FuncState *fs, int list) {194luaK_getlabel(fs);195luaK_concat(fs, &fs->jpc, list);196}197198199void luaK_concat (FuncState *fs, int *l1, int l2) {200if (l2 == NO_JUMP) return;201else if (*l1 == NO_JUMP)202*l1 = l2;203else {204int list = *l1;205int next;206while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */207list = next;208fixjump(fs, list, l2);209}210}211212213static int luaK_code (FuncState *fs, Instruction i) {214Proto *f = fs->f;215dischargejpc(fs); /* `pc' will change */216/* put new instruction in code array */217luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,218MAX_INT, "opcodes");219f->code[fs->pc] = i;220/* save corresponding line information */221luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int,222MAX_INT, "opcodes");223f->lineinfo[fs->pc] = fs->ls->lastline;224return fs->pc++;225}226227228int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {229lua_assert(getOpMode(o) == iABC);230lua_assert(getBMode(o) != OpArgN || b == 0);231lua_assert(getCMode(o) != OpArgN || c == 0);232lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C);233return luaK_code(fs, CREATE_ABC(o, a, b, c));234}235236237int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {238lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);239lua_assert(getCMode(o) == OpArgN);240lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);241return luaK_code(fs, CREATE_ABx(o, a, bc));242}243244245static int codeextraarg (FuncState *fs, int a) {246lua_assert(a <= MAXARG_Ax);247return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));248}249250251int luaK_codek (FuncState *fs, int reg, int k) {252if (k <= MAXARG_Bx)253return luaK_codeABx(fs, OP_LOADK, reg, k);254else {255int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);256codeextraarg(fs, k);257return p;258}259}260261262void luaK_checkstack (FuncState *fs, int n) {263int newstack = fs->freereg + n;264if (newstack > fs->f->maxstacksize) {265if (newstack >= MAXSTACK)266luaX_syntaxerror(fs->ls, "function or expression too complex");267fs->f->maxstacksize = cast_byte(newstack);268}269}270271272void luaK_reserveregs (FuncState *fs, int n) {273luaK_checkstack(fs, n);274fs->freereg += n;275}276277278static void freereg (FuncState *fs, int reg) {279if (!ISK(reg) && reg >= fs->nactvar) {280fs->freereg--;281lua_assert(reg == fs->freereg);282}283}284285286static void freeexp (FuncState *fs, expdesc *e) {287if (e->k == VNONRELOC)288freereg(fs, e->u.info);289}290291292static int addk (FuncState *fs, TValue *key, TValue *v) {293lua_State *L = fs->ls->L;294TValue *idx = luaH_set(L, fs->h, key);295Proto *f = fs->f;296int k, oldsize;297if (ttisnumber(idx)) {298lua_Number n = nvalue(idx);299lua_number2int(k, n);300if (luaV_rawequalobj(&f->k[k], v))301return k;302/* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");303go through and create a new entry for this value */304}305/* constant not found; create a new entry */306oldsize = f->sizek;307k = fs->nk;308/* numerical value does not need GC barrier;309table has no metatable, so it does not need to invalidate cache */310setnvalue(idx, cast_num(k));311luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");312while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);313setobj(L, &f->k[k], v);314fs->nk++;315luaC_barrier(L, f, v);316return k;317}318319320int luaK_stringK (FuncState *fs, TString *s) {321TValue o;322setsvalue(fs->ls->L, &o, s);323return addk(fs, &o, &o);324}325326327int luaK_numberK (FuncState *fs, lua_Number r) {328int n;329lua_State *L = fs->ls->L;330TValue o;331setnvalue(&o, r);332if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */333/* use raw representation as key to avoid numeric problems */334setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r)));335n = addk(fs, L->top - 1, &o);336L->top--;337}338else339n = addk(fs, &o, &o); /* regular case */340return n;341}342343344static int boolK (FuncState *fs, int b) {345TValue o;346setbvalue(&o, b);347return addk(fs, &o, &o);348}349350351static int nilK (FuncState *fs) {352TValue k, v;353setnilvalue(&v);354/* cannot use nil as key; instead use table itself to represent nil */355sethvalue(fs->ls->L, &k, fs->h);356return addk(fs, &k, &v);357}358359360void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {361if (e->k == VCALL) { /* expression is an open function call? */362SETARG_C(getcode(fs, e), nresults+1);363}364else if (e->k == VVARARG) {365SETARG_B(getcode(fs, e), nresults+1);366SETARG_A(getcode(fs, e), fs->freereg);367luaK_reserveregs(fs, 1);368}369}370371372void luaK_setoneret (FuncState *fs, expdesc *e) {373if (e->k == VCALL) { /* expression is an open function call? */374e->k = VNONRELOC;375e->u.info = GETARG_A(getcode(fs, e));376}377else if (e->k == VVARARG) {378SETARG_B(getcode(fs, e), 2);379e->k = VRELOCABLE; /* can relocate its simple result */380}381}382383384void luaK_dischargevars (FuncState *fs, expdesc *e) {385switch (e->k) {386case VLOCAL: {387e->k = VNONRELOC;388break;389}390case VUPVAL: {391e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);392e->k = VRELOCABLE;393break;394}395case VINDEXED: {396OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */397freereg(fs, e->u.ind.idx);398if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */399freereg(fs, e->u.ind.t);400op = OP_GETTABLE;401}402e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx);403e->k = VRELOCABLE;404break;405}406case VVARARG:407case VCALL: {408luaK_setoneret(fs, e);409break;410}411default: break; /* there is one value available (somewhere) */412}413}414415416static int code_label (FuncState *fs, int A, int b, int jump) {417luaK_getlabel(fs); /* those instructions may be jump targets */418return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);419}420421422static void discharge2reg (FuncState *fs, expdesc *e, int reg) {423luaK_dischargevars(fs, e);424switch (e->k) {425case VNIL: {426luaK_nil(fs, reg, 1);427break;428}429case VFALSE: case VTRUE: {430luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);431break;432}433case VK: {434luaK_codek(fs, reg, e->u.info);435break;436}437case VKNUM: {438luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval));439break;440}441case VRELOCABLE: {442Instruction *pc = &getcode(fs, e);443SETARG_A(*pc, reg);444break;445}446case VNONRELOC: {447if (reg != e->u.info)448luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);449break;450}451default: {452lua_assert(e->k == VVOID || e->k == VJMP);453return; /* nothing to do... */454}455}456e->u.info = reg;457e->k = VNONRELOC;458}459460461static void discharge2anyreg (FuncState *fs, expdesc *e) {462if (e->k != VNONRELOC) {463luaK_reserveregs(fs, 1);464discharge2reg(fs, e, fs->freereg-1);465}466}467468469static void exp2reg (FuncState *fs, expdesc *e, int reg) {470discharge2reg(fs, e, reg);471if (e->k == VJMP)472luaK_concat(fs, &e->t, e->u.info); /* put this jump in `t' list */473if (hasjumps(e)) {474int final; /* position after whole expression */475int p_f = NO_JUMP; /* position of an eventual LOAD false */476int p_t = NO_JUMP; /* position of an eventual LOAD true */477if (need_value(fs, e->t) || need_value(fs, e->f)) {478int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);479p_f = code_label(fs, reg, 0, 1);480p_t = code_label(fs, reg, 1, 0);481luaK_patchtohere(fs, fj);482}483final = luaK_getlabel(fs);484patchlistaux(fs, e->f, final, reg, p_f);485patchlistaux(fs, e->t, final, reg, p_t);486}487e->f = e->t = NO_JUMP;488e->u.info = reg;489e->k = VNONRELOC;490}491492493void luaK_exp2nextreg (FuncState *fs, expdesc *e) {494luaK_dischargevars(fs, e);495freeexp(fs, e);496luaK_reserveregs(fs, 1);497exp2reg(fs, e, fs->freereg - 1);498}499500501int luaK_exp2anyreg (FuncState *fs, expdesc *e) {502luaK_dischargevars(fs, e);503if (e->k == VNONRELOC) {504if (!hasjumps(e)) return e->u.info; /* exp is already in a register */505if (e->u.info >= fs->nactvar) { /* reg. is not a local? */506exp2reg(fs, e, e->u.info); /* put value on it */507return e->u.info;508}509}510luaK_exp2nextreg(fs, e); /* default */511return e->u.info;512}513514515void luaK_exp2anyregup (FuncState *fs, expdesc *e) {516if (e->k != VUPVAL || hasjumps(e))517luaK_exp2anyreg(fs, e);518}519520521void luaK_exp2val (FuncState *fs, expdesc *e) {522if (hasjumps(e))523luaK_exp2anyreg(fs, e);524else525luaK_dischargevars(fs, e);526}527528529int luaK_exp2RK (FuncState *fs, expdesc *e) {530luaK_exp2val(fs, e);531switch (e->k) {532case VTRUE:533case VFALSE:534case VNIL: {535if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */536e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE));537e->k = VK;538return RKASK(e->u.info);539}540else break;541}542case VKNUM: {543e->u.info = luaK_numberK(fs, e->u.nval);544e->k = VK;545/* go through */546}547case VK: {548if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */549return RKASK(e->u.info);550else break;551}552default: break;553}554/* not a constant in the right range: put it in a register */555return luaK_exp2anyreg(fs, e);556}557558559void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {560switch (var->k) {561case VLOCAL: {562freeexp(fs, ex);563exp2reg(fs, ex, var->u.info);564return;565}566case VUPVAL: {567int e = luaK_exp2anyreg(fs, ex);568luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);569break;570}571case VINDEXED: {572OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP;573int e = luaK_exp2RK(fs, ex);574luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);575break;576}577default: {578lua_assert(0); /* invalid var kind to store */579break;580}581}582freeexp(fs, ex);583}584585586void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {587int ereg;588luaK_exp2anyreg(fs, e);589ereg = e->u.info; /* register where 'e' was placed */590freeexp(fs, e);591e->u.info = fs->freereg; /* base register for op_self */592e->k = VNONRELOC;593luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */594luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));595freeexp(fs, key);596}597598599static void invertjump (FuncState *fs, expdesc *e) {600Instruction *pc = getjumpcontrol(fs, e->u.info);601lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&602GET_OPCODE(*pc) != OP_TEST);603SETARG_A(*pc, !(GETARG_A(*pc)));604}605606607static int jumponcond (FuncState *fs, expdesc *e, int cond) {608if (e->k == VRELOCABLE) {609Instruction ie = getcode(fs, e);610if (GET_OPCODE(ie) == OP_NOT) {611fs->pc--; /* remove previous OP_NOT */612return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);613}614/* else go through */615}616discharge2anyreg(fs, e);617freeexp(fs, e);618return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond);619}620621622void luaK_goiftrue (FuncState *fs, expdesc *e) {623int pc; /* pc of last jump */624luaK_dischargevars(fs, e);625switch (e->k) {626case VJMP: {627invertjump(fs, e);628pc = e->u.info;629break;630}631case VK: case VKNUM: case VTRUE: {632pc = NO_JUMP; /* always true; do nothing */633break;634}635default: {636pc = jumponcond(fs, e, 0);637break;638}639}640luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */641luaK_patchtohere(fs, e->t);642e->t = NO_JUMP;643}644645646void luaK_goiffalse (FuncState *fs, expdesc *e) {647int pc; /* pc of last jump */648luaK_dischargevars(fs, e);649switch (e->k) {650case VJMP: {651pc = e->u.info;652break;653}654case VNIL: case VFALSE: {655pc = NO_JUMP; /* always false; do nothing */656break;657}658default: {659pc = jumponcond(fs, e, 1);660break;661}662}663luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */664luaK_patchtohere(fs, e->f);665e->f = NO_JUMP;666}667668669static void codenot (FuncState *fs, expdesc *e) {670luaK_dischargevars(fs, e);671switch (e->k) {672case VNIL: case VFALSE: {673e->k = VTRUE;674break;675}676case VK: case VKNUM: case VTRUE: {677e->k = VFALSE;678break;679}680case VJMP: {681invertjump(fs, e);682break;683}684case VRELOCABLE:685case VNONRELOC: {686discharge2anyreg(fs, e);687freeexp(fs, e);688e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);689e->k = VRELOCABLE;690break;691}692default: {693lua_assert(0); /* cannot happen */694break;695}696}697/* interchange true and false lists */698{ int temp = e->f; e->f = e->t; e->t = temp; }699removevalues(fs, e->f);700removevalues(fs, e->t);701}702703704void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {705lua_assert(!hasjumps(t));706t->u.ind.t = t->u.info;707t->u.ind.idx = luaK_exp2RK(fs, k);708t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL709: check_exp(vkisinreg(t->k), VLOCAL);710t->k = VINDEXED;711}712713714static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {715lua_Number r;716if (!isnumeral(e1) || !isnumeral(e2)) return 0;717if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)718return 0; /* do not attempt to divide by 0 */719/*720* Patched: check for MIN_INT / -1721*/722if (op == OP_DIV && e1->u.nval == INT64_MIN && e2->u.nval == -1)723return 0;724r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);725e1->u.nval = r;726return 1;727}728729730static void codearith (FuncState *fs, OpCode op,731expdesc *e1, expdesc *e2, int line) {732if (constfolding(op, e1, e2))733return;734else {735int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;736int o1 = luaK_exp2RK(fs, e1);737if (o1 > o2) {738freeexp(fs, e1);739freeexp(fs, e2);740}741else {742freeexp(fs, e2);743freeexp(fs, e1);744}745e1->u.info = luaK_codeABC(fs, op, 0, o1, o2);746e1->k = VRELOCABLE;747luaK_fixline(fs, line);748}749}750751752static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,753expdesc *e2) {754int o1 = luaK_exp2RK(fs, e1);755int o2 = luaK_exp2RK(fs, e2);756freeexp(fs, e2);757freeexp(fs, e1);758if (cond == 0 && op != OP_EQ) {759int temp; /* exchange args to replace by `<' or `<=' */760temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */761cond = 1;762}763e1->u.info = condjump(fs, op, cond, o1, o2);764e1->k = VJMP;765}766767768void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {769expdesc e2;770e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;771switch (op) {772case OPR_MINUS: {773if (isnumeral(e)) /* minus constant? */774e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */775else {776luaK_exp2anyreg(fs, e);777codearith(fs, OP_UNM, e, &e2, line);778}779break;780}781case OPR_NOT: codenot(fs, e); break;782case OPR_LEN: {783luaK_exp2anyreg(fs, e); /* cannot operate on constants */784codearith(fs, OP_LEN, e, &e2, line);785break;786}787default: lua_assert(0);788}789}790791792void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {793switch (op) {794case OPR_AND: {795luaK_goiftrue(fs, v);796break;797}798case OPR_OR: {799luaK_goiffalse(fs, v);800break;801}802case OPR_CONCAT: {803luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */804break;805}806case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:807case OPR_MOD: case OPR_POW: {808if (!isnumeral(v)) luaK_exp2RK(fs, v);809break;810}811default: {812luaK_exp2RK(fs, v);813break;814}815}816}817818819void luaK_posfix (FuncState *fs, BinOpr op,820expdesc *e1, expdesc *e2, int line) {821switch (op) {822case OPR_AND: {823lua_assert(e1->t == NO_JUMP); /* list must be closed */824luaK_dischargevars(fs, e2);825luaK_concat(fs, &e2->f, e1->f);826*e1 = *e2;827break;828}829case OPR_OR: {830lua_assert(e1->f == NO_JUMP); /* list must be closed */831luaK_dischargevars(fs, e2);832luaK_concat(fs, &e2->t, e1->t);833*e1 = *e2;834break;835}836case OPR_CONCAT: {837luaK_exp2val(fs, e2);838if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {839lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1);840freeexp(fs, e1);841SETARG_B(getcode(fs, e2), e1->u.info);842e1->k = VRELOCABLE; e1->u.info = e2->u.info;843}844else {845luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */846codearith(fs, OP_CONCAT, e1, e2, line);847}848break;849}850case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:851case OPR_MOD: case OPR_POW: {852codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);853break;854}855case OPR_EQ: case OPR_LT: case OPR_LE: {856codecomp(fs, cast(OpCode, op - OPR_EQ + OP_EQ), 1, e1, e2);857break;858}859case OPR_NE: case OPR_GT: case OPR_GE: {860codecomp(fs, cast(OpCode, op - OPR_NE + OP_EQ), 0, e1, e2);861break;862}863default: lua_assert(0);864}865}866867868void luaK_fixline (FuncState *fs, int line) {869fs->f->lineinfo[fs->pc - 1] = line;870}871872873void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {874int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1;875int b = (tostore == LUA_MULTRET) ? 0 : tostore;876lua_assert(tostore != 0);877if (c <= MAXARG_C)878luaK_codeABC(fs, OP_SETLIST, base, b, c);879else if (c <= MAXARG_Ax) {880luaK_codeABC(fs, OP_SETLIST, base, b, 0);881codeextraarg(fs, c);882}883else884luaX_syntaxerror(fs->ls, "constructor too long");885fs->freereg = base + 1; /* free registers with list values */886}887888889