codeBlock.cpp 33 KB

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