compiler.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_64
  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. return newStr->start;
  144. }
  145. U32 CompilerStringTable::addIntString(U32 value)
  146. {
  147. dSprintf(buf, sizeof(buf), "%d", value);
  148. return add(buf);
  149. }
  150. U32 CompilerStringTable::addFloatString(F64 value)
  151. {
  152. dSprintf(buf, sizeof(buf), "%.9g", value);
  153. return add(buf);
  154. }
  155. void CompilerStringTable::reset()
  156. {
  157. list = NULL;
  158. totalLen = 0;
  159. }
  160. char *CompilerStringTable::build()
  161. {
  162. char *ret = new char[totalLen];
  163. for(Entry *walk = list; walk; walk = walk->next)
  164. dStrcpy(ret + walk->start, walk->string);
  165. return ret;
  166. }
  167. void CompilerStringTable::write(Stream &st)
  168. {
  169. st.write(totalLen);
  170. for(Entry *walk = list; walk; walk = walk->next)
  171. st.write(walk->len, walk->string);
  172. }
  173. //------------------------------------------------------------
  174. U32 CompilerFloatTable::add(F64 value)
  175. {
  176. Entry **walk;
  177. U32 i = 0;
  178. for(walk = &list; *walk; walk = &((*walk)->next), i++)
  179. if(value == (*walk)->val)
  180. return i;
  181. Entry *newFloat = (Entry *) consoleAlloc(sizeof(Entry));
  182. newFloat->val = value;
  183. newFloat->next = NULL;
  184. count++;
  185. *walk = newFloat;
  186. return count-1;
  187. }
  188. void CompilerFloatTable::reset()
  189. {
  190. list = NULL;
  191. count = 0;
  192. }
  193. F64 *CompilerFloatTable::build()
  194. {
  195. F64 *ret = new F64[count];
  196. U32 i = 0;
  197. for(Entry *walk = list; walk; walk = walk->next, i++)
  198. ret[i] = walk->val;
  199. return ret;
  200. }
  201. void CompilerFloatTable::write(Stream &st)
  202. {
  203. st.write(count);
  204. for(Entry *walk = list; walk; walk = walk->next)
  205. st.write(walk->val);
  206. }
  207. //------------------------------------------------------------
  208. void CompilerIdentTable::reset()
  209. {
  210. list = NULL;
  211. }
  212. void CompilerIdentTable::add(StringTableEntry ste, U32 ip)
  213. {
  214. U32 index = gGlobalStringTable.add(ste, false);
  215. Entry *newEntry = (Entry *) consoleAlloc(sizeof(Entry));
  216. newEntry->offset = index;
  217. newEntry->ip = ip;
  218. for(Entry *walk = list; walk; walk = walk->next)
  219. {
  220. if(walk->offset == index)
  221. {
  222. newEntry->nextIdent = walk->nextIdent;
  223. walk->nextIdent = newEntry;
  224. return;
  225. }
  226. }
  227. newEntry->next = list;
  228. list = newEntry;
  229. newEntry->nextIdent = NULL;
  230. }
  231. void CompilerIdentTable::write(Stream &st)
  232. {
  233. U32 count = 0;
  234. Entry * walk;
  235. for(walk = list; walk; walk = walk->next)
  236. count++;
  237. st.write(count);
  238. for(walk = list; walk; walk = walk->next)
  239. {
  240. U32 ec = 0;
  241. Entry * el;
  242. for(el = walk; el; el = el->nextIdent)
  243. ec++;
  244. st.write(walk->offset);
  245. st.write(ec);
  246. for(el = walk; el; el = el->nextIdent)
  247. st.write(el->ip);
  248. }
  249. }