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