codeBlock.cpp 33 KB

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