codeBlock.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. 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 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 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. Con::printf("%i: OP_ADD_OBJECT placeAtRoot=%i", ip - 1, placeAtRoot);
  624. break;
  625. }
  626. case OP_END_OBJECT:
  627. {
  628. bool placeAtRoot = code[ip++];
  629. Con::printf("%i: OP_END_OBJECT placeAtRoot=%i", ip - 1, placeAtRoot);
  630. break;
  631. }
  632. case OP_FINISH_OBJECT:
  633. {
  634. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  635. break;
  636. }
  637. case OP_JMPIFFNOT:
  638. {
  639. Con::printf("%i: OP_JMPIFFNOT ip=%i", ip - 1, code[ip]);
  640. ++ip;
  641. break;
  642. }
  643. case OP_JMPIFNOT:
  644. {
  645. Con::printf("%i: OP_JMPIFNOT ip=%i", ip - 1, code[ip]);
  646. ++ip;
  647. break;
  648. }
  649. case OP_JMPIFF:
  650. {
  651. Con::printf("%i: OP_JMPIFF ip=%i", ip - 1, code[ip]);
  652. ++ip;
  653. break;
  654. }
  655. case OP_JMPIF:
  656. {
  657. Con::printf("%i: OP_JMPIF ip=%i", ip - 1, code[ip]);
  658. ++ip;
  659. break;
  660. }
  661. case OP_JMPIFNOT_NP:
  662. {
  663. Con::printf("%i: OP_JMPIFNOT_NP ip=%i", ip - 1, code[ip]);
  664. ++ip;
  665. break;
  666. }
  667. case OP_JMPIF_NP:
  668. {
  669. Con::printf("%i: OP_JMPIF_NP ip=%i", ip - 1, code[ip]);
  670. ++ip;
  671. break;
  672. }
  673. case OP_JMP:
  674. {
  675. Con::printf("%i: OP_JMP ip=%i", ip - 1, code[ip]);
  676. ++ip;
  677. break;
  678. }
  679. case OP_RETURN:
  680. {
  681. Con::printf("%i: OP_RETURN", ip - 1);
  682. if (upToReturn)
  683. return;
  684. break;
  685. }
  686. case OP_RETURN_VOID:
  687. {
  688. Con::printf("%i: OP_RETURNVOID", ip - 1);
  689. if (upToReturn)
  690. return;
  691. break;
  692. }
  693. case OP_RETURN_UINT:
  694. {
  695. Con::printf("%i: OP_RETURNUINT", ip - 1);
  696. if (upToReturn)
  697. return;
  698. break;
  699. }
  700. case OP_RETURN_FLT:
  701. {
  702. Con::printf("%i: OP_RETURNFLT", ip - 1);
  703. if (upToReturn)
  704. return;
  705. break;
  706. }
  707. case OP_CMPEQ:
  708. {
  709. Con::printf("%i: OP_CMPEQ", ip - 1);
  710. break;
  711. }
  712. case OP_CMPGR:
  713. {
  714. Con::printf("%i: OP_CMPGR", ip - 1);
  715. break;
  716. }
  717. case OP_CMPGE:
  718. {
  719. Con::printf("%i: OP_CMPGE", ip - 1);
  720. break;
  721. }
  722. case OP_CMPLT:
  723. {
  724. Con::printf("%i: OP_CMPLT", ip - 1);
  725. break;
  726. }
  727. case OP_CMPLE:
  728. {
  729. Con::printf("%i: OP_CMPLE", ip - 1);
  730. break;
  731. }
  732. case OP_CMPNE:
  733. {
  734. Con::printf("%i: OP_CMPNE", ip - 1);
  735. break;
  736. }
  737. case OP_XOR:
  738. {
  739. Con::printf("%i: OP_XOR", ip - 1);
  740. break;
  741. }
  742. case OP_MOD:
  743. {
  744. Con::printf("%i: OP_MOD", ip - 1);
  745. break;
  746. }
  747. case OP_BITAND:
  748. {
  749. Con::printf("%i: OP_BITAND", ip - 1);
  750. break;
  751. }
  752. case OP_BITOR:
  753. {
  754. Con::printf("%i: OP_BITOR", ip - 1);
  755. break;
  756. }
  757. case OP_NOT:
  758. {
  759. Con::printf("%i: OP_NOT", ip - 1);
  760. break;
  761. }
  762. case OP_NOTF:
  763. {
  764. Con::printf("%i: OP_NOTF", ip - 1);
  765. break;
  766. }
  767. case OP_ONESCOMPLEMENT:
  768. {
  769. Con::printf("%i: OP_ONESCOMPLEMENT", ip - 1);
  770. break;
  771. }
  772. case OP_SHR:
  773. {
  774. Con::printf("%i: OP_SHR", ip - 1);
  775. break;
  776. }
  777. case OP_SHL:
  778. {
  779. Con::printf("%i: OP_SHL", ip - 1);
  780. break;
  781. }
  782. case OP_AND:
  783. {
  784. Con::printf("%i: OP_AND", ip - 1);
  785. break;
  786. }
  787. case OP_OR:
  788. {
  789. Con::printf("%i: OP_OR", ip - 1);
  790. break;
  791. }
  792. case OP_ADD:
  793. {
  794. Con::printf("%i: OP_ADD", ip - 1);
  795. break;
  796. }
  797. case OP_SUB:
  798. {
  799. Con::printf("%i: OP_SUB", ip - 1);
  800. break;
  801. }
  802. case OP_MUL:
  803. {
  804. Con::printf("%i: OP_MUL", ip - 1);
  805. break;
  806. }
  807. case OP_DIV:
  808. {
  809. Con::printf("%i: OP_DIV", ip - 1);
  810. break;
  811. }
  812. case OP_NEG:
  813. {
  814. Con::printf("%i: OP_NEG", ip - 1);
  815. break;
  816. }
  817. case OP_INC:
  818. {
  819. Con::printf("%i: OP_INC reg=%i", ip - 1, code[ip]);
  820. ++ip;
  821. break;
  822. }
  823. case OP_SETCURVAR:
  824. {
  825. StringTableEntry var = CodeToSTE(code, ip);
  826. Con::printf("%i: OP_SETCURVAR var=%s", ip - 1, var);
  827. ip += 2;
  828. break;
  829. }
  830. case OP_SETCURVAR_CREATE:
  831. {
  832. StringTableEntry var = CodeToSTE(code, ip);
  833. Con::printf("%i: OP_SETCURVAR_CREATE var=%s", ip - 1, var);
  834. ip += 2;
  835. break;
  836. }
  837. case OP_SETCURVAR_ARRAY:
  838. {
  839. Con::printf("%i: OP_SETCURVAR_ARRAY", ip - 1);
  840. break;
  841. }
  842. case OP_SETCURVAR_ARRAY_CREATE:
  843. {
  844. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE", ip - 1);
  845. break;
  846. }
  847. case OP_LOADVAR_UINT:
  848. {
  849. Con::printf("%i: OP_LOADVAR_UINT", ip - 1);
  850. break;
  851. }
  852. case OP_LOADVAR_FLT:
  853. {
  854. Con::printf("%i: OP_LOADVAR_FLT", ip - 1);
  855. break;
  856. }
  857. case OP_LOADVAR_STR:
  858. {
  859. Con::printf("%i: OP_LOADVAR_STR", ip - 1);
  860. break;
  861. }
  862. case OP_SAVEVAR_UINT:
  863. {
  864. Con::printf("%i: OP_SAVEVAR_UINT", ip - 1);
  865. break;
  866. }
  867. case OP_SAVEVAR_FLT:
  868. {
  869. Con::printf("%i: OP_SAVEVAR_FLT", ip - 1);
  870. break;
  871. }
  872. case OP_SAVEVAR_STR:
  873. {
  874. Con::printf("%i: OP_SAVEVAR_STR", ip - 1);
  875. break;
  876. }
  877. case OP_LOAD_LOCAL_VAR_UINT:
  878. {
  879. Con::printf("%i: OP_LOAD_LOCAL_VAR_UINT reg=%i", ip - 1, code[ip]);
  880. ++ip;
  881. break;
  882. }
  883. case OP_LOAD_LOCAL_VAR_FLT:
  884. {
  885. Con::printf("%i: OP_LOAD_LOCAL_VAR_FLT reg=%i", ip - 1, code[ip]);
  886. ++ip;
  887. break;
  888. }
  889. case OP_LOAD_LOCAL_VAR_STR:
  890. {
  891. Con::printf("%i: OP_LOAD_LOCAL_VAR_STR reg=%i", ip - 1, code[ip]);
  892. ++ip;
  893. break;
  894. }
  895. case OP_SAVE_LOCAL_VAR_UINT:
  896. {
  897. Con::printf("%i: OP_SAVE_LOCAL_VAR_UINT reg=%i", ip - 1, code[ip]);
  898. ++ip;
  899. break;
  900. }
  901. case OP_SAVE_LOCAL_VAR_FLT:
  902. {
  903. Con::printf("%i: OP_SAVE_LOCAL_VAR_FLT reg=%i", ip - 1, code[ip]);
  904. ++ip;
  905. break;
  906. }
  907. case OP_SAVE_LOCAL_VAR_STR:
  908. {
  909. Con::printf("%i: OP_SAVE_LOCAL_VAR_STR reg=%i", ip - 1, code[ip]);
  910. ++ip;
  911. break;
  912. }
  913. case OP_SETCUROBJECT:
  914. {
  915. Con::printf("%i: OP_SETCUROBJECT", ip - 1);
  916. break;
  917. }
  918. case OP_SETCUROBJECT_NEW:
  919. {
  920. Con::printf("%i: OP_SETCUROBJECT_NEW", ip - 1);
  921. break;
  922. }
  923. case OP_SETCUROBJECT_INTERNAL:
  924. {
  925. Con::printf("%i: OP_SETCUROBJECT_INTERNAL", ip - 1);
  926. ++ip;
  927. break;
  928. }
  929. case OP_SETCURFIELD:
  930. {
  931. StringTableEntry curField = CodeToSTE(code, ip);
  932. Con::printf("%i: OP_SETCURFIELD field=%s", ip - 1, curField);
  933. ip += 2;
  934. break;
  935. }
  936. case OP_SETCURFIELD_ARRAY:
  937. {
  938. Con::printf("%i: OP_SETCURFIELD_ARRAY", ip - 1);
  939. break;
  940. }
  941. case OP_SETCURFIELD_TYPE:
  942. {
  943. U32 type = code[ip];
  944. Con::printf("%i: OP_SETCURFIELD_TYPE type=%i", ip - 1, type);
  945. ++ip;
  946. break;
  947. }
  948. case OP_LOADFIELD_UINT:
  949. {
  950. Con::printf("%i: OP_LOADFIELD_UINT", ip - 1);
  951. break;
  952. }
  953. case OP_LOADFIELD_FLT:
  954. {
  955. Con::printf("%i: OP_LOADFIELD_FLT", ip - 1);
  956. break;
  957. }
  958. case OP_LOADFIELD_STR:
  959. {
  960. Con::printf("%i: OP_LOADFIELD_STR", ip - 1);
  961. break;
  962. }
  963. case OP_SAVEFIELD_UINT:
  964. {
  965. Con::printf("%i: OP_SAVEFIELD_UINT", ip - 1);
  966. break;
  967. }
  968. case OP_SAVEFIELD_FLT:
  969. {
  970. Con::printf("%i: OP_SAVEFIELD_FLT", ip - 1);
  971. break;
  972. }
  973. case OP_SAVEFIELD_STR:
  974. {
  975. Con::printf("%i: OP_SAVEFIELD_STR", ip - 1);
  976. break;
  977. }
  978. case OP_STR_TO_UINT:
  979. {
  980. Con::printf("%i: OP_STR_TO_UINT", ip - 1);
  981. break;
  982. }
  983. case OP_STR_TO_FLT:
  984. {
  985. Con::printf("%i: OP_STR_TO_FLT", ip - 1);
  986. break;
  987. }
  988. case OP_STR_TO_NONE:
  989. {
  990. Con::printf("%i: OP_STR_TO_NONE", ip - 1);
  991. break;
  992. }
  993. case OP_FLT_TO_UINT:
  994. {
  995. Con::printf("%i: OP_FLT_TO_UINT", ip - 1);
  996. break;
  997. }
  998. case OP_FLT_TO_STR:
  999. {
  1000. Con::printf("%i: OP_FLT_TO_STR", ip - 1);
  1001. break;
  1002. }
  1003. case OP_UINT_TO_FLT:
  1004. {
  1005. Con::printf("%i: OP_UINT_TO_FLT", ip - 1);
  1006. break;
  1007. }
  1008. case OP_UINT_TO_STR:
  1009. {
  1010. Con::printf("%i: OP_UINT_TO_STR", ip - 1);
  1011. break;
  1012. }
  1013. case OP_NUM_TO_NONE:
  1014. {
  1015. Con::printf("%i: OP_NUM_TO_NONE", ip - 1);
  1016. break;
  1017. }
  1018. case OP_LOADIMMED_UINT:
  1019. {
  1020. U32 val = code[ip];
  1021. Con::printf("%i: OP_LOADIMMED_UINT val=%i", ip - 1, val);
  1022. ++ip;
  1023. break;
  1024. }
  1025. case OP_LOADIMMED_FLT:
  1026. {
  1027. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  1028. Con::printf("%i: OP_LOADIMMED_FLT val=%f", ip - 1, val);
  1029. ++ip;
  1030. break;
  1031. }
  1032. case OP_TAG_TO_STR:
  1033. {
  1034. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1035. Con::printf("%i: OP_TAG_TO_STR str=%s", ip - 1, str);
  1036. ++ip;
  1037. break;
  1038. }
  1039. case OP_LOADIMMED_STR:
  1040. {
  1041. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1042. Con::printf("%i: OP_LOADIMMED_STR str=%s", ip - 1, str);
  1043. ++ip;
  1044. break;
  1045. }
  1046. case OP_DOCBLOCK_STR:
  1047. {
  1048. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1049. Con::printf("%i: OP_DOCBLOCK_STR str=%s", ip - 1, str);
  1050. ++ip;
  1051. break;
  1052. }
  1053. case OP_LOADIMMED_IDENT:
  1054. {
  1055. StringTableEntry str = CodeToSTE(code, ip);
  1056. Con::printf("%i: OP_LOADIMMED_IDENT str=%s", ip - 1, str);
  1057. ip += 2;
  1058. break;
  1059. }
  1060. case OP_CALLFUNC:
  1061. {
  1062. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1063. StringTableEntry fnName = CodeToSTE(code, ip);
  1064. U32 callType = code[ip + 4];
  1065. StringTableEntry callTypeName;
  1066. switch (callType)
  1067. {
  1068. case FuncCallExprNode::FunctionCall: callTypeName = "FunctionCall"; break;
  1069. case FuncCallExprNode::MethodCall: callTypeName = "MethodCall"; break;
  1070. case FuncCallExprNode::ParentCall: callTypeName = "ParentCall"; break;
  1071. case FuncCallExprNode::StaticCall: callTypeName = "StaticCall"; break;
  1072. }
  1073. Con::printf("%i: OP_CALLFUNC name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace, callTypeName);
  1074. ip += 5;
  1075. break;
  1076. }
  1077. case OP_ADVANCE_STR:
  1078. {
  1079. Con::printf("%i: OP_ADVANCE_STR", ip - 1);
  1080. break;
  1081. }
  1082. case OP_ADVANCE_STR_APPENDCHAR:
  1083. {
  1084. char ch = code[ip];
  1085. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR char=%c", ip - 1, ch);
  1086. ++ip;
  1087. break;
  1088. }
  1089. case OP_ADVANCE_STR_COMMA:
  1090. {
  1091. Con::printf("%i: OP_ADVANCE_STR_COMMA", ip - 1);
  1092. break;
  1093. }
  1094. case OP_ADVANCE_STR_NUL:
  1095. {
  1096. Con::printf("%i: OP_ADVANCE_STR_NUL", ip - 1);
  1097. break;
  1098. }
  1099. case OP_REWIND_STR:
  1100. {
  1101. Con::printf("%i: OP_REWIND_STR", ip - 1);
  1102. break;
  1103. }
  1104. case OP_TERMINATE_REWIND_STR:
  1105. {
  1106. Con::printf("%i: OP_TERMINATE_REWIND_STR", ip - 1);
  1107. break;
  1108. }
  1109. case OP_COMPARE_STR:
  1110. {
  1111. Con::printf("%i: OP_COMPARE_STR", ip - 1);
  1112. break;
  1113. }
  1114. case OP_PUSH:
  1115. {
  1116. Con::printf("%i: OP_PUSH", ip - 1);
  1117. break;
  1118. }
  1119. case OP_PUSH_UINT:
  1120. {
  1121. Con::printf("%i: OP_PUSH_UINT", ip - 1);
  1122. break;
  1123. }
  1124. case OP_PUSH_FLT:
  1125. {
  1126. Con::printf("%i: OP_PUSH_FLT", ip - 1);
  1127. break;
  1128. }
  1129. case OP_PUSH_FRAME:
  1130. {
  1131. Con::printf("%i: OP_PUSH_FRAME count=%i", ip - 1, code[ip]);
  1132. ++ip;
  1133. break;
  1134. }
  1135. case OP_ASSERT:
  1136. {
  1137. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1138. Con::printf("%i: OP_ASSERT message=%s", ip - 1, message);
  1139. ++ip;
  1140. break;
  1141. }
  1142. case OP_BREAK:
  1143. {
  1144. Con::printf("%i: OP_BREAK", ip - 1);
  1145. break;
  1146. }
  1147. case OP_ITER_BEGIN:
  1148. {
  1149. bool isGlobal = code[ip];
  1150. if (isGlobal)
  1151. {
  1152. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1153. U32 failIp = code[ip + 3];
  1154. Con::printf("%i: OP_ITER_BEGIN varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1155. ip += 4;
  1156. }
  1157. else
  1158. {
  1159. S32 reg = code[ip + 1];
  1160. U32 failIp = code[ip + 2];
  1161. Con::printf("%i: OP_ITER_BEGIN varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1162. ip += 3;
  1163. }
  1164. break;
  1165. }
  1166. case OP_ITER_BEGIN_STR:
  1167. {
  1168. bool isGlobal = code[ip];
  1169. if (isGlobal)
  1170. {
  1171. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1172. U32 failIp = code[ip + 3];
  1173. Con::printf("%i: OP_ITER_BEGIN_STR varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1174. ip += 4;
  1175. }
  1176. else
  1177. {
  1178. S32 reg = code[ip + 1];
  1179. U32 failIp = code[ip + 2];
  1180. Con::printf("%i: OP_ITER_BEGIN_STR varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1181. ip += 3;
  1182. }
  1183. break;
  1184. }
  1185. case OP_ITER:
  1186. {
  1187. U32 breakIp = code[ip];
  1188. Con::printf("%i: OP_ITER breakIp=%i", ip - 1, breakIp);
  1189. ++ip;
  1190. break;
  1191. }
  1192. case OP_ITER_END:
  1193. {
  1194. Con::printf("%i: OP_ITER_END", ip - 1);
  1195. break;
  1196. }
  1197. default:
  1198. Con::printf("%i: !!INVALID!!", ip - 1);
  1199. break;
  1200. }
  1201. }
  1202. smInFunction = false;
  1203. }