compiler.h 9.9 KB

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