codeBlock.cpp 32 KB

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