compiler.cpp 7.9 KB

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