compiler.h 6.3 KB

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