codeBlock.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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 "compiler.h"
  24. #include "codeBlock.h"
  25. #include "ast.h"
  26. #include "console/telnetDebugger.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. TorqueScriptParser *CodeBlock::smCurrentParser = NULL;
  35. extern FuncVars gEvalFuncVars;
  36. extern FuncVars gGlobalScopeFuncVars;
  37. extern FuncVars* gFuncVars;
  38. //-------------------------------------------------------------------------
  39. CodeBlock::CodeBlock()
  40. {
  41. globalStrings = NULL;
  42. functionStrings = NULL;
  43. functionStringsMaxLen = 0;
  44. globalStringsMaxLen = 0;
  45. globalFloats = NULL;
  46. functionFloats = NULL;
  47. lineBreakPairs = NULL;
  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(Con::getCurrentScriptModule() != 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. }
  71. //-------------------------------------------------------------------------
  72. CodeBlock *CodeBlock::find(StringTableEntry name)
  73. {
  74. for (CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  75. if (walk->name == name)
  76. return walk;
  77. return NULL;
  78. }
  79. //-------------------------------------------------------------------------
  80. void CodeBlock::addToCodeList()
  81. {
  82. // remove any code blocks with my name
  83. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  84. {
  85. if ((*walk)->name == name)
  86. {
  87. *walk = (*walk)->nextFile;
  88. break;
  89. }
  90. }
  91. nextFile = smCodeBlockList;
  92. smCodeBlockList = this;
  93. }
  94. void CodeBlock::clearAllBreaks()
  95. {
  96. if (!lineBreakPairs)
  97. return;
  98. for (U32 i = 0; i < lineBreakPairCount; i++)
  99. {
  100. U32 *p = lineBreakPairs + i * 2;
  101. code[p[1]] = p[0] & 0xFF;
  102. }
  103. }
  104. void CodeBlock::clearBreakpoint(U32 lineNumber)
  105. {
  106. if (!lineBreakPairs)
  107. return;
  108. for (U32 i = 0; i < lineBreakPairCount; i++)
  109. {
  110. U32 *p = lineBreakPairs + i * 2;
  111. if ((p[0] >> 8) == lineNumber)
  112. {
  113. code[p[1]] = p[0] & 0xFF;
  114. return;
  115. }
  116. }
  117. }
  118. void CodeBlock::setAllBreaks()
  119. {
  120. if (!lineBreakPairs)
  121. return;
  122. for (U32 i = 0; i < lineBreakPairCount; i++)
  123. {
  124. U32 *p = lineBreakPairs + i * 2;
  125. code[p[1]] = OP_BREAK;
  126. }
  127. }
  128. bool CodeBlock::setBreakpoint(U32 lineNumber)
  129. {
  130. if (!lineBreakPairs)
  131. return false;
  132. for (U32 i = 0; i < lineBreakPairCount; i++)
  133. {
  134. U32 *p = lineBreakPairs + i * 2;
  135. if ((p[0] >> 8) == lineNumber)
  136. {
  137. code[p[1]] = OP_BREAK;
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. U32 CodeBlock::findFirstBreakLine(U32 lineNumber)
  144. {
  145. if (!lineBreakPairs)
  146. return 0;
  147. for (U32 i = 0; i < lineBreakPairCount; i++)
  148. {
  149. U32 *p = lineBreakPairs + i * 2;
  150. U32 line = (p[0] >> 8);
  151. if (lineNumber <= line)
  152. return line;
  153. }
  154. return 0;
  155. }
  156. struct LinePair
  157. {
  158. U32 instLine;
  159. U32 ip;
  160. };
  161. void CodeBlock::findBreakLine(U32 ip, U32 &line, U32 &instruction)
  162. {
  163. U32 min = 0;
  164. U32 max = lineBreakPairCount - 1;
  165. LinePair *p = (LinePair *)lineBreakPairs;
  166. U32 found;
  167. if (!lineBreakPairCount || p[min].ip > ip || p[max].ip < ip)
  168. {
  169. line = 0;
  170. instruction = OP_INVALID;
  171. return;
  172. }
  173. else if (p[min].ip == ip)
  174. found = min;
  175. else if (p[max].ip == ip)
  176. found = max;
  177. else
  178. {
  179. for (;;)
  180. {
  181. if (min == max - 1)
  182. {
  183. found = min;
  184. break;
  185. }
  186. U32 mid = (min + max) >> 1;
  187. if (p[mid].ip == ip)
  188. {
  189. found = mid;
  190. break;
  191. }
  192. else if (p[mid].ip > ip)
  193. max = mid;
  194. else
  195. min = mid;
  196. }
  197. }
  198. instruction = p[found].instLine & 0xFF;
  199. line = p[found].instLine >> 8;
  200. }
  201. const char *CodeBlock::getFileLine(U32 ip)
  202. {
  203. static char nameBuffer[256];
  204. U32 line, inst;
  205. findBreakLine(ip, line, inst);
  206. dSprintf(nameBuffer, sizeof(nameBuffer), "%s (%d)", name ? name : "<input>", line);
  207. return nameBuffer;
  208. }
  209. void CodeBlock::removeFromCodeList()
  210. {
  211. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  212. {
  213. if (*walk == this)
  214. {
  215. *walk = nextFile;
  216. // clear out all breakpoints
  217. clearAllBreaks();
  218. break;
  219. }
  220. }
  221. // Let the telnet debugger know that this code
  222. // block has been unloaded and that it needs to
  223. // remove references to it.
  224. if (TelDebugger)
  225. TelDebugger->clearCodeBlockPointers(this);
  226. }
  227. void CodeBlock::calcBreakList()
  228. {
  229. U32 size = 0;
  230. S32 line = -1;
  231. U32 seqCount = 0;
  232. U32 i;
  233. for (i = 0; i < lineBreakPairCount; i++)
  234. {
  235. U32 lineNumber = lineBreakPairs[i * 2];
  236. if (lineNumber == U32(line + 1))
  237. seqCount++;
  238. else
  239. {
  240. if (seqCount)
  241. size++;
  242. size++;
  243. seqCount = 1;
  244. }
  245. line = lineNumber;
  246. }
  247. if (seqCount)
  248. size++;
  249. breakList.setSize(size+3); //lineBreakPairs plus pad
  250. line = -1;
  251. seqCount = 0;
  252. size = 0;
  253. for (i = 0; i < lineBreakPairCount; i++)
  254. {
  255. U32 lineNumber = lineBreakPairs[i * 2];
  256. if (lineNumber == U32(line + 1))
  257. seqCount++;
  258. else
  259. {
  260. if (seqCount)
  261. breakList[size++] = seqCount;
  262. breakList[size++] = lineNumber - getMax(0, line) - 1;
  263. seqCount = 1;
  264. }
  265. line = lineNumber;
  266. }
  267. if (seqCount)
  268. breakList[size++] = seqCount;
  269. for (i = 0; i < lineBreakPairCount; i++)
  270. {
  271. U32 *p = lineBreakPairs + i * 2;
  272. p[0] = (p[0] << 8) | code[p[1]];
  273. }
  274. // Let the telnet debugger know that this code
  275. // block has been loaded and that it can add break
  276. // points it has for it.
  277. if (TelDebugger)
  278. TelDebugger->addAllBreakpoints(this);
  279. }
  280. bool CodeBlock::read(StringTableEntry fileName, Stream &st)
  281. {
  282. const StringTableEntry exePath = Platform::getMainDotCsDir();
  283. const StringTableEntry cwd = Platform::getCurrentDirectory();
  284. name = fileName;
  285. if (fileName)
  286. {
  287. fullPath = NULL;
  288. if (Platform::isFullPath(fileName))
  289. fullPath = fileName;
  290. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  291. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  292. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  293. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  294. if (fullPath == NULL)
  295. {
  296. char buf[1024];
  297. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  298. }
  299. modPath = Con::getModNameFromPath(fileName);
  300. }
  301. //
  302. addToCodeList();
  303. U32 globalSize, size, i;
  304. st.read(&size);
  305. if (size)
  306. {
  307. globalStrings = new char[size];
  308. globalStringsMaxLen = size;
  309. st.read(size, globalStrings);
  310. }
  311. globalSize = size;
  312. st.read(&size);
  313. if (size)
  314. {
  315. functionStrings = new char[size];
  316. functionStringsMaxLen = size;
  317. st.read(size, functionStrings);
  318. }
  319. st.read(&size);
  320. if (size)
  321. {
  322. globalFloats = new F64[size];
  323. for (i = 0; i < size; i++)
  324. st.read(&globalFloats[i]);
  325. }
  326. st.read(&size);
  327. if (size)
  328. {
  329. functionFloats = new F64[size];
  330. for (i = 0; i < size; i++)
  331. st.read(&functionFloats[i]);
  332. }
  333. // Variable register mapping table
  334. st.read(&size);
  335. if (size)
  336. {
  337. for (i = 0; i < size; i++)
  338. {
  339. StringTableEntry fnName = st.readSTString();
  340. U32 count;
  341. st.read(&count);
  342. for (U32 j = 0; j < count; j++)
  343. {
  344. StringTableEntry varName = st.readSTString();
  345. variableRegisterTable.localVarToRegister[fnName].varList.push_back(varName);
  346. }
  347. }
  348. }
  349. U32 codeLength;
  350. st.read(&codeLength);
  351. st.read(&lineBreakPairCount);
  352. U32 totSize = codeLength + lineBreakPairCount * 2;
  353. code = new U32[totSize+1];
  354. // 0xFF is used as a flag to help compress the bytecode.
  355. // If detected, the bytecode is only a U8.
  356. for (i = 0; i < codeLength; i++)
  357. {
  358. U8 b;
  359. st.read(&b);
  360. if (b == 0xFF)
  361. st.read(&code[i]);
  362. else
  363. code[i] = b;
  364. }
  365. for (i = codeLength; i < totSize; i++)
  366. st.read(&code[i]);
  367. lineBreakPairs = code + codeLength;
  368. // StringTable-ize our identifiers.
  369. U32 identCount;
  370. st.read(&identCount);
  371. while (identCount--)
  372. {
  373. U32 offset;
  374. st.read(&offset);
  375. StringTableEntry ste;
  376. if (offset < globalSize)
  377. ste = StringTable->insert(globalStrings + offset);
  378. else
  379. ste = StringTable->EmptyString();
  380. U32 count;
  381. st.read(&count);
  382. while (count--)
  383. {
  384. U32 ip;
  385. st.read(&ip);
  386. #if defined(TORQUE_CPU_X64) || defined(TORQUE_CPU_ARM64)
  387. *(U64*)(code + ip) = (U64)ste;
  388. #else
  389. code[ip] = *((U32 *)&ste);
  390. #endif
  391. }
  392. }
  393. if (lineBreakPairCount)
  394. calcBreakList();
  395. return true;
  396. }
  397. bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, const char *inScript, bool overrideNoDso)
  398. {
  399. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  400. // This will return true, but return value is ignored
  401. char *script;
  402. chompUTF8BOM(inScript, &script);
  403. gSyntaxError = false;
  404. gIsEvalCompile = false;
  405. gFuncVars = NULL;
  406. consoleAllocReset();
  407. STEtoCode = compileSTEtoCode;
  408. Script::gStatementList = NULL;
  409. // Set up the parser.
  410. smCurrentParser = new TorqueScriptParser();
  411. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  412. // Now do some parsing.
  413. smCurrentParser->setScanBuffer(script, fileName);
  414. smCurrentParser->restart(NULL);
  415. smCurrentParser->parse();
  416. if (gSyntaxError)
  417. {
  418. consoleAllocReset();
  419. return false;
  420. }
  421. #ifdef TORQUE_NO_DSO_GENERATION
  422. if (!overrideNoDso)
  423. return false;
  424. #endif // !TORQUE_NO_DSO_GENERATION
  425. FileStream st;
  426. if (!st.open(codeFileName, Torque::FS::File::Write))
  427. return false;
  428. st.write(U32(Con::DSOVersion));
  429. // Reset all our value tables...
  430. resetTables();
  431. smInFunction = false;
  432. CodeStream codeStream;
  433. U32 lastIp;
  434. if (Script::gStatementList)
  435. {
  436. lastIp = compileBlock(Script::gStatementList, codeStream, 0) + 1;
  437. }
  438. else
  439. {
  440. codeSize = 1;
  441. lastIp = 0;
  442. }
  443. codeStream.emit(OP_RETURN_VOID);
  444. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  445. lineBreakPairCount = codeStream.getNumLineBreaks();
  446. // Write string table data...
  447. getGlobalStringTable().write(st);
  448. getFunctionStringTable().write(st);
  449. // Write float table data...
  450. getGlobalFloatTable().write(st);
  451. getFunctionFloatTable().write(st);
  452. // write variable mapping table
  453. getFunctionVariableMappingTable().write(st);
  454. if (lastIp != codeSize)
  455. Con::errorf(ConsoleLogEntry::General, "CodeBlock::compile - precompile size mismatch, a precompile/compile function pair is probably mismatched.");
  456. U32 totSize = codeSize + codeStream.getNumLineBreaks() * 2;
  457. st.write(codeSize);
  458. st.write(lineBreakPairCount);
  459. // Write out our bytecode, doing a bit of compression for low numbers.
  460. U32 i;
  461. for (i = 0; i < codeSize; i++)
  462. {
  463. if (code[i] < 0xFF)
  464. st.write(U8(code[i]));
  465. else
  466. {
  467. st.write(U8(0xFF));
  468. st.write(code[i]);
  469. }
  470. }
  471. // Write the break info...
  472. for (i = codeSize; i < totSize; i++)
  473. st.write(code[i]);
  474. getIdentTable().write(st);
  475. consoleAllocReset();
  476. st.close();
  477. return true;
  478. }
  479. Con::EvalResult CodeBlock::compileExec(StringTableEntry fileName, const char *inString, bool noCalls, S32 setFrame)
  480. {
  481. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  482. // Check for a UTF8 script file
  483. char *string;
  484. chompUTF8BOM(inString, &string);
  485. STEtoCode = evalSTEtoCode;
  486. consoleAllocReset();
  487. name = fileName;
  488. if (fileName)
  489. {
  490. const StringTableEntry exePath = Platform::getMainDotCsDir();
  491. const StringTableEntry cwd = Platform::getCurrentDirectory();
  492. fullPath = NULL;
  493. if (Platform::isFullPath(fileName))
  494. fullPath = fileName;
  495. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  496. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  497. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  498. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  499. if (fullPath == NULL)
  500. {
  501. char buf[1024];
  502. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  503. }
  504. modPath = Con::getModNameFromPath(fileName);
  505. }
  506. if (name)
  507. addToCodeList();
  508. Script::gStatementList = NULL;
  509. // we are an eval compile if we don't have a file name associated (no exec)
  510. gIsEvalCompile = fileName == NULL;
  511. gFuncVars = gIsEvalCompile ? &gEvalFuncVars : &gGlobalScopeFuncVars;
  512. // Set up the parser.
  513. smCurrentParser = new TorqueScriptParser();
  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 (!Script::gStatementList)
  520. {
  521. delete this;
  522. return Con::EvalResult(Con::getVariable("$ScriptError"));
  523. }
  524. resetTables();
  525. smInFunction = false;
  526. CodeStream codeStream;
  527. U32 lastIp = compileBlock(Script::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. S32 localRegisterCount = gIsEvalCompile ? gEvalFuncVars.count() : gGlobalScopeFuncVars.count();
  539. consoleAllocReset();
  540. #ifndef TORQUE_SHIPPING
  541. if (Con::getBoolVariable("$Debug::DumpByteCode"))
  542. {
  543. dumpInstructions();
  544. }
  545. #endif
  546. if (lineBreakPairCount && fileName)
  547. calcBreakList();
  548. if (lastIp + 1 != codeSize)
  549. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
  550. // repurpose argc as local register counter for global state
  551. return (exec(0, fileName, NULL, localRegisterCount, 0, noCalls, NULL, setFrame));
  552. }
  553. //-------------------------------------------------------------------------
  554. String CodeBlock::getFunctionArgs(U32 ip)
  555. {
  556. StringBuilder str;
  557. StringTableEntry fnName = CodeToSTE(code, ip);
  558. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  559. StringTableEntry fnNsName = StringTable->insert(avar("%s::%s", fnNamespace, fnName));
  560. U32 fnArgc = code[ip + 8];
  561. for (U32 i = 0; i < fnArgc; ++i)
  562. {
  563. StringTableEntry var = variableRegisterTable.localVarToRegister[fnNsName].varList[i];
  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 stk=+0 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 stk=+0 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. const char* stk = placeAtRoot ? "+1" : "0";
  623. Con::printf("%i: OP_ADD_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  624. break;
  625. }
  626. case OP_END_OBJECT:
  627. {
  628. bool placeAtRoot = code[ip++];
  629. const char* stk = placeAtRoot ? "-1" : "0";
  630. Con::printf("%i: OP_END_OBJECT stk=%s placeAtRoot=%i", ip - 1, stk, placeAtRoot);
  631. break;
  632. }
  633. case OP_FINISH_OBJECT:
  634. {
  635. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  636. break;
  637. }
  638. case OP_JMPIFFNOT:
  639. {
  640. Con::printf("%i: OP_JMPIFFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  641. ++ip;
  642. break;
  643. }
  644. case OP_JMPIFNOT:
  645. {
  646. Con::printf("%i: OP_JMPIFNOT stk=-1 ip=%i", ip - 1, code[ip]);
  647. ++ip;
  648. break;
  649. }
  650. case OP_JMPIFF:
  651. {
  652. Con::printf("%i: OP_JMPIFF stk=-1 ip=%i", ip - 1, code[ip]);
  653. ++ip;
  654. break;
  655. }
  656. case OP_JMPIF:
  657. {
  658. Con::printf("%i: OP_JMPIF stk=-1 ip=%i", ip - 1, code[ip]);
  659. ++ip;
  660. break;
  661. }
  662. case OP_JMPIFNOT_NP:
  663. {
  664. Con::printf("%i: OP_JMPIFNOT_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  665. ++ip;
  666. break;
  667. }
  668. case OP_JMPIF_NP:
  669. {
  670. Con::printf("%i: OP_JMPIF_NP stk=-1 or 0 ip=%i", ip - 1, code[ip]);
  671. ++ip;
  672. break;
  673. }
  674. case OP_JMP:
  675. {
  676. Con::printf("%i: OP_JMP stk=0 ip=%i", ip - 1, code[ip]);
  677. ++ip;
  678. break;
  679. }
  680. case OP_RETURN_VOID:
  681. {
  682. Con::printf("%i: OP_RETURN_VOID stk=0", ip - 1);
  683. if (upToReturn)
  684. return;
  685. break;
  686. }
  687. case OP_RETURN:
  688. {
  689. Con::printf("%i: OP_RETURN stk=-1", ip - 1);
  690. if (upToReturn)
  691. return;
  692. break;
  693. }
  694. case OP_RETURN_UINT:
  695. {
  696. Con::printf("%i: OP_RETURNUINT stk=-1", ip - 1);
  697. if (upToReturn)
  698. return;
  699. break;
  700. }
  701. case OP_RETURN_FLT:
  702. {
  703. Con::printf("%i: OP_RETURNFLT stk=-1", ip - 1);
  704. if (upToReturn)
  705. return;
  706. break;
  707. }
  708. case OP_CMPEQ:
  709. {
  710. Con::printf("%i: OP_CMPEQ stk=-1", ip - 1);
  711. break;
  712. }
  713. case OP_CMPGR:
  714. {
  715. Con::printf("%i: OP_CMPGR stk=-1", ip - 1);
  716. break;
  717. }
  718. case OP_CMPGE:
  719. {
  720. Con::printf("%i: OP_CMPGE stk=-1", ip - 1);
  721. break;
  722. }
  723. case OP_CMPLT:
  724. {
  725. Con::printf("%i: OP_CMPLT stk=-1", ip - 1);
  726. break;
  727. }
  728. case OP_CMPLE:
  729. {
  730. Con::printf("%i: OP_CMPLE stk=-1", ip - 1);
  731. break;
  732. }
  733. case OP_CMPNE:
  734. {
  735. Con::printf("%i: OP_CMPNE stk=-1", ip - 1);
  736. break;
  737. }
  738. case OP_XOR:
  739. {
  740. Con::printf("%i: OP_XOR stk=-1", ip - 1);
  741. break;
  742. }
  743. case OP_MOD:
  744. {
  745. Con::printf("%i: OP_MOD stk=-1", ip - 1);
  746. break;
  747. }
  748. case OP_BITAND:
  749. {
  750. Con::printf("%i: OP_BITAND stk=-1", ip - 1);
  751. break;
  752. }
  753. case OP_BITOR:
  754. {
  755. Con::printf("%i: OP_BITOR stk=-1", ip - 1);
  756. break;
  757. }
  758. case OP_NOT:
  759. {
  760. Con::printf("%i: OP_NOT stk=0", ip - 1);
  761. break;
  762. }
  763. case OP_NOTF:
  764. {
  765. Con::printf("%i: OP_NOTF stk=0", ip - 1);
  766. break;
  767. }
  768. case OP_ONESCOMPLEMENT:
  769. {
  770. Con::printf("%i: OP_ONESCOMPLEMENT stk=0", ip - 1);
  771. break;
  772. }
  773. case OP_SHR:
  774. {
  775. Con::printf("%i: OP_SHR stk=-1", ip - 1);
  776. break;
  777. }
  778. case OP_SHL:
  779. {
  780. Con::printf("%i: OP_SHL stk=-1", ip - 1);
  781. break;
  782. }
  783. case OP_AND:
  784. {
  785. Con::printf("%i: OP_AND stk=-1", ip - 1);
  786. break;
  787. }
  788. case OP_OR:
  789. {
  790. Con::printf("%i: OP_OR stk=-1", ip - 1);
  791. break;
  792. }
  793. case OP_ADD:
  794. {
  795. Con::printf("%i: OP_ADD stk=-1", ip - 1);
  796. break;
  797. }
  798. case OP_SUB:
  799. {
  800. Con::printf("%i: OP_SUB stk=-1", ip - 1);
  801. break;
  802. }
  803. case OP_MUL:
  804. {
  805. Con::printf("%i: OP_MUL stk=-1", ip - 1);
  806. break;
  807. }
  808. case OP_DIV:
  809. {
  810. Con::printf("%i: OP_DIV stk=-1", ip - 1);
  811. break;
  812. }
  813. case OP_NEG:
  814. {
  815. Con::printf("%i: OP_NEG stk=0", ip - 1);
  816. break;
  817. }
  818. case OP_INC:
  819. {
  820. Con::printf("%i: OP_INC stk=0 reg=%i", ip - 1, code[ip]);
  821. ++ip;
  822. break;
  823. }
  824. case OP_SETCURVAR:
  825. {
  826. StringTableEntry var = CodeToSTE(code, ip);
  827. Con::printf("%i: OP_SETCURVAR stk=0 var=%s", ip - 1, var);
  828. ip += 2;
  829. break;
  830. }
  831. case OP_SETCURVAR_CREATE:
  832. {
  833. StringTableEntry var = CodeToSTE(code, ip);
  834. Con::printf("%i: OP_SETCURVAR_CREATE stk=0 var=%s", ip - 1, var);
  835. ip += 2;
  836. break;
  837. }
  838. case OP_SETCURVAR_ARRAY:
  839. {
  840. Con::printf("%i: OP_SETCURVAR_ARRAY stk=0", ip - 1);
  841. break;
  842. }
  843. case OP_SETCURVAR_ARRAY_CREATE:
  844. {
  845. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE stk=0", ip - 1);
  846. break;
  847. }
  848. case OP_LOADVAR_UINT:
  849. {
  850. Con::printf("%i: OP_LOADVAR_UINT stk=+1", ip - 1);
  851. break;
  852. }
  853. case OP_LOADVAR_FLT:
  854. {
  855. Con::printf("%i: OP_LOADVAR_FLT stk=+1", ip - 1);
  856. break;
  857. }
  858. case OP_LOADVAR_STR:
  859. {
  860. Con::printf("%i: OP_LOADVAR_STR stk=+1", ip - 1);
  861. break;
  862. }
  863. case OP_SAVEVAR_UINT:
  864. {
  865. Con::printf("%i: OP_SAVEVAR_UINT stk=0", ip - 1);
  866. break;
  867. }
  868. case OP_SAVEVAR_FLT:
  869. {
  870. Con::printf("%i: OP_SAVEVAR_FLT stk=0", ip - 1);
  871. break;
  872. }
  873. case OP_SAVEVAR_STR:
  874. {
  875. Con::printf("%i: OP_SAVEVAR_STR stk=0", ip - 1);
  876. break;
  877. }
  878. case OP_LOAD_LOCAL_VAR_UINT:
  879. {
  880. Con::printf("%i: OP_LOAD_LOCAL_VAR_UINT stk=+1 reg=%i", ip - 1, code[ip]);
  881. ++ip;
  882. break;
  883. }
  884. case OP_LOAD_LOCAL_VAR_FLT:
  885. {
  886. Con::printf("%i: OP_LOAD_LOCAL_VAR_FLT stk=+1 reg=%i", ip - 1, code[ip]);
  887. ++ip;
  888. break;
  889. }
  890. case OP_LOAD_LOCAL_VAR_STR:
  891. {
  892. Con::printf("%i: OP_LOAD_LOCAL_VAR_STR stk=+1 reg=%i", ip - 1, code[ip]);
  893. ++ip;
  894. break;
  895. }
  896. case OP_SAVE_LOCAL_VAR_UINT:
  897. {
  898. Con::printf("%i: OP_SAVE_LOCAL_VAR_UINT stk=0 reg=%i", ip - 1, code[ip]);
  899. ++ip;
  900. break;
  901. }
  902. case OP_SAVE_LOCAL_VAR_FLT:
  903. {
  904. Con::printf("%i: OP_SAVE_LOCAL_VAR_FLT stk=0 reg=%i", ip - 1, code[ip]);
  905. ++ip;
  906. break;
  907. }
  908. case OP_SAVE_LOCAL_VAR_STR:
  909. {
  910. Con::printf("%i: OP_SAVE_LOCAL_VAR_STR stk=0 reg=%i", ip - 1, code[ip]);
  911. ++ip;
  912. break;
  913. }
  914. case OP_SETCUROBJECT:
  915. {
  916. Con::printf("%i: OP_SETCUROBJECT stk=0", ip - 1);
  917. break;
  918. }
  919. case OP_SETCUROBJECT_NEW:
  920. {
  921. Con::printf("%i: OP_SETCUROBJECT_NEW stk=0", ip - 1);
  922. break;
  923. }
  924. case OP_SETCUROBJECT_INTERNAL:
  925. {
  926. Con::printf("%i: OP_SETCUROBJECT_INTERNAL stk=0", ip - 1);
  927. ++ip;
  928. break;
  929. }
  930. case OP_SETCURFIELD:
  931. {
  932. StringTableEntry curField = CodeToSTE(code, ip);
  933. Con::printf("%i: OP_SETCURFIELD stk=0 field=%s", ip - 1, curField);
  934. ip += 2;
  935. break;
  936. }
  937. case OP_SETCURFIELD_ARRAY:
  938. {
  939. Con::printf("%i: OP_SETCURFIELD_ARRAY stk=0", ip - 1);
  940. break;
  941. }
  942. case OP_SETCURFIELD_TYPE:
  943. {
  944. U32 type = code[ip];
  945. Con::printf("%i: OP_SETCURFIELD_TYPE stk=0 type=%i", ip - 1, type);
  946. ++ip;
  947. break;
  948. }
  949. case OP_LOADFIELD_UINT:
  950. {
  951. Con::printf("%i: OP_LOADFIELD_UINT stk=+1", ip - 1);
  952. break;
  953. }
  954. case OP_LOADFIELD_FLT:
  955. {
  956. Con::printf("%i: OP_LOADFIELD_FLT stk=+1", ip - 1);
  957. break;
  958. }
  959. case OP_LOADFIELD_STR:
  960. {
  961. Con::printf("%i: OP_LOADFIELD_STR stk=+1", ip - 1);
  962. break;
  963. }
  964. case OP_SAVEFIELD_UINT:
  965. {
  966. Con::printf("%i: OP_SAVEFIELD_UINT stk=0", ip - 1);
  967. break;
  968. }
  969. case OP_SAVEFIELD_FLT:
  970. {
  971. Con::printf("%i: OP_SAVEFIELD_FLT stk=0", ip - 1);
  972. break;
  973. }
  974. case OP_SAVEFIELD_STR:
  975. {
  976. Con::printf("%i: OP_SAVEFIELD_STR stk=0", ip - 1);
  977. break;
  978. }
  979. case OP_POP_STK:
  980. {
  981. Con::printf("%i: OP_POP_STK stk=-1", ip - 1);
  982. break;
  983. }
  984. case OP_LOADIMMED_UINT:
  985. {
  986. U32 val = code[ip];
  987. Con::printf("%i: OP_LOADIMMED_UINT stk=+1 val=%i", ip - 1, val);
  988. ++ip;
  989. break;
  990. }
  991. case OP_LOADIMMED_FLT:
  992. {
  993. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  994. Con::printf("%i: OP_LOADIMMED_FLT stk=+1 val=%f", ip - 1, val);
  995. ++ip;
  996. break;
  997. }
  998. case OP_TAG_TO_STR:
  999. {
  1000. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1001. Con::printf("%i: OP_TAG_TO_STR stk=0 str=%s", ip - 1, str);
  1002. Con::printf(" OP_LOADIMMED_STR stk=+1 (fallthrough)");
  1003. ++ip;
  1004. break;
  1005. }
  1006. case OP_LOADIMMED_STR:
  1007. {
  1008. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1009. Con::printf("%i: OP_LOADIMMED_STR stk=+1 str=%s", ip - 1, str);
  1010. ++ip;
  1011. break;
  1012. }
  1013. case OP_DOCBLOCK_STR:
  1014. {
  1015. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1016. Con::printf("%i: OP_DOCBLOCK_STR stk=0 str=%s", ip - 1, str);
  1017. ++ip;
  1018. break;
  1019. }
  1020. case OP_LOADIMMED_IDENT:
  1021. {
  1022. StringTableEntry str = CodeToSTE(code, ip);
  1023. Con::printf("%i: OP_LOADIMMED_IDENT stk=+1 str=%s", ip - 1, str);
  1024. ip += 2;
  1025. break;
  1026. }
  1027. case OP_CALLFUNC:
  1028. {
  1029. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1030. StringTableEntry fnName = CodeToSTE(code, ip);
  1031. U32 callType = code[ip + 4];
  1032. StringTableEntry callTypeName;
  1033. switch (callType)
  1034. {
  1035. case FuncCallExprNode::FunctionCall: callTypeName = "FunctionCall"; break;
  1036. case FuncCallExprNode::MethodCall: callTypeName = "MethodCall"; break;
  1037. case FuncCallExprNode::ParentCall: callTypeName = "ParentCall"; break;
  1038. case FuncCallExprNode::StaticCall: callTypeName = "StaticCall"; break;
  1039. default: callTypeName = "INVALID"; break;
  1040. }
  1041. Con::printf("%i: OP_CALLFUNC stk=+1 name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace, callTypeName);
  1042. ip += 5;
  1043. break;
  1044. }
  1045. case OP_ADVANCE_STR_APPENDCHAR:
  1046. {
  1047. char ch = code[ip];
  1048. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR stk=0 char=%c", ip - 1, ch);
  1049. ++ip;
  1050. break;
  1051. }
  1052. case OP_REWIND_STR:
  1053. {
  1054. Con::printf("%i: OP_REWIND_STR stk=0", ip - 1);
  1055. Con::printf(" OP_TERMINATE_REWIND_STR stk=-1 (fallthrough)");
  1056. break;
  1057. }
  1058. case OP_TERMINATE_REWIND_STR:
  1059. {
  1060. Con::printf("%i: OP_TERMINATE_REWIND_STR stk=-1", ip - 1);
  1061. break;
  1062. }
  1063. case OP_COMPARE_STR:
  1064. {
  1065. Con::printf("%i: OP_COMPARE_STR stk=-1", ip - 1);
  1066. break;
  1067. }
  1068. case OP_PUSH:
  1069. {
  1070. Con::printf("%i: OP_PUSH stk=-1", ip - 1);
  1071. break;
  1072. }
  1073. case OP_PUSH_FRAME:
  1074. {
  1075. Con::printf("%i: OP_PUSH_FRAME stk=0 count=%i", ip - 1, code[ip]);
  1076. ++ip;
  1077. break;
  1078. }
  1079. case OP_ASSERT:
  1080. {
  1081. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1082. Con::printf("%i: OP_ASSERT stk=-1 message=%s", ip - 1, message);
  1083. ++ip;
  1084. break;
  1085. }
  1086. case OP_BREAK:
  1087. {
  1088. Con::printf("%i: OP_BREAK stk=0", ip - 1);
  1089. break;
  1090. }
  1091. case OP_ITER_BEGIN:
  1092. {
  1093. bool isGlobal = code[ip];
  1094. if (isGlobal)
  1095. {
  1096. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1097. U32 failIp = code[ip + 3];
  1098. Con::printf("%i: OP_ITER_BEGIN stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1099. ip += 4;
  1100. }
  1101. else
  1102. {
  1103. S32 reg = code[ip + 1];
  1104. U32 failIp = code[ip + 2];
  1105. Con::printf("%i: OP_ITER_BEGIN stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1106. ip += 3;
  1107. }
  1108. break;
  1109. }
  1110. case OP_ITER_BEGIN_STR:
  1111. {
  1112. bool isGlobal = code[ip];
  1113. if (isGlobal)
  1114. {
  1115. StringTableEntry varName = CodeToSTE(code, ip + 1);
  1116. U32 failIp = code[ip + 3];
  1117. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varName=%s failIp=%i isGlobal=%s", ip - 1, varName, failIp, "true");
  1118. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1119. ip += 4;
  1120. }
  1121. else
  1122. {
  1123. S32 reg = code[ip + 1];
  1124. U32 failIp = code[ip + 2];
  1125. Con::printf("%i: OP_ITER_BEGIN_STR stk=0 varRegister=%d failIp=%i isGlobal=%s", ip - 1, reg, failIp, "false");
  1126. Con::printf(" OP_ITER_BEGIN stk=0 (fallthrough)");
  1127. ip += 3;
  1128. }
  1129. break;
  1130. }
  1131. case OP_ITER:
  1132. {
  1133. U32 breakIp = code[ip];
  1134. Con::printf("%i: OP_ITER stk=0 breakIp=%i", ip - 1, breakIp);
  1135. ++ip;
  1136. break;
  1137. }
  1138. case OP_ITER_END:
  1139. {
  1140. Con::printf("%i: OP_ITER_END stk=-1", ip - 1);
  1141. break;
  1142. }
  1143. default:
  1144. Con::printf("%i: !!INVALID!!", ip - 1);
  1145. break;
  1146. }
  1147. }
  1148. smInFunction = false;
  1149. }