compiler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. // Autogenerated, so we should only ever include from once place - here.
  30. // (We can't stick include guards in it without patching bison.)
  31. #ifndef _CMDGRAM_H_
  32. #define _CMDGRAM_H_
  33. #include "console/cmdgram.h"
  34. #endif
  35. namespace Compiler
  36. {
  37. /// The opcodes for the TorqueScript VM.
  38. enum CompiledInstructions
  39. {
  40. OP_FUNC_DECL,
  41. OP_CREATE_OBJECT,
  42. OP_ADD_OBJECT,
  43. OP_END_OBJECT,
  44. OP_JMPIFFNOT,
  45. OP_JMPIFNOT,
  46. OP_JMPIFF,
  47. OP_JMPIF,
  48. OP_JMPIFNOT_NP,
  49. OP_JMPIF_NP,
  50. OP_JMP,
  51. OP_RETURN,
  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_LOADFIELD_UINT,
  90. OP_LOADFIELD_FLT,
  91. OP_LOADFIELD_STR,
  92. OP_SAVEFIELD_UINT,
  93. OP_SAVEFIELD_FLT,
  94. OP_SAVEFIELD_STR,
  95. OP_STR_TO_UINT,
  96. OP_STR_TO_FLT,
  97. OP_STR_TO_NONE,
  98. OP_FLT_TO_UINT,
  99. OP_FLT_TO_STR,
  100. OP_FLT_TO_NONE,
  101. OP_UINT_TO_FLT,
  102. OP_UINT_TO_STR,
  103. OP_UINT_TO_NONE,
  104. OP_LOADIMMED_UINT,
  105. OP_LOADIMMED_FLT,
  106. OP_TAG_TO_STR,
  107. OP_LOADIMMED_STR,
  108. OP_DOCBLOCK_STR,
  109. OP_LOADIMMED_IDENT,
  110. OP_CALLFUNC_RESOLVE,
  111. OP_CALLFUNC,
  112. OP_ADVANCE_STR,
  113. OP_ADVANCE_STR_APPENDCHAR,
  114. OP_ADVANCE_STR_COMMA,
  115. OP_ADVANCE_STR_NUL,
  116. OP_REWIND_STR,
  117. OP_TERMINATE_REWIND_STR,
  118. OP_COMPARE_STR,
  119. OP_PUSH,
  120. OP_PUSH_FRAME,
  121. OP_BREAK,
  122. OP_INVALID
  123. };
  124. //------------------------------------------------------------
  125. F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
  126. U32 precompileBlock(StmtNode *block, U32 loopCount);
  127. U32 compileBlock(StmtNode *block, U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint);
  128. //------------------------------------------------------------
  129. struct CompilerIdentTable
  130. {
  131. struct Entry
  132. {
  133. U32 offset;
  134. U32 ip;
  135. Entry *next;
  136. Entry *nextIdent;
  137. };
  138. Entry *list;
  139. void add(StringTableEntry ste, U32 ip);
  140. void reset();
  141. void write(Stream &st);
  142. };
  143. //------------------------------------------------------------
  144. struct CompilerStringTable
  145. {
  146. U32 totalLen;
  147. struct Entry
  148. {
  149. char *string;
  150. U32 start;
  151. U32 len;
  152. bool tag;
  153. Entry *next;
  154. };
  155. Entry *list;
  156. char buf[256];
  157. U32 add(const char *str, bool caseSens = true, bool tag = false);
  158. U32 addIntString(U32 value);
  159. U32 addFloatString(F64 value);
  160. void reset();
  161. char *build();
  162. void write(Stream &st);
  163. };
  164. //------------------------------------------------------------
  165. struct CompilerFloatTable
  166. {
  167. struct Entry
  168. {
  169. F64 val;
  170. Entry *next;
  171. };
  172. U32 count;
  173. Entry *list;
  174. U32 add(F64 value);
  175. void reset();
  176. F64 *build();
  177. void write(Stream &st);
  178. };
  179. //------------------------------------------------------------
  180. inline StringTableEntry CodeToSTE(U32 *code, U32 ip)
  181. {
  182. #ifdef TORQUE_CPU_X64
  183. return (StringTableEntry)(*((U64*)(code+ip)));
  184. #else
  185. return (StringTableEntry)(*(code+ip));
  186. #endif
  187. }
  188. extern void (*STEtoCode)(StringTableEntry ste, U32 ip, U32 *codeStream);
  189. void evalSTEtoCode(StringTableEntry ste, U32 ip, U32 *codeStream);
  190. void compileSTEtoCode(StringTableEntry ste, U32 ip, U32 *codeStream);
  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