codeBlock.cpp 33 KB

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