xref: /freebsd/contrib/lua/src/lcode.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lcode.c $
38e3e3a7aSWarner Losh ** Code generator for Lua
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lcode_c
88e3e3a7aSWarner Losh #define LUA_CORE
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh 
138c784bb8SWarner Losh #include <float.h>
140495ed39SKyle Evans #include <limits.h>
158e3e3a7aSWarner Losh #include <math.h>
168e3e3a7aSWarner Losh #include <stdlib.h>
178e3e3a7aSWarner Losh 
188e3e3a7aSWarner Losh #include "lua.h"
198e3e3a7aSWarner Losh 
208e3e3a7aSWarner Losh #include "lcode.h"
218e3e3a7aSWarner Losh #include "ldebug.h"
228e3e3a7aSWarner Losh #include "ldo.h"
238e3e3a7aSWarner Losh #include "lgc.h"
248e3e3a7aSWarner Losh #include "llex.h"
258e3e3a7aSWarner Losh #include "lmem.h"
268e3e3a7aSWarner Losh #include "lobject.h"
278e3e3a7aSWarner Losh #include "lopcodes.h"
288e3e3a7aSWarner Losh #include "lparser.h"
298e3e3a7aSWarner Losh #include "lstring.h"
308e3e3a7aSWarner Losh #include "ltable.h"
318e3e3a7aSWarner Losh #include "lvm.h"
328e3e3a7aSWarner Losh 
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh /* Maximum number of registers in a Lua function (must fit in 8 bits) */
358e3e3a7aSWarner Losh #define MAXREGS		255
368e3e3a7aSWarner Losh 
378e3e3a7aSWarner Losh 
388e3e3a7aSWarner Losh #define hasjumps(e)	((e)->t != (e)->f)
398e3e3a7aSWarner Losh 
408e3e3a7aSWarner Losh 
410495ed39SKyle Evans static int codesJ (FuncState *fs, OpCode o, int sj, int k);
420495ed39SKyle Evans 
430495ed39SKyle Evans 
440495ed39SKyle Evans 
450495ed39SKyle Evans /* semantic error */
luaK_semerror(LexState * ls,const char * msg)460495ed39SKyle Evans l_noret luaK_semerror (LexState *ls, const char *msg) {
470495ed39SKyle Evans   ls->t.token = 0;  /* remove "near <token>" from final message */
480495ed39SKyle Evans   luaX_syntaxerror(ls, msg);
490495ed39SKyle Evans }
500495ed39SKyle Evans 
510495ed39SKyle Evans 
528e3e3a7aSWarner Losh /*
538e3e3a7aSWarner Losh ** If expression is a numeric constant, fills 'v' with its value
548e3e3a7aSWarner Losh ** and returns 1. Otherwise, returns 0.
558e3e3a7aSWarner Losh */
tonumeral(const expdesc * e,TValue * v)568e3e3a7aSWarner Losh static int tonumeral (const expdesc *e, TValue *v) {
578e3e3a7aSWarner Losh   if (hasjumps(e))
588e3e3a7aSWarner Losh     return 0;  /* not a numeral */
598e3e3a7aSWarner Losh   switch (e->k) {
608e3e3a7aSWarner Losh     case VKINT:
618e3e3a7aSWarner Losh       if (v) setivalue(v, e->u.ival);
628e3e3a7aSWarner Losh       return 1;
638e3e3a7aSWarner Losh     case VKFLT:
648e3e3a7aSWarner Losh       if (v) setfltvalue(v, e->u.nval);
658e3e3a7aSWarner Losh       return 1;
668e3e3a7aSWarner Losh     default: return 0;
678e3e3a7aSWarner Losh   }
688e3e3a7aSWarner Losh }
698e3e3a7aSWarner Losh 
708e3e3a7aSWarner Losh 
718e3e3a7aSWarner Losh /*
720495ed39SKyle Evans ** Get the constant value from a constant expression
730495ed39SKyle Evans */
const2val(FuncState * fs,const expdesc * e)740495ed39SKyle Evans static TValue *const2val (FuncState *fs, const expdesc *e) {
750495ed39SKyle Evans   lua_assert(e->k == VCONST);
760495ed39SKyle Evans   return &fs->ls->dyd->actvar.arr[e->u.info].k;
770495ed39SKyle Evans }
780495ed39SKyle Evans 
790495ed39SKyle Evans 
800495ed39SKyle Evans /*
810495ed39SKyle Evans ** If expression is a constant, fills 'v' with its value
820495ed39SKyle Evans ** and returns 1. Otherwise, returns 0.
830495ed39SKyle Evans */
luaK_exp2const(FuncState * fs,const expdesc * e,TValue * v)840495ed39SKyle Evans int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
850495ed39SKyle Evans   if (hasjumps(e))
860495ed39SKyle Evans     return 0;  /* not a constant */
870495ed39SKyle Evans   switch (e->k) {
880495ed39SKyle Evans     case VFALSE:
890495ed39SKyle Evans       setbfvalue(v);
900495ed39SKyle Evans       return 1;
910495ed39SKyle Evans     case VTRUE:
920495ed39SKyle Evans       setbtvalue(v);
930495ed39SKyle Evans       return 1;
940495ed39SKyle Evans     case VNIL:
950495ed39SKyle Evans       setnilvalue(v);
960495ed39SKyle Evans       return 1;
970495ed39SKyle Evans     case VKSTR: {
980495ed39SKyle Evans       setsvalue(fs->ls->L, v, e->u.strval);
990495ed39SKyle Evans       return 1;
1000495ed39SKyle Evans     }
1010495ed39SKyle Evans     case VCONST: {
1020495ed39SKyle Evans       setobj(fs->ls->L, v, const2val(fs, e));
1030495ed39SKyle Evans       return 1;
1040495ed39SKyle Evans     }
1050495ed39SKyle Evans     default: return tonumeral(e, v);
1060495ed39SKyle Evans   }
1070495ed39SKyle Evans }
1080495ed39SKyle Evans 
1090495ed39SKyle Evans 
1100495ed39SKyle Evans /*
1110495ed39SKyle Evans ** Return the previous instruction of the current code. If there
1120495ed39SKyle Evans ** may be a jump target between the current instruction and the
1130495ed39SKyle Evans ** previous one, return an invalid instruction (to avoid wrong
1140495ed39SKyle Evans ** optimizations).
1150495ed39SKyle Evans */
previousinstruction(FuncState * fs)1160495ed39SKyle Evans static Instruction *previousinstruction (FuncState *fs) {
1170495ed39SKyle Evans   static const Instruction invalidinstruction = ~(Instruction)0;
1180495ed39SKyle Evans   if (fs->pc > fs->lasttarget)
1190495ed39SKyle Evans     return &fs->f->code[fs->pc - 1];  /* previous instruction */
1200495ed39SKyle Evans   else
1210495ed39SKyle Evans     return cast(Instruction*, &invalidinstruction);
1220495ed39SKyle Evans }
1230495ed39SKyle Evans 
1240495ed39SKyle Evans 
1250495ed39SKyle Evans /*
1268e3e3a7aSWarner Losh ** Create a OP_LOADNIL instruction, but try to optimize: if the previous
1278e3e3a7aSWarner Losh ** instruction is also OP_LOADNIL and ranges are compatible, adjust
1288e3e3a7aSWarner Losh ** range of previous instruction instead of emitting a new one. (For
1298e3e3a7aSWarner Losh ** instance, 'local a; local b' will generate a single opcode.)
1308e3e3a7aSWarner Losh */
luaK_nil(FuncState * fs,int from,int n)1318e3e3a7aSWarner Losh void luaK_nil (FuncState *fs, int from, int n) {
1328e3e3a7aSWarner Losh   int l = from + n - 1;  /* last register to set nil */
1330495ed39SKyle Evans   Instruction *previous = previousinstruction(fs);
1348e3e3a7aSWarner Losh   if (GET_OPCODE(*previous) == OP_LOADNIL) {  /* previous is LOADNIL? */
1358e3e3a7aSWarner Losh     int pfrom = GETARG_A(*previous);  /* get previous range */
1368e3e3a7aSWarner Losh     int pl = pfrom + GETARG_B(*previous);
1378e3e3a7aSWarner Losh     if ((pfrom <= from && from <= pl + 1) ||
1388e3e3a7aSWarner Losh         (from <= pfrom && pfrom <= l + 1)) {  /* can connect both? */
1398e3e3a7aSWarner Losh       if (pfrom < from) from = pfrom;  /* from = min(from, pfrom) */
1408e3e3a7aSWarner Losh       if (pl > l) l = pl;  /* l = max(l, pl) */
1418e3e3a7aSWarner Losh       SETARG_A(*previous, from);
1428e3e3a7aSWarner Losh       SETARG_B(*previous, l - from);
1438e3e3a7aSWarner Losh       return;
1448e3e3a7aSWarner Losh     }  /* else go through */
1458e3e3a7aSWarner Losh   }
1468e3e3a7aSWarner Losh   luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0);  /* else no optimization */
1478e3e3a7aSWarner Losh }
1488e3e3a7aSWarner Losh 
1498e3e3a7aSWarner Losh 
1508e3e3a7aSWarner Losh /*
1518e3e3a7aSWarner Losh ** Gets the destination address of a jump instruction. Used to traverse
1528e3e3a7aSWarner Losh ** a list of jumps.
1538e3e3a7aSWarner Losh */
getjump(FuncState * fs,int pc)1548e3e3a7aSWarner Losh static int getjump (FuncState *fs, int pc) {
1550495ed39SKyle Evans   int offset = GETARG_sJ(fs->f->code[pc]);
1568e3e3a7aSWarner Losh   if (offset == NO_JUMP)  /* point to itself represents end of list */
1578e3e3a7aSWarner Losh     return NO_JUMP;  /* end of list */
1588e3e3a7aSWarner Losh   else
1598e3e3a7aSWarner Losh     return (pc+1)+offset;  /* turn offset into absolute position */
1608e3e3a7aSWarner Losh }
1618e3e3a7aSWarner Losh 
1628e3e3a7aSWarner Losh 
1638e3e3a7aSWarner Losh /*
1648e3e3a7aSWarner Losh ** Fix jump instruction at position 'pc' to jump to 'dest'.
1658e3e3a7aSWarner Losh ** (Jump addresses are relative in Lua)
1668e3e3a7aSWarner Losh */
fixjump(FuncState * fs,int pc,int dest)1678e3e3a7aSWarner Losh static void fixjump (FuncState *fs, int pc, int dest) {
1688e3e3a7aSWarner Losh   Instruction *jmp = &fs->f->code[pc];
1698e3e3a7aSWarner Losh   int offset = dest - (pc + 1);
1708e3e3a7aSWarner Losh   lua_assert(dest != NO_JUMP);
1710495ed39SKyle Evans   if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ))
1728e3e3a7aSWarner Losh     luaX_syntaxerror(fs->ls, "control structure too long");
1730495ed39SKyle Evans   lua_assert(GET_OPCODE(*jmp) == OP_JMP);
1740495ed39SKyle Evans   SETARG_sJ(*jmp, offset);
1758e3e3a7aSWarner Losh }
1768e3e3a7aSWarner Losh 
1778e3e3a7aSWarner Losh 
1788e3e3a7aSWarner Losh /*
1798e3e3a7aSWarner Losh ** Concatenate jump-list 'l2' into jump-list 'l1'
1808e3e3a7aSWarner Losh */
luaK_concat(FuncState * fs,int * l1,int l2)1818e3e3a7aSWarner Losh void luaK_concat (FuncState *fs, int *l1, int l2) {
1828e3e3a7aSWarner Losh   if (l2 == NO_JUMP) return;  /* nothing to concatenate? */
1838e3e3a7aSWarner Losh   else if (*l1 == NO_JUMP)  /* no original list? */
1848e3e3a7aSWarner Losh     *l1 = l2;  /* 'l1' points to 'l2' */
1858e3e3a7aSWarner Losh   else {
1868e3e3a7aSWarner Losh     int list = *l1;
1878e3e3a7aSWarner Losh     int next;
1888e3e3a7aSWarner Losh     while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */
1898e3e3a7aSWarner Losh       list = next;
1908e3e3a7aSWarner Losh     fixjump(fs, list, l2);  /* last element links to 'l2' */
1918e3e3a7aSWarner Losh   }
1928e3e3a7aSWarner Losh }
1938e3e3a7aSWarner Losh 
1948e3e3a7aSWarner Losh 
1958e3e3a7aSWarner Losh /*
1968e3e3a7aSWarner Losh ** Create a jump instruction and return its position, so its destination
1970495ed39SKyle Evans ** can be fixed later (with 'fixjump').
1988e3e3a7aSWarner Losh */
luaK_jump(FuncState * fs)1998e3e3a7aSWarner Losh int luaK_jump (FuncState *fs) {
2000495ed39SKyle Evans   return codesJ(fs, OP_JMP, NO_JUMP, 0);
2018e3e3a7aSWarner Losh }
2028e3e3a7aSWarner Losh 
2038e3e3a7aSWarner Losh 
2048e3e3a7aSWarner Losh /*
2058e3e3a7aSWarner Losh ** Code a 'return' instruction
2068e3e3a7aSWarner Losh */
luaK_ret(FuncState * fs,int first,int nret)2078e3e3a7aSWarner Losh void luaK_ret (FuncState *fs, int first, int nret) {
2080495ed39SKyle Evans   OpCode op;
2090495ed39SKyle Evans   switch (nret) {
2100495ed39SKyle Evans     case 0: op = OP_RETURN0; break;
2110495ed39SKyle Evans     case 1: op = OP_RETURN1; break;
2120495ed39SKyle Evans     default: op = OP_RETURN; break;
2130495ed39SKyle Evans   }
2140495ed39SKyle Evans   luaK_codeABC(fs, op, first, nret + 1, 0);
2158e3e3a7aSWarner Losh }
2168e3e3a7aSWarner Losh 
2178e3e3a7aSWarner Losh 
2188e3e3a7aSWarner Losh /*
2198e3e3a7aSWarner Losh ** Code a "conditional jump", that is, a test or comparison opcode
2208e3e3a7aSWarner Losh ** followed by a jump. Return jump position.
2218e3e3a7aSWarner Losh */
condjump(FuncState * fs,OpCode op,int A,int B,int C,int k)2220495ed39SKyle Evans static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) {
2230495ed39SKyle Evans   luaK_codeABCk(fs, op, A, B, C, k);
2248e3e3a7aSWarner Losh   return luaK_jump(fs);
2258e3e3a7aSWarner Losh }
2268e3e3a7aSWarner Losh 
2278e3e3a7aSWarner Losh 
2288e3e3a7aSWarner Losh /*
2298e3e3a7aSWarner Losh ** returns current 'pc' and marks it as a jump target (to avoid wrong
2308e3e3a7aSWarner Losh ** optimizations with consecutive instructions not in the same basic block).
2318e3e3a7aSWarner Losh */
luaK_getlabel(FuncState * fs)2328e3e3a7aSWarner Losh int luaK_getlabel (FuncState *fs) {
2338e3e3a7aSWarner Losh   fs->lasttarget = fs->pc;
2348e3e3a7aSWarner Losh   return fs->pc;
2358e3e3a7aSWarner Losh }
2368e3e3a7aSWarner Losh 
2378e3e3a7aSWarner Losh 
2388e3e3a7aSWarner Losh /*
2398e3e3a7aSWarner Losh ** Returns the position of the instruction "controlling" a given
2408e3e3a7aSWarner Losh ** jump (that is, its condition), or the jump itself if it is
2418e3e3a7aSWarner Losh ** unconditional.
2428e3e3a7aSWarner Losh */
getjumpcontrol(FuncState * fs,int pc)2438e3e3a7aSWarner Losh static Instruction *getjumpcontrol (FuncState *fs, int pc) {
2448e3e3a7aSWarner Losh   Instruction *pi = &fs->f->code[pc];
2458e3e3a7aSWarner Losh   if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))
2468e3e3a7aSWarner Losh     return pi-1;
2478e3e3a7aSWarner Losh   else
2488e3e3a7aSWarner Losh     return pi;
2498e3e3a7aSWarner Losh }
2508e3e3a7aSWarner Losh 
2518e3e3a7aSWarner Losh 
2528e3e3a7aSWarner Losh /*
2538e3e3a7aSWarner Losh ** Patch destination register for a TESTSET instruction.
2548e3e3a7aSWarner Losh ** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
2558e3e3a7aSWarner Losh ** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
2568e3e3a7aSWarner Losh ** register. Otherwise, change instruction to a simple 'TEST' (produces
2578e3e3a7aSWarner Losh ** no register value)
2588e3e3a7aSWarner Losh */
patchtestreg(FuncState * fs,int node,int reg)2598e3e3a7aSWarner Losh static int patchtestreg (FuncState *fs, int node, int reg) {
2608e3e3a7aSWarner Losh   Instruction *i = getjumpcontrol(fs, node);
2618e3e3a7aSWarner Losh   if (GET_OPCODE(*i) != OP_TESTSET)
2628e3e3a7aSWarner Losh     return 0;  /* cannot patch other instructions */
2638e3e3a7aSWarner Losh   if (reg != NO_REG && reg != GETARG_B(*i))
2648e3e3a7aSWarner Losh     SETARG_A(*i, reg);
2658e3e3a7aSWarner Losh   else {
2668e3e3a7aSWarner Losh      /* no register to put value or register already has the value;
2678e3e3a7aSWarner Losh         change instruction to simple test */
2680495ed39SKyle Evans     *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
2698e3e3a7aSWarner Losh   }
2708e3e3a7aSWarner Losh   return 1;
2718e3e3a7aSWarner Losh }
2728e3e3a7aSWarner Losh 
2738e3e3a7aSWarner Losh 
2748e3e3a7aSWarner Losh /*
2758e3e3a7aSWarner Losh ** Traverse a list of tests ensuring no one produces a value
2768e3e3a7aSWarner Losh */
removevalues(FuncState * fs,int list)2778e3e3a7aSWarner Losh static void removevalues (FuncState *fs, int list) {
2788e3e3a7aSWarner Losh   for (; list != NO_JUMP; list = getjump(fs, list))
2798e3e3a7aSWarner Losh       patchtestreg(fs, list, NO_REG);
2808e3e3a7aSWarner Losh }
2818e3e3a7aSWarner Losh 
2828e3e3a7aSWarner Losh 
2838e3e3a7aSWarner Losh /*
2848e3e3a7aSWarner Losh ** Traverse a list of tests, patching their destination address and
2858e3e3a7aSWarner Losh ** registers: tests producing values jump to 'vtarget' (and put their
2868e3e3a7aSWarner Losh ** values in 'reg'), other tests jump to 'dtarget'.
2878e3e3a7aSWarner Losh */
patchlistaux(FuncState * fs,int list,int vtarget,int reg,int dtarget)2888e3e3a7aSWarner Losh static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,
2898e3e3a7aSWarner Losh                           int dtarget) {
2908e3e3a7aSWarner Losh   while (list != NO_JUMP) {
2918e3e3a7aSWarner Losh     int next = getjump(fs, list);
2928e3e3a7aSWarner Losh     if (patchtestreg(fs, list, reg))
2938e3e3a7aSWarner Losh       fixjump(fs, list, vtarget);
2948e3e3a7aSWarner Losh     else
2958e3e3a7aSWarner Losh       fixjump(fs, list, dtarget);  /* jump to default target */
2968e3e3a7aSWarner Losh     list = next;
2978e3e3a7aSWarner Losh   }
2988e3e3a7aSWarner Losh }
2998e3e3a7aSWarner Losh 
3008e3e3a7aSWarner Losh 
3018e3e3a7aSWarner Losh /*
3028e3e3a7aSWarner Losh ** Path all jumps in 'list' to jump to 'target'.
3038e3e3a7aSWarner Losh ** (The assert means that we cannot fix a jump to a forward address
3048e3e3a7aSWarner Losh ** because we only know addresses once code is generated.)
3058e3e3a7aSWarner Losh */
luaK_patchlist(FuncState * fs,int list,int target)3068e3e3a7aSWarner Losh void luaK_patchlist (FuncState *fs, int list, int target) {
3070495ed39SKyle Evans   lua_assert(target <= fs->pc);
3088e3e3a7aSWarner Losh   patchlistaux(fs, list, target, NO_REG, target);
3098e3e3a7aSWarner Losh }
3100495ed39SKyle Evans 
3110495ed39SKyle Evans 
luaK_patchtohere(FuncState * fs,int list)3120495ed39SKyle Evans void luaK_patchtohere (FuncState *fs, int list) {
3130495ed39SKyle Evans   int hr = luaK_getlabel(fs);  /* mark "here" as a jump target */
3140495ed39SKyle Evans   luaK_patchlist(fs, list, hr);
3150495ed39SKyle Evans }
3160495ed39SKyle Evans 
3170495ed39SKyle Evans 
3180495ed39SKyle Evans /* limit for difference between lines in relative line info. */
3190495ed39SKyle Evans #define LIMLINEDIFF	0x80
3200495ed39SKyle Evans 
3210495ed39SKyle Evans 
3220495ed39SKyle Evans /*
3230495ed39SKyle Evans ** Save line info for a new instruction. If difference from last line
3240495ed39SKyle Evans ** does not fit in a byte, of after that many instructions, save a new
3250495ed39SKyle Evans ** absolute line info; (in that case, the special value 'ABSLINEINFO'
3260495ed39SKyle Evans ** in 'lineinfo' signals the existence of this absolute information.)
3270495ed39SKyle Evans ** Otherwise, store the difference from last line in 'lineinfo'.
3280495ed39SKyle Evans */
savelineinfo(FuncState * fs,Proto * f,int line)3290495ed39SKyle Evans static void savelineinfo (FuncState *fs, Proto *f, int line) {
3300495ed39SKyle Evans   int linedif = line - fs->previousline;
3310495ed39SKyle Evans   int pc = fs->pc - 1;  /* last instruction coded */
3328c784bb8SWarner Losh   if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) {
3330495ed39SKyle Evans     luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
3340495ed39SKyle Evans                     f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
3350495ed39SKyle Evans     f->abslineinfo[fs->nabslineinfo].pc = pc;
3360495ed39SKyle Evans     f->abslineinfo[fs->nabslineinfo++].line = line;
3370495ed39SKyle Evans     linedif = ABSLINEINFO;  /* signal that there is absolute information */
3388c784bb8SWarner Losh     fs->iwthabs = 1;  /* restart counter */
3390495ed39SKyle Evans   }
3400495ed39SKyle Evans   luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
3410495ed39SKyle Evans                   MAX_INT, "opcodes");
3420495ed39SKyle Evans   f->lineinfo[pc] = linedif;
3430495ed39SKyle Evans   fs->previousline = line;  /* last line saved */
3440495ed39SKyle Evans }
3450495ed39SKyle Evans 
3460495ed39SKyle Evans 
3470495ed39SKyle Evans /*
3480495ed39SKyle Evans ** Remove line information from the last instruction.
3490495ed39SKyle Evans ** If line information for that instruction is absolute, set 'iwthabs'
3500495ed39SKyle Evans ** above its max to force the new (replacing) instruction to have
3510495ed39SKyle Evans ** absolute line info, too.
3520495ed39SKyle Evans */
removelastlineinfo(FuncState * fs)3530495ed39SKyle Evans static void removelastlineinfo (FuncState *fs) {
3540495ed39SKyle Evans   Proto *f = fs->f;
3550495ed39SKyle Evans   int pc = fs->pc - 1;  /* last instruction coded */
3560495ed39SKyle Evans   if (f->lineinfo[pc] != ABSLINEINFO) {  /* relative line info? */
3570495ed39SKyle Evans     fs->previousline -= f->lineinfo[pc];  /* correct last line saved */
3580495ed39SKyle Evans     fs->iwthabs--;  /* undo previous increment */
3590495ed39SKyle Evans   }
3600495ed39SKyle Evans   else {  /* absolute line information */
3610495ed39SKyle Evans     lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc);
3620495ed39SKyle Evans     fs->nabslineinfo--;  /* remove it */
3630495ed39SKyle Evans     fs->iwthabs = MAXIWTHABS + 1;  /* force next line info to be absolute */
3640495ed39SKyle Evans   }
3658e3e3a7aSWarner Losh }
3668e3e3a7aSWarner Losh 
3678e3e3a7aSWarner Losh 
3688e3e3a7aSWarner Losh /*
3690495ed39SKyle Evans ** Remove the last instruction created, correcting line information
3700495ed39SKyle Evans ** accordingly.
3718e3e3a7aSWarner Losh */
removelastinstruction(FuncState * fs)3720495ed39SKyle Evans static void removelastinstruction (FuncState *fs) {
3730495ed39SKyle Evans   removelastlineinfo(fs);
3740495ed39SKyle Evans   fs->pc--;
3758e3e3a7aSWarner Losh }
3768e3e3a7aSWarner Losh 
3778e3e3a7aSWarner Losh 
3788e3e3a7aSWarner Losh /*
3798e3e3a7aSWarner Losh ** Emit instruction 'i', checking for array sizes and saving also its
3808e3e3a7aSWarner Losh ** line information. Return 'i' position.
3818e3e3a7aSWarner Losh */
luaK_code(FuncState * fs,Instruction i)3820495ed39SKyle Evans int luaK_code (FuncState *fs, Instruction i) {
3838e3e3a7aSWarner Losh   Proto *f = fs->f;
3848e3e3a7aSWarner Losh   /* put new instruction in code array */
3858e3e3a7aSWarner Losh   luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
3868e3e3a7aSWarner Losh                   MAX_INT, "opcodes");
3870495ed39SKyle Evans   f->code[fs->pc++] = i;
3880495ed39SKyle Evans   savelineinfo(fs, f, fs->ls->lastline);
3890495ed39SKyle Evans   return fs->pc - 1;  /* index of new instruction */
3908e3e3a7aSWarner Losh }
3918e3e3a7aSWarner Losh 
3928e3e3a7aSWarner Losh 
3938e3e3a7aSWarner Losh /*
3948e3e3a7aSWarner Losh ** Format and emit an 'iABC' instruction. (Assertions check consistency
3958e3e3a7aSWarner Losh ** of parameters versus opcode.)
3968e3e3a7aSWarner Losh */
luaK_codeABCk(FuncState * fs,OpCode o,int a,int b,int c,int k)3970495ed39SKyle Evans int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) {
3988e3e3a7aSWarner Losh   lua_assert(getOpMode(o) == iABC);
3990495ed39SKyle Evans   lua_assert(a <= MAXARG_A && b <= MAXARG_B &&
4000495ed39SKyle Evans              c <= MAXARG_C && (k & ~1) == 0);
4010495ed39SKyle Evans   return luaK_code(fs, CREATE_ABCk(o, a, b, c, k));
4028e3e3a7aSWarner Losh }
4038e3e3a7aSWarner Losh 
4048e3e3a7aSWarner Losh 
4058e3e3a7aSWarner Losh /*
4068e3e3a7aSWarner Losh ** Format and emit an 'iABx' instruction.
4078e3e3a7aSWarner Losh */
luaK_codeABx(FuncState * fs,OpCode o,int a,unsigned int bc)4088e3e3a7aSWarner Losh int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
4090495ed39SKyle Evans   lua_assert(getOpMode(o) == iABx);
4108e3e3a7aSWarner Losh   lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx);
4118e3e3a7aSWarner Losh   return luaK_code(fs, CREATE_ABx(o, a, bc));
4128e3e3a7aSWarner Losh }
4138e3e3a7aSWarner Losh 
4148e3e3a7aSWarner Losh 
4158e3e3a7aSWarner Losh /*
4160495ed39SKyle Evans ** Format and emit an 'iAsBx' instruction.
4170495ed39SKyle Evans */
luaK_codeAsBx(FuncState * fs,OpCode o,int a,int bc)4180495ed39SKyle Evans int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
4190495ed39SKyle Evans   unsigned int b = bc + OFFSET_sBx;
4200495ed39SKyle Evans   lua_assert(getOpMode(o) == iAsBx);
4210495ed39SKyle Evans   lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
4220495ed39SKyle Evans   return luaK_code(fs, CREATE_ABx(o, a, b));
4230495ed39SKyle Evans }
4240495ed39SKyle Evans 
4250495ed39SKyle Evans 
4260495ed39SKyle Evans /*
4270495ed39SKyle Evans ** Format and emit an 'isJ' instruction.
4280495ed39SKyle Evans */
codesJ(FuncState * fs,OpCode o,int sj,int k)4290495ed39SKyle Evans static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
4300495ed39SKyle Evans   unsigned int j = sj + OFFSET_sJ;
4310495ed39SKyle Evans   lua_assert(getOpMode(o) == isJ);
4320495ed39SKyle Evans   lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
4330495ed39SKyle Evans   return luaK_code(fs, CREATE_sJ(o, j, k));
4340495ed39SKyle Evans }
4350495ed39SKyle Evans 
4360495ed39SKyle Evans 
4370495ed39SKyle Evans /*
4388e3e3a7aSWarner Losh ** Emit an "extra argument" instruction (format 'iAx')
4398e3e3a7aSWarner Losh */
codeextraarg(FuncState * fs,int a)4408e3e3a7aSWarner Losh static int codeextraarg (FuncState *fs, int a) {
4418e3e3a7aSWarner Losh   lua_assert(a <= MAXARG_Ax);
4428e3e3a7aSWarner Losh   return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
4438e3e3a7aSWarner Losh }
4448e3e3a7aSWarner Losh 
4458e3e3a7aSWarner Losh 
4468e3e3a7aSWarner Losh /*
4478e3e3a7aSWarner Losh ** Emit a "load constant" instruction, using either 'OP_LOADK'
4488e3e3a7aSWarner Losh ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
4498e3e3a7aSWarner Losh ** instruction with "extra argument".
4508e3e3a7aSWarner Losh */
luaK_codek(FuncState * fs,int reg,int k)4510495ed39SKyle Evans static int luaK_codek (FuncState *fs, int reg, int k) {
4528e3e3a7aSWarner Losh   if (k <= MAXARG_Bx)
4538e3e3a7aSWarner Losh     return luaK_codeABx(fs, OP_LOADK, reg, k);
4548e3e3a7aSWarner Losh   else {
4558e3e3a7aSWarner Losh     int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
4568e3e3a7aSWarner Losh     codeextraarg(fs, k);
4578e3e3a7aSWarner Losh     return p;
4588e3e3a7aSWarner Losh   }
4598e3e3a7aSWarner Losh }
4608e3e3a7aSWarner Losh 
4618e3e3a7aSWarner Losh 
4628e3e3a7aSWarner Losh /*
4638e3e3a7aSWarner Losh ** Check register-stack level, keeping track of its maximum size
4648e3e3a7aSWarner Losh ** in field 'maxstacksize'
4658e3e3a7aSWarner Losh */
luaK_checkstack(FuncState * fs,int n)4668e3e3a7aSWarner Losh void luaK_checkstack (FuncState *fs, int n) {
4678e3e3a7aSWarner Losh   int newstack = fs->freereg + n;
4688e3e3a7aSWarner Losh   if (newstack > fs->f->maxstacksize) {
4698e3e3a7aSWarner Losh     if (newstack >= MAXREGS)
4708e3e3a7aSWarner Losh       luaX_syntaxerror(fs->ls,
4718e3e3a7aSWarner Losh         "function or expression needs too many registers");
4728e3e3a7aSWarner Losh     fs->f->maxstacksize = cast_byte(newstack);
4738e3e3a7aSWarner Losh   }
4748e3e3a7aSWarner Losh }
4758e3e3a7aSWarner Losh 
4768e3e3a7aSWarner Losh 
4778e3e3a7aSWarner Losh /*
4788e3e3a7aSWarner Losh ** Reserve 'n' registers in register stack
4798e3e3a7aSWarner Losh */
luaK_reserveregs(FuncState * fs,int n)4808e3e3a7aSWarner Losh void luaK_reserveregs (FuncState *fs, int n) {
4818e3e3a7aSWarner Losh   luaK_checkstack(fs, n);
4828e3e3a7aSWarner Losh   fs->freereg += n;
4838e3e3a7aSWarner Losh }
4848e3e3a7aSWarner Losh 
4858e3e3a7aSWarner Losh 
4868e3e3a7aSWarner Losh /*
4878e3e3a7aSWarner Losh ** Free register 'reg', if it is neither a constant index nor
4888e3e3a7aSWarner Losh ** a local variable.
4898e3e3a7aSWarner Losh )
4908e3e3a7aSWarner Losh */
freereg(FuncState * fs,int reg)4918e3e3a7aSWarner Losh static void freereg (FuncState *fs, int reg) {
4920495ed39SKyle Evans   if (reg >= luaY_nvarstack(fs)) {
4938e3e3a7aSWarner Losh     fs->freereg--;
4948e3e3a7aSWarner Losh     lua_assert(reg == fs->freereg);
4958e3e3a7aSWarner Losh   }
4968e3e3a7aSWarner Losh }
4978e3e3a7aSWarner Losh 
4988e3e3a7aSWarner Losh 
4998e3e3a7aSWarner Losh /*
5000495ed39SKyle Evans ** Free two registers in proper order
5010495ed39SKyle Evans */
freeregs(FuncState * fs,int r1,int r2)5020495ed39SKyle Evans static void freeregs (FuncState *fs, int r1, int r2) {
5030495ed39SKyle Evans   if (r1 > r2) {
5040495ed39SKyle Evans     freereg(fs, r1);
5050495ed39SKyle Evans     freereg(fs, r2);
5060495ed39SKyle Evans   }
5070495ed39SKyle Evans   else {
5080495ed39SKyle Evans     freereg(fs, r2);
5090495ed39SKyle Evans     freereg(fs, r1);
5100495ed39SKyle Evans   }
5110495ed39SKyle Evans }
5120495ed39SKyle Evans 
5130495ed39SKyle Evans 
5140495ed39SKyle Evans /*
5158e3e3a7aSWarner Losh ** Free register used by expression 'e' (if any)
5168e3e3a7aSWarner Losh */
freeexp(FuncState * fs,expdesc * e)5178e3e3a7aSWarner Losh static void freeexp (FuncState *fs, expdesc *e) {
5188e3e3a7aSWarner Losh   if (e->k == VNONRELOC)
5198e3e3a7aSWarner Losh     freereg(fs, e->u.info);
5208e3e3a7aSWarner Losh }
5218e3e3a7aSWarner Losh 
5228e3e3a7aSWarner Losh 
5238e3e3a7aSWarner Losh /*
5248e3e3a7aSWarner Losh ** Free registers used by expressions 'e1' and 'e2' (if any) in proper
5258e3e3a7aSWarner Losh ** order.
5268e3e3a7aSWarner Losh */
freeexps(FuncState * fs,expdesc * e1,expdesc * e2)5278e3e3a7aSWarner Losh static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) {
5288e3e3a7aSWarner Losh   int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1;
5298e3e3a7aSWarner Losh   int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1;
5300495ed39SKyle Evans   freeregs(fs, r1, r2);
5318e3e3a7aSWarner Losh }
5328e3e3a7aSWarner Losh 
5338e3e3a7aSWarner Losh 
5348e3e3a7aSWarner Losh /*
5358e3e3a7aSWarner Losh ** Add constant 'v' to prototype's list of constants (field 'k').
5368e3e3a7aSWarner Losh ** Use scanner's table to cache position of constants in constant list
5378e3e3a7aSWarner Losh ** and try to reuse constants. Because some values should not be used
5388e3e3a7aSWarner Losh ** as keys (nil cannot be a key, integer keys can collapse with float
5398e3e3a7aSWarner Losh ** keys), the caller must provide a useful 'key' for indexing the cache.
5408c784bb8SWarner Losh ** Note that all functions share the same table, so entering or exiting
5418c784bb8SWarner Losh ** a function can make some indices wrong.
5428e3e3a7aSWarner Losh */
addk(FuncState * fs,TValue * key,TValue * v)5438e3e3a7aSWarner Losh static int addk (FuncState *fs, TValue *key, TValue *v) {
5448c784bb8SWarner Losh   TValue val;
5458e3e3a7aSWarner Losh   lua_State *L = fs->ls->L;
5468e3e3a7aSWarner Losh   Proto *f = fs->f;
5478c784bb8SWarner Losh   const TValue *idx = luaH_get(fs->ls->h, key);  /* query scanner table */
5488e3e3a7aSWarner Losh   int k, oldsize;
5498e3e3a7aSWarner Losh   if (ttisinteger(idx)) {  /* is there an index there? */
5508e3e3a7aSWarner Losh     k = cast_int(ivalue(idx));
5518e3e3a7aSWarner Losh     /* correct value? (warning: must distinguish floats from integers!) */
5520495ed39SKyle Evans     if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
5538e3e3a7aSWarner Losh                       luaV_rawequalobj(&f->k[k], v))
5548e3e3a7aSWarner Losh       return k;  /* reuse index */
5558e3e3a7aSWarner Losh   }
5568e3e3a7aSWarner Losh   /* constant not found; create a new entry */
5578e3e3a7aSWarner Losh   oldsize = f->sizek;
5588e3e3a7aSWarner Losh   k = fs->nk;
5598e3e3a7aSWarner Losh   /* numerical value does not need GC barrier;
5608e3e3a7aSWarner Losh      table has no metatable, so it does not need to invalidate cache */
5618c784bb8SWarner Losh   setivalue(&val, k);
5628c784bb8SWarner Losh   luaH_finishset(L, fs->ls->h, key, idx, &val);
5638e3e3a7aSWarner Losh   luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
5648e3e3a7aSWarner Losh   while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
5658e3e3a7aSWarner Losh   setobj(L, &f->k[k], v);
5668e3e3a7aSWarner Losh   fs->nk++;
5678e3e3a7aSWarner Losh   luaC_barrier(L, f, v);
5688e3e3a7aSWarner Losh   return k;
5698e3e3a7aSWarner Losh }
5708e3e3a7aSWarner Losh 
5718e3e3a7aSWarner Losh 
5728e3e3a7aSWarner Losh /*
5738e3e3a7aSWarner Losh ** Add a string to list of constants and return its index.
5748e3e3a7aSWarner Losh */
stringK(FuncState * fs,TString * s)5750495ed39SKyle Evans static int stringK (FuncState *fs, TString *s) {
5768e3e3a7aSWarner Losh   TValue o;
5778e3e3a7aSWarner Losh   setsvalue(fs->ls->L, &o, s);
5788e3e3a7aSWarner Losh   return addk(fs, &o, &o);  /* use string itself as key */
5798e3e3a7aSWarner Losh }
5808e3e3a7aSWarner Losh 
5818e3e3a7aSWarner Losh 
5828e3e3a7aSWarner Losh /*
5838e3e3a7aSWarner Losh ** Add an integer to list of constants and return its index.
5848e3e3a7aSWarner Losh */
luaK_intK(FuncState * fs,lua_Integer n)5850495ed39SKyle Evans static int luaK_intK (FuncState *fs, lua_Integer n) {
5868c784bb8SWarner Losh   TValue o;
5878e3e3a7aSWarner Losh   setivalue(&o, n);
5888c784bb8SWarner Losh   return addk(fs, &o, &o);  /* use integer itself as key */
5898e3e3a7aSWarner Losh }
5908e3e3a7aSWarner Losh 
5918e3e3a7aSWarner Losh /*
5928c784bb8SWarner Losh ** Add a float to list of constants and return its index. Floats
5938c784bb8SWarner Losh ** with integral values need a different key, to avoid collision
5948c784bb8SWarner Losh ** with actual integers. To that, we add to the number its smaller
5958c784bb8SWarner Losh ** power-of-two fraction that is still significant in its scale.
5968c784bb8SWarner Losh ** For doubles, that would be 1/2^52.
5978c784bb8SWarner Losh ** (This method is not bulletproof: there may be another float
5988c784bb8SWarner Losh ** with that value, and for floats larger than 2^53 the result is
5998c784bb8SWarner Losh ** still an integer. At worst, this only wastes an entry with
6008c784bb8SWarner Losh ** a duplicate.)
6018e3e3a7aSWarner Losh */
luaK_numberK(FuncState * fs,lua_Number r)6028e3e3a7aSWarner Losh static int luaK_numberK (FuncState *fs, lua_Number r) {
6038e3e3a7aSWarner Losh   TValue o;
6048c784bb8SWarner Losh   lua_Integer ik;
6058e3e3a7aSWarner Losh   setfltvalue(&o, r);
6068c784bb8SWarner Losh #ifndef LUA_AVOID_FLOAT
6078c784bb8SWarner Losh   if (!luaV_flttointeger(r, &ik, F2Ieq))  /* not an integral value? */
6088e3e3a7aSWarner Losh     return addk(fs, &o, &o);  /* use number itself as key */
6098c784bb8SWarner Losh   else {  /* must build an alternative key */
6108c784bb8SWarner Losh     const int nbm = l_floatatt(MANT_DIG);
6118c784bb8SWarner Losh     const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
6128c784bb8SWarner Losh     const lua_Number k = (ik == 0) ? q : r + r*q;  /* new key */
6138c784bb8SWarner Losh     TValue kv;
6148c784bb8SWarner Losh     setfltvalue(&kv, k);
6158c784bb8SWarner Losh     /* result is not an integral value, unless value is too large */
6168c784bb8SWarner Losh     lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) ||
6178c784bb8SWarner Losh                 l_mathop(fabs)(r) >= l_mathop(1e6));
6188c784bb8SWarner Losh     return addk(fs, &kv, &o);
6198c784bb8SWarner Losh   }
6208c784bb8SWarner Losh #else
6218c784bb8SWarner Losh   /*
6228c784bb8SWarner Losh   ** When we're avoiding floats, allow any collision since floats are ints.
6238c784bb8SWarner Losh   */
6248c784bb8SWarner Losh   return addk(fs, &o, &o);  /* use number itself as key */
6258c784bb8SWarner Losh #endif
6268e3e3a7aSWarner Losh }
6278e3e3a7aSWarner Losh 
6288e3e3a7aSWarner Losh 
6298e3e3a7aSWarner Losh /*
6300495ed39SKyle Evans ** Add a false to list of constants and return its index.
6318e3e3a7aSWarner Losh */
boolF(FuncState * fs)6320495ed39SKyle Evans static int boolF (FuncState *fs) {
6338e3e3a7aSWarner Losh   TValue o;
6340495ed39SKyle Evans   setbfvalue(&o);
6350495ed39SKyle Evans   return addk(fs, &o, &o);  /* use boolean itself as key */
6360495ed39SKyle Evans }
6370495ed39SKyle Evans 
6380495ed39SKyle Evans 
6390495ed39SKyle Evans /*
6400495ed39SKyle Evans ** Add a true to list of constants and return its index.
6410495ed39SKyle Evans */
boolT(FuncState * fs)6420495ed39SKyle Evans static int boolT (FuncState *fs) {
6430495ed39SKyle Evans   TValue o;
6440495ed39SKyle Evans   setbtvalue(&o);
6458e3e3a7aSWarner Losh   return addk(fs, &o, &o);  /* use boolean itself as key */
6468e3e3a7aSWarner Losh }
6478e3e3a7aSWarner Losh 
6488e3e3a7aSWarner Losh 
6498e3e3a7aSWarner Losh /*
6508e3e3a7aSWarner Losh ** Add nil to list of constants and return its index.
6518e3e3a7aSWarner Losh */
nilK(FuncState * fs)6528e3e3a7aSWarner Losh static int nilK (FuncState *fs) {
6538e3e3a7aSWarner Losh   TValue k, v;
6548e3e3a7aSWarner Losh   setnilvalue(&v);
6558e3e3a7aSWarner Losh   /* cannot use nil as key; instead use table itself to represent nil */
6568e3e3a7aSWarner Losh   sethvalue(fs->ls->L, &k, fs->ls->h);
6578e3e3a7aSWarner Losh   return addk(fs, &k, &v);
6588e3e3a7aSWarner Losh }
6598e3e3a7aSWarner Losh 
6608e3e3a7aSWarner Losh 
6618e3e3a7aSWarner Losh /*
6620495ed39SKyle Evans ** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
6630495ed39SKyle Evans ** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
6640495ed39SKyle Evans ** overflows in the hidden addition inside 'int2sC'.
6650495ed39SKyle Evans */
fitsC(lua_Integer i)6660495ed39SKyle Evans static int fitsC (lua_Integer i) {
6670495ed39SKyle Evans   return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C));
6680495ed39SKyle Evans }
6690495ed39SKyle Evans 
6700495ed39SKyle Evans 
6710495ed39SKyle Evans /*
6720495ed39SKyle Evans ** Check whether 'i' can be stored in an 'sBx' operand.
6730495ed39SKyle Evans */
fitsBx(lua_Integer i)6740495ed39SKyle Evans static int fitsBx (lua_Integer i) {
6750495ed39SKyle Evans   return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx);
6760495ed39SKyle Evans }
6770495ed39SKyle Evans 
6780495ed39SKyle Evans 
luaK_int(FuncState * fs,int reg,lua_Integer i)6790495ed39SKyle Evans void luaK_int (FuncState *fs, int reg, lua_Integer i) {
6800495ed39SKyle Evans   if (fitsBx(i))
6810495ed39SKyle Evans     luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
6820495ed39SKyle Evans   else
6830495ed39SKyle Evans     luaK_codek(fs, reg, luaK_intK(fs, i));
6840495ed39SKyle Evans }
6850495ed39SKyle Evans 
6860495ed39SKyle Evans 
luaK_float(FuncState * fs,int reg,lua_Number f)6870495ed39SKyle Evans static void luaK_float (FuncState *fs, int reg, lua_Number f) {
6880495ed39SKyle Evans   lua_Integer fi;
6890495ed39SKyle Evans   if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
6900495ed39SKyle Evans     luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
6910495ed39SKyle Evans   else
6920495ed39SKyle Evans     luaK_codek(fs, reg, luaK_numberK(fs, f));
6930495ed39SKyle Evans }
6940495ed39SKyle Evans 
6950495ed39SKyle Evans 
6960495ed39SKyle Evans /*
6970495ed39SKyle Evans ** Convert a constant in 'v' into an expression description 'e'
6980495ed39SKyle Evans */
const2exp(TValue * v,expdesc * e)6990495ed39SKyle Evans static void const2exp (TValue *v, expdesc *e) {
7000495ed39SKyle Evans   switch (ttypetag(v)) {
7010495ed39SKyle Evans     case LUA_VNUMINT:
7020495ed39SKyle Evans       e->k = VKINT; e->u.ival = ivalue(v);
7030495ed39SKyle Evans       break;
7040495ed39SKyle Evans     case LUA_VNUMFLT:
7050495ed39SKyle Evans       e->k = VKFLT; e->u.nval = fltvalue(v);
7060495ed39SKyle Evans       break;
7070495ed39SKyle Evans     case LUA_VFALSE:
7080495ed39SKyle Evans       e->k = VFALSE;
7090495ed39SKyle Evans       break;
7100495ed39SKyle Evans     case LUA_VTRUE:
7110495ed39SKyle Evans       e->k = VTRUE;
7120495ed39SKyle Evans       break;
7130495ed39SKyle Evans     case LUA_VNIL:
7140495ed39SKyle Evans       e->k = VNIL;
7150495ed39SKyle Evans       break;
7160495ed39SKyle Evans     case LUA_VSHRSTR:  case LUA_VLNGSTR:
7170495ed39SKyle Evans       e->k = VKSTR; e->u.strval = tsvalue(v);
7180495ed39SKyle Evans       break;
7190495ed39SKyle Evans     default: lua_assert(0);
7200495ed39SKyle Evans   }
7210495ed39SKyle Evans }
7220495ed39SKyle Evans 
7230495ed39SKyle Evans 
7240495ed39SKyle Evans /*
7258e3e3a7aSWarner Losh ** Fix an expression to return the number of results 'nresults'.
7260495ed39SKyle Evans ** 'e' must be a multi-ret expression (function call or vararg).
7278e3e3a7aSWarner Losh */
luaK_setreturns(FuncState * fs,expdesc * e,int nresults)7288e3e3a7aSWarner Losh void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
7298e3e3a7aSWarner Losh   Instruction *pc = &getinstruction(fs, e);
7300495ed39SKyle Evans   if (e->k == VCALL)  /* expression is an open function call? */
7310495ed39SKyle Evans     SETARG_C(*pc, nresults + 1);
7320495ed39SKyle Evans   else {
7330495ed39SKyle Evans     lua_assert(e->k == VVARARG);
7340495ed39SKyle Evans     SETARG_C(*pc, nresults + 1);
7358e3e3a7aSWarner Losh     SETARG_A(*pc, fs->freereg);
7368e3e3a7aSWarner Losh     luaK_reserveregs(fs, 1);
7378e3e3a7aSWarner Losh   }
7380495ed39SKyle Evans }
7390495ed39SKyle Evans 
7400495ed39SKyle Evans 
7410495ed39SKyle Evans /*
7420495ed39SKyle Evans ** Convert a VKSTR to a VK
7430495ed39SKyle Evans */
str2K(FuncState * fs,expdesc * e)7440495ed39SKyle Evans static void str2K (FuncState *fs, expdesc *e) {
7450495ed39SKyle Evans   lua_assert(e->k == VKSTR);
7460495ed39SKyle Evans   e->u.info = stringK(fs, e->u.strval);
7470495ed39SKyle Evans   e->k = VK;
7488e3e3a7aSWarner Losh }
7498e3e3a7aSWarner Losh 
7508e3e3a7aSWarner Losh 
7518e3e3a7aSWarner Losh /*
7528e3e3a7aSWarner Losh ** Fix an expression to return one result.
7538e3e3a7aSWarner Losh ** If expression is not a multi-ret expression (function call or
7548e3e3a7aSWarner Losh ** vararg), it already returns one result, so nothing needs to be done.
7558e3e3a7aSWarner Losh ** Function calls become VNONRELOC expressions (as its result comes
7568e3e3a7aSWarner Losh ** fixed in the base register of the call), while vararg expressions
7570495ed39SKyle Evans ** become VRELOC (as OP_VARARG puts its results where it wants).
7588e3e3a7aSWarner Losh ** (Calls are created returning one result, so that does not need
7598e3e3a7aSWarner Losh ** to be fixed.)
7608e3e3a7aSWarner Losh */
luaK_setoneret(FuncState * fs,expdesc * e)7618e3e3a7aSWarner Losh void luaK_setoneret (FuncState *fs, expdesc *e) {
7628e3e3a7aSWarner Losh   if (e->k == VCALL) {  /* expression is an open function call? */
7638e3e3a7aSWarner Losh     /* already returns 1 value */
7648e3e3a7aSWarner Losh     lua_assert(GETARG_C(getinstruction(fs, e)) == 2);
7658e3e3a7aSWarner Losh     e->k = VNONRELOC;  /* result has fixed position */
7668e3e3a7aSWarner Losh     e->u.info = GETARG_A(getinstruction(fs, e));
7678e3e3a7aSWarner Losh   }
7688e3e3a7aSWarner Losh   else if (e->k == VVARARG) {
7690495ed39SKyle Evans     SETARG_C(getinstruction(fs, e), 2);
7700495ed39SKyle Evans     e->k = VRELOC;  /* can relocate its simple result */
7718e3e3a7aSWarner Losh   }
7728e3e3a7aSWarner Losh }
7738e3e3a7aSWarner Losh 
7748e3e3a7aSWarner Losh 
7758e3e3a7aSWarner Losh /*
7760495ed39SKyle Evans ** Ensure that expression 'e' is not a variable (nor a <const>).
7770495ed39SKyle Evans ** (Expression still may have jump lists.)
7788e3e3a7aSWarner Losh */
luaK_dischargevars(FuncState * fs,expdesc * e)7798e3e3a7aSWarner Losh void luaK_dischargevars (FuncState *fs, expdesc *e) {
7808e3e3a7aSWarner Losh   switch (e->k) {
7810495ed39SKyle Evans     case VCONST: {
7820495ed39SKyle Evans       const2exp(const2val(fs, e), e);
7830495ed39SKyle Evans       break;
7840495ed39SKyle Evans     }
7858e3e3a7aSWarner Losh     case VLOCAL: {  /* already in a register */
7868c784bb8SWarner Losh       e->u.info = e->u.var.ridx;
7878e3e3a7aSWarner Losh       e->k = VNONRELOC;  /* becomes a non-relocatable value */
7888e3e3a7aSWarner Losh       break;
7898e3e3a7aSWarner Losh     }
7908e3e3a7aSWarner Losh     case VUPVAL: {  /* move value to some (pending) register */
7918e3e3a7aSWarner Losh       e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
7920495ed39SKyle Evans       e->k = VRELOC;
7930495ed39SKyle Evans       break;
7940495ed39SKyle Evans     }
7950495ed39SKyle Evans     case VINDEXUP: {
7960495ed39SKyle Evans       e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
7970495ed39SKyle Evans       e->k = VRELOC;
7980495ed39SKyle Evans       break;
7990495ed39SKyle Evans     }
8000495ed39SKyle Evans     case VINDEXI: {
8010495ed39SKyle Evans       freereg(fs, e->u.ind.t);
8020495ed39SKyle Evans       e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
8030495ed39SKyle Evans       e->k = VRELOC;
8040495ed39SKyle Evans       break;
8050495ed39SKyle Evans     }
8060495ed39SKyle Evans     case VINDEXSTR: {
8070495ed39SKyle Evans       freereg(fs, e->u.ind.t);
8080495ed39SKyle Evans       e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
8090495ed39SKyle Evans       e->k = VRELOC;
8108e3e3a7aSWarner Losh       break;
8118e3e3a7aSWarner Losh     }
8128e3e3a7aSWarner Losh     case VINDEXED: {
8130495ed39SKyle Evans       freeregs(fs, e->u.ind.t, e->u.ind.idx);
8140495ed39SKyle Evans       e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
8150495ed39SKyle Evans       e->k = VRELOC;
8168e3e3a7aSWarner Losh       break;
8178e3e3a7aSWarner Losh     }
8188e3e3a7aSWarner Losh     case VVARARG: case VCALL: {
8198e3e3a7aSWarner Losh       luaK_setoneret(fs, e);
8208e3e3a7aSWarner Losh       break;
8218e3e3a7aSWarner Losh     }
8228e3e3a7aSWarner Losh     default: break;  /* there is one value available (somewhere) */
8238e3e3a7aSWarner Losh   }
8248e3e3a7aSWarner Losh }
8258e3e3a7aSWarner Losh 
8268e3e3a7aSWarner Losh 
8278e3e3a7aSWarner Losh /*
8280495ed39SKyle Evans ** Ensure expression value is in register 'reg', making 'e' a
8290495ed39SKyle Evans ** non-relocatable expression.
8300495ed39SKyle Evans ** (Expression still may have jump lists.)
8318e3e3a7aSWarner Losh */
discharge2reg(FuncState * fs,expdesc * e,int reg)8328e3e3a7aSWarner Losh static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
8338e3e3a7aSWarner Losh   luaK_dischargevars(fs, e);
8348e3e3a7aSWarner Losh   switch (e->k) {
8358e3e3a7aSWarner Losh     case VNIL: {
8368e3e3a7aSWarner Losh       luaK_nil(fs, reg, 1);
8378e3e3a7aSWarner Losh       break;
8388e3e3a7aSWarner Losh     }
8390495ed39SKyle Evans     case VFALSE: {
8400495ed39SKyle Evans       luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
8418e3e3a7aSWarner Losh       break;
8428e3e3a7aSWarner Losh     }
8430495ed39SKyle Evans     case VTRUE: {
8440495ed39SKyle Evans       luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
8450495ed39SKyle Evans       break;
8460495ed39SKyle Evans     }
8470495ed39SKyle Evans     case VKSTR: {
8480495ed39SKyle Evans       str2K(fs, e);
8490495ed39SKyle Evans     }  /* FALLTHROUGH */
8508e3e3a7aSWarner Losh     case VK: {
8518e3e3a7aSWarner Losh       luaK_codek(fs, reg, e->u.info);
8528e3e3a7aSWarner Losh       break;
8538e3e3a7aSWarner Losh     }
8548e3e3a7aSWarner Losh     case VKFLT: {
8550495ed39SKyle Evans       luaK_float(fs, reg, e->u.nval);
8568e3e3a7aSWarner Losh       break;
8578e3e3a7aSWarner Losh     }
8588e3e3a7aSWarner Losh     case VKINT: {
8590495ed39SKyle Evans       luaK_int(fs, reg, e->u.ival);
8608e3e3a7aSWarner Losh       break;
8618e3e3a7aSWarner Losh     }
8620495ed39SKyle Evans     case VRELOC: {
8638e3e3a7aSWarner Losh       Instruction *pc = &getinstruction(fs, e);
8648e3e3a7aSWarner Losh       SETARG_A(*pc, reg);  /* instruction will put result in 'reg' */
8658e3e3a7aSWarner Losh       break;
8668e3e3a7aSWarner Losh     }
8678e3e3a7aSWarner Losh     case VNONRELOC: {
8688e3e3a7aSWarner Losh       if (reg != e->u.info)
8698e3e3a7aSWarner Losh         luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
8708e3e3a7aSWarner Losh       break;
8718e3e3a7aSWarner Losh     }
8728e3e3a7aSWarner Losh     default: {
8738e3e3a7aSWarner Losh       lua_assert(e->k == VJMP);
8748e3e3a7aSWarner Losh       return;  /* nothing to do... */
8758e3e3a7aSWarner Losh     }
8768e3e3a7aSWarner Losh   }
8778e3e3a7aSWarner Losh   e->u.info = reg;
8788e3e3a7aSWarner Losh   e->k = VNONRELOC;
8798e3e3a7aSWarner Losh }
8808e3e3a7aSWarner Losh 
8818e3e3a7aSWarner Losh 
8828e3e3a7aSWarner Losh /*
8830495ed39SKyle Evans ** Ensure expression value is in a register, making 'e' a
8840495ed39SKyle Evans ** non-relocatable expression.
8850495ed39SKyle Evans ** (Expression still may have jump lists.)
8868e3e3a7aSWarner Losh */
discharge2anyreg(FuncState * fs,expdesc * e)8878e3e3a7aSWarner Losh static void discharge2anyreg (FuncState *fs, expdesc *e) {
8888e3e3a7aSWarner Losh   if (e->k != VNONRELOC) {  /* no fixed register yet? */
8898e3e3a7aSWarner Losh     luaK_reserveregs(fs, 1);  /* get a register */
8908e3e3a7aSWarner Losh     discharge2reg(fs, e, fs->freereg-1);  /* put value there */
8918e3e3a7aSWarner Losh   }
8928e3e3a7aSWarner Losh }
8938e3e3a7aSWarner Losh 
8948e3e3a7aSWarner Losh 
code_loadbool(FuncState * fs,int A,OpCode op)8950495ed39SKyle Evans static int code_loadbool (FuncState *fs, int A, OpCode op) {
8968e3e3a7aSWarner Losh   luaK_getlabel(fs);  /* those instructions may be jump targets */
8970495ed39SKyle Evans   return luaK_codeABC(fs, op, A, 0, 0);
8988e3e3a7aSWarner Losh }
8998e3e3a7aSWarner Losh 
9008e3e3a7aSWarner Losh 
9018e3e3a7aSWarner Losh /*
9028e3e3a7aSWarner Losh ** check whether list has any jump that do not produce a value
9038e3e3a7aSWarner Losh ** or produce an inverted value
9048e3e3a7aSWarner Losh */
need_value(FuncState * fs,int list)9058e3e3a7aSWarner Losh static int need_value (FuncState *fs, int list) {
9068e3e3a7aSWarner Losh   for (; list != NO_JUMP; list = getjump(fs, list)) {
9078e3e3a7aSWarner Losh     Instruction i = *getjumpcontrol(fs, list);
9088e3e3a7aSWarner Losh     if (GET_OPCODE(i) != OP_TESTSET) return 1;
9098e3e3a7aSWarner Losh   }
9108e3e3a7aSWarner Losh   return 0;  /* not found */
9118e3e3a7aSWarner Losh }
9128e3e3a7aSWarner Losh 
9138e3e3a7aSWarner Losh 
9148e3e3a7aSWarner Losh /*
9150495ed39SKyle Evans ** Ensures final expression result (which includes results from its
9160495ed39SKyle Evans ** jump lists) is in register 'reg'.
9178e3e3a7aSWarner Losh ** If expression has jumps, need to patch these jumps either to
9188e3e3a7aSWarner Losh ** its final position or to "load" instructions (for those tests
9198e3e3a7aSWarner Losh ** that do not produce values).
9208e3e3a7aSWarner Losh */
exp2reg(FuncState * fs,expdesc * e,int reg)9218e3e3a7aSWarner Losh static void exp2reg (FuncState *fs, expdesc *e, int reg) {
9228e3e3a7aSWarner Losh   discharge2reg(fs, e, reg);
9238e3e3a7aSWarner Losh   if (e->k == VJMP)  /* expression itself is a test? */
9248e3e3a7aSWarner Losh     luaK_concat(fs, &e->t, e->u.info);  /* put this jump in 't' list */
9258e3e3a7aSWarner Losh   if (hasjumps(e)) {
9268e3e3a7aSWarner Losh     int final;  /* position after whole expression */
9278e3e3a7aSWarner Losh     int p_f = NO_JUMP;  /* position of an eventual LOAD false */
9288e3e3a7aSWarner Losh     int p_t = NO_JUMP;  /* position of an eventual LOAD true */
9298e3e3a7aSWarner Losh     if (need_value(fs, e->t) || need_value(fs, e->f)) {
9308e3e3a7aSWarner Losh       int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
9310495ed39SKyle Evans       p_f = code_loadbool(fs, reg, OP_LFALSESKIP);  /* skip next inst. */
9320495ed39SKyle Evans       p_t = code_loadbool(fs, reg, OP_LOADTRUE);
9330495ed39SKyle Evans       /* jump around these booleans if 'e' is not a test */
9348e3e3a7aSWarner Losh       luaK_patchtohere(fs, fj);
9358e3e3a7aSWarner Losh     }
9368e3e3a7aSWarner Losh     final = luaK_getlabel(fs);
9378e3e3a7aSWarner Losh     patchlistaux(fs, e->f, final, reg, p_f);
9388e3e3a7aSWarner Losh     patchlistaux(fs, e->t, final, reg, p_t);
9398e3e3a7aSWarner Losh   }
9408e3e3a7aSWarner Losh   e->f = e->t = NO_JUMP;
9418e3e3a7aSWarner Losh   e->u.info = reg;
9428e3e3a7aSWarner Losh   e->k = VNONRELOC;
9438e3e3a7aSWarner Losh }
9448e3e3a7aSWarner Losh 
9458e3e3a7aSWarner Losh 
9468e3e3a7aSWarner Losh /*
9470495ed39SKyle Evans ** Ensures final expression result is in next available register.
9488e3e3a7aSWarner Losh */
luaK_exp2nextreg(FuncState * fs,expdesc * e)9498e3e3a7aSWarner Losh void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
9508e3e3a7aSWarner Losh   luaK_dischargevars(fs, e);
9518e3e3a7aSWarner Losh   freeexp(fs, e);
9528e3e3a7aSWarner Losh   luaK_reserveregs(fs, 1);
9538e3e3a7aSWarner Losh   exp2reg(fs, e, fs->freereg - 1);
9548e3e3a7aSWarner Losh }
9558e3e3a7aSWarner Losh 
9568e3e3a7aSWarner Losh 
9578e3e3a7aSWarner Losh /*
9580495ed39SKyle Evans ** Ensures final expression result is in some (any) register
9590495ed39SKyle Evans ** and return that register.
9608e3e3a7aSWarner Losh */
luaK_exp2anyreg(FuncState * fs,expdesc * e)9618e3e3a7aSWarner Losh int luaK_exp2anyreg (FuncState *fs, expdesc *e) {
9628e3e3a7aSWarner Losh   luaK_dischargevars(fs, e);
9638e3e3a7aSWarner Losh   if (e->k == VNONRELOC) {  /* expression already has a register? */
9648e3e3a7aSWarner Losh     if (!hasjumps(e))  /* no jumps? */
9658e3e3a7aSWarner Losh       return e->u.info;  /* result is already in a register */
9660495ed39SKyle Evans     if (e->u.info >= luaY_nvarstack(fs)) {  /* reg. is not a local? */
9678e3e3a7aSWarner Losh       exp2reg(fs, e, e->u.info);  /* put final result in it */
9688e3e3a7aSWarner Losh       return e->u.info;
9698e3e3a7aSWarner Losh     }
9700495ed39SKyle Evans     /* else expression has jumps and cannot change its register
9710495ed39SKyle Evans        to hold the jump values, because it is a local variable.
9720495ed39SKyle Evans        Go through to the default case. */
9738e3e3a7aSWarner Losh   }
9740495ed39SKyle Evans   luaK_exp2nextreg(fs, e);  /* default: use next available register */
9758e3e3a7aSWarner Losh   return e->u.info;
9768e3e3a7aSWarner Losh }
9778e3e3a7aSWarner Losh 
9788e3e3a7aSWarner Losh 
9798e3e3a7aSWarner Losh /*
9800495ed39SKyle Evans ** Ensures final expression result is either in a register
9810495ed39SKyle Evans ** or in an upvalue.
9828e3e3a7aSWarner Losh */
luaK_exp2anyregup(FuncState * fs,expdesc * e)9838e3e3a7aSWarner Losh void luaK_exp2anyregup (FuncState *fs, expdesc *e) {
9848e3e3a7aSWarner Losh   if (e->k != VUPVAL || hasjumps(e))
9858e3e3a7aSWarner Losh     luaK_exp2anyreg(fs, e);
9868e3e3a7aSWarner Losh }
9878e3e3a7aSWarner Losh 
9888e3e3a7aSWarner Losh 
9898e3e3a7aSWarner Losh /*
9900495ed39SKyle Evans ** Ensures final expression result is either in a register
9910495ed39SKyle Evans ** or it is a constant.
9928e3e3a7aSWarner Losh */
luaK_exp2val(FuncState * fs,expdesc * e)9938e3e3a7aSWarner Losh void luaK_exp2val (FuncState *fs, expdesc *e) {
9948e3e3a7aSWarner Losh   if (hasjumps(e))
9958e3e3a7aSWarner Losh     luaK_exp2anyreg(fs, e);
9968e3e3a7aSWarner Losh   else
9978e3e3a7aSWarner Losh     luaK_dischargevars(fs, e);
9988e3e3a7aSWarner Losh }
9998e3e3a7aSWarner Losh 
10008e3e3a7aSWarner Losh 
10018e3e3a7aSWarner Losh /*
10020495ed39SKyle Evans ** Try to make 'e' a K expression with an index in the range of R/K
10030495ed39SKyle Evans ** indices. Return true iff succeeded.
10040495ed39SKyle Evans */
luaK_exp2K(FuncState * fs,expdesc * e)10050495ed39SKyle Evans static int luaK_exp2K (FuncState *fs, expdesc *e) {
10060495ed39SKyle Evans   if (!hasjumps(e)) {
10070495ed39SKyle Evans     int info;
10080495ed39SKyle Evans     switch (e->k) {  /* move constants to 'k' */
10090495ed39SKyle Evans       case VTRUE: info = boolT(fs); break;
10100495ed39SKyle Evans       case VFALSE: info = boolF(fs); break;
10110495ed39SKyle Evans       case VNIL: info = nilK(fs); break;
10120495ed39SKyle Evans       case VKINT: info = luaK_intK(fs, e->u.ival); break;
10130495ed39SKyle Evans       case VKFLT: info = luaK_numberK(fs, e->u.nval); break;
10140495ed39SKyle Evans       case VKSTR: info = stringK(fs, e->u.strval); break;
10150495ed39SKyle Evans       case VK: info = e->u.info; break;
10160495ed39SKyle Evans       default: return 0;  /* not a constant */
10170495ed39SKyle Evans     }
10180495ed39SKyle Evans     if (info <= MAXINDEXRK) {  /* does constant fit in 'argC'? */
10190495ed39SKyle Evans       e->k = VK;  /* make expression a 'K' expression */
10200495ed39SKyle Evans       e->u.info = info;
10210495ed39SKyle Evans       return 1;
10220495ed39SKyle Evans     }
10230495ed39SKyle Evans   }
10240495ed39SKyle Evans   /* else, expression doesn't fit; leave it unchanged */
10250495ed39SKyle Evans   return 0;
10260495ed39SKyle Evans }
10270495ed39SKyle Evans 
10280495ed39SKyle Evans 
10290495ed39SKyle Evans /*
10308e3e3a7aSWarner Losh ** Ensures final expression result is in a valid R/K index
10318e3e3a7aSWarner Losh ** (that is, it is either in a register or in 'k' with an index
10328e3e3a7aSWarner Losh ** in the range of R/K indices).
10330495ed39SKyle Evans ** Returns 1 iff expression is K.
10348e3e3a7aSWarner Losh */
luaK_exp2RK(FuncState * fs,expdesc * e)10358e3e3a7aSWarner Losh int luaK_exp2RK (FuncState *fs, expdesc *e) {
10360495ed39SKyle Evans   if (luaK_exp2K(fs, e))
10370495ed39SKyle Evans     return 1;
10380495ed39SKyle Evans   else {  /* not a constant in the right range: put it in a register */
10390495ed39SKyle Evans     luaK_exp2anyreg(fs, e);
10400495ed39SKyle Evans     return 0;
10418e3e3a7aSWarner Losh   }
10420495ed39SKyle Evans }
10430495ed39SKyle Evans 
10440495ed39SKyle Evans 
codeABRK(FuncState * fs,OpCode o,int a,int b,expdesc * ec)10450495ed39SKyle Evans static void codeABRK (FuncState *fs, OpCode o, int a, int b,
10460495ed39SKyle Evans                       expdesc *ec) {
10470495ed39SKyle Evans   int k = luaK_exp2RK(fs, ec);
10480495ed39SKyle Evans   luaK_codeABCk(fs, o, a, b, ec->u.info, k);
10498e3e3a7aSWarner Losh }
10508e3e3a7aSWarner Losh 
10518e3e3a7aSWarner Losh 
10528e3e3a7aSWarner Losh /*
10538e3e3a7aSWarner Losh ** Generate code to store result of expression 'ex' into variable 'var'.
10548e3e3a7aSWarner Losh */
luaK_storevar(FuncState * fs,expdesc * var,expdesc * ex)10558e3e3a7aSWarner Losh void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
10568e3e3a7aSWarner Losh   switch (var->k) {
10578e3e3a7aSWarner Losh     case VLOCAL: {
10588e3e3a7aSWarner Losh       freeexp(fs, ex);
10598c784bb8SWarner Losh       exp2reg(fs, ex, var->u.var.ridx);  /* compute 'ex' into proper place */
10608e3e3a7aSWarner Losh       return;
10618e3e3a7aSWarner Losh     }
10628e3e3a7aSWarner Losh     case VUPVAL: {
10638e3e3a7aSWarner Losh       int e = luaK_exp2anyreg(fs, ex);
10648e3e3a7aSWarner Losh       luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
10658e3e3a7aSWarner Losh       break;
10668e3e3a7aSWarner Losh     }
10670495ed39SKyle Evans     case VINDEXUP: {
10680495ed39SKyle Evans       codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex);
10690495ed39SKyle Evans       break;
10700495ed39SKyle Evans     }
10710495ed39SKyle Evans     case VINDEXI: {
10720495ed39SKyle Evans       codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex);
10730495ed39SKyle Evans       break;
10740495ed39SKyle Evans     }
10750495ed39SKyle Evans     case VINDEXSTR: {
10760495ed39SKyle Evans       codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex);
10770495ed39SKyle Evans       break;
10780495ed39SKyle Evans     }
10798e3e3a7aSWarner Losh     case VINDEXED: {
10800495ed39SKyle Evans       codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex);
10818e3e3a7aSWarner Losh       break;
10828e3e3a7aSWarner Losh     }
10838e3e3a7aSWarner Losh     default: lua_assert(0);  /* invalid var kind to store */
10848e3e3a7aSWarner Losh   }
10858e3e3a7aSWarner Losh   freeexp(fs, ex);
10868e3e3a7aSWarner Losh }
10878e3e3a7aSWarner Losh 
10888e3e3a7aSWarner Losh 
10898e3e3a7aSWarner Losh /*
10908e3e3a7aSWarner Losh ** Emit SELF instruction (convert expression 'e' into 'e:key(e,').
10918e3e3a7aSWarner Losh */
luaK_self(FuncState * fs,expdesc * e,expdesc * key)10928e3e3a7aSWarner Losh void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
10938e3e3a7aSWarner Losh   int ereg;
10948e3e3a7aSWarner Losh   luaK_exp2anyreg(fs, e);
10958e3e3a7aSWarner Losh   ereg = e->u.info;  /* register where 'e' was placed */
10968e3e3a7aSWarner Losh   freeexp(fs, e);
10978e3e3a7aSWarner Losh   e->u.info = fs->freereg;  /* base register for op_self */
10988e3e3a7aSWarner Losh   e->k = VNONRELOC;  /* self expression has a fixed register */
10998e3e3a7aSWarner Losh   luaK_reserveregs(fs, 2);  /* function and 'self' produced by op_self */
11000495ed39SKyle Evans   codeABRK(fs, OP_SELF, e->u.info, ereg, key);
11018e3e3a7aSWarner Losh   freeexp(fs, key);
11028e3e3a7aSWarner Losh }
11038e3e3a7aSWarner Losh 
11048e3e3a7aSWarner Losh 
11058e3e3a7aSWarner Losh /*
11068e3e3a7aSWarner Losh ** Negate condition 'e' (where 'e' is a comparison).
11078e3e3a7aSWarner Losh */
negatecondition(FuncState * fs,expdesc * e)11088e3e3a7aSWarner Losh static void negatecondition (FuncState *fs, expdesc *e) {
11098e3e3a7aSWarner Losh   Instruction *pc = getjumpcontrol(fs, e->u.info);
11108e3e3a7aSWarner Losh   lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&
11118e3e3a7aSWarner Losh                                            GET_OPCODE(*pc) != OP_TEST);
11120495ed39SKyle Evans   SETARG_k(*pc, (GETARG_k(*pc) ^ 1));
11138e3e3a7aSWarner Losh }
11148e3e3a7aSWarner Losh 
11158e3e3a7aSWarner Losh 
11168e3e3a7aSWarner Losh /*
11178e3e3a7aSWarner Losh ** Emit instruction to jump if 'e' is 'cond' (that is, if 'cond'
11188e3e3a7aSWarner Losh ** is true, code will jump if 'e' is true.) Return jump position.
11198e3e3a7aSWarner Losh ** Optimize when 'e' is 'not' something, inverting the condition
11208e3e3a7aSWarner Losh ** and removing the 'not'.
11218e3e3a7aSWarner Losh */
jumponcond(FuncState * fs,expdesc * e,int cond)11228e3e3a7aSWarner Losh static int jumponcond (FuncState *fs, expdesc *e, int cond) {
11230495ed39SKyle Evans   if (e->k == VRELOC) {
11248e3e3a7aSWarner Losh     Instruction ie = getinstruction(fs, e);
11258e3e3a7aSWarner Losh     if (GET_OPCODE(ie) == OP_NOT) {
11260495ed39SKyle Evans       removelastinstruction(fs);  /* remove previous OP_NOT */
11270495ed39SKyle Evans       return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond);
11288e3e3a7aSWarner Losh     }
11298e3e3a7aSWarner Losh     /* else go through */
11308e3e3a7aSWarner Losh   }
11318e3e3a7aSWarner Losh   discharge2anyreg(fs, e);
11328e3e3a7aSWarner Losh   freeexp(fs, e);
11330495ed39SKyle Evans   return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond);
11348e3e3a7aSWarner Losh }
11358e3e3a7aSWarner Losh 
11368e3e3a7aSWarner Losh 
11378e3e3a7aSWarner Losh /*
11388e3e3a7aSWarner Losh ** Emit code to go through if 'e' is true, jump otherwise.
11398e3e3a7aSWarner Losh */
luaK_goiftrue(FuncState * fs,expdesc * e)11408e3e3a7aSWarner Losh void luaK_goiftrue (FuncState *fs, expdesc *e) {
11418e3e3a7aSWarner Losh   int pc;  /* pc of new jump */
11428e3e3a7aSWarner Losh   luaK_dischargevars(fs, e);
11438e3e3a7aSWarner Losh   switch (e->k) {
11448e3e3a7aSWarner Losh     case VJMP: {  /* condition? */
11458e3e3a7aSWarner Losh       negatecondition(fs, e);  /* jump when it is false */
11468e3e3a7aSWarner Losh       pc = e->u.info;  /* save jump position */
11478e3e3a7aSWarner Losh       break;
11488e3e3a7aSWarner Losh     }
11490495ed39SKyle Evans     case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
11508e3e3a7aSWarner Losh       pc = NO_JUMP;  /* always true; do nothing */
11518e3e3a7aSWarner Losh       break;
11528e3e3a7aSWarner Losh     }
11538e3e3a7aSWarner Losh     default: {
11548e3e3a7aSWarner Losh       pc = jumponcond(fs, e, 0);  /* jump when false */
11558e3e3a7aSWarner Losh       break;
11568e3e3a7aSWarner Losh     }
11578e3e3a7aSWarner Losh   }
11588e3e3a7aSWarner Losh   luaK_concat(fs, &e->f, pc);  /* insert new jump in false list */
11598e3e3a7aSWarner Losh   luaK_patchtohere(fs, e->t);  /* true list jumps to here (to go through) */
11608e3e3a7aSWarner Losh   e->t = NO_JUMP;
11618e3e3a7aSWarner Losh }
11628e3e3a7aSWarner Losh 
11638e3e3a7aSWarner Losh 
11648e3e3a7aSWarner Losh /*
11658e3e3a7aSWarner Losh ** Emit code to go through if 'e' is false, jump otherwise.
11668e3e3a7aSWarner Losh */
luaK_goiffalse(FuncState * fs,expdesc * e)11678e3e3a7aSWarner Losh void luaK_goiffalse (FuncState *fs, expdesc *e) {
11688e3e3a7aSWarner Losh   int pc;  /* pc of new jump */
11698e3e3a7aSWarner Losh   luaK_dischargevars(fs, e);
11708e3e3a7aSWarner Losh   switch (e->k) {
11718e3e3a7aSWarner Losh     case VJMP: {
11728e3e3a7aSWarner Losh       pc = e->u.info;  /* already jump if true */
11738e3e3a7aSWarner Losh       break;
11748e3e3a7aSWarner Losh     }
11758e3e3a7aSWarner Losh     case VNIL: case VFALSE: {
11768e3e3a7aSWarner Losh       pc = NO_JUMP;  /* always false; do nothing */
11778e3e3a7aSWarner Losh       break;
11788e3e3a7aSWarner Losh     }
11798e3e3a7aSWarner Losh     default: {
11808e3e3a7aSWarner Losh       pc = jumponcond(fs, e, 1);  /* jump if true */
11818e3e3a7aSWarner Losh       break;
11828e3e3a7aSWarner Losh     }
11838e3e3a7aSWarner Losh   }
11848e3e3a7aSWarner Losh   luaK_concat(fs, &e->t, pc);  /* insert new jump in 't' list */
11858e3e3a7aSWarner Losh   luaK_patchtohere(fs, e->f);  /* false list jumps to here (to go through) */
11868e3e3a7aSWarner Losh   e->f = NO_JUMP;
11878e3e3a7aSWarner Losh }
11888e3e3a7aSWarner Losh 
11898e3e3a7aSWarner Losh 
11908e3e3a7aSWarner Losh /*
11918e3e3a7aSWarner Losh ** Code 'not e', doing constant folding.
11928e3e3a7aSWarner Losh */
codenot(FuncState * fs,expdesc * e)11938e3e3a7aSWarner Losh static void codenot (FuncState *fs, expdesc *e) {
11948e3e3a7aSWarner Losh   switch (e->k) {
11958e3e3a7aSWarner Losh     case VNIL: case VFALSE: {
11968e3e3a7aSWarner Losh       e->k = VTRUE;  /* true == not nil == not false */
11978e3e3a7aSWarner Losh       break;
11988e3e3a7aSWarner Losh     }
11990495ed39SKyle Evans     case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: {
12008e3e3a7aSWarner Losh       e->k = VFALSE;  /* false == not "x" == not 0.5 == not 1 == not true */
12018e3e3a7aSWarner Losh       break;
12028e3e3a7aSWarner Losh     }
12038e3e3a7aSWarner Losh     case VJMP: {
12048e3e3a7aSWarner Losh       negatecondition(fs, e);
12058e3e3a7aSWarner Losh       break;
12068e3e3a7aSWarner Losh     }
12070495ed39SKyle Evans     case VRELOC:
12088e3e3a7aSWarner Losh     case VNONRELOC: {
12098e3e3a7aSWarner Losh       discharge2anyreg(fs, e);
12108e3e3a7aSWarner Losh       freeexp(fs, e);
12118e3e3a7aSWarner Losh       e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0);
12120495ed39SKyle Evans       e->k = VRELOC;
12138e3e3a7aSWarner Losh       break;
12148e3e3a7aSWarner Losh     }
12158e3e3a7aSWarner Losh     default: lua_assert(0);  /* cannot happen */
12168e3e3a7aSWarner Losh   }
12178e3e3a7aSWarner Losh   /* interchange true and false lists */
12188e3e3a7aSWarner Losh   { int temp = e->f; e->f = e->t; e->t = temp; }
12198e3e3a7aSWarner Losh   removevalues(fs, e->f);  /* values are useless when negated */
12208e3e3a7aSWarner Losh   removevalues(fs, e->t);
12218e3e3a7aSWarner Losh }
12228e3e3a7aSWarner Losh 
12238e3e3a7aSWarner Losh 
12248e3e3a7aSWarner Losh /*
12250495ed39SKyle Evans ** Check whether expression 'e' is a small literal string
12260495ed39SKyle Evans */
isKstr(FuncState * fs,expdesc * e)12270495ed39SKyle Evans static int isKstr (FuncState *fs, expdesc *e) {
12280495ed39SKyle Evans   return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
12290495ed39SKyle Evans           ttisshrstring(&fs->f->k[e->u.info]));
12300495ed39SKyle Evans }
12310495ed39SKyle Evans 
12320495ed39SKyle Evans /*
12330495ed39SKyle Evans ** Check whether expression 'e' is a literal integer.
12340495ed39SKyle Evans */
luaK_isKint(expdesc * e)12350495ed39SKyle Evans int luaK_isKint (expdesc *e) {
12360495ed39SKyle Evans   return (e->k == VKINT && !hasjumps(e));
12370495ed39SKyle Evans }
12380495ed39SKyle Evans 
12390495ed39SKyle Evans 
12400495ed39SKyle Evans /*
12410495ed39SKyle Evans ** Check whether expression 'e' is a literal integer in
12420495ed39SKyle Evans ** proper range to fit in register C
12430495ed39SKyle Evans */
isCint(expdesc * e)12440495ed39SKyle Evans static int isCint (expdesc *e) {
12450495ed39SKyle Evans   return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
12460495ed39SKyle Evans }
12470495ed39SKyle Evans 
12480495ed39SKyle Evans 
12490495ed39SKyle Evans /*
12500495ed39SKyle Evans ** Check whether expression 'e' is a literal integer in
12510495ed39SKyle Evans ** proper range to fit in register sC
12520495ed39SKyle Evans */
isSCint(expdesc * e)12530495ed39SKyle Evans static int isSCint (expdesc *e) {
12540495ed39SKyle Evans   return luaK_isKint(e) && fitsC(e->u.ival);
12550495ed39SKyle Evans }
12560495ed39SKyle Evans 
12570495ed39SKyle Evans 
12580495ed39SKyle Evans /*
12590495ed39SKyle Evans ** Check whether expression 'e' is a literal integer or float in
12600495ed39SKyle Evans ** proper range to fit in a register (sB or sC).
12610495ed39SKyle Evans */
isSCnumber(expdesc * e,int * pi,int * isfloat)12620495ed39SKyle Evans static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
12630495ed39SKyle Evans   lua_Integer i;
12640495ed39SKyle Evans   if (e->k == VKINT)
12650495ed39SKyle Evans     i = e->u.ival;
12660495ed39SKyle Evans   else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
12670495ed39SKyle Evans     *isfloat = 1;
12680495ed39SKyle Evans   else
12690495ed39SKyle Evans     return 0;  /* not a number */
12700495ed39SKyle Evans   if (!hasjumps(e) && fitsC(i)) {
12710495ed39SKyle Evans     *pi = int2sC(cast_int(i));
12720495ed39SKyle Evans     return 1;
12730495ed39SKyle Evans   }
12740495ed39SKyle Evans   else
12750495ed39SKyle Evans     return 0;
12760495ed39SKyle Evans }
12770495ed39SKyle Evans 
12780495ed39SKyle Evans 
12790495ed39SKyle Evans /*
12808e3e3a7aSWarner Losh ** Create expression 't[k]'. 't' must have its final result already in a
12810495ed39SKyle Evans ** register or upvalue. Upvalues can only be indexed by literal strings.
12820495ed39SKyle Evans ** Keys can be literal strings in the constant table or arbitrary
12830495ed39SKyle Evans ** values in registers.
12848e3e3a7aSWarner Losh */
luaK_indexed(FuncState * fs,expdesc * t,expdesc * k)12858e3e3a7aSWarner Losh void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
12860495ed39SKyle Evans   if (k->k == VKSTR)
12870495ed39SKyle Evans     str2K(fs, k);
12880495ed39SKyle Evans   lua_assert(!hasjumps(t) &&
12890495ed39SKyle Evans              (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL));
12900495ed39SKyle Evans   if (t->k == VUPVAL && !isKstr(fs, k))  /* upvalue indexed by non 'Kstr'? */
12910495ed39SKyle Evans     luaK_exp2anyreg(fs, t);  /* put it in a register */
12920495ed39SKyle Evans   if (t->k == VUPVAL) {
12930495ed39SKyle Evans     t->u.ind.t = t->u.info;  /* upvalue index */
12940495ed39SKyle Evans     t->u.ind.idx = k->u.info;  /* literal string */
12950495ed39SKyle Evans     t->k = VINDEXUP;
12960495ed39SKyle Evans   }
12970495ed39SKyle Evans   else {
12980495ed39SKyle Evans     /* register index of the table */
12998c784bb8SWarner Losh     t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
13000495ed39SKyle Evans     if (isKstr(fs, k)) {
13010495ed39SKyle Evans       t->u.ind.idx = k->u.info;  /* literal string */
13020495ed39SKyle Evans       t->k = VINDEXSTR;
13030495ed39SKyle Evans     }
13040495ed39SKyle Evans     else if (isCint(k)) {
13050495ed39SKyle Evans       t->u.ind.idx = cast_int(k->u.ival);  /* int. constant in proper range */
13060495ed39SKyle Evans       t->k = VINDEXI;
13070495ed39SKyle Evans     }
13080495ed39SKyle Evans     else {
13090495ed39SKyle Evans       t->u.ind.idx = luaK_exp2anyreg(fs, k);  /* register */
13108e3e3a7aSWarner Losh       t->k = VINDEXED;
13118e3e3a7aSWarner Losh     }
13120495ed39SKyle Evans   }
13130495ed39SKyle Evans }
13148e3e3a7aSWarner Losh 
13158e3e3a7aSWarner Losh 
13168e3e3a7aSWarner Losh /*
13178e3e3a7aSWarner Losh ** Return false if folding can raise an error.
13188e3e3a7aSWarner Losh ** Bitwise operations need operands convertible to integers; division
13198e3e3a7aSWarner Losh ** operations cannot have 0 as divisor.
13208e3e3a7aSWarner Losh */
validop(int op,TValue * v1,TValue * v2)13218e3e3a7aSWarner Losh static int validop (int op, TValue *v1, TValue *v2) {
13228e3e3a7aSWarner Losh   switch (op) {
13238e3e3a7aSWarner Losh     case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
13248e3e3a7aSWarner Losh     case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: {  /* conversion errors */
13258e3e3a7aSWarner Losh       lua_Integer i;
13268c784bb8SWarner Losh       return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
13278c784bb8SWarner Losh               luaV_tointegerns(v2, &i, LUA_FLOORN2I));
13288e3e3a7aSWarner Losh     }
13298e3e3a7aSWarner Losh     case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD:  /* division by 0 */
13308e3e3a7aSWarner Losh       return (nvalue(v2) != 0);
13318e3e3a7aSWarner Losh     default: return 1;  /* everything else is valid */
13328e3e3a7aSWarner Losh   }
13338e3e3a7aSWarner Losh }
13348e3e3a7aSWarner Losh 
13358e3e3a7aSWarner Losh 
13368e3e3a7aSWarner Losh /*
13378e3e3a7aSWarner Losh ** Try to "constant-fold" an operation; return 1 iff successful.
13388e3e3a7aSWarner Losh ** (In this case, 'e1' has the final result.)
13398e3e3a7aSWarner Losh */
constfolding(FuncState * fs,int op,expdesc * e1,const expdesc * e2)13408e3e3a7aSWarner Losh static int constfolding (FuncState *fs, int op, expdesc *e1,
13418e3e3a7aSWarner Losh                                         const expdesc *e2) {
13428e3e3a7aSWarner Losh   TValue v1, v2, res;
13438e3e3a7aSWarner Losh   if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
13448e3e3a7aSWarner Losh     return 0;  /* non-numeric operands or not safe to fold */
13450495ed39SKyle Evans   luaO_rawarith(fs->ls->L, op, &v1, &v2, &res);  /* does operation */
13468e3e3a7aSWarner Losh   if (ttisinteger(&res)) {
13478e3e3a7aSWarner Losh     e1->k = VKINT;
13488e3e3a7aSWarner Losh     e1->u.ival = ivalue(&res);
13498e3e3a7aSWarner Losh   }
13508e3e3a7aSWarner Losh   else {  /* folds neither NaN nor 0.0 (to avoid problems with -0.0) */
13518e3e3a7aSWarner Losh     lua_Number n = fltvalue(&res);
13528e3e3a7aSWarner Losh     if (luai_numisnan(n) || n == 0)
13538e3e3a7aSWarner Losh       return 0;
13548e3e3a7aSWarner Losh     e1->k = VKFLT;
13558e3e3a7aSWarner Losh     e1->u.nval = n;
13568e3e3a7aSWarner Losh   }
13578e3e3a7aSWarner Losh   return 1;
13588e3e3a7aSWarner Losh }
13598e3e3a7aSWarner Losh 
13608e3e3a7aSWarner Losh 
13618e3e3a7aSWarner Losh /*
1362*a9490b81SWarner Losh ** Convert a BinOpr to an OpCode  (ORDER OPR - ORDER OP)
1363*a9490b81SWarner Losh */
binopr2op(BinOpr opr,BinOpr baser,OpCode base)1364*a9490b81SWarner Losh l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1365*a9490b81SWarner Losh   lua_assert(baser <= opr &&
1366*a9490b81SWarner Losh             ((baser == OPR_ADD && opr <= OPR_SHR) ||
1367*a9490b81SWarner Losh              (baser == OPR_LT && opr <= OPR_LE)));
1368*a9490b81SWarner Losh   return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1369*a9490b81SWarner Losh }
1370*a9490b81SWarner Losh 
1371*a9490b81SWarner Losh 
1372*a9490b81SWarner Losh /*
1373*a9490b81SWarner Losh ** Convert a UnOpr to an OpCode  (ORDER OPR - ORDER OP)
1374*a9490b81SWarner Losh */
unopr2op(UnOpr opr)1375*a9490b81SWarner Losh l_sinline OpCode unopr2op (UnOpr opr) {
1376*a9490b81SWarner Losh   return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1377*a9490b81SWarner Losh                                        cast_int(OP_UNM));
1378*a9490b81SWarner Losh }
1379*a9490b81SWarner Losh 
1380*a9490b81SWarner Losh 
1381*a9490b81SWarner Losh /*
1382*a9490b81SWarner Losh ** Convert a BinOpr to a tag method  (ORDER OPR - ORDER TM)
1383*a9490b81SWarner Losh */
binopr2TM(BinOpr opr)1384*a9490b81SWarner Losh l_sinline TMS binopr2TM (BinOpr opr) {
1385*a9490b81SWarner Losh   lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1386*a9490b81SWarner Losh   return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1387*a9490b81SWarner Losh }
1388*a9490b81SWarner Losh 
1389*a9490b81SWarner Losh 
1390*a9490b81SWarner Losh /*
13918e3e3a7aSWarner Losh ** Emit code for unary expressions that "produce values"
13928e3e3a7aSWarner Losh ** (everything but 'not').
13938e3e3a7aSWarner Losh ** Expression to produce final result will be encoded in 'e'.
13948e3e3a7aSWarner Losh */
codeunexpval(FuncState * fs,OpCode op,expdesc * e,int line)13958e3e3a7aSWarner Losh static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
13968e3e3a7aSWarner Losh   int r = luaK_exp2anyreg(fs, e);  /* opcodes operate only on registers */
13978e3e3a7aSWarner Losh   freeexp(fs, e);
13988e3e3a7aSWarner Losh   e->u.info = luaK_codeABC(fs, op, 0, r, 0);  /* generate opcode */
13990495ed39SKyle Evans   e->k = VRELOC;  /* all those operations are relocatable */
14008e3e3a7aSWarner Losh   luaK_fixline(fs, line);
14018e3e3a7aSWarner Losh }
14028e3e3a7aSWarner Losh 
14038e3e3a7aSWarner Losh 
14048e3e3a7aSWarner Losh /*
14058e3e3a7aSWarner Losh ** Emit code for binary expressions that "produce values"
14068e3e3a7aSWarner Losh ** (everything but logical operators 'and'/'or' and comparison
14078e3e3a7aSWarner Losh ** operators).
14088e3e3a7aSWarner Losh ** Expression to produce final result will be encoded in 'e1'.
14098e3e3a7aSWarner Losh */
finishbinexpval(FuncState * fs,expdesc * e1,expdesc * e2,OpCode op,int v2,int flip,int line,OpCode mmop,TMS event)14100495ed39SKyle Evans static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
14110495ed39SKyle Evans                              OpCode op, int v2, int flip, int line,
14120495ed39SKyle Evans                              OpCode mmop, TMS event) {
14130495ed39SKyle Evans   int v1 = luaK_exp2anyreg(fs, e1);
14140495ed39SKyle Evans   int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0);
14158e3e3a7aSWarner Losh   freeexps(fs, e1, e2);
14160495ed39SKyle Evans   e1->u.info = pc;
14170495ed39SKyle Evans   e1->k = VRELOC;  /* all those operations are relocatable */
14180495ed39SKyle Evans   luaK_fixline(fs, line);
14190495ed39SKyle Evans   luaK_codeABCk(fs, mmop, v1, v2, event, flip);  /* to call metamethod */
14208e3e3a7aSWarner Losh   luaK_fixline(fs, line);
14218e3e3a7aSWarner Losh }
14228e3e3a7aSWarner Losh 
14238e3e3a7aSWarner Losh 
14248e3e3a7aSWarner Losh /*
14250495ed39SKyle Evans ** Emit code for binary expressions that "produce values" over
14260495ed39SKyle Evans ** two registers.
14278e3e3a7aSWarner Losh */
codebinexpval(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int line)1428*a9490b81SWarner Losh static void codebinexpval (FuncState *fs, BinOpr opr,
14290495ed39SKyle Evans                            expdesc *e1, expdesc *e2, int line) {
1430*a9490b81SWarner Losh   OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1431*a9490b81SWarner Losh   int v2 = luaK_exp2anyreg(fs, e2);  /* make sure 'e2' is in a register */
1432*a9490b81SWarner Losh   /* 'e1' must be already in a register or it is a constant */
1433*a9490b81SWarner Losh   lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1434*a9490b81SWarner Losh              e1->k == VNONRELOC || e1->k == VRELOC);
14350495ed39SKyle Evans   lua_assert(OP_ADD <= op && op <= OP_SHR);
1436*a9490b81SWarner Losh   finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
14370495ed39SKyle Evans }
14380495ed39SKyle Evans 
14390495ed39SKyle Evans 
14400495ed39SKyle Evans /*
14410495ed39SKyle Evans ** Code binary operators with immediate operands.
14420495ed39SKyle Evans */
codebini(FuncState * fs,OpCode op,expdesc * e1,expdesc * e2,int flip,int line,TMS event)14430495ed39SKyle Evans static void codebini (FuncState *fs, OpCode op,
14440495ed39SKyle Evans                        expdesc *e1, expdesc *e2, int flip, int line,
14450495ed39SKyle Evans                        TMS event) {
14460495ed39SKyle Evans   int v2 = int2sC(cast_int(e2->u.ival));  /* immediate operand */
14470495ed39SKyle Evans   lua_assert(e2->k == VKINT);
14480495ed39SKyle Evans   finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event);
14490495ed39SKyle Evans }
14500495ed39SKyle Evans 
14510495ed39SKyle Evans 
1452*a9490b81SWarner Losh /*
1453*a9490b81SWarner Losh ** Code binary operators with K operand.
1454*a9490b81SWarner Losh */
codebinK(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int flip,int line)1455*a9490b81SWarner Losh static void codebinK (FuncState *fs, BinOpr opr,
1456*a9490b81SWarner Losh                       expdesc *e1, expdesc *e2, int flip, int line) {
1457*a9490b81SWarner Losh   TMS event = binopr2TM(opr);
1458*a9490b81SWarner Losh   int v2 = e2->u.info;  /* K index */
1459*a9490b81SWarner Losh   OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1460*a9490b81SWarner Losh   finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1461*a9490b81SWarner Losh }
1462*a9490b81SWarner Losh 
1463*a9490b81SWarner Losh 
14640495ed39SKyle Evans /* Try to code a binary operator negating its second operand.
14650495ed39SKyle Evans ** For the metamethod, 2nd operand must keep its original value.
14660495ed39SKyle Evans */
finishbinexpneg(FuncState * fs,expdesc * e1,expdesc * e2,OpCode op,int line,TMS event)14670495ed39SKyle Evans static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
14680495ed39SKyle Evans                              OpCode op, int line, TMS event) {
14690495ed39SKyle Evans   if (!luaK_isKint(e2))
14700495ed39SKyle Evans     return 0;  /* not an integer constant */
14710495ed39SKyle Evans   else {
14720495ed39SKyle Evans     lua_Integer i2 = e2->u.ival;
14730495ed39SKyle Evans     if (!(fitsC(i2) && fitsC(-i2)))
14740495ed39SKyle Evans       return 0;  /* not in the proper range */
14750495ed39SKyle Evans     else {  /* operating a small integer constant */
14760495ed39SKyle Evans       int v2 = cast_int(i2);
14770495ed39SKyle Evans       finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event);
14780495ed39SKyle Evans       /* correct metamethod argument */
14790495ed39SKyle Evans       SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2));
14800495ed39SKyle Evans       return 1;  /* successfully coded */
14810495ed39SKyle Evans     }
14820495ed39SKyle Evans   }
14830495ed39SKyle Evans }
14840495ed39SKyle Evans 
14850495ed39SKyle Evans 
swapexps(expdesc * e1,expdesc * e2)14860495ed39SKyle Evans static void swapexps (expdesc *e1, expdesc *e2) {
14870495ed39SKyle Evans   expdesc temp = *e1; *e1 = *e2; *e2 = temp;  /* swap 'e1' and 'e2' */
14880495ed39SKyle Evans }
14890495ed39SKyle Evans 
14900495ed39SKyle Evans 
14910495ed39SKyle Evans /*
1492*a9490b81SWarner Losh ** Code binary operators with no constant operand.
1493*a9490b81SWarner Losh */
codebinNoK(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int flip,int line)1494*a9490b81SWarner Losh static void codebinNoK (FuncState *fs, BinOpr opr,
1495*a9490b81SWarner Losh                         expdesc *e1, expdesc *e2, int flip, int line) {
1496*a9490b81SWarner Losh   if (flip)
1497*a9490b81SWarner Losh     swapexps(e1, e2);  /* back to original order */
1498*a9490b81SWarner Losh   codebinexpval(fs, opr, e1, e2, line);  /* use standard operators */
1499*a9490b81SWarner Losh }
1500*a9490b81SWarner Losh 
1501*a9490b81SWarner Losh 
1502*a9490b81SWarner Losh /*
15030495ed39SKyle Evans ** Code arithmetic operators ('+', '-', ...). If second operand is a
15040495ed39SKyle Evans ** constant in the proper range, use variant opcodes with K operands.
15050495ed39SKyle Evans */
codearith(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int flip,int line)15060495ed39SKyle Evans static void codearith (FuncState *fs, BinOpr opr,
15070495ed39SKyle Evans                        expdesc *e1, expdesc *e2, int flip, int line) {
1508*a9490b81SWarner Losh   if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2))  /* K operand? */
1509*a9490b81SWarner Losh     codebinK(fs, opr, e1, e2, flip, line);
1510*a9490b81SWarner Losh   else  /* 'e2' is neither an immediate nor a K operand */
1511*a9490b81SWarner Losh     codebinNoK(fs, opr, e1, e2, flip, line);
15120495ed39SKyle Evans }
15130495ed39SKyle Evans 
15140495ed39SKyle Evans 
15150495ed39SKyle Evans /*
15160495ed39SKyle Evans ** Code commutative operators ('+', '*'). If first operand is a
15170495ed39SKyle Evans ** numeric constant, change order of operands to try to use an
15180495ed39SKyle Evans ** immediate or K operator.
15190495ed39SKyle Evans */
codecommutative(FuncState * fs,BinOpr op,expdesc * e1,expdesc * e2,int line)15200495ed39SKyle Evans static void codecommutative (FuncState *fs, BinOpr op,
15210495ed39SKyle Evans                              expdesc *e1, expdesc *e2, int line) {
15220495ed39SKyle Evans   int flip = 0;
15230495ed39SKyle Evans   if (tonumeral(e1, NULL)) {  /* is first operand a numeric constant? */
15240495ed39SKyle Evans     swapexps(e1, e2);  /* change order */
15250495ed39SKyle Evans     flip = 1;
15260495ed39SKyle Evans   }
15270495ed39SKyle Evans   if (op == OPR_ADD && isSCint(e2))  /* immediate operand? */
1528*a9490b81SWarner Losh     codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
15290495ed39SKyle Evans   else
15300495ed39SKyle Evans     codearith(fs, op, e1, e2, flip, line);
15310495ed39SKyle Evans }
15320495ed39SKyle Evans 
15330495ed39SKyle Evans 
15340495ed39SKyle Evans /*
1535*a9490b81SWarner Losh ** Code bitwise operations; they are all commutative, so the function
15360495ed39SKyle Evans ** tries to put an integer constant as the 2nd operand (a K operand).
15370495ed39SKyle Evans */
codebitwise(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int line)15380495ed39SKyle Evans static void codebitwise (FuncState *fs, BinOpr opr,
15390495ed39SKyle Evans                          expdesc *e1, expdesc *e2, int line) {
15400495ed39SKyle Evans   int flip = 0;
1541*a9490b81SWarner Losh   if (e1->k == VKINT) {
15420495ed39SKyle Evans     swapexps(e1, e2);  /* 'e2' will be the constant operand */
15430495ed39SKyle Evans     flip = 1;
15440495ed39SKyle Evans   }
1545*a9490b81SWarner Losh   if (e2->k == VKINT && luaK_exp2K(fs, e2))  /* K operand? */
1546*a9490b81SWarner Losh     codebinK(fs, opr, e1, e2, flip, line);
1547*a9490b81SWarner Losh   else  /* no constants */
1548*a9490b81SWarner Losh     codebinNoK(fs, opr, e1, e2, flip, line);
15490495ed39SKyle Evans }
15500495ed39SKyle Evans 
15510495ed39SKyle Evans 
15520495ed39SKyle Evans /*
15530495ed39SKyle Evans ** Emit code for order comparisons. When using an immediate operand,
15540495ed39SKyle Evans ** 'isfloat' tells whether the original value was a float.
15550495ed39SKyle Evans */
codeorder(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2)1556*a9490b81SWarner Losh static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15570495ed39SKyle Evans   int r1, r2;
15580495ed39SKyle Evans   int im;
15590495ed39SKyle Evans   int isfloat = 0;
1560*a9490b81SWarner Losh   OpCode op;
15610495ed39SKyle Evans   if (isSCnumber(e2, &im, &isfloat)) {
15620495ed39SKyle Evans     /* use immediate operand */
15630495ed39SKyle Evans     r1 = luaK_exp2anyreg(fs, e1);
15640495ed39SKyle Evans     r2 = im;
1565*a9490b81SWarner Losh     op = binopr2op(opr, OPR_LT, OP_LTI);
15660495ed39SKyle Evans   }
15670495ed39SKyle Evans   else if (isSCnumber(e1, &im, &isfloat)) {
15680495ed39SKyle Evans     /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
15690495ed39SKyle Evans     r1 = luaK_exp2anyreg(fs, e2);
15700495ed39SKyle Evans     r2 = im;
1571*a9490b81SWarner Losh     op = binopr2op(opr, OPR_LT, OP_GTI);
15720495ed39SKyle Evans   }
15730495ed39SKyle Evans   else {  /* regular case, compare two registers */
15740495ed39SKyle Evans     r1 = luaK_exp2anyreg(fs, e1);
15750495ed39SKyle Evans     r2 = luaK_exp2anyreg(fs, e2);
1576*a9490b81SWarner Losh     op = binopr2op(opr, OPR_LT, OP_LT);
15770495ed39SKyle Evans   }
15788e3e3a7aSWarner Losh   freeexps(fs, e1, e2);
15790495ed39SKyle Evans   e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
15800495ed39SKyle Evans   e1->k = VJMP;
15818e3e3a7aSWarner Losh }
15820495ed39SKyle Evans 
15830495ed39SKyle Evans 
15840495ed39SKyle Evans /*
15850495ed39SKyle Evans ** Emit code for equality comparisons ('==', '~=').
15860495ed39SKyle Evans ** 'e1' was already put as RK by 'luaK_infix'.
15870495ed39SKyle Evans */
codeeq(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2)15880495ed39SKyle Evans static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15890495ed39SKyle Evans   int r1, r2;
15900495ed39SKyle Evans   int im;
15910495ed39SKyle Evans   int isfloat = 0;  /* not needed here, but kept for symmetry */
15920495ed39SKyle Evans   OpCode op;
15930495ed39SKyle Evans   if (e1->k != VNONRELOC) {
15940495ed39SKyle Evans     lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
15950495ed39SKyle Evans     swapexps(e1, e2);
15968e3e3a7aSWarner Losh   }
15970495ed39SKyle Evans   r1 = luaK_exp2anyreg(fs, e1);  /* 1st expression must be in register */
15980495ed39SKyle Evans   if (isSCnumber(e2, &im, &isfloat)) {
15990495ed39SKyle Evans     op = OP_EQI;
16000495ed39SKyle Evans     r2 = im;  /* immediate operand */
16018e3e3a7aSWarner Losh   }
1602*a9490b81SWarner Losh   else if (luaK_exp2RK(fs, e2)) {  /* 2nd expression is constant? */
16030495ed39SKyle Evans     op = OP_EQK;
16040495ed39SKyle Evans     r2 = e2->u.info;  /* constant index */
16058e3e3a7aSWarner Losh   }
16060495ed39SKyle Evans   else {
16070495ed39SKyle Evans     op = OP_EQ;  /* will compare two registers */
16080495ed39SKyle Evans     r2 = luaK_exp2anyreg(fs, e2);
16090495ed39SKyle Evans   }
16100495ed39SKyle Evans   freeexps(fs, e1, e2);
16110495ed39SKyle Evans   e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ));
16128e3e3a7aSWarner Losh   e1->k = VJMP;
16138e3e3a7aSWarner Losh }
16148e3e3a7aSWarner Losh 
16158e3e3a7aSWarner Losh 
16168e3e3a7aSWarner Losh /*
1617bf9580a1SKyle Evans ** Apply prefix operation 'op' to expression 'e'.
16188e3e3a7aSWarner Losh */
luaK_prefix(FuncState * fs,UnOpr opr,expdesc * e,int line)1619*a9490b81SWarner Losh void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
16208e3e3a7aSWarner Losh   static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
16210495ed39SKyle Evans   luaK_dischargevars(fs, e);
1622*a9490b81SWarner Losh   switch (opr) {
16238e3e3a7aSWarner Losh     case OPR_MINUS: case OPR_BNOT:  /* use 'ef' as fake 2nd operand */
1624*a9490b81SWarner Losh       if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
16258e3e3a7aSWarner Losh         break;
16260495ed39SKyle Evans       /* else */ /* FALLTHROUGH */
16278e3e3a7aSWarner Losh     case OPR_LEN:
1628*a9490b81SWarner Losh       codeunexpval(fs, unopr2op(opr), e, line);
16298e3e3a7aSWarner Losh       break;
16308e3e3a7aSWarner Losh     case OPR_NOT: codenot(fs, e); break;
16318e3e3a7aSWarner Losh     default: lua_assert(0);
16328e3e3a7aSWarner Losh   }
16338e3e3a7aSWarner Losh }
16348e3e3a7aSWarner Losh 
16358e3e3a7aSWarner Losh 
16368e3e3a7aSWarner Losh /*
16378e3e3a7aSWarner Losh ** Process 1st operand 'v' of binary operation 'op' before reading
16388e3e3a7aSWarner Losh ** 2nd operand.
16398e3e3a7aSWarner Losh */
luaK_infix(FuncState * fs,BinOpr op,expdesc * v)16408e3e3a7aSWarner Losh void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
16410495ed39SKyle Evans   luaK_dischargevars(fs, v);
16428e3e3a7aSWarner Losh   switch (op) {
16438e3e3a7aSWarner Losh     case OPR_AND: {
16448e3e3a7aSWarner Losh       luaK_goiftrue(fs, v);  /* go ahead only if 'v' is true */
16458e3e3a7aSWarner Losh       break;
16468e3e3a7aSWarner Losh     }
16478e3e3a7aSWarner Losh     case OPR_OR: {
16488e3e3a7aSWarner Losh       luaK_goiffalse(fs, v);  /* go ahead only if 'v' is false */
16498e3e3a7aSWarner Losh       break;
16508e3e3a7aSWarner Losh     }
16518e3e3a7aSWarner Losh     case OPR_CONCAT: {
16520495ed39SKyle Evans       luaK_exp2nextreg(fs, v);  /* operand must be on the stack */
16538e3e3a7aSWarner Losh       break;
16548e3e3a7aSWarner Losh     }
16558e3e3a7aSWarner Losh     case OPR_ADD: case OPR_SUB:
16568e3e3a7aSWarner Losh     case OPR_MUL: case OPR_DIV: case OPR_IDIV:
16578e3e3a7aSWarner Losh     case OPR_MOD: case OPR_POW:
16588e3e3a7aSWarner Losh     case OPR_BAND: case OPR_BOR: case OPR_BXOR:
16598e3e3a7aSWarner Losh     case OPR_SHL: case OPR_SHR: {
16608e3e3a7aSWarner Losh       if (!tonumeral(v, NULL))
16610495ed39SKyle Evans         luaK_exp2anyreg(fs, v);
1662*a9490b81SWarner Losh       /* else keep numeral, which may be folded or used as an immediate
1663*a9490b81SWarner Losh          operand */
16648e3e3a7aSWarner Losh       break;
16658e3e3a7aSWarner Losh     }
16660495ed39SKyle Evans     case OPR_EQ: case OPR_NE: {
16670495ed39SKyle Evans       if (!tonumeral(v, NULL))
16688e3e3a7aSWarner Losh         luaK_exp2RK(fs, v);
16690495ed39SKyle Evans       /* else keep numeral, which may be an immediate operand */
16708e3e3a7aSWarner Losh       break;
16718e3e3a7aSWarner Losh     }
16720495ed39SKyle Evans     case OPR_LT: case OPR_LE:
16730495ed39SKyle Evans     case OPR_GT: case OPR_GE: {
16740495ed39SKyle Evans       int dummy, dummy2;
16750495ed39SKyle Evans       if (!isSCnumber(v, &dummy, &dummy2))
16760495ed39SKyle Evans         luaK_exp2anyreg(fs, v);
16770495ed39SKyle Evans       /* else keep numeral, which may be an immediate operand */
16780495ed39SKyle Evans       break;
16790495ed39SKyle Evans     }
16800495ed39SKyle Evans     default: lua_assert(0);
16810495ed39SKyle Evans   }
16820495ed39SKyle Evans }
16830495ed39SKyle Evans 
16840495ed39SKyle Evans /*
16850495ed39SKyle Evans ** Create code for '(e1 .. e2)'.
16860495ed39SKyle Evans ** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
16870495ed39SKyle Evans ** because concatenation is right associative), merge both CONCATs.
16880495ed39SKyle Evans */
codeconcat(FuncState * fs,expdesc * e1,expdesc * e2,int line)16890495ed39SKyle Evans static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
16900495ed39SKyle Evans   Instruction *ie2 = previousinstruction(fs);
16910495ed39SKyle Evans   if (GET_OPCODE(*ie2) == OP_CONCAT) {  /* is 'e2' a concatenation? */
16920495ed39SKyle Evans     int n = GETARG_B(*ie2);  /* # of elements concatenated in 'e2' */
16930495ed39SKyle Evans     lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
16940495ed39SKyle Evans     freeexp(fs, e2);
16950495ed39SKyle Evans     SETARG_A(*ie2, e1->u.info);  /* correct first element ('e1') */
16960495ed39SKyle Evans     SETARG_B(*ie2, n + 1);  /* will concatenate one more element */
16970495ed39SKyle Evans   }
16980495ed39SKyle Evans   else {  /* 'e2' is not a concatenation */
16990495ed39SKyle Evans     luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0);  /* new concat opcode */
17000495ed39SKyle Evans     freeexp(fs, e2);
17010495ed39SKyle Evans     luaK_fixline(fs, line);
17028e3e3a7aSWarner Losh   }
17038e3e3a7aSWarner Losh }
17048e3e3a7aSWarner Losh 
17058e3e3a7aSWarner Losh 
17068e3e3a7aSWarner Losh /*
17078e3e3a7aSWarner Losh ** Finalize code for binary operation, after reading 2nd operand.
17088e3e3a7aSWarner Losh */
luaK_posfix(FuncState * fs,BinOpr opr,expdesc * e1,expdesc * e2,int line)17090495ed39SKyle Evans void luaK_posfix (FuncState *fs, BinOpr opr,
17108e3e3a7aSWarner Losh                   expdesc *e1, expdesc *e2, int line) {
17118e3e3a7aSWarner Losh   luaK_dischargevars(fs, e2);
17120495ed39SKyle Evans   if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
17130495ed39SKyle Evans     return;  /* done by folding */
17140495ed39SKyle Evans   switch (opr) {
17150495ed39SKyle Evans     case OPR_AND: {
17160495ed39SKyle Evans       lua_assert(e1->t == NO_JUMP);  /* list closed by 'luaK_infix' */
17178e3e3a7aSWarner Losh       luaK_concat(fs, &e2->f, e1->f);
17188e3e3a7aSWarner Losh       *e1 = *e2;
17198e3e3a7aSWarner Losh       break;
17208e3e3a7aSWarner Losh     }
17218e3e3a7aSWarner Losh     case OPR_OR: {
17220495ed39SKyle Evans       lua_assert(e1->f == NO_JUMP);  /* list closed by 'luaK_infix' */
17238e3e3a7aSWarner Losh       luaK_concat(fs, &e2->t, e1->t);
17248e3e3a7aSWarner Losh       *e1 = *e2;
17258e3e3a7aSWarner Losh       break;
17268e3e3a7aSWarner Losh     }
17270495ed39SKyle Evans     case OPR_CONCAT: {  /* e1 .. e2 */
17280495ed39SKyle Evans       luaK_exp2nextreg(fs, e2);
17290495ed39SKyle Evans       codeconcat(fs, e1, e2, line);
17308e3e3a7aSWarner Losh       break;
17318e3e3a7aSWarner Losh     }
17320495ed39SKyle Evans     case OPR_ADD: case OPR_MUL: {
17330495ed39SKyle Evans       codecommutative(fs, opr, e1, e2, line);
17348e3e3a7aSWarner Losh       break;
17358e3e3a7aSWarner Losh     }
17360495ed39SKyle Evans     case OPR_SUB: {
17370495ed39SKyle Evans       if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB))
17380495ed39SKyle Evans         break; /* coded as (r1 + -I) */
17390495ed39SKyle Evans       /* ELSE */
17400495ed39SKyle Evans     }  /* FALLTHROUGH */
17410495ed39SKyle Evans     case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: {
17420495ed39SKyle Evans       codearith(fs, opr, e1, e2, 0, line);
17430495ed39SKyle Evans       break;
17440495ed39SKyle Evans     }
17450495ed39SKyle Evans     case OPR_BAND: case OPR_BOR: case OPR_BXOR: {
17460495ed39SKyle Evans       codebitwise(fs, opr, e1, e2, line);
17470495ed39SKyle Evans       break;
17480495ed39SKyle Evans     }
17490495ed39SKyle Evans     case OPR_SHL: {
17500495ed39SKyle Evans       if (isSCint(e1)) {
17510495ed39SKyle Evans         swapexps(e1, e2);
17520495ed39SKyle Evans         codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL);  /* I << r2 */
17530495ed39SKyle Evans       }
17540495ed39SKyle Evans       else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) {
17550495ed39SKyle Evans         /* coded as (r1 >> -I) */;
17560495ed39SKyle Evans       }
17570495ed39SKyle Evans       else  /* regular case (two registers) */
1758*a9490b81SWarner Losh        codebinexpval(fs, opr, e1, e2, line);
17590495ed39SKyle Evans       break;
17600495ed39SKyle Evans     }
17610495ed39SKyle Evans     case OPR_SHR: {
17620495ed39SKyle Evans       if (isSCint(e2))
17630495ed39SKyle Evans         codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR);  /* r1 >> I */
17640495ed39SKyle Evans       else  /* regular case (two registers) */
1765*a9490b81SWarner Losh         codebinexpval(fs, opr, e1, e2, line);
17660495ed39SKyle Evans       break;
17670495ed39SKyle Evans     }
17680495ed39SKyle Evans     case OPR_EQ: case OPR_NE: {
17690495ed39SKyle Evans       codeeq(fs, opr, e1, e2);
17700495ed39SKyle Evans       break;
17710495ed39SKyle Evans     }
17720495ed39SKyle Evans     case OPR_GT: case OPR_GE: {
17730495ed39SKyle Evans       /* '(a > b)' <=> '(b < a)';  '(a >= b)' <=> '(b <= a)' */
17740495ed39SKyle Evans       swapexps(e1, e2);
1775*a9490b81SWarner Losh       opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1776*a9490b81SWarner Losh     }  /* FALLTHROUGH */
1777*a9490b81SWarner Losh     case OPR_LT: case OPR_LE: {
1778*a9490b81SWarner Losh       codeorder(fs, opr, e1, e2);
17798e3e3a7aSWarner Losh       break;
17808e3e3a7aSWarner Losh     }
17818e3e3a7aSWarner Losh     default: lua_assert(0);
17828e3e3a7aSWarner Losh   }
17838e3e3a7aSWarner Losh }
17848e3e3a7aSWarner Losh 
17858e3e3a7aSWarner Losh 
17868e3e3a7aSWarner Losh /*
17870495ed39SKyle Evans ** Change line information associated with current position, by removing
17880495ed39SKyle Evans ** previous info and adding it again with new line.
17898e3e3a7aSWarner Losh */
luaK_fixline(FuncState * fs,int line)17908e3e3a7aSWarner Losh void luaK_fixline (FuncState *fs, int line) {
17910495ed39SKyle Evans   removelastlineinfo(fs);
17920495ed39SKyle Evans   savelineinfo(fs, fs->f, line);
17930495ed39SKyle Evans }
17940495ed39SKyle Evans 
17950495ed39SKyle Evans 
luaK_settablesize(FuncState * fs,int pc,int ra,int asize,int hsize)17960495ed39SKyle Evans void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
17970495ed39SKyle Evans   Instruction *inst = &fs->f->code[pc];
17980495ed39SKyle Evans   int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0;  /* hash size */
17990495ed39SKyle Evans   int extra = asize / (MAXARG_C + 1);  /* higher bits of array size */
18000495ed39SKyle Evans   int rc = asize % (MAXARG_C + 1);  /* lower bits of array size */
18010495ed39SKyle Evans   int k = (extra > 0);  /* true iff needs extra argument */
18020495ed39SKyle Evans   *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k);
18030495ed39SKyle Evans   *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
18048e3e3a7aSWarner Losh }
18058e3e3a7aSWarner Losh 
18068e3e3a7aSWarner Losh 
18078e3e3a7aSWarner Losh /*
18088e3e3a7aSWarner Losh ** Emit a SETLIST instruction.
18098e3e3a7aSWarner Losh ** 'base' is register that keeps table;
18108e3e3a7aSWarner Losh ** 'nelems' is #table plus those to be stored now;
18118e3e3a7aSWarner Losh ** 'tostore' is number of values (in registers 'base + 1',...) to add to
18128e3e3a7aSWarner Losh ** table (or LUA_MULTRET to add up to stack top).
18138e3e3a7aSWarner Losh */
luaK_setlist(FuncState * fs,int base,int nelems,int tostore)18148e3e3a7aSWarner Losh void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
18158e3e3a7aSWarner Losh   lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH);
18160495ed39SKyle Evans   if (tostore == LUA_MULTRET)
18170495ed39SKyle Evans     tostore = 0;
18180495ed39SKyle Evans   if (nelems <= MAXARG_C)
18190495ed39SKyle Evans     luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems);
18200495ed39SKyle Evans   else {
18210495ed39SKyle Evans     int extra = nelems / (MAXARG_C + 1);
18220495ed39SKyle Evans     nelems %= (MAXARG_C + 1);
18230495ed39SKyle Evans     luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
18240495ed39SKyle Evans     codeextraarg(fs, extra);
18258e3e3a7aSWarner Losh   }
18268e3e3a7aSWarner Losh   fs->freereg = base + 1;  /* free registers with list values */
18278e3e3a7aSWarner Losh }
18288e3e3a7aSWarner Losh 
18290495ed39SKyle Evans 
18300495ed39SKyle Evans /*
18310495ed39SKyle Evans ** return the final target of a jump (skipping jumps to jumps)
18320495ed39SKyle Evans */
finaltarget(Instruction * code,int i)18330495ed39SKyle Evans static int finaltarget (Instruction *code, int i) {
18340495ed39SKyle Evans   int count;
18350495ed39SKyle Evans   for (count = 0; count < 100; count++) {  /* avoid infinite loops */
18360495ed39SKyle Evans     Instruction pc = code[i];
18370495ed39SKyle Evans     if (GET_OPCODE(pc) != OP_JMP)
18380495ed39SKyle Evans       break;
18390495ed39SKyle Evans      else
18400495ed39SKyle Evans        i += GETARG_sJ(pc) + 1;
18410495ed39SKyle Evans   }
18420495ed39SKyle Evans   return i;
18430495ed39SKyle Evans }
18440495ed39SKyle Evans 
18450495ed39SKyle Evans 
18460495ed39SKyle Evans /*
18470495ed39SKyle Evans ** Do a final pass over the code of a function, doing small peephole
18480495ed39SKyle Evans ** optimizations and adjustments.
18490495ed39SKyle Evans */
luaK_finish(FuncState * fs)18500495ed39SKyle Evans void luaK_finish (FuncState *fs) {
18510495ed39SKyle Evans   int i;
18520495ed39SKyle Evans   Proto *p = fs->f;
18530495ed39SKyle Evans   for (i = 0; i < fs->pc; i++) {
18540495ed39SKyle Evans     Instruction *pc = &p->code[i];
18550495ed39SKyle Evans     lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc));
18560495ed39SKyle Evans     switch (GET_OPCODE(*pc)) {
18570495ed39SKyle Evans       case OP_RETURN0: case OP_RETURN1: {
18580495ed39SKyle Evans         if (!(fs->needclose || p->is_vararg))
18590495ed39SKyle Evans           break;  /* no extra work */
18600495ed39SKyle Evans         /* else use OP_RETURN to do the extra work */
18610495ed39SKyle Evans         SET_OPCODE(*pc, OP_RETURN);
18620495ed39SKyle Evans       }  /* FALLTHROUGH */
18630495ed39SKyle Evans       case OP_RETURN: case OP_TAILCALL: {
18640495ed39SKyle Evans         if (fs->needclose)
18650495ed39SKyle Evans           SETARG_k(*pc, 1);  /* signal that it needs to close */
18660495ed39SKyle Evans         if (p->is_vararg)
18670495ed39SKyle Evans           SETARG_C(*pc, p->numparams + 1);  /* signal that it is vararg */
18680495ed39SKyle Evans         break;
18690495ed39SKyle Evans       }
18700495ed39SKyle Evans       case OP_JMP: {
18710495ed39SKyle Evans         int target = finaltarget(p->code, i);
18720495ed39SKyle Evans         fixjump(fs, i, target);
18730495ed39SKyle Evans         break;
18740495ed39SKyle Evans       }
18750495ed39SKyle Evans       default: break;
18760495ed39SKyle Evans     }
18770495ed39SKyle Evans   }
18780495ed39SKyle Evans }
1879