compiler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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);
  137. return newStr->start;
  138. }
  139. U32 CompilerStringTable::addIntString(U32 value)
  140. {
  141. dSprintf(buf, sizeof(buf), "%d", value);
  142. return add(buf);
  143. }
  144. U32 CompilerStringTable::addFloatString(F64 value)
  145. {
  146. dSprintf(buf, sizeof(buf), "%g", value);
  147. return add(buf);
  148. }
  149. void CompilerStringTable::reset()
  150. {
  151. list = NULL;
  152. totalLen = 0;
  153. }
  154. char *CompilerStringTable::build()
  155. {
  156. char *ret = new char[totalLen];
  157. for(Entry *walk = list; walk; walk = walk->next)
  158. dStrcpy(ret + walk->start, walk->string);
  159. return ret;
  160. }
  161. void CompilerStringTable::write(Stream &st)
  162. {
  163. st.write(totalLen);
  164. for(Entry *walk = list; walk; walk = walk->next)
  165. st.write(walk->len, walk->string);
  166. }
  167. //------------------------------------------------------------
  168. U32 CompilerFloatTable::add(F64 value)
  169. {
  170. Entry **walk;
  171. U32 i = 0;
  172. for(walk = &list; *walk; walk = &((*walk)->next), i++)
  173. if(value == (*walk)->val)
  174. return i;
  175. Entry *newFloat = (Entry *) consoleAlloc(sizeof(Entry));
  176. newFloat->val = value;
  177. newFloat->next = NULL;
  178. count++;
  179. *walk = newFloat;
  180. return count-1;
  181. }
  182. void CompilerFloatTable::reset()
  183. {
  184. list = NULL;
  185. count = 0;
  186. }
  187. F64 *CompilerFloatTable::build()
  188. {
  189. F64 *ret = new F64[count];
  190. U32 i = 0;
  191. for(Entry *walk = list; walk; walk = walk->next, i++)
  192. ret[i] = walk->val;
  193. return ret;
  194. }
  195. void CompilerFloatTable::write(Stream &st)
  196. {
  197. st.write(count);
  198. for(Entry *walk = list; walk; walk = walk->next)
  199. st.write(walk->val);
  200. }
  201. //------------------------------------------------------------
  202. void CompilerIdentTable::reset()
  203. {
  204. list = NULL;
  205. }
  206. void CompilerIdentTable::add(StringTableEntry ste, U32 ip)
  207. {
  208. U32 index = gGlobalStringTable.add(ste, false);
  209. Entry *newEntry = (Entry *) consoleAlloc(sizeof(Entry));
  210. newEntry->offset = index;
  211. newEntry->ip = ip;
  212. for(Entry *walk = list; walk; walk = walk->next)
  213. {
  214. if(walk->offset == index)
  215. {
  216. newEntry->nextIdent = walk->nextIdent;
  217. walk->nextIdent = newEntry;
  218. return;
  219. }
  220. }
  221. newEntry->next = list;
  222. list = newEntry;
  223. newEntry->nextIdent = NULL;
  224. }
  225. void CompilerIdentTable::write(Stream &st)
  226. {
  227. U32 count = 0;
  228. Entry * walk;
  229. for(walk = list; walk; walk = walk->next)
  230. count++;
  231. st.write(count);
  232. for(walk = list; walk; walk = walk->next)
  233. {
  234. U32 ec = 0;
  235. Entry * el;
  236. for(el = walk; el; el = el->nextIdent)
  237. ec++;
  238. st.write(walk->offset);
  239. st.write(ec);
  240. for(el = walk; el; el = el->nextIdent)
  241. st.write(el->ip);
  242. }
  243. }
  244. //-------------------------------------------------------------------------
  245. U8 *CodeStream::allocCode(U32 sz)
  246. {
  247. U8 *ptr = NULL;
  248. if (mCodeHead)
  249. {
  250. const U32 bytesLeft = BlockSize - mCodeHead->size;
  251. if (bytesLeft > sz)
  252. {
  253. ptr = mCodeHead->data + mCodeHead->size;
  254. mCodeHead->size += sz;
  255. return ptr;
  256. }
  257. }
  258. CodeData *data = new CodeData;
  259. data->data = (U8*)dMalloc(BlockSize);
  260. data->size = sz;
  261. data->next = NULL;
  262. if (mCodeHead)
  263. mCodeHead->next = data;
  264. mCodeHead = data;
  265. if (mCode == NULL)
  266. mCode = data;
  267. return data->data;
  268. }
  269. //-------------------------------------------------------------------------
  270. void CodeStream::fixLoop(U32 loopBlockStart, U32 breakPoint, U32 continuePoint)
  271. {
  272. AssertFatal(mFixStack.size() > 0, "Fix stack mismatch");
  273. U32 fixStart = mFixStack[mFixStack.size()-1];
  274. for (U32 i=fixStart; i<mFixList.size(); i += 2)
  275. {
  276. FixType type = (FixType)mFixList[i+1];
  277. U32 fixedIp = 0;
  278. bool valid = true;
  279. switch (type)
  280. {
  281. case FIXTYPE_LOOPBLOCKSTART:
  282. fixedIp = loopBlockStart;
  283. break;
  284. case FIXTYPE_BREAK:
  285. fixedIp = breakPoint;
  286. break;
  287. case FIXTYPE_CONTINUE:
  288. fixedIp = continuePoint;
  289. break;
  290. default:
  291. //Con::warnf("Address %u fixed as %u", mFixList[i], mFixList[i+1]);
  292. valid = false;
  293. break;
  294. }
  295. if (valid)
  296. {
  297. patch(mFixList[i], fixedIp);
  298. }
  299. }
  300. }
  301. //-------------------------------------------------------------------------
  302. void CodeStream::emitCodeStream(U32 *size, U32 **stream, U32 **lineBreaks)
  303. {
  304. // Alloc stream
  305. U32 numLineBreaks = getNumLineBreaks();
  306. *stream = new U32[mCodePos + (numLineBreaks * 2)];
  307. dMemset(*stream, '\0', mCodePos + (numLineBreaks * 2));
  308. *size = mCodePos;
  309. // Dump chunks & line breaks
  310. U32 outBytes = mCodePos * sizeof(U32);
  311. U8 *outPtr = *((U8**)stream);
  312. for (CodeData *itr = mCode; itr != NULL; itr = itr->next)
  313. {
  314. U32 bytesToCopy = itr->size > outBytes ? outBytes : itr->size;
  315. dMemcpy(outPtr, itr->data, bytesToCopy);
  316. outPtr += bytesToCopy;
  317. outBytes -= bytesToCopy;
  318. }
  319. *lineBreaks = *stream + mCodePos;
  320. dMemcpy(*lineBreaks, mBreakLines.address(), sizeof(U32) * mBreakLines.size());
  321. // Apply patches on top
  322. for (U32 i=0; i<mPatchList.size(); i++)
  323. {
  324. PatchEntry &e = mPatchList[i];
  325. (*stream)[e.addr] = e.value;
  326. }
  327. }
  328. //-------------------------------------------------------------------------
  329. void CodeStream::reset()
  330. {
  331. mCodePos = 0;
  332. mFixStack.clear();
  333. mFixLoopStack.clear();
  334. mFixList.clear();
  335. mBreakLines.clear();
  336. // Pop down to one code block
  337. CodeData *itr = mCode ? mCode->next : NULL;
  338. while (itr != NULL)
  339. {
  340. CodeData *next = itr->next;
  341. dFree(itr->data);
  342. delete(itr);
  343. itr = next;
  344. }
  345. if (mCode)
  346. {
  347. mCode->size = 0;
  348. mCode->next = NULL;
  349. mCodeHead = mCode;
  350. }
  351. }