astNodes.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  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. {
  997. // What we need to do in this case is turn it into a VarNode reference.
  998. // Unfortunately other nodes such as field access (SlotAccessNode)
  999. // cannot be optimized in the same manner as all fields are exposed
  1000. // and set as strings.
  1001. if (dynamic_cast<VarNode*>(expr) != NULL)
  1002. {
  1003. subType = TypeReqVar;
  1004. }
  1005. else
  1006. {
  1007. subType = TypeReqString;
  1008. }
  1009. }
  1010. // if it's an array expr, the formula is:
  1011. // eval expr
  1012. // (push and pop if it's TypeReqString) OP_ADVANCE_STR
  1013. // OP_LOADIMMED_IDENT
  1014. // varName
  1015. // OP_ADVANCE_STR
  1016. // eval array
  1017. // OP_REWIND_STR
  1018. // OP_SETCURVAR_ARRAY_CREATE
  1019. // OP_TERMINATE_REWIND_STR
  1020. // OP_SAVEVAR
  1021. //else
  1022. // eval expr
  1023. // OP_SETCURVAR_CREATE
  1024. // varname
  1025. // OP_SAVEVAR
  1026. const U32 addSize = (type != subType ? 1 : 0);
  1027. const U32 retSize = expr->precompile(subType);
  1028. #ifdef DEBUG_AST_NODES
  1029. if(retSize > 1000)
  1030. Con::printf("Bad expr %s", expr->dbgStmtType().c_str());
  1031. #endif
  1032. precompileIdent(varName);
  1033. return retSize + addSize + (arrayIndex ? arrayIndex->precompile(TypeReqString) + (subType == TypeReqString ? 8 : 6 ) : 3);
  1034. }
  1035. U32 AssignExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1036. {
  1037. ip = expr->compile(codeStream, ip, subType); // this is the value of VarNode
  1038. if(arrayIndex)
  1039. {
  1040. if(subType == TypeReqString)
  1041. codeStream[ip++] = OP_ADVANCE_STR;
  1042. codeStream[ip++] = OP_LOADIMMED_IDENT;
  1043. codeStream[ip] = STEtoU32(varName, ip);
  1044. ip++;
  1045. codeStream[ip++] = OP_ADVANCE_STR;
  1046. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  1047. codeStream[ip++] = OP_REWIND_STR;
  1048. codeStream[ip++] = OP_SETCURVAR_ARRAY_CREATE;
  1049. if(subType == TypeReqString)
  1050. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1051. }
  1052. else
  1053. {
  1054. codeStream[ip++] = OP_SETCURVAR_CREATE;
  1055. codeStream[ip] = STEtoU32(varName, ip);
  1056. ip++;
  1057. }
  1058. switch(subType)
  1059. {
  1060. case TypeReqString:
  1061. codeStream[ip++] = OP_SAVEVAR_STR;
  1062. break;
  1063. case TypeReqUInt:
  1064. codeStream[ip++] = OP_SAVEVAR_UINT;
  1065. break;
  1066. case TypeReqFloat:
  1067. codeStream[ip++] = OP_SAVEVAR_FLT;
  1068. break;
  1069. case TypeReqVar:
  1070. codeStream[ip++] = OP_SAVEVAR_VAR;
  1071. break;
  1072. case TypeReqNone:
  1073. break;
  1074. }
  1075. if(type != subType)
  1076. codeStream[ip++] = conversionOp(subType, type);
  1077. return ip;
  1078. }
  1079. TypeReq AssignExprNode::getPreferredType()
  1080. {
  1081. return expr->getPreferredType();
  1082. }
  1083. //------------------------------------------------------------
  1084. static void getAssignOpTypeOp(S32 op, TypeReq &type, U32 &operand)
  1085. {
  1086. switch(op)
  1087. {
  1088. case '+':
  1089. type = TypeReqFloat;
  1090. operand = OP_ADD;
  1091. break;
  1092. case '-':
  1093. type = TypeReqFloat;
  1094. operand = OP_SUB;
  1095. break;
  1096. case '*':
  1097. type = TypeReqFloat;
  1098. operand = OP_MUL;
  1099. break;
  1100. case '/':
  1101. type = TypeReqFloat;
  1102. operand = OP_DIV;
  1103. break;
  1104. case '%':
  1105. type = TypeReqUInt;
  1106. operand = OP_MOD;
  1107. break;
  1108. case '&':
  1109. type = TypeReqUInt;
  1110. operand = OP_BITAND;
  1111. break;
  1112. case '^':
  1113. type = TypeReqUInt;
  1114. operand = OP_XOR;
  1115. break;
  1116. case '|':
  1117. type = TypeReqUInt;
  1118. operand = OP_BITOR;
  1119. break;
  1120. case opSHL:
  1121. type = TypeReqUInt;
  1122. operand = OP_SHL;
  1123. break;
  1124. case opSHR:
  1125. type = TypeReqUInt;
  1126. operand = OP_SHR;
  1127. break;
  1128. }
  1129. }
  1130. U32 AssignOpExprNode::precompile(TypeReq type)
  1131. {
  1132. // goes like this...
  1133. // eval expr as float or int
  1134. // if there's an arrayIndex
  1135. // OP_LOADIMMED_IDENT
  1136. // varName
  1137. // OP_ADVANCE_STR
  1138. // eval arrayIndex stringwise
  1139. // OP_REWIND_STR
  1140. // OP_SETCURVAR_ARRAY_CREATE
  1141. // else
  1142. // OP_SETCURVAR_CREATE
  1143. // varName
  1144. // OP_LOADVAR_FLT or UINT
  1145. // operand
  1146. // OP_SAVEVAR_FLT or UINT
  1147. // conversion OP if necessary.
  1148. getAssignOpTypeOp(op, subType, operand);
  1149. precompileIdent(varName);
  1150. U32 size = expr->precompile(subType);
  1151. if(type != subType)
  1152. size++;
  1153. if(!arrayIndex)
  1154. return size + 5;
  1155. else
  1156. {
  1157. size += arrayIndex->precompile(TypeReqString);
  1158. return size + 8;
  1159. }
  1160. }
  1161. U32 AssignOpExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1162. {
  1163. ip = expr->compile(codeStream, ip, subType);
  1164. if(!arrayIndex)
  1165. {
  1166. codeStream[ip++] = OP_SETCURVAR_CREATE;
  1167. codeStream[ip] = STEtoU32(varName, ip);
  1168. ip++;
  1169. }
  1170. else
  1171. {
  1172. codeStream[ip++] = OP_LOADIMMED_IDENT;
  1173. codeStream[ip] = STEtoU32(varName, ip);
  1174. ip++;
  1175. codeStream[ip++] = OP_ADVANCE_STR;
  1176. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  1177. codeStream[ip++] = OP_REWIND_STR;
  1178. codeStream[ip++] = OP_SETCURVAR_ARRAY_CREATE;
  1179. }
  1180. codeStream[ip++] = (subType == TypeReqFloat) ? OP_LOADVAR_FLT : OP_LOADVAR_UINT;
  1181. codeStream[ip++] = operand;
  1182. codeStream[ip++] = (subType == TypeReqFloat) ? OP_SAVEVAR_FLT : OP_SAVEVAR_UINT;
  1183. if(subType != type)
  1184. codeStream[ip++] = conversionOp(subType, type);
  1185. return ip;
  1186. }
  1187. TypeReq AssignOpExprNode::getPreferredType()
  1188. {
  1189. getAssignOpTypeOp(op, subType, operand);
  1190. return subType;
  1191. }
  1192. //------------------------------------------------------------
  1193. U32 TTagSetStmtNode::precompileStmt(U32 loopCount)
  1194. {
  1195. TORQUE_UNUSED(loopCount);
  1196. return 0;
  1197. }
  1198. U32 TTagSetStmtNode::compileStmt(U32*, U32 ip, U32, U32)
  1199. {
  1200. return ip;
  1201. }
  1202. //------------------------------------------------------------
  1203. U32 TTagDerefNode::precompile(TypeReq)
  1204. {
  1205. return 0;
  1206. }
  1207. U32 TTagDerefNode::compile(U32*, U32 ip, TypeReq)
  1208. {
  1209. return ip;
  1210. }
  1211. TypeReq TTagDerefNode::getPreferredType()
  1212. {
  1213. return TypeReqNone;
  1214. }
  1215. //------------------------------------------------------------
  1216. U32 TTagExprNode::precompile(TypeReq)
  1217. {
  1218. return 0;
  1219. }
  1220. U32 TTagExprNode::compile(U32*, U32 ip, TypeReq)
  1221. {
  1222. return ip;
  1223. }
  1224. TypeReq TTagExprNode::getPreferredType()
  1225. {
  1226. return TypeReqNone;
  1227. }
  1228. //------------------------------------------------------------
  1229. U32 FuncCallExprNode::precompile(TypeReq type)
  1230. {
  1231. // OP_PUSH_FRAME
  1232. // arg OP_PUSH arg OP_PUSH arg OP_PUSH
  1233. // eval all the args, then call the function.
  1234. // OP_CALLFUNC
  1235. // function
  1236. // namespace
  1237. // isDot
  1238. U32 size = 0;
  1239. if(type != TypeReqString)
  1240. size++;
  1241. precompileIdent(funcName);
  1242. precompileIdent(nameSpace);
  1243. for(ExprNode *walk = args; walk; walk = (ExprNode *) walk->getNext()) {
  1244. TypeReq walkType = walk->getPreferredType();
  1245. if (walkType == TypeReqNone) walkType = TypeReqString;
  1246. size += walk->precompile(walkType) + 1;
  1247. }
  1248. return size + 5;
  1249. }
  1250. U32 FuncCallExprNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1251. {
  1252. codeStream[ip++] = OP_PUSH_FRAME;
  1253. for(ExprNode *walk = args; walk; walk = (ExprNode *) walk->getNext())
  1254. {
  1255. TypeReq walkType = walk->getPreferredType();
  1256. if (walkType == TypeReqNone) walkType = TypeReqString;
  1257. ip = walk->compile(codeStream, ip, walkType);
  1258. switch (walk->getPreferredType())
  1259. {
  1260. case TypeReqFloat:
  1261. codeStream[ip++] = OP_PUSH_FLT;
  1262. break;
  1263. case TypeReqUInt:
  1264. codeStream[ip++] = OP_PUSH_UINT;
  1265. break;
  1266. default:
  1267. codeStream[ip++] = OP_PUSH;
  1268. break;
  1269. }
  1270. }
  1271. if(callType == MethodCall || callType == ParentCall)
  1272. codeStream[ip++] = OP_CALLFUNC;
  1273. else
  1274. codeStream[ip++] = OP_CALLFUNC_RESOLVE;
  1275. codeStream[ip] = STEtoU32(funcName, ip);
  1276. ip++;
  1277. codeStream[ip] = STEtoU32(nameSpace, ip);
  1278. ip++;
  1279. codeStream[ip++] = callType;
  1280. if(type != TypeReqString)
  1281. codeStream[ip++] = conversionOp(TypeReqString, type);
  1282. return ip;
  1283. }
  1284. TypeReq FuncCallExprNode::getPreferredType()
  1285. {
  1286. return TypeReqString;
  1287. }
  1288. //------------------------------------------------------------
  1289. U32 AssertCallExprNode::precompile( TypeReq type )
  1290. {
  1291. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1292. messageIndex = getCurrentStringTable()->add( message, true, false );
  1293. U32 exprSize = testExpr->precompile(TypeReqUInt);
  1294. return exprSize + 2;
  1295. #else
  1296. return 0;
  1297. #endif
  1298. }
  1299. U32 AssertCallExprNode::compile( U32 *codeStream, U32 ip, TypeReq type )
  1300. {
  1301. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1302. ip = testExpr->compile( codeStream, ip, TypeReqUInt );
  1303. codeStream[ip++] = OP_ASSERT;
  1304. codeStream[ip++] = messageIndex;
  1305. #endif
  1306. return ip;
  1307. }
  1308. TypeReq AssertCallExprNode::getPreferredType()
  1309. {
  1310. return TypeReqNone;
  1311. }
  1312. //------------------------------------------------------------
  1313. U32 SlotAccessNode::precompile(TypeReq type)
  1314. {
  1315. if(type == TypeReqNone)
  1316. return 0;
  1317. U32 size = 0;
  1318. precompileIdent(slotName);
  1319. if(arrayExpr)
  1320. {
  1321. // eval array
  1322. // OP_ADVANCE_STR
  1323. // evaluate object expression sub (OP_SETCURFIELD)
  1324. // OP_TERMINATE_REWIND_STR
  1325. // OP_SETCURFIELDARRAY
  1326. // total add of 4 + array precomp
  1327. size += 3 + arrayExpr->precompile(TypeReqString);
  1328. }
  1329. // eval object expression sub + 3 (op_setCurField + OP_SETCUROBJECT)
  1330. size += objectExpr->precompile(TypeReqString) + 3;
  1331. // get field in desired type:
  1332. return size + 1;
  1333. }
  1334. U32 SlotAccessNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1335. {
  1336. if(type == TypeReqNone)
  1337. return ip;
  1338. if(arrayExpr)
  1339. {
  1340. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1341. codeStream[ip++] = OP_ADVANCE_STR;
  1342. }
  1343. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1344. codeStream[ip++] = OP_SETCUROBJECT;
  1345. codeStream[ip++] = OP_SETCURFIELD;
  1346. codeStream[ip] = STEtoU32(slotName, ip);
  1347. ip++;
  1348. if(arrayExpr)
  1349. {
  1350. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1351. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1352. }
  1353. switch(type)
  1354. {
  1355. case TypeReqUInt:
  1356. codeStream[ip++] = OP_LOADFIELD_UINT;
  1357. break;
  1358. case TypeReqFloat:
  1359. codeStream[ip++] = OP_LOADFIELD_FLT;
  1360. break;
  1361. case TypeReqString:
  1362. codeStream[ip++] = OP_LOADFIELD_STR;
  1363. break;
  1364. case TypeReqNone:
  1365. break;
  1366. }
  1367. return ip;
  1368. }
  1369. TypeReq SlotAccessNode::getPreferredType()
  1370. {
  1371. return TypeReqNone;
  1372. }
  1373. //-----------------------------------------------------------------------------
  1374. U32 InternalSlotAccessNode::precompile(TypeReq type)
  1375. {
  1376. if(type == TypeReqNone)
  1377. return 0;
  1378. U32 size = 3;
  1379. // eval object expression sub + 3 (op_setCurField + OP_SETCUROBJECT)
  1380. size += objectExpr->precompile(TypeReqString);
  1381. size += slotExpr->precompile(TypeReqString);
  1382. if(type != TypeReqUInt)
  1383. size++;
  1384. // get field in desired type:
  1385. return size;
  1386. }
  1387. U32 InternalSlotAccessNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1388. {
  1389. if(type == TypeReqNone)
  1390. return ip;
  1391. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1392. codeStream[ip++] = OP_SETCUROBJECT;
  1393. ip = slotExpr->compile(codeStream, ip, TypeReqString);
  1394. codeStream[ip++] = OP_SETCUROBJECT_INTERNAL;
  1395. codeStream[ip++] = recurse;
  1396. if(type != TypeReqUInt)
  1397. codeStream[ip++] = conversionOp(TypeReqUInt, type);
  1398. return ip;
  1399. }
  1400. TypeReq InternalSlotAccessNode::getPreferredType()
  1401. {
  1402. return TypeReqUInt;
  1403. }
  1404. //-----------------------------------------------------------------------------
  1405. //------------------------------------------------------------
  1406. U32 SlotAssignNode::precompile(TypeReq type)
  1407. {
  1408. // first eval the expression TypeReqString
  1409. // if it's an array:
  1410. // if OP_ADVANCE_STR 1
  1411. // eval array
  1412. // OP_ADVANCE_STR 1
  1413. // evaluate object expr
  1414. // OP_SETCUROBJECT 1
  1415. // OP_SETCURFIELD 1
  1416. // fieldName 1
  1417. // OP_TERMINATE_REWIND_STR 1
  1418. // OP_SETCURFIELDARRAY 1
  1419. // OP_TERMINATE_REWIND_STR 1
  1420. // else
  1421. // OP_ADVANCE_STR
  1422. // evaluate object expr
  1423. // OP_SETCUROBJECT
  1424. // OP_SETCURFIELD
  1425. // fieldName
  1426. // OP_TERMINATE_REWIND_STR
  1427. // OP_SAVEFIELD
  1428. // convert to return type if necessary.
  1429. U32 size = 0;
  1430. if(type != TypeReqString)
  1431. size++;
  1432. precompileIdent(slotName);
  1433. size += valueExpr->precompile(TypeReqString);
  1434. if(objectExpr)
  1435. size += objectExpr->precompile(TypeReqString) + 5;
  1436. else
  1437. size += 5;
  1438. if(arrayExpr)
  1439. size += arrayExpr->precompile(TypeReqString) + 3;
  1440. if(typeID != -1)
  1441. size += 2;
  1442. return size + 1;
  1443. }
  1444. U32 SlotAssignNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1445. {
  1446. ip = valueExpr->compile(codeStream, ip, TypeReqString);
  1447. codeStream[ip++] = OP_ADVANCE_STR;
  1448. if(arrayExpr)
  1449. {
  1450. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1451. codeStream[ip++] = OP_ADVANCE_STR;
  1452. }
  1453. if(objectExpr)
  1454. {
  1455. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1456. codeStream[ip++] = OP_SETCUROBJECT;
  1457. }
  1458. else
  1459. codeStream[ip++] = OP_SETCUROBJECT_NEW;
  1460. codeStream[ip++] = OP_SETCURFIELD;
  1461. codeStream[ip] = STEtoU32(slotName, ip);
  1462. ip++;
  1463. if(arrayExpr)
  1464. {
  1465. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1466. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1467. }
  1468. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1469. codeStream[ip++] = OP_SAVEFIELD_STR;
  1470. if(typeID != -1)
  1471. {
  1472. codeStream[ip++] = OP_SETCURFIELD_TYPE;
  1473. codeStream[ip++] = typeID;
  1474. }
  1475. if(type != TypeReqString)
  1476. codeStream[ip++] = conversionOp(TypeReqString, type);
  1477. return ip;
  1478. }
  1479. TypeReq SlotAssignNode::getPreferredType()
  1480. {
  1481. return TypeReqString;
  1482. }
  1483. //------------------------------------------------------------
  1484. U32 SlotAssignOpNode::precompile(TypeReq type)
  1485. {
  1486. // first eval the expression as its type
  1487. // if it's an array:
  1488. // eval array
  1489. // OP_ADVANCE_STR
  1490. // evaluate object expr
  1491. // OP_SETCUROBJECT
  1492. // OP_SETCURFIELD
  1493. // fieldName
  1494. // OP_TERMINATE_REWIND_STR
  1495. // OP_SETCURFIELDARRAY
  1496. // else
  1497. // evaluate object expr
  1498. // OP_SETCUROBJECT
  1499. // OP_SETCURFIELD
  1500. // fieldName
  1501. // OP_LOADFIELD of appropriate type
  1502. // operand
  1503. // OP_SAVEFIELD of appropriate type
  1504. // convert to return type if necessary.
  1505. getAssignOpTypeOp(op, subType, operand);
  1506. precompileIdent(slotName);
  1507. U32 size = valueExpr->precompile(subType);
  1508. if(type != subType)
  1509. size++;
  1510. if(arrayExpr)
  1511. return size + 9 + arrayExpr->precompile(TypeReqString) + objectExpr->precompile(TypeReqString);
  1512. else
  1513. return size + 6 + objectExpr->precompile(TypeReqString);
  1514. }
  1515. U32 SlotAssignOpNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1516. {
  1517. ip = valueExpr->compile(codeStream, ip, subType);
  1518. if(arrayExpr)
  1519. {
  1520. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1521. codeStream[ip++] = OP_ADVANCE_STR;
  1522. }
  1523. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1524. codeStream[ip++] = OP_SETCUROBJECT;
  1525. codeStream[ip++] = OP_SETCURFIELD;
  1526. codeStream[ip] = STEtoU32(slotName, ip);
  1527. ip++;
  1528. if(arrayExpr)
  1529. {
  1530. codeStream[ip++] = OP_TERMINATE_REWIND_STR;
  1531. codeStream[ip++] = OP_SETCURFIELD_ARRAY;
  1532. }
  1533. codeStream[ip++] = (subType == TypeReqFloat) ? OP_LOADFIELD_FLT : OP_LOADFIELD_UINT;
  1534. codeStream[ip++] = operand;
  1535. codeStream[ip++] = (subType == TypeReqFloat) ? OP_SAVEFIELD_FLT : OP_SAVEFIELD_UINT;
  1536. if(subType != type)
  1537. codeStream[ip++] = conversionOp(subType, type);
  1538. return ip;
  1539. }
  1540. TypeReq SlotAssignOpNode::getPreferredType()
  1541. {
  1542. getAssignOpTypeOp(op, subType, operand);
  1543. return subType;
  1544. }
  1545. //------------------------------------------------------------
  1546. U32 ObjectDeclNode::precompileSubObject(bool)
  1547. {
  1548. // goes
  1549. // OP_PUSHFRAME 1
  1550. // name expr
  1551. // OP_PUSH 1
  1552. // args... PUSH
  1553. // OP_CREATE_OBJECT 1
  1554. // parentObject 1
  1555. // isDatablock 1
  1556. // internalName 1
  1557. // isSingleton 1
  1558. // lineNumber 1
  1559. // fail point 1
  1560. // for each field, eval
  1561. // OP_ADD_OBJECT (to UINT[0]) 1
  1562. // root? 1
  1563. // add all the sub objects.
  1564. // OP_END_OBJECT 1
  1565. // root? 1
  1566. // To fix the stack issue [7/9/2007 Black]
  1567. // OP_FINISH_OBJECT <-- fail point jumps to this opcode
  1568. U32 argSize = 0;
  1569. precompileIdent(parentObject);
  1570. for(ExprNode *exprWalk = argList; exprWalk; exprWalk = (ExprNode *) exprWalk->getNext()) {
  1571. TypeReq walkType = exprWalk->getPreferredType();
  1572. if (walkType == TypeReqNone) walkType = TypeReqString;
  1573. argSize += exprWalk->precompile(walkType) + 1;
  1574. }
  1575. argSize += classNameExpr->precompile(TypeReqString) + 1;
  1576. U32 nameSize = objectNameExpr->precompile(TypeReqString) + 1;
  1577. U32 slotSize = 0;
  1578. for(SlotAssignNode *slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode *) slotWalk->getNext())
  1579. slotSize += slotWalk->precompile(TypeReqNone);
  1580. // OP_ADD_OBJECT
  1581. U32 subObjSize = 0;
  1582. for(ObjectDeclNode *objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode *) objectWalk->getNext())
  1583. subObjSize += objectWalk->precompileSubObject(false);
  1584. failOffset = 12 + nameSize + argSize + slotSize + subObjSize;
  1585. // +1 because the failOffset should jump to OP_FINISH_OBJECT [7/9/2007 Black]
  1586. return failOffset + 1;
  1587. }
  1588. U32 ObjectDeclNode::precompile(TypeReq type)
  1589. {
  1590. // root object decl does:
  1591. // push 0 onto the UINT stack OP_LOADIMMED_UINT
  1592. // precompiles the subObject(true)
  1593. // UINT stack now has object id
  1594. // type conv to type
  1595. U32 ret = 2 + precompileSubObject(true);
  1596. if(type != TypeReqUInt)
  1597. return ret + 1;
  1598. return ret;
  1599. }
  1600. U32 ObjectDeclNode::compileSubObject(U32 *codeStream, U32 ip, bool root)
  1601. {
  1602. U32 start = ip;
  1603. codeStream[ip++] = OP_PUSH_FRAME;
  1604. ip = classNameExpr->compile(codeStream, ip, TypeReqString);
  1605. codeStream[ip++] = OP_PUSH;
  1606. ip = objectNameExpr->compile(codeStream, ip, TypeReqString);
  1607. codeStream[ip++] = OP_PUSH;
  1608. for(ExprNode *exprWalk = argList; exprWalk; exprWalk = (ExprNode *) exprWalk->getNext())
  1609. {
  1610. TypeReq walkType = exprWalk->getPreferredType();
  1611. if (walkType == TypeReqNone) walkType = TypeReqString;
  1612. ip = exprWalk->compile(codeStream, ip, walkType);
  1613. switch (exprWalk->getPreferredType())
  1614. {
  1615. case TypeReqFloat:
  1616. codeStream[ip++] = OP_PUSH_FLT;
  1617. break;
  1618. case TypeReqUInt:
  1619. codeStream[ip++] = OP_PUSH_UINT;
  1620. break;
  1621. default:
  1622. codeStream[ip++] = OP_PUSH;
  1623. break;
  1624. }
  1625. }
  1626. codeStream[ip++] = OP_CREATE_OBJECT;
  1627. codeStream[ip] = STEtoU32(parentObject, ip);
  1628. ip++;
  1629. codeStream[ip++] = isDatablock;
  1630. codeStream[ip++] = isClassNameInternal;
  1631. codeStream[ip++] = isSingleton;
  1632. codeStream[ip++] = dbgLineNumber;
  1633. codeStream[ip++] = start + failOffset;
  1634. for(SlotAssignNode *slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode *) slotWalk->getNext())
  1635. ip = slotWalk->compile(codeStream, ip, TypeReqNone);
  1636. codeStream[ip++] = OP_ADD_OBJECT;
  1637. codeStream[ip++] = root;
  1638. for(ObjectDeclNode *objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode *) objectWalk->getNext())
  1639. ip = objectWalk->compileSubObject(codeStream, ip, false);
  1640. codeStream[ip++] = OP_END_OBJECT;
  1641. codeStream[ip++] = root || isDatablock;
  1642. // Added to fix the object creation issue [7/9/2007 Black]
  1643. codeStream[ip++] = OP_FINISH_OBJECT;
  1644. return ip;
  1645. }
  1646. U32 ObjectDeclNode::compile(U32 *codeStream, U32 ip, TypeReq type)
  1647. {
  1648. codeStream[ip++] = OP_LOADIMMED_UINT;
  1649. codeStream[ip++] = 0;
  1650. ip = compileSubObject(codeStream, ip, true);
  1651. if(type != TypeReqUInt)
  1652. codeStream[ip++] = conversionOp(TypeReqUInt, type);
  1653. return ip;
  1654. }
  1655. TypeReq ObjectDeclNode::getPreferredType()
  1656. {
  1657. return TypeReqUInt;
  1658. }
  1659. //------------------------------------------------------------
  1660. U32 FunctionDeclStmtNode::precompileStmt(U32)
  1661. {
  1662. // OP_FUNC_DECL
  1663. // func name
  1664. // namespace
  1665. // package
  1666. // hasBody?
  1667. // func end ip
  1668. // argc
  1669. // ident array[argc]
  1670. // code
  1671. // OP_RETURN_VOID
  1672. setCurrentStringTable(&getFunctionStringTable());
  1673. setCurrentFloatTable(&getFunctionFloatTable());
  1674. argc = 0;
  1675. for(VarNode *walk = args; walk; walk = (VarNode *)((StmtNode*)walk)->getNext())
  1676. argc++;
  1677. CodeBlock::smInFunction = true;
  1678. precompileIdent(fnName);
  1679. precompileIdent(nameSpace);
  1680. precompileIdent(package);
  1681. U32 subSize = precompileBlock(stmts, 0);
  1682. CodeBlock::smInFunction = false;
  1683. addBreakCount();
  1684. setCurrentStringTable(&getGlobalStringTable());
  1685. setCurrentFloatTable(&getGlobalFloatTable());
  1686. endOffset = argc + subSize + 8;
  1687. return endOffset;
  1688. }
  1689. U32 FunctionDeclStmtNode::compileStmt(U32 *codeStream, U32 ip, U32, U32)
  1690. {
  1691. U32 start = ip;
  1692. codeStream[ip++] = OP_FUNC_DECL;
  1693. codeStream[ip] = STEtoU32(fnName, ip);
  1694. ip++;
  1695. codeStream[ip] = STEtoU32(nameSpace, ip);
  1696. ip++;
  1697. codeStream[ip] = STEtoU32(package, ip);
  1698. ip++;
  1699. codeStream[ip++] = U32( bool(stmts != NULL) ? 1 : 0 ) + U32( dbgLineNumber << 1 );
  1700. codeStream[ip++] = start + endOffset;
  1701. codeStream[ip++] = argc;
  1702. for(VarNode *walk = args; walk; walk = (VarNode *)((StmtNode*)walk)->getNext())
  1703. {
  1704. codeStream[ip] = STEtoU32(walk->varName, ip);
  1705. ip++;
  1706. }
  1707. CodeBlock::smInFunction = true;
  1708. ip = compileBlock(stmts, codeStream, ip, 0, 0);
  1709. // Add break so breakpoint can be set at closing brace or
  1710. // in empty function.
  1711. addBreakLine( ip );
  1712. CodeBlock::smInFunction = false;
  1713. codeStream[ip++] = OP_RETURN_VOID;
  1714. return ip;
  1715. }