astNodes.cpp 40 KB

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