codeBlock.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "console/telnetDebugger.h"
  26. #include "console/ast.h"
  27. #include "core/strings/unicode.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "core/stringTable.h"
  30. #include "core/stream/fileStream.h"
  31. using namespace Compiler;
  32. bool CodeBlock::smInFunction = false;
  33. CodeBlock * CodeBlock::smCodeBlockList = NULL;
  34. CodeBlock * CodeBlock::smCurrentCodeBlock = NULL;
  35. ConsoleParser *CodeBlock::smCurrentParser = NULL;
  36. //-------------------------------------------------------------------------
  37. CodeBlock::CodeBlock()
  38. {
  39. globalStrings = NULL;
  40. functionStrings = NULL;
  41. functionStringsMaxLen = 0;
  42. globalStringsMaxLen = 0;
  43. globalFloats = NULL;
  44. functionFloats = NULL;
  45. lineBreakPairs = NULL;
  46. breakList = NULL;
  47. breakListSize = 0;
  48. refCount = 0;
  49. code = NULL;
  50. name = NULL;
  51. fullPath = NULL;
  52. modPath = NULL;
  53. codeSize = 0;
  54. lineBreakPairCount = 0;
  55. nextFile = NULL;
  56. }
  57. CodeBlock::~CodeBlock()
  58. {
  59. // Make sure we aren't lingering in the current code block...
  60. AssertFatal(smCurrentCodeBlock != this, "CodeBlock::~CodeBlock - Caught lingering in smCurrentCodeBlock!");
  61. if (name)
  62. removeFromCodeList();
  63. delete[] const_cast<char*>(globalStrings);
  64. delete[] const_cast<char*>(functionStrings);
  65. functionStringsMaxLen = 0;
  66. globalStringsMaxLen = 0;
  67. delete[] globalFloats;
  68. delete[] functionFloats;
  69. delete[] code;
  70. delete[] breakList;
  71. }
  72. //-------------------------------------------------------------------------
  73. StringTableEntry CodeBlock::getCurrentCodeBlockName()
  74. {
  75. if (CodeBlock::getCurrentBlock())
  76. return CodeBlock::getCurrentBlock()->name;
  77. else
  78. return NULL;
  79. }
  80. StringTableEntry CodeBlock::getCurrentCodeBlockFullPath()
  81. {
  82. if (CodeBlock::getCurrentBlock())
  83. return CodeBlock::getCurrentBlock()->fullPath;
  84. else
  85. return NULL;
  86. }
  87. StringTableEntry CodeBlock::getCurrentCodeBlockModName()
  88. {
  89. if (CodeBlock::getCurrentBlock())
  90. return CodeBlock::getCurrentBlock()->modPath;
  91. else
  92. return NULL;
  93. }
  94. CodeBlock *CodeBlock::find(StringTableEntry name)
  95. {
  96. for (CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  97. if (walk->name == name)
  98. return walk;
  99. return NULL;
  100. }
  101. //-------------------------------------------------------------------------
  102. void CodeBlock::addToCodeList()
  103. {
  104. // remove any code blocks with my name
  105. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  106. {
  107. if ((*walk)->name == name)
  108. {
  109. *walk = (*walk)->nextFile;
  110. break;
  111. }
  112. }
  113. nextFile = smCodeBlockList;
  114. smCodeBlockList = this;
  115. }
  116. void CodeBlock::clearAllBreaks()
  117. {
  118. if (!lineBreakPairs)
  119. return;
  120. for (U32 i = 0; i < lineBreakPairCount; i++)
  121. {
  122. U32 *p = lineBreakPairs + i * 2;
  123. code[p[1]] = p[0] & 0xFF;
  124. }
  125. }
  126. void CodeBlock::clearBreakpoint(U32 lineNumber)
  127. {
  128. if (!lineBreakPairs)
  129. return;
  130. for (U32 i = 0; i < lineBreakPairCount; i++)
  131. {
  132. U32 *p = lineBreakPairs + i * 2;
  133. if ((p[0] >> 8) == lineNumber)
  134. {
  135. code[p[1]] = p[0] & 0xFF;
  136. return;
  137. }
  138. }
  139. }
  140. void CodeBlock::setAllBreaks()
  141. {
  142. if (!lineBreakPairs)
  143. return;
  144. for (U32 i = 0; i < lineBreakPairCount; i++)
  145. {
  146. U32 *p = lineBreakPairs + i * 2;
  147. code[p[1]] = OP_BREAK;
  148. }
  149. }
  150. bool CodeBlock::setBreakpoint(U32 lineNumber)
  151. {
  152. if (!lineBreakPairs)
  153. return false;
  154. for (U32 i = 0; i < lineBreakPairCount; i++)
  155. {
  156. U32 *p = lineBreakPairs + i * 2;
  157. if ((p[0] >> 8) == lineNumber)
  158. {
  159. code[p[1]] = OP_BREAK;
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. U32 CodeBlock::findFirstBreakLine(U32 lineNumber)
  166. {
  167. if (!lineBreakPairs)
  168. return 0;
  169. for (U32 i = 0; i < lineBreakPairCount; i++)
  170. {
  171. U32 *p = lineBreakPairs + i * 2;
  172. U32 line = (p[0] >> 8);
  173. if (lineNumber <= line)
  174. return line;
  175. }
  176. return 0;
  177. }
  178. struct LinePair
  179. {
  180. U32 instLine;
  181. U32 ip;
  182. };
  183. void CodeBlock::findBreakLine(U32 ip, U32 &line, U32 &instruction)
  184. {
  185. U32 min = 0;
  186. U32 max = lineBreakPairCount - 1;
  187. LinePair *p = (LinePair *)lineBreakPairs;
  188. U32 found;
  189. if (!lineBreakPairCount || p[min].ip > ip || p[max].ip < ip)
  190. {
  191. line = 0;
  192. instruction = OP_INVALID;
  193. return;
  194. }
  195. else if (p[min].ip == ip)
  196. found = min;
  197. else if (p[max].ip == ip)
  198. found = max;
  199. else
  200. {
  201. for (;;)
  202. {
  203. if (min == max - 1)
  204. {
  205. found = min;
  206. break;
  207. }
  208. U32 mid = (min + max) >> 1;
  209. if (p[mid].ip == ip)
  210. {
  211. found = mid;
  212. break;
  213. }
  214. else if (p[mid].ip > ip)
  215. max = mid;
  216. else
  217. min = mid;
  218. }
  219. }
  220. instruction = p[found].instLine & 0xFF;
  221. line = p[found].instLine >> 8;
  222. }
  223. const char *CodeBlock::getFileLine(U32 ip)
  224. {
  225. static char nameBuffer[256];
  226. U32 line, inst;
  227. findBreakLine(ip, line, inst);
  228. dSprintf(nameBuffer, sizeof(nameBuffer), "%s (%d)", name ? name : "<input>", line);
  229. return nameBuffer;
  230. }
  231. void CodeBlock::removeFromCodeList()
  232. {
  233. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  234. {
  235. if (*walk == this)
  236. {
  237. *walk = nextFile;
  238. // clear out all breakpoints
  239. clearAllBreaks();
  240. break;
  241. }
  242. }
  243. // Let the telnet debugger know that this code
  244. // block has been unloaded and that it needs to
  245. // remove references to it.
  246. if (TelDebugger)
  247. TelDebugger->clearCodeBlockPointers(this);
  248. }
  249. void CodeBlock::calcBreakList()
  250. {
  251. U32 size = 0;
  252. S32 line = -1;
  253. U32 seqCount = 0;
  254. U32 i;
  255. for (i = 0; i < lineBreakPairCount; i++)
  256. {
  257. U32 lineNumber = lineBreakPairs[i * 2];
  258. if (lineNumber == U32(line + 1))
  259. seqCount++;
  260. else
  261. {
  262. if (seqCount)
  263. size++;
  264. size++;
  265. seqCount = 1;
  266. }
  267. line = lineNumber;
  268. }
  269. if (seqCount)
  270. size++;
  271. breakList = new U32[size];
  272. breakListSize = size;
  273. line = -1;
  274. seqCount = 0;
  275. size = 0;
  276. for (i = 0; i < lineBreakPairCount; i++)
  277. {
  278. U32 lineNumber = lineBreakPairs[i * 2];
  279. if (lineNumber == U32(line + 1))
  280. seqCount++;
  281. else
  282. {
  283. if (seqCount)
  284. breakList[size++] = seqCount;
  285. breakList[size++] = lineNumber - getMax(0, line) - 1;
  286. seqCount = 1;
  287. }
  288. line = lineNumber;
  289. }
  290. if (seqCount)
  291. breakList[size++] = seqCount;
  292. for (i = 0; i < lineBreakPairCount; i++)
  293. {
  294. U32 *p = lineBreakPairs + i * 2;
  295. p[0] = (p[0] << 8) | code[p[1]];
  296. }
  297. // Let the telnet debugger know that this code
  298. // block has been loaded and that it can add break
  299. // points it has for it.
  300. if (TelDebugger)
  301. TelDebugger->addAllBreakpoints(this);
  302. }
  303. bool CodeBlock::read(StringTableEntry fileName, Stream &st)
  304. {
  305. const StringTableEntry exePath = Platform::getMainDotCsDir();
  306. const StringTableEntry cwd = Platform::getCurrentDirectory();
  307. name = fileName;
  308. if (fileName)
  309. {
  310. fullPath = NULL;
  311. if (Platform::isFullPath(fileName))
  312. fullPath = fileName;
  313. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  314. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  315. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  316. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  317. if (fullPath == NULL)
  318. {
  319. char buf[1024];
  320. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  321. }
  322. modPath = Con::getModNameFromPath(fileName);
  323. }
  324. //
  325. addToCodeList();
  326. U32 globalSize, size, i;
  327. st.read(&size);
  328. if (size)
  329. {
  330. globalStrings = new char[size];
  331. globalStringsMaxLen = size;
  332. st.read(size, globalStrings);
  333. }
  334. globalSize = size;
  335. st.read(&size);
  336. if (size)
  337. {
  338. functionStrings = new char[size];
  339. functionStringsMaxLen = size;
  340. st.read(size, functionStrings);
  341. }
  342. st.read(&size);
  343. if (size)
  344. {
  345. globalFloats = new F64[size];
  346. for (i = 0; i < size; i++)
  347. st.read(&globalFloats[i]);
  348. }
  349. st.read(&size);
  350. if (size)
  351. {
  352. functionFloats = new F64[size];
  353. for (i = 0; i < size; i++)
  354. st.read(&functionFloats[i]);
  355. }
  356. // Variable register mapping table
  357. st.read(&size);
  358. if (size)
  359. {
  360. for (i = 0; i < size; i++)
  361. {
  362. char functionNameBuffer[256];
  363. st.readString(functionNameBuffer);
  364. StringTableEntry fnName = StringTable->insert(functionNameBuffer);
  365. U32 count;
  366. st.read(&count);
  367. for (U32 j = 0; j < count; j++)
  368. {
  369. char varNameBuffer[256];
  370. st.readString(varNameBuffer);
  371. StringTableEntry varName = StringTable->insert(varNameBuffer);
  372. variableRegisterTable.localVarToRegister[fnName].varList.push_back(varName);
  373. }
  374. }
  375. }
  376. U32 codeLength;
  377. st.read(&codeLength);
  378. st.read(&lineBreakPairCount);
  379. U32 totSize = codeLength + lineBreakPairCount * 2;
  380. code = new U32[totSize];
  381. // 0xFF is used as a flag to help compress the bytecode.
  382. // If detected, the bytecode is only a U8.
  383. for (i = 0; i < codeLength; i++)
  384. {
  385. U8 b;
  386. st.read(&b);
  387. if (b == 0xFF)
  388. st.read(&code[i]);
  389. else
  390. code[i] = b;
  391. }
  392. for (i = codeLength; i < totSize; i++)
  393. st.read(&code[i]);
  394. lineBreakPairs = code + codeLength;
  395. // StringTable-ize our identifiers.
  396. U32 identCount;
  397. st.read(&identCount);
  398. while (identCount--)
  399. {
  400. U32 offset;
  401. st.read(&offset);
  402. StringTableEntry ste;
  403. if (offset < globalSize)
  404. ste = StringTable->insert(globalStrings + offset);
  405. else
  406. ste = StringTable->EmptyString();
  407. U32 count;
  408. st.read(&count);
  409. while (count--)
  410. {
  411. U32 ip;
  412. st.read(&ip);
  413. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  414. *(U64*)(code + ip) = (U64)ste;
  415. #else
  416. code[ip] = *((U32 *)&ste);
  417. #endif
  418. }
  419. }
  420. if (lineBreakPairCount)
  421. calcBreakList();
  422. return true;
  423. }
  424. bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, const char *inScript, bool overrideNoDso)
  425. {
  426. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  427. // This will return true, but return value is ignored
  428. char *script;
  429. chompUTF8BOM(inScript, &script);
  430. gSyntaxError = false;
  431. consoleAllocReset();
  432. STEtoCode = compileSTEtoCode;
  433. gStatementList = NULL;
  434. // Set up the parser.
  435. smCurrentParser = getParserForFile(fileName);
  436. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  437. // Now do some parsing.
  438. smCurrentParser->setScanBuffer(script, fileName);
  439. smCurrentParser->restart(NULL);
  440. smCurrentParser->parse();
  441. if (gSyntaxError)
  442. {
  443. consoleAllocReset();
  444. return false;
  445. }
  446. #ifdef TORQUE_NO_DSO_GENERATION
  447. if (!overrideNoDso)
  448. return false;
  449. #endif // !TORQUE_NO_DSO_GENERATION
  450. FileStream st;
  451. if (!st.open(codeFileName, Torque::FS::File::Write))
  452. return false;
  453. st.write(U32(Con::DSOVersion));
  454. // Reset all our value tables...
  455. resetTables();
  456. smInFunction = false;
  457. CodeStream codeStream;
  458. U32 lastIp;
  459. if (gStatementList)
  460. {
  461. lastIp = compileBlock(gStatementList, codeStream, 0) + 1;
  462. }
  463. else
  464. {
  465. codeSize = 1;
  466. lastIp = 0;
  467. }
  468. codeStream.emit(OP_RETURN_VOID);
  469. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  470. lineBreakPairCount = codeStream.getNumLineBreaks();
  471. // Write string table data...
  472. getGlobalStringTable().write(st);
  473. getFunctionStringTable().write(st);
  474. // Write float table data...
  475. getGlobalFloatTable().write(st);
  476. getFunctionFloatTable().write(st);
  477. // write variable mapping table
  478. getFunctionVariableMappingTable().write(st);
  479. if (lastIp != codeSize)
  480. Con::errorf(ConsoleLogEntry::General, "CodeBlock::compile - precompile size mismatch, a precompile/compile function pair is probably mismatched.");
  481. U32 totSize = codeSize + codeStream.getNumLineBreaks() * 2;
  482. st.write(codeSize);
  483. st.write(lineBreakPairCount);
  484. // Write out our bytecode, doing a bit of compression for low numbers.
  485. U32 i;
  486. for (i = 0; i < codeSize; i++)
  487. {
  488. if (code[i] < 0xFF)
  489. st.write(U8(code[i]));
  490. else
  491. {
  492. st.write(U8(0xFF));
  493. st.write(code[i]);
  494. }
  495. }
  496. // Write the break info...
  497. for (i = codeSize; i < totSize; i++)
  498. st.write(code[i]);
  499. getIdentTable().write(st);
  500. consoleAllocReset();
  501. st.close();
  502. return true;
  503. }
  504. ConsoleValue CodeBlock::compileExec(StringTableEntry fileName, const char *inString, bool noCalls, S32 setFrame)
  505. {
  506. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  507. // Check for a UTF8 script file
  508. char *string;
  509. chompUTF8BOM(inString, &string);
  510. STEtoCode = evalSTEtoCode;
  511. consoleAllocReset();
  512. name = fileName;
  513. if (fileName)
  514. {
  515. const StringTableEntry exePath = Platform::getMainDotCsDir();
  516. const StringTableEntry cwd = Platform::getCurrentDirectory();
  517. fullPath = NULL;
  518. if (Platform::isFullPath(fileName))
  519. fullPath = fileName;
  520. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  521. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  522. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  523. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  524. if (fullPath == NULL)
  525. {
  526. char buf[1024];
  527. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  528. }
  529. modPath = Con::getModNameFromPath(fileName);
  530. }
  531. if (name)
  532. addToCodeList();
  533. gStatementList = NULL;
  534. // Set up the parser.
  535. smCurrentParser = getParserForFile(fileName);
  536. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  537. // Now do some parsing.
  538. smCurrentParser->setScanBuffer(string, fileName);
  539. smCurrentParser->restart(NULL);
  540. smCurrentParser->parse();
  541. if (!gStatementList)
  542. {
  543. delete this;
  544. return std::move(ConsoleValue());
  545. }
  546. resetTables();
  547. smInFunction = false;
  548. CodeStream codeStream;
  549. U32 lastIp = compileBlock(gStatementList, codeStream, 0);
  550. lineBreakPairCount = codeStream.getNumLineBreaks();
  551. globalStrings = getGlobalStringTable().build();
  552. globalStringsMaxLen = getGlobalStringTable().totalLen;
  553. functionStrings = getFunctionStringTable().build();
  554. functionStringsMaxLen = getFunctionStringTable().totalLen;
  555. globalFloats = getGlobalFloatTable().build();
  556. functionFloats = getFunctionFloatTable().build();
  557. variableRegisterTable = getFunctionVariableMappingTable().copy();
  558. codeStream.emit(OP_RETURN_VOID);
  559. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  560. consoleAllocReset();
  561. if (lineBreakPairCount && fileName)
  562. calcBreakList();
  563. if (lastIp + 1 != codeSize)
  564. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
  565. return std::move(exec(0, fileName, NULL, 0, 0, noCalls, NULL, setFrame));
  566. }
  567. //-------------------------------------------------------------------------
  568. void CodeBlock::incRefCount()
  569. {
  570. refCount++;
  571. }
  572. void CodeBlock::decRefCount()
  573. {
  574. refCount--;
  575. if (!refCount)
  576. delete this;
  577. }
  578. //-------------------------------------------------------------------------
  579. String CodeBlock::getFunctionArgs(U32 ip)
  580. {
  581. StringBuilder str;
  582. StringTableEntry fnName = CodeToSTE(code, ip);
  583. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  584. StringTableEntry fnNsName = StringTable->insert(avar("%s::%s", fnNamespace, fnName));
  585. U32 fnArgc = code[ip + 8];
  586. for (U32 i = 0; i < fnArgc; ++i)
  587. {
  588. StringTableEntry var = variableRegisterTable.localVarToRegister[fnNsName].varList[i];
  589. if (i != 0)
  590. str.append(", ");
  591. str.append("string ");
  592. // Try to capture junked parameters
  593. if (var[0])
  594. str.append(var + 1);
  595. else
  596. str.append("JUNK");
  597. }
  598. return str.end();
  599. }
  600. //-------------------------------------------------------------------------
  601. void CodeBlock::dumpInstructions(U32 startIp, bool upToReturn)
  602. {
  603. U32 ip = startIp;
  604. smInFunction = false;
  605. U32 endFuncIp = 0;
  606. while (ip < codeSize)
  607. {
  608. if (ip > endFuncIp)
  609. {
  610. smInFunction = false;
  611. }
  612. switch (code[ip++])
  613. {
  614. case OP_FUNC_DECL:
  615. {
  616. StringTableEntry fnName = CodeToSTE(code, ip);
  617. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  618. StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
  619. bool hasBody = bool(code[ip + 6]);
  620. U32 newIp = code[ip + 7];
  621. U32 argc = code[ip + 8];
  622. U32 regCount = code[ip + 9];
  623. endFuncIp = newIp;
  624. Con::printf("%i: OP_FUNC_DECL stk=+0 name=%s nspace=%s package=%s hasbody=%i newip=%i argc=%i regCount=%i",
  625. ip - 1, fnName, fnNamespace, fnPackage, hasBody, newIp, argc, regCount);
  626. // Skip args.
  627. ip += 10 + argc;
  628. smInFunction = true;
  629. break;
  630. }
  631. case OP_CREATE_OBJECT:
  632. {
  633. StringTableEntry objParent = CodeToSTE(code, ip);
  634. bool isDataBlock = code[ip + 2];
  635. bool isInternal = code[ip + 3];
  636. bool isSingleton = code[ip + 4];
  637. U32 lineNumber = code[ip + 5];
  638. U32 failJump = code[ip + 6];
  639. Con::printf("%i: OP_CREATE_OBJECT stk=+0 objParent=%s isDataBlock=%i isInternal=%i isSingleton=%i lineNumber=%i failJump=%i",
  640. ip - 1, objParent, isDataBlock, isInternal, isSingleton, lineNumber, failJump);
  641. ip += 7;
  642. break;
  643. }
  644. case OP_ADD_OBJECT:
  645. {
  646. bool placeAtRoot = code[ip++];
  647. const char* stk = placeAtRoot ? "+1" : "0";
  648. Con::printf("%i: OP_ADD_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  649. break;
  650. }
  651. case OP_END_OBJECT:
  652. {
  653. bool placeAtRoot = code[ip++];
  654. const char* stk = placeAtRoot ? "-1" : "0";
  655. Con::printf("%i: OP_END_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  656. break;
  657. }
  658. case OP_FINISH_OBJECT:
  659. {
  660. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  661. break;
  662. }
  663. case OP_JMPIFFNOT:
  664. {
  665. Con::printf("%i: OP_JMPIFFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  666. ++ip;
  667. break;
  668. }
  669. case OP_JMPIFNOT:
  670. {
  671. Con::printf("%i: OP_JMPIFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  672. ++ip;
  673. break;
  674. }
  675. case OP_JMPIFF:
  676. {
  677. Con::printf("%i: OP_JMPIFF stk=-1 ip=%i", ip - 1, code[ip]);
  678. ++ip;
  679. break;
  680. }
  681. case OP_JMPIF:
  682. {
  683. Con::printf("%i: OP_JMPIF stk=-1 ip=%i", ip - 1, code[ip]);
  684. ++ip;
  685. break;
  686. }
  687. case OP_JMPIFNOT_NP:
  688. {
  689. Con::printf("%i: OP_JMPIFNOT_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  690. ++ip;
  691. break;
  692. }
  693. case OP_JMPIF_NP:
  694. {
  695. Con::printf("%i: OP_JMPIF_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  696. ++ip;
  697. break;
  698. }
  699. case OP_JMP:
  700. {
  701. Con::printf("%i: OP_JMP stk=0 ip=%i", ip - 1, code[ip]);
  702. ++ip;
  703. break;
  704. }
  705. case OP_RETURN_VOID:
  706. {
  707. Con::printf("%i: OP_RETURN_VOID stk=0", ip - 1);
  708. if (upToReturn)
  709. return;
  710. break;
  711. }
  712. case OP_RETURN:
  713. {
  714. Con::printf("%i: OP_RETURN stk=-1", ip - 1);
  715. if (upToReturn)
  716. return;
  717. break;
  718. }
  719. case OP_RETURN_UINT:
  720. {
  721. Con::printf("%i: OP_RETURNUINT stk=-1", ip - 1);
  722. if (upToReturn)
  723. return;
  724. break;
  725. }
  726. case OP_RETURN_FLT:
  727. {
  728. Con::printf("%i: OP_RETURNFLT stk=-1", ip - 1);
  729. if (upToReturn)
  730. return;
  731. break;
  732. }
  733. case OP_CMPEQ:
  734. {
  735. Con::printf("%i: OP_CMPEQ stk=-1", ip - 1);
  736. break;
  737. }
  738. case OP_CMPGR:
  739. {
  740. Con::printf("%i: OP_CMPGR stk=-1", ip - 1);
  741. break;
  742. }
  743. case OP_CMPGE:
  744. {
  745. Con::printf("%i: OP_CMPGE stk=-1", ip - 1);
  746. break;
  747. }
  748. case OP_CMPLT:
  749. {
  750. Con::printf("%i: OP_CMPLT stk=-1", ip - 1);
  751. break;
  752. }
  753. case OP_CMPLE:
  754. {
  755. Con::printf("%i: OP_CMPLE stk=-1", ip - 1);
  756. break;
  757. }
  758. case OP_CMPNE:
  759. {
  760. Con::printf("%i: OP_CMPNE stk=-1", ip - 1);
  761. break;
  762. }
  763. case OP_XOR:
  764. {
  765. Con::printf("%i: OP_XOR stk=-1", ip - 1);
  766. break;
  767. }
  768. case OP_MOD:
  769. {
  770. Con::printf("%i: OP_MOD stk=-1", ip - 1);
  771. break;
  772. }
  773. case OP_BITAND:
  774. {
  775. Con::printf("%i: OP_BITAND stk=-1", ip - 1);
  776. break;
  777. }
  778. case OP_BITOR:
  779. {
  780. Con::printf("%i: OP_BITOR stk=-1", ip - 1);
  781. break;
  782. }
  783. case OP_NOT:
  784. {
  785. Con::printf("%i: OP_NOT stk=0", ip - 1);
  786. break;
  787. }
  788. case OP_NOTF:
  789. {
  790. Con::printf("%i: OP_NOTF stk=0", ip - 1);
  791. break;
  792. }
  793. case OP_ONESCOMPLEMENT:
  794. {
  795. Con::printf("%i: OP_ONESCOMPLEMENT stk=0", ip - 1);
  796. break;
  797. }
  798. case OP_SHR:
  799. {
  800. Con::printf("%i: OP_SHR stk=-1", ip - 1);
  801. break;
  802. }
  803. case OP_SHL:
  804. {
  805. Con::printf("%i: OP_SHL stk=-1", ip - 1);
  806. break;
  807. }
  808. case OP_AND:
  809. {
  810. Con::printf("%i: OP_AND stk=-1", ip - 1);
  811. break;
  812. }
  813. case OP_OR:
  814. {
  815. Con::printf("%i: OP_OR stk=-1", ip - 1);
  816. break;
  817. }
  818. case OP_ADD:
  819. {
  820. Con::printf("%i: OP_ADD stk=-1", ip - 1);
  821. break;
  822. }
  823. case OP_SUB:
  824. {
  825. Con::printf("%i: OP_SUB stk=-1", ip - 1);
  826. break;
  827. }
  828. case OP_MUL:
  829. {
  830. Con::printf("%i: OP_MUL stk=-1", ip - 1);
  831. break;
  832. }
  833. case OP_DIV:
  834. {
  835. Con::printf("%i: OP_DIV stk=-1", ip - 1);
  836. break;
  837. }
  838. case OP_NEG:
  839. {
  840. Con::printf("%i: OP_NEG stk=0", ip - 1);
  841. break;
  842. }
  843. case OP_INC:
  844. {
  845. Con::printf("%i: OP_INC stk=0 reg=%i", ip - 1, code[ip]);
  846. ++ip;
  847. break;
  848. }
  849. case OP_SETCURVAR:
  850. {
  851. StringTableEntry var = CodeToSTE(code, ip);
  852. Con::printf("%i: OP_SETCURVAR stk=0 var=%s", ip - 1, var);
  853. ip += 2;
  854. break;
  855. }
  856. case OP_SETCURVAR_CREATE:
  857. {
  858. StringTableEntry var = CodeToSTE(code, ip);
  859. Con::printf("%i: OP_SETCURVAR_CREATE stk=0 var=%s", ip - 1, var);
  860. ip += 2;
  861. break;
  862. }
  863. case OP_SETCURVAR_ARRAY:
  864. {
  865. Con::printf("%i: OP_SETCURVAR_ARRAY stk=0", ip - 1);
  866. break;
  867. }
  868. case OP_SETCURVAR_ARRAY_CREATE:
  869. {
  870. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE stk=0", ip - 1);
  871. break;
  872. }
  873. case OP_LOADVAR_UINT:
  874. {
  875. Con::printf("%i: OP_LOADVAR_UINT stk=+1", ip - 1);
  876. break;
  877. }
  878. case OP_LOADVAR_FLT:
  879. {
  880. Con::printf("%i: OP_LOADVAR_FLT stk=+1", ip - 1);
  881. break;
  882. }
  883. case OP_LOADVAR_STR:
  884. {
  885. Con::printf("%i: OP_LOADVAR_STR stk=+1", ip - 1);
  886. break;
  887. }
  888. case OP_SAVEVAR_UINT:
  889. {
  890. Con::printf("%i: OP_SAVEVAR_UINT stk=0", ip - 1);
  891. break;
  892. }
  893. case OP_SAVEVAR_FLT:
  894. {
  895. Con::printf("%i: OP_SAVEVAR_FLT stk=0", ip - 1);
  896. break;
  897. }
  898. case OP_SAVEVAR_STR:
  899. {
  900. Con::printf("%i: OP_SAVEVAR_STR stk=0", ip - 1);
  901. break;
  902. }
  903. case OP_LOAD_LOCAL_VAR_UINT:
  904. {
  905. Con::printf("%i: OP_LOAD_LOCAL_VAR_UINT stk=+1 reg=%i", ip - 1, code[ip]);
  906. ++ip;
  907. break;
  908. }
  909. case OP_LOAD_LOCAL_VAR_FLT:
  910. {
  911. Con::printf("%i: OP_LOAD_LOCAL_VAR_FLT stk=+1 reg=%i", ip - 1, code[ip]);
  912. ++ip;
  913. break;
  914. }
  915. case OP_LOAD_LOCAL_VAR_STR:
  916. {
  917. Con::printf("%i: OP_LOAD_LOCAL_VAR_STR stk=+1 reg=%i", ip - 1, code[ip]);
  918. ++ip;
  919. break;
  920. }
  921. case OP_SAVE_LOCAL_VAR_UINT:
  922. {
  923. Con::printf("%i: OP_SAVE_LOCAL_VAR_UINT stk=0 reg=%i", ip - 1, code[ip]);
  924. ++ip;
  925. break;
  926. }
  927. case OP_SAVE_LOCAL_VAR_FLT:
  928. {
  929. Con::printf("%i: OP_SAVE_LOCAL_VAR_FLT stk=0 reg=%i", ip - 1, code[ip]);
  930. ++ip;
  931. break;
  932. }
  933. case OP_SAVE_LOCAL_VAR_STR:
  934. {
  935. Con::printf("%i: OP_SAVE_LOCAL_VAR_STR stk=0 reg=%i", ip - 1, code[ip]);
  936. ++ip;
  937. break;
  938. }
  939. case OP_SETCUROBJECT:
  940. {
  941. Con::printf("%i: OP_SETCUROBJECT stk=0", ip - 1);
  942. break;
  943. }
  944. case OP_SETCUROBJECT_NEW:
  945. {
  946. Con::printf("%i: OP_SETCUROBJECT_NEW stk=0", ip - 1);
  947. break;
  948. }
  949. case OP_SETCUROBJECT_INTERNAL:
  950. {
  951. Con::printf("%i: OP_SETCUROBJECT_INTERNAL stk=0", ip - 1);
  952. ++ip;
  953. break;
  954. }
  955. case OP_SETCURFIELD:
  956. {
  957. StringTableEntry curField = CodeToSTE(code, ip);
  958. Con::printf("%i: OP_SETCURFIELD stk=0 field=%s", ip - 1, curField);
  959. ip += 2;
  960. break;
  961. }
  962. case OP_SETCURFIELD_ARRAY:
  963. {
  964. Con::printf("%i: OP_SETCURFIELD_ARRAY stk=0", ip - 1);
  965. break;
  966. }
  967. case OP_SETCURFIELD_TYPE:
  968. {
  969. U32 type = code[ip];
  970. Con::printf("%i: OP_SETCURFIELD_TYPE stk=0 type=%i", ip - 1, type);
  971. ++ip;
  972. break;
  973. }
  974. case OP_LOADFIELD_UINT:
  975. {
  976. Con::printf("%i: OP_LOADFIELD_UINT stk=+1", ip - 1);
  977. break;
  978. }
  979. case OP_LOADFIELD_FLT:
  980. {
  981. Con::printf("%i: OP_LOADFIELD_FLT stk=+1", ip - 1);
  982. break;
  983. }
  984. case OP_LOADFIELD_STR:
  985. {
  986. Con::printf("%i: OP_LOADFIELD_STR stk=+1", ip - 1);
  987. break;
  988. }
  989. case OP_SAVEFIELD_UINT:
  990. {
  991. Con::printf("%i: OP_SAVEFIELD_UINT stk=0", ip - 1);
  992. break;
  993. }
  994. case OP_SAVEFIELD_FLT:
  995. {
  996. Con::printf("%i: OP_SAVEFIELD_FLT stk=0", ip - 1);
  997. break;
  998. }
  999. case OP_SAVEFIELD_STR:
  1000. {
  1001. Con::printf("%i: OP_SAVEFIELD_STR stk=0", ip - 1);
  1002. break;
  1003. }
  1004. case OP_POP_STK:
  1005. {
  1006. Con::printf("%i: OP_POP_STK stk=-1", ip - 1);
  1007. break;
  1008. }
  1009. case OP_LOADIMMED_UINT:
  1010. {
  1011. U32 val = code[ip];
  1012. Con::printf("%i: OP_LOADIMMED_UINT stk=+1 val=%i", ip - 1, val);
  1013. ++ip;
  1014. break;
  1015. }
  1016. case OP_LOADIMMED_FLT:
  1017. {
  1018. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  1019. Con::printf("%i: OP_LOADIMMED_FLT stk=+1 val=%f", ip - 1, val);
  1020. ++ip;
  1021. break;
  1022. }
  1023. case OP_TAG_TO_STR:
  1024. {
  1025. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1026. Con::printf("%i: OP_TAG_TO_STR stk=0 str=%s", ip - 1, str);
  1027. Con::printf(" OP_LOADIMMED_STR stk=+1 (fallthrough)");
  1028. ++ip;
  1029. break;
  1030. }
  1031. case OP_LOADIMMED_STR:
  1032. {
  1033. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1034. Con::printf("%i: OP_LOADIMMED_STR stk=+1 str=%s", ip - 1, str);
  1035. ++ip;
  1036. break;
  1037. }
  1038. case OP_DOCBLOCK_STR:
  1039. {
  1040. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1041. Con::printf("%i: OP_DOCBLOCK_STR stk=0 str=%s", ip - 1, str);
  1042. ++ip;
  1043. break;
  1044. }
  1045. case OP_LOADIMMED_IDENT:
  1046. {
  1047. StringTableEntry str = CodeToSTE(code, ip);
  1048. Con::printf("%i: OP_LOADIMMED_IDENT stk=+1 str=%s", ip - 1, str);
  1049. ip += 2;
  1050. break;
  1051. }
  1052. case OP_CALLFUNC:
  1053. {
  1054. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1055. StringTableEntry fnName = CodeToSTE(code, ip);
  1056. U32 callType = code[ip + 4];
  1057. StringTableEntry callTypeName;
  1058. switch (callType)
  1059. {
  1060. case FuncCallExprNode::FunctionCall: callTypeName = "FunctionCall"; break;
  1061. case FuncCallExprNode::MethodCall: callTypeName = "MethodCall"; break;
  1062. case FuncCallExprNode::ParentCall: callTypeName = "ParentCall"; break;
  1063. case FuncCallExprNode::StaticCall: callTypeName = "StaticCall"; break;
  1064. }
  1065. Con::printf("%i: OP_CALLFUNC stk=+1 name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace, callTypeName);
  1066. ip += 5;
  1067. break;
  1068. }
  1069. case OP_ADVANCE_STR_APPENDCHAR:
  1070. {
  1071. char ch = code[ip];
  1072. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR stk=0 char=%c", ip - 1, ch);
  1073. ++ip;
  1074. break;
  1075. }
  1076. case OP_REWIND_STR:
  1077. {
  1078. Con::printf("%i: OP_REWIND_STR stk=0", ip - 1);
  1079. Con::printf(" OP_TERMINATE_REWIND_STR stk=-1 (fallthrough)");
  1080. break;
  1081. }
  1082. case OP_TERMINATE_REWIND_STR:
  1083. {
  1084. Con::printf("%i: OP_TERMINATE_REWIND_STR stk=-1", ip - 1);
  1085. break;
  1086. }
  1087. case OP_COMPARE_STR:
  1088. {
  1089. Con::printf("%i: OP_COMPARE_STR stk=-1", ip - 1);
  1090. break;
  1091. }
  1092. case OP_PUSH:
  1093. {
  1094. Con::printf("%i: OP_PUSH stk=-1", ip - 1);
  1095. break;
  1096. }
  1097. case OP_PUSH_FRAME:
  1098. {
  1099. Con::printf("%i: OP_PUSH_FRAME stk=0 count=%i", ip - 1, code[ip]);
  1100. ++ip;
  1101. break;
  1102. }
  1103. case OP_ASSERT:
  1104. {
  1105. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1106. Con::printf("%i: OP_ASSERT stk=-1 message=%s", ip - 1, message);
  1107. ++ip;
  1108. break;
  1109. }
  1110. case OP_BREAK:
  1111. {
  1112. Con::printf("%i: OP_BREAK stk=0", ip - 1);
  1113. break;
  1114. }
  1115. case OP_ITER_BEGIN:
  1116. {
  1117. bool isGlobal = code[ip];
  1118. if (isGlobal)
  1119. {
  1120. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1121. U32 failIp = code[ip + 3];
  1122. Con::printf("%i: OP_ITER_BEGIN stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1123. ip += 4;
  1124. }
  1125. else
  1126. {
  1127. S32 reg = code[ip + 1];
  1128. U32 failIp = code[ip + 2];
  1129. Con::printf("%i: OP_ITER_BEGIN stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1130. ip += 3;
  1131. }
  1132. break;
  1133. }
  1134. case OP_ITER_BEGIN_STR:
  1135. {
  1136. bool isGlobal = code[ip];
  1137. if (isGlobal)
  1138. {
  1139. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1140. U32 failIp = code[ip + 3];
  1141. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1142. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1143. ip += 4;
  1144. }
  1145. else
  1146. {
  1147. S32 reg = code[ip + 1];
  1148. U32 failIp = code[ip + 2];
  1149. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1150. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1151. ip += 3;
  1152. }
  1153. break;
  1154. }
  1155. case OP_ITER:
  1156. {
  1157. U32 breakIp = code[ip];
  1158. Con::printf("%i: OP_ITER stk=0 breakIp=%i", ip - 1, breakIp);
  1159. ++ip;
  1160. break;
  1161. }
  1162. case OP_ITER_END:
  1163. {
  1164. Con::printf("%i: OP_ITER_END stk=-1", ip - 1);
  1165. break;
  1166. }
  1167. default:
  1168. Con::printf("%i: !!INVALID!!", ip - 1);
  1169. break;
  1170. }
  1171. }
  1172. smInFunction = false;
  1173. }