compiler.h 9.6 KB

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