xref: /netbsd/external/mit/lua/dist/src/lparser.h (revision 5d7f6829)
1 /*	$NetBSD: lparser.h,v 1.9 2023/04/16 20:46:17 nikita Exp $	*/
2 
3 /*
4 ** Id: lparser.h
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 of 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; ival = numerical integer value */
38   VKSTR,  /* string constant; strval = TString address;
39              (string is fixed by the lexer) */
40   VNONRELOC,  /* expression has its value in a fixed register;
41                  info = result register */
42   VLOCAL,  /* local variable; var.ridx = register index;
43               var.vidx = relative index in 'actvar.arr'  */
44   VUPVAL,  /* upvalue variable; info = index of upvalue in 'upvalues' */
45   VCONST,  /* compile-time <const> variable;
46               info = absolute index in 'actvar.arr'  */
47   VINDEXED,  /* indexed variable;
48                 ind.t = table register;
49                 ind.idx = key's R index */
50   VINDEXUP,  /* indexed upvalue;
51                 ind.t = table upvalue;
52                 ind.idx = key's K index */
53   VINDEXI, /* indexed variable with constant integer;
54                 ind.t = table register;
55                 ind.idx = key's value */
56   VINDEXSTR, /* indexed variable with literal string;
57                 ind.t = table register;
58                 ind.idx = key's K index */
59   VJMP,  /* expression is a test/comparison;
60             info = pc of corresponding jump instruction */
61   VRELOC,  /* expression can put result in any register;
62               info = instruction pc */
63   VCALL,  /* expression is a function call; info = instruction pc */
64   VVARARG  /* vararg expression; info = instruction pc */
65 } expkind;
66 
67 
68 #define vkisvar(k)	(VLOCAL <= (k) && (k) <= VINDEXSTR)
69 #define vkisindexed(k)	(VINDEXED <= (k) && (k) <= VINDEXSTR)
70 
71 
72 typedef struct expdesc {
73   expkind k;
74   union {
75     lua_Integer ival;    /* for VKINT */
76     lua_Number nval;  /* for VKFLT */
77     TString *strval;  /* for VKSTR */
78     int info;  /* for generic use */
79     struct {  /* for indexed variables */
80       short idx;  /* index (R or "long" K) */
81       lu_byte t;  /* table (register or upvalue) */
82     } ind;
83     struct {  /* for local variables */
84       lu_byte ridx;  /* register holding the variable */
85       unsigned short vidx;  /* compiler index (in 'actvar.arr')  */
86     } var;
87   } u;
88   int t;  /* patch list of 'exit when true' */
89   int f;  /* patch list of 'exit when false' */
90 } expdesc;
91 
92 
93 /* kinds of variables */
94 #define VDKREG		0   /* regular */
95 #define RDKCONST	1   /* constant */
96 #define RDKTOCLOSE	2   /* to-be-closed */
97 #define RDKCTC		3   /* compile-time constant */
98 
99 /* description of an active local variable */
100 typedef union Vardesc {
101   struct {
102     TValuefields;  /* constant value (if it is a compile-time constant) */
103     lu_byte kind;
104     lu_byte ridx;  /* register holding the variable */
105     short pidx;  /* index of the variable in the Proto's 'locvars' array */
106     TString *name;  /* variable name */
107   } vd;
108   TValue k;  /* constant value (if any) */
109 } Vardesc;
110 
111 
112 
113 /* description of pending goto statements and label statements */
114 typedef struct Labeldesc {
115   TString *name;  /* label identifier */
116   int pc;  /* position in code */
117   int line;  /* line where it appeared */
118   lu_byte nactvar;  /* number of active variables in that position */
119   lu_byte close;  /* goto that escapes upvalues */
120 } Labeldesc;
121 
122 
123 /* list of labels or gotos */
124 typedef struct Labellist {
125   Labeldesc *arr;  /* array */
126   int n;  /* number of entries in use */
127   int size;  /* array size */
128 } Labellist;
129 
130 
131 /* dynamic structures used by the parser */
132 typedef struct Dyndata {
133   struct {  /* list of all active local variables */
134     Vardesc *arr;
135     int n;
136     int size;
137   } actvar;
138   Labellist gt;  /* list of pending gotos */
139   Labellist label;   /* list of active labels */
140 } Dyndata;
141 
142 
143 /* control of blocks */
144 struct BlockCnt;  /* defined in lparser.c */
145 
146 
147 /* state needed to generate code for a given function */
148 typedef struct FuncState {
149   Proto *f;  /* current function header */
150   struct FuncState *prev;  /* enclosing function */
151   struct LexState *ls;  /* lexical state */
152   struct BlockCnt *bl;  /* chain of current blocks */
153   int pc;  /* next position to code (equivalent to 'ncode') */
154   int lasttarget;   /* 'label' of last 'jump label' */
155   int previousline;  /* last line that was saved in 'lineinfo' */
156   int nk;  /* number of elements in 'k' */
157   int np;  /* number of elements in 'p' */
158   int nabslineinfo;  /* number of elements in 'abslineinfo' */
159   int firstlocal;  /* index of first local var (in Dyndata array) */
160   int firstlabel;  /* index of first label (in 'dyd->label->arr') */
161   short ndebugvars;  /* number of elements in 'f->locvars' */
162   lu_byte nactvar;  /* number of active local variables */
163   lu_byte nups;  /* number of upvalues */
164   lu_byte freereg;  /* first free register */
165   lu_byte iwthabs;  /* instructions issued since last absolute line info */
166   lu_byte needclose;  /* function needs to close upvalues when returning */
167 } FuncState;
168 
169 
170 LUAI_FUNC int luaY_nvarstack (FuncState *fs);
171 LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
172                                  Dyndata *dyd, const char *name, int firstchar);
173 
174 
175 #endif
176