compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. //#define DEBUG_CODESTREAM
  25. #ifdef DEBUG_CODESTREAM
  26. #include <stdio.h>
  27. #endif
  28. #include <string>
  29. #include <unordered_map>
  30. class Stream;
  31. class DataChunker;
  32. #include "platform/platform.h"
  33. #include "console/ast.h"
  34. #include "console/codeBlock.h"
  35. #ifndef _TVECTOR_H_
  36. #include "core/util/tVector.h"
  37. #endif
  38. namespace Compiler
  39. {
  40. /// The opcodes for the TorqueScript VM.
  41. enum CompiledInstructions
  42. {
  43. OP_FUNC_DECL,
  44. OP_CREATE_OBJECT,
  45. OP_ADD_OBJECT,
  46. OP_END_OBJECT,
  47. // Added to fix the stack issue [7/9/2007 Black]
  48. OP_FINISH_OBJECT,
  49. OP_JMPIFFNOT,
  50. OP_JMPIFNOT,
  51. OP_JMPIFF,
  52. OP_JMPIF,
  53. OP_JMPIFNOT_NP,
  54. OP_JMPIF_NP, // 10
  55. OP_JMP,
  56. OP_RETURN,
  57. // fixes a bug when not explicitly returning a value
  58. OP_RETURN_VOID,
  59. OP_RETURN_FLT,
  60. OP_RETURN_UINT,
  61. OP_CMPEQ,
  62. OP_CMPGR,
  63. OP_CMPGE,
  64. OP_CMPLT,
  65. OP_CMPLE,
  66. OP_CMPNE,
  67. OP_XOR, // 20
  68. OP_MOD,
  69. OP_BITAND,
  70. OP_BITOR,
  71. OP_NOT,
  72. OP_NOTF,
  73. OP_ONESCOMPLEMENT,
  74. OP_SHR,
  75. OP_SHL,
  76. OP_AND,
  77. OP_OR, // 30
  78. OP_ADD,
  79. OP_SUB,
  80. OP_MUL,
  81. OP_DIV,
  82. OP_NEG,
  83. OP_INC,
  84. OP_SETCURVAR,
  85. OP_SETCURVAR_CREATE,
  86. OP_SETCURVAR_ARRAY,
  87. OP_SETCURVAR_ARRAY_CREATE,
  88. OP_LOADVAR_UINT,// 40
  89. OP_LOADVAR_FLT,
  90. OP_LOADVAR_STR,
  91. OP_SAVEVAR_UINT,
  92. OP_SAVEVAR_FLT,
  93. OP_SAVEVAR_STR,
  94. OP_LOAD_LOCAL_VAR_UINT,
  95. OP_LOAD_LOCAL_VAR_FLT,
  96. OP_LOAD_LOCAL_VAR_STR,
  97. OP_SAVE_LOCAL_VAR_UINT,
  98. OP_SAVE_LOCAL_VAR_FLT,
  99. OP_SAVE_LOCAL_VAR_STR,
  100. OP_SETCUROBJECT,
  101. OP_SETCUROBJECT_NEW,
  102. OP_SETCUROBJECT_INTERNAL,
  103. OP_SETCURFIELD,
  104. OP_SETCURFIELD_ARRAY, // 50
  105. OP_SETCURFIELD_TYPE,
  106. OP_LOADFIELD_UINT,
  107. OP_LOADFIELD_FLT,
  108. OP_LOADFIELD_STR,
  109. OP_SAVEFIELD_UINT,
  110. OP_SAVEFIELD_FLT,
  111. OP_SAVEFIELD_STR,
  112. OP_STR_TO_UINT,
  113. OP_STR_TO_FLT,
  114. OP_STR_TO_NONE, // 60
  115. OP_FLT_TO_UINT,
  116. OP_FLT_TO_STR,
  117. OP_FLT_TO_NONE,
  118. OP_UINT_TO_FLT,
  119. OP_UINT_TO_STR,
  120. OP_UINT_TO_NONE,
  121. OP_LOADIMMED_UINT,
  122. OP_LOADIMMED_FLT,
  123. OP_TAG_TO_STR,
  124. OP_LOADIMMED_STR, // 70
  125. OP_DOCBLOCK_STR, // 76
  126. OP_LOADIMMED_IDENT,
  127. OP_CALLFUNC,
  128. OP_ADVANCE_STR,
  129. OP_ADVANCE_STR_APPENDCHAR,
  130. OP_ADVANCE_STR_COMMA,
  131. OP_ADVANCE_STR_NUL,
  132. OP_REWIND_STR,
  133. OP_TERMINATE_REWIND_STR, // 80
  134. OP_COMPARE_STR,
  135. OP_PUSH, // String
  136. OP_PUSH_UINT, // Integer
  137. OP_PUSH_FLT, // Float
  138. OP_PUSH_FRAME, // Frame
  139. OP_ASSERT,
  140. OP_BREAK,
  141. OP_ITER_BEGIN, ///< Prepare foreach iterator.
  142. OP_ITER_BEGIN_STR, ///< Prepare foreach$ iterator.
  143. OP_ITER, ///< Enter foreach loop.
  144. OP_ITER_END, ///< End foreach loop.
  145. OP_INVALID, // 90
  146. MAX_OP_CODELEN ///< The amount of op codes.
  147. };
  148. //------------------------------------------------------------
  149. F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
  150. U32 compileBlock(StmtNode *block, CodeStream &codeStream, U32 ip);
  151. //------------------------------------------------------------
  152. struct CompilerIdentTable
  153. {
  154. struct Entry
  155. {
  156. U32 offset;
  157. U32 ip;
  158. Entry *next;
  159. Entry *nextIdent;
  160. };
  161. Entry *list;
  162. void add(StringTableEntry ste, U32 ip);
  163. void reset();
  164. void write(Stream &st);
  165. };
  166. //------------------------------------------------------------
  167. struct CompilerStringTable
  168. {
  169. U32 totalLen;
  170. struct Entry
  171. {
  172. char *string;
  173. U32 start;
  174. U32 len;
  175. bool tag;
  176. Entry *next;
  177. };
  178. Entry *list;
  179. char buf[256];
  180. std::unordered_map<std::string, Entry*> hashTable;
  181. U32 add(const char *str, bool caseSens = true, bool tag = false);
  182. U32 addIntString(U32 value);
  183. U32 addFloatString(F64 value);
  184. void reset();
  185. char *build();
  186. void write(Stream &st);
  187. };
  188. //------------------------------------------------------------
  189. struct CompilerFloatTable
  190. {
  191. struct Entry
  192. {
  193. F64 val;
  194. Entry *next;
  195. };
  196. U32 count;
  197. Entry *list;
  198. U32 add(F64 value);
  199. void reset();
  200. F64 *build();
  201. void write(Stream &st);
  202. };
  203. //------------------------------------------------------------
  204. inline StringTableEntry CodeToSTE(U32 *code, U32 ip)
  205. {
  206. #ifdef TORQUE_CPU_X64
  207. return (StringTableEntry)(*((U64*)(code + ip)));
  208. #else
  209. return (StringTableEntry)(*(code + ip));
  210. #endif
  211. }
  212. extern void(*STEtoCode)(StringTableEntry ste, U32 ip, U32 *ptr);
  213. void evalSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
  214. void compileSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
  215. CompilerStringTable *getCurrentStringTable();
  216. CompilerStringTable &getGlobalStringTable();
  217. CompilerStringTable &getFunctionStringTable();
  218. void setCurrentStringTable(CompilerStringTable* cst);
  219. CompilerFloatTable *getCurrentFloatTable();
  220. CompilerFloatTable &getGlobalFloatTable();
  221. CompilerFloatTable &getFunctionFloatTable();
  222. void setCurrentFloatTable(CompilerFloatTable* cst);
  223. CompilerIdentTable &getIdentTable();
  224. void precompileIdent(StringTableEntry ident);
  225. /// Helper function to reset the float, string, and ident tables to a base
  226. /// starting state.
  227. void resetTables();
  228. void *consoleAlloc(U32 size);
  229. void consoleAllocReset();
  230. extern bool gSyntaxError;
  231. };
  232. /// Utility class to emit and patch bytecode
  233. class CodeStream
  234. {
  235. public:
  236. enum FixType
  237. {
  238. // For loops
  239. FIXTYPE_LOOPBLOCKSTART,
  240. FIXTYPE_BREAK,
  241. FIXTYPE_CONTINUE
  242. };
  243. enum Constants
  244. {
  245. BlockSize = 16384,
  246. };
  247. protected:
  248. typedef struct PatchEntry
  249. {
  250. U32 addr; ///< Address to patch
  251. U32 value; ///< Value to place at addr
  252. PatchEntry(): addr(0), value(0) { ; }
  253. PatchEntry(U32 a, U32 v) : addr(a), value(v) { ; }
  254. } PatchEntry;
  255. typedef struct CodeData
  256. {
  257. U8 *data; ///< Allocated data (size is BlockSize)
  258. U32 size; ///< Bytes used in data
  259. CodeData *next; ///< Next block
  260. } CodeData;
  261. /// @name Emitted code
  262. /// {
  263. CodeData *mCode;
  264. CodeData *mCodeHead;
  265. U32 mCodePos;
  266. /// }
  267. /// @name Code fixing stacks
  268. /// {
  269. Vector<U32> mFixList;
  270. Vector<U32> mFixStack;
  271. Vector<bool> mFixLoopStack;
  272. Vector<PatchEntry> mPatchList;
  273. /// }
  274. Vector<U32> mBreakLines; ///< Line numbers
  275. public:
  276. CodeStream() : mCode(0), mCodeHead(NULL), mCodePos(0)
  277. {
  278. }
  279. ~CodeStream()
  280. {
  281. reset();
  282. if (mCode)
  283. {
  284. dFree(mCode->data);
  285. delete mCode;
  286. }
  287. }
  288. U8 *allocCode(U32 sz);
  289. inline U32 emit(U32 code)
  290. {
  291. U32 *ptr = (U32*)allocCode(4);
  292. *ptr = code;
  293. #ifdef DEBUG_CODESTREAM
  294. printf("code[%u] = %u\n", mCodePos, code);
  295. #endif
  296. return mCodePos++;
  297. }
  298. inline void patch(U32 addr, U32 code)
  299. {
  300. #ifdef DEBUG_CODESTREAM
  301. printf("patch[%u] = %u\n", addr, code);
  302. #endif
  303. mPatchList.push_back(PatchEntry(addr, code));
  304. }
  305. inline U32 emitSTE(const char *code)
  306. {
  307. U64 *ptr = (U64*)allocCode(8);
  308. *ptr = 0;
  309. Compiler::STEtoCode(code, mCodePos, (U32*)ptr);
  310. #ifdef DEBUG_CODESTREAM
  311. printf("code[%u] = %s\n", mCodePos, code);
  312. #endif
  313. mCodePos += 2;
  314. return mCodePos - 2;
  315. }
  316. inline U32 tell()
  317. {
  318. return mCodePos;
  319. }
  320. inline bool inLoop()
  321. {
  322. for (U32 i = 0; i<mFixLoopStack.size(); i++)
  323. {
  324. if (mFixLoopStack[i])
  325. return true;
  326. }
  327. return false;
  328. }
  329. inline U32 emitFix(FixType type)
  330. {
  331. U32 *ptr = (U32*)allocCode(4);
  332. *ptr = (U32)type;
  333. #ifdef DEBUG_CODESTREAM
  334. printf("code[%u] = [FIX:%u]\n", mCodePos, (U32)type);
  335. #endif
  336. mFixList.push_back(mCodePos);
  337. mFixList.push_back((U32)type);
  338. return mCodePos++;
  339. }
  340. inline void pushFixScope(bool isLoop)
  341. {
  342. mFixStack.push_back(mFixList.size());
  343. mFixLoopStack.push_back(isLoop);
  344. }
  345. inline void popFixScope()
  346. {
  347. AssertFatal(mFixStack.size() > 0, "Fix stack mismatch");
  348. U32 newSize = mFixStack[mFixStack.size() - 1];
  349. while (mFixList.size() > newSize)
  350. mFixList.pop_back();
  351. mFixStack.pop_back();
  352. mFixLoopStack.pop_back();
  353. }
  354. void fixLoop(U32 loopBlockStart, U32 breakPoint, U32 continuePoint);
  355. inline void addBreakLine(U32 lineNumber, U32 ip)
  356. {
  357. mBreakLines.push_back(lineNumber);
  358. mBreakLines.push_back(ip);
  359. }
  360. inline U32 getNumLineBreaks()
  361. {
  362. return mBreakLines.size() / 2;
  363. }
  364. void emitCodeStream(U32 *size, U32 **stream, U32 **lineBreaks);
  365. void reset();
  366. };
  367. #endif