compiler.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "platform/event.h"
  26. #include "console/ast.h"
  27. #include "core/tAlgorithm.h"
  28. #include "core/strings/findMatch.h"
  29. #include "console/consoleInternal.h"
  30. #include "core/stream/fileStream.h"
  31. #include "console/compiler.h"
  32. #include "console/simBase.h"
  33. namespace Compiler
  34. {
  35. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  36. {
  37. F64 val = dAtof(str);
  38. if(val != 0)
  39. return val;
  40. else if(!dStricmp(str, "true"))
  41. return 1;
  42. else if(!dStricmp(str, "false"))
  43. return 0;
  44. else if(file)
  45. {
  46. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  47. return 0;
  48. }
  49. return 0;
  50. }
  51. //------------------------------------------------------------
  52. CompilerStringTable *gCurrentStringTable, gGlobalStringTable, gFunctionStringTable;
  53. CompilerFloatTable *gCurrentFloatTable, gGlobalFloatTable, gFunctionFloatTable;
  54. DataChunker gConsoleAllocator;
  55. CompilerIdentTable gIdentTable;
  56. CodeBlock *gCurBreakBlock;
  57. //------------------------------------------------------------
  58. CodeBlock *getBreakCodeBlock() { return gCurBreakBlock; }
  59. void setBreakCodeBlock(CodeBlock *cb) { gCurBreakBlock = cb; }
  60. //------------------------------------------------------------
  61. U32 evalSTEtoU32(StringTableEntry ste, U32)
  62. {
  63. return *((U32 *) &ste);
  64. }
  65. U32 compileSTEtoU32(StringTableEntry ste, U32 ip)
  66. {
  67. if(ste)
  68. getIdentTable().add(ste, ip);
  69. return 0;
  70. }
  71. U32 (*STEtoU32)(StringTableEntry ste, U32 ip) = evalSTEtoU32;
  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. }