astNodes.cpp 48 KB

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