compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_DEC,
  85. OP_SETCURVAR,
  86. OP_SETCURVAR_CREATE,
  87. OP_SETCURVAR_ARRAY,
  88. OP_SETCURVAR_ARRAY_VARLOOKUP,
  89. OP_SETCURVAR_ARRAY_CREATE,
  90. OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP,
  91. OP_LOADVAR_UINT,// 40
  92. OP_LOADVAR_FLT,
  93. OP_LOADVAR_STR,
  94. OP_LOADVAR_VAR,
  95. OP_SAVEVAR_UINT,
  96. OP_SAVEVAR_FLT,
  97. OP_SAVEVAR_STR,
  98. OP_SAVEVAR_VAR,
  99. OP_SETCUROBJECT,
  100. OP_SETCUROBJECT_NEW,
  101. OP_SETCUROBJECT_INTERNAL,
  102. OP_SETCURFIELD,
  103. OP_SETCURFIELD_ARRAY, // 50
  104. OP_SETCURFIELD_TYPE,
  105. OP_SETCURFIELD_ARRAY_VAR,
  106. OP_SETCURFIELD_THIS,
  107. OP_LOADFIELD_UINT,
  108. OP_LOADFIELD_FLT,
  109. OP_LOADFIELD_STR,
  110. OP_SAVEFIELD_UINT,
  111. OP_SAVEFIELD_FLT,
  112. OP_SAVEFIELD_STR,
  113. OP_STR_TO_UINT,
  114. OP_STR_TO_FLT,
  115. OP_STR_TO_NONE, // 60
  116. OP_FLT_TO_UINT,
  117. OP_FLT_TO_STR,
  118. OP_FLT_TO_NONE,
  119. OP_UINT_TO_FLT,
  120. OP_UINT_TO_STR,
  121. OP_UINT_TO_NONE,
  122. OP_COPYVAR_TO_NONE,
  123. OP_LOADIMMED_UINT,
  124. OP_LOADIMMED_FLT,
  125. OP_TAG_TO_STR,
  126. OP_LOADIMMED_STR, // 70
  127. OP_DOCBLOCK_STR,
  128. OP_LOADIMMED_IDENT,
  129. OP_CALLFUNC_RESOLVE,
  130. OP_CALLFUNC,
  131. OP_CALLFUNC_POINTER,
  132. OP_CALLFUNC_THIS,
  133. OP_ADVANCE_STR,
  134. OP_ADVANCE_STR_APPENDCHAR,
  135. OP_ADVANCE_STR_COMMA,
  136. OP_ADVANCE_STR_NUL,
  137. OP_REWIND_STR,
  138. OP_TERMINATE_REWIND_STR, // 80
  139. OP_COMPARE_STR,
  140. OP_PUSH, // String
  141. OP_PUSH_UINT, // Integer
  142. OP_PUSH_FLT, // Float
  143. OP_PUSH_VAR, // Variable
  144. OP_PUSH_THIS, // This pointer
  145. OP_PUSH_FRAME, // Frame
  146. OP_ASSERT,
  147. OP_BREAK,
  148. OP_ITER_BEGIN, ///< Prepare foreach iterator.
  149. OP_ITER_BEGIN_STR, ///< Prepare foreach$ iterator.
  150. OP_ITER, ///< Enter foreach loop.
  151. OP_ITER_END, ///< End foreach loop.
  152. OP_INVALID, // 90
  153. MAX_OP_CODELEN ///< The amount of op codes.
  154. };
  155. //------------------------------------------------------------
  156. F64 consoleStringToNumber(const char *str, StringTableEntry file = 0, U32 line = 0);
  157. U32 compileBlock(StmtNode *block, CodeStream &codeStream, U32 ip);
  158. //------------------------------------------------------------
  159. struct CompilerIdentTable
  160. {
  161. struct Entry
  162. {
  163. U32 offset;
  164. U32 ip;
  165. Entry *next;
  166. Entry *nextIdent;
  167. };
  168. Entry *list;
  169. void add(StringTableEntry ste, U32 ip);
  170. void reset();
  171. void write(Stream &st);
  172. };
  173. //------------------------------------------------------------
  174. struct CompilerStringTable
  175. {
  176. U32 totalLen;
  177. struct Entry
  178. {
  179. char *string;
  180. U32 start;
  181. U32 len;
  182. bool tag;
  183. Entry *next;
  184. };
  185. Entry *list;
  186. char buf[256];
  187. std::unordered_map<std::string, Entry*> hashTable;
  188. U32 add(const char *str, bool caseSens = true, bool tag = false);
  189. U32 addIntString(U32 value);
  190. U32 addFloatString(F64 value);
  191. void reset();
  192. char *build();
  193. void write(Stream &st);
  194. };
  195. //------------------------------------------------------------
  196. struct CompilerFloatTable
  197. {
  198. struct Entry
  199. {
  200. F64 val;
  201. Entry *next;
  202. };
  203. U32 count;
  204. Entry *list;
  205. U32 add(F64 value);
  206. void reset();
  207. F64 *build();
  208. void write(Stream &st);
  209. };
  210. //------------------------------------------------------------
  211. inline StringTableEntry CodeToSTE(U32 *code, U32 ip)
  212. {
  213. #ifdef TORQUE_CPU_X64
  214. return (StringTableEntry)(*((U64*)(code + ip)));
  215. #else
  216. return (StringTableEntry)(*(code + ip));
  217. #endif
  218. }
  219. extern void(*STEtoCode)(StringTableEntry ste, U32 ip, U32 *ptr);
  220. void evalSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
  221. void compileSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr);
  222. CompilerStringTable *getCurrentStringTable();
  223. CompilerStringTable &getGlobalStringTable();
  224. CompilerStringTable &getFunctionStringTable();
  225. void setCurrentStringTable(CompilerStringTable* cst);
  226. CompilerFloatTable *getCurrentFloatTable();
  227. CompilerFloatTable &getGlobalFloatTable();
  228. CompilerFloatTable &getFunctionFloatTable();
  229. void setCurrentFloatTable(CompilerFloatTable* cst);
  230. CompilerIdentTable &getIdentTable();
  231. void precompileIdent(StringTableEntry ident);
  232. /// Helper function to reset the float, string, and ident tables to a base
  233. /// starting state.
  234. void resetTables();
  235. void *consoleAlloc(U32 size);
  236. void consoleAllocReset();
  237. extern bool gSyntaxError;
  238. };
  239. /// Utility class to emit and patch bytecode
  240. class CodeStream
  241. {
  242. public:
  243. enum FixType
  244. {
  245. // For loops
  246. FIXTYPE_LOOPBLOCKSTART,
  247. FIXTYPE_BREAK,
  248. FIXTYPE_CONTINUE
  249. };
  250. enum Constants
  251. {
  252. BlockSize = 16384,
  253. };
  254. protected:
  255. typedef struct PatchEntry
  256. {
  257. U32 addr; ///< Address to patch
  258. U32 value; ///< Value to place at addr
  259. PatchEntry() { ; }
  260. PatchEntry(U32 a, U32 v) : addr(a), value(v) { ; }
  261. } PatchEntry;
  262. typedef struct CodeData
  263. {
  264. U8 *data; ///< Allocated data (size is BlockSize)
  265. U32 size; ///< Bytes used in data
  266. CodeData *next; ///< Next block
  267. } CodeData;
  268. /// @name Emitted code
  269. /// {
  270. CodeData *mCode;
  271. CodeData *mCodeHead;
  272. U32 mCodePos;
  273. /// }
  274. /// @name Code fixing stacks
  275. /// {
  276. Vector<U32> mFixList;
  277. Vector<U32> mFixStack;
  278. Vector<bool> mFixLoopStack;
  279. Vector<PatchEntry> mPatchList;
  280. /// }
  281. Vector<U32> mBreakLines; ///< Line numbers
  282. public:
  283. CodeStream() : mCode(0), mCodeHead(NULL), mCodePos(0)
  284. {
  285. }
  286. ~CodeStream()
  287. {
  288. reset();
  289. if (mCode)
  290. {
  291. dFree(mCode->data);
  292. delete mCode;
  293. }
  294. }
  295. U8 *allocCode(U32 sz);
  296. inline U32 emit(U32 code)
  297. {
  298. U32 *ptr = (U32*)allocCode(4);
  299. *ptr = code;
  300. #ifdef DEBUG_CODESTREAM
  301. printf("code[%u] = %u\n", mCodePos, code);
  302. #endif
  303. return mCodePos++;
  304. }
  305. inline void patch(U32 addr, U32 code)
  306. {
  307. #ifdef DEBUG_CODESTREAM
  308. printf("patch[%u] = %u\n", addr, code);
  309. #endif
  310. mPatchList.push_back(PatchEntry(addr, code));
  311. }
  312. inline U32 emitSTE(const char *code)
  313. {
  314. U64 *ptr = (U64*)allocCode(8);
  315. *ptr = 0;
  316. Compiler::STEtoCode(code, mCodePos, (U32*)ptr);
  317. #ifdef DEBUG_CODESTREAM
  318. printf("code[%u] = %s\n", mCodePos, code);
  319. #endif
  320. mCodePos += 2;
  321. return mCodePos - 2;
  322. }
  323. inline U32 tell()
  324. {
  325. return mCodePos;
  326. }
  327. inline bool inLoop()
  328. {
  329. for (U32 i = 0; i<mFixLoopStack.size(); i++)
  330. {
  331. if (mFixLoopStack[i])
  332. return true;
  333. }
  334. return false;
  335. }
  336. inline U32 emitFix(FixType type)
  337. {
  338. U32 *ptr = (U32*)allocCode(4);
  339. *ptr = (U32)type;
  340. #ifdef DEBUG_CODESTREAM
  341. printf("code[%u] = [FIX:%u]\n", mCodePos, (U32)type);
  342. #endif
  343. mFixList.push_back(mCodePos);
  344. mFixList.push_back((U32)type);
  345. return mCodePos++;
  346. }
  347. inline void pushFixScope(bool isLoop)
  348. {
  349. mFixStack.push_back(mFixList.size());
  350. mFixLoopStack.push_back(isLoop);
  351. }
  352. inline void popFixScope()
  353. {
  354. AssertFatal(mFixStack.size() > 0, "Fix stack mismatch");
  355. U32 newSize = mFixStack[mFixStack.size() - 1];
  356. while (mFixList.size() > newSize)
  357. mFixList.pop_back();
  358. mFixStack.pop_back();
  359. mFixLoopStack.pop_back();
  360. }
  361. void fixLoop(U32 loopBlockStart, U32 breakPoint, U32 continuePoint);
  362. inline void addBreakLine(U32 lineNumber, U32 ip)
  363. {
  364. mBreakLines.push_back(lineNumber);
  365. mBreakLines.push_back(ip);
  366. }
  367. inline U32 getNumLineBreaks()
  368. {
  369. return mBreakLines.size() / 2;
  370. }
  371. void emitCodeStream(U32 *size, U32 **stream, U32 **lineBreaks);
  372. void reset();
  373. };
  374. #endif