lparser.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. VGLOBAL, /* global variable;
  36. info = relative index in 'actvar.arr' (or -1 for
  37. implicit declaration) */
  38. VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
  39. VCONST, /* compile-time <const> variable;
  40. info = absolute index in 'actvar.arr' */
  41. VINDEXED, /* indexed variable;
  42. ind.t = table register;
  43. ind.idx = key's R index;
  44. ind.ro = true if it represents a read-only global;
  45. ind.keystr = if key is a string, index in 'k' of that string;
  46. -1 if key is not a string */
  47. VINDEXUP, /* indexed upvalue;
  48. ind.idx = key's K index;
  49. ind.* as in VINDEXED */
  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.idx = key's K index;
  55. ind.* as in VINDEXED */
  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 */
  62. } expkind;
  63. #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
  64. #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
  65. typedef struct expdesc {
  66. expkind k;
  67. union {
  68. lua_Integer ival; /* for VKINT */
  69. lua_Number nval; /* for VKFLT */
  70. TString *strval; /* for VKSTR */
  71. int info; /* for generic use */
  72. struct { /* for indexed variables */
  73. short idx; /* index (R or "long" K) */
  74. lu_byte t; /* table (register or upvalue) */
  75. lu_byte ro; /* true if variable is read-only */
  76. int keystr; /* index in 'k' of string key, or -1 if not a string */
  77. } ind;
  78. struct { /* for local variables */
  79. lu_byte ridx; /* register holding the variable */
  80. short vidx; /* index in 'actvar.arr' */
  81. } var;
  82. } u;
  83. int t; /* patch list of 'exit when true' */
  84. int f; /* patch list of 'exit when false' */
  85. } expdesc;
  86. /* kinds of variables */
  87. #define VDKREG 0 /* regular local */
  88. #define RDKCONST 1 /* local constant */
  89. #define RDKTOCLOSE 2 /* to-be-closed */
  90. #define RDKCTC 3 /* local compile-time constant */
  91. #define GDKREG 4 /* regular global */
  92. #define GDKCONST 5 /* global constant */
  93. /* variables that live in registers */
  94. #define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE)
  95. /* test for global variables */
  96. #define varglobal(v) ((v)->vd.kind >= GDKREG)
  97. /* description of an active variable */
  98. typedef union Vardesc {
  99. struct {
  100. TValuefields; /* constant value (if it is a compile-time constant) */
  101. lu_byte kind;
  102. lu_byte ridx; /* register holding the variable */
  103. short pidx; /* index of the variable in the Proto's 'locvars' array */
  104. TString *name; /* variable name */
  105. } vd;
  106. TValue k; /* constant value (if any) */
  107. } Vardesc;
  108. /* description of pending goto statements and label statements */
  109. typedef struct Labeldesc {
  110. TString *name; /* label identifier */
  111. int pc; /* position in code */
  112. int line; /* line where it appeared */
  113. short nactvar; /* number of active variables in that position */
  114. lu_byte close; /* true for goto that escapes upvalues */
  115. } Labeldesc;
  116. /* list of labels or gotos */
  117. typedef struct Labellist {
  118. Labeldesc *arr; /* array */
  119. int n; /* number of entries in use */
  120. int size; /* array size */
  121. } Labellist;
  122. /* dynamic structures used by the parser */
  123. typedef struct Dyndata {
  124. struct { /* list of all active local variables */
  125. Vardesc *arr;
  126. int n;
  127. int size;
  128. } actvar;
  129. Labellist gt; /* list of pending gotos */
  130. Labellist label; /* list of active labels */
  131. } Dyndata;
  132. /* control of blocks */
  133. struct BlockCnt; /* defined in lparser.c */
  134. /* state needed to generate code for a given function */
  135. typedef struct FuncState {
  136. Proto *f; /* current function header */
  137. struct FuncState *prev; /* enclosing function */
  138. struct LexState *ls; /* lexical state */
  139. struct BlockCnt *bl; /* chain of current blocks */
  140. Table *kcache; /* cache for reusing constants */
  141. int pc; /* next position to code (equivalent to 'ncode') */
  142. int lasttarget; /* 'label' of last 'jump label' */
  143. int previousline; /* last line that was saved in 'lineinfo' */
  144. int nk; /* number of elements in 'k' */
  145. int np; /* number of elements in 'p' */
  146. int nabslineinfo; /* number of elements in 'abslineinfo' */
  147. int firstlocal; /* index of first local var (in Dyndata array) */
  148. int firstlabel; /* index of first label (in 'dyd->label->arr') */
  149. short ndebugvars; /* number of elements in 'f->locvars' */
  150. short nactvar; /* number of active variable declarations */
  151. lu_byte nups; /* number of upvalues */
  152. lu_byte freereg; /* first free register */
  153. lu_byte iwthabs; /* instructions issued since last absolute line info */
  154. lu_byte needclose; /* function needs to close upvalues when returning */
  155. } FuncState;
  156. LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs);
  157. LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l,
  158. const char *what);
  159. LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
  160. Dyndata *dyd, const char *name, int firstchar);
  161. #endif