codeBlock.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. }
  54. CodeBlock::~CodeBlock()
  55. {
  56. // Make sure we aren't lingering in the current code block...
  57. AssertFatal(smCurrentCodeBlock != this, "CodeBlock::~CodeBlock - Caught lingering in smCurrentCodeBlock!");
  58. if (name)
  59. removeFromCodeList();
  60. delete[] const_cast<char*>(globalStrings);
  61. delete[] const_cast<char*>(functionStrings);
  62. functionStringsMaxLen = 0;
  63. globalStringsMaxLen = 0;
  64. delete[] globalFloats;
  65. delete[] functionFloats;
  66. delete[] code;
  67. delete[] breakList;
  68. }
  69. //-------------------------------------------------------------------------
  70. StringTableEntry CodeBlock::getCurrentCodeBlockName()
  71. {
  72. if (CodeBlock::getCurrentBlock())
  73. return CodeBlock::getCurrentBlock()->name;
  74. else
  75. return NULL;
  76. }
  77. StringTableEntry CodeBlock::getCurrentCodeBlockFullPath()
  78. {
  79. if (CodeBlock::getCurrentBlock())
  80. return CodeBlock::getCurrentBlock()->fullPath;
  81. else
  82. return NULL;
  83. }
  84. StringTableEntry CodeBlock::getCurrentCodeBlockModName()
  85. {
  86. if (CodeBlock::getCurrentBlock())
  87. return CodeBlock::getCurrentBlock()->modPath;
  88. else
  89. return NULL;
  90. }
  91. CodeBlock *CodeBlock::find(StringTableEntry name)
  92. {
  93. for (CodeBlock *walk = CodeBlock::getCodeBlockList(); walk; walk = walk->nextFile)
  94. if (walk->name == name)
  95. return walk;
  96. return NULL;
  97. }
  98. //-------------------------------------------------------------------------
  99. void CodeBlock::addToCodeList()
  100. {
  101. // remove any code blocks with my name
  102. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  103. {
  104. if ((*walk)->name == name)
  105. {
  106. *walk = (*walk)->nextFile;
  107. break;
  108. }
  109. }
  110. nextFile = smCodeBlockList;
  111. smCodeBlockList = this;
  112. }
  113. void CodeBlock::clearAllBreaks()
  114. {
  115. if (!lineBreakPairs)
  116. return;
  117. for (U32 i = 0; i < lineBreakPairCount; i++)
  118. {
  119. U32 *p = lineBreakPairs + i * 2;
  120. code[p[1]] = p[0] & 0xFF;
  121. }
  122. }
  123. void CodeBlock::clearBreakpoint(U32 lineNumber)
  124. {
  125. if (!lineBreakPairs)
  126. return;
  127. for (U32 i = 0; i < lineBreakPairCount; i++)
  128. {
  129. U32 *p = lineBreakPairs + i * 2;
  130. if ((p[0] >> 8) == lineNumber)
  131. {
  132. code[p[1]] = p[0] & 0xFF;
  133. return;
  134. }
  135. }
  136. }
  137. void CodeBlock::setAllBreaks()
  138. {
  139. if (!lineBreakPairs)
  140. return;
  141. for (U32 i = 0; i < lineBreakPairCount; i++)
  142. {
  143. U32 *p = lineBreakPairs + i * 2;
  144. code[p[1]] = OP_BREAK;
  145. }
  146. }
  147. bool CodeBlock::setBreakpoint(U32 lineNumber)
  148. {
  149. if (!lineBreakPairs)
  150. return false;
  151. for (U32 i = 0; i < lineBreakPairCount; i++)
  152. {
  153. U32 *p = lineBreakPairs + i * 2;
  154. if ((p[0] >> 8) == lineNumber)
  155. {
  156. code[p[1]] = OP_BREAK;
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. U32 CodeBlock::findFirstBreakLine(U32 lineNumber)
  163. {
  164. if (!lineBreakPairs)
  165. return 0;
  166. for (U32 i = 0; i < lineBreakPairCount; i++)
  167. {
  168. U32 *p = lineBreakPairs + i * 2;
  169. U32 line = (p[0] >> 8);
  170. if (lineNumber <= line)
  171. return line;
  172. }
  173. return 0;
  174. }
  175. struct LinePair
  176. {
  177. U32 instLine;
  178. U32 ip;
  179. };
  180. void CodeBlock::findBreakLine(U32 ip, U32 &line, U32 &instruction)
  181. {
  182. U32 min = 0;
  183. U32 max = lineBreakPairCount - 1;
  184. LinePair *p = (LinePair *)lineBreakPairs;
  185. U32 found;
  186. if (!lineBreakPairCount || p[min].ip > ip || p[max].ip < ip)
  187. {
  188. line = 0;
  189. instruction = OP_INVALID;
  190. return;
  191. }
  192. else if (p[min].ip == ip)
  193. found = min;
  194. else if (p[max].ip == ip)
  195. found = max;
  196. else
  197. {
  198. for (;;)
  199. {
  200. if (min == max - 1)
  201. {
  202. found = min;
  203. break;
  204. }
  205. U32 mid = (min + max) >> 1;
  206. if (p[mid].ip == ip)
  207. {
  208. found = mid;
  209. break;
  210. }
  211. else if (p[mid].ip > ip)
  212. max = mid;
  213. else
  214. min = mid;
  215. }
  216. }
  217. instruction = p[found].instLine & 0xFF;
  218. line = p[found].instLine >> 8;
  219. }
  220. const char *CodeBlock::getFileLine(U32 ip)
  221. {
  222. static char nameBuffer[256];
  223. U32 line, inst;
  224. findBreakLine(ip, line, inst);
  225. dSprintf(nameBuffer, sizeof(nameBuffer), "%s (%d)", name ? name : "<input>", line);
  226. return nameBuffer;
  227. }
  228. void CodeBlock::removeFromCodeList()
  229. {
  230. for (CodeBlock **walk = &smCodeBlockList; *walk; walk = &((*walk)->nextFile))
  231. {
  232. if (*walk == this)
  233. {
  234. *walk = nextFile;
  235. // clear out all breakpoints
  236. clearAllBreaks();
  237. break;
  238. }
  239. }
  240. // Let the telnet debugger know that this code
  241. // block has been unloaded and that it needs to
  242. // remove references to it.
  243. if (TelDebugger)
  244. TelDebugger->clearCodeBlockPointers(this);
  245. }
  246. void CodeBlock::calcBreakList()
  247. {
  248. U32 size = 0;
  249. S32 line = -1;
  250. U32 seqCount = 0;
  251. U32 i;
  252. for (i = 0; i < lineBreakPairCount; i++)
  253. {
  254. U32 lineNumber = lineBreakPairs[i * 2];
  255. if (lineNumber == U32(line + 1))
  256. seqCount++;
  257. else
  258. {
  259. if (seqCount)
  260. size++;
  261. size++;
  262. seqCount = 1;
  263. }
  264. line = lineNumber;
  265. }
  266. if (seqCount)
  267. size++;
  268. breakList = new U32[size];
  269. breakListSize = size;
  270. line = -1;
  271. seqCount = 0;
  272. size = 0;
  273. for (i = 0; i < lineBreakPairCount; i++)
  274. {
  275. U32 lineNumber = lineBreakPairs[i * 2];
  276. if (lineNumber == U32(line + 1))
  277. seqCount++;
  278. else
  279. {
  280. if (seqCount)
  281. breakList[size++] = seqCount;
  282. breakList[size++] = lineNumber - getMax(0, line) - 1;
  283. seqCount = 1;
  284. }
  285. line = lineNumber;
  286. }
  287. if (seqCount)
  288. breakList[size++] = seqCount;
  289. for (i = 0; i < lineBreakPairCount; i++)
  290. {
  291. U32 *p = lineBreakPairs + i * 2;
  292. p[0] = (p[0] << 8) | code[p[1]];
  293. }
  294. // Let the telnet debugger know that this code
  295. // block has been loaded and that it can add break
  296. // points it has for it.
  297. if (TelDebugger)
  298. TelDebugger->addAllBreakpoints(this);
  299. }
  300. bool CodeBlock::read(StringTableEntry fileName, Stream &st)
  301. {
  302. const StringTableEntry exePath = Platform::getMainDotCsDir();
  303. const StringTableEntry cwd = Platform::getCurrentDirectory();
  304. name = fileName;
  305. if (fileName)
  306. {
  307. fullPath = NULL;
  308. if (Platform::isFullPath(fileName))
  309. fullPath = fileName;
  310. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  311. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  312. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  313. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  314. if (fullPath == NULL)
  315. {
  316. char buf[1024];
  317. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  318. }
  319. modPath = Con::getModNameFromPath(fileName);
  320. }
  321. //
  322. addToCodeList();
  323. U32 globalSize, size, i;
  324. st.read(&size);
  325. if (size)
  326. {
  327. globalStrings = new char[size];
  328. globalStringsMaxLen = size;
  329. st.read(size, globalStrings);
  330. }
  331. globalSize = size;
  332. st.read(&size);
  333. if (size)
  334. {
  335. functionStrings = new char[size];
  336. functionStringsMaxLen = size;
  337. st.read(size, functionStrings);
  338. }
  339. st.read(&size);
  340. if (size)
  341. {
  342. globalFloats = new F64[size];
  343. for (U32 i = 0; i < size; i++)
  344. st.read(&globalFloats[i]);
  345. }
  346. st.read(&size);
  347. if (size)
  348. {
  349. functionFloats = new F64[size];
  350. for (U32 i = 0; i < size; i++)
  351. st.read(&functionFloats[i]);
  352. }
  353. U32 codeLength;
  354. st.read(&codeLength);
  355. st.read(&lineBreakPairCount);
  356. U32 totSize = codeLength + lineBreakPairCount * 2;
  357. code = new U32[totSize];
  358. for (i = 0; i < codeLength; i++)
  359. {
  360. U8 b;
  361. st.read(&b);
  362. if (b == 0xFF)
  363. st.read(&code[i]);
  364. else
  365. code[i] = b;
  366. }
  367. for (i = codeLength; i < totSize; i++)
  368. st.read(&code[i]);
  369. lineBreakPairs = code + codeLength;
  370. // StringTable-ize our identifiers.
  371. U32 identCount;
  372. st.read(&identCount);
  373. while (identCount--)
  374. {
  375. U32 offset;
  376. st.read(&offset);
  377. StringTableEntry ste;
  378. if (offset < globalSize)
  379. ste = StringTable->insert(globalStrings + offset);
  380. else
  381. ste = StringTable->EmptyString();
  382. U32 count;
  383. st.read(&count);
  384. while (count--)
  385. {
  386. U32 ip;
  387. st.read(&ip);
  388. code[ip] = *((U32 *)&ste);
  389. }
  390. }
  391. if (lineBreakPairCount)
  392. calcBreakList();
  393. return true;
  394. }
  395. bool CodeBlock::compile(const char *codeFileName, StringTableEntry fileName, const char *inScript, bool overrideNoDso)
  396. {
  397. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  398. // This will return true, but return value is ignored
  399. char *script;
  400. chompUTF8BOM(inScript, &script);
  401. gSyntaxError = false;
  402. consoleAllocReset();
  403. STEtoCode = compileSTEtoCode;
  404. gStatementList = NULL;
  405. gAnonFunctionList = NULL;
  406. // Set up the parser.
  407. smCurrentParser = getParserForFile(fileName);
  408. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  409. // Now do some parsing.
  410. smCurrentParser->setScanBuffer(script, fileName);
  411. smCurrentParser->restart(NULL);
  412. smCurrentParser->parse();
  413. if (gStatementList)
  414. {
  415. if (gAnonFunctionList)
  416. {
  417. // Prepend anonymous functions to statement list, so they're defined already when
  418. // the statements run.
  419. gAnonFunctionList->append(gStatementList);
  420. gStatementList = gAnonFunctionList;
  421. }
  422. }
  423. if (gSyntaxError)
  424. {
  425. consoleAllocReset();
  426. return false;
  427. }
  428. #ifdef TORQUE_NO_DSO_GENERATION
  429. if (!overrideNoDso)
  430. return false;
  431. #endif // !TORQUE_NO_DSO_GENERATION
  432. FileStream st;
  433. if (!st.open(codeFileName, Torque::FS::File::Write))
  434. return false;
  435. st.write(U32(Con::DSOVersion));
  436. // Reset all our value tables...
  437. resetTables();
  438. smInFunction = false;
  439. CodeStream codeStream;
  440. U32 lastIp;
  441. if (gStatementList)
  442. {
  443. lastIp = compileBlock(gStatementList, codeStream, 0) + 1;
  444. }
  445. else
  446. {
  447. codeSize = 1;
  448. lastIp = 0;
  449. }
  450. codeStream.emit(OP_RETURN);
  451. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  452. lineBreakPairCount = codeStream.getNumLineBreaks();
  453. // Write string table data...
  454. getGlobalStringTable().write(st);
  455. getFunctionStringTable().write(st);
  456. // Write float table data...
  457. getGlobalFloatTable().write(st);
  458. getFunctionFloatTable().write(st);
  459. if (lastIp != codeSize)
  460. Con::errorf(ConsoleLogEntry::General, "CodeBlock::compile - precompile size mismatch, a precompile/compile function pair is probably mismatched.");
  461. U32 totSize = codeSize + codeStream.getNumLineBreaks() * 2;
  462. st.write(codeSize);
  463. st.write(lineBreakPairCount);
  464. // Write out our bytecode, doing a bit of compression for low numbers.
  465. U32 i;
  466. for (i = 0; i < codeSize; i++)
  467. {
  468. if (code[i] < 0xFF)
  469. st.write(U8(code[i]));
  470. else
  471. {
  472. st.write(U8(0xFF));
  473. st.write(code[i]);
  474. }
  475. }
  476. // Write the break info...
  477. for (i = codeSize; i < totSize; i++)
  478. st.write(code[i]);
  479. getIdentTable().write(st);
  480. consoleAllocReset();
  481. st.close();
  482. return true;
  483. }
  484. ConsoleValueRef CodeBlock::compileExec(StringTableEntry fileName, const char *inString, bool noCalls, S32 setFrame)
  485. {
  486. AssertFatal(Con::isMainThread(), "Compiling code on a secondary thread");
  487. // Check for a UTF8 script file
  488. char *string;
  489. chompUTF8BOM(inString, &string);
  490. STEtoCode = evalSTEtoCode;
  491. consoleAllocReset();
  492. name = fileName;
  493. if (fileName)
  494. {
  495. const StringTableEntry exePath = Platform::getMainDotCsDir();
  496. const StringTableEntry cwd = Platform::getCurrentDirectory();
  497. fullPath = NULL;
  498. if (Platform::isFullPath(fileName))
  499. fullPath = fileName;
  500. if (dStrnicmp(exePath, fileName, dStrlen(exePath)) == 0)
  501. name = StringTable->insert(fileName + dStrlen(exePath) + 1, true);
  502. else if (dStrnicmp(cwd, fileName, dStrlen(cwd)) == 0)
  503. name = StringTable->insert(fileName + dStrlen(cwd) + 1, true);
  504. if (fullPath == NULL)
  505. {
  506. char buf[1024];
  507. fullPath = StringTable->insert(Platform::makeFullPathName(fileName, buf, sizeof(buf)), true);
  508. }
  509. modPath = Con::getModNameFromPath(fileName);
  510. }
  511. if (name)
  512. addToCodeList();
  513. gStatementList = NULL;
  514. gAnonFunctionList = NULL;
  515. // Set up the parser.
  516. smCurrentParser = getParserForFile(fileName);
  517. AssertISV(smCurrentParser, avar("CodeBlock::compile - no parser available for '%s'!", fileName));
  518. // Now do some parsing.
  519. smCurrentParser->setScanBuffer(string, fileName);
  520. smCurrentParser->restart(NULL);
  521. smCurrentParser->parse();
  522. if (gStatementList)
  523. {
  524. if (gAnonFunctionList)
  525. {
  526. // Prepend anonymous functions to statement list, so they're defined already when
  527. // the statements run.
  528. gAnonFunctionList->append(gStatementList);
  529. gStatementList = gAnonFunctionList;
  530. }
  531. }
  532. if (!gStatementList)
  533. {
  534. delete this;
  535. return ConsoleValueRef();
  536. }
  537. resetTables();
  538. smInFunction = false;
  539. CodeStream codeStream;
  540. U32 lastIp = compileBlock(gStatementList, codeStream, 0);
  541. lineBreakPairCount = codeStream.getNumLineBreaks();
  542. globalStrings = getGlobalStringTable().build();
  543. globalStringsMaxLen = getGlobalStringTable().totalLen;
  544. functionStrings = getFunctionStringTable().build();
  545. functionStringsMaxLen = getFunctionStringTable().totalLen;
  546. globalFloats = getGlobalFloatTable().build();
  547. functionFloats = getFunctionFloatTable().build();
  548. codeStream.emit(OP_RETURN);
  549. codeStream.emitCodeStream(&codeSize, &code, &lineBreakPairs);
  550. //dumpInstructions(0, false);
  551. consoleAllocReset();
  552. if (lineBreakPairCount && fileName)
  553. calcBreakList();
  554. if (lastIp + 1 != codeSize)
  555. Con::warnf(ConsoleLogEntry::General, "precompile size mismatch, precompile: %d compile: %d", codeSize, lastIp);
  556. return exec(0, fileName, NULL, 0, 0, noCalls, NULL, setFrame);
  557. }
  558. //-------------------------------------------------------------------------
  559. void CodeBlock::incRefCount()
  560. {
  561. refCount++;
  562. }
  563. void CodeBlock::decRefCount()
  564. {
  565. refCount--;
  566. if (!refCount)
  567. delete this;
  568. }
  569. //-------------------------------------------------------------------------
  570. String CodeBlock::getFunctionArgs(U32 ip)
  571. {
  572. StringBuilder str;
  573. U32 fnArgc = code[ip + 5];
  574. for (U32 i = 0; i < fnArgc; ++i)
  575. {
  576. StringTableEntry var = CodeToSTE(code, ip + (i * 2) + 6);
  577. if (i != 0)
  578. str.append(", ");
  579. str.append("string ");
  580. // Try to capture junked parameters
  581. if (var[0])
  582. str.append(var + 1);
  583. else
  584. str.append("JUNK");
  585. }
  586. return str.end();
  587. }
  588. //-------------------------------------------------------------------------
  589. void CodeBlock::dumpInstructions(U32 startIp, bool upToReturn)
  590. {
  591. U32 ip = startIp;
  592. smInFunction = false;
  593. U32 endFuncIp = 0;
  594. while (ip < codeSize)
  595. {
  596. if (ip > endFuncIp)
  597. {
  598. smInFunction = false;
  599. }
  600. switch (code[ip++])
  601. {
  602. case OP_FUNC_DECL:
  603. {
  604. StringTableEntry fnName = CodeToSTE(code, ip);
  605. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  606. StringTableEntry fnPackage = CodeToSTE(code, ip + 4);
  607. bool hasBody = bool(code[ip + 6]);
  608. U32 newIp = code[ip + 7];
  609. U32 argc = code[ip + 8];
  610. endFuncIp = newIp;
  611. Con::printf("%i: OP_FUNC_DECL name=%s nspace=%s package=%s hasbody=%i newip=%i argc=%i",
  612. ip - 1, fnName, fnNamespace, fnPackage, hasBody, newIp, argc);
  613. // Skip args.
  614. ip += 9 + (argc * 2);
  615. smInFunction = true;
  616. break;
  617. }
  618. case OP_CREATE_OBJECT:
  619. {
  620. StringTableEntry objParent = CodeToSTE(code, ip);
  621. bool isDataBlock = code[ip + 2];
  622. bool isInternal = code[ip + 3];
  623. bool isSingleton = code[ip + 4];
  624. U32 lineNumber = code[ip + 5];
  625. U32 failJump = code[ip + 6];
  626. Con::printf("%i: OP_CREATE_OBJECT objParent=%s isDataBlock=%i isInternal=%i isSingleton=%i lineNumber=%i failJump=%i",
  627. ip - 1, objParent, isDataBlock, isInternal, isSingleton, lineNumber, failJump);
  628. ip += 7;
  629. break;
  630. }
  631. case OP_ADD_OBJECT:
  632. {
  633. bool placeAtRoot = code[ip++];
  634. Con::printf("%i: OP_ADD_OBJECT placeAtRoot=%i", ip - 1, placeAtRoot);
  635. break;
  636. }
  637. case OP_END_OBJECT:
  638. {
  639. bool placeAtRoot = code[ip++];
  640. Con::printf("%i: OP_END_OBJECT placeAtRoot=%i", ip - 1, placeAtRoot);
  641. break;
  642. }
  643. case OP_FINISH_OBJECT:
  644. {
  645. Con::printf("%i: OP_FINISH_OBJECT", ip - 1);
  646. break;
  647. }
  648. case OP_JMPIFFNOT:
  649. {
  650. Con::printf("%i: OP_JMPIFFNOT ip=%i", ip - 1, code[ip]);
  651. ++ip;
  652. break;
  653. }
  654. case OP_JMPIFNOT:
  655. {
  656. Con::printf("%i: OP_JMPIFNOT ip=%i", ip - 1, code[ip]);
  657. ++ip;
  658. break;
  659. }
  660. case OP_JMPIFF:
  661. {
  662. Con::printf("%i: OP_JMPIFF ip=%i", ip - 1, code[ip]);
  663. ++ip;
  664. break;
  665. }
  666. case OP_JMPIF:
  667. {
  668. Con::printf("%i: OP_JMPIF ip=%i", ip - 1, code[ip]);
  669. ++ip;
  670. break;
  671. }
  672. case OP_JMPIFNOT_NP:
  673. {
  674. Con::printf("%i: OP_JMPIFNOT_NP ip=%i", ip - 1, code[ip]);
  675. ++ip;
  676. break;
  677. }
  678. case OP_JMPIF_NP:
  679. {
  680. Con::printf("%i: OP_JMPIF_NP ip=%i", ip - 1, code[ip]);
  681. ++ip;
  682. break;
  683. }
  684. case OP_JMP:
  685. {
  686. Con::printf("%i: OP_JMP ip=%i", ip - 1, code[ip]);
  687. ++ip;
  688. break;
  689. }
  690. case OP_RETURN:
  691. {
  692. Con::printf("%i: OP_RETURN", ip - 1);
  693. if (upToReturn)
  694. return;
  695. break;
  696. }
  697. case OP_RETURN_VOID:
  698. {
  699. Con::printf("%i: OP_RETURNVOID", ip - 1);
  700. if (upToReturn)
  701. return;
  702. break;
  703. }
  704. case OP_RETURN_UINT:
  705. {
  706. Con::printf("%i: OP_RETURNUINT", ip - 1);
  707. if (upToReturn)
  708. return;
  709. break;
  710. }
  711. case OP_RETURN_FLT:
  712. {
  713. Con::printf("%i: OP_RETURNFLT", ip - 1);
  714. if (upToReturn)
  715. return;
  716. break;
  717. }
  718. case OP_CMPEQ:
  719. {
  720. Con::printf("%i: OP_CMPEQ", ip - 1);
  721. break;
  722. }
  723. case OP_CMPGR:
  724. {
  725. Con::printf("%i: OP_CMPGR", ip - 1);
  726. break;
  727. }
  728. case OP_CMPGE:
  729. {
  730. Con::printf("%i: OP_CMPGE", ip - 1);
  731. break;
  732. }
  733. case OP_CMPLT:
  734. {
  735. Con::printf("%i: OP_CMPLT", ip - 1);
  736. break;
  737. }
  738. case OP_CMPLE:
  739. {
  740. Con::printf("%i: OP_CMPLE", ip - 1);
  741. break;
  742. }
  743. case OP_CMPNE:
  744. {
  745. Con::printf("%i: OP_CMPNE", ip - 1);
  746. break;
  747. }
  748. case OP_XOR:
  749. {
  750. Con::printf("%i: OP_XOR", ip - 1);
  751. break;
  752. }
  753. case OP_MOD:
  754. {
  755. Con::printf("%i: OP_MOD", ip - 1);
  756. break;
  757. }
  758. case OP_BITAND:
  759. {
  760. Con::printf("%i: OP_BITAND", ip - 1);
  761. break;
  762. }
  763. case OP_BITOR:
  764. {
  765. Con::printf("%i: OP_BITOR", ip - 1);
  766. break;
  767. }
  768. case OP_NOT:
  769. {
  770. Con::printf("%i: OP_NOT", ip - 1);
  771. break;
  772. }
  773. case OP_NOTF:
  774. {
  775. Con::printf("%i: OP_NOTF", ip - 1);
  776. break;
  777. }
  778. case OP_ONESCOMPLEMENT:
  779. {
  780. Con::printf("%i: OP_ONESCOMPLEMENT", ip - 1);
  781. break;
  782. }
  783. case OP_SHR:
  784. {
  785. Con::printf("%i: OP_SHR", ip - 1);
  786. break;
  787. }
  788. case OP_SHL:
  789. {
  790. Con::printf("%i: OP_SHL", ip - 1);
  791. break;
  792. }
  793. case OP_AND:
  794. {
  795. Con::printf("%i: OP_AND", ip - 1);
  796. break;
  797. }
  798. case OP_OR:
  799. {
  800. Con::printf("%i: OP_OR", ip - 1);
  801. break;
  802. }
  803. case OP_ADD:
  804. {
  805. Con::printf("%i: OP_ADD", ip - 1);
  806. break;
  807. }
  808. case OP_SUB:
  809. {
  810. Con::printf("%i: OP_SUB", ip - 1);
  811. break;
  812. }
  813. case OP_MUL:
  814. {
  815. Con::printf("%i: OP_MUL", ip - 1);
  816. break;
  817. }
  818. case OP_DIV:
  819. {
  820. Con::printf("%i: OP_DIV", ip - 1);
  821. break;
  822. }
  823. case OP_NEG:
  824. {
  825. Con::printf("%i: OP_NEG", ip - 1);
  826. break;
  827. }
  828. case OP_INC:
  829. {
  830. Con::printf("%i: OP_INC varName=%s", ip - 1, CodeToSTE(code, ip));
  831. ip += 2;
  832. break;
  833. }
  834. case OP_DEC:
  835. {
  836. Con::printf("%i: OP_DEC varName=%s", ip - 1, CodeToSTE(code, ip));
  837. ip += 2;
  838. break;
  839. }
  840. case OP_SETCURVAR:
  841. {
  842. StringTableEntry var = CodeToSTE(code, ip);
  843. Con::printf("%i: OP_SETCURVAR var=%s", ip - 1, var);
  844. ip += 2;
  845. break;
  846. }
  847. case OP_SETCURVAR_CREATE:
  848. {
  849. StringTableEntry var = CodeToSTE(code, ip);
  850. Con::printf("%i: OP_SETCURVAR_CREATE var=%s", ip - 1, var);
  851. ip += 2;
  852. break;
  853. }
  854. case OP_SETCURVAR_ARRAY:
  855. {
  856. Con::printf("%i: OP_SETCURVAR_ARRAY", ip - 1);
  857. break;
  858. }
  859. case OP_SETCURVAR_ARRAY_VARLOOKUP:
  860. {
  861. StringTableEntry arrayName = CodeToSTE(code, ip);
  862. StringTableEntry arrayLookup = CodeToSTE(code, ip + 2);
  863. Con::printf("%i: OP_SETCURVAR_ARRAY_VARLOOKUP arrayName=%s arrayLookup=%s", ip - 1, arrayName, arrayLookup);
  864. ip += 4;
  865. break;
  866. }
  867. case OP_SETCURVAR_ARRAY_CREATE:
  868. {
  869. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE", ip - 1);
  870. break;
  871. }
  872. case OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP:
  873. {
  874. StringTableEntry arrayName = CodeToSTE(code, ip);
  875. StringTableEntry arrayLookup = CodeToSTE(code, ip + 2);
  876. Con::printf("%i: OP_SETCURVAR_ARRAY_CREATE_VARLOOKUP arrayName=%s arrayLookup=%s", ip - 1, arrayName, arrayLookup);
  877. ip += 4;
  878. break;
  879. }
  880. case OP_LOADVAR_UINT:
  881. {
  882. Con::printf("%i: OP_LOADVAR_UINT", ip - 1);
  883. break;
  884. }
  885. case OP_LOADVAR_FLT:
  886. {
  887. Con::printf("%i: OP_LOADVAR_FLT", ip - 1);
  888. break;
  889. }
  890. case OP_LOADVAR_STR:
  891. {
  892. Con::printf("%i: OP_LOADVAR_STR", ip - 1);
  893. break;
  894. }
  895. case OP_LOADVAR_VAR:
  896. {
  897. Con::printf("%i: OP_LOADVAR_VAR", ip - 1);
  898. break;
  899. }
  900. case OP_SAVEVAR_UINT:
  901. {
  902. Con::printf("%i: OP_SAVEVAR_UINT", ip - 1);
  903. break;
  904. }
  905. case OP_SAVEVAR_FLT:
  906. {
  907. Con::printf("%i: OP_SAVEVAR_FLT", ip - 1);
  908. break;
  909. }
  910. case OP_SAVEVAR_STR:
  911. {
  912. Con::printf("%i: OP_SAVEVAR_STR", ip - 1);
  913. break;
  914. }
  915. case OP_SAVEVAR_VAR:
  916. {
  917. Con::printf("%i: OP_SAVEVAR_VAR", ip - 1);
  918. break;
  919. }
  920. case OP_SETCUROBJECT:
  921. {
  922. Con::printf("%i: OP_SETCUROBJECT", ip - 1);
  923. break;
  924. }
  925. case OP_SETCUROBJECT_NEW:
  926. {
  927. Con::printf("%i: OP_SETCUROBJECT_NEW", ip - 1);
  928. break;
  929. }
  930. case OP_SETCUROBJECT_INTERNAL:
  931. {
  932. Con::printf("%i: OP_SETCUROBJECT_INTERNAL", ip - 1);
  933. ++ip;
  934. break;
  935. }
  936. case OP_SETCURFIELD:
  937. {
  938. StringTableEntry curField = CodeToSTE(code, ip);
  939. Con::printf("%i: OP_SETCURFIELD field=%s", ip - 1, curField);
  940. ip += 2;
  941. break;
  942. }
  943. case OP_SETCURFIELD_ARRAY:
  944. {
  945. Con::printf("%i: OP_SETCURFIELD_ARRAY", ip - 1);
  946. break;
  947. }
  948. case OP_SETCURFIELD_ARRAY_VAR:
  949. {
  950. StringTableEntry var = CodeToSTE(code, ip);
  951. Con::printf("%i: OP_SETCURFIELD_ARRAY_VAR var=%s", ip - 1, var);
  952. ip += 2;
  953. break;
  954. }
  955. case OP_SETCURFIELD_THIS:
  956. {
  957. StringTableEntry curField = CodeToSTE(code, ip);
  958. Con::printf("%i: OP_SETCURFIELD_THIS field=%s", ip - 1, curField);
  959. ip += 2;
  960. break;
  961. }
  962. case OP_SETCURFIELD_TYPE:
  963. {
  964. U32 type = code[ip];
  965. Con::printf("%i: OP_SETCURFIELD_TYPE type=%i", ip - 1, type);
  966. ++ip;
  967. break;
  968. }
  969. case OP_LOADFIELD_UINT:
  970. {
  971. Con::printf("%i: OP_LOADFIELD_UINT", ip - 1);
  972. break;
  973. }
  974. case OP_LOADFIELD_FLT:
  975. {
  976. Con::printf("%i: OP_LOADFIELD_FLT", ip - 1);
  977. break;
  978. }
  979. case OP_LOADFIELD_STR:
  980. {
  981. Con::printf("%i: OP_LOADFIELD_STR", ip - 1);
  982. break;
  983. }
  984. case OP_SAVEFIELD_UINT:
  985. {
  986. Con::printf("%i: OP_SAVEFIELD_UINT", ip - 1);
  987. break;
  988. }
  989. case OP_SAVEFIELD_FLT:
  990. {
  991. Con::printf("%i: OP_SAVEFIELD_FLT", ip - 1);
  992. break;
  993. }
  994. case OP_SAVEFIELD_STR:
  995. {
  996. Con::printf("%i: OP_SAVEFIELD_STR", ip - 1);
  997. break;
  998. }
  999. case OP_STR_TO_UINT:
  1000. {
  1001. Con::printf("%i: OP_STR_TO_UINT", ip - 1);
  1002. break;
  1003. }
  1004. case OP_STR_TO_FLT:
  1005. {
  1006. Con::printf("%i: OP_STR_TO_FLT", ip - 1);
  1007. break;
  1008. }
  1009. case OP_STR_TO_NONE:
  1010. {
  1011. Con::printf("%i: OP_STR_TO_NONE", ip - 1);
  1012. break;
  1013. }
  1014. case OP_FLT_TO_UINT:
  1015. {
  1016. Con::printf("%i: OP_FLT_TO_UINT", ip - 1);
  1017. break;
  1018. }
  1019. case OP_FLT_TO_STR:
  1020. {
  1021. Con::printf("%i: OP_FLT_TO_STR", ip - 1);
  1022. break;
  1023. }
  1024. case OP_FLT_TO_NONE:
  1025. {
  1026. Con::printf("%i: OP_FLT_TO_NONE", ip - 1);
  1027. break;
  1028. }
  1029. case OP_UINT_TO_FLT:
  1030. {
  1031. Con::printf("%i: OP_SAVEFIELD_STR", ip - 1);
  1032. break;
  1033. }
  1034. case OP_UINT_TO_STR:
  1035. {
  1036. Con::printf("%i: OP_UINT_TO_STR", ip - 1);
  1037. break;
  1038. }
  1039. case OP_UINT_TO_NONE:
  1040. {
  1041. Con::printf("%i: OP_UINT_TO_NONE", ip - 1);
  1042. break;
  1043. }
  1044. case OP_COPYVAR_TO_NONE:
  1045. {
  1046. Con::printf("%i: OP_COPYVAR_TO_NONE", ip - 1);
  1047. break;
  1048. }
  1049. case OP_LOADIMMED_UINT:
  1050. {
  1051. U32 val = code[ip];
  1052. Con::printf("%i: OP_LOADIMMED_UINT val=%i", ip - 1, val);
  1053. ++ip;
  1054. break;
  1055. }
  1056. case OP_LOADIMMED_FLT:
  1057. {
  1058. F64 val = (smInFunction ? functionFloats : globalFloats)[code[ip]];
  1059. Con::printf("%i: OP_LOADIMMED_FLT val=%f", ip - 1, val);
  1060. ++ip;
  1061. break;
  1062. }
  1063. case OP_TAG_TO_STR:
  1064. {
  1065. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1066. Con::printf("%i: OP_TAG_TO_STR str=%s", ip - 1, str);
  1067. ++ip;
  1068. break;
  1069. }
  1070. case OP_LOADIMMED_STR:
  1071. {
  1072. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1073. Con::printf("%i: OP_LOADIMMED_STR str=%s", ip - 1, str);
  1074. ++ip;
  1075. break;
  1076. }
  1077. case OP_DOCBLOCK_STR:
  1078. {
  1079. const char* str = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1080. Con::printf("%i: OP_DOCBLOCK_STR str=%s", ip - 1, str);
  1081. ++ip;
  1082. break;
  1083. }
  1084. case OP_LOADIMMED_IDENT:
  1085. {
  1086. StringTableEntry str = CodeToSTE(code, ip);
  1087. Con::printf("%i: OP_LOADIMMED_IDENT str=%s", ip - 1, str);
  1088. ip += 2;
  1089. break;
  1090. }
  1091. case OP_CALLFUNC_RESOLVE:
  1092. {
  1093. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1094. StringTableEntry fnName = CodeToSTE(code, ip);
  1095. U32 callType = code[ip + 2];
  1096. Con::printf("%i: OP_CALLFUNC_RESOLVE name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace,
  1097. callType == FuncCallExprNode::FunctionCall ? "FunctionCall"
  1098. : callType == FuncCallExprNode::MethodCall ? "MethodCall" : "ParentCall");
  1099. ip += 5;
  1100. break;
  1101. }
  1102. case OP_CALLFUNC:
  1103. {
  1104. StringTableEntry fnNamespace = CodeToSTE(code, ip + 2);
  1105. StringTableEntry fnName = CodeToSTE(code, ip);
  1106. U32 callType = code[ip + 4];
  1107. Con::printf("%i: OP_CALLFUNC name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace,
  1108. callType == FuncCallExprNode::FunctionCall ? "FunctionCall"
  1109. : callType == FuncCallExprNode::MethodCall ? "MethodCall" : "ParentCall");
  1110. ip += 5;
  1111. break;
  1112. }
  1113. case OP_CALLFUNC_POINTER:
  1114. {
  1115. Con::printf("%i: OP_CALLFUNC_POINTER", ip - 1);
  1116. break;
  1117. }
  1118. case OP_CALLFUNC_THIS:
  1119. {
  1120. StringTableEntry fnName = CodeToSTE(code, ip);
  1121. Con::printf("%i: OP_CALLFUNC_THIS name=%s ", ip - 1, fnName);
  1122. ip += 2;
  1123. break;
  1124. }
  1125. case OP_ADVANCE_STR:
  1126. {
  1127. Con::printf("%i: OP_ADVANCE_STR", ip - 1);
  1128. break;
  1129. }
  1130. case OP_ADVANCE_STR_APPENDCHAR:
  1131. {
  1132. char ch = code[ip];
  1133. Con::printf("%i: OP_ADVANCE_STR_APPENDCHAR char=%c", ip - 1, ch);
  1134. ++ip;
  1135. break;
  1136. }
  1137. case OP_ADVANCE_STR_COMMA:
  1138. {
  1139. Con::printf("%i: OP_ADVANCE_STR_COMMA", ip - 1);
  1140. break;
  1141. }
  1142. case OP_ADVANCE_STR_NUL:
  1143. {
  1144. Con::printf("%i: OP_ADVANCE_STR_NUL", ip - 1);
  1145. break;
  1146. }
  1147. case OP_REWIND_STR:
  1148. {
  1149. Con::printf("%i: OP_REWIND_STR", ip - 1);
  1150. break;
  1151. }
  1152. case OP_TERMINATE_REWIND_STR:
  1153. {
  1154. Con::printf("%i: OP_TERMINATE_REWIND_STR", ip - 1);
  1155. break;
  1156. }
  1157. case OP_COMPARE_STR:
  1158. {
  1159. Con::printf("%i: OP_COMPARE_STR", ip - 1);
  1160. break;
  1161. }
  1162. case OP_PUSH:
  1163. {
  1164. Con::printf("%i: OP_PUSH", ip - 1);
  1165. break;
  1166. }
  1167. case OP_PUSH_UINT:
  1168. {
  1169. Con::printf("%i: OP_PUSH_UINT", ip - 1);
  1170. break;
  1171. }
  1172. case OP_PUSH_FLT:
  1173. {
  1174. Con::printf("%i: OP_PUSH_FLT", ip - 1);
  1175. break;
  1176. }
  1177. case OP_PUSH_VAR:
  1178. {
  1179. Con::printf("%i: OP_PUSH_VAR", ip - 1);
  1180. break;
  1181. }
  1182. case OP_PUSH_THIS:
  1183. {
  1184. Con::printf("%i: OP_PUSH_THIS varName=%s", ip - 1, CodeToSTE(code, ip));
  1185. ip += 2;
  1186. break;
  1187. }
  1188. case OP_PUSH_FRAME:
  1189. {
  1190. Con::printf("%i: OP_PUSH_FRAME", ip - 1);
  1191. break;
  1192. }
  1193. case OP_ASSERT:
  1194. {
  1195. const char* message = (smInFunction ? functionStrings : globalStrings) + code[ip];
  1196. Con::printf("%i: OP_ASSERT message=%s", ip - 1, message);
  1197. ++ip;
  1198. break;
  1199. }
  1200. case OP_BREAK:
  1201. {
  1202. Con::printf("%i: OP_BREAK", ip - 1);
  1203. break;
  1204. }
  1205. case OP_ITER_BEGIN:
  1206. {
  1207. StringTableEntry varName = CodeToSTE(code, ip);
  1208. U32 failIp = code[ip + 2];
  1209. Con::printf("%i: OP_ITER_BEGIN varName=%s failIp=%i", ip - 1, varName, failIp);
  1210. ip += 3;
  1211. break;
  1212. }
  1213. case OP_ITER_BEGIN_STR:
  1214. {
  1215. StringTableEntry varName = CodeToSTE(code, ip);
  1216. U32 failIp = code[ip + 2];
  1217. Con::printf("%i: OP_ITER_BEGIN varName=%s failIp=%i", ip - 1, varName, failIp);
  1218. ip += 3;
  1219. break;
  1220. }
  1221. case OP_ITER:
  1222. {
  1223. U32 breakIp = code[ip];
  1224. Con::printf("%i: OP_ITER breakIp=%i", ip - 1, breakIp);
  1225. ++ip;
  1226. break;
  1227. }
  1228. case OP_ITER_END:
  1229. {
  1230. Con::printf("%i: OP_ITER_END", ip - 1);
  1231. break;
  1232. }
  1233. default:
  1234. Con::printf("%i: !!INVALID!!", ip - 1);
  1235. break;
  1236. }
  1237. }
  1238. smInFunction = false;
  1239. }