1 /* $NetBSD: lparser.h,v 1.7 2017/04/26 13:17:33 mbalmer Exp $ */ 2 3 /* 4 ** Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp 5 ** Lua Parser 6 ** See Copyright Notice in lua.h 7 */ 8 9 #ifndef lparser_h 10 #define lparser_h 11 12 #include "llimits.h" 13 #include "lobject.h" 14 #include "lzio.h" 15 16 17 /* 18 ** Expression and variable descriptor. 19 ** Code generation for variables and expressions can be delayed to allow 20 ** optimizations; An 'expdesc' structure describes a potentially-delayed 21 ** variable/expression. It has a description of its "main" value plus a 22 ** list of conditional jumps that can also produce its value (generated 23 ** by short-circuit operators 'and'/'or'). 24 */ 25 26 /* kinds of variables/expressions */ 27 typedef enum { 28 VVOID, /* when 'expdesc' describes the last expression a list, 29 this kind means an empty list (so, no expression) */ 30 VNIL, /* constant nil */ 31 VTRUE, /* constant true */ 32 VFALSE, /* constant false */ 33 VK, /* constant in 'k'; info = index of constant in 'k' */ 34 #ifndef _KERNEL 35 VKFLT, /* floating constant; nval = numerical float value */ 36 #endif 37 VKINT, /* integer constant; nval = numerical integer value */ 38 VNONRELOC, /* expression has its value in a fixed register; 39 info = result register */ 40 VLOCAL, /* local variable; info = local register */ 41 VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ 42 VINDEXED, /* indexed variable; 43 ind.vt = whether 't' is register or upvalue; 44 ind.t = table register or upvalue; 45 ind.idx = key's R/K index */ 46 VJMP, /* expression is a test/comparison; 47 info = pc of corresponding jump instruction */ 48 VRELOCABLE, /* expression can put result in any register; 49 info = instruction pc */ 50 VCALL, /* expression is a function call; info = instruction pc */ 51 VVARARG /* vararg expression; info = instruction pc */ 52 } expkind; 53 54 55 #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 56 #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 57 58 typedef struct expdesc { 59 expkind k; 60 union { 61 lua_Integer ival; /* for VKINT */ 62 lua_Number nval; /* for VKFLT */ 63 int info; /* for generic use */ 64 struct { /* for indexed variables (VINDEXED) */ 65 short idx; /* index (R/K) */ 66 lu_byte t; /* table (register or upvalue) */ 67 lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 68 } ind; 69 } u; 70 int t; /* patch list of 'exit when true' */ 71 int f; /* patch list of 'exit when false' */ 72 } expdesc; 73 74 75 /* description of active local variable */ 76 typedef struct Vardesc { 77 short idx; /* variable index in stack */ 78 } Vardesc; 79 80 81 /* description of pending goto statements and label statements */ 82 typedef struct Labeldesc { 83 TString *name; /* label identifier */ 84 int pc; /* position in code */ 85 int line; /* line where it appeared */ 86 lu_byte nactvar; /* local level where it appears in current block */ 87 } Labeldesc; 88 89 90 /* list of labels or gotos */ 91 typedef struct Labellist { 92 Labeldesc *arr; /* array */ 93 int n; /* number of entries in use */ 94 int size; /* array size */ 95 } Labellist; 96 97 98 /* dynamic structures used by the parser */ 99 typedef struct Dyndata { 100 struct { /* list of active local variables */ 101 Vardesc *arr; 102 int n; 103 int size; 104 } actvar; 105 Labellist gt; /* list of pending gotos */ 106 Labellist label; /* list of active labels */ 107 } Dyndata; 108 109 110 /* control of blocks */ 111 struct BlockCnt; /* defined in lparser.c */ 112 113 114 /* state needed to generate code for a given function */ 115 typedef struct FuncState { 116 Proto *f; /* current function header */ 117 struct FuncState *prev; /* enclosing function */ 118 struct LexState *ls; /* lexical state */ 119 struct BlockCnt *bl; /* chain of current blocks */ 120 int pc; /* next position to code (equivalent to 'ncode') */ 121 int lasttarget; /* 'label' of last 'jump label' */ 122 int jpc; /* list of pending jumps to 'pc' */ 123 int nk; /* number of elements in 'k' */ 124 int np; /* number of elements in 'p' */ 125 int firstlocal; /* index of first local var (in Dyndata array) */ 126 short nlocvars; /* number of elements in 'f->locvars' */ 127 lu_byte nactvar; /* number of active local variables */ 128 lu_byte nups; /* number of upvalues */ 129 lu_byte freereg; /* first free register */ 130 } FuncState; 131 132 133 LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 134 Dyndata *dyd, const char *name, int firstchar); 135 136 137 #endif 138