lparser.h 6.9 KB

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