codeBlock.cpp 32 KB

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