codeBlock.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 "console/console.h"
  23. #include "console/compiler.h"
  24. #include "console/codeBlock.h"
  25. #include "io/resource/resourceManager.h"
  26. #include "math/mMath.h"
  27. #include "debug/telnetDebugger.h"
  28. #ifndef _REMOTE_DEBUGGER_BASE_H_
  29. #include "debug/remote/RemoteDebuggerBase.h"
  30. #endif
  31. using namespace Compiler;
  32. bool CodeBlock::smInFunction = false;
  33. U32 CodeBlock::smBreakLineCount = 0;
  34. CodeBlock * CodeBlock::smCodeBlockList = NULL;
  35. CodeBlock * CodeBlock::smCurrentCodeBlock = NULL;
  36. ConsoleParser *CodeBlock::smCurrentParser = NULL;
  37. //-------------------------------------------------------------------------
  38. CodeBlock::CodeBlock()
  39. {
  40. globalStrings = NULL;
  41. functionStrings = NULL;
  42. globalFloats = NULL;
  43. functionFloats = NULL;
  44. lineBreakPairs = NULL;
  45. breakList = NULL;
  46. breakListSize = 0;
  47. refCount = 0;
  48. code = NULL;
  49. name = NULL;
  50. fullPath = NULL;
  51. modPath = NULL;
  52. mRoot = StringTable->EmptyString;
  53. }
  54. CodeBlock::~CodeBlock()
  55. {
  56. // Make sure we aren't lingering in the current code block...
  57. AssertFatal(smCurrentCodeBlock != this, "CodeBlock::~CodeBlock - Caught lingering in smCurrentCodeBlock!")
  58. if(name)
  59. removeFromCodeList();
  60. delete[] const_cast<char*>(globalStrings);
  61. delete[] const_cast<char*>(functionStrings);
  62. delete[] globalFloats;
  63. delete[] functionFloats;
  64. delete[] code;
  65. delete[] breakList;
  66. }
  67. //-------------------------------------------------------------------------
  68. StringTableEntry CodeBlock::getCurrentCodeBlockName()
  69. {
  70. if (CodeBlock::getCurrentBlock())
  71. return CodeBlock::getCurrentBlock()->name;
  72. else
  73. return NULL;
  74. }
  75. StringTableEntry CodeBlock::getCurrentCodeBlockFullPath()
  76. {
  77. if (CodeBlock::getCurrentBlock())
  78. return CodeBlock::getCurrentBlock()->fullPath;
  79. else
  80. return NULL;
  81. }
  82. StringTableEntry CodeBlock::getCurrentCodeBlockModName()
  83. {
  84. if (CodeBlock::getCurrentBlock())
  85. return CodeBlock::getCurrentBlock()->modPath;
  86. else
  87. return NULL;
  88. }
  89. CodeBlock *CodeBlock::find(StringTableEntry name)
  90. {
  91. for(CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  92. if(walk->name == name)
  93. return walk;
  94. return NULL;
  95. }
  96. //-------------------------------------------------------------------------
  97. void CodeBlock::addToCodeList()
  98. {
  99. // remove any code blocks with my name
  100. for(CodeBlock **walk = &smCodeBlockList; *walk;walk = &((*walk)->nextFile))
  101. {
  102. if((*walk)->name == name)
  103. {
  104. *walk = (*walk)->nextFile;
  105. break;
  106. }
  107. }
  108. nextFile = smCodeBlockList;
  109. smCodeBlockList = this;
  110. }
  111. void CodeBlock::clearAllBreaks()
  112. {
  113. if(!lineBreakPairs)
  114. return;
  115. for(U32 i = 0; i < lineBreakPairCount; i++)
  116. {
  117. U32 *p = lineBreakPairs + i * 2;
  118. code[p[1]] = p[0] & 0xFF;
  119. }
  120. }
  121. void CodeBlock::clearBreakpoint(U32 lineNumber)
  122. {
  123. if(!lineBreakPairs)
  124. return;
  125. for(U32 i = 0; i < lineBreakPairCount; i++)
  126. {
  127. U32 *p = lineBreakPairs + i * 2;
  128. if((p[0] >> 8) == lineNumber)
  129. {
  130. code[p[1]] = p[0] & 0xFF;
  131. return;
  132. }
  133. }
  134. }
  135. void CodeBlock::setAllBreaks()
  136. {
  137. if(!lineBreakPairs)
  138. return;
  139. for(U32 i = 0; i < lineBreakPairCount; i++)
  140. {
  141. U32 *p = lineBreakPairs + i * 2;
  142. code[p[1]] = OP_BREAK;
  143. }
  144. }
  145. bool CodeBlock::setBreakpoint(U32 lineNumber)
  146. {
  147. if(!lineBreakPairs)
  148. return false;
  149. for(U32 i = 0; i < lineBreakPairCount; i++)
  150. {
  151. U32 *p = lineBreakPairs + i * 2;
  152. if((p[0] >> 8) == lineNumber)
  153. {
  154. code[p[1]] = OP_BREAK;
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. U32 CodeBlock::findFirstBreakLine(U32 lineNumber)
  161. {
  162. if(!lineBreakPairs)
  163. return 0;
  164. for(U32 i = 0; i < lineBreakPairCount; i++)
  165. {
  166. U32 *p = lineBreakPairs + i * 2;
  167. U32 line = (p[0] >> 8);
  168. if( lineNumber <= line )
  169. return line;
  170. }
  171. return 0;
  172. }
  173. struct LinePair
  174. {
  175. U32 instLine;
  176. U32 ip;
  177. };
  178. void CodeBlock::findBreakLine(U32 ip, U32 &line, U32 &instruction)
  179. {
  180. U32 min = 0;
  181. U32 max = lineBreakPairCount - 1;
  182. LinePair *p = (LinePair *) lineBreakPairs;
  183. U32 found;
  184. if(!lineBreakPairCount || p[min].ip > ip || p[max].ip < ip)
  185. {
  186. line = 0;
  187. instruction = OP_INVALID;
  188. return;
  189. }
  190. else if(p[min].ip == ip)
  191. found = min;
  192. else if(p[max].ip == ip)
  193. found = max;
  194. else
  195. {
  196. for(;;)
  197. {
  198. if(min == max - 1)
  199. {
  200. found = min;
  201. break;
  202. }
  203. U32 mid = (min + max) >> 1;
  204. if(p[mid].ip == ip)
  205. {
  206. found = mid;
  207. break;
  208. }
  209. else if(p[mid].ip > ip)
  210. max = mid;
  211. else
  212. min = mid;
  213. }
  214. }
  215. instruction = p[found].instLine & 0xFF;
  216. line = p[found].instLine >> 8;
  217. }
  218. const char *CodeBlock::getFileLine(U32 ip)
  219. {
  220. static char nameBuffer[256];
  221. U32 line, inst;
  222. findBreakLine(ip, line, inst);
  223. dSprintf(nameBuffer, sizeof(nameBuffer), "%s (%d)", name ? name : "<input>", line);
  224. return nameBuffer;
  225. }
  226. void CodeBlock::removeFromCodeList()
  227. {
  228. for(CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  229. {
  230. if(*walk == this)
  231. {
  232. *walk = nextFile;
  233. // clear out all breakpoints
  234. clearAllBreaks();
  235. break;
  236. }
  237. }
  238. // Let the telnet debugger know that this code
  239. // block has been unloaded and that it needs to
  240. // remove references to it.
  241. if ( TelDebugger )
  242. TelDebugger->clearCodeBlockPointers( this );
  243. // Notify the remote debugger.
  244. RemoteDebuggerBase* pRemoteDebugger = RemoteDebuggerBase::getRemoteDebugger();
  245. if ( pRemoteDebugger != NULL )
  246. pRemoteDebugger->removeCodeBlock( this );
  247. }
  248. void CodeBlock::calcBreakList()
  249. {
  250. U32 size = 0;
  251. S32 line = -1;
  252. U32 seqCount = 0;
  253. U32 i;
  254. for(i = 0; i < lineBreakPairCount; i++)
  255. {
  256. U32 lineNumber = lineBreakPairs[i * 2];
  257. if(lineNumber == U32(line + 1))
  258. seqCount++;
  259. else
  260. {
  261. if(seqCount)
  262. size++;
  263. size++;
  264. seqCount = 1;
  265. }
  266. line = lineNumber;
  267. }
  268. if(seqCount)
  269. size++;
  270. breakList = new U32[size];
  271. breakListSize = size;
  272. line = -1;
  273. seqCount = 0;
  274. size = 0;
  275. for(i = 0; i < lineBreakPairCount; i++)
  276. {
  277. U32 lineNumber = lineBreakPairs[i * 2];
  278. if(lineNumber == U32(line + 1))
  279. seqCount++;
  280. else
  281. {
  282. if(seqCount)
  283. breakList[size++] = seqCount;
  284. breakList[size++] = lineNumber - getMax(0, line) - 1;
  285. seqCount = 1;
  286. }
  287. line = lineNumber;
  288. }
  289. if(seqCount)
  290. breakList[size++] = seqCount;
  291. for(i = 0; i < lineBreakPairCount; i++)
  292. {
  293. U32 *p = lineBreakPairs + i * 2;
  294. p[0] = (p[0] << 8) | code[p[1]];
  295. }
  296. // Let the telnet debugger know that this code
  297. // block has been loaded and that it can add break
  298. // points it has for it.
  299. if ( TelDebugger )
  300. TelDebugger->addAllBreakpoints( this );
  301. // Notify the remote debugger.
  302. RemoteDebuggerBase* pRemoteDebugger = RemoteDebuggerBase::getRemoteDebugger();
  303. if ( pRemoteDebugger != NULL )
  304. pRemoteDebugger->addCodeBlock( this );
  305. }
  306. bool CodeBlock::read(StringTableEntry fileName, Stream &st)
  307. {
  308. const StringTableEntry exePath = Platform::getMainDotCsDir();
  309. const StringTableEntry cwd = Platform::getCurrentDirectory();
  310. name = fileName;
  311. if(fileName)
  312. {
  313. fullPath = NULL;
  314. if(Platform::isFullPath(fileName))
  315. fullPath = fileName;
  316. if(dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  317. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  318. else if(dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  319. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  320. if(fullPath == NULL)
  321. {
  322. char buf[1024];
  323. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  324. }
  325. modPath = Con::getModNameFromPath(fileName);
  326. }
  327. //
  328. if (name)
  329. {
  330. if (const char *slash = dStrchr(this->name, '/'))
  331. {
  332. char root[512];
  333. dStrncpy(root, this->name, slash-this->name);
  334. root[slash-this->name] = 0;
  335. mRoot = StringTable->insert(root);
  336. }
  337. }
  338. //
  339. addToCodeList();
  340. U32 globalSize,size,i;
  341. st.read(&size);
  342. if(size)
  343. {
  344. globalSize = size;
  345. globalStrings = new char[size];
  346. st.read(size, globalStrings);
  347. }
  348. st.read(&size);
  349. if(size)
  350. {
  351. functionStrings = new char[size];
  352. st.read(size, functionStrings);
  353. }
  354. st.read(&size);
  355. if(size)
  356. {
  357. globalFloats = new F64[size];
  358. for(U32 i = 0; i < size; i++)
  359. st.read(&globalFloats[i]);
  360. }
  361. st.read(&size);
  362. if(size)
  363. {
  364. functionFloats = new F64[size];
  365. for(U32 i = 0; i < size; i++)
  366. st.read(&functionFloats[i]);
  367. }
  368. U32 codeSize;
  369. st.read(&codeSize);
  370. st.read(&lineBreakPairCount);
  371. U32 totSize = codeSize + lineBreakPairCount * 2;
  372. code = new U32[totSize];
  373. for(i = 0; i < codeSize; i++)
  374. {
  375. U8 b;
  376. st.read(&b);
  377. if(b == 0xFF)
  378. st.read(&code[i]);
  379. else
  380. code[i] = b;
  381. }
  382. for(i = codeSize; i < totSize; i++)
  383. st.read(&code[i]);
  384. lineBreakPairs = code + codeSize;
  385. // StringTable-ize our identifiers.
  386. U32 identCount;
  387. st.read(&identCount);
  388. while(identCount--)
  389. {
  390. U32 offset;
  391. st.read(&offset);
  392. StringTableEntry ste;
  393. if(offset < globalSize)
  394. ste = StringTable->insert(globalStrings + offset);
  395. else
  396. ste = StringTable->EmptyString;
  397. U32 count;
  398. st.read(&count);
  399. while(count--)
  400. {
  401. U32 ip;
  402. st.read(&ip);
  403. #ifdef TORQUE_CPU_X64
  404. *(U64*)(code+ip) = (U64)ste;
  405. #else
  406. code[ip] = (U32)ste;
  407. #endif
  408. }
  409. }
  410. if(lineBreakPairCount)
  411. calcBreakList();
  412. return true;
  413. }
  414. bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, const char *script)
  415. {
  416. gSyntaxError = false;
  417. consoleAllocReset();
  418. STEtoCode = compileSTEtoCode;
  419. statementList = NULL;
  420. // Set up the parser.
  421. smCurrentParser = getParserForFile(fileName);
  422. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  423. // Now do some parsing.
  424. smCurrentParser->setScanBuffer(script, fileName);
  425. smCurrentParser->restart(NULL);
  426. smCurrentParser->parse();
  427. if(gSyntaxError)
  428. {
  429. consoleAllocReset();
  430. return false;
  431. }
  432. FileStream st;
  433. if(!ResourceManager->openFileForWrite(st, codeFileName))
  434. return false;
  435. st.write(DSO_VERSION);
  436. // Reset all our value tables...
  437. resetTables();
  438. smInFunction = false;
  439. smBreakLineCount = 0;
  440. setBreakCodeBlock(this);
  441. if(statementList)
  442. codeSize = precompileBlock(statementList, 0) + 1;
  443. else
  444. codeSize = 1;
  445. lineBreakPairCount = smBreakLineCount;
  446. code = new U32[codeSize + smBreakLineCount * 2];
  447. lineBreakPairs = code + codeSize;
  448. // Write string table data...
  449. getGlobalStringTable().write(st);
  450. getFunctionStringTable().write(st);
  451. // Write float table data...
  452. getGlobalFloatTable().write(st);
  453. getFunctionFloatTable().write(st);
  454. smBreakLineCount = 0;
  455. U32 lastIp;
  456. if(statementList)
  457. lastIp = compileBlock(statementList, code, 0, 0, 0);
  458. else
  459. lastIp = 0;
  460. if(lastIp != codeSize - 1)
  461. Con::errorf(ConsoleLogEntry::General, "CodeBlock::compile - precompile size mismatch, a precompile/compile function pair is probably mismatched.");
  462. code[lastIp++] = OP_RETURN;
  463. U32 totSize = codeSize + smBreakLineCount * 2;
  464. st.write(codeSize);
  465. st.write(lineBreakPairCount);
  466. // Write out our bytecode, doing a bit of compression for low numbers.
  467. U32 i;
  468. for(i = 0; i < codeSize; i++)
  469. {
  470. if(code[i] < 0xFF)
  471. st.write(U8(code[i]));
  472. else
  473. {
  474. st.write(U8(0xFF));
  475. st.write(code[i]);
  476. }
  477. }
  478. // Write the break info...
  479. for(i = codeSize; i < totSize; i++)
  480. st.write(code[i]);
  481. getIdentTable().write(st);
  482. consoleAllocReset();
  483. st.close();
  484. return true;
  485. }
  486. const char *CodeBlock::compileExec(StringTableEntry fileName, const char *string, bool noCalls, int setFrame)
  487. {
  488. STEtoCode = evalSTEtoCode;
  489. consoleAllocReset();
  490. name = fileName;
  491. if(fileName)
  492. {
  493. const StringTableEntry exePath = Platform::getMainDotCsDir();
  494. const StringTableEntry cwd = Platform::getCurrentDirectory();
  495. fullPath = NULL;
  496. if(Platform::isFullPath(fileName))
  497. fullPath = fileName;
  498. if(dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  499. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  500. else if(dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  501. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  502. if(fullPath == NULL)
  503. {
  504. char buf[1024];
  505. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  506. }
  507. modPath = Con::getModNameFromPath(fileName);
  508. }
  509. if(name)
  510. addToCodeList();
  511. statementList = NULL;
  512. // Set up the parser.
  513. smCurrentParser = getParserForFile(fileName);
  514. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  515. // Now do some parsing.
  516. smCurrentParser->setScanBuffer(string, fileName);
  517. smCurrentParser->restart(NULL);
  518. smCurrentParser->parse();
  519. if(!statementList)
  520. {
  521. delete this;
  522. return "";
  523. }
  524. resetTables();
  525. smInFunction = false;
  526. smBreakLineCount = 0;
  527. setBreakCodeBlock(this);
  528. codeSize = precompileBlock(statementList, 0) + 1;
  529. lineBreakPairCount = smBreakLineCount;
  530. globalStrings = getGlobalStringTable().build();
  531. functionStrings = getFunctionStringTable().build();
  532. globalFloats = getGlobalFloatTable().build();
  533. functionFloats = getFunctionFloatTable().build();
  534. code = new U32[codeSize + lineBreakPairCount * 2];
  535. lineBreakPairs = code + codeSize;
  536. smBreakLineCount = 0;
  537. U32 lastIp = compileBlock(statementList, code, 0, 0, 0);
  538. code[lastIp++] = OP_RETURN;
  539. consoleAllocReset();
  540. if(lineBreakPairCount && fileName)
  541. calcBreakList();
  542. if(lastIp != codeSize)
  543. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch");
  544. return exec(0, fileName, NULL, 0, 0, noCalls, NULL, setFrame);
  545. }
  546. //-------------------------------------------------------------------------
  547. void CodeBlock::incRefCount()
  548. {
  549. refCount++;
  550. }
  551. void CodeBlock::decRefCount()
  552. {
  553. refCount--;
  554. if(!refCount)
  555. delete this;
  556. }