codeBlock.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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 codeLength;
  355. st.read(&codeLength);
  356. st.read(&lineBreakPairCount);
  357. U32 totSize = codeLength + lineBreakPairCount * 2;
  358. code = new U32[totSize];
  359. for(i = 0; i < codeLength; 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 = codeLength; i < totSize; i++)
  369. st.read(&code[i]);
  370. lineBreakPairs = code + codeLength;
  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_CMPEQ:
  679. {
  680. Con::printf( "%i: OP_CMPEQ", ip - 1 );
  681. break;
  682. }
  683. case OP_CMPGR:
  684. {
  685. Con::printf( "%i: OP_CMPGR", ip - 1 );
  686. break;
  687. }
  688. case OP_CMPGE:
  689. {
  690. Con::printf( "%i: OP_CMPGE", ip - 1 );
  691. break;
  692. }
  693. case OP_CMPLT:
  694. {
  695. Con::printf( "%i: OP_CMPLT", ip - 1 );
  696. break;
  697. }
  698. case OP_CMPLE:
  699. {
  700. Con::printf( "%i: OP_CMPLE", ip - 1 );
  701. break;
  702. }
  703. case OP_CMPNE:
  704. {
  705. Con::printf( "%i: OP_CMPNE", ip - 1 );
  706. break;
  707. }
  708. case OP_XOR:
  709. {
  710. Con::printf( "%i: OP_XOR", ip - 1 );
  711. break;
  712. }
  713. case OP_MOD:
  714. {
  715. Con::printf( "%i: OP_MOD", ip - 1 );
  716. break;
  717. }
  718. case OP_BITAND:
  719. {
  720. Con::printf( "%i: OP_BITAND", ip - 1 );
  721. break;
  722. }
  723. case OP_BITOR:
  724. {
  725. Con::printf( "%i: OP_BITOR", ip - 1 );
  726. break;
  727. }
  728. case OP_NOT:
  729. {
  730. Con::printf( "%i: OP_NOT", ip - 1 );
  731. break;
  732. }
  733. case OP_NOTF:
  734. {
  735. Con::printf( "%i: OP_NOTF", ip - 1 );
  736. break;
  737. }
  738. case OP_ONESCOMPLEMENT:
  739. {
  740. Con::printf( "%i: OP_ONESCOMPLEMENT", ip - 1 );
  741. break;
  742. }
  743. case OP_SHR:
  744. {
  745. Con::printf( "%i: OP_SHR", ip - 1 );
  746. break;
  747. }
  748. case OP_SHL:
  749. {
  750. Con::printf( "%i: OP_SHL", ip - 1 );
  751. break;
  752. }
  753. case OP_AND:
  754. {
  755. Con::printf( "%i: OP_AND", ip - 1 );
  756. break;
  757. }
  758. case OP_OR:
  759. {
  760. Con::printf( "%i: OP_OR", ip - 1 );
  761. break;
  762. }
  763. case OP_ADD:
  764. {
  765. Con::printf( "%i: OP_ADD", ip - 1 );
  766. break;
  767. }
  768. case OP_SUB:
  769. {
  770. Con::printf( "%i: OP_SUB", ip - 1 );
  771. break;
  772. }
  773. case OP_MUL:
  774. {
  775. Con::printf( "%i: OP_MUL", ip - 1 );
  776. break;
  777. }
  778. case OP_DIV:
  779. {
  780. Con::printf( "%i: OP_DIV", ip - 1 );
  781. break;
  782. }
  783. case OP_NEG:
  784. {
  785. Con::printf( "%i: OP_NEG", ip - 1 );
  786. break;
  787. }
  788. case OP_SETCURVAR:
  789. {
  790. StringTableEntry var = U32toSTE(code[ip]);
  791. Con::printf( "%i: OP_SETCURVAR var=%s", ip - 1, var );
  792. ip++;
  793. break;
  794. }
  795. case OP_SETCURVAR_CREATE:
  796. {
  797. StringTableEntry var = U32toSTE(code[ip]);
  798. Con::printf( "%i: OP_SETCURVAR_CREATE var=%s", ip - 1, var );
  799. ip++;
  800. break;
  801. }
  802. case OP_SETCURVAR_ARRAY:
  803. {
  804. Con::printf( "%i: OP_SETCURVAR_ARRAY", ip - 1 );
  805. break;
  806. }
  807. case OP_SETCURVAR_ARRAY_CREATE:
  808. {
  809. Con::printf( "%i: OP_SETCURVAR_ARRAY_CREATE", ip - 1 );
  810. break;
  811. }
  812. case OP_LOADVAR_UINT:
  813. {
  814. Con::printf( "%i: OP_LOADVAR_UINT", ip - 1 );
  815. break;
  816. }
  817. case OP_LOADVAR_FLT:
  818. {
  819. Con::printf( "%i: OP_LOADVAR_FLT", ip - 1 );
  820. break;
  821. }
  822. case OP_LOADVAR_STR:
  823. {
  824. Con::printf( "%i: OP_LOADVAR_STR", ip - 1 );
  825. break;
  826. }
  827. case OP_SAVEVAR_UINT:
  828. {
  829. Con::printf( "%i: OP_SAVEVAR_UINT", ip - 1 );
  830. break;
  831. }
  832. case OP_SAVEVAR_FLT:
  833. {
  834. Con::printf( "%i: OP_SAVEVAR_FLT", ip - 1 );
  835. break;
  836. }
  837. case OP_SAVEVAR_STR:
  838. {
  839. Con::printf( "%i: OP_SAVEVAR_STR", ip - 1 );
  840. break;
  841. }
  842. case OP_SETCUROBJECT:
  843. {
  844. Con::printf( "%i: OP_SETCUROBJECT", ip - 1 );
  845. break;
  846. }
  847. case OP_SETCUROBJECT_NEW:
  848. {
  849. Con::printf( "%i: OP_SETCUROBJECT_NEW", ip - 1 );
  850. break;
  851. }
  852. case OP_SETCUROBJECT_INTERNAL:
  853. {
  854. Con::printf( "%i: OP_SETCUROBJECT_INTERNAL", ip - 1 );
  855. ++ ip;
  856. break;
  857. }
  858. case OP_SETCURFIELD:
  859. {
  860. StringTableEntry curField = U32toSTE(code[ip]);
  861. Con::printf( "%i: OP_SETCURFIELD field=%s", ip - 1, curField );
  862. ++ ip;
  863. }
  864. case OP_SETCURFIELD_ARRAY:
  865. {
  866. Con::printf( "%i: OP_SETCURFIELD_ARRAY", ip - 1 );
  867. break;
  868. }
  869. case OP_SETCURFIELD_TYPE:
  870. {
  871. U32 type = code[ ip ];
  872. Con::printf( "%i: OP_SETCURFIELD_TYPE type=%i", ip - 1, type );
  873. ++ ip;
  874. break;
  875. }
  876. case OP_LOADFIELD_UINT:
  877. {
  878. Con::printf( "%i: OP_LOADFIELD_UINT", ip - 1 );
  879. break;
  880. }
  881. case OP_LOADFIELD_FLT:
  882. {
  883. Con::printf( "%i: OP_LOADFIELD_FLT", ip - 1 );
  884. break;
  885. }
  886. case OP_LOADFIELD_STR:
  887. {
  888. Con::printf( "%i: OP_LOADFIELD_STR", ip - 1 );
  889. break;
  890. }
  891. case OP_SAVEFIELD_UINT:
  892. {
  893. Con::printf( "%i: OP_SAVEFIELD_UINT", ip - 1 );
  894. break;
  895. }
  896. case OP_SAVEFIELD_FLT:
  897. {
  898. Con::printf( "%i: OP_SAVEFIELD_FLT", ip - 1 );
  899. break;
  900. }
  901. case OP_SAVEFIELD_STR:
  902. {
  903. Con::printf( "%i: OP_SAVEFIELD_STR", ip - 1 );
  904. break;
  905. }
  906. case OP_STR_TO_UINT:
  907. {
  908. Con::printf( "%i: OP_STR_TO_UINT", ip - 1 );
  909. break;
  910. }
  911. case OP_STR_TO_FLT:
  912. {
  913. Con::printf( "%i: OP_STR_TO_FLT", ip - 1 );
  914. break;
  915. }
  916. case OP_STR_TO_NONE:
  917. {
  918. Con::printf( "%i: OP_STR_TO_NONE", ip - 1 );
  919. break;
  920. }
  921. case OP_FLT_TO_UINT:
  922. {
  923. Con::printf( "%i: OP_FLT_TO_UINT", ip - 1 );
  924. break;
  925. }
  926. case OP_FLT_TO_STR:
  927. {
  928. Con::printf( "%i: OP_FLT_TO_STR", ip - 1 );
  929. break;
  930. }
  931. case OP_FLT_TO_NONE:
  932. {
  933. Con::printf( "%i: OP_FLT_TO_NONE", ip - 1 );
  934. break;
  935. }
  936. case OP_UINT_TO_FLT:
  937. {
  938. Con::printf( "%i: OP_SAVEFIELD_STR", ip - 1 );
  939. break;
  940. }
  941. case OP_UINT_TO_STR:
  942. {
  943. Con::printf( "%i: OP_UINT_TO_STR", ip - 1 );
  944. break;
  945. }
  946. case OP_UINT_TO_NONE:
  947. {
  948. Con::printf( "%i: OP_UINT_TO_NONE", ip - 1 );
  949. break;
  950. }
  951. case OP_LOADIMMED_UINT:
  952. {
  953. U32 val = code[ ip ];
  954. Con::printf( "%i: OP_LOADIMMED_UINT val=%i", ip - 1, val );
  955. ++ ip;
  956. break;
  957. }
  958. case OP_LOADIMMED_FLT:
  959. {
  960. F64 val = functionFloats[ code[ ip ] ];
  961. Con::printf( "%i: OP_LOADIMMED_FLT val=%f", ip - 1, val );
  962. ++ ip;
  963. break;
  964. }
  965. case OP_TAG_TO_STR:
  966. {
  967. const char* str = functionStrings + code[ ip ];
  968. Con::printf( "%i: OP_TAG_TO_STR str=%s", ip - 1, str );
  969. ++ ip;
  970. break;
  971. }
  972. case OP_LOADIMMED_STR:
  973. {
  974. const char* str = functionStrings + code[ ip ];
  975. Con::printf( "%i: OP_LOADIMMED_STR str=%s", ip - 1, str );
  976. ++ ip;
  977. break;
  978. }
  979. case OP_DOCBLOCK_STR:
  980. {
  981. const char* str = functionStrings + code[ ip ];
  982. Con::printf( "%i: OP_DOCBLOCK_STR str=%s", ip - 1, str );
  983. ++ ip;
  984. break;
  985. }
  986. case OP_LOADIMMED_IDENT:
  987. {
  988. StringTableEntry str = U32toSTE( code[ ip ] );
  989. Con::printf( "%i: OP_LOADIMMED_IDENT str=%s", ip - 1, str );
  990. ++ ip;
  991. break;
  992. }
  993. case OP_CALLFUNC_RESOLVE:
  994. {
  995. StringTableEntry fnNamespace = U32toSTE(code[ip+1]);
  996. StringTableEntry fnName = U32toSTE(code[ip]);
  997. U32 callType = code[ip+2];
  998. Con::printf( "%i: OP_CALLFUNC_RESOLVE name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace,
  999. callType == FuncCallExprNode::FunctionCall ? "FunctionCall"
  1000. : callType == FuncCallExprNode::MethodCall ? "MethodCall" : "ParentCall" );
  1001. ip += 3;
  1002. break;
  1003. }
  1004. case OP_CALLFUNC:
  1005. {
  1006. StringTableEntry fnNamespace = U32toSTE(code[ip+1]);
  1007. StringTableEntry fnName = U32toSTE(code[ip]);
  1008. U32 callType = code[ip+2];
  1009. Con::printf( "%i: OP_CALLFUNC name=%s nspace=%s callType=%s", ip - 1, fnName, fnNamespace,
  1010. callType == FuncCallExprNode::FunctionCall ? "FunctionCall"
  1011. : callType == FuncCallExprNode::MethodCall ? "MethodCall" : "ParentCall" );
  1012. ip += 3;
  1013. break;
  1014. }
  1015. case OP_ADVANCE_STR:
  1016. {
  1017. Con::printf( "%i: OP_ADVANCE_STR", ip - 1 );
  1018. break;
  1019. }
  1020. case OP_ADVANCE_STR_APPENDCHAR:
  1021. {
  1022. char ch = code[ ip ];
  1023. Con::printf( "%i: OP_ADVANCE_STR_APPENDCHAR char=%c", ip - 1, ch );
  1024. ++ ip;
  1025. break;
  1026. }
  1027. case OP_ADVANCE_STR_COMMA:
  1028. {
  1029. Con::printf( "%i: OP_ADVANCE_STR_COMMA", ip - 1 );
  1030. break;
  1031. }
  1032. case OP_ADVANCE_STR_NUL:
  1033. {
  1034. Con::printf( "%i: OP_ADVANCE_STR_NUL", ip - 1 );
  1035. break;
  1036. }
  1037. case OP_REWIND_STR:
  1038. {
  1039. Con::printf( "%i: OP_REWIND_STR", ip - 1 );
  1040. break;
  1041. }
  1042. case OP_TERMINATE_REWIND_STR:
  1043. {
  1044. Con::printf( "%i: OP_TERMINATE_REWIND_STR", ip - 1 );
  1045. break;
  1046. }
  1047. case OP_COMPARE_STR:
  1048. {
  1049. Con::printf( "%i: OP_COMPARE_STR", ip - 1 );
  1050. break;
  1051. }
  1052. case OP_PUSH:
  1053. {
  1054. Con::printf( "%i: OP_PUSH", ip - 1 );
  1055. break;
  1056. }
  1057. case OP_PUSH_FRAME:
  1058. {
  1059. Con::printf( "%i: OP_PUSH_FRAME", ip - 1 );
  1060. break;
  1061. }
  1062. case OP_ASSERT:
  1063. {
  1064. const char* message = functionStrings + code[ ip ];
  1065. Con::printf( "%i: OP_ASSERT message=%s", ip - 1, message );
  1066. ++ ip;
  1067. break;
  1068. }
  1069. case OP_BREAK:
  1070. {
  1071. Con::printf( "%i: OP_BREAK", ip - 1 );
  1072. break;
  1073. }
  1074. case OP_ITER_BEGIN:
  1075. {
  1076. StringTableEntry varName = U32toSTE( code[ ip ] );
  1077. U32 failIp = code[ ip + 1 ];
  1078. Con::printf( "%i: OP_ITER_BEGIN varName=%s failIp=%i", varName, failIp );
  1079. ++ ip;
  1080. }
  1081. case OP_ITER_BEGIN_STR:
  1082. {
  1083. StringTableEntry varName = U32toSTE( code[ ip ] );
  1084. U32 failIp = code[ ip + 1 ];
  1085. Con::printf( "%i: OP_ITER_BEGIN varName=%s failIp=%i", varName, failIp );
  1086. ip += 2;
  1087. }
  1088. case OP_ITER:
  1089. {
  1090. U32 breakIp = code[ ip ];
  1091. Con::printf( "%i: OP_ITER breakIp=%i", breakIp );
  1092. ++ ip;
  1093. }
  1094. case OP_ITER_END:
  1095. {
  1096. Con::printf( "%i: OP_ITER_END", ip - 1 );
  1097. break;
  1098. }
  1099. default:
  1100. Con::printf( "%i: !!INVALID!!", ip - 1 );
  1101. break;
  1102. }
  1103. }
  1104. }