astNodes.cc 44 KB

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