lparser.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. ** $Id: lparser.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $
  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; info = local register */
  32. VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */
  33. VINDEXED, /* indexed variable;
  34. ind.t = table register;
  35. ind.idx = key's R index */
  36. VINDEXUP, /* indexed upvalue;
  37. ind.t = table upvalue;
  38. ind.idx = key's K index */
  39. VINDEXI, /* indexed variable with constant integer;
  40. ind.t = table register;
  41. ind.idx = key's value */
  42. VINDEXSTR, /* indexed variable with literal string;
  43. ind.t = table register;
  44. ind.idx = key's K index */
  45. VJMP, /* expression is a test/comparison;
  46. info = pc of corresponding jump instruction */
  47. VRELOC, /* expression can put result in any register;
  48. info = instruction pc */
  49. VCALL, /* expression is a function call; info = instruction pc */
  50. VVARARG /* vararg expression; info = instruction pc */
  51. } expkind;
  52. #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR)
  53. #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR)
  54. #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL)
  55. typedef struct expdesc {
  56. expkind k;
  57. union {
  58. lua_Integer ival; /* for VKINT */
  59. lua_Number nval; /* for VKFLT */
  60. int info; /* for generic use */
  61. struct { /* for indexed variables */
  62. short idx; /* index (R or "long" K) */
  63. lu_byte t; /* table (register or upvalue) */
  64. } ind;
  65. } u;
  66. int t; /* patch list of 'exit when true' */
  67. int f; /* patch list of 'exit when false' */
  68. } expdesc;
  69. /* description of active local variable */
  70. typedef struct Vardesc {
  71. short idx; /* variable index in stack */
  72. } Vardesc;
  73. /* description of pending goto statements and label statements */
  74. typedef struct Labeldesc {
  75. TString *name; /* label identifier */
  76. int pc; /* position in code */
  77. int line; /* line where it appeared */
  78. lu_byte nactvar; /* local level where it appears in current block */
  79. } Labeldesc;
  80. /* list of labels or gotos */
  81. typedef struct Labellist {
  82. Labeldesc *arr; /* array */
  83. int n; /* number of entries in use */
  84. int size; /* array size */
  85. } Labellist;
  86. /* dynamic structures used by the parser */
  87. typedef struct Dyndata {
  88. struct { /* list of active local variables */
  89. Vardesc *arr;
  90. int n;
  91. int size;
  92. } actvar;
  93. Labellist gt; /* list of pending gotos */
  94. Labellist label; /* list of active labels */
  95. } Dyndata;
  96. /* control of blocks */
  97. struct BlockCnt; /* defined in lparser.c */
  98. /* state needed to generate code for a given function */
  99. typedef struct FuncState {
  100. Proto *f; /* current function header */
  101. struct FuncState *prev; /* enclosing function */
  102. struct LexState *ls; /* lexical state */
  103. struct BlockCnt *bl; /* chain of current blocks */
  104. int pc; /* next position to code (equivalent to 'ncode') */
  105. int lasttarget; /* 'label' of last 'jump label' */
  106. int previousline; /* last line that was saved in 'lineinfo' */
  107. int nk; /* number of elements in 'k' */
  108. int np; /* number of elements in 'p' */
  109. int nabslineinfo; /* number of elements in 'abslineinfo' */
  110. int firstlocal; /* index of first local var (in Dyndata array) */
  111. short nlocvars; /* number of elements in 'f->locvars' */
  112. lu_byte nactvar; /* number of active local variables */
  113. lu_byte nups; /* number of upvalues */
  114. lu_byte freereg; /* first free register */
  115. lu_byte iwthabs; /* instructions issued since last absolute line info */
  116. } FuncState;
  117. LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
  118. Dyndata *dyd, const char *name, int firstchar);
  119. #endif