2
0

compiler.cc 8.2 KB

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