compiler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_RETURN_FLT,
  51. OP_RETURN_UINT,
  52. OP_CMPEQ,
  53. OP_CMPGR,
  54. OP_CMPGE,
  55. OP_CMPLT,
  56. OP_CMPLE,
  57. OP_CMPNE,
  58. OP_XOR,
  59. OP_MOD,
  60. OP_BITAND,
  61. OP_BITOR,
  62. OP_NOT,
  63. OP_NOTF,
  64. OP_ONESCOMPLEMENT,
  65. OP_SHR,
  66. OP_SHL,
  67. OP_AND,
  68. OP_OR,
  69. OP_ADD,
  70. OP_SUB,
  71. OP_MUL,
  72. OP_DIV,
  73. OP_NEG,
  74. OP_SETCURVAR,
  75. OP_SETCURVAR_CREATE,
  76. OP_SETCURVAR_ARRAY,
  77. OP_SETCURVAR_ARRAY_CREATE,
  78. OP_LOADVAR_UINT,
  79. OP_LOADVAR_FLT,
  80. OP_LOADVAR_STR,
  81. OP_LOADVAR_VAR,
  82. OP_SAVEVAR_UINT,
  83. OP_SAVEVAR_FLT,
  84. OP_SAVEVAR_STR,
  85. OP_SAVEVAR_VAR,
  86. OP_SETCUROBJECT,
  87. OP_SETCUROBJECT_NEW,
  88. OP_SETCUROBJECT_INTERNAL,
  89. OP_SETCURFIELD,
  90. OP_SETCURFIELD_ARRAY,
  91. OP_SETCURFIELD_TYPE,
  92. OP_LOADFIELD_UINT,
  93. OP_LOADFIELD_FLT,
  94. OP_LOADFIELD_STR,
  95. OP_SAVEFIELD_UINT,
  96. OP_SAVEFIELD_FLT,
  97. OP_SAVEFIELD_STR,
  98. OP_STR_TO_UINT,
  99. OP_STR_TO_FLT,
  100. OP_STR_TO_NONE,
  101. OP_FLT_TO_UINT,
  102. OP_FLT_TO_STR,
  103. OP_FLT_TO_NONE,
  104. OP_UINT_TO_FLT,
  105. OP_UINT_TO_STR,
  106. OP_UINT_TO_NONE,
  107. OP_COPYVAR_TO_NONE,
  108. OP_LOADIMMED_UINT,
  109. OP_LOADIMMED_FLT,
  110. OP_TAG_TO_STR,
  111. OP_LOADIMMED_STR,
  112. OP_DOCBLOCK_STR,
  113. OP_LOADIMMED_IDENT,
  114. OP_CALLFUNC_RESOLVE,
  115. OP_CALLFUNC,
  116. OP_ADVANCE_STR,
  117. OP_ADVANCE_STR_APPENDCHAR,
  118. OP_ADVANCE_STR_COMMA,
  119. OP_ADVANCE_STR_NUL,
  120. OP_REWIND_STR,
  121. OP_TERMINATE_REWIND_STR,
  122. OP_COMPARE_STR,
  123. OP_PUSH, // String
  124. OP_PUSH_UINT, // Integer
  125. OP_PUSH_FLT, // Float
  126. OP_PUSH_VAR, // Variable
  127. OP_PUSH_FRAME, // Frame
  128. OP_ASSERT,
  129. OP_BREAK,
  130. OP_ITER_BEGIN, ///< Prepare foreach iterator.
  131. OP_ITER_BEGIN_STR, ///< Prepare foreach$ iterator.
  132. OP_ITER, ///< Enter foreach loop.
  133. OP_ITER_END, ///< End foreach loop.
  134. OP_INVALID
  135. };
  136. //------------------------------------------------------------
  137. F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
  138. U32 precompileBlock(StmtNode *block, U32 loopCount);
  139. U32 compileBlock(StmtNode *block, U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint);
  140. //------------------------------------------------------------
  141. struct CompilerIdentTable
  142. {
  143. struct Entry
  144. {
  145. U32 offset;
  146. U32 ip;
  147. Entry *next;
  148. Entry *nextIdent;
  149. };
  150. Entry *list;
  151. void add(StringTableEntry ste, U32 ip);
  152. void reset();
  153. void write(Stream &st);
  154. };
  155. //------------------------------------------------------------
  156. struct CompilerStringTable
  157. {
  158. U32 totalLen;
  159. struct Entry
  160. {
  161. char *string;
  162. U32 start;
  163. U32 len;
  164. bool tag;
  165. Entry *next;
  166. };
  167. Entry *list;
  168. char buf[256];
  169. U32 add(const char *str, bool caseSens = true, bool tag = false);
  170. U32 addIntString(U32 value);
  171. U32 addFloatString(F64 value);
  172. void reset();
  173. char *build();
  174. void write(Stream &st);
  175. };
  176. //------------------------------------------------------------
  177. struct CompilerFloatTable
  178. {
  179. struct Entry
  180. {
  181. F64 val;
  182. Entry *next;
  183. };
  184. U32 count;
  185. Entry *list;
  186. U32 add(F64 value);
  187. void reset();
  188. F64 *build();
  189. void write(Stream &st);
  190. };
  191. //------------------------------------------------------------
  192. inline StringTableEntry U32toSTE(U32 u)
  193. {
  194. return *((StringTableEntry *) &u);
  195. }
  196. extern U32 (*STEtoU32)(StringTableEntry ste, U32 ip);
  197. U32 evalSTEtoU32(StringTableEntry ste, U32);
  198. U32 compileSTEtoU32(StringTableEntry ste, U32 ip);
  199. CompilerStringTable *getCurrentStringTable();
  200. CompilerStringTable &getGlobalStringTable();
  201. CompilerStringTable &getFunctionStringTable();
  202. void setCurrentStringTable (CompilerStringTable* cst);
  203. CompilerFloatTable *getCurrentFloatTable();
  204. CompilerFloatTable &getGlobalFloatTable();
  205. CompilerFloatTable &getFunctionFloatTable();
  206. void setCurrentFloatTable (CompilerFloatTable* cst);
  207. CompilerIdentTable &getIdentTable();
  208. void precompileIdent(StringTableEntry ident);
  209. CodeBlock *getBreakCodeBlock();
  210. void setBreakCodeBlock(CodeBlock *cb);
  211. /// Helper function to reset the float, string, and ident tables to a base
  212. /// starting state.
  213. void resetTables();
  214. void *consoleAlloc(U32 size);
  215. void consoleAllocReset();
  216. extern bool gSyntaxError;
  217. };
  218. #endif