compiler.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "debug/telnetDebugger.h"
  25. #include "platform/event.h"
  26. #include "console/ast.h"
  27. #include "collection/findIterator.h"
  28. #include "io/resource/resourceManager.h"
  29. #include "string/findMatch.h"
  30. #include "console/consoleInternal.h"
  31. #include "io/fileStream.h"
  32. #include "console/compiler.h"
  33. #include "sim/simBase.h"
  34. namespace Compiler
  35. {
  36. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  37. {
  38. F64 val = dAtof(str);
  39. if(val != 0)
  40. return val;
  41. else if(!dStricmp(str, "true"))
  42. return 1;
  43. else if(!dStricmp(str, "false"))
  44. return 0;
  45. else if(file)
  46. {
  47. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  48. return 0;
  49. }
  50. return 0;
  51. }
  52. //------------------------------------------------------------
  53. CompilerStringTable *gCurrentStringTable, gGlobalStringTable, gFunctionStringTable;
  54. CompilerFloatTable *gCurrentFloatTable, gGlobalFloatTable, gFunctionFloatTable;
  55. DataChunker gConsoleAllocator;
  56. CompilerIdentTable gIdentTable;
  57. CodeBlock *gCurBreakBlock;
  58. //------------------------------------------------------------
  59. CodeBlock *getBreakCodeBlock() { return gCurBreakBlock; }
  60. void setBreakCodeBlock(CodeBlock *cb) { gCurBreakBlock = cb; }
  61. //------------------------------------------------------------
  62. U32 evalSTEtoU32(StringTableEntry ste, U32)
  63. {
  64. return *((U32 *) &ste);
  65. }
  66. U32 compileSTEtoU32(StringTableEntry ste, U32 ip)
  67. {
  68. if(ste)
  69. getIdentTable().add(ste, ip);
  70. return 0;
  71. }
  72. U32 (*STEtoU32)(StringTableEntry ste, U32 ip) = evalSTEtoU32;
  73. //------------------------------------------------------------
  74. bool gSyntaxError = false;
  75. //------------------------------------------------------------
  76. CompilerStringTable *getCurrentStringTable() { return gCurrentStringTable; }
  77. CompilerStringTable &getGlobalStringTable() { return gGlobalStringTable; }
  78. CompilerStringTable &getFunctionStringTable() { return gFunctionStringTable; }
  79. void setCurrentStringTable (CompilerStringTable* cst) { gCurrentStringTable = cst; }
  80. CompilerFloatTable *getCurrentFloatTable() { return gCurrentFloatTable; }
  81. CompilerFloatTable &getGlobalFloatTable() { return gGlobalFloatTable; }
  82. CompilerFloatTable &getFunctionFloatTable() { return gFunctionFloatTable; }
  83. void setCurrentFloatTable (CompilerFloatTable* cst) { gCurrentFloatTable = cst; }
  84. CompilerIdentTable &getIdentTable() { return gIdentTable; }
  85. void precompileIdent(StringTableEntry ident)
  86. {
  87. if(ident)
  88. gGlobalStringTable.add(ident);
  89. }
  90. void resetTables()
  91. {
  92. setCurrentStringTable(&gGlobalStringTable);
  93. setCurrentFloatTable(&gGlobalFloatTable);
  94. getGlobalFloatTable().reset();
  95. getGlobalStringTable().reset();
  96. getFunctionFloatTable().reset();
  97. getFunctionStringTable().reset();
  98. getIdentTable().reset();
  99. }
  100. void *consoleAlloc(U32 size) { return gConsoleAllocator.alloc(size); }
  101. void consoleAllocReset() { gConsoleAllocator.freeBlocks(); }
  102. }
  103. //-------------------------------------------------------------------------
  104. using namespace Compiler;
  105. //-------------------------------------------------------------------------
  106. U32 CompilerStringTable::add(const char *str, bool caseSens, bool tag)
  107. {
  108. // Is it already in?
  109. Entry **walk;
  110. for(walk = &list; *walk; walk = &((*walk)->next))
  111. {
  112. if((*walk)->tag != tag)
  113. continue;
  114. if(caseSens)
  115. {
  116. if(!dStrcmp((*walk)->string, str))
  117. return (*walk)->start;
  118. }
  119. else
  120. {
  121. if(!dStricmp((*walk)->string, str))
  122. return (*walk)->start;
  123. }
  124. }
  125. // Write it out.
  126. Entry *newStr = (Entry *) consoleAlloc(sizeof(Entry));
  127. *walk = newStr;
  128. newStr->next = NULL;
  129. newStr->start = totalLen;
  130. U32 len = dStrlen(str) + 1;
  131. if(tag && len < 7) // alloc space for the numeric tag 1 for tag, 5 for # and 1 for nul
  132. len = 7;
  133. totalLen += len;
  134. newStr->string = (char *) consoleAlloc(len);
  135. newStr->len = len;
  136. newStr->tag = tag;
  137. dStrcpy(newStr->string, str);
  138. #ifdef EMSCRIPTEN
  139. consoleAlloc(2);
  140. //Con::printf("CompilerStringTable::add(%s) %s %s @ %u:%u", str, caseSens ? "CASE" : "NOCASE", tag ? "TAG" : "NOTAG", newStr->string, len);
  141. #endif
  142. return newStr->start;
  143. }
  144. U32 CompilerStringTable::addIntString(U32 value)
  145. {
  146. dSprintf(buf, sizeof(buf), "%d", value);
  147. return add(buf);
  148. }
  149. U32 CompilerStringTable::addFloatString(F64 value)
  150. {
  151. dSprintf(buf, sizeof(buf), "%.9g", value);
  152. return add(buf);
  153. }
  154. void CompilerStringTable::reset()
  155. {
  156. list = NULL;
  157. totalLen = 0;
  158. }
  159. char *CompilerStringTable::build()
  160. {
  161. char *ret = new char[totalLen];
  162. for(Entry *walk = list; walk; walk = walk->next)
  163. dStrcpy(ret + walk->start, walk->string);
  164. return ret;
  165. }
  166. void CompilerStringTable::write(Stream &st)
  167. {
  168. st.write(totalLen);
  169. for(Entry *walk = list; walk; walk = walk->next)
  170. st.write(walk->len, walk->string);
  171. }
  172. //------------------------------------------------------------
  173. U32 CompilerFloatTable::add(F64 value)
  174. {
  175. Entry **walk;
  176. U32 i = 0;
  177. for(walk = &list; *walk; walk = &((*walk)->next), i++)
  178. if(value == (*walk)->val)
  179. return i;
  180. Entry *newFloat = (Entry *) consoleAlloc(sizeof(Entry));
  181. newFloat->val = value;
  182. newFloat->next = NULL;
  183. count++;
  184. *walk = newFloat;
  185. return count-1;
  186. }
  187. void CompilerFloatTable::reset()
  188. {
  189. list = NULL;
  190. count = 0;
  191. }
  192. F64 *CompilerFloatTable::build()
  193. {
  194. F64 *ret = new F64[count];
  195. U32 i = 0;
  196. for(Entry *walk = list; walk; walk = walk->next, i++)
  197. ret[i] = walk->val;
  198. return ret;
  199. }
  200. void CompilerFloatTable::write(Stream &st)
  201. {
  202. st.write(count);
  203. for(Entry *walk = list; walk; walk = walk->next)
  204. st.write(walk->val);
  205. }
  206. //------------------------------------------------------------
  207. void CompilerIdentTable::reset()
  208. {
  209. list = NULL;
  210. }
  211. void CompilerIdentTable::add(StringTableEntry ste, U32 ip)
  212. {
  213. U32 index = gGlobalStringTable.add(ste, false);
  214. Entry *newEntry = (Entry *) consoleAlloc(sizeof(Entry));
  215. newEntry->offset = index;
  216. newEntry->ip = ip;
  217. for(Entry *walk = list; walk; walk = walk->next)
  218. {
  219. if(walk->offset == index)
  220. {
  221. newEntry->nextIdent = walk->nextIdent;
  222. walk->nextIdent = newEntry;
  223. return;
  224. }
  225. }
  226. newEntry->next = list;
  227. list = newEntry;
  228. newEntry->nextIdent = NULL;
  229. }
  230. void CompilerIdentTable::write(Stream &st)
  231. {
  232. U32 count = 0;
  233. Entry * walk;
  234. for(walk = list; walk; walk = walk->next)
  235. count++;
  236. st.write(count);
  237. for(walk = list; walk; walk = walk->next)
  238. {
  239. U32 ec = 0;
  240. Entry * el;
  241. for(el = walk; el; el = el->nextIdent)
  242. ec++;
  243. st.write(walk->offset);
  244. st.write(ec);
  245. for(el = walk; el; el = el->nextIdent)
  246. st.write(el->ip);
  247. }
  248. }