2
0

codeBlock.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. variableRegisterTable = getFunctionVariableMappingTable().copy();
  536. codeStream.emit(OP_RETURN_VOID);
  537. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  538. //if (Con::getBoolVariable("dump"))
  539. //dumpInstructions(0, false);
  540. consoleAllocReset();
  541. if (lineBreakPairCount && fileName)
  542. calcBreakList();
  543. if (lastIp + 1 != codeSize)
  544. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
  545. return std::move(exec(0, fileName, NULL, 0, 0, noCalls, NULL, setFrame));
  546. }
  547. //-------------------------------------------------------------------------
  548. void CodeBlock::incRefCount()
  549. {
  550. refCount++;
  551. }
  552. void CodeBlock::decRefCount()
  553. {
  554. refCount--;
  555. if (!refCount)
  556. delete this;
  557. }
  558. //-------------------------------------------------------------------------
  559. String CodeBlock::getFunctionArgs(U32 ip)
  560. {
  561. StringBuilder str;
  562. U32 fnArgc = code[ip + 8];
  563. for (U32 i = 0; i < fnArgc; ++i)
  564. {
  565. StringTableEntry var = CodeToSTE(code, ip + (i * 2) + 9);
  566. if (i != 0)
  567. str.append(", ");
  568. str.append("string ");
  569. // Try to capture junked parameters
  570. if (var[0])
  571. str.append(var + 1);
  572. else
  573. str.append("JUNK");
  574. }
  575. return str.end();
  576. }
  577. //-------------------------------------------------------------------------
  578. void CodeBlock::dumpInstructions(U32 startIp, bool upToReturn)
  579. {
  580. U32 ip = startIp;
  581. smInFunction = false;
  582. U32 endFuncIp = 0;
  583. while (ip < codeSize)
  584. {
  585. if (ip > endFuncIp)
  586. {
  587. smInFunction = false;
  588. }
  589. switch (code[ip++])
  590. {
  591. case OP_FUNC_DECL:
  592. {
  593. StringTableEntry fnName = CodeToSTE(code, ip);
  594. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  595. StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
  596. bool hasBody = bool(code[ip + 6]);
  597. U32 newIp = code[ip + 7];
  598. U32 argc = code[ip + 8];
  599. U32 regCount = code[ip + 9];
  600. endFuncIp = newIp;
  601. Con::printf("%i: OP_FUNC_DECL stk=+0 name=%s nspace=%s package=%s hasbody=%i newip=%i argc=%i regCount=%i",
  602. ip - 1, fnName, fnNamespace, fnPackage, hasBody, newIp, argc, regCount);
  603. // Skip args.
  604. ip += 10 + argc;
  605. smInFunction = true;
  606. break;
  607. }
  608. case OP_CREATE_OBJECT:
  609. {
  610. StringTableEntry objParent = CodeToSTE(code, ip);
  611. bool isDataBlock = code[ip + 2];
  612. bool isInternal = code[ip + 3];
  613. bool isSingleton = code[ip + 4];
  614. U32 lineNumber = code[ip + 5];
  615. U32 failJump = code[ip + 6];
  616. Con::printf("%i: OP_CREATE_OBJECT stk=+0 objParent=%s isDataBlock=%i isInternal=%i isSingleton=%i lineNumber=%i failJump=%i",
  617. ip - 1, objParent, isDataBlock, isInternal, isSingleton, lineNumber, failJump);
  618. ip += 7;
  619. break;
  620. }
  621. case OP_ADD_OBJECT:
  622. {
  623. bool placeAtRoot = code[ip++];
  624. const char* stk = placeAtRoot ? "+1" : "0";
  625. Con::printf("%i: OP_ADD_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  626. break;
  627. }
  628. case OP_END_OBJECT:
  629. {
  630. bool placeAtRoot = code[ip++];
  631. const char* stk = placeAtRoot ? "-1" : "0";
  632. Con::printf("%i: OP_END_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  633. break;
  634. }
  635. case OP_FINISH_OBJECT:
  636. {
  637. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  638. break;
  639. }
  640. case OP_JMPIFFNOT:
  641. {
  642. Con::printf("%i: OP_JMPIFFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  643. ++ip;
  644. break;
  645. }
  646. case OP_JMPIFNOT:
  647. {
  648. Con::printf("%i: OP_JMPIFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  649. ++ip;
  650. break;
  651. }
  652. case OP_JMPIFF:
  653. {
  654. Con::printf("%i: OP_JMPIFF stk=-1 ip=%i", ip - 1, code[ip]);
  655. ++ip;
  656. break;
  657. }
  658. case OP_JMPIF:
  659. {
  660. Con::printf("%i: OP_JMPIF stk=-1 ip=%i", ip - 1, code[ip]);
  661. ++ip;
  662. break;
  663. }
  664. case OP_JMPIFNOT_NP:
  665. {
  666. Con::printf("%i: OP_JMPIFNOT_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  667. ++ip;
  668. break;
  669. }
  670. case OP_JMPIF_NP:
  671. {
  672. Con::printf("%i: OP_JMPIF_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  673. ++ip;
  674. break;
  675. }
  676. case OP_JMP:
  677. {
  678. Con::printf("%i: OP_JMP stk=0 ip=%i", ip - 1, code[ip]);
  679. ++ip;
  680. break;
  681. }
  682. case OP_RETURN_VOID:
  683. {
  684. Con::printf("%i: OP_RETURN_VOID stk=0", ip - 1);
  685. if (upToReturn)
  686. return;
  687. break;
  688. }
  689. case OP_RETURN:
  690. {
  691. Con::printf("%i: OP_RETURN stk=-1", ip - 1);
  692. if (upToReturn)
  693. return;
  694. break;
  695. }
  696. case OP_RETURN_UINT:
  697. {
  698. Con::printf("%i: OP_RETURNUINT stk=-1", ip - 1);
  699. if (upToReturn)
  700. return;
  701. break;
  702. }
  703. case OP_RETURN_FLT:
  704. {
  705. Con::printf("%i: OP_RETURNFLT stk=-1", ip - 1);
  706. if (upToReturn)
  707. return;
  708. break;
  709. }
  710. case OP_CMPEQ:
  711. {
  712. Con::printf("%i: OP_CMPEQ stk=-1", ip - 1);
  713. break;
  714. }
  715. case OP_CMPGR:
  716. {
  717. Con::printf("%i: OP_CMPGR stk=-1", ip - 1);
  718. break;
  719. }
  720. case OP_CMPGE:
  721. {
  722. Con::printf("%i: OP_CMPGE stk=-1", ip - 1);
  723. break;
  724. }
  725. case OP_CMPLT:
  726. {
  727. Con::printf("%i: OP_CMPLT stk=-1", ip - 1);
  728. break;
  729. }
  730. case OP_CMPLE:
  731. {
  732. Con::printf("%i: OP_CMPLE stk=-1", ip - 1);
  733. break;
  734. }
  735. case OP_CMPNE:
  736. {
  737. Con::printf("%i: OP_CMPNE stk=-1", ip - 1);
  738. break;
  739. }
  740. case OP_XOR:
  741. {
  742. Con::printf("%i: OP_XOR stk=-1", ip - 1);
  743. break;
  744. }
  745. case OP_MOD:
  746. {
  747. Con::printf("%i: OP_MOD stk=-1", ip - 1);
  748. break;
  749. }
  750. case OP_BITAND:
  751. {
  752. Con::printf("%i: OP_BITAND stk=-1", ip - 1);
  753. break;
  754. }
  755. case OP_BITOR:
  756. {
  757. Con::printf("%i: OP_BITOR stk=-1", ip - 1);
  758. break;
  759. }
  760. case OP_NOT:
  761. {
  762. Con::printf("%i: OP_NOT stk=0", ip - 1);
  763. break;
  764. }
  765. case OP_NOTF:
  766. {
  767. Con::printf("%i: OP_NOTF stk=0", ip - 1);
  768. break;
  769. }
  770. case OP_ONESCOMPLEMENT:
  771. {
  772. Con::printf("%i: OP_ONESCOMPLEMENT stk=0", ip - 1);
  773. break;
  774. }
  775. case OP_SHR:
  776. {
  777. Con::printf("%i: OP_SHR stk=-1", ip - 1);
  778. break;
  779. }
  780. case OP_SHL:
  781. {
  782. Con::printf("%i: OP_SHL stk=-1", ip - 1);
  783. break;
  784. }
  785. case OP_AND:
  786. {
  787. Con::printf("%i: OP_AND stk=-1", ip - 1);
  788. break;
  789. }
  790. case OP_OR:
  791. {
  792. Con::printf("%i: OP_OR stk=-1", ip - 1);
  793. break;
  794. }
  795. case OP_ADD:
  796. {
  797. Con::printf("%i: OP_ADD stk=-1", ip - 1);
  798. break;
  799. }
  800. case OP_SUB:
  801. {
  802. Con::printf("%i: OP_SUB stk=-1", ip - 1);
  803. break;
  804. }
  805. case OP_MUL:
  806. {
  807. Con::printf("%i: OP_MUL stk=-1", ip - 1);
  808. break;
  809. }
  810. case OP_DIV:
  811. {
  812. Con::printf("%i: OP_DIV stk=-1", ip - 1);
  813. break;
  814. }
  815. case OP_NEG:
  816. {
  817. Con::printf("%i: OP_NEG stk=0", ip - 1);
  818. break;
  819. }
  820. case OP_INC:
  821. {
  822. Con::printf("%i: OP_INC stk=0 reg=%i", ip - 1, code[ip]);
  823. ++ip;
  824. break;
  825. }
  826. case OP_SETCURVAR:
  827. {
  828. StringTableEntry var = CodeToSTE(code, ip);
  829. Con::printf("%i: OP_SETCURVAR stk=0 var=%s", ip - 1, var);
  830. ip += 2;
  831. break;
  832. }
  833. case OP_SETCURVAR_CREATE:
  834. {
  835. StringTableEntry var = CodeToSTE(code, ip);
  836. Con::printf("%i: OP_SETCURVAR_CREATE stk=0 var=%s", ip - 1, var);
  837. ip += 2;
  838. break;
  839. }
  840. case OP_SETCURVAR_ARRAY:
  841. {
  842. Con::printf("%i: OP_SETCURVAR_ARRAY stk=0", ip - 1);
  843. break;
  844. }
  845. case OP_SETCURVAR_ARRAY_CREATE:
  846. {
  847. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE stk=0", ip - 1);
  848. break;
  849. }
  850. case OP_LOADVAR_UINT:
  851. {
  852. Con::printf("%i: OP_LOADVAR_UINT stk=+1", ip - 1);
  853. break;
  854. }
  855. case OP_LOADVAR_FLT:
  856. {
  857. Con::printf("%i: OP_LOADVAR_FLT stk=+1", ip - 1);
  858. break;
  859. }
  860. case OP_LOADVAR_STR:
  861. {
  862. Con::printf("%i: OP_LOADVAR_STR stk=+1", ip - 1);
  863. break;
  864. }
  865. case OP_SAVEVAR_UINT:
  866. {
  867. Con::printf("%i: OP_SAVEVAR_UINT stk=0", ip - 1);
  868. break;
  869. }
  870. case OP_SAVEVAR_FLT:
  871. {
  872. Con::printf("%i: OP_SAVEVAR_FLT stk=0", ip - 1);
  873. break;
  874. }
  875. case OP_SAVEVAR_STR:
  876. {
  877. Con::printf("%i: OP_SAVEVAR_STR stk=0", ip - 1);
  878. break;
  879. }
  880. case OP_LOAD_LOCAL_VAR_UINT:
  881. {
  882. Con::printf("%i: OP_LOAD_LOCAL_VAR_UINT stk=+1 reg=%i", ip - 1, code[ip]);
  883. ++ip;
  884. break;
  885. }
  886. case OP_LOAD_LOCAL_VAR_FLT:
  887. {
  888. Con::printf("%i: OP_LOAD_LOCAL_VAR_FLT stk=+1 reg=%i", ip - 1, code[ip]);
  889. ++ip;
  890. break;
  891. }
  892. case OP_LOAD_LOCAL_VAR_STR:
  893. {
  894. Con::printf("%i: OP_LOAD_LOCAL_VAR_STR stk=+1 reg=%i", ip - 1, code[ip]);
  895. ++ip;
  896. break;
  897. }
  898. case OP_SAVE_LOCAL_VAR_UINT:
  899. {
  900. Con::printf("%i: OP_SAVE_LOCAL_VAR_UINT stk=0 reg=%i", ip - 1, code[ip]);
  901. ++ip;
  902. break;
  903. }
  904. case OP_SAVE_LOCAL_VAR_FLT:
  905. {
  906. Con::printf("%i: OP_SAVE_LOCAL_VAR_FLT stk=0 reg=%i", ip - 1, code[ip]);
  907. ++ip;
  908. break;
  909. }
  910. case OP_SAVE_LOCAL_VAR_STR:
  911. {
  912. Con::printf("%i: OP_SAVE_LOCAL_VAR_STR stk=0 reg=%i", ip - 1, code[ip]);
  913. ++ip;
  914. break;
  915. }
  916. case OP_SETCUROBJECT:
  917. {
  918. Con::printf("%i: OP_SETCUROBJECT stk=0", ip - 1);
  919. break;
  920. }
  921. case OP_SETCUROBJECT_NEW:
  922. {
  923. Con::printf("%i: OP_SETCUROBJECT_NEW stk=0", ip - 1);
  924. break;
  925. }
  926. case OP_SETCUROBJECT_INTERNAL:
  927. {
  928. Con::printf("%i: OP_SETCUROBJECT_INTERNAL stk=0", ip - 1);
  929. ++ip;
  930. break;
  931. }
  932. case OP_SETCURFIELD:
  933. {
  934. StringTableEntry curField = CodeToSTE(code, ip);
  935. Con::printf("%i: OP_SETCURFIELD stk=0 field=%s", ip - 1, curField);
  936. ip += 2;
  937. break;
  938. }
  939. case OP_SETCURFIELD_ARRAY:
  940. {
  941. Con::printf("%i: OP_SETCURFIELD_ARRAY stk=0", ip - 1);
  942. break;
  943. }
  944. case OP_SETCURFIELD_TYPE:
  945. {
  946. U32 type = code[ip];
  947. Con::printf("%i: OP_SETCURFIELD_TYPE stk=0 type=%i", ip - 1, type);
  948. ++ip;
  949. break;
  950. }
  951. case OP_LOADFIELD_UINT:
  952. {
  953. Con::printf("%i: OP_LOADFIELD_UINT stk=+1", ip - 1);
  954. break;
  955. }
  956. case OP_LOADFIELD_FLT:
  957. {
  958. Con::printf("%i: OP_LOADFIELD_FLT stk=+1", ip - 1);
  959. break;
  960. }
  961. case OP_LOADFIELD_STR:
  962. {
  963. Con::printf("%i: OP_LOADFIELD_STR stk=+1", ip - 1);
  964. break;
  965. }
  966. case OP_SAVEFIELD_UINT:
  967. {
  968. Con::printf("%i: OP_SAVEFIELD_UINT stk=0", ip - 1);
  969. break;
  970. }
  971. case OP_SAVEFIELD_FLT:
  972. {
  973. Con::printf("%i: OP_SAVEFIELD_FLT stk=0", ip - 1);
  974. break;
  975. }
  976. case OP_SAVEFIELD_STR:
  977. {
  978. Con::printf("%i: OP_SAVEFIELD_STR stk=0", ip - 1);
  979. break;
  980. }
  981. case OP_POP_STK:
  982. {
  983. Con::printf("%i: OP_POP_STK stk=-1", ip - 1);
  984. break;
  985. }
  986. case OP_LOADIMMED_UINT:
  987. {
  988. U32 val = code[ip];
  989. Con::printf("%i: OP_LOADIMMED_UINT stk=+1 val=%i", ip - 1, val);
  990. ++ip;
  991. break;
  992. }
  993. case OP_LOADIMMED_FLT:
  994. {
  995. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  996. Con::printf("%i: OP_LOADIMMED_FLT stk=+1 val=%f", ip - 1, val);
  997. ++ip;
  998. break;
  999. }
  1000. case OP_TAG_TO_STR:
  1001. {
  1002. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1003. Con::printf("%i: OP_TAG_TO_STR stk=0 str=%s", ip - 1, str);
  1004. Con::printf(" OP_LOADIMMED_STR stk=+1 (fallthrough)");
  1005. ++ip;
  1006. break;
  1007. }
  1008. case OP_LOADIMMED_STR:
  1009. {
  1010. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1011. Con::printf("%i: OP_LOADIMMED_STR stk=+1 str=%s", ip - 1, str);
  1012. ++ip;
  1013. break;
  1014. }
  1015. case OP_DOCBLOCK_STR:
  1016. {
  1017. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1018. Con::printf("%i: OP_DOCBLOCK_STR stk=0 str=%s", ip - 1, str);
  1019. ++ip;
  1020. break;
  1021. }
  1022. case OP_LOADIMMED_IDENT:
  1023. {
  1024. StringTableEntry str = CodeToSTE(code, ip);
  1025. Con::printf("%i: OP_LOADIMMED_IDENT stk=+1 str=%s", ip - 1, str);
  1026. ip += 2;
  1027. break;
  1028. }
  1029. case OP_CALLFUNC:
  1030. {
  1031. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1032. StringTableEntry fnName = CodeToSTE(code, ip);
  1033. U32 callType = code[ip + 4];
  1034. StringTableEntry callTypeName;
  1035. switch (callType)
  1036. {
  1037. case FuncCallExprNode::FunctionCall: callTypeName = "FunctionCall"; break;
  1038. case FuncCallExprNode::MethodCall: callTypeName = "MethodCall"; break;
  1039. case FuncCallExprNode::ParentCall: callTypeName = "ParentCall"; break;
  1040. case FuncCallExprNode::StaticCall: callTypeName = "StaticCall"; break;
  1041. }
  1042. Con::printf("%i: OP_CALLFUNC stk=+1 name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace, callTypeName);
  1043. ip += 5;
  1044. break;
  1045. }
  1046. case OP_ADVANCE_STR_APPENDCHAR:
  1047. {
  1048. char ch = code[ip];
  1049. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR stk=0 char=%c", ip - 1, ch);
  1050. ++ip;
  1051. break;
  1052. }
  1053. case OP_REWIND_STR:
  1054. {
  1055. Con::printf("%i: OP_REWIND_STR stk=0", ip - 1);
  1056. Con::printf(" OP_TERMINATE_REWIND_STR stk=-1 (fallthrough)");
  1057. break;
  1058. }
  1059. case OP_TERMINATE_REWIND_STR:
  1060. {
  1061. Con::printf("%i: OP_TERMINATE_REWIND_STR stk=-1", ip - 1);
  1062. break;
  1063. }
  1064. case OP_COMPARE_STR:
  1065. {
  1066. Con::printf("%i: OP_COMPARE_STR stk=-1", ip - 1);
  1067. break;
  1068. }
  1069. case OP_PUSH:
  1070. {
  1071. Con::printf("%i: OP_PUSH stk=-1", ip - 1);
  1072. break;
  1073. }
  1074. case OP_PUSH_FRAME:
  1075. {
  1076. Con::printf("%i: OP_PUSH_FRAME stk=0 count=%i", ip - 1, code[ip]);
  1077. ++ip;
  1078. break;
  1079. }
  1080. case OP_ASSERT:
  1081. {
  1082. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1083. Con::printf("%i: OP_ASSERT stk=-1 message=%s", ip - 1, message);
  1084. ++ip;
  1085. break;
  1086. }
  1087. case OP_BREAK:
  1088. {
  1089. Con::printf("%i: OP_BREAK stk=0", ip - 1);
  1090. break;
  1091. }
  1092. case OP_ITER_BEGIN:
  1093. {
  1094. bool isGlobal = code[ip];
  1095. if (isGlobal)
  1096. {
  1097. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1098. U32 failIp = code[ip + 3];
  1099. Con::printf("%i: OP_ITER_BEGIN stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1100. ip += 4;
  1101. }
  1102. else
  1103. {
  1104. S32 reg = code[ip + 1];
  1105. U32 failIp = code[ip + 2];
  1106. Con::printf("%i: OP_ITER_BEGIN stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1107. ip += 3;
  1108. }
  1109. break;
  1110. }
  1111. case OP_ITER_BEGIN_STR:
  1112. {
  1113. bool isGlobal = code[ip];
  1114. if (isGlobal)
  1115. {
  1116. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1117. U32 failIp = code[ip + 3];
  1118. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1119. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1120. ip += 4;
  1121. }
  1122. else
  1123. {
  1124. S32 reg = code[ip + 1];
  1125. U32 failIp = code[ip + 2];
  1126. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1127. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1128. ip += 3;
  1129. }
  1130. break;
  1131. }
  1132. case OP_ITER:
  1133. {
  1134. U32 breakIp = code[ip];
  1135. Con::printf("%i: OP_ITER stk=0 breakIp=%i", ip - 1, breakIp);
  1136. ++ip;
  1137. break;
  1138. }
  1139. case OP_ITER_END:
  1140. {
  1141. Con::printf("%i: OP_ITER_END stk=-1", ip - 1);
  1142. break;
  1143. }
  1144. default:
  1145. Con::printf("%i: !!INVALID!!", ip - 1);
  1146. break;
  1147. }
  1148. }
  1149. smInFunction = false;
  1150. }