astNodes.cpp 49 KB

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