1*088332b5SXin Li /* 2*088332b5SXin Li ** $Id: lparser.h $ 3*088332b5SXin Li ** Lua Parser 4*088332b5SXin Li ** See Copyright Notice in lua.h 5*088332b5SXin Li */ 6*088332b5SXin Li 7*088332b5SXin Li #ifndef lparser_h 8*088332b5SXin Li #define lparser_h 9*088332b5SXin Li 10*088332b5SXin Li #include "llimits.h" 11*088332b5SXin Li #include "lobject.h" 12*088332b5SXin Li #include "lzio.h" 13*088332b5SXin Li 14*088332b5SXin Li 15*088332b5SXin Li /* 16*088332b5SXin Li ** Expression and variable descriptor. 17*088332b5SXin Li ** Code generation for variables and expressions can be delayed to allow 18*088332b5SXin Li ** optimizations; An 'expdesc' structure describes a potentially-delayed 19*088332b5SXin Li ** variable/expression. It has a description of its "main" value plus a 20*088332b5SXin Li ** list of conditional jumps that can also produce its value (generated 21*088332b5SXin Li ** by short-circuit operators 'and'/'or'). 22*088332b5SXin Li */ 23*088332b5SXin Li 24*088332b5SXin Li /* kinds of variables/expressions */ 25*088332b5SXin Li typedef enum { 26*088332b5SXin Li VVOID, /* when 'expdesc' describes the last expression a list, 27*088332b5SXin Li this kind means an empty list (so, no expression) */ 28*088332b5SXin Li VNIL, /* constant nil */ 29*088332b5SXin Li VTRUE, /* constant true */ 30*088332b5SXin Li VFALSE, /* constant false */ 31*088332b5SXin Li VK, /* constant in 'k'; info = index of constant in 'k' */ 32*088332b5SXin Li VKFLT, /* floating constant; nval = numerical float value */ 33*088332b5SXin Li VKINT, /* integer constant; ival = numerical integer value */ 34*088332b5SXin Li VKSTR, /* string constant; strval = TString address; 35*088332b5SXin Li (string is fixed by the lexer) */ 36*088332b5SXin Li VNONRELOC, /* expression has its value in a fixed register; 37*088332b5SXin Li info = result register */ 38*088332b5SXin Li VLOCAL, /* local variable; var.sidx = stack index (local register); 39*088332b5SXin Li var.vidx = relative index in 'actvar.arr' */ 40*088332b5SXin Li VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 41*088332b5SXin Li VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ 42*088332b5SXin Li VINDEXED, /* indexed variable; 43*088332b5SXin Li ind.t = table register; 44*088332b5SXin Li ind.idx = key's R index */ 45*088332b5SXin Li VINDEXUP, /* indexed upvalue; 46*088332b5SXin Li ind.t = table upvalue; 47*088332b5SXin Li ind.idx = key's K index */ 48*088332b5SXin Li VINDEXI, /* indexed variable with constant integer; 49*088332b5SXin Li ind.t = table register; 50*088332b5SXin Li ind.idx = key's value */ 51*088332b5SXin Li VINDEXSTR, /* indexed variable with literal string; 52*088332b5SXin Li ind.t = table register; 53*088332b5SXin Li ind.idx = key's K index */ 54*088332b5SXin Li VJMP, /* expression is a test/comparison; 55*088332b5SXin Li info = pc of corresponding jump instruction */ 56*088332b5SXin Li VRELOC, /* expression can put result in any register; 57*088332b5SXin Li info = instruction pc */ 58*088332b5SXin Li VCALL, /* expression is a function call; info = instruction pc */ 59*088332b5SXin Li VVARARG /* vararg expression; info = instruction pc */ 60*088332b5SXin Li } expkind; 61*088332b5SXin Li 62*088332b5SXin Li 63*088332b5SXin Li #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) 64*088332b5SXin Li #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) 65*088332b5SXin Li 66*088332b5SXin Li 67*088332b5SXin Li typedef struct expdesc { 68*088332b5SXin Li expkind k; 69*088332b5SXin Li union { 70*088332b5SXin Li lua_Integer ival; /* for VKINT */ 71*088332b5SXin Li lua_Number nval; /* for VKFLT */ 72*088332b5SXin Li TString *strval; /* for VKSTR */ 73*088332b5SXin Li int info; /* for generic use */ 74*088332b5SXin Li struct { /* for indexed variables */ 75*088332b5SXin Li short idx; /* index (R or "long" K) */ 76*088332b5SXin Li lu_byte t; /* table (register or upvalue) */ 77*088332b5SXin Li } ind; 78*088332b5SXin Li struct { /* for local variables */ 79*088332b5SXin Li lu_byte sidx; /* index in the stack */ 80*088332b5SXin Li unsigned short vidx; /* compiler index (in 'actvar.arr') */ 81*088332b5SXin Li } var; 82*088332b5SXin Li } u; 83*088332b5SXin Li int t; /* patch list of 'exit when true' */ 84*088332b5SXin Li int f; /* patch list of 'exit when false' */ 85*088332b5SXin Li } expdesc; 86*088332b5SXin Li 87*088332b5SXin Li 88*088332b5SXin Li /* kinds of variables */ 89*088332b5SXin Li #define VDKREG 0 /* regular */ 90*088332b5SXin Li #define RDKCONST 1 /* constant */ 91*088332b5SXin Li #define RDKTOCLOSE 2 /* to-be-closed */ 92*088332b5SXin Li #define RDKCTC 3 /* compile-time constant */ 93*088332b5SXin Li 94*088332b5SXin Li /* description of an active local variable */ 95*088332b5SXin Li typedef union Vardesc { 96*088332b5SXin Li struct { 97*088332b5SXin Li TValuefields; /* constant value (if it is a compile-time constant) */ 98*088332b5SXin Li lu_byte kind; 99*088332b5SXin Li lu_byte sidx; /* index of the variable in the stack */ 100*088332b5SXin Li short pidx; /* index of the variable in the Proto's 'locvars' array */ 101*088332b5SXin Li TString *name; /* variable name */ 102*088332b5SXin Li } vd; 103*088332b5SXin Li TValue k; /* constant value (if any) */ 104*088332b5SXin Li } Vardesc; 105*088332b5SXin Li 106*088332b5SXin Li 107*088332b5SXin Li 108*088332b5SXin Li /* description of pending goto statements and label statements */ 109*088332b5SXin Li typedef struct Labeldesc { 110*088332b5SXin Li TString *name; /* label identifier */ 111*088332b5SXin Li int pc; /* position in code */ 112*088332b5SXin Li int line; /* line where it appeared */ 113*088332b5SXin Li lu_byte nactvar; /* number of active variables in that position */ 114*088332b5SXin Li lu_byte close; /* goto that escapes upvalues */ 115*088332b5SXin Li } Labeldesc; 116*088332b5SXin Li 117*088332b5SXin Li 118*088332b5SXin Li /* list of labels or gotos */ 119*088332b5SXin Li typedef struct Labellist { 120*088332b5SXin Li Labeldesc *arr; /* array */ 121*088332b5SXin Li int n; /* number of entries in use */ 122*088332b5SXin Li int size; /* array size */ 123*088332b5SXin Li } Labellist; 124*088332b5SXin Li 125*088332b5SXin Li 126*088332b5SXin Li /* dynamic structures used by the parser */ 127*088332b5SXin Li typedef struct Dyndata { 128*088332b5SXin Li struct { /* list of all active local variables */ 129*088332b5SXin Li Vardesc *arr; 130*088332b5SXin Li int n; 131*088332b5SXin Li int size; 132*088332b5SXin Li } actvar; 133*088332b5SXin Li Labellist gt; /* list of pending gotos */ 134*088332b5SXin Li Labellist label; /* list of active labels */ 135*088332b5SXin Li } Dyndata; 136*088332b5SXin Li 137*088332b5SXin Li 138*088332b5SXin Li /* control of blocks */ 139*088332b5SXin Li struct BlockCnt; /* defined in lparser.c */ 140*088332b5SXin Li 141*088332b5SXin Li 142*088332b5SXin Li /* state needed to generate code for a given function */ 143*088332b5SXin Li typedef struct FuncState { 144*088332b5SXin Li Proto *f; /* current function header */ 145*088332b5SXin Li struct FuncState *prev; /* enclosing function */ 146*088332b5SXin Li struct LexState *ls; /* lexical state */ 147*088332b5SXin Li struct BlockCnt *bl; /* chain of current blocks */ 148*088332b5SXin Li int pc; /* next position to code (equivalent to 'ncode') */ 149*088332b5SXin Li int lasttarget; /* 'label' of last 'jump label' */ 150*088332b5SXin Li int previousline; /* last line that was saved in 'lineinfo' */ 151*088332b5SXin Li int nk; /* number of elements in 'k' */ 152*088332b5SXin Li int np; /* number of elements in 'p' */ 153*088332b5SXin Li int nabslineinfo; /* number of elements in 'abslineinfo' */ 154*088332b5SXin Li int firstlocal; /* index of first local var (in Dyndata array) */ 155*088332b5SXin Li int firstlabel; /* index of first label (in 'dyd->label->arr') */ 156*088332b5SXin Li short ndebugvars; /* number of elements in 'f->locvars' */ 157*088332b5SXin Li lu_byte nactvar; /* number of active local variables */ 158*088332b5SXin Li lu_byte nups; /* number of upvalues */ 159*088332b5SXin Li lu_byte freereg; /* first free register */ 160*088332b5SXin Li lu_byte iwthabs; /* instructions issued since last absolute line info */ 161*088332b5SXin Li lu_byte needclose; /* function needs to close upvalues when returning */ 162*088332b5SXin Li } FuncState; 163*088332b5SXin Li 164*088332b5SXin Li 165*088332b5SXin Li LUAI_FUNC int luaY_nvarstack (FuncState *fs); 166*088332b5SXin Li LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 167*088332b5SXin Li Dyndata *dyd, const char *name, int firstchar); 168*088332b5SXin Li 169*088332b5SXin Li 170*088332b5SXin Li #endif 171