2
0

codeBlock.cpp 33 KB

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