compiler.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _COMPILER_H_
  23. #define _COMPILER_H_
  24. class Stream;
  25. class DataChunker;
  26. #include "platform/platform.h"
  27. #include "console/ast.h"
  28. #include "console/codeBlock.h"
  29. namespace Compiler
  30. {
  31. /// The opcodes for the TorqueScript VM.
  32. enum CompiledInstructions
  33. {
  34. OP_FUNC_DECL,
  35. OP_CREATE_OBJECT,
  36. OP_ADD_OBJECT,
  37. OP_END_OBJECT,
  38. // Added to fix the stack issue [7/9/2007 Black]
  39. OP_FINISH_OBJECT,
  40. OP_JMPIFFNOT,
  41. OP_JMPIFNOT,
  42. OP_JMPIFF,
  43. OP_JMPIF,
  44. OP_JMPIFNOT_NP,
  45. OP_JMPIF_NP,
  46. OP_JMP,
  47. OP_RETURN,
  48. // fixes a bug when not explicitly returning a value
  49. OP_RETURN_VOID,
  50. OP_CMPEQ,
  51. OP_CMPGR,
  52. OP_CMPGE,
  53. OP_CMPLT,
  54. OP_CMPLE,
  55. OP_CMPNE,
  56. OP_XOR,
  57. OP_MOD,
  58. OP_BITAND,
  59. OP_BITOR,
  60. OP_NOT,
  61. OP_NOTF,
  62. OP_ONESCOMPLEMENT,
  63. OP_SHR,
  64. OP_SHL,
  65. OP_AND,
  66. OP_OR,
  67. OP_ADD,
  68. OP_SUB,
  69. OP_MUL,
  70. OP_DIV,
  71. OP_NEG,
  72. OP_SETCURVAR,
  73. OP_SETCURVAR_CREATE,
  74. OP_SETCURVAR_ARRAY,
  75. OP_SETCURVAR_ARRAY_CREATE,
  76. OP_LOADVAR_UINT,
  77. OP_LOADVAR_FLT,
  78. OP_LOADVAR_STR,
  79. OP_SAVEVAR_UINT,
  80. OP_SAVEVAR_FLT,
  81. OP_SAVEVAR_STR,
  82. OP_SETCUROBJECT,
  83. OP_SETCUROBJECT_NEW,
  84. OP_SETCUROBJECT_INTERNAL,
  85. OP_SETCURFIELD,
  86. OP_SETCURFIELD_ARRAY,
  87. OP_SETCURFIELD_TYPE,
  88. OP_LOADFIELD_UINT,
  89. OP_LOADFIELD_FLT,
  90. OP_LOADFIELD_STR,
  91. OP_SAVEFIELD_UINT,
  92. OP_SAVEFIELD_FLT,
  93. OP_SAVEFIELD_STR,
  94. OP_STR_TO_UINT,
  95. OP_STR_TO_FLT,
  96. OP_STR_TO_NONE,
  97. OP_FLT_TO_UINT,
  98. OP_FLT_TO_STR,
  99. OP_FLT_TO_NONE,
  100. OP_UINT_TO_FLT,
  101. OP_UINT_TO_STR,
  102. OP_UINT_TO_NONE,
  103. OP_LOADIMMED_UINT,
  104. OP_LOADIMMED_FLT,
  105. OP_TAG_TO_STR,
  106. OP_LOADIMMED_STR,
  107. OP_DOCBLOCK_STR,
  108. OP_LOADIMMED_IDENT,
  109. OP_CALLFUNC_RESOLVE,
  110. OP_CALLFUNC,
  111. OP_ADVANCE_STR,
  112. OP_ADVANCE_STR_APPENDCHAR,
  113. OP_ADVANCE_STR_COMMA,
  114. OP_ADVANCE_STR_NUL,
  115. OP_REWIND_STR,
  116. OP_TERMINATE_REWIND_STR,
  117. OP_COMPARE_STR,
  118. OP_PUSH,
  119. OP_PUSH_FRAME,
  120. OP_ASSERT,
  121. OP_BREAK,
  122. OP_ITER_BEGIN, ///< Prepare foreach iterator.
  123. OP_ITER_BEGIN_STR, ///< Prepare foreach$ iterator.
  124. OP_ITER, ///< Enter foreach loop.
  125. OP_ITER_END, ///< End foreach loop.
  126. OP_INVALID
  127. };
  128. //------------------------------------------------------------
  129. F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
  130. U32 precompileBlock(StmtNode *block, U32 loopCount);
  131. U32 compileBlock(StmtNode *block, U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint);
  132. //------------------------------------------------------------
  133. struct CompilerIdentTable
  134. {
  135. struct Entry
  136. {
  137. U32 offset;
  138. U32 ip;
  139. Entry *next;
  140. Entry *nextIdent;
  141. };
  142. Entry *list;
  143. void add(StringTableEntry ste, U32 ip);
  144. void reset();
  145. void write(Stream &st);
  146. };
  147. //------------------------------------------------------------
  148. struct CompilerStringTable
  149. {
  150. U32 totalLen;
  151. struct Entry
  152. {
  153. char *string;
  154. U32 start;
  155. U32 len;
  156. bool tag;
  157. Entry *next;
  158. };
  159. Entry *list;
  160. char buf[256];
  161. U32 add(const char *str, bool caseSens = true, bool tag = false);
  162. U32 addIntString(U32 value);
  163. U32 addFloatString(F64 value);
  164. void reset();
  165. char *build();
  166. void write(Stream &st);
  167. };
  168. //------------------------------------------------------------
  169. struct CompilerFloatTable
  170. {
  171. struct Entry
  172. {
  173. F64 val;
  174. Entry *next;
  175. };
  176. U32 count;
  177. Entry *list;
  178. U32 add(F64 value);
  179. void reset();
  180. F64 *build();
  181. void write(Stream &st);
  182. };
  183. //------------------------------------------------------------
  184. inline StringTableEntry U32toSTE(U32 u)
  185. {
  186. return *((StringTableEntry *) &u);
  187. }
  188. extern U32 (*STEtoU32)(StringTableEntry ste, U32 ip);
  189. U32 evalSTEtoU32(StringTableEntry ste, U32);
  190. U32 compileSTEtoU32(StringTableEntry ste, U32 ip);
  191. CompilerStringTable *getCurrentStringTable();
  192. CompilerStringTable &getGlobalStringTable();
  193. CompilerStringTable &getFunctionStringTable();
  194. void setCurrentStringTable (CompilerStringTable* cst);
  195. CompilerFloatTable *getCurrentFloatTable();
  196. CompilerFloatTable &getGlobalFloatTable();
  197. CompilerFloatTable &getFunctionFloatTable();
  198. void setCurrentFloatTable (CompilerFloatTable* cst);
  199. CompilerIdentTable &getIdentTable();
  200. void precompileIdent(StringTableEntry ident);
  201. CodeBlock *getBreakCodeBlock();
  202. void setBreakCodeBlock(CodeBlock *cb);
  203. /// Helper function to reset the float, string, and ident tables to a base
  204. /// starting state.
  205. void resetTables();
  206. void *consoleAlloc(U32 size);
  207. void consoleAllocReset();
  208. extern bool gSyntaxError;
  209. };
  210. #endif