compiler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "console/telnetDebugger.h"
  25. #include "console/ast.h"
  26. #include "core/tAlgorithm.h"
  27. #include "core/strings/findMatch.h"
  28. #include "console/consoleInternal.h"
  29. #include "core/stream/fileStream.h"
  30. #include "console/compiler.h"
  31. #include "console/simBase.h"
  32. namespace Compiler
  33. {
  34. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  35. {
  36. F64 val = dAtof(str);
  37. if (val != 0)
  38. return val;
  39. else if (!dStricmp(str, "true"))
  40. return 1;
  41. else if (!dStricmp(str, "false"))
  42. return 0;
  43. else if (file)
  44. {
  45. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  46. return 0;
  47. }
  48. return 0;
  49. }
  50. //------------------------------------------------------------
  51. CompilerStringTable *gCurrentStringTable, gGlobalStringTable, gFunctionStringTable;
  52. CompilerFloatTable *gCurrentFloatTable, gGlobalFloatTable, gFunctionFloatTable;
  53. DataChunker gConsoleAllocator;
  54. CompilerIdentTable gIdentTable;
  55. //------------------------------------------------------------
  56. void evalSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr)
  57. {
  58. #ifdef TORQUE_CPU_X64
  59. *(U64*)(ptr) = (U64)ste;
  60. #else
  61. *ptr = (U32)ste;
  62. #endif
  63. }
  64. void compileSTEtoCode(StringTableEntry ste, U32 ip, U32 *ptr)
  65. {
  66. if (ste)
  67. getIdentTable().add(ste, ip);
  68. *ptr = 0;
  69. *(ptr + 1) = 0;
  70. }
  71. void(*STEtoCode)(StringTableEntry ste, U32 ip, U32 *ptr) = evalSTEtoCode;
  72. //------------------------------------------------------------
  73. bool gSyntaxError = false;
  74. //------------------------------------------------------------
  75. CompilerStringTable *getCurrentStringTable() { return gCurrentStringTable; }
  76. CompilerStringTable &getGlobalStringTable() { return gGlobalStringTable; }
  77. CompilerStringTable &getFunctionStringTable() { return gFunctionStringTable; }
  78. void setCurrentStringTable(CompilerStringTable* cst) { gCurrentStringTable = cst; }
  79. CompilerFloatTable *getCurrentFloatTable() { return gCurrentFloatTable; }
  80. CompilerFloatTable &getGlobalFloatTable() { return gGlobalFloatTable; }
  81. CompilerFloatTable &getFunctionFloatTable() { return gFunctionFloatTable; }
  82. void setCurrentFloatTable(CompilerFloatTable* cst) { gCurrentFloatTable = cst; }
  83. CompilerIdentTable &getIdentTable() { return gIdentTable; }
  84. void precompileIdent(StringTableEntry ident)
  85. {
  86. if (ident)
  87. gGlobalStringTable.add(ident);
  88. }
  89. void resetTables()
  90. {
  91. setCurrentStringTable(&gGlobalStringTable);
  92. setCurrentFloatTable(&gGlobalFloatTable);
  93. getGlobalFloatTable().reset();
  94. getGlobalStringTable().reset();
  95. getFunctionFloatTable().reset();
  96. getFunctionStringTable().reset();
  97. getIdentTable().reset();
  98. }
  99. void *consoleAlloc(U32 size) { return gConsoleAllocator.alloc(size); }
  100. void consoleAllocReset() { gConsoleAllocator.freeBlocks(); }
  101. }
  102. //-------------------------------------------------------------------------
  103. using namespace Compiler;
  104. //-------------------------------------------------------------------------
  105. U32 CompilerStringTable::add(const char *str, bool caseSens, bool tag)
  106. {
  107. // Is it already in?
  108. Entry **walk;
  109. for (walk = &list; *walk; walk = &((*walk)->next))
  110. {
  111. if ((*walk)->tag != tag)
  112. continue;
  113. if (caseSens)
  114. {
  115. if (!dStrcmp((*walk)->string, str))
  116. return (*walk)->start;
  117. }
  118. else
  119. {
  120. if (!dStricmp((*walk)->string, str))
  121. return (*walk)->start;
  122. }
  123. }
  124. // Write it out.
  125. Entry *newStr = (Entry *)consoleAlloc(sizeof(Entry));
  126. *walk = newStr;
  127. newStr->next = NULL;
  128. newStr->start = totalLen;
  129. U32 len = dStrlen(str) + 1;
  130. if (tag && len < 7) // alloc space for the numeric tag 1 for tag, 5 for # and 1 for nul
  131. len = 7;
  132. totalLen += len;
  133. newStr->string = (char *)consoleAlloc(len);
  134. newStr->len = len;
  135. newStr->tag = tag;
  136. dStrcpy(newStr->string, str, len);
  137. // Put into the hash table.
  138. hashTable[str] = newStr;
  139. return newStr->start;
  140. }
  141. U32 CompilerStringTable::addIntString(U32 value)
  142. {
  143. dSprintf(buf, sizeof(buf), "%d", value);
  144. return add(buf);
  145. }
  146. U32 CompilerStringTable::addFloatString(F64 value)
  147. {
  148. dSprintf(buf, sizeof(buf), "%g", value);
  149. return add(buf);
  150. }
  151. void CompilerStringTable::reset()
  152. {
  153. list = NULL;
  154. totalLen = 0;
  155. }
  156. char *CompilerStringTable::build()
  157. {
  158. char *ret = new char[totalLen];
  159. dMemset(ret, 0, totalLen);
  160. for (Entry *walk = list; walk; walk = walk->next)
  161. dStrcpy(ret + walk->start, walk->string, totalLen - walk->start);
  162. return ret;
  163. }
  164. void CompilerStringTable::write(Stream &st)
  165. {
  166. st.write(totalLen);
  167. for (Entry *walk = list; walk; walk = walk->next)
  168. st.write(walk->len, walk->string);
  169. }
  170. //------------------------------------------------------------
  171. U32 CompilerFloatTable::add(F64 value)
  172. {
  173. Entry **walk;
  174. U32 i = 0;
  175. for (walk = &list; *walk; walk = &((*walk)->next), i++)
  176. if (value == (*walk)->val)
  177. return i;
  178. Entry *newFloat = (Entry *)consoleAlloc(sizeof(Entry));
  179. newFloat->val = value;
  180. newFloat->next = NULL;
  181. count++;
  182. *walk = newFloat;
  183. return count - 1;
  184. }
  185. void CompilerFloatTable::reset()
  186. {
  187. list = NULL;
  188. count = 0;
  189. }
  190. F64 *CompilerFloatTable::build()
  191. {
  192. F64 *ret = new F64[count];
  193. U32 i = 0;
  194. for (Entry *walk = list; walk; walk = walk->next, i++)
  195. ret[i] = walk->val;
  196. return ret;
  197. }
  198. void CompilerFloatTable::write(Stream &st)
  199. {
  200. st.write(count);
  201. for (Entry *walk = list; walk; walk = walk->next)
  202. st.write(walk->val);
  203. }
  204. //------------------------------------------------------------
  205. void CompilerIdentTable::reset()
  206. {
  207. list = NULL;
  208. }
  209. void CompilerIdentTable::add(StringTableEntry ste, U32 ip)
  210. {
  211. U32 index = gGlobalStringTable.add(ste, false);
  212. Entry *newEntry = (Entry *)consoleAlloc(sizeof(Entry));
  213. newEntry->offset = index;
  214. newEntry->ip = ip;
  215. for (Entry *walk = list; walk; walk = walk->next)
  216. {
  217. if (walk->offset == index)
  218. {
  219. newEntry->nextIdent = walk->nextIdent;
  220. walk->nextIdent = newEntry;
  221. return;
  222. }
  223. }
  224. newEntry->next = list;
  225. list = newEntry;
  226. newEntry->nextIdent = NULL;
  227. }
  228. void CompilerIdentTable::write(Stream &st)
  229. {
  230. U32 count = 0;
  231. Entry * walk;
  232. for (walk = list; walk; walk = walk->next)
  233. count++;
  234. st.write(count);
  235. for (walk = list; walk; walk = walk->next)
  236. {
  237. U32 ec = 0;
  238. Entry * el;
  239. for (el = walk; el; el = el->nextIdent)
  240. ec++;
  241. st.write(walk->offset);
  242. st.write(ec);
  243. for (el = walk; el; el = el->nextIdent)
  244. st.write(el->ip);
  245. }
  246. }
  247. //-------------------------------------------------------------------------
  248. U8 *CodeStream::allocCode(U32 sz)
  249. {
  250. U8 *ptr = NULL;
  251. if (mCodeHead)
  252. {
  253. const U32 bytesLeft = BlockSize - mCodeHead->size;
  254. if (bytesLeft > sz)
  255. {
  256. ptr = mCodeHead->data + mCodeHead->size;
  257. mCodeHead->size += sz;
  258. return ptr;
  259. }
  260. }
  261. CodeData *data = new CodeData;
  262. data->data = (U8*)dMalloc(BlockSize);
  263. data->size = sz;
  264. data->next = NULL;
  265. if (mCodeHead)
  266. mCodeHead->next = data;
  267. mCodeHead = data;
  268. if (mCode == NULL)
  269. mCode = data;
  270. return data->data;
  271. }
  272. //-------------------------------------------------------------------------
  273. void CodeStream::fixLoop(U32 loopBlockStart, U32 breakPoint, U32 continuePoint)
  274. {
  275. AssertFatal(mFixStack.size() > 0, "Fix stack mismatch");
  276. U32 fixStart = mFixStack[mFixStack.size() - 1];
  277. for (U32 i = fixStart; i<mFixList.size(); i += 2)
  278. {
  279. FixType type = (FixType)mFixList[i + 1];
  280. U32 fixedIp = 0;
  281. bool valid = true;
  282. switch (type)
  283. {
  284. case FIXTYPE_LOOPBLOCKSTART:
  285. fixedIp = loopBlockStart;
  286. break;
  287. case FIXTYPE_BREAK:
  288. fixedIp = breakPoint;
  289. break;
  290. case FIXTYPE_CONTINUE:
  291. fixedIp = continuePoint;
  292. break;
  293. default:
  294. //Con::warnf("Address %u fixed as %u", mFixList[i], mFixList[i+1]);
  295. valid = false;
  296. break;
  297. }
  298. if (valid)
  299. {
  300. patch(mFixList[i], fixedIp);
  301. }
  302. }
  303. }
  304. //-------------------------------------------------------------------------
  305. void CodeStream::emitCodeStream(U32 *size, U32 **stream, U32 **lineBreaks)
  306. {
  307. // Alloc stream
  308. U32 numLineBreaks = getNumLineBreaks();
  309. *stream = new U32[mCodePos + (numLineBreaks * 2)];
  310. dMemset(*stream, '\0', mCodePos + (numLineBreaks * 2));
  311. *size = mCodePos;
  312. // Dump chunks & line breaks
  313. U32 outBytes = mCodePos * sizeof(U32);
  314. U8 *outPtr = *((U8**)stream);
  315. for (CodeData *itr = mCode; itr != NULL; itr = itr->next)
  316. {
  317. U32 bytesToCopy = itr->size > outBytes ? outBytes : itr->size;
  318. dMemcpy(outPtr, itr->data, bytesToCopy);
  319. outPtr += bytesToCopy;
  320. outBytes -= bytesToCopy;
  321. }
  322. *lineBreaks = *stream + mCodePos;
  323. dMemcpy(*lineBreaks, mBreakLines.address(), sizeof(U32) * mBreakLines.size());
  324. // Apply patches on top
  325. for (U32 i = 0; i<mPatchList.size(); i++)
  326. {
  327. PatchEntry &e = mPatchList[i];
  328. (*stream)[e.addr] = e.value;
  329. }
  330. }
  331. //-------------------------------------------------------------------------
  332. void CodeStream::reset()
  333. {
  334. mCodePos = 0;
  335. mFixStack.clear();
  336. mFixLoopStack.clear();
  337. mFixList.clear();
  338. mBreakLines.clear();
  339. // Pop down to one code block
  340. CodeData *itr = mCode ? mCode->next : NULL;
  341. while (itr != NULL)
  342. {
  343. CodeData *next = itr->next;
  344. dFree(itr->data);
  345. delete(itr);
  346. itr = next;
  347. }
  348. if (mCode)
  349. {
  350. mCode->size = 0;
  351. mCode->next = NULL;
  352. mCodeHead = mCode;
  353. }
  354. }