codeBlock.cpp 31 KB

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