Lua TeamLua 5.1.3-rc1ea97e7918 years agoHistory
839 lines · 20.8 KB · C
RawDownload
/*** $Id: lcode.c,v 2.25.1.3 2007/12/28 15:32:23 roberto Exp $** Code generator for Lua** See Copyright Notice in lua.h*/  #include <stdlib.h> #define lcode_c#define LUA_CORE #include "lua.h" #include "lcode.h"#include "ldebug.h"#include "ldo.h"#include "lgc.h"#include "llex.h"#include "lmem.h"#include "lobject.h"#include "lopcodes.h"#include "lparser.h"#include "ltable.h"  #define hasjumps(e)	((e)->t != (e)->f)  static int isnumeral(expdesc *e) {  return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);}  void luaK_nil (FuncState *fs, int from, int n) {  Instruction *previous;  if (fs->pc > fs->lasttarget) {  /* no jumps to current position? */    if (fs->pc == 0) {  /* function start? */      if (from >= fs->nactvar)        return;  /* positions are already clean */    }    else {      previous = &fs->f->code[fs->pc-1];      if (GET_OPCODE(*previous) == OP_LOADNIL) {        int pfrom = GETARG_A(*previous);        int pto = GETARG_B(*previous);        if (pfrom <= from && from <= pto+1) {  /* can connect both? */          if (from+n-1 > pto)            SETARG_B(*previous, from+n-1);          return;        }      }    }  }  luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0);  /* else no optimization */}  int luaK_jump (FuncState *fs) {  int jpc = fs->jpc;  /* save list of jumps to here */  int j;  fs->jpc = NO_JUMP;  j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);  luaK_concat(fs, &j, jpc);  /* keep them on hold */  return j;}  void luaK_ret (FuncState *fs, int first, int nret) {  luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);}  static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {  luaK_codeABC(fs, op, A, B, C);  return luaK_jump(fs);}  static void fixjump (FuncState *fs, int pc, int dest) {  Instruction *jmp = &fs->f->code[pc];  int offset = dest-(pc+1);  lua_assert(dest != NO_JUMP);  if (abs(offset) > MAXARG_sBx)    luaX_syntaxerror(fs->ls, "control structure too long");  SETARG_sBx(*jmp, offset);}  /*** returns current `pc' and marks it as a jump target (to avoid wrong** optimizations with consecutive instructions not in the same basic block).*/int luaK_getlabel (FuncState *fs) {  fs->lasttarget = fs->pc;  return fs->pc;}  static int getjump (FuncState *fs, int pc) {  int offset = GETARG_sBx(fs->f->code[pc]);  if (offset == NO_JUMP)  /* point to itself represents end of list */    return NO_JUMP;  /* end of list */  else    return (pc+1)+offset;  /* turn offset into absolute position */}  static Instruction *getjumpcontrol (FuncState *fs, int pc) {  Instruction *pi = &fs->f->code[pc];  if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))    return pi-1;  else    return pi;}  /*** check whether list has any jump that do not produce a value** (or produce an inverted value)*/static int need_value (FuncState *fs, int list) {  for (; list != NO_JUMP; list = getjump(fs, list)) {    Instruction i = *getjumpcontrol(fs, list);    if (GET_OPCODE(i) != OP_TESTSET) return 1;  }  return 0;  /* not found */}  static int patchtestreg (FuncState *fs, int node, int reg) {  Instruction *i = getjumpcontrol(fs, node);  if (GET_OPCODE(*i) != OP_TESTSET)    return 0;  /* cannot patch other instructions */  if (reg != NO_REG && reg != GETARG_B(*i))    SETARG_A(*i, reg);  else  /* no register to put value or register already has the value */    *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));   return 1;}  static void removevalues (FuncState *fs, int list) {  for (; list != NO_JUMP; list = getjump(fs, list))      patchtestreg(fs, list, NO_REG);}  static void patchlistaux (FuncState *fs, int list, int vtarget, int reg,                          int dtarget) {  while (list != NO_JUMP) {    int next = getjump(fs, list);    if (patchtestreg(fs, list, reg))      fixjump(fs, list, vtarget);    else      fixjump(fs, list, dtarget);  /* jump to default target */    list = next;  }}  static void dischargejpc (FuncState *fs) {  patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);  fs->jpc = NO_JUMP;}  void luaK_patchlist (FuncState *fs, int list, int target) {  if (target == fs->pc)    luaK_patchtohere(fs, list);  else {    lua_assert(target < fs->pc);    patchlistaux(fs, list, target, NO_REG, target);  }}  void luaK_patchtohere (FuncState *fs, int list) {  luaK_getlabel(fs);  luaK_concat(fs, &fs->jpc, list);}  void luaK_concat (FuncState *fs, int *l1, int l2) {  if (l2 == NO_JUMP) return;  else if (*l1 == NO_JUMP)    *l1 = l2;  else {    int list = *l1;    int next;    while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */      list = next;    fixjump(fs, list, l2);  }}  void luaK_checkstack (FuncState *fs, int n) {  int newstack = fs->freereg + n;  if (newstack > fs->f->maxstacksize) {    if (newstack >= MAXSTACK)      luaX_syntaxerror(fs->ls, "function or expression too complex");    fs->f->maxstacksize = cast_byte(newstack);  }}  void luaK_reserveregs (FuncState *fs, int n) {  luaK_checkstack(fs, n);  fs->freereg += n;}  static void freereg (FuncState *fs, int reg) {  if (!ISK(reg) && reg >= fs->nactvar) {    fs->freereg--;    lua_assert(reg == fs->freereg);  }}  static void freeexp (FuncState *fs, expdesc *e) {  if (e->k == VNONRELOC)    freereg(fs, e->u.s.info);}  static int addk (FuncState *fs, TValue *k, TValue *v) {  lua_State *L = fs->L;  TValue *idx = luaH_set(L, fs->h, k);  Proto *f = fs->f;  int oldsize = f->sizek;  if (ttisnumber(idx)) {    lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));    return cast_int(nvalue(idx));  }  else {  /* constant not found; create a new entry */    setnvalue(idx, cast_num(fs->nk));    luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,                    MAXARG_Bx, "constant table overflow");    while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);    setobj(L, &f->k[fs->nk], v);    luaC_barrier(L, f, v);    return fs->nk++;  }}  int luaK_stringK (FuncState *fs, TString *s) {  TValue o;  setsvalue(fs->L, &o, s);  return addk(fs, &o, &o);}  int luaK_numberK (FuncState *fs, lua_Number r) {  TValue o;  setnvalue(&o, r);  return addk(fs, &o, &o);}  static int boolK (FuncState *fs, int b) {  TValue o;  setbvalue(&o, b);  return addk(fs, &o, &o);}  static int nilK (FuncState *fs) {  TValue k, v;  setnilvalue(&v);  /* cannot use nil as key; instead use table itself to represent nil */  sethvalue(fs->L, &k, fs->h);  return addk(fs, &k, &v);}  void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {  if (e->k == VCALL) {  /* expression is an open function call? */    SETARG_C(getcode(fs, e), nresults+1);  }  else if (e->k == VVARARG) {    SETARG_B(getcode(fs, e), nresults+1);    SETARG_A(getcode(fs, e), fs->freereg);    luaK_reserveregs(fs, 1);  }}  void luaK_setoneret (FuncState *fs, expdesc *e) {  if (e->k == VCALL) {  /* expression is an open function call? */    e->k = VNONRELOC;    e->u.s.info = GETARG_A(getcode(fs, e));  }  else if (e->k == VVARARG) {    SETARG_B(getcode(fs, e), 2);    e->k = VRELOCABLE;  /* can relocate its simple result */  }}  void luaK_dischargevars (FuncState *fs, expdesc *e) {  switch (e->k) {    case VLOCAL: {      e->k = VNONRELOC;      break;    }    case VUPVAL: {      e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);      e->k = VRELOCABLE;      break;    }    case VGLOBAL: {      e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);      e->k = VRELOCABLE;      break;    }    case VINDEXED: {      freereg(fs, e->u.s.aux);      freereg(fs, e->u.s.info);      e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);      e->k = VRELOCABLE;      break;    }    case VVARARG:    case VCALL: {      luaK_setoneret(fs, e);      break;    }    default: break;  /* there is one value available (somewhere) */  }}  static int code_label (FuncState *fs, int A, int b, int jump) {  luaK_getlabel(fs);  /* those instructions may be jump targets */  return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);}  static void discharge2reg (FuncState *fs, expdesc *e, int reg) {  luaK_dischargevars(fs, e);  switch (e->k) {    case VNIL: {      luaK_nil(fs, reg, 1);      break;    }    case VFALSE:  case VTRUE: {      luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);      break;    }    case VK: {      luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);      break;    }    case VKNUM: {      luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));      break;    }    case VRELOCABLE: {      Instruction *pc = &getcode(fs, e);      SETARG_A(*pc, reg);      break;    }    case VNONRELOC: {      if (reg != e->u.s.info)        luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);      break;    }    default: {      lua_assert(e->k == VVOID || e->k == VJMP);      return;  /* nothing to do... */    }  }  e->u.s.info = reg;  e->k = VNONRELOC;}  static void discharge2anyreg (FuncState *fs, expdesc *e) {  if (e->k != VNONRELOC) {    luaK_reserveregs(fs, 1);    discharge2reg(fs, e, fs->freereg-1);  }}  static void exp2reg (FuncState *fs, expdesc *e, int reg) {  discharge2reg(fs, e, reg);  if (e->k == VJMP)    luaK_concat(fs, &e->t, e->u.s.info);  /* put this jump in `t' list */  if (hasjumps(e)) {    int final;  /* position after whole expression */    int p_f = NO_JUMP;  /* position of an eventual LOAD false */    int p_t = NO_JUMP;  /* position of an eventual LOAD true */    if (need_value(fs, e->t) || need_value(fs, e->f)) {      int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);      p_f = code_label(fs, reg, 0, 1);      p_t = code_label(fs, reg, 1, 0);      luaK_patchtohere(fs, fj);    }    final = luaK_getlabel(fs);    patchlistaux(fs, e->f, final, reg, p_f);    patchlistaux(fs, e->t, final, reg, p_t);  }  e->f = e->t = NO_JUMP;  e->u.s.info = reg;  e->k = VNONRELOC;}  void luaK_exp2nextreg (FuncState *fs, expdesc *e) {  luaK_dischargevars(fs, e);  freeexp(fs, e);  luaK_reserveregs(fs, 1);  exp2reg(fs, e, fs->freereg - 1);}  int luaK_exp2anyreg (FuncState *fs, expdesc *e) {  luaK_dischargevars(fs, e);  if (e->k == VNONRELOC) {    if (!hasjumps(e)) return e->u.s.info;  /* exp is already in a register */    if (e->u.s.info >= fs->nactvar) {  /* reg. is not a local? */      exp2reg(fs, e, e->u.s.info);  /* put value on it */      return e->u.s.info;    }  }  luaK_exp2nextreg(fs, e);  /* default */  return e->u.s.info;}  void luaK_exp2val (FuncState *fs, expdesc *e) {  if (hasjumps(e))    luaK_exp2anyreg(fs, e);  else    luaK_dischargevars(fs, e);}  int luaK_exp2RK (FuncState *fs, expdesc *e) {  luaK_exp2val(fs, e);  switch (e->k) {    case VKNUM:    case VTRUE:    case VFALSE:    case VNIL: {      if (fs->nk <= MAXINDEXRK) {  /* constant fit in RK operand? */        e->u.s.info = (e->k == VNIL)  ? nilK(fs) :                      (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :                                        boolK(fs, (e->k == VTRUE));        e->k = VK;        return RKASK(e->u.s.info);      }      else break;    }    case VK: {      if (e->u.s.info <= MAXINDEXRK)  /* constant fit in argC? */        return RKASK(e->u.s.info);      else break;    }    default: break;  }  /* not a constant in the right range: put it in a register */  return luaK_exp2anyreg(fs, e);}  void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {  switch (var->k) {    case VLOCAL: {      freeexp(fs, ex);      exp2reg(fs, ex, var->u.s.info);      return;    }    case VUPVAL: {      int e = luaK_exp2anyreg(fs, ex);      luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);      break;    }    case VGLOBAL: {      int e = luaK_exp2anyreg(fs, ex);      luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);      break;    }    case VINDEXED: {      int e = luaK_exp2RK(fs, ex);      luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);      break;    }    default: {      lua_assert(0);  /* invalid var kind to store */      break;    }  }  freeexp(fs, ex);}

Lines 1–500 of 839.