astNodes.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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 "platform/platform.h"
  23. #include "console/console.h"
  24. #include "console/telnetDebugger.h"
  25. #include "platform/event.h"
  26. #include "console/ast.h"
  27. #include "core/tAlgorithm.h"
  28. #include "core/strings/findMatch.h"
  29. #include "console/consoleInternal.h"
  30. #include "core/stream/fileStream.h"
  31. #include "console/compiler.h"
  32. #include "console/simBase.h"
  33. template< typename T >
  34. struct Token
  35. {
  36. T value;
  37. S32 lineNumber;
  38. };
  39. #include "console/cmdgram.h"
  40. namespace Compiler
  41. {
  42. U32 precompileBlock(StmtNode *block, U32 loopCount)
  43. {
  44. U32 sum = 0;
  45. for(StmtNode *walk = block; walk; walk = walk->getNext())
  46. {
  47. const U32 temp = walk->precompileStmt(loopCount);
  48. #ifdef DEBUG_AST_NODES
  49. if(temp > 1000)
  50. Con::printf("suspect %s '%s' line %d", walk->dbgStmtType().c_str(), walk->dbgFileName, walk->dbgLineNumber);
  51. #endif
  52. sum += temp;
  53. }
  54. return sum;
  55. }
  56. U32 compileBlock(StmtNode *block, U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint)
  57. {
  58. for(StmtNode *walk = block; walk; walk = walk->getNext())
  59. ip = walk->compileStmt(codeStream, ip, continuePoint, breakPoint);
  60. return ip;
  61. }
  62. }
  63. using namespace Compiler;
  64. //-----------------------------------------------------------------------------
  65. void StmtNode::addBreakCount()
  66. {
  67. CodeBlock::smBreakLineCount++;
  68. }
  69. void StmtNode::addBreakLine(U32 ip)
  70. {
  71. U32 line = CodeBlock::smBreakLineCount * 2;
  72. CodeBlock::smBreakLineCount++;
  73. if(getBreakCodeBlock()->lineBreakPairs)
  74. {
  75. getBreakCodeBlock()->lineBreakPairs[line] = dbgLineNumber;
  76. getBreakCodeBlock()->lineBreakPairs[line+1] = ip;
  77. }
  78. }
  79. //------------------------------------------------------------
  80. StmtNode::StmtNode()
  81. {
  82. next = NULL;
  83. dbgFileName = CodeBlock::smCurrentParser->getCurrentFile();
  84. }
  85. void StmtNode::setPackage(StringTableEntry)
  86. {
  87. }
  88. void StmtNode::append(StmtNode *next)
  89. {
  90. StmtNode *walk = this;
  91. while(walk->next)
  92. walk = walk->next;
  93. walk->next = next;
  94. }
  95. void FunctionDeclStmtNode::setPackage(StringTableEntry packageName)
  96. {
  97. package = packageName;
  98. }
  99. //------------------------------------------------------------
  100. //
  101. // Console language compilers
  102. //
  103. //------------------------------------------------------------
  104. static U32 conversionOp(TypeReq src, TypeReq dst)
  105. {
  106. if(src == TypeReqString)
  107. {
  108. switch(dst)
  109. {
  110. case TypeReqUInt:
  111. return OP_STR_TO_UINT;
  112. case TypeReqFloat:
  113. return OP_STR_TO_FLT;
  114. case TypeReqNone:
  115. return OP_STR_TO_NONE;
  116. default:
  117. break;
  118. }
  119. }
  120. else if(src == TypeReqFloat)
  121. {
  122. switch(dst)
  123. {
  124. case TypeReqUInt:
  125. return OP_FLT_TO_UINT;
  126. case TypeReqString:
  127. return OP_FLT_TO_STR;
  128. case TypeReqNone:
  129. return OP_FLT_TO_NONE;
  130. default:
  131. break;
  132. }
  133. }
  134. else if(src == TypeReqUInt)
  135. {
  136. switch(dst)
  137. {
  138. case TypeReqFloat:
  139. return OP_UINT_TO_FLT;
  140. case TypeReqString:
  141. return OP_UINT_TO_STR;
  142. case TypeReqNone:
  143. return OP_UINT_TO_NONE;
  144. default:
  145. break;
  146. }
  147. }
  148. return OP_INVALID;
  149. }
  150. //------------------------------------------------------------
  151. U32 BreakStmtNode::precompileStmt(U32 loopCount)
  152. {
  153. if(loopCount)
  154. {
  155. addBreakCount();
  156. return 2;
  157. }
  158. Con::warnf(ConsoleLogEntry::General, "%s (%d): break outside of loop... ignoring.", dbgFileName, dbgLineNumber);
  159. return 0;
  160. }
  161. U32 BreakStmtNode::compileStmt(U32 *codeStream, U32 ip, U32, U32 breakPoint)
  162. {
  163. if(breakPoint)
  164. {
  165. addBreakLine(ip);
  166. codeStream[ip++] = OP_JMP;
  167. codeStream[ip++] = breakPoint;
  168. }
  169. return ip;
  170. }
  171. //------------------------------------------------------------
  172. U32 ContinueStmtNode::precompileStmt(U32 loopCount)
  173. {
  174. if(loopCount)
  175. {
  176. addBreakCount();
  177. return 2;
  178. }
  179. Con::warnf(ConsoleLogEntry::General, "%s (%d): continue outside of loop... ignoring.", dbgFileName, dbgLineNumber);
  180. return 0;
  181. }
  182. U32 ContinueStmtNode::compileStmt(U32 *codeStream, U32 ip, U32 continuePoint, U32)
  183. {
  184. if(continuePoint)
  185. {
  186. addBreakLine(ip);
  187. codeStream[ip++] = OP_JMP;
  188. codeStream[ip++] = continuePoint;
  189. }
  190. return ip;
  191. }
  192. //------------------------------------------------------------
  193. U32 ExprNode::precompileStmt(U32)
  194. {
  195. addBreakCount();
  196. return precompile(TypeReqNone);
  197. }
  198. U32 ExprNode::compileStmt(U32 *codeStream, U32 ip, U32, U32)
  199. {
  200. addBreakLine(ip);
  201. return compile(codeStream, ip, TypeReqNone);
  202. }
  203. //------------------------------------------------------------
  204. U32 ReturnStmtNode::precompileStmt(U32)
  205. {
  206. addBreakCount();
  207. return 1 + (expr ? expr->precompile(TypeReqString) : 0);
  208. }
  209. U32 ReturnStmtNode::compileStmt(U32 *codeStream, U32 ip, U32, U32)
  210. {
  211. addBreakLine(ip);
  212. if(!expr)
  213. codeStream[ip++] = OP_RETURN_VOID;
  214. else
  215. {
  216. ip = expr->compile(codeStream, ip, TypeReqString);
  217. codeStream[ip++] = OP_RETURN;
  218. }
  219. return ip;
  220. }
  221. //------------------------------------------------------------
  222. ExprNode *IfStmtNode::getSwitchOR(ExprNode *left, ExprNode *list, bool string)
  223. {
  224. ExprNode *nextExpr = (ExprNode *) list->getNext();
  225. ExprNode *test;
  226. if(string)
  227. test = StreqExprNode::alloc( left->dbgLineNumber, left, list, true );
  228. else
  229. test = IntBinaryExprNode::alloc( left->dbgLineNumber, opEQ, left, list );
  230. if(!nextExpr)
  231. return test;
  232. return IntBinaryExprNode::alloc( test->dbgLineNumber, opOR, test, getSwitchOR( left, nextExpr, string ) );
  233. }
  234. void IfStmtNode::propagateSwitchExpr(ExprNode *left, bool string)
  235. {
  236. testExpr = getSwitchOR(left, testExpr, string);
  237. if(propagate && elseBlock)
  238. ((IfStmtNode *) elseBlock)->propagateSwitchExpr(left, string);
  239. }
  240. U32 IfStmtNode::precompileStmt(U32 loopCount)
  241. {
  242. U32 exprSize;
  243. addBreakCount();
  244. if(testExpr->getPreferredType() == TypeReqUInt)
  245. {
  246. exprSize = testExpr->precompile(TypeReqUInt);
  247. integer = true;
  248. }
  249. else
  250. {
  251. exprSize = testExpr->precompile(TypeReqFloat);
  252. integer = false;
  253. }
  254. // next is the JMPIFNOT or JMPIFFNOT - size of 2
  255. U32 ifSize = precompileBlock(ifBlock, loopCount);
  256. if(!elseBlock)
  257. endifOffset = ifSize + 2 + exprSize;
  258. else
  259. {
  260. elseOffset = exprSize + 2 + ifSize + 2;
  261. U32 elseSize = precompileBlock(elseBlock, loopCount);
  262. endifOffset = elseOffset + elseSize;
  263. }
  264. return endifOffset;
  265. }
  266. U32 IfStmtNode::compileStmt(U32 *codeStream, U32 ip, U32 continuePoint, U32 breakPoint)
  267. {
  268. U32 start = ip;
  269. addBreakLine(ip);
  270. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  271. codeStream[ip++] = integer ? OP_JMPIFNOT : OP_JMPIFFNOT;
  272. if(elseBlock)
  273. {
  274. codeStream[ip++] = start + elseOffset;
  275. ip = compileBlock(ifBlock, codeStream, ip, continuePoint, breakPoint);
  276. codeStream[ip++] = OP_JMP;
  277. codeStream[ip++] = start + endifOffset;
  278. ip = compileBlock(elseBlock, codeStream, ip, continuePoint, breakPoint);
  279. }
  280. else
  281. {
  282. codeStream[ip++] = start + endifOffset;
  283. ip = compileBlock(ifBlock, codeStream, ip, continuePoint, breakPoint);
  284. }
  285. return ip;
  286. }
  287. //------------------------------------------------------------
  288. U32 LoopStmtNode::precompileStmt(U32 loopCount)
  289. {
  290. U32 initSize = 0;
  291. addBreakCount();
  292. if(initExpr)
  293. initSize = initExpr->precompile(TypeReqNone);
  294. U32 testSize;
  295. if(testExpr->getPreferredType() == TypeReqUInt)
  296. {
  297. integer = true;
  298. testSize = testExpr->precompile(TypeReqUInt);
  299. }
  300. else
  301. {
  302. integer = false;
  303. testSize = testExpr->precompile(TypeReqFloat);
  304. }
  305. U32 blockSize = precompileBlock(loopBlock, loopCount + 1);
  306. U32 endLoopSize = 0;
  307. if(endLoopExpr)
  308. endLoopSize = endLoopExpr->precompile(TypeReqNone);
  309. // if it's a for loop or a while loop it goes:
  310. // initExpr
  311. // testExpr
  312. // OP_JMPIFNOT to break point
  313. // loopStartPoint:
  314. // loopBlock
  315. // continuePoint:
  316. // endLoopExpr
  317. // testExpr
  318. // OP_JMPIF loopStartPoint
  319. // breakPoint:
  320. // otherwise if it's a do ... while() it goes:
  321. // initExpr
  322. // loopStartPoint:
  323. // loopBlock
  324. // continuePoint:
  325. // endLoopExpr
  326. // testExpr
  327. // OP_JMPIF loopStartPoint
  328. // breakPoint:
  329. if(!isDoLoop)
  330. {
  331. loopBlockStartOffset = initSize + testSize + 2;
  332. continueOffset = loopBlockStartOffset + blockSize;
  333. breakOffset = continueOffset + endLoopSize + testSize + 2;
  334. }
  335. else
  336. {
  337. loopBlockStartOffset = initSize;
  338. continueOffset = initSize + blockSize;
  339. breakOffset = continueOffset + endLoopSize + testSize + 2;
  340. }
  341. return breakOffset;
  342. }
  343. U32 LoopStmtNode::compileStmt(U32 *codeStream, U32 ip, U32, U32)
  344. {
  345. addBreakLine(ip);
  346. U32 start = ip;
  347. if(initExpr)
  348. ip = initExpr->compile(codeStream, ip, TypeReqNone);
  349. if(!isDoLoop)
  350. {
  351. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  352. codeStream[ip++] = integer ? OP_JMPIFNOT : OP_JMPIFFNOT;
  353. codeStream[ip++] = start + breakOffset;
  354. }
  355. // Compile internals of loop.
  356. ip = compileBlock(loopBlock, codeStream, ip, start + continueOffset, start + breakOffset);
  357. if(endLoopExpr)
  358. ip = endLoopExpr->compile(codeStream, ip, TypeReqNone);
  359. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  360. codeStream[ip++] = integer ? OP_JMPIF : OP_JMPIFF;
  361. codeStream[ip++] = start + loopBlockStartOffset;
  362. return ip;
  363. }
  364. //------------------------------------------------------------
  365. U32 IterStmtNode::precompileStmt( U32 loopCount )
  366. {
  367. addBreakCount();
  368. // Instruction sequence:
  369. //
  370. // containerExpr
  371. // OP_ITER_BEGIN varName .fail
  372. // .continue:
  373. // OP_ITER .break
  374. // body
  375. // OP_JMP .continue
  376. // .break:
  377. // OP_ITER_END
  378. // .fail:
  379. U32 exprSize = containerExpr->precompile( TypeReqString );
  380. bodySize = precompileBlock( body, loopCount + 1 );
  381. return
  382. exprSize
  383. + 3 // OP_ITER_BEGIN
  384. + 2 // OP_ITER
  385. + bodySize
  386. + 2 // OP_JMP
  387. + 1 // OP_ITER_END
  388. ;
  389. }
  390. U32 IterStmtNode::compileStmt( U32* codeStream, U32 ip, U32 continuePoint, U32 breakPoint )
  391. {
  392. addBreakLine( ip );
  393. const U32 startIp = ip;
  394. const U32 iterBeginIp = containerExpr->compile( codeStream, startIp, TypeReqString );
  395. const U32 continueIp = iterBeginIp + 3;
  396. const U32 bodyIp = continueIp + 2;
  397. const U32 jmpIp = bodyIp + bodySize;
  398. const U32 breakIp = jmpIp + 2;
  399. const U32 finalIp = breakIp + 1;
  400. codeStream[ iterBeginIp ] = isStringIter ? OP_ITER_BEGIN_STR : OP_ITER_BEGIN;
  401. codeStream[ iterBeginIp + 1 ] = STEtoU32( varName, iterBeginIp + 1 );
  402. codeStream[ iterBeginIp + 2 ] = finalIp;
  403. codeStream[ continueIp ] = OP_ITER;
  404. codeStream[ continueIp + 1 ] = breakIp;
  405. compileBlock( body, codeStream, bodyIp, continueIp, breakIp );
  406. codeStream[ jmpIp ] = OP_JMP;
  407. codeStream[ jmpIp + 1 ] = continueIp;
  408. codeStream[ breakIp ] = OP_ITER_END;
  409. return finalIp;
  410. }
  411. //------------------------------------------------------------
  412. U32 ConditionalExprNode::precompile(TypeReq type)
  413. {
  414. // code is testExpr
  415. // JMPIFNOT falseStart
  416. // trueExpr
  417. // JMP end
  418. // falseExpr
  419. U32 exprSize;
  420. if(testExpr->getPreferredType() == TypeReqUInt)
  421. {
  422. exprSize = testExpr->precompile(TypeReqUInt);
  423. integer = true;
  424. }
  425. else
  426. {
  427. exprSize = testExpr->precompile(TypeReqFloat);
  428. integer = false;
  429. }
  430. return exprSize +
  431. trueExpr->precompile(type) +
  432. falseExpr->precompile(type) + 4;
  433. }
  434. U32 ConditionalExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  435. {
  436. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  437. codeStream[ip++] = integer ? OP_JMPIFNOT : OP_JMPIFFNOT;
  438. U32 jumpElseIp = ip++;
  439. ip = trueExpr->compile(codeStream, ip, type);
  440. codeStream[ip++] = OP_JMP;
  441. U32 jumpEndIp = ip++;
  442. codeStream[jumpElseIp] = ip;
  443. ip = falseExpr->compile(codeStream, ip, type);
  444. codeStream[jumpEndIp] = ip;
  445. return ip;
  446. }
  447. TypeReq ConditionalExprNode::getPreferredType()
  448. {
  449. return trueExpr->getPreferredType();
  450. }
  451. //------------------------------------------------------------
  452. U32 FloatBinaryExprNode::precompile(TypeReq type)
  453. {
  454. U32 addSize = left->precompile(TypeReqFloat) + right->precompile(TypeReqFloat) + 1;
  455. if(type != TypeReqFloat)
  456. addSize++;
  457. return addSize;
  458. }
  459. U32 FloatBinaryExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  460. {
  461. ip = right->compile(codeStream, ip, TypeReqFloat);
  462. ip = left->compile(codeStream, ip, TypeReqFloat);
  463. U32 operand = OP_INVALID;
  464. switch(op)
  465. {
  466. case '+':
  467. operand = OP_ADD;
  468. break;
  469. case '-':
  470. operand = OP_SUB;
  471. break;
  472. case '/':
  473. operand = OP_DIV;
  474. break;
  475. case '*':
  476. operand = OP_MUL;
  477. break;
  478. }
  479. codeStream[ip++] = operand;
  480. if(type != TypeReqFloat)
  481. codeStream[ip++] =conversionOp(TypeReqFloat, type);
  482. return ip;
  483. }
  484. TypeReq FloatBinaryExprNode::getPreferredType()
  485. {
  486. return TypeReqFloat;
  487. }
  488. //------------------------------------------------------------
  489. void IntBinaryExprNode::getSubTypeOperand()
  490. {
  491. subType = TypeReqUInt;
  492. switch(op)
  493. {
  494. case '^':
  495. operand = OP_XOR;
  496. break;
  497. case '%':
  498. operand = OP_MOD;
  499. break;
  500. case '&':
  501. operand = OP_BITAND;
  502. break;
  503. case '|':
  504. operand = OP_BITOR;
  505. break;
  506. case '<':
  507. operand = OP_CMPLT;
  508. subType = TypeReqFloat;
  509. break;
  510. case '>':
  511. operand = OP_CMPGR;
  512. subType = TypeReqFloat;
  513. break;
  514. case opGE:
  515. operand = OP_CMPGE;
  516. subType = TypeReqFloat;
  517. break;
  518. case opLE:
  519. operand = OP_CMPLE;
  520. subType = TypeReqFloat;
  521. break;
  522. case opEQ:
  523. operand = OP_CMPEQ;
  524. subType = TypeReqFloat;
  525. break;
  526. case opNE:
  527. operand = OP_CMPNE;
  528. subType = TypeReqFloat;
  529. break;
  530. case opOR:
  531. operand = OP_OR;
  532. break;
  533. case opAND:
  534. operand = OP_AND;
  535. break;
  536. case opSHR:
  537. operand = OP_SHR;
  538. break;
  539. case opSHL:
  540. operand = OP_SHL;
  541. break;
  542. }
  543. }
  544. U32 IntBinaryExprNode::precompile(TypeReq type)
  545. {
  546. getSubTypeOperand();
  547. U32 addSize = left->precompile(subType) + right->precompile(subType) + 1;
  548. if(operand == OP_OR || operand == OP_AND)
  549. addSize++;
  550. if(type != TypeReqUInt)
  551. addSize++;
  552. return addSize;
  553. }
  554. U32 IntBinaryExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  555. {
  556. if(operand == OP_OR || operand == OP_AND)
  557. {
  558. ip = left->compile(codeStream, ip, subType);
  559. codeStream[ip++] = operand == OP_OR ? OP_JMPIF_NP : OP_JMPIFNOT_NP;
  560. U32 jmpIp = ip++;
  561. ip = right->compile(codeStream, ip, subType);
  562. codeStream[jmpIp] = ip;
  563. }
  564. else
  565. {
  566. ip = right->compile(codeStream, ip, subType);
  567. ip = left->compile(codeStream, ip, subType);
  568. codeStream[ip++] = operand;
  569. }
  570. if(type != TypeReqUInt)
  571. codeStream[ip++] =conversionOp(TypeReqUInt, type);
  572. return ip;
  573. }
  574. TypeReq IntBinaryExprNode::getPreferredType()
  575. {
  576. return TypeReqUInt;
  577. }
  578. //------------------------------------------------------------
  579. U32 StreqExprNode::precompile(TypeReq type)
  580. {
  581. // eval str left
  582. // OP_ADVANCE_STR_NUL
  583. // eval str right
  584. // OP_COMPARE_STR
  585. // optional conversion
  586. U32 addSize = left->precompile(TypeReqString) + right->precompile(TypeReqString) + 2;
  587. if(!eq)
  588. addSize ++;
  589. if(type != TypeReqUInt)
  590. addSize ++;
  591. return addSize;
  592. }
  593. U32 StreqExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  594. {
  595. ip = left->compile(codeStream, ip, TypeReqString);
  596. codeStream[ip++] = OP_ADVANCE_STR_NUL;
  597. ip = right->compile(codeStream, ip, TypeReqString);
  598. codeStream[ip++] = OP_COMPARE_STR;
  599. if(!eq)
  600. codeStream[ip++] = OP_NOT;
  601. if(type != TypeReqUInt)
  602. codeStream[ip++] = conversionOp(TypeReqUInt, type);
  603. return ip;
  604. }
  605. TypeReq StreqExprNode::getPreferredType()
  606. {
  607. return TypeReqUInt;
  608. }
  609. //------------------------------------------------------------
  610. U32 StrcatExprNode::precompile(TypeReq type)
  611. {
  612. U32 addSize = left->precompile(TypeReqString) + right->precompile(TypeReqString) + 2;
  613. if(appendChar)
  614. addSize++;
  615. if(type != TypeReqString)
  616. addSize ++;
  617. return addSize;
  618. }
  619. U32 StrcatExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  620. {
  621. ip = left->compile(codeStream, ip, TypeReqString);
  622. if(!appendChar)
  623. codeStream[ip++] = OP_ADVANCE_STR;
  624. else
  625. {
  626. codeStream[ip++] = OP_ADVANCE_STR_APPENDCHAR;
  627. codeStream[ip++] = appendChar;
  628. }
  629. ip = right->compile(codeStream, ip, TypeReqString);
  630. codeStream[ip++] = OP_REWIND_STR;
  631. if(type == TypeReqUInt)
  632. codeStream[ip++] = OP_STR_TO_UINT;
  633. else if(type == TypeReqFloat)
  634. codeStream[ip++] = OP_STR_TO_FLT;
  635. return ip;
  636. }
  637. TypeReq StrcatExprNode::getPreferredType()
  638. {
  639. return TypeReqString;
  640. }
  641. //------------------------------------------------------------
  642. U32 CommaCatExprNode::precompile(TypeReq type)
  643. {
  644. U32 addSize = left->precompile(TypeReqString) + right->precompile(TypeReqString) + 2;
  645. if(type != TypeReqString)
  646. addSize ++;
  647. return addSize;
  648. }
  649. U32 CommaCatExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  650. {
  651. ip = left->compile(codeStream, ip, TypeReqString);
  652. codeStream[ip++] = OP_ADVANCE_STR_COMMA;
  653. ip = right->compile(codeStream, ip, TypeReqString);
  654. codeStream[ip++] = OP_REWIND_STR;
  655. // At this point the stack has the concatenated string.
  656. // But we're paranoid, so accept (but whine) if we get an oddity...
  657. if(type == TypeReqUInt || type == TypeReqFloat)
  658. Con::warnf(ConsoleLogEntry::General, "%s (%d): converting comma string to a number... probably wrong.", dbgFileName, dbgLineNumber);
  659. if(type == TypeReqUInt)
  660. codeStream[ip++] = OP_STR_TO_UINT;
  661. else if(type == TypeReqFloat)
  662. codeStream[ip++] = OP_STR_TO_FLT;
  663. return ip;
  664. }
  665. TypeReq CommaCatExprNode::getPreferredType()
  666. {
  667. return TypeReqString;
  668. }
  669. //------------------------------------------------------------
  670. U32 IntUnaryExprNode::precompile(TypeReq type)
  671. {
  672. integer = true;
  673. TypeReq prefType = expr->getPreferredType();
  674. if(op == '!' && (prefType == TypeReqFloat || prefType == TypeReqString))
  675. integer = false;
  676. U32 exprSize = expr->precompile(integer ? TypeReqUInt : TypeReqFloat);
  677. if(type != TypeReqUInt)
  678. return exprSize + 2;
  679. else
  680. return exprSize + 1;
  681. }
  682. U32 IntUnaryExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  683. {
  684. ip = expr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  685. if(op == '!')
  686. codeStream[ip++] = integer ? OP_NOT : OP_NOTF;
  687. else if(op == '~')
  688. codeStream[ip++] = OP_ONESCOMPLEMENT;
  689. if(type != TypeReqUInt)
  690. codeStream[ip++] =conversionOp(TypeReqUInt, type);
  691. return ip;
  692. }
  693. TypeReq IntUnaryExprNode::getPreferredType()
  694. {
  695. return TypeReqUInt;
  696. }
  697. //------------------------------------------------------------
  698. U32 FloatUnaryExprNode::precompile(TypeReq type)
  699. {
  700. U32 exprSize = expr->precompile(TypeReqFloat);
  701. if(type != TypeReqFloat)
  702. return exprSize + 2;
  703. else
  704. return exprSize + 1;
  705. }
  706. U32 FloatUnaryExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  707. {
  708. ip = expr->compile(codeStream, ip, TypeReqFloat);
  709. codeStream[ip++] = OP_NEG;
  710. if(type != TypeReqFloat)
  711. codeStream[ip++] =conversionOp(TypeReqFloat, type);
  712. return ip;
  713. }
  714. TypeReq FloatUnaryExprNode::getPreferredType()
  715. {
  716. return TypeReqFloat;
  717. }
  718. //------------------------------------------------------------
  719. U32 VarNode::precompile(TypeReq type)
  720. {
  721. // if this has an arrayIndex...
  722. // OP_LOADIMMED_IDENT
  723. // varName
  724. // OP_ADVANCE_STR
  725. // evaluate arrayIndex TypeReqString
  726. // OP_REWIND_STR
  727. // OP_SETCURVAR_ARRAY
  728. // OP_LOADVAR (type)
  729. // else
  730. // OP_SETCURVAR
  731. // varName
  732. // OP_LOADVAR (type)
  733. if(type == TypeReqNone)
  734. return 0;
  735. precompileIdent(varName);
  736. return (arrayIndex ? arrayIndex->precompile(TypeReqString) + 6 : 3);
  737. }
  738. U32 VarNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  739. {
  740. if(type == TypeReqNone)
  741. return ip;
  742. codeStream[ip++] = arrayIndex ? OP_LOADIMMED_IDENT : OP_SETCURVAR;
  743. codeStream[ip] = STEtoU32(varName, ip);
  744. ip++;
  745. if(arrayIndex)
  746. {
  747. codeStream[ip++] = OP_ADVANCE_STR;
  748. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  749. codeStream[ip++] = OP_REWIND_STR;
  750. codeStream[ip++] = OP_SETCURVAR_ARRAY;
  751. }
  752. switch(type)
  753. {
  754. case TypeReqUInt:
  755. codeStream[ip++] = OP_LOADVAR_UINT;
  756. break;
  757. case TypeReqFloat:
  758. codeStream[ip++] = OP_LOADVAR_FLT;
  759. break;
  760. case TypeReqString:
  761. codeStream[ip++] = OP_LOADVAR_STR;
  762. break;
  763. case TypeReqNone:
  764. break;
  765. }
  766. return ip;
  767. }
  768. TypeReq VarNode::getPreferredType()
  769. {
  770. return TypeReqNone; // no preferred type
  771. }
  772. //------------------------------------------------------------
  773. U32 IntNode::precompile(TypeReq type)
  774. {
  775. if(type == TypeReqNone)
  776. return 0;
  777. if(type == TypeReqString)
  778. index = getCurrentStringTable()->addIntString(value);
  779. else if(type == TypeReqFloat)
  780. index = getCurrentFloatTable()->add(value);
  781. return 2;
  782. }
  783. U32 IntNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  784. {
  785. switch(type)
  786. {
  787. case TypeReqUInt:
  788. codeStream[ip++] = OP_LOADIMMED_UINT;
  789. codeStream[ip++] = value;
  790. break;
  791. case TypeReqString:
  792. codeStream[ip++] = OP_LOADIMMED_STR;
  793. codeStream[ip++] = index;
  794. break;
  795. case TypeReqFloat:
  796. codeStream[ip++] = OP_LOADIMMED_FLT;
  797. codeStream[ip++] = index;
  798. break;
  799. case TypeReqNone:
  800. break;
  801. }
  802. return ip;
  803. }
  804. TypeReq IntNode::getPreferredType()
  805. {
  806. return TypeReqUInt;
  807. }
  808. //------------------------------------------------------------
  809. U32 FloatNode::precompile(TypeReq type)
  810. {
  811. if(type == TypeReqNone)
  812. return 0;
  813. if(type == TypeReqString)
  814. index = getCurrentStringTable()->addFloatString(value);
  815. else if(type == TypeReqFloat)
  816. index = getCurrentFloatTable()->add(value);
  817. return 2;
  818. }
  819. U32 FloatNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  820. {
  821. switch(type)
  822. {
  823. case TypeReqUInt:
  824. codeStream[ip++] = OP_LOADIMMED_UINT;
  825. codeStream[ip++] = U32(value);
  826. break;
  827. case TypeReqString:
  828. codeStream[ip++] = OP_LOADIMMED_STR;
  829. codeStream[ip++] = index;
  830. break;
  831. case TypeReqFloat:
  832. codeStream[ip++] = OP_LOADIMMED_FLT;
  833. codeStream[ip++] = index;
  834. break;
  835. case TypeReqNone:
  836. break;
  837. }
  838. return ip;
  839. }
  840. TypeReq FloatNode::getPreferredType()
  841. {
  842. return TypeReqFloat;
  843. }
  844. //------------------------------------------------------------
  845. U32 StrConstNode::precompile(TypeReq type)
  846. {
  847. // Early out for documentation block.
  848. if( doc )
  849. {
  850. index = getCurrentStringTable()->add(str, true, tag);
  851. return 2;
  852. }
  853. if(type == TypeReqString)
  854. {
  855. index = getCurrentStringTable()->add(str, true, tag);
  856. return 2;
  857. }
  858. else if(type == TypeReqNone)
  859. {
  860. return 0;
  861. }
  862. fVal = consoleStringToNumber(str, dbgFileName, dbgLineNumber);
  863. if(type == TypeReqFloat)
  864. index = getCurrentFloatTable()->add(fVal);
  865. return 2;
  866. }
  867. U32 StrConstNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  868. {
  869. // If this is a DOCBLOCK, then process w/ appropriate op...
  870. if( doc )
  871. {
  872. codeStream[ip++] = OP_DOCBLOCK_STR;
  873. codeStream[ip++] = index;
  874. return ip;
  875. }
  876. // Otherwise, deal with it normally as a string literal case.
  877. switch(type)
  878. {
  879. case TypeReqString:
  880. codeStream[ip++] = tag ? OP_TAG_TO_STR : OP_LOADIMMED_STR;
  881. codeStream[ip++] = index;
  882. break;
  883. case TypeReqUInt:
  884. codeStream[ip++] = OP_LOADIMMED_UINT;
  885. codeStream[ip++] = U32(fVal);
  886. break;
  887. case TypeReqFloat:
  888. codeStream[ip++] = OP_LOADIMMED_FLT;
  889. codeStream[ip++] = index;
  890. break;
  891. case TypeReqNone:
  892. break;
  893. }
  894. return ip;
  895. }
  896. TypeReq StrConstNode::getPreferredType()
  897. {
  898. return TypeReqString;
  899. }
  900. //------------------------------------------------------------
  901. U32 ConstantNode::precompile(TypeReq type)
  902. {
  903. if(type == TypeReqString)
  904. {
  905. precompileIdent(value);
  906. return 2;
  907. }
  908. else if(type == TypeReqNone)
  909. return 0;
  910. fVal = consoleStringToNumber(value, dbgFileName, dbgLineNumber);
  911. if(type == TypeReqFloat)
  912. index = getCurrentFloatTable()->add(fVal);
  913. return 2;
  914. }
  915. U32 ConstantNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  916. {
  917. switch(type)
  918. {
  919. case TypeReqString:
  920. codeStream[ip++] = OP_LOADIMMED_IDENT;
  921. codeStream[ip] = STEtoU32(value, ip);
  922. ip++;
  923. break;
  924. case TypeReqUInt:
  925. codeStream[ip++] = OP_LOADIMMED_UINT;
  926. codeStream[ip++] = U32(fVal);
  927. break;
  928. case TypeReqFloat:
  929. codeStream[ip++] = OP_LOADIMMED_FLT;
  930. codeStream[ip++] = index;
  931. break;
  932. case TypeReqNone:
  933. break;
  934. }
  935. return ip;
  936. }
  937. TypeReq ConstantNode::getPreferredType()
  938. {
  939. return TypeReqString;
  940. }
  941. //------------------------------------------------------------
  942. U32 AssignExprNode::precompile(TypeReq type)
  943. {
  944. subType = expr->getPreferredType();
  945. if(subType == TypeReqNone)
  946. subType = type;
  947. if(subType == TypeReqNone)
  948. subType = TypeReqString;
  949. // if it's an array expr, the formula is:
  950. // eval expr
  951. // (push and pop if it's TypeReqString) OP_ADVANCE_STR
  952. // OP_LOADIMMED_IDENT
  953. // varName
  954. // OP_ADVANCE_STR
  955. // eval array
  956. // OP_REWIND_STR
  957. // OP_SETCURVAR_ARRAY_CREATE
  958. // OP_TERMINATE_REWIND_STR
  959. // OP_SAVEVAR
  960. //else
  961. // eval expr
  962. // OP_SETCURVAR_CREATE
  963. // varname
  964. // OP_SAVEVAR
  965. const U32 addSize = (type != subType ? 1 : 0);
  966. const U32 retSize = expr->precompile(subType);
  967. #ifdef DEBUG_AST_NODES
  968. if(retSize > 1000)
  969. Con::printf("Bad expr %s", expr->dbgStmtType().c_str());
  970. #endif
  971. precompileIdent(varName);
  972. return retSize + addSize + (arrayIndex ? arrayIndex->precompile(TypeReqString) + (subType == TypeReqString ? 8 : 6 ) : 3);
  973. }
  974. U32 AssignExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  975. {
  976. ip = expr->compile(codeStream, ip, subType);
  977. if(arrayIndex)
  978. {
  979. if(subType == TypeReqString)
  980. codeStream[ip++] = OP_ADVANCE_STR;
  981. codeStream[ip++] = OP_LOADIMMED_IDENT;
  982. codeStream[ip] = STEtoU32(varName, ip);
  983. ip++;
  984. codeStream[ip++] = OP_ADVANCE_STR;
  985. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  986. codeStream[ip++] = OP_REWIND_STR;
  987. codeStream[ip++] = OP_SETCURVAR_ARRAY_CREATE;
  988. if(subType == TypeReqString)
  989. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  990. }
  991. else
  992. {
  993. codeStream[ip++] = OP_SETCURVAR_CREATE;
  994. codeStream[ip] = STEtoU32(varName, ip);
  995. ip++;
  996. }
  997. switch(subType)
  998. {
  999. case TypeReqString:
  1000. codeStream[ip++] = OP_SAVEVAR_STR;
  1001. break;
  1002. case TypeReqUInt:
  1003. codeStream[ip++] = OP_SAVEVAR_UINT;
  1004. break;
  1005. case TypeReqFloat:
  1006. codeStream[ip++] = OP_SAVEVAR_FLT;
  1007. break;
  1008. case TypeReqNone:
  1009. break;
  1010. }
  1011. if(type != subType)
  1012. codeStream[ip++] = conversionOp(subType, type);
  1013. return ip;
  1014. }
  1015. TypeReq AssignExprNode::getPreferredType()
  1016. {
  1017. return expr->getPreferredType();
  1018. }
  1019. //------------------------------------------------------------
  1020. static void getAssignOpTypeOp(S32 op, TypeReq &type, U32 &operand)
  1021. {
  1022. switch(op)
  1023. {
  1024. case '+':
  1025. type = TypeReqFloat;
  1026. operand = OP_ADD;
  1027. break;
  1028. case '-':
  1029. type = TypeReqFloat;
  1030. operand = OP_SUB;
  1031. break;
  1032. case '*':
  1033. type = TypeReqFloat;
  1034. operand = OP_MUL;
  1035. break;
  1036. case '/':
  1037. type = TypeReqFloat;
  1038. operand = OP_DIV;
  1039. break;
  1040. case '%':
  1041. type = TypeReqUInt;
  1042. operand = OP_MOD;
  1043. break;
  1044. case '&':
  1045. type = TypeReqUInt;
  1046. operand = OP_BITAND;
  1047. break;
  1048. case '^':
  1049. type = TypeReqUInt;
  1050. operand = OP_XOR;
  1051. break;
  1052. case '|':
  1053. type = TypeReqUInt;
  1054. operand = OP_BITOR;
  1055. break;
  1056. case opSHL:
  1057. type = TypeReqUInt;
  1058. operand = OP_SHL;
  1059. break;
  1060. case opSHR:
  1061. type = TypeReqUInt;
  1062. operand = OP_SHR;
  1063. break;
  1064. }
  1065. }
  1066. U32 AssignOpExprNode::precompile(TypeReq type)
  1067. {
  1068. // goes like this...
  1069. // eval expr as float or int
  1070. // if there's an arrayIndex
  1071. // OP_LOADIMMED_IDENT
  1072. // varName
  1073. // OP_ADVANCE_STR
  1074. // eval arrayIndex stringwise
  1075. // OP_REWIND_STR
  1076. // OP_SETCURVAR_ARRAY_CREATE
  1077. // else
  1078. // OP_SETCURVAR_CREATE
  1079. // varName
  1080. // OP_LOADVAR_FLT or UINT
  1081. // operand
  1082. // OP_SAVEVAR_FLT or UINT
  1083. // conversion OP if necessary.
  1084. getAssignOpTypeOp(op, subType, operand);
  1085. precompileIdent(varName);
  1086. U32 size = expr->precompile(subType);
  1087. if(type != subType)
  1088. size++;
  1089. if(!arrayIndex)
  1090. return size + 5;
  1091. else
  1092. {
  1093. size += arrayIndex->precompile(TypeReqString);
  1094. return size + 8;
  1095. }
  1096. }
  1097. U32 AssignOpExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1098. {
  1099. ip = expr->compile(codeStream, ip, subType);
  1100. if(!arrayIndex)
  1101. {
  1102. codeStream[ip++] = OP_SETCURVAR_CREATE;
  1103. codeStream[ip] = STEtoU32(varName, ip);
  1104. ip++;
  1105. }
  1106. else
  1107. {
  1108. codeStream[ip++] = OP_LOADIMMED_IDENT;
  1109. codeStream[ip] = STEtoU32(varName, ip);
  1110. ip++;
  1111. codeStream[ip++] = OP_ADVANCE_STR;
  1112. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  1113. codeStream[ip++] = OP_REWIND_STR;
  1114. codeStream[ip++] = OP_SETCURVAR_ARRAY_CREATE;
  1115. }
  1116. codeStream[ip++] = (subType == TypeReqFloat) ? OP_LOADVAR_FLT : OP_LOADVAR_UINT;
  1117. codeStream[ip++] = operand;
  1118. codeStream[ip++] = (subType == TypeReqFloat) ? OP_SAVEVAR_FLT : OP_SAVEVAR_UINT;
  1119. if(subType != type)
  1120. codeStream[ip++] = conversionOp(subType, type);
  1121. return ip;
  1122. }
  1123. TypeReq AssignOpExprNode::getPreferredType()
  1124. {
  1125. getAssignOpTypeOp(op, subType, operand);
  1126. return subType;
  1127. }
  1128. //------------------------------------------------------------
  1129. U32 TTagSetStmtNode::precompileStmt(U32 loopCount)
  1130. {
  1131. TORQUE_UNUSED(loopCount);
  1132. return 0;
  1133. }
  1134. U32 TTagSetStmtNode::compileStmt(U32*, U32 ip, U32, U32)
  1135. {
  1136. return ip;
  1137. }
  1138. //------------------------------------------------------------
  1139. U32 TTagDerefNode::precompile(TypeReq)
  1140. {
  1141. return 0;
  1142. }
  1143. U32 TTagDerefNode::compile(U32*, U32 ip, TypeReq)
  1144. {
  1145. return ip;
  1146. }
  1147. TypeReq TTagDerefNode::getPreferredType()
  1148. {
  1149. return TypeReqNone;
  1150. }
  1151. //------------------------------------------------------------
  1152. U32 TTagExprNode::precompile(TypeReq)
  1153. {
  1154. return 0;
  1155. }
  1156. U32 TTagExprNode::compile(U32*, U32 ip, TypeReq)
  1157. {
  1158. return ip;
  1159. }
  1160. TypeReq TTagExprNode::getPreferredType()
  1161. {
  1162. return TypeReqNone;
  1163. }
  1164. //------------------------------------------------------------
  1165. U32 FuncCallExprNode::precompile(TypeReq type)
  1166. {
  1167. // OP_PUSH_FRAME
  1168. // arg OP_PUSH arg OP_PUSH arg OP_PUSH
  1169. // eval all the args, then call the function.
  1170. // OP_CALLFUNC
  1171. // function
  1172. // namespace
  1173. // isDot
  1174. U32 size = 0;
  1175. if(type != TypeReqString)
  1176. size++;
  1177. precompileIdent(funcName);
  1178. precompileIdent(nameSpace);
  1179. for(ExprNode *walk = args; walk; walk = (ExprNode *) walk->getNext())
  1180. size += walk->precompile(TypeReqString) + 1;
  1181. return size + 5;
  1182. }
  1183. U32 FuncCallExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1184. {
  1185. codeStream[ip++] = OP_PUSH_FRAME;
  1186. for(ExprNode *walk = args; walk; walk = (ExprNode *) walk->getNext())
  1187. {
  1188. ip = walk->compile(codeStream, ip, TypeReqString);
  1189. codeStream[ip++] = OP_PUSH;
  1190. }
  1191. if(callType == MethodCall || callType == ParentCall)
  1192. codeStream[ip++] = OP_CALLFUNC;
  1193. else
  1194. codeStream[ip++] = OP_CALLFUNC_RESOLVE;
  1195. codeStream[ip] = STEtoU32(funcName, ip);
  1196. ip++;
  1197. codeStream[ip] = STEtoU32(nameSpace, ip);
  1198. ip++;
  1199. codeStream[ip++] = callType;
  1200. if(type != TypeReqString)
  1201. codeStream[ip++] = conversionOp(TypeReqString, type);
  1202. return ip;
  1203. }
  1204. TypeReq FuncCallExprNode::getPreferredType()
  1205. {
  1206. return TypeReqString;
  1207. }
  1208. //------------------------------------------------------------
  1209. U32 AssertCallExprNode::precompile( TypeReq type )
  1210. {
  1211. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1212. messageIndex = getCurrentStringTable()->add( message, true, false );
  1213. U32 exprSize = testExpr->precompile(TypeReqUInt);
  1214. return exprSize + 2;
  1215. #else
  1216. return 0;
  1217. #endif
  1218. }
  1219. U32 AssertCallExprNode::compile( U32 *codeStream, U32 ip, TypeReq type )
  1220. {
  1221. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1222. ip = testExpr->compile( codeStream, ip, TypeReqUInt );
  1223. codeStream[ip++] = OP_ASSERT;
  1224. codeStream[ip++] = messageIndex;
  1225. #endif
  1226. return ip;
  1227. }
  1228. TypeReq AssertCallExprNode::getPreferredType()
  1229. {
  1230. return TypeReqNone;
  1231. }
  1232. //------------------------------------------------------------
  1233. U32 SlotAccessNode::precompile(TypeReq type)
  1234. {
  1235. if(type == TypeReqNone)
  1236. return 0;
  1237. U32 size = 0;
  1238. precompileIdent(slotName);
  1239. if(arrayExpr)
  1240. {
  1241. // eval array
  1242. // OP_ADVANCE_STR
  1243. // evaluate object expression sub (OP_SETCURFIELD)
  1244. // OP_TERMINATE_REWIND_STR
  1245. // OP_SETCURFIELDARRAY
  1246. // total add of 4 + array precomp
  1247. size += 3 + arrayExpr->precompile(TypeReqString);
  1248. }
  1249. // eval object expression sub + 3 (op_setCurField + OP_SETCUROBJECT)
  1250. size += objectExpr->precompile(TypeReqString) + 3;
  1251. // get field in desired type:
  1252. return size + 1;
  1253. }
  1254. U32 SlotAccessNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1255. {
  1256. if(type == TypeReqNone)
  1257. return ip;
  1258. if(arrayExpr)
  1259. {
  1260. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1261. codeStream[ip++] = OP_ADVANCE_STR;
  1262. }
  1263. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1264. codeStream[ip++] = OP_SETCUROBJECT;
  1265. codeStream[ip++] = OP_SETCURFIELD;
  1266. codeStream[ip] = STEtoU32(slotName, ip);
  1267. ip++;
  1268. if(arrayExpr)
  1269. {
  1270. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1271. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1272. }
  1273. switch(type)
  1274. {
  1275. case TypeReqUInt:
  1276. codeStream[ip++] = OP_LOADFIELD_UINT;
  1277. break;
  1278. case TypeReqFloat:
  1279. codeStream[ip++] = OP_LOADFIELD_FLT;
  1280. break;
  1281. case TypeReqString:
  1282. codeStream[ip++] = OP_LOADFIELD_STR;
  1283. break;
  1284. case TypeReqNone:
  1285. break;
  1286. }
  1287. return ip;
  1288. }
  1289. TypeReq SlotAccessNode::getPreferredType()
  1290. {
  1291. return TypeReqNone;
  1292. }
  1293. //-----------------------------------------------------------------------------
  1294. U32 InternalSlotAccessNode::precompile(TypeReq type)
  1295. {
  1296. if(type == TypeReqNone)
  1297. return 0;
  1298. U32 size = 3;
  1299. // eval object expression sub + 3 (op_setCurField + OP_SETCUROBJECT)
  1300. size += objectExpr->precompile(TypeReqString);
  1301. size += slotExpr->precompile(TypeReqString);
  1302. if(type != TypeReqUInt)
  1303. size++;
  1304. // get field in desired type:
  1305. return size;
  1306. }
  1307. U32 InternalSlotAccessNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1308. {
  1309. if(type == TypeReqNone)
  1310. return ip;
  1311. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1312. codeStream[ip++] = OP_SETCUROBJECT;
  1313. ip = slotExpr->compile(codeStream, ip, TypeReqString);
  1314. codeStream[ip++] = OP_SETCUROBJECT_INTERNAL;
  1315. codeStream[ip++] = recurse;
  1316. if(type != TypeReqUInt)
  1317. codeStream[ip++] = conversionOp(TypeReqUInt, type);
  1318. return ip;
  1319. }
  1320. TypeReq InternalSlotAccessNode::getPreferredType()
  1321. {
  1322. return TypeReqUInt;
  1323. }
  1324. //-----------------------------------------------------------------------------
  1325. //------------------------------------------------------------
  1326. U32 SlotAssignNode::precompile(TypeReq type)
  1327. {
  1328. // first eval the expression TypeReqString
  1329. // if it's an array:
  1330. // if OP_ADVANCE_STR 1
  1331. // eval array
  1332. // OP_ADVANCE_STR 1
  1333. // evaluate object expr
  1334. // OP_SETCUROBJECT 1
  1335. // OP_SETCURFIELD 1
  1336. // fieldName 1
  1337. // OP_TERMINATE_REWIND_STR 1
  1338. // OP_SETCURFIELDARRAY 1
  1339. // OP_TERMINATE_REWIND_STR 1
  1340. // else
  1341. // OP_ADVANCE_STR
  1342. // evaluate object expr
  1343. // OP_SETCUROBJECT
  1344. // OP_SETCURFIELD
  1345. // fieldName
  1346. // OP_TERMINATE_REWIND_STR
  1347. // OP_SAVEFIELD
  1348. // convert to return type if necessary.
  1349. U32 size = 0;
  1350. if(type != TypeReqString)
  1351. size++;
  1352. precompileIdent(slotName);
  1353. size += valueExpr->precompile(TypeReqString);
  1354. if(objectExpr)
  1355. size += objectExpr->precompile(TypeReqString) + 5;
  1356. else
  1357. size += 5;
  1358. if(arrayExpr)
  1359. size += arrayExpr->precompile(TypeReqString) + 3;
  1360. if(typeID != -1)
  1361. size += 2;
  1362. return size + 1;
  1363. }
  1364. U32 SlotAssignNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1365. {
  1366. ip = valueExpr->compile(codeStream, ip, TypeReqString);
  1367. codeStream[ip++] = OP_ADVANCE_STR;
  1368. if(arrayExpr)
  1369. {
  1370. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1371. codeStream[ip++] = OP_ADVANCE_STR;
  1372. }
  1373. if(objectExpr)
  1374. {
  1375. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1376. codeStream[ip++] = OP_SETCUROBJECT;
  1377. }
  1378. else
  1379. codeStream[ip++] = OP_SETCUROBJECT_NEW;
  1380. codeStream[ip++] = OP_SETCURFIELD;
  1381. codeStream[ip] = STEtoU32(slotName, ip);
  1382. ip++;
  1383. if(arrayExpr)
  1384. {
  1385. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1386. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1387. }
  1388. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1389. codeStream[ip++] = OP_SAVEFIELD_STR;
  1390. if(typeID != -1)
  1391. {
  1392. codeStream[ip++] = OP_SETCURFIELD_TYPE;
  1393. codeStream[ip++] = typeID;
  1394. }
  1395. if(type != TypeReqString)
  1396. codeStream[ip++] = conversionOp(TypeReqString, type);
  1397. return ip;
  1398. }
  1399. TypeReq SlotAssignNode::getPreferredType()
  1400. {
  1401. return TypeReqString;
  1402. }
  1403. //------------------------------------------------------------
  1404. U32 SlotAssignOpNode::precompile(TypeReq type)
  1405. {
  1406. // first eval the expression as its type
  1407. // if it's an array:
  1408. // eval array
  1409. // OP_ADVANCE_STR
  1410. // evaluate object expr
  1411. // OP_SETCUROBJECT
  1412. // OP_SETCURFIELD
  1413. // fieldName
  1414. // OP_TERMINATE_REWIND_STR
  1415. // OP_SETCURFIELDARRAY
  1416. // else
  1417. // evaluate object expr
  1418. // OP_SETCUROBJECT
  1419. // OP_SETCURFIELD
  1420. // fieldName
  1421. // OP_LOADFIELD of appropriate type
  1422. // operand
  1423. // OP_SAVEFIELD of appropriate type
  1424. // convert to return type if necessary.
  1425. getAssignOpTypeOp(op, subType, operand);
  1426. precompileIdent(slotName);
  1427. U32 size = valueExpr->precompile(subType);
  1428. if(type != subType)
  1429. size++;
  1430. if(arrayExpr)
  1431. return size + 9 + arrayExpr->precompile(TypeReqString) + objectExpr->precompile(TypeReqString);
  1432. else
  1433. return size + 6 + objectExpr->precompile(TypeReqString);
  1434. }
  1435. U32 SlotAssignOpNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1436. {
  1437. ip = valueExpr->compile(codeStream, ip, subType);
  1438. if(arrayExpr)
  1439. {
  1440. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1441. codeStream[ip++] = OP_ADVANCE_STR;
  1442. }
  1443. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1444. codeStream[ip++] = OP_SETCUROBJECT;
  1445. codeStream[ip++] = OP_SETCURFIELD;
  1446. codeStream[ip] = STEtoU32(slotName, ip);
  1447. ip++;
  1448. if(arrayExpr)
  1449. {
  1450. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1451. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1452. }
  1453. codeStream[ip++] = (subType == TypeReqFloat) ? OP_LOADFIELD_FLT : OP_LOADFIELD_UINT;
  1454. codeStream[ip++] = operand;
  1455. codeStream[ip++] = (subType == TypeReqFloat) ? OP_SAVEFIELD_FLT : OP_SAVEFIELD_UINT;
  1456. if(subType != type)
  1457. codeStream[ip++] = conversionOp(subType, type);
  1458. return ip;
  1459. }
  1460. TypeReq SlotAssignOpNode::getPreferredType()
  1461. {
  1462. getAssignOpTypeOp(op, subType, operand);
  1463. return subType;
  1464. }
  1465. //------------------------------------------------------------
  1466. U32 ObjectDeclNode::precompileSubObject(bool)
  1467. {
  1468. // goes
  1469. // OP_PUSHFRAME 1
  1470. // name expr
  1471. // OP_PUSH 1
  1472. // args... PUSH
  1473. // OP_CREATE_OBJECT 1
  1474. // parentObject 1
  1475. // isDatablock 1
  1476. // internalName 1
  1477. // isSingleton 1
  1478. // lineNumber 1
  1479. // fail point 1
  1480. // for each field, eval
  1481. // OP_ADD_OBJECT (to UINT[0]) 1
  1482. // root? 1
  1483. // add all the sub objects.
  1484. // OP_END_OBJECT 1
  1485. // root? 1
  1486. // To fix the stack issue [7/9/2007 Black]
  1487. // OP_FINISH_OBJECT <-- fail point jumps to this opcode
  1488. U32 argSize = 0;
  1489. precompileIdent(parentObject);
  1490. for(ExprNode *exprWalk = argList; exprWalk; exprWalk = (ExprNode *) exprWalk->getNext())
  1491. argSize += exprWalk->precompile(TypeReqString) + 1;
  1492. argSize += classNameExpr->precompile(TypeReqString) + 1;
  1493. U32 nameSize = objectNameExpr->precompile(TypeReqString) + 1;
  1494. U32 slotSize = 0;
  1495. for(SlotAssignNode *slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode *) slotWalk->getNext())
  1496. slotSize += slotWalk->precompile(TypeReqNone);
  1497. // OP_ADD_OBJECT
  1498. U32 subObjSize = 0;
  1499. for(ObjectDeclNode *objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode *) objectWalk->getNext())
  1500. subObjSize += objectWalk->precompileSubObject(false);
  1501. failOffset = 12 + nameSize + argSize + slotSize + subObjSize;
  1502. // +1 because the failOffset should jump to OP_FINISH_OBJECT [7/9/2007 Black]
  1503. return failOffset + 1;
  1504. }
  1505. U32 ObjectDeclNode::precompile(TypeReq type)
  1506. {
  1507. // root object decl does:
  1508. // push 0 onto the UINT stack OP_LOADIMMED_UINT
  1509. // precompiles the subObject(true)
  1510. // UINT stack now has object id
  1511. // type conv to type
  1512. U32 ret = 2 + precompileSubObject(true);
  1513. if(type != TypeReqUInt)
  1514. return ret + 1;
  1515. return ret;
  1516. }
  1517. U32 ObjectDeclNode::compileSubObject(U32 *codeStream, U32 ip, bool root)
  1518. {
  1519. U32 start = ip;
  1520. codeStream[ip++] = OP_PUSH_FRAME;
  1521. ip = classNameExpr->compile(codeStream, ip, TypeReqString);
  1522. codeStream[ip++] = OP_PUSH;
  1523. ip = objectNameExpr->compile(codeStream, ip, TypeReqString);
  1524. codeStream[ip++] = OP_PUSH;
  1525. for(ExprNode *exprWalk = argList; exprWalk; exprWalk = (ExprNode *) exprWalk->getNext())
  1526. {
  1527. ip = exprWalk->compile(codeStream, ip, TypeReqString);
  1528. codeStream[ip++] = OP_PUSH;
  1529. }
  1530. codeStream[ip++] = OP_CREATE_OBJECT;
  1531. codeStream[ip] = STEtoU32(parentObject, ip);
  1532. ip++;
  1533. codeStream[ip++] = isDatablock;
  1534. codeStream[ip++] = isClassNameInternal;
  1535. codeStream[ip++] = isSingleton;
  1536. codeStream[ip++] = dbgLineNumber;
  1537. codeStream[ip++] = start + failOffset;
  1538. for(SlotAssignNode *slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode *) slotWalk->getNext())
  1539. ip = slotWalk->compile(codeStream, ip, TypeReqNone);
  1540. codeStream[ip++] = OP_ADD_OBJECT;
  1541. codeStream[ip++] = root;
  1542. for(ObjectDeclNode *objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode *) objectWalk->getNext())
  1543. ip = objectWalk->compileSubObject(codeStream, ip, false);
  1544. codeStream[ip++] = OP_END_OBJECT;
  1545. codeStream[ip++] = root || isDatablock;
  1546. // Added to fix the object creation issue [7/9/2007 Black]
  1547. codeStream[ip++] = OP_FINISH_OBJECT;
  1548. return ip;
  1549. }
  1550. U32 ObjectDeclNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1551. {
  1552. codeStream[ip++] = OP_LOADIMMED_UINT;
  1553. codeStream[ip++] = 0;
  1554. ip = compileSubObject(codeStream, ip, true);
  1555. if(type != TypeReqUInt)
  1556. codeStream[ip++] = conversionOp(TypeReqUInt, type);
  1557. return ip;
  1558. }
  1559. TypeReq ObjectDeclNode::getPreferredType()
  1560. {
  1561. return TypeReqUInt;
  1562. }
  1563. //------------------------------------------------------------
  1564. U32 FunctionDeclStmtNode::precompileStmt(U32)
  1565. {
  1566. // OP_FUNC_DECL
  1567. // func name
  1568. // namespace
  1569. // package
  1570. // hasBody?
  1571. // func end ip
  1572. // argc
  1573. // ident array[argc]
  1574. // code
  1575. // OP_RETURN_VOID
  1576. setCurrentStringTable(&getFunctionStringTable());
  1577. setCurrentFloatTable(&getFunctionFloatTable());
  1578. argc = 0;
  1579. for(VarNode *walk = args; walk; walk = (VarNode *)((StmtNode*)walk)->getNext())
  1580. argc++;
  1581. CodeBlock::smInFunction = true;
  1582. precompileIdent(fnName);
  1583. precompileIdent(nameSpace);
  1584. precompileIdent(package);
  1585. U32 subSize = precompileBlock(stmts, 0);
  1586. CodeBlock::smInFunction = false;
  1587. addBreakCount();
  1588. setCurrentStringTable(&getGlobalStringTable());
  1589. setCurrentFloatTable(&getGlobalFloatTable());
  1590. endOffset = argc + subSize + 8;
  1591. return endOffset;
  1592. }
  1593. U32 FunctionDeclStmtNode::compileStmt(U32 *codeStream, U32 ip, U32, U32)
  1594. {
  1595. U32 start = ip;
  1596. codeStream[ip++] = OP_FUNC_DECL;
  1597. codeStream[ip] = STEtoU32(fnName, ip);
  1598. ip++;
  1599. codeStream[ip] = STEtoU32(nameSpace, ip);
  1600. ip++;
  1601. codeStream[ip] = STEtoU32(package, ip);
  1602. ip++;
  1603. codeStream[ip++] = U32( bool(stmts != NULL) ? 1 : 0 ) + U32( dbgLineNumber << 1 );
  1604. codeStream[ip++] = start + endOffset;
  1605. codeStream[ip++] = argc;
  1606. for(VarNode *walk = args; walk; walk = (VarNode *)((StmtNode*)walk)->getNext())
  1607. {
  1608. codeStream[ip] = STEtoU32(walk->varName, ip);
  1609. ip++;
  1610. }
  1611. CodeBlock::smInFunction = true;
  1612. ip = compileBlock(stmts, codeStream, ip, 0, 0);
  1613. // Add break so breakpoint can be set at closing brace or
  1614. // in empty function.
  1615. addBreakLine( ip );
  1616. CodeBlock::smInFunction = false;
  1617. codeStream[ip++] = OP_RETURN_VOID;
  1618. return ip;
  1619. }