lparser.h 5.6 KB

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