astNodes.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. {
  45. ip = walk->compileStmt(codeStream, ip);
  46. }
  47. return codeStream.tell();
  48. }
  49. }
  50. using namespace Compiler;
  51. FuncVars gEvalFuncVars;
  52. FuncVars gGlobalScopeFuncVars;
  53. FuncVars* gFuncVars = NULL;
  54. inline FuncVars* getFuncVars(S32 lineNumber)
  55. {
  56. if (gFuncVars == &gGlobalScopeFuncVars)
  57. {
  58. const char* str = avar("Attemping to use local variable in global scope. File: %s Line: %d", CodeBlock::smCurrentParser->getCurrentFile(), lineNumber);
  59. scriptErrorHandler(str);
  60. }
  61. return gFuncVars;
  62. }
  63. //-----------------------------------------------------------------------------
  64. void StmtNode::addBreakLine(CodeStream& code)
  65. {
  66. code.addBreakLine(dbgLineNumber, code.tell());
  67. }
  68. //------------------------------------------------------------
  69. StmtNode::StmtNode() : dbgLineNumber(0)
  70. {
  71. next = NULL;
  72. dbgFileName = CodeBlock::smCurrentParser->getCurrentFile();
  73. }
  74. void StmtNode::setPackage(StringTableEntry)
  75. {
  76. }
  77. void StmtNode::append(StmtNode* appended)
  78. {
  79. StmtNode* walk = this;
  80. while (walk->next)
  81. walk = walk->next;
  82. walk->next = appended;
  83. }
  84. void FunctionDeclStmtNode::setPackage(StringTableEntry packageName)
  85. {
  86. package = packageName;
  87. }
  88. //------------------------------------------------------------
  89. //
  90. // Console language compilers
  91. //
  92. //------------------------------------------------------------
  93. U32 BreakStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  94. {
  95. if (codeStream.inLoop())
  96. {
  97. addBreakLine(codeStream);
  98. codeStream.emit(OP_JMP);
  99. codeStream.emitFix(CodeStream::FIXTYPE_BREAK);
  100. }
  101. else
  102. {
  103. Con::warnf(ConsoleLogEntry::General, "%s (%d): break outside of loop... ignoring.", dbgFileName, dbgLineNumber);
  104. }
  105. return codeStream.tell();
  106. }
  107. //------------------------------------------------------------
  108. U32 ContinueStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  109. {
  110. if (codeStream.inLoop())
  111. {
  112. addBreakLine(codeStream);
  113. codeStream.emit(OP_JMP);
  114. codeStream.emitFix(CodeStream::FIXTYPE_CONTINUE);
  115. }
  116. else
  117. {
  118. Con::warnf(ConsoleLogEntry::General, "%s (%d): continue outside of loop... ignoring.", dbgFileName, dbgLineNumber);
  119. }
  120. return codeStream.tell();
  121. }
  122. //------------------------------------------------------------
  123. U32 ExprNode::compileStmt(CodeStream& codeStream, U32 ip)
  124. {
  125. addBreakLine(codeStream);
  126. return compile(codeStream, ip, TypeReqNone);
  127. }
  128. //------------------------------------------------------------
  129. U32 ReturnStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  130. {
  131. addBreakLine(codeStream);
  132. if (!expr)
  133. codeStream.emit(OP_RETURN_VOID);
  134. else
  135. {
  136. TypeReq walkType = expr->getPreferredType();
  137. if (walkType == TypeReqNone) walkType = TypeReqString;
  138. ip = expr->compile(codeStream, ip, walkType);
  139. // Return the correct type
  140. switch (walkType) {
  141. case TypeReqUInt:
  142. codeStream.emit(OP_RETURN_UINT);
  143. break;
  144. case TypeReqFloat:
  145. codeStream.emit(OP_RETURN_FLT);
  146. break;
  147. default:
  148. codeStream.emit(OP_RETURN);
  149. break;
  150. }
  151. }
  152. return codeStream.tell();
  153. }
  154. //------------------------------------------------------------
  155. ExprNode* IfStmtNode::getSwitchOR(ExprNode* left, ExprNode* list, bool string)
  156. {
  157. ExprNode* nextExpr = (ExprNode*)list->getNext();
  158. ExprNode* test;
  159. if (string)
  160. test = StreqExprNode::alloc(left->dbgLineNumber, left, list, true);
  161. else
  162. test = IntBinaryExprNode::alloc(left->dbgLineNumber, opEQ, left, list);
  163. if (!nextExpr)
  164. return test;
  165. return IntBinaryExprNode::alloc(test->dbgLineNumber, opOR, test, getSwitchOR(left, nextExpr, string));
  166. }
  167. void IfStmtNode::propagateSwitchExpr(ExprNode* left, bool string)
  168. {
  169. testExpr = getSwitchOR(left, testExpr, string);
  170. if (propagate && elseBlock)
  171. ((IfStmtNode*)elseBlock)->propagateSwitchExpr(left, string);
  172. }
  173. U32 IfStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  174. {
  175. U32 endifIp, elseIp;
  176. addBreakLine(codeStream);
  177. if (testExpr->getPreferredType() == TypeReqUInt)
  178. {
  179. integer = true;
  180. }
  181. else
  182. {
  183. integer = false;
  184. }
  185. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  186. codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT);
  187. if (elseBlock)
  188. {
  189. elseIp = codeStream.emit(0);
  190. elseOffset = compileBlock(ifBlock, codeStream, ip) + 2;
  191. codeStream.emit(OP_JMP);
  192. endifIp = codeStream.emit(0);
  193. endifOffset = compileBlock(elseBlock, codeStream, ip);
  194. codeStream.patch(endifIp, endifOffset);
  195. codeStream.patch(elseIp, elseOffset);
  196. }
  197. else
  198. {
  199. endifIp = codeStream.emit(0);
  200. endifOffset = compileBlock(ifBlock, codeStream, ip);
  201. codeStream.patch(endifIp, endifOffset);
  202. }
  203. // Resolve fixes
  204. return codeStream.tell();
  205. }
  206. //------------------------------------------------------------
  207. U32 LoopStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  208. {
  209. if (testExpr->getPreferredType() == TypeReqUInt)
  210. {
  211. integer = true;
  212. }
  213. else
  214. {
  215. integer = false;
  216. }
  217. // if it's a for loop or a while loop it goes:
  218. // initExpr
  219. // testExpr
  220. // OP_JMPIFNOT to break point
  221. // loopStartPoint:
  222. // loopBlock
  223. // continuePoint:
  224. // endLoopExpr
  225. // testExpr
  226. // OP_JMPIF loopStartPoint
  227. // breakPoint:
  228. // otherwise if it's a do ... while() it goes:
  229. // initExpr
  230. // loopStartPoint:
  231. // loopBlock
  232. // continuePoint:
  233. // endLoopExpr
  234. // testExpr
  235. // OP_JMPIF loopStartPoint
  236. // breakPoint:
  237. // loopBlockStart == start of loop block
  238. // continue == skip to end
  239. // break == exit loop
  240. addBreakLine(codeStream);
  241. codeStream.pushFixScope(true);
  242. if (initExpr)
  243. ip = initExpr->compile(codeStream, ip, TypeReqNone);
  244. if (!isDoLoop)
  245. {
  246. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  247. codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT);
  248. codeStream.emitFix(CodeStream::FIXTYPE_BREAK);
  249. }
  250. // Compile internals of loop.
  251. loopBlockStartOffset = codeStream.tell();
  252. continueOffset = compileBlock(loopBlock, codeStream, ip);
  253. if (endLoopExpr)
  254. ip = endLoopExpr->compile(codeStream, ip, TypeReqNone);
  255. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  256. codeStream.emit(integer ? OP_JMPIF : OP_JMPIFF);
  257. codeStream.emitFix(CodeStream::FIXTYPE_LOOPBLOCKSTART);
  258. breakOffset = codeStream.tell(); // exit loop
  259. codeStream.fixLoop(loopBlockStartOffset, breakOffset, continueOffset);
  260. codeStream.popFixScope();
  261. return codeStream.tell();
  262. }
  263. //------------------------------------------------------------
  264. U32 IterStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  265. {
  266. // Instruction sequence:
  267. //
  268. // containerExpr
  269. // OP_ITER_BEGIN varName .fail
  270. // .continue:
  271. // OP_ITER .break
  272. // body
  273. // OP_JMP .continue
  274. // .break:
  275. // OP_ITER_END
  276. // .fail:
  277. addBreakLine(codeStream);
  278. codeStream.pushFixScope(true);
  279. bool isGlobal = varName[0] == '$';
  280. TypeReq varType = isStringIter ? TypeReqString : TypeReqUInt;
  281. const U32 startIp = ip;
  282. containerExpr->compile(codeStream, startIp, TypeReqString);
  283. codeStream.emit(isStringIter ? OP_ITER_BEGIN_STR : OP_ITER_BEGIN);
  284. codeStream.emit(isGlobal);
  285. if (isGlobal)
  286. codeStream.emitSTE(varName);
  287. else
  288. codeStream.emit(getFuncVars(dbgLineNumber)->assign(varName, varType, dbgLineNumber));
  289. const U32 finalFix = codeStream.emit(0);
  290. const U32 continueIp = codeStream.emit(OP_ITER);
  291. codeStream.emitFix(CodeStream::FIXTYPE_BREAK);
  292. const U32 bodyIp = codeStream.tell();
  293. const U32 jmpIp = compileBlock(body, codeStream, bodyIp);
  294. const U32 breakIp = jmpIp + 2;
  295. const U32 finalIp = breakIp + 1;
  296. codeStream.emit(OP_JMP);
  297. codeStream.emitFix(CodeStream::FIXTYPE_CONTINUE);
  298. codeStream.emit(OP_ITER_END);
  299. codeStream.patch(finalFix, finalIp);
  300. codeStream.fixLoop(bodyIp, breakIp, continueIp);
  301. codeStream.popFixScope();
  302. return codeStream.tell();
  303. }
  304. //------------------------------------------------------------
  305. U32 ConditionalExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  306. {
  307. // code is testExpr
  308. // JMPIFNOT falseStart
  309. // trueExpr
  310. // JMP end
  311. // falseExpr
  312. if (testExpr->getPreferredType() == TypeReqUInt)
  313. {
  314. integer = true;
  315. }
  316. else
  317. {
  318. integer = false;
  319. }
  320. ip = testExpr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  321. codeStream.emit(integer ? OP_JMPIFNOT : OP_JMPIFFNOT);
  322. U32 jumpElseIp = codeStream.emit(0);
  323. ip = trueExpr->compile(codeStream, ip, type);
  324. codeStream.emit(OP_JMP);
  325. U32 jumpEndIp = codeStream.emit(0);
  326. codeStream.patch(jumpElseIp, codeStream.tell());
  327. ip = falseExpr->compile(codeStream, ip, type);
  328. codeStream.patch(jumpEndIp, codeStream.tell());
  329. return codeStream.tell();
  330. }
  331. TypeReq ConditionalExprNode::getPreferredType()
  332. {
  333. return trueExpr->getPreferredType();
  334. }
  335. //------------------------------------------------------------
  336. U32 FloatBinaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  337. {
  338. if (optimize())
  339. {
  340. ip = optimizedNode->compile(codeStream, ip, type);
  341. return codeStream.tell();
  342. }
  343. ip = right->compile(codeStream, ip, TypeReqFloat);
  344. ip = left->compile(codeStream, ip, TypeReqFloat);
  345. U32 operand = OP_INVALID;
  346. switch (op)
  347. {
  348. case '+':
  349. operand = OP_ADD;
  350. break;
  351. case '-':
  352. operand = OP_SUB;
  353. break;
  354. case '/':
  355. operand = OP_DIV;
  356. break;
  357. case '*':
  358. operand = OP_MUL;
  359. break;
  360. }
  361. codeStream.emit(operand);
  362. return codeStream.tell();
  363. }
  364. TypeReq FloatBinaryExprNode::getPreferredType()
  365. {
  366. return TypeReqFloat;
  367. }
  368. //------------------------------------------------------------
  369. void IntBinaryExprNode::getSubTypeOperand()
  370. {
  371. subType = TypeReqUInt;
  372. switch (op)
  373. {
  374. case '^':
  375. operand = OP_XOR;
  376. break;
  377. case '%':
  378. operand = OP_MOD;
  379. break;
  380. case '&':
  381. operand = OP_BITAND;
  382. break;
  383. case '|':
  384. operand = OP_BITOR;
  385. break;
  386. case '<':
  387. operand = OP_CMPLT;
  388. subType = TypeReqFloat;
  389. break;
  390. case '>':
  391. operand = OP_CMPGR;
  392. subType = TypeReqFloat;
  393. break;
  394. case opGE:
  395. operand = OP_CMPGE;
  396. subType = TypeReqFloat;
  397. break;
  398. case opLE:
  399. operand = OP_CMPLE;
  400. subType = TypeReqFloat;
  401. break;
  402. case opEQ:
  403. operand = OP_CMPEQ;
  404. subType = TypeReqFloat;
  405. break;
  406. case opNE:
  407. operand = OP_CMPNE;
  408. subType = TypeReqFloat;
  409. break;
  410. case opOR:
  411. operand = OP_OR;
  412. break;
  413. case opAND:
  414. operand = OP_AND;
  415. break;
  416. case opSHR:
  417. operand = OP_SHR;
  418. break;
  419. case opSHL:
  420. operand = OP_SHL;
  421. break;
  422. }
  423. }
  424. U32 IntBinaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  425. {
  426. if (optimize())
  427. right = optimizedNode;
  428. getSubTypeOperand();
  429. if (operand == OP_OR || operand == OP_AND)
  430. {
  431. ip = left->compile(codeStream, ip, subType);
  432. codeStream.emit(operand == OP_OR ? OP_JMPIF_NP : OP_JMPIFNOT_NP);
  433. U32 jmpIp = codeStream.emit(0);
  434. ip = right->compile(codeStream, ip, subType);
  435. codeStream.patch(jmpIp, ip);
  436. }
  437. else
  438. {
  439. ip = right->compile(codeStream, ip, subType);
  440. ip = left->compile(codeStream, ip, subType);
  441. codeStream.emit(operand);
  442. }
  443. return codeStream.tell();
  444. }
  445. TypeReq IntBinaryExprNode::getPreferredType()
  446. {
  447. return TypeReqUInt;
  448. }
  449. //------------------------------------------------------------
  450. U32 StreqExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  451. {
  452. // eval str left
  453. // OP_ADVANCE_STR_NUL
  454. // eval str right
  455. // OP_COMPARE_STR
  456. // optional conversion
  457. ip = left->compile(codeStream, ip, TypeReqString);
  458. ip = right->compile(codeStream, ip, TypeReqString);
  459. codeStream.emit(OP_COMPARE_STR);
  460. if (!eq)
  461. codeStream.emit(OP_NOT);
  462. return codeStream.tell();
  463. }
  464. TypeReq StreqExprNode::getPreferredType()
  465. {
  466. return TypeReqUInt;
  467. }
  468. //------------------------------------------------------------
  469. U32 StrcatExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  470. {
  471. ip = left->compile(codeStream, ip, TypeReqString);
  472. if (appendChar)
  473. {
  474. codeStream.emit(OP_ADVANCE_STR_APPENDCHAR);
  475. codeStream.emit(appendChar);
  476. }
  477. ip = right->compile(codeStream, ip, TypeReqString);
  478. codeStream.emit(OP_REWIND_STR);
  479. return codeStream.tell();
  480. }
  481. TypeReq StrcatExprNode::getPreferredType()
  482. {
  483. return TypeReqString;
  484. }
  485. //------------------------------------------------------------
  486. U32 CommaCatExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  487. {
  488. ip = left->compile(codeStream, ip, TypeReqString);
  489. codeStream.emit(OP_ADVANCE_STR_APPENDCHAR);
  490. codeStream.emit('_');
  491. ip = right->compile(codeStream, ip, TypeReqString);
  492. codeStream.emit(OP_REWIND_STR);
  493. // At this point the stack has the concatenated string.
  494. // But we're paranoid, so accept (but whine) if we get an oddity...
  495. if (type == TypeReqUInt || type == TypeReqFloat)
  496. Con::warnf(ConsoleLogEntry::General, "%s (%d): converting comma string to a number... probably wrong.", dbgFileName, dbgLineNumber);
  497. return codeStream.tell();
  498. }
  499. TypeReq CommaCatExprNode::getPreferredType()
  500. {
  501. return TypeReqString;
  502. }
  503. //------------------------------------------------------------
  504. U32 IntUnaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  505. {
  506. integer = true;
  507. TypeReq prefType = expr->getPreferredType();
  508. if (op == '!' && (prefType == TypeReqFloat || prefType == TypeReqString))
  509. integer = false;
  510. ip = expr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  511. if (op == '!')
  512. codeStream.emit(integer ? OP_NOT : OP_NOTF);
  513. else if (op == '~')
  514. codeStream.emit(OP_ONESCOMPLEMENT);
  515. return codeStream.tell();
  516. }
  517. TypeReq IntUnaryExprNode::getPreferredType()
  518. {
  519. return TypeReqUInt;
  520. }
  521. //------------------------------------------------------------
  522. U32 FloatUnaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  523. {
  524. ip = expr->compile(codeStream, ip, TypeReqFloat);
  525. codeStream.emit(OP_NEG);
  526. return codeStream.tell();
  527. }
  528. TypeReq FloatUnaryExprNode::getPreferredType()
  529. {
  530. return TypeReqFloat;
  531. }
  532. //------------------------------------------------------------
  533. U32 VarNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  534. {
  535. // if this has an arrayIndex...
  536. // OP_LOADIMMED_IDENT
  537. // varName
  538. // OP_ADVANCE_STR
  539. // evaluate arrayIndex TypeReqString
  540. // OP_REWIND_STR
  541. // OP_SETCURVAR_ARRAY
  542. // OP_LOADVAR (type)
  543. // else
  544. // OP_SETCURVAR
  545. // varName
  546. // OP_LOADVAR (type)
  547. if (type == TypeReqNone)
  548. return codeStream.tell();
  549. precompileIdent(varName);
  550. bool oldVariables = arrayIndex || varName[0] == '$';
  551. if (oldVariables)
  552. {
  553. codeStream.emit(arrayIndex ? OP_LOADIMMED_IDENT : OP_SETCURVAR);
  554. codeStream.emitSTE(varName);
  555. if (arrayIndex)
  556. {
  557. //codeStream.emit(OP_ADVANCE_STR);
  558. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  559. codeStream.emit(OP_REWIND_STR);
  560. codeStream.emit(OP_SETCURVAR_ARRAY);
  561. codeStream.emit(OP_POP_STK);
  562. }
  563. switch (type)
  564. {
  565. case TypeReqUInt:
  566. codeStream.emit(OP_LOADVAR_UINT);
  567. break;
  568. case TypeReqFloat:
  569. codeStream.emit(OP_LOADVAR_FLT);
  570. break;
  571. case TypeReqString:
  572. codeStream.emit(OP_LOADVAR_STR);
  573. break;
  574. case TypeReqNone:
  575. break;
  576. default:
  577. break;
  578. }
  579. }
  580. else
  581. {
  582. switch (type)
  583. {
  584. case TypeReqUInt: codeStream.emit(OP_LOAD_LOCAL_VAR_UINT); break;
  585. case TypeReqFloat: codeStream.emit(OP_LOAD_LOCAL_VAR_FLT); break;
  586. default: codeStream.emit(OP_LOAD_LOCAL_VAR_STR);
  587. }
  588. codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber));
  589. }
  590. return codeStream.tell();
  591. }
  592. TypeReq VarNode::getPreferredType()
  593. {
  594. bool oldVariables = arrayIndex || varName[0] == '$';
  595. return oldVariables ? TypeReqNone : getFuncVars(dbgLineNumber)->lookupType(varName, dbgLineNumber);
  596. }
  597. //------------------------------------------------------------
  598. U32 IntNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  599. {
  600. if (type == TypeReqString)
  601. index = getCurrentStringTable()->addIntString(value);
  602. else if (type == TypeReqFloat)
  603. index = getCurrentFloatTable()->add(value);
  604. switch (type)
  605. {
  606. case TypeReqUInt:
  607. codeStream.emit(OP_LOADIMMED_UINT);
  608. codeStream.emit(value);
  609. break;
  610. case TypeReqString:
  611. codeStream.emit(OP_LOADIMMED_STR);
  612. codeStream.emit(index);
  613. break;
  614. case TypeReqFloat:
  615. codeStream.emit(OP_LOADIMMED_FLT);
  616. codeStream.emit(index);
  617. break;
  618. case TypeReqNone:
  619. break;
  620. }
  621. return codeStream.tell();
  622. }
  623. TypeReq IntNode::getPreferredType()
  624. {
  625. return TypeReqUInt;
  626. }
  627. //------------------------------------------------------------
  628. U32 FloatNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  629. {
  630. if (type == TypeReqString)
  631. index = getCurrentStringTable()->addFloatString(value);
  632. else if (type == TypeReqFloat)
  633. index = getCurrentFloatTable()->add(value);
  634. switch (type)
  635. {
  636. case TypeReqUInt:
  637. codeStream.emit(OP_LOADIMMED_UINT);
  638. codeStream.emit(U32(value));
  639. break;
  640. case TypeReqString:
  641. codeStream.emit(OP_LOADIMMED_STR);
  642. codeStream.emit(index);
  643. break;
  644. case TypeReqFloat:
  645. codeStream.emit(OP_LOADIMMED_FLT);
  646. codeStream.emit(index);
  647. break;
  648. case TypeReqNone:
  649. break;
  650. }
  651. return codeStream.tell();
  652. }
  653. TypeReq FloatNode::getPreferredType()
  654. {
  655. return TypeReqFloat;
  656. }
  657. //------------------------------------------------------------
  658. U32 StrConstNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  659. {
  660. // Early out for documentation block.
  661. if (doc)
  662. {
  663. index = getCurrentStringTable()->add(str, true, tag);
  664. }
  665. else if (type == TypeReqString)
  666. {
  667. index = getCurrentStringTable()->add(str, true, tag);
  668. }
  669. else if (type != TypeReqNone)
  670. {
  671. fVal = consoleStringToNumber(str, dbgFileName, dbgLineNumber);
  672. if (type == TypeReqFloat)
  673. {
  674. index = getCurrentFloatTable()->add(fVal);
  675. }
  676. }
  677. // If this is a DOCBLOCK, then process w/ appropriate op...
  678. if (doc)
  679. {
  680. codeStream.emit(OP_DOCBLOCK_STR);
  681. codeStream.emit(index);
  682. return ip;
  683. }
  684. // Otherwise, deal with it normally as a string literal case.
  685. switch (type)
  686. {
  687. case TypeReqString:
  688. codeStream.emit(tag ? OP_TAG_TO_STR : OP_LOADIMMED_STR);
  689. codeStream.emit(index);
  690. break;
  691. case TypeReqUInt:
  692. codeStream.emit(OP_LOADIMMED_UINT);
  693. codeStream.emit(U32(fVal));
  694. break;
  695. case TypeReqFloat:
  696. codeStream.emit(OP_LOADIMMED_FLT);
  697. codeStream.emit(index);
  698. break;
  699. case TypeReqNone:
  700. break;
  701. }
  702. return codeStream.tell();
  703. }
  704. TypeReq StrConstNode::getPreferredType()
  705. {
  706. return TypeReqString;
  707. }
  708. //------------------------------------------------------------
  709. U32 ConstantNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  710. {
  711. if (type == TypeReqString)
  712. {
  713. precompileIdent(value);
  714. }
  715. else if (type != TypeReqNone)
  716. {
  717. fVal = consoleStringToNumber(value, dbgFileName, dbgLineNumber);
  718. if (type == TypeReqFloat)
  719. index = getCurrentFloatTable()->add(fVal);
  720. }
  721. switch (type)
  722. {
  723. case TypeReqString:
  724. codeStream.emit(OP_LOADIMMED_IDENT);
  725. codeStream.emitSTE(value);
  726. break;
  727. case TypeReqUInt:
  728. codeStream.emit(OP_LOADIMMED_UINT);
  729. codeStream.emit(U32(fVal));
  730. break;
  731. case TypeReqFloat:
  732. codeStream.emit(OP_LOADIMMED_FLT);
  733. codeStream.emit(index);
  734. break;
  735. case TypeReqNone:
  736. break;
  737. }
  738. return ip;
  739. }
  740. TypeReq ConstantNode::getPreferredType()
  741. {
  742. return TypeReqString;
  743. }
  744. //------------------------------------------------------------
  745. U32 AssignExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  746. {
  747. subType = expr->getPreferredType();
  748. if (subType == TypeReqNone)
  749. subType = type;
  750. if (subType == TypeReqNone)
  751. subType = TypeReqString;
  752. // if it's an array expr, the formula is:
  753. // eval expr
  754. // (push and pop if it's TypeReqString) OP_ADVANCE_STR
  755. // OP_LOADIMMED_IDENT
  756. // varName
  757. // OP_ADVANCE_STR
  758. // eval array
  759. // OP_REWIND_STR
  760. // OP_SETCURVAR_ARRAY_CREATE
  761. // OP_TERMINATE_REWIND_STR
  762. // OP_SAVEVAR
  763. //else
  764. // eval expr
  765. // OP_SETCURVAR_CREATE
  766. // varname
  767. // OP_SAVEVAR
  768. precompileIdent(varName);
  769. ip = expr->compile(codeStream, ip, subType);
  770. bool oldVariables = arrayIndex || varName[0] == '$';
  771. if (oldVariables)
  772. {
  773. if (arrayIndex)
  774. {
  775. //if (subType == TypeReqString)
  776. // codeStream.emit(OP_ADVANCE_STR);
  777. codeStream.emit(OP_LOADIMMED_IDENT);
  778. codeStream.emitSTE(varName);
  779. //codeStream.emit(OP_ADVANCE_STR);
  780. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  781. codeStream.emit(OP_REWIND_STR);
  782. codeStream.emit(OP_SETCURVAR_ARRAY_CREATE);
  783. if (type == TypeReqNone)
  784. codeStream.emit(OP_POP_STK);
  785. }
  786. else
  787. {
  788. codeStream.emit(OP_SETCURVAR_CREATE);
  789. codeStream.emitSTE(varName);
  790. }
  791. switch (subType)
  792. {
  793. case TypeReqString: codeStream.emit(OP_SAVEVAR_STR); break;
  794. case TypeReqUInt: codeStream.emit(OP_SAVEVAR_UINT); break;
  795. case TypeReqFloat: codeStream.emit(OP_SAVEVAR_FLT); break;
  796. }
  797. }
  798. else
  799. {
  800. switch (subType)
  801. {
  802. case TypeReqUInt: codeStream.emit(OP_SAVE_LOCAL_VAR_UINT); break;
  803. case TypeReqFloat: codeStream.emit(OP_SAVE_LOCAL_VAR_FLT); break;
  804. default: codeStream.emit(OP_SAVE_LOCAL_VAR_STR);
  805. }
  806. codeStream.emit(getFuncVars(dbgLineNumber)->assign(varName, subType == TypeReqNone ? TypeReqString : subType, dbgLineNumber));
  807. }
  808. if (type == TypeReqNone)
  809. codeStream.emit(OP_POP_STK);
  810. return ip;
  811. }
  812. TypeReq AssignExprNode::getPreferredType()
  813. {
  814. return expr->getPreferredType();
  815. }
  816. //------------------------------------------------------------
  817. static void getAssignOpTypeOp(S32 op, TypeReq& type, U32& operand)
  818. {
  819. switch (op)
  820. {
  821. case opPLUSPLUS:
  822. TORQUE_CASE_FALLTHROUGH;
  823. case '+':
  824. type = TypeReqFloat;
  825. operand = OP_ADD;
  826. break;
  827. case opMINUSMINUS:
  828. TORQUE_CASE_FALLTHROUGH;
  829. case '-':
  830. type = TypeReqFloat;
  831. operand = OP_SUB;
  832. break;
  833. case '*':
  834. type = TypeReqFloat;
  835. operand = OP_MUL;
  836. break;
  837. case '/':
  838. type = TypeReqFloat;
  839. operand = OP_DIV;
  840. break;
  841. case '%':
  842. type = TypeReqUInt;
  843. operand = OP_MOD;
  844. break;
  845. case '&':
  846. type = TypeReqUInt;
  847. operand = OP_BITAND;
  848. break;
  849. case '^':
  850. type = TypeReqUInt;
  851. operand = OP_XOR;
  852. break;
  853. case '|':
  854. type = TypeReqUInt;
  855. operand = OP_BITOR;
  856. break;
  857. case opSHL:
  858. type = TypeReqUInt;
  859. operand = OP_SHL;
  860. break;
  861. case opSHR:
  862. type = TypeReqUInt;
  863. operand = OP_SHR;
  864. break;
  865. default:
  866. AssertFatal(false, "Invalid opcode on operation expression");
  867. }
  868. }
  869. U32 AssignOpExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  870. {
  871. // goes like this...
  872. // eval expr as float or int
  873. // if there's an arrayIndex
  874. // OP_LOADIMMED_IDENT
  875. // varName
  876. // OP_ADVANCE_STR
  877. // eval arrayIndex stringwise
  878. // OP_REWIND_STR
  879. // OP_SETCURVAR_ARRAY_CREATE
  880. // else
  881. // OP_SETCURVAR_CREATE
  882. // varName
  883. // OP_LOADVAR_FLT or UINT
  884. // operand
  885. // OP_SAVEVAR_FLT or UINT
  886. // conversion OP if necessary.
  887. getAssignOpTypeOp(op, subType, operand);
  888. precompileIdent(varName);
  889. bool oldVariables = arrayIndex || varName[0] == '$';
  890. if (op == opPLUSPLUS && !oldVariables && type == TypeReqNone)
  891. {
  892. const S32 varIdx = getFuncVars(dbgLineNumber)->assign(varName, TypeReqFloat, dbgLineNumber);
  893. codeStream.emit(OP_INC);
  894. codeStream.emit(varIdx);
  895. }
  896. else
  897. {
  898. ip = expr->compile(codeStream, ip, subType);
  899. if (oldVariables)
  900. {
  901. if (!arrayIndex)
  902. {
  903. codeStream.emit(OP_SETCURVAR_CREATE);
  904. codeStream.emitSTE(varName);
  905. }
  906. else
  907. {
  908. codeStream.emit(OP_LOADIMMED_IDENT);
  909. codeStream.emitSTE(varName);
  910. //codeStream.emit(OP_ADVANCE_STR);
  911. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  912. codeStream.emit(OP_REWIND_STR);
  913. codeStream.emit(OP_SETCURVAR_ARRAY_CREATE);
  914. if (type == TypeReqNone)
  915. codeStream.emit(OP_POP_STK);
  916. }
  917. codeStream.emit((subType == TypeReqFloat) ? OP_LOADVAR_FLT : OP_LOADVAR_UINT);
  918. codeStream.emit(operand);
  919. codeStream.emit((subType == TypeReqFloat) ? OP_SAVEVAR_FLT : OP_SAVEVAR_UINT);
  920. }
  921. else
  922. {
  923. const bool isFloat = subType == TypeReqFloat;
  924. const S32 varIdx = getFuncVars(dbgLineNumber)->assign(varName, subType == TypeReqNone ? TypeReqString : subType, dbgLineNumber);
  925. codeStream.emit(isFloat ? OP_LOAD_LOCAL_VAR_FLT : OP_LOAD_LOCAL_VAR_UINT);
  926. codeStream.emit(varIdx);
  927. codeStream.emit(operand);
  928. codeStream.emit(isFloat ? OP_SAVE_LOCAL_VAR_FLT : OP_SAVE_LOCAL_VAR_UINT);
  929. codeStream.emit(varIdx);
  930. }
  931. if (type == TypeReqNone)
  932. codeStream.emit(OP_POP_STK);
  933. }
  934. return codeStream.tell();
  935. }
  936. TypeReq AssignOpExprNode::getPreferredType()
  937. {
  938. getAssignOpTypeOp(op, subType, operand);
  939. return subType;
  940. }
  941. //------------------------------------------------------------
  942. U32 TTagSetStmtNode::compileStmt(CodeStream&, U32 ip)
  943. {
  944. return ip;
  945. }
  946. //------------------------------------------------------------
  947. U32 TTagDerefNode::compile(CodeStream&, U32 ip, TypeReq)
  948. {
  949. return ip;
  950. }
  951. TypeReq TTagDerefNode::getPreferredType()
  952. {
  953. return TypeReqNone;
  954. }
  955. //------------------------------------------------------------
  956. U32 TTagExprNode::compile(CodeStream&, U32 ip, TypeReq)
  957. {
  958. return ip;
  959. }
  960. TypeReq TTagExprNode::getPreferredType()
  961. {
  962. return TypeReqNone;
  963. }
  964. //------------------------------------------------------------
  965. U32 FuncCallExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  966. {
  967. // OP_PUSH_FRAME
  968. // arg OP_PUSH arg OP_PUSH arg OP_PUSH
  969. // eval all the args, then call the function.
  970. // OP_CALLFUNC
  971. // function
  972. // namespace
  973. // isDot
  974. precompileIdent(funcName);
  975. precompileIdent(nameSpace);
  976. S32 count = 0;
  977. for (ExprNode* walk = args; walk; walk = static_cast<ExprNode*>(walk->getNext()))
  978. count++;
  979. codeStream.emit(OP_PUSH_FRAME);
  980. codeStream.emit(count);
  981. for (ExprNode* walk = args; walk; walk = static_cast<ExprNode*>(walk->getNext()))
  982. {
  983. TypeReq walkType = walk->getPreferredType();
  984. if (walkType == TypeReqNone)
  985. walkType = TypeReqString;
  986. ip = walk->compile(codeStream, ip, walkType);
  987. codeStream.emit(OP_PUSH);
  988. }
  989. codeStream.emit(OP_CALLFUNC);
  990. codeStream.emitSTE(funcName);
  991. codeStream.emitSTE(nameSpace);
  992. codeStream.emit(callType);
  993. if (type == TypeReqNone)
  994. codeStream.emit(OP_POP_STK);
  995. return codeStream.tell();
  996. }
  997. TypeReq FuncCallExprNode::getPreferredType()
  998. {
  999. return TypeReqString;
  1000. }
  1001. //------------------------------------------------------------
  1002. U32 AssertCallExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1003. {
  1004. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1005. messageIndex = getCurrentStringTable()->add(message, true, false);
  1006. ip = testExpr->compile(codeStream, ip, TypeReqUInt);
  1007. codeStream.emit(OP_ASSERT);
  1008. codeStream.emit(messageIndex);
  1009. #endif
  1010. return codeStream.tell();
  1011. }
  1012. TypeReq AssertCallExprNode::getPreferredType()
  1013. {
  1014. return TypeReqNone;
  1015. }
  1016. //------------------------------------------------------------
  1017. U32 SlotAccessNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1018. {
  1019. if (type == TypeReqNone)
  1020. return ip;
  1021. precompileIdent(slotName);
  1022. if (arrayExpr)
  1023. {
  1024. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1025. }
  1026. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1027. codeStream.emit(OP_SETCUROBJECT);
  1028. codeStream.emit(OP_SETCURFIELD);
  1029. codeStream.emitSTE(slotName);
  1030. codeStream.emit(OP_POP_STK);
  1031. if (arrayExpr)
  1032. {
  1033. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1034. codeStream.emit(OP_POP_STK);
  1035. }
  1036. switch (type)
  1037. {
  1038. case TypeReqUInt:
  1039. codeStream.emit(OP_LOADFIELD_UINT);
  1040. break;
  1041. case TypeReqFloat:
  1042. codeStream.emit(OP_LOADFIELD_FLT);
  1043. break;
  1044. case TypeReqString:
  1045. codeStream.emit(OP_LOADFIELD_STR);
  1046. break;
  1047. case TypeReqNone:
  1048. break;
  1049. }
  1050. return codeStream.tell();
  1051. }
  1052. TypeReq SlotAccessNode::getPreferredType()
  1053. {
  1054. return TypeReqNone;
  1055. }
  1056. //-----------------------------------------------------------------------------
  1057. U32 InternalSlotAccessNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1058. {
  1059. if (type == TypeReqNone)
  1060. return ip;
  1061. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1062. codeStream.emit(OP_SETCUROBJECT);
  1063. // we pop the stack as we will override the current object with the internal object
  1064. codeStream.emit(OP_POP_STK);
  1065. ip = slotExpr->compile(codeStream, ip, TypeReqString);
  1066. codeStream.emit(OP_SETCUROBJECT_INTERNAL);
  1067. codeStream.emit(recurse);
  1068. return codeStream.tell();
  1069. }
  1070. TypeReq InternalSlotAccessNode::getPreferredType()
  1071. {
  1072. return TypeReqUInt;
  1073. }
  1074. //-----------------------------------------------------------------------------
  1075. U32 SlotAssignNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1076. {
  1077. precompileIdent(slotName);
  1078. ip = valueExpr->compile(codeStream, ip, TypeReqString);
  1079. if (arrayExpr)
  1080. {
  1081. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1082. }
  1083. if (objectExpr)
  1084. {
  1085. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1086. codeStream.emit(OP_SETCUROBJECT);
  1087. }
  1088. else
  1089. codeStream.emit(OP_SETCUROBJECT_NEW);
  1090. codeStream.emit(OP_SETCURFIELD);
  1091. codeStream.emitSTE(slotName);
  1092. if (objectExpr)
  1093. {
  1094. // Don't pop unless we are assigning a field to an object
  1095. // (For initializer fields, we don't wanna pop)
  1096. codeStream.emit(OP_POP_STK);
  1097. }
  1098. if (arrayExpr)
  1099. {
  1100. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1101. codeStream.emit(OP_POP_STK);
  1102. }
  1103. codeStream.emit(OP_SAVEFIELD_STR);
  1104. if (typeID != -1)
  1105. {
  1106. codeStream.emit(OP_SETCURFIELD_TYPE);
  1107. codeStream.emit(typeID);
  1108. }
  1109. if (type == TypeReqNone)
  1110. codeStream.emit(OP_POP_STK);
  1111. return codeStream.tell();
  1112. }
  1113. TypeReq SlotAssignNode::getPreferredType()
  1114. {
  1115. return TypeReqString;
  1116. }
  1117. //------------------------------------------------------------
  1118. U32 SlotAssignOpNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1119. {
  1120. // first eval the expression as its type
  1121. // if it's an array:
  1122. // eval array
  1123. // OP_ADVANCE_STR
  1124. // evaluate object expr
  1125. // OP_SETCUROBJECT
  1126. // OP_SETCURFIELD
  1127. // fieldName
  1128. // OP_TERMINATE_REWIND_STR
  1129. // OP_SETCURFIELDARRAY
  1130. // else
  1131. // evaluate object expr
  1132. // OP_SETCUROBJECT
  1133. // OP_SETCURFIELD
  1134. // fieldName
  1135. // OP_LOADFIELD of appropriate type
  1136. // operand
  1137. // OP_SAVEFIELD of appropriate type
  1138. // convert to return type if necessary.
  1139. getAssignOpTypeOp(op, subType, operand);
  1140. precompileIdent(slotName);
  1141. ip = valueExpr->compile(codeStream, ip, subType);
  1142. if (arrayExpr)
  1143. {
  1144. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1145. }
  1146. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1147. codeStream.emit(OP_SETCUROBJECT);
  1148. codeStream.emit(OP_SETCURFIELD);
  1149. codeStream.emitSTE(slotName);
  1150. codeStream.emit(OP_POP_STK);
  1151. if (arrayExpr)
  1152. {
  1153. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1154. if (subType == TypeReqNone)
  1155. codeStream.emit(OP_POP_STK);
  1156. }
  1157. codeStream.emit((subType == TypeReqFloat) ? OP_LOADFIELD_FLT : OP_LOADFIELD_UINT);
  1158. codeStream.emit(operand);
  1159. codeStream.emit((subType == TypeReqFloat) ? OP_SAVEFIELD_FLT : OP_SAVEFIELD_UINT);
  1160. if (type == TypeReqNone)
  1161. codeStream.emit(OP_POP_STK);
  1162. return codeStream.tell();
  1163. }
  1164. TypeReq SlotAssignOpNode::getPreferredType()
  1165. {
  1166. getAssignOpTypeOp(op, subType, operand);
  1167. return subType;
  1168. }
  1169. //------------------------------------------------------------
  1170. U32 ObjectDeclNode::compileSubObject(CodeStream& codeStream, U32 ip, bool root)
  1171. {
  1172. // goes
  1173. // OP_PUSHFRAME 1
  1174. // name expr
  1175. // OP_PUSH 1
  1176. // args... PUSH
  1177. // OP_CREATE_OBJECT 1
  1178. // parentObject 1
  1179. // isDatablock 1
  1180. // internalName 1
  1181. // isSingleton 1
  1182. // lineNumber 1
  1183. // fail point 1
  1184. // for each field, eval
  1185. // OP_ADD_OBJECT (to UINT[0]) 1
  1186. // root? 1
  1187. // add all the sub objects.
  1188. // OP_END_OBJECT 1
  1189. // root? 1
  1190. // To fix the stack issue [7/9/2007 Black]
  1191. // OP_FINISH_OBJECT <-- fail point jumps to this opcode
  1192. S32 count = 2; // 2 OP_PUSH's
  1193. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1194. count++;
  1195. codeStream.emit(OP_PUSH_FRAME);
  1196. codeStream.emit(count);
  1197. ip = classNameExpr->compile(codeStream, ip, TypeReqString);
  1198. codeStream.emit(OP_PUSH);
  1199. ip = objectNameExpr->compile(codeStream, ip, TypeReqString);
  1200. codeStream.emit(OP_PUSH);
  1201. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1202. {
  1203. TypeReq walkType = exprWalk->getPreferredType();
  1204. if (walkType == TypeReqNone) walkType = TypeReqString;
  1205. ip = exprWalk->compile(codeStream, ip, walkType);
  1206. codeStream.emit(OP_PUSH);
  1207. }
  1208. codeStream.emit(OP_CREATE_OBJECT);
  1209. codeStream.emitSTE(parentObject);
  1210. codeStream.emit(isDatablock);
  1211. codeStream.emit(isClassNameInternal);
  1212. codeStream.emit(isSingleton);
  1213. codeStream.emit(dbgLineNumber);
  1214. const U32 failIp = codeStream.emit(0);
  1215. for (SlotAssignNode* slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode*)slotWalk->getNext())
  1216. ip = slotWalk->compile(codeStream, ip, TypeReqNone);
  1217. codeStream.emit(OP_ADD_OBJECT);
  1218. codeStream.emit(root);
  1219. for (ObjectDeclNode* objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode*)objectWalk->getNext())
  1220. ip = objectWalk->compileSubObject(codeStream, ip, false);
  1221. codeStream.emit(OP_END_OBJECT);
  1222. codeStream.emit(root || isDatablock);
  1223. // Added to fix the object creation issue [7/9/2007 Black]
  1224. failOffset = codeStream.emit(OP_FINISH_OBJECT);
  1225. codeStream.patch(failIp, failOffset);
  1226. return codeStream.tell();
  1227. }
  1228. U32 ObjectDeclNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1229. {
  1230. // root object decl does:
  1231. // push 0 onto the UINT stack OP_LOADIMMED_UINT
  1232. // precompiles the subObject(true)
  1233. // UINT stack now has object id
  1234. // type conv to type
  1235. codeStream.emit(OP_LOADIMMED_UINT);
  1236. codeStream.emit(0);
  1237. ip = compileSubObject(codeStream, ip, true);
  1238. if (type == TypeReqNone)
  1239. codeStream.emit(OP_POP_STK);
  1240. return codeStream.tell();
  1241. }
  1242. TypeReq ObjectDeclNode::getPreferredType()
  1243. {
  1244. return TypeReqUInt;
  1245. }
  1246. //------------------------------------------------------------
  1247. U32 FunctionDeclStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  1248. {
  1249. // OP_FUNC_DECL
  1250. // func name
  1251. // namespace
  1252. // package
  1253. // hasBody?
  1254. // func end ip
  1255. // argc
  1256. // ident array[argc]
  1257. // code
  1258. // OP_RETURN_VOID
  1259. setCurrentStringTable(&getFunctionStringTable());
  1260. setCurrentFloatTable(&getFunctionFloatTable());
  1261. FuncVars vars;
  1262. gFuncVars = &vars;
  1263. argc = 0;
  1264. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1265. {
  1266. precompileIdent(walk->varName);
  1267. getFuncVars(dbgLineNumber)->assign(walk->varName, TypeReqNone, dbgLineNumber);
  1268. argc++;
  1269. }
  1270. CodeBlock::smInFunction = true;
  1271. precompileIdent(fnName);
  1272. precompileIdent(nameSpace);
  1273. precompileIdent(package);
  1274. CodeBlock::smInFunction = false;
  1275. codeStream.emit(OP_FUNC_DECL);
  1276. codeStream.emitSTE(fnName);
  1277. codeStream.emitSTE(nameSpace);
  1278. codeStream.emitSTE(package);
  1279. codeStream.emit(U32(bool(stmts != NULL) ? 1 : 0) + U32(dbgLineNumber << 1));
  1280. const U32 endIp = codeStream.emit(0);
  1281. codeStream.emit(argc);
  1282. const U32 localNumVarsIP = codeStream.emit(0);
  1283. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1284. {
  1285. StringTableEntry name = walk->varName;
  1286. codeStream.emit(getFuncVars(dbgLineNumber)->lookup(name, dbgLineNumber));
  1287. }
  1288. CodeBlock::smInFunction = true;
  1289. ip = compileBlock(stmts, codeStream, ip);
  1290. // Add break so breakpoint can be set at closing brace or
  1291. // in empty function.
  1292. addBreakLine(codeStream);
  1293. CodeBlock::smInFunction = false;
  1294. codeStream.emit(OP_RETURN_VOID);
  1295. codeStream.patch(localNumVarsIP, getFuncVars(dbgLineNumber)->count());
  1296. codeStream.patch(endIp, codeStream.tell());
  1297. setCurrentStringTable(&getGlobalStringTable());
  1298. setCurrentFloatTable(&getGlobalFloatTable());
  1299. // map local variables to registers for this function.
  1300. // Note we have to map these in order because the table itself is ordered by the register id.
  1301. CompilerLocalVariableToRegisterMappingTable* tbl = &getFunctionVariableMappingTable();
  1302. for (size_t i = 0; i < gFuncVars->variableNameMap.size(); ++i)
  1303. {
  1304. StringTableEntry varName = gFuncVars->variableNameMap[i];
  1305. tbl->add(fnName, nameSpace, varName);
  1306. }
  1307. gFuncVars = gIsEvalCompile ? &gEvalFuncVars : &gGlobalScopeFuncVars;
  1308. return ip;
  1309. }