compiler.cc 8.6 KB

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