codeBlock.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  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. StringTableEntry fnName = st.readSTString();
  363. U32 count;
  364. st.read(&count);
  365. for (U32 j = 0; j < count; j++)
  366. {
  367. StringTableEntry varName = st.readSTString();
  368. variableRegisterTable.localVarToRegister[fnName].varList.push_back(varName);
  369. }
  370. }
  371. }
  372. U32 codeLength;
  373. st.read(&codeLength);
  374. st.read(&lineBreakPairCount);
  375. U32 totSize = codeLength + lineBreakPairCount * 2;
  376. code = new U32[totSize];
  377. // 0xFF is used as a flag to help compress the bytecode.
  378. // If detected, the bytecode is only a U8.
  379. for (i = 0; i < codeLength; i++)
  380. {
  381. U8 b;
  382. st.read(&b);
  383. if (b == 0xFF)
  384. st.read(&code[i]);
  385. else
  386. code[i] = b;
  387. }
  388. for (i = codeLength; i < totSize; i++)
  389. st.read(&code[i]);
  390. lineBreakPairs = code + codeLength;
  391. // StringTable-ize our identifiers.
  392. U32 identCount;
  393. st.read(&identCount);
  394. while (identCount--)
  395. {
  396. U32 offset;
  397. st.read(&offset);
  398. StringTableEntry ste;
  399. if (offset < globalSize)
  400. ste = StringTable->insert(globalStrings + offset);
  401. else
  402. ste = StringTable->EmptyString();
  403. U32 count;
  404. st.read(&count);
  405. while (count--)
  406. {
  407. U32 ip;
  408. st.read(&ip);
  409. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  410. *(U64*)(code + ip) = (U64)ste;
  411. #else
  412. code[ip] = *((U32 *)&ste);
  413. #endif
  414. }
  415. }
  416. if (lineBreakPairCount)
  417. calcBreakList();
  418. return true;
  419. }
  420. bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, const char *inScript, bool overrideNoDso)
  421. {
  422. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  423. // This will return true, but return value is ignored
  424. char *script;
  425. chompUTF8BOM(inScript, &script);
  426. gSyntaxError = false;
  427. consoleAllocReset();
  428. STEtoCode = compileSTEtoCode;
  429. gStatementList = NULL;
  430. // Set up the parser.
  431. smCurrentParser = getParserForFile(fileName);
  432. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  433. // Now do some parsing.
  434. smCurrentParser->setScanBuffer(script, fileName);
  435. smCurrentParser->restart(NULL);
  436. smCurrentParser->parse();
  437. if (gSyntaxError)
  438. {
  439. consoleAllocReset();
  440. return false;
  441. }
  442. #ifdef TORQUE_NO_DSO_GENERATION
  443. if (!overrideNoDso)
  444. return false;
  445. #endif // !TORQUE_NO_DSO_GENERATION
  446. FileStream st;
  447. if (!st.open(codeFileName, Torque::FS::File::Write))
  448. return false;
  449. st.write(U32(Con::DSOVersion));
  450. // Reset all our value tables...
  451. resetTables();
  452. smInFunction = false;
  453. CodeStream codeStream;
  454. U32 lastIp;
  455. if (gStatementList)
  456. {
  457. lastIp = compileBlock(gStatementList, codeStream, 0) + 1;
  458. }
  459. else
  460. {
  461. codeSize = 1;
  462. lastIp = 0;
  463. }
  464. codeStream.emit(OP_RETURN_VOID);
  465. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  466. lineBreakPairCount = codeStream.getNumLineBreaks();
  467. // Write string table data...
  468. getGlobalStringTable().write(st);
  469. getFunctionStringTable().write(st);
  470. // Write float table data...
  471. getGlobalFloatTable().write(st);
  472. getFunctionFloatTable().write(st);
  473. // write variable mapping table
  474. getFunctionVariableMappingTable().write(st);
  475. if (lastIp != codeSize)
  476. Con::errorf(ConsoleLogEntry::General, "CodeBlock::compile - precompile size mismatch, a precompile/compile function pair is probably mismatched.");
  477. U32 totSize = codeSize + codeStream.getNumLineBreaks() * 2;
  478. st.write(codeSize);
  479. st.write(lineBreakPairCount);
  480. // Write out our bytecode, doing a bit of compression for low numbers.
  481. U32 i;
  482. for (i = 0; i < codeSize; i++)
  483. {
  484. if (code[i] < 0xFF)
  485. st.write(U8(code[i]));
  486. else
  487. {
  488. st.write(U8(0xFF));
  489. st.write(code[i]);
  490. }
  491. }
  492. // Write the break info...
  493. for (i = codeSize; i < totSize; i++)
  494. st.write(code[i]);
  495. getIdentTable().write(st);
  496. consoleAllocReset();
  497. st.close();
  498. return true;
  499. }
  500. ConsoleValue CodeBlock::compileExec(StringTableEntry fileName, const char *inString, bool noCalls, S32 setFrame)
  501. {
  502. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  503. // Check for a UTF8 script file
  504. char *string;
  505. chompUTF8BOM(inString, &string);
  506. STEtoCode = evalSTEtoCode;
  507. consoleAllocReset();
  508. name = fileName;
  509. if (fileName)
  510. {
  511. const StringTableEntry exePath = Platform::getMainDotCsDir();
  512. const StringTableEntry cwd = Platform::getCurrentDirectory();
  513. fullPath = NULL;
  514. if (Platform::isFullPath(fileName))
  515. fullPath = fileName;
  516. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  517. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  518. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  519. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  520. if (fullPath == NULL)
  521. {
  522. char buf[1024];
  523. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  524. }
  525. modPath = Con::getModNameFromPath(fileName);
  526. }
  527. if (name)
  528. addToCodeList();
  529. gStatementList = NULL;
  530. // Set up the parser.
  531. smCurrentParser = getParserForFile(fileName);
  532. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  533. // Now do some parsing.
  534. smCurrentParser->setScanBuffer(string, fileName);
  535. smCurrentParser->restart(NULL);
  536. smCurrentParser->parse();
  537. if (!gStatementList)
  538. {
  539. delete this;
  540. return std::move(ConsoleValue());
  541. }
  542. resetTables();
  543. smInFunction = false;
  544. CodeStream codeStream;
  545. U32 lastIp = compileBlock(gStatementList, codeStream, 0);
  546. lineBreakPairCount = codeStream.getNumLineBreaks();
  547. globalStrings = getGlobalStringTable().build();
  548. globalStringsMaxLen = getGlobalStringTable().totalLen;
  549. functionStrings = getFunctionStringTable().build();
  550. functionStringsMaxLen = getFunctionStringTable().totalLen;
  551. globalFloats = getGlobalFloatTable().build();
  552. functionFloats = getFunctionFloatTable().build();
  553. variableRegisterTable = getFunctionVariableMappingTable().copy();
  554. codeStream.emit(OP_RETURN_VOID);
  555. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  556. consoleAllocReset();
  557. #ifndef TORQUE_SHIPPING
  558. if (Con::getBoolVariable("$Debug::DumpByteCode"))
  559. {
  560. dumpInstructions();
  561. }
  562. #endif
  563. if (lineBreakPairCount && fileName)
  564. calcBreakList();
  565. if (lastIp + 1 != codeSize)
  566. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
  567. return std::move(exec(0, fileName, NULL, 0, 0, noCalls, NULL, setFrame));
  568. }
  569. //-------------------------------------------------------------------------
  570. void CodeBlock::incRefCount()
  571. {
  572. refCount++;
  573. }
  574. void CodeBlock::decRefCount()
  575. {
  576. refCount--;
  577. if (!refCount)
  578. delete this;
  579. }
  580. //-------------------------------------------------------------------------
  581. String CodeBlock::getFunctionArgs(U32 ip)
  582. {
  583. StringBuilder str;
  584. StringTableEntry fnName = CodeToSTE(code, ip);
  585. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  586. StringTableEntry fnNsName = StringTable->insert(avar("%s::%s", fnNamespace, fnName));
  587. U32 fnArgc = code[ip + 8];
  588. for (U32 i = 0; i < fnArgc; ++i)
  589. {
  590. StringTableEntry var = variableRegisterTable.localVarToRegister[fnNsName].varList[i];
  591. if (i != 0)
  592. str.append(", ");
  593. str.append("string ");
  594. // Try to capture junked parameters
  595. if (var[0])
  596. str.append(var + 1);
  597. else
  598. str.append("JUNK");
  599. }
  600. return str.end();
  601. }
  602. //-------------------------------------------------------------------------
  603. void CodeBlock::dumpInstructions(U32 startIp, bool upToReturn)
  604. {
  605. U32 ip = startIp;
  606. smInFunction = false;
  607. U32 endFuncIp = 0;
  608. while (ip < codeSize)
  609. {
  610. if (ip > endFuncIp)
  611. {
  612. smInFunction = false;
  613. }
  614. switch (code[ip++])
  615. {
  616. case OP_FUNC_DECL:
  617. {
  618. StringTableEntry fnName = CodeToSTE(code, ip);
  619. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  620. StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
  621. bool hasBody = bool(code[ip + 6]);
  622. U32 newIp = code[ip + 7];
  623. U32 argc = code[ip + 8];
  624. U32 regCount = code[ip + 9];
  625. endFuncIp = newIp;
  626. Con::printf("%i: OP_FUNC_DECL stk=+0 name=%s nspace=%s package=%s hasbody=%i newip=%i argc=%i regCount=%i",
  627. ip - 1, fnName, fnNamespace, fnPackage, hasBody, newIp, argc, regCount);
  628. // Skip args.
  629. ip += 10 + argc;
  630. smInFunction = true;
  631. break;
  632. }
  633. case OP_CREATE_OBJECT:
  634. {
  635. StringTableEntry objParent = CodeToSTE(code, ip);
  636. bool isDataBlock = code[ip + 2];
  637. bool isInternal = code[ip + 3];
  638. bool isSingleton = code[ip + 4];
  639. U32 lineNumber = code[ip + 5];
  640. U32 failJump = code[ip + 6];
  641. Con::printf("%i: OP_CREATE_OBJECT stk=+0 objParent=%s isDataBlock=%i isInternal=%i isSingleton=%i lineNumber=%i failJump=%i",
  642. ip - 1, objParent, isDataBlock, isInternal, isSingleton, lineNumber, failJump);
  643. ip += 7;
  644. break;
  645. }
  646. case OP_ADD_OBJECT:
  647. {
  648. bool placeAtRoot = code[ip++];
  649. const char* stk = placeAtRoot ? "+1" : "0";
  650. Con::printf("%i: OP_ADD_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  651. break;
  652. }
  653. case OP_END_OBJECT:
  654. {
  655. bool placeAtRoot = code[ip++];
  656. const char* stk = placeAtRoot ? "-1" : "0";
  657. Con::printf("%i: OP_END_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  658. break;
  659. }
  660. case OP_FINISH_OBJECT:
  661. {
  662. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  663. break;
  664. }
  665. case OP_JMPIFFNOT:
  666. {
  667. Con::printf("%i: OP_JMPIFFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  668. ++ip;
  669. break;
  670. }
  671. case OP_JMPIFNOT:
  672. {
  673. Con::printf("%i: OP_JMPIFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  674. ++ip;
  675. break;
  676. }
  677. case OP_JMPIFF:
  678. {
  679. Con::printf("%i: OP_JMPIFF stk=-1 ip=%i", ip - 1, code[ip]);
  680. ++ip;
  681. break;
  682. }
  683. case OP_JMPIF:
  684. {
  685. Con::printf("%i: OP_JMPIF stk=-1 ip=%i", ip - 1, code[ip]);
  686. ++ip;
  687. break;
  688. }
  689. case OP_JMPIFNOT_NP:
  690. {
  691. Con::printf("%i: OP_JMPIFNOT_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  692. ++ip;
  693. break;
  694. }
  695. case OP_JMPIF_NP:
  696. {
  697. Con::printf("%i: OP_JMPIF_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  698. ++ip;
  699. break;
  700. }
  701. case OP_JMP:
  702. {
  703. Con::printf("%i: OP_JMP stk=0 ip=%i", ip - 1, code[ip]);
  704. ++ip;
  705. break;
  706. }
  707. case OP_RETURN_VOID:
  708. {
  709. Con::printf("%i: OP_RETURN_VOID stk=0", ip - 1);
  710. if (upToReturn)
  711. return;
  712. break;
  713. }
  714. case OP_RETURN:
  715. {
  716. Con::printf("%i: OP_RETURN stk=-1", ip - 1);
  717. if (upToReturn)
  718. return;
  719. break;
  720. }
  721. case OP_RETURN_UINT:
  722. {
  723. Con::printf("%i: OP_RETURNUINT stk=-1", ip - 1);
  724. if (upToReturn)
  725. return;
  726. break;
  727. }
  728. case OP_RETURN_FLT:
  729. {
  730. Con::printf("%i: OP_RETURNFLT stk=-1", ip - 1);
  731. if (upToReturn)
  732. return;
  733. break;
  734. }
  735. case OP_CMPEQ:
  736. {
  737. Con::printf("%i: OP_CMPEQ stk=-1", ip - 1);
  738. break;
  739. }
  740. case OP_CMPGR:
  741. {
  742. Con::printf("%i: OP_CMPGR stk=-1", ip - 1);
  743. break;
  744. }
  745. case OP_CMPGE:
  746. {
  747. Con::printf("%i: OP_CMPGE stk=-1", ip - 1);
  748. break;
  749. }
  750. case OP_CMPLT:
  751. {
  752. Con::printf("%i: OP_CMPLT stk=-1", ip - 1);
  753. break;
  754. }
  755. case OP_CMPLE:
  756. {
  757. Con::printf("%i: OP_CMPLE stk=-1", ip - 1);
  758. break;
  759. }
  760. case OP_CMPNE:
  761. {
  762. Con::printf("%i: OP_CMPNE stk=-1", ip - 1);
  763. break;
  764. }
  765. case OP_XOR:
  766. {
  767. Con::printf("%i: OP_XOR stk=-1", ip - 1);
  768. break;
  769. }
  770. case OP_MOD:
  771. {
  772. Con::printf("%i: OP_MOD stk=-1", ip - 1);
  773. break;
  774. }
  775. case OP_BITAND:
  776. {
  777. Con::printf("%i: OP_BITAND stk=-1", ip - 1);
  778. break;
  779. }
  780. case OP_BITOR:
  781. {
  782. Con::printf("%i: OP_BITOR stk=-1", ip - 1);
  783. break;
  784. }
  785. case OP_NOT:
  786. {
  787. Con::printf("%i: OP_NOT stk=0", ip - 1);
  788. break;
  789. }
  790. case OP_NOTF:
  791. {
  792. Con::printf("%i: OP_NOTF stk=0", ip - 1);
  793. break;
  794. }
  795. case OP_ONESCOMPLEMENT:
  796. {
  797. Con::printf("%i: OP_ONESCOMPLEMENT stk=0", ip - 1);
  798. break;
  799. }
  800. case OP_SHR:
  801. {
  802. Con::printf("%i: OP_SHR stk=-1", ip - 1);
  803. break;
  804. }
  805. case OP_SHL:
  806. {
  807. Con::printf("%i: OP_SHL stk=-1", ip - 1);
  808. break;
  809. }
  810. case OP_AND:
  811. {
  812. Con::printf("%i: OP_AND stk=-1", ip - 1);
  813. break;
  814. }
  815. case OP_OR:
  816. {
  817. Con::printf("%i: OP_OR stk=-1", ip - 1);
  818. break;
  819. }
  820. case OP_ADD:
  821. {
  822. Con::printf("%i: OP_ADD stk=-1", ip - 1);
  823. break;
  824. }
  825. case OP_SUB:
  826. {
  827. Con::printf("%i: OP_SUB stk=-1", ip - 1);
  828. break;
  829. }
  830. case OP_MUL:
  831. {
  832. Con::printf("%i: OP_MUL stk=-1", ip - 1);
  833. break;
  834. }
  835. case OP_DIV:
  836. {
  837. Con::printf("%i: OP_DIV stk=-1", ip - 1);
  838. break;
  839. }
  840. case OP_NEG:
  841. {
  842. Con::printf("%i: OP_NEG stk=0", ip - 1);
  843. break;
  844. }
  845. case OP_INC:
  846. {
  847. Con::printf("%i: OP_INC stk=0 reg=%i", ip - 1, code[ip]);
  848. ++ip;
  849. break;
  850. }
  851. case OP_SETCURVAR:
  852. {
  853. StringTableEntry var = CodeToSTE(code, ip);
  854. Con::printf("%i: OP_SETCURVAR stk=0 var=%s", ip - 1, var);
  855. ip += 2;
  856. break;
  857. }
  858. case OP_SETCURVAR_CREATE:
  859. {
  860. StringTableEntry var = CodeToSTE(code, ip);
  861. Con::printf("%i: OP_SETCURVAR_CREATE stk=0 var=%s", ip - 1, var);
  862. ip += 2;
  863. break;
  864. }
  865. case OP_SETCURVAR_ARRAY:
  866. {
  867. Con::printf("%i: OP_SETCURVAR_ARRAY stk=0", ip - 1);
  868. break;
  869. }
  870. case OP_SETCURVAR_ARRAY_CREATE:
  871. {
  872. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE stk=0", ip - 1);
  873. break;
  874. }
  875. case OP_LOADVAR_UINT:
  876. {
  877. Con::printf("%i: OP_LOADVAR_UINT stk=+1", ip - 1);
  878. break;
  879. }
  880. case OP_LOADVAR_FLT:
  881. {
  882. Con::printf("%i: OP_LOADVAR_FLT stk=+1", ip - 1);
  883. break;
  884. }
  885. case OP_LOADVAR_STR:
  886. {
  887. Con::printf("%i: OP_LOADVAR_STR stk=+1", ip - 1);
  888. break;
  889. }
  890. case OP_SAVEVAR_UINT:
  891. {
  892. Con::printf("%i: OP_SAVEVAR_UINT stk=0", ip - 1);
  893. break;
  894. }
  895. case OP_SAVEVAR_FLT:
  896. {
  897. Con::printf("%i: OP_SAVEVAR_FLT stk=0", ip - 1);
  898. break;
  899. }
  900. case OP_SAVEVAR_STR:
  901. {
  902. Con::printf("%i: OP_SAVEVAR_STR stk=0", ip - 1);
  903. break;
  904. }
  905. case OP_LOAD_LOCAL_VAR_UINT:
  906. {
  907. Con::printf("%i: OP_LOAD_LOCAL_VAR_UINT stk=+1 reg=%i", ip - 1, code[ip]);
  908. ++ip;
  909. break;
  910. }
  911. case OP_LOAD_LOCAL_VAR_FLT:
  912. {
  913. Con::printf("%i: OP_LOAD_LOCAL_VAR_FLT stk=+1 reg=%i", ip - 1, code[ip]);
  914. ++ip;
  915. break;
  916. }
  917. case OP_LOAD_LOCAL_VAR_STR:
  918. {
  919. Con::printf("%i: OP_LOAD_LOCAL_VAR_STR stk=+1 reg=%i", ip - 1, code[ip]);
  920. ++ip;
  921. break;
  922. }
  923. case OP_SAVE_LOCAL_VAR_UINT:
  924. {
  925. Con::printf("%i: OP_SAVE_LOCAL_VAR_UINT stk=0 reg=%i", ip - 1, code[ip]);
  926. ++ip;
  927. break;
  928. }
  929. case OP_SAVE_LOCAL_VAR_FLT:
  930. {
  931. Con::printf("%i: OP_SAVE_LOCAL_VAR_FLT stk=0 reg=%i", ip - 1, code[ip]);
  932. ++ip;
  933. break;
  934. }
  935. case OP_SAVE_LOCAL_VAR_STR:
  936. {
  937. Con::printf("%i: OP_SAVE_LOCAL_VAR_STR stk=0 reg=%i", ip - 1, code[ip]);
  938. ++ip;
  939. break;
  940. }
  941. case OP_SETCUROBJECT:
  942. {
  943. Con::printf("%i: OP_SETCUROBJECT stk=0", ip - 1);
  944. break;
  945. }
  946. case OP_SETCUROBJECT_NEW:
  947. {
  948. Con::printf("%i: OP_SETCUROBJECT_NEW stk=0", ip - 1);
  949. break;
  950. }
  951. case OP_SETCUROBJECT_INTERNAL:
  952. {
  953. Con::printf("%i: OP_SETCUROBJECT_INTERNAL stk=0", ip - 1);
  954. ++ip;
  955. break;
  956. }
  957. case OP_SETCURFIELD:
  958. {
  959. StringTableEntry curField = CodeToSTE(code, ip);
  960. Con::printf("%i: OP_SETCURFIELD stk=0 field=%s", ip - 1, curField);
  961. ip += 2;
  962. break;
  963. }
  964. case OP_SETCURFIELD_ARRAY:
  965. {
  966. Con::printf("%i: OP_SETCURFIELD_ARRAY stk=0", ip - 1);
  967. break;
  968. }
  969. case OP_SETCURFIELD_TYPE:
  970. {
  971. U32 type = code[ip];
  972. Con::printf("%i: OP_SETCURFIELD_TYPE stk=0 type=%i", ip - 1, type);
  973. ++ip;
  974. break;
  975. }
  976. case OP_LOADFIELD_UINT:
  977. {
  978. Con::printf("%i: OP_LOADFIELD_UINT stk=+1", ip - 1);
  979. break;
  980. }
  981. case OP_LOADFIELD_FLT:
  982. {
  983. Con::printf("%i: OP_LOADFIELD_FLT stk=+1", ip - 1);
  984. break;
  985. }
  986. case OP_LOADFIELD_STR:
  987. {
  988. Con::printf("%i: OP_LOADFIELD_STR stk=+1", ip - 1);
  989. break;
  990. }
  991. case OP_SAVEFIELD_UINT:
  992. {
  993. Con::printf("%i: OP_SAVEFIELD_UINT stk=0", ip - 1);
  994. break;
  995. }
  996. case OP_SAVEFIELD_FLT:
  997. {
  998. Con::printf("%i: OP_SAVEFIELD_FLT stk=0", ip - 1);
  999. break;
  1000. }
  1001. case OP_SAVEFIELD_STR:
  1002. {
  1003. Con::printf("%i: OP_SAVEFIELD_STR stk=0", ip - 1);
  1004. break;
  1005. }
  1006. case OP_POP_STK:
  1007. {
  1008. Con::printf("%i: OP_POP_STK stk=-1", ip - 1);
  1009. break;
  1010. }
  1011. case OP_LOADIMMED_UINT:
  1012. {
  1013. U32 val = code[ip];
  1014. Con::printf("%i: OP_LOADIMMED_UINT stk=+1 val=%i", ip - 1, val);
  1015. ++ip;
  1016. break;
  1017. }
  1018. case OP_LOADIMMED_FLT:
  1019. {
  1020. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  1021. Con::printf("%i: OP_LOADIMMED_FLT stk=+1 val=%f", ip - 1, val);
  1022. ++ip;
  1023. break;
  1024. }
  1025. case OP_TAG_TO_STR:
  1026. {
  1027. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1028. Con::printf("%i: OP_TAG_TO_STR stk=0 str=%s", ip - 1, str);
  1029. Con::printf(" OP_LOADIMMED_STR stk=+1 (fallthrough)");
  1030. ++ip;
  1031. break;
  1032. }
  1033. case OP_LOADIMMED_STR:
  1034. {
  1035. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1036. Con::printf("%i: OP_LOADIMMED_STR stk=+1 str=%s", ip - 1, str);
  1037. ++ip;
  1038. break;
  1039. }
  1040. case OP_DOCBLOCK_STR:
  1041. {
  1042. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1043. Con::printf("%i: OP_DOCBLOCK_STR stk=0 str=%s", ip - 1, str);
  1044. ++ip;
  1045. break;
  1046. }
  1047. case OP_LOADIMMED_IDENT:
  1048. {
  1049. StringTableEntry str = CodeToSTE(code, ip);
  1050. Con::printf("%i: OP_LOADIMMED_IDENT stk=+1 str=%s", ip - 1, str);
  1051. ip += 2;
  1052. break;
  1053. }
  1054. case OP_CALLFUNC:
  1055. {
  1056. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1057. StringTableEntry fnName = CodeToSTE(code, ip);
  1058. U32 callType = code[ip + 4];
  1059. StringTableEntry callTypeName;
  1060. switch (callType)
  1061. {
  1062. case FuncCallExprNode::FunctionCall: callTypeName = "FunctionCall"; break;
  1063. case FuncCallExprNode::MethodCall: callTypeName = "MethodCall"; break;
  1064. case FuncCallExprNode::ParentCall: callTypeName = "ParentCall"; break;
  1065. case FuncCallExprNode::StaticCall: callTypeName = "StaticCall"; break;
  1066. }
  1067. Con::printf("%i: OP_CALLFUNC stk=+1 name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace, callTypeName);
  1068. ip += 5;
  1069. break;
  1070. }
  1071. case OP_ADVANCE_STR_APPENDCHAR:
  1072. {
  1073. char ch = code[ip];
  1074. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR stk=0 char=%c", ip - 1, ch);
  1075. ++ip;
  1076. break;
  1077. }
  1078. case OP_REWIND_STR:
  1079. {
  1080. Con::printf("%i: OP_REWIND_STR stk=0", ip - 1);
  1081. Con::printf(" OP_TERMINATE_REWIND_STR stk=-1 (fallthrough)");
  1082. break;
  1083. }
  1084. case OP_TERMINATE_REWIND_STR:
  1085. {
  1086. Con::printf("%i: OP_TERMINATE_REWIND_STR stk=-1", ip - 1);
  1087. break;
  1088. }
  1089. case OP_COMPARE_STR:
  1090. {
  1091. Con::printf("%i: OP_COMPARE_STR stk=-1", ip - 1);
  1092. break;
  1093. }
  1094. case OP_PUSH:
  1095. {
  1096. Con::printf("%i: OP_PUSH stk=-1", ip - 1);
  1097. break;
  1098. }
  1099. case OP_PUSH_FRAME:
  1100. {
  1101. Con::printf("%i: OP_PUSH_FRAME stk=0 count=%i", ip - 1, code[ip]);
  1102. ++ip;
  1103. break;
  1104. }
  1105. case OP_ASSERT:
  1106. {
  1107. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1108. Con::printf("%i: OP_ASSERT stk=-1 message=%s", ip - 1, message);
  1109. ++ip;
  1110. break;
  1111. }
  1112. case OP_BREAK:
  1113. {
  1114. Con::printf("%i: OP_BREAK stk=0", ip - 1);
  1115. break;
  1116. }
  1117. case OP_ITER_BEGIN:
  1118. {
  1119. bool isGlobal = code[ip];
  1120. if (isGlobal)
  1121. {
  1122. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1123. U32 failIp = code[ip + 3];
  1124. Con::printf("%i: OP_ITER_BEGIN stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1125. ip += 4;
  1126. }
  1127. else
  1128. {
  1129. S32 reg = code[ip + 1];
  1130. U32 failIp = code[ip + 2];
  1131. Con::printf("%i: OP_ITER_BEGIN stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1132. ip += 3;
  1133. }
  1134. break;
  1135. }
  1136. case OP_ITER_BEGIN_STR:
  1137. {
  1138. bool isGlobal = code[ip];
  1139. if (isGlobal)
  1140. {
  1141. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1142. U32 failIp = code[ip + 3];
  1143. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1144. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1145. ip += 4;
  1146. }
  1147. else
  1148. {
  1149. S32 reg = code[ip + 1];
  1150. U32 failIp = code[ip + 2];
  1151. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1152. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1153. ip += 3;
  1154. }
  1155. break;
  1156. }
  1157. case OP_ITER:
  1158. {
  1159. U32 breakIp = code[ip];
  1160. Con::printf("%i: OP_ITER stk=0 breakIp=%i", ip - 1, breakIp);
  1161. ++ip;
  1162. break;
  1163. }
  1164. case OP_ITER_END:
  1165. {
  1166. Con::printf("%i: OP_ITER_END stk=-1", ip - 1);
  1167. break;
  1168. }
  1169. default:
  1170. Con::printf("%i: !!INVALID!!", ip - 1);
  1171. break;
  1172. }
  1173. }
  1174. smInFunction = false;
  1175. }