compiler.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. CodeBlock *gCurBreakBlock;
  56. //------------------------------------------------------------
  57. CodeBlock *getBreakCodeBlock() { return gCurBreakBlock; }
  58. void setBreakCodeBlock(CodeBlock *cb) { gCurBreakBlock = cb; }
  59. //------------------------------------------------------------
  60. #ifdef TORQUE_CPU_X64
  61. // Fixed unsafe conversion from pointer to U32. @todo x64 revise
  62. U32 u32toSTEId = 0;
  63. typedef Map< U32, StringTableEntry > U32toSteMap;
  64. U32toSteMap u32toSTEMap;
  65. StringTableEntry U32toSTE( U32 u )
  66. {
  67. // @todo x64 Added thread-safe convertion.
  68. const U32toSteMap::Iterator result = u32toSTEMap.find( u );
  69. AssertFatal( result != u32toSTEMap.end( ),
  70. "Don't converted U32 to STE. See evalSTEtoU32()." );
  71. return result->value;
  72. }
  73. U32 evalSTEtoU32( StringTableEntry ste, U32 )
  74. {
  75. // @todo x64 Added thread-safe convertion.
  76. u32toSTEMap.insert( u32toSTEId++, ste );
  77. return (u32toSTEId - 1); // pointer to inserted
  78. }
  79. #else
  80. StringTableEntry U32toSTE(U32 u)
  81. {
  82. return *((StringTableEntry *) &u);
  83. }
  84. U32 evalSTEtoU32(StringTableEntry ste, U32)
  85. {
  86. return *((U32 *) &ste);
  87. }
  88. #endif
  89. U32 compileSTEtoU32(StringTableEntry ste, U32 ip)
  90. {
  91. if(ste)
  92. getIdentTable().add(ste, ip);
  93. return 0;
  94. }
  95. U32 (*STEtoU32)(StringTableEntry ste, U32 ip) = evalSTEtoU32;
  96. //------------------------------------------------------------
  97. bool gSyntaxError = false;
  98. //------------------------------------------------------------
  99. CompilerStringTable *getCurrentStringTable() { return gCurrentStringTable; }
  100. CompilerStringTable &getGlobalStringTable() { return gGlobalStringTable; }
  101. CompilerStringTable &getFunctionStringTable() { return gFunctionStringTable; }
  102. void setCurrentStringTable (CompilerStringTable* cst) { gCurrentStringTable = cst; }
  103. CompilerFloatTable *getCurrentFloatTable() { return gCurrentFloatTable; }
  104. CompilerFloatTable &getGlobalFloatTable() { return gGlobalFloatTable; }
  105. CompilerFloatTable &getFunctionFloatTable() { return gFunctionFloatTable; }
  106. void setCurrentFloatTable (CompilerFloatTable* cst) { gCurrentFloatTable = cst; }
  107. CompilerIdentTable &getIdentTable() { return gIdentTable; }
  108. void precompileIdent(StringTableEntry ident)
  109. {
  110. if(ident)
  111. gGlobalStringTable.add(ident);
  112. }
  113. void resetTables()
  114. {
  115. setCurrentStringTable(&gGlobalStringTable);
  116. setCurrentFloatTable(&gGlobalFloatTable);
  117. getGlobalFloatTable().reset();
  118. getGlobalStringTable().reset();
  119. getFunctionFloatTable().reset();
  120. getFunctionStringTable().reset();
  121. getIdentTable().reset();
  122. }
  123. void *consoleAlloc(U32 size) { return gConsoleAllocator.alloc(size); }
  124. void consoleAllocReset() { gConsoleAllocator.freeBlocks(); }
  125. }
  126. //-------------------------------------------------------------------------
  127. using namespace Compiler;
  128. //-------------------------------------------------------------------------
  129. U32 CompilerStringTable::add(const char *str, bool caseSens, bool tag)
  130. {
  131. // Is it already in?
  132. Entry **walk;
  133. for(walk = &list; *walk; walk = &((*walk)->next))
  134. {
  135. if((*walk)->tag != tag)
  136. continue;
  137. if(caseSens)
  138. {
  139. if(!dStrcmp((*walk)->string, str))
  140. return (*walk)->start;
  141. }
  142. else
  143. {
  144. if(!dStricmp((*walk)->string, str))
  145. return (*walk)->start;
  146. }
  147. }
  148. // Write it out.
  149. Entry *newStr = (Entry *) consoleAlloc(sizeof(Entry));
  150. *walk = newStr;
  151. newStr->next = NULL;
  152. newStr->start = totalLen;
  153. U32 len = dStrlen(str) + 1;
  154. if(tag && len < 7) // alloc space for the numeric tag 1 for tag, 5 for # and 1 for nul
  155. len = 7;
  156. totalLen += len;
  157. newStr->string = (char *) consoleAlloc(len);
  158. newStr->len = len;
  159. newStr->tag = tag;
  160. dStrcpy(newStr->string, str);
  161. return newStr->start;
  162. }
  163. U32 CompilerStringTable::addIntString(U32 value)
  164. {
  165. dSprintf(buf, sizeof(buf), "%d", value);
  166. return add(buf);
  167. }
  168. U32 CompilerStringTable::addFloatString(F64 value)
  169. {
  170. dSprintf(buf, sizeof(buf), "%g", value);
  171. return add(buf);
  172. }
  173. void CompilerStringTable::reset()
  174. {
  175. list = NULL;
  176. totalLen = 0;
  177. }
  178. char *CompilerStringTable::build()
  179. {
  180. char *ret = new char[totalLen];
  181. for(Entry *walk = list; walk; walk = walk->next)
  182. dStrcpy(ret + walk->start, walk->string);
  183. return ret;
  184. }
  185. void CompilerStringTable::write(Stream &st)
  186. {
  187. st.write(totalLen);
  188. for(Entry *walk = list; walk; walk = walk->next)
  189. st.write(walk->len, walk->string);
  190. }
  191. //------------------------------------------------------------
  192. U32 CompilerFloatTable::add(F64 value)
  193. {
  194. Entry **walk;
  195. U32 i = 0;
  196. for(walk = &list; *walk; walk = &((*walk)->next), i++)
  197. if(value == (*walk)->val)
  198. return i;
  199. Entry *newFloat = (Entry *) consoleAlloc(sizeof(Entry));
  200. newFloat->val = value;
  201. newFloat->next = NULL;
  202. count++;
  203. *walk = newFloat;
  204. return count-1;
  205. }
  206. void CompilerFloatTable::reset()
  207. {
  208. list = NULL;
  209. count = 0;
  210. }
  211. F64 *CompilerFloatTable::build()
  212. {
  213. F64 *ret = new F64[count];
  214. U32 i = 0;
  215. for(Entry *walk = list; walk; walk = walk->next, i++)
  216. ret[i] = walk->val;
  217. return ret;
  218. }
  219. void CompilerFloatTable::write(Stream &st)
  220. {
  221. st.write(count);
  222. for(Entry *walk = list; walk; walk = walk->next)
  223. st.write(walk->val);
  224. }
  225. //------------------------------------------------------------
  226. void CompilerIdentTable::reset()
  227. {
  228. list = NULL;
  229. }
  230. void CompilerIdentTable::add(StringTableEntry ste, U32 ip)
  231. {
  232. U32 index = gGlobalStringTable.add(ste, false);
  233. Entry *newEntry = (Entry *) consoleAlloc(sizeof(Entry));
  234. newEntry->offset = index;
  235. newEntry->ip = ip;
  236. for(Entry *walk = list; walk; walk = walk->next)
  237. {
  238. if(walk->offset == index)
  239. {
  240. newEntry->nextIdent = walk->nextIdent;
  241. walk->nextIdent = newEntry;
  242. return;
  243. }
  244. }
  245. newEntry->next = list;
  246. list = newEntry;
  247. newEntry->nextIdent = NULL;
  248. }
  249. void CompilerIdentTable::write(Stream &st)
  250. {
  251. U32 count = 0;
  252. Entry * walk;
  253. for(walk = list; walk; walk = walk->next)
  254. count++;
  255. st.write(count);
  256. for(walk = list; walk; walk = walk->next)
  257. {
  258. U32 ec = 0;
  259. Entry * el;
  260. for(el = walk; el; el = el->nextIdent)
  261. ec++;
  262. st.write(walk->offset);
  263. st.write(ec);
  264. for(el = walk; el; el = el->nextIdent)
  265. st.write(el->ip);
  266. }
  267. }