Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:37:57

0001 /*
0002 ** $Id: lparser.h $
0003 ** Lua Parser
0004 ** See Copyright Notice in lua.h
0005 */
0006 
0007 #ifndef lparser_h
0008 #define lparser_h
0009 
0010 #include "llimits.h"
0011 #include "lobject.h"
0012 #include "lzio.h"
0013 
0014 
0015 /*
0016 ** Expression and variable descriptor.
0017 ** Code generation for variables and expressions can be delayed to allow
0018 ** optimizations; An 'expdesc' structure describes a potentially-delayed
0019 ** variable/expression. It has a description of its "main" value plus a
0020 ** list of conditional jumps that can also produce its value (generated
0021 ** by short-circuit operators 'and'/'or').
0022 */
0023 
0024 /* kinds of variables/expressions */
0025 typedef enum {
0026   VVOID,  /* when 'expdesc' describes the last expression of a list,
0027              this kind means an empty list (so, no expression) */
0028   VNIL,  /* constant nil */
0029   VTRUE,  /* constant true */
0030   VFALSE,  /* constant false */
0031   VK,  /* constant in 'k'; info = index of constant in 'k' */
0032   VKFLT,  /* floating constant; nval = numerical float value */
0033   VKINT,  /* integer constant; ival = numerical integer value */
0034   VKSTR,  /* string constant; strval = TString address;
0035              (string is fixed by the lexer) */
0036   VNONRELOC,  /* expression has its value in a fixed register;
0037                  info = result register */
0038   VLOCAL,  /* local variable; var.ridx = register index;
0039               var.vidx = relative index in 'actvar.arr'  */
0040   VUPVAL,  /* upvalue variable; info = index of upvalue in 'upvalues' */
0041   VCONST,  /* compile-time <const> variable;
0042               info = absolute index in 'actvar.arr'  */
0043   VINDEXED,  /* indexed variable;
0044                 ind.t = table register;
0045                 ind.idx = key's R index */
0046   VINDEXUP,  /* indexed upvalue;
0047                 ind.t = table upvalue;
0048                 ind.idx = key's K index */
0049   VINDEXI, /* indexed variable with constant integer;
0050                 ind.t = table register;
0051                 ind.idx = key's value */
0052   VINDEXSTR, /* indexed variable with literal string;
0053                 ind.t = table register;
0054                 ind.idx = key's K index */
0055   VJMP,  /* expression is a test/comparison;
0056             info = pc of corresponding jump instruction */
0057   VRELOC,  /* expression can put result in any register;
0058               info = instruction pc */
0059   VCALL,  /* expression is a function call; info = instruction pc */
0060   VVARARG  /* vararg expression; info = instruction pc */
0061 } expkind;
0062 
0063 
0064 #define vkisvar(k)  (VLOCAL <= (k) && (k) <= VINDEXSTR)
0065 #define vkisindexed(k)  (VINDEXED <= (k) && (k) <= VINDEXSTR)
0066 
0067 
0068 typedef struct expdesc {
0069   expkind k;
0070   union {
0071     lua_Integer ival;    /* for VKINT */
0072     lua_Number nval;  /* for VKFLT */
0073     TString *strval;  /* for VKSTR */
0074     int info;  /* for generic use */
0075     struct {  /* for indexed variables */
0076       short idx;  /* index (R or "long" K) */
0077       lu_byte t;  /* table (register or upvalue) */
0078     } ind;
0079     struct {  /* for local variables */
0080       lu_byte ridx;  /* register holding the variable */
0081       unsigned short vidx;  /* compiler index (in 'actvar.arr')  */
0082     } var;
0083   } u;
0084   int t;  /* patch list of 'exit when true' */
0085   int f;  /* patch list of 'exit when false' */
0086 } expdesc;
0087 
0088 
0089 /* kinds of variables */
0090 #define VDKREG      0   /* regular */
0091 #define RDKCONST    1   /* constant */
0092 #define RDKTOCLOSE  2   /* to-be-closed */
0093 #define RDKCTC      3   /* compile-time constant */
0094 
0095 /* description of an active local variable */
0096 typedef union Vardesc {
0097   struct {
0098     TValuefields;  /* constant value (if it is a compile-time constant) */
0099     lu_byte kind;
0100     lu_byte ridx;  /* register holding the variable */
0101     short pidx;  /* index of the variable in the Proto's 'locvars' array */
0102     TString *name;  /* variable name */
0103   } vd;
0104   TValue k;  /* constant value (if any) */
0105 } Vardesc;
0106 
0107 
0108 
0109 /* description of pending goto statements and label statements */
0110 typedef struct Labeldesc {
0111   TString *name;  /* label identifier */
0112   int pc;  /* position in code */
0113   int line;  /* line where it appeared */
0114   lu_byte nactvar;  /* number of active variables in that position */
0115   lu_byte close;  /* goto that escapes upvalues */
0116 } Labeldesc;
0117 
0118 
0119 /* list of labels or gotos */
0120 typedef struct Labellist {
0121   Labeldesc *arr;  /* array */
0122   int n;  /* number of entries in use */
0123   int size;  /* array size */
0124 } Labellist;
0125 
0126 
0127 /* dynamic structures used by the parser */
0128 typedef struct Dyndata {
0129   struct {  /* list of all active local variables */
0130     Vardesc *arr;
0131     int n;
0132     int size;
0133   } actvar;
0134   Labellist gt;  /* list of pending gotos */
0135   Labellist label;   /* list of active labels */
0136 } Dyndata;
0137 
0138 
0139 /* control of blocks */
0140 struct BlockCnt;  /* defined in lparser.c */
0141 
0142 
0143 /* state needed to generate code for a given function */
0144 typedef struct FuncState {
0145   Proto *f;  /* current function header */
0146   struct FuncState *prev;  /* enclosing function */
0147   struct LexState *ls;  /* lexical state */
0148   struct BlockCnt *bl;  /* chain of current blocks */
0149   int pc;  /* next position to code (equivalent to 'ncode') */
0150   int lasttarget;   /* 'label' of last 'jump label' */
0151   int previousline;  /* last line that was saved in 'lineinfo' */
0152   int nk;  /* number of elements in 'k' */
0153   int np;  /* number of elements in 'p' */
0154   int nabslineinfo;  /* number of elements in 'abslineinfo' */
0155   int firstlocal;  /* index of first local var (in Dyndata array) */
0156   int firstlabel;  /* index of first label (in 'dyd->label->arr') */
0157   short ndebugvars;  /* number of elements in 'f->locvars' */
0158   lu_byte nactvar;  /* number of active local variables */
0159   lu_byte nups;  /* number of upvalues */
0160   lu_byte freereg;  /* first free register */
0161   lu_byte iwthabs;  /* instructions issued since last absolute line info */
0162   lu_byte needclose;  /* function needs to close upvalues when returning */
0163 } FuncState;
0164 
0165 
0166 LUAI_FUNC int luaY_nvarstack (FuncState *fs);
0167 LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
0168                                  Dyndata *dyd, const char *name, int firstchar);
0169 
0170 
0171 #endif