astNodes.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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); // todo: figure out better way to codegen this so we don't rely on STR
  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. // TODO: What if we do other optimizations and this doesn't work for it..this
  462. // so far only works for simple MOD optimizations...
  463. if (optimize())
  464. right = optimizedNode;
  465. getSubTypeOperand();
  466. if (operand == OP_OR || operand == OP_AND)
  467. {
  468. ip = left->compile(codeStream, ip, subType);
  469. codeStream.emit(operand == OP_OR ? OP_JMPIF_NP : OP_JMPIFNOT_NP);
  470. U32 jmpIp = codeStream.emit(0);
  471. ip = right->compile(codeStream, ip, subType);
  472. codeStream.patch(jmpIp, ip);
  473. }
  474. else
  475. {
  476. ip = right->compile(codeStream, ip, subType);
  477. ip = left->compile(codeStream, ip, subType);
  478. codeStream.emit(operand);
  479. }
  480. return codeStream.tell();
  481. }
  482. TypeReq IntBinaryExprNode::getPreferredType()
  483. {
  484. return TypeReqUInt;
  485. }
  486. //------------------------------------------------------------
  487. U32 StreqExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  488. {
  489. // eval str left
  490. // OP_ADVANCE_STR_NUL
  491. // eval str right
  492. // OP_COMPARE_STR
  493. // optional conversion
  494. ip = left->compile(codeStream, ip, TypeReqString);
  495. ip = right->compile(codeStream, ip, TypeReqString);
  496. codeStream.emit(OP_COMPARE_STR);
  497. if (!eq)
  498. codeStream.emit(OP_NOT);
  499. return codeStream.tell();
  500. }
  501. TypeReq StreqExprNode::getPreferredType()
  502. {
  503. return TypeReqUInt;
  504. }
  505. //------------------------------------------------------------
  506. U32 StrcatExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  507. {
  508. ip = left->compile(codeStream, ip, TypeReqString);
  509. if (appendChar)
  510. {
  511. codeStream.emit(OP_ADVANCE_STR_APPENDCHAR);
  512. codeStream.emit(appendChar);
  513. }
  514. ip = right->compile(codeStream, ip, TypeReqString);
  515. codeStream.emit(OP_REWIND_STR);
  516. return codeStream.tell();
  517. }
  518. TypeReq StrcatExprNode::getPreferredType()
  519. {
  520. return TypeReqString;
  521. }
  522. //------------------------------------------------------------
  523. U32 CommaCatExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  524. {
  525. ip = left->compile(codeStream, ip, TypeReqString);
  526. codeStream.emit(OP_ADVANCE_STR_APPENDCHAR);
  527. codeStream.emit('_');
  528. ip = right->compile(codeStream, ip, TypeReqString);
  529. codeStream.emit(OP_REWIND_STR);
  530. // At this point the stack has the concatenated string.
  531. // But we're paranoid, so accept (but whine) if we get an oddity...
  532. if (type == TypeReqUInt || type == TypeReqFloat)
  533. Con::warnf(ConsoleLogEntry::General, "%s (%d): converting comma string to a number... probably wrong.", dbgFileName, dbgLineNumber);
  534. return codeStream.tell();
  535. }
  536. TypeReq CommaCatExprNode::getPreferredType()
  537. {
  538. return TypeReqString;
  539. }
  540. //------------------------------------------------------------
  541. U32 IntUnaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  542. {
  543. integer = true;
  544. TypeReq prefType = expr->getPreferredType();
  545. if (op == '!' && (prefType == TypeReqFloat || prefType == TypeReqString))
  546. integer = false;
  547. ip = expr->compile(codeStream, ip, integer ? TypeReqUInt : TypeReqFloat);
  548. if (op == '!')
  549. codeStream.emit(integer ? OP_NOT : OP_NOTF);
  550. else if (op == '~')
  551. codeStream.emit(OP_ONESCOMPLEMENT);
  552. return codeStream.tell();
  553. }
  554. TypeReq IntUnaryExprNode::getPreferredType()
  555. {
  556. return TypeReqUInt;
  557. }
  558. //------------------------------------------------------------
  559. U32 FloatUnaryExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  560. {
  561. ip = expr->compile(codeStream, ip, TypeReqFloat);
  562. codeStream.emit(OP_NEG);
  563. return codeStream.tell();
  564. }
  565. TypeReq FloatUnaryExprNode::getPreferredType()
  566. {
  567. return TypeReqFloat;
  568. }
  569. //------------------------------------------------------------
  570. U32 VarNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  571. {
  572. // if this has an arrayIndex...
  573. // OP_LOADIMMED_IDENT
  574. // varName
  575. // OP_ADVANCE_STR
  576. // evaluate arrayIndex TypeReqString
  577. // OP_REWIND_STR
  578. // OP_SETCURVAR_ARRAY
  579. // OP_LOADVAR (type)
  580. // else
  581. // OP_SETCURVAR
  582. // varName
  583. // OP_LOADVAR (type)
  584. if (type == TypeReqNone)
  585. return codeStream.tell();
  586. precompileIdent(varName);
  587. bool oldVariables = arrayIndex || varName[0] == '$';
  588. if (oldVariables)
  589. {
  590. codeStream.emit(arrayIndex ? OP_LOADIMMED_IDENT : OP_SETCURVAR);
  591. codeStream.emitSTE(varName);
  592. if (arrayIndex)
  593. {
  594. //codeStream.emit(OP_ADVANCE_STR);
  595. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  596. codeStream.emit(OP_REWIND_STR);
  597. codeStream.emit(OP_SETCURVAR_ARRAY);
  598. codeStream.emit(OP_POP_STK);
  599. }
  600. switch (type)
  601. {
  602. case TypeReqUInt:
  603. codeStream.emit(OP_LOADVAR_UINT);
  604. break;
  605. case TypeReqFloat:
  606. codeStream.emit(OP_LOADVAR_FLT);
  607. break;
  608. case TypeReqString:
  609. codeStream.emit(OP_LOADVAR_STR);
  610. break;
  611. case TypeReqNone:
  612. break;
  613. default:
  614. break;
  615. }
  616. }
  617. else
  618. {
  619. switch (type)
  620. {
  621. case TypeReqUInt: codeStream.emit(OP_LOAD_LOCAL_VAR_UINT); break;
  622. case TypeReqFloat: codeStream.emit(OP_LOAD_LOCAL_VAR_FLT); break;
  623. default: codeStream.emit(OP_LOAD_LOCAL_VAR_STR);
  624. }
  625. codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber));
  626. }
  627. return codeStream.tell();
  628. }
  629. TypeReq VarNode::getPreferredType()
  630. {
  631. bool oldVariables = arrayIndex || varName[0] == '$';
  632. return oldVariables ? TypeReqNone : getFuncVars(dbgLineNumber)->lookupType(varName, dbgLineNumber);
  633. }
  634. //------------------------------------------------------------
  635. U32 IntNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  636. {
  637. if (type == TypeReqString)
  638. index = getCurrentStringTable()->addIntString(value);
  639. else if (type == TypeReqFloat)
  640. index = getCurrentFloatTable()->add(value);
  641. switch (type)
  642. {
  643. case TypeReqUInt:
  644. codeStream.emit(OP_LOADIMMED_UINT);
  645. codeStream.emit(value);
  646. break;
  647. case TypeReqString:
  648. codeStream.emit(OP_LOADIMMED_STR);
  649. codeStream.emit(index);
  650. break;
  651. case TypeReqFloat:
  652. codeStream.emit(OP_LOADIMMED_FLT);
  653. codeStream.emit(index);
  654. break;
  655. case TypeReqNone:
  656. break;
  657. }
  658. return codeStream.tell();
  659. }
  660. TypeReq IntNode::getPreferredType()
  661. {
  662. return TypeReqUInt;
  663. }
  664. //------------------------------------------------------------
  665. U32 FloatNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  666. {
  667. if (type == TypeReqString)
  668. index = getCurrentStringTable()->addFloatString(value);
  669. else if (type == TypeReqFloat)
  670. index = getCurrentFloatTable()->add(value);
  671. switch (type)
  672. {
  673. case TypeReqUInt:
  674. codeStream.emit(OP_LOADIMMED_UINT);
  675. codeStream.emit(U32(value));
  676. break;
  677. case TypeReqString:
  678. codeStream.emit(OP_LOADIMMED_STR);
  679. codeStream.emit(index);
  680. break;
  681. case TypeReqFloat:
  682. codeStream.emit(OP_LOADIMMED_FLT);
  683. codeStream.emit(index);
  684. break;
  685. case TypeReqNone:
  686. break;
  687. }
  688. return codeStream.tell();
  689. }
  690. TypeReq FloatNode::getPreferredType()
  691. {
  692. return TypeReqFloat;
  693. }
  694. //------------------------------------------------------------
  695. U32 StrConstNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  696. {
  697. // Early out for documentation block.
  698. if (doc)
  699. {
  700. index = getCurrentStringTable()->add(str, true, tag);
  701. }
  702. else if (type == TypeReqString)
  703. {
  704. index = getCurrentStringTable()->add(str, true, tag);
  705. }
  706. else if (type != TypeReqNone)
  707. {
  708. fVal = consoleStringToNumber(str, dbgFileName, dbgLineNumber);
  709. if (type == TypeReqFloat)
  710. {
  711. index = getCurrentFloatTable()->add(fVal);
  712. }
  713. }
  714. // If this is a DOCBLOCK, then process w/ appropriate op...
  715. if (doc)
  716. {
  717. codeStream.emit(OP_DOCBLOCK_STR);
  718. codeStream.emit(index);
  719. return ip;
  720. }
  721. // Otherwise, deal with it normally as a string literal case.
  722. switch (type)
  723. {
  724. case TypeReqString:
  725. codeStream.emit(tag ? OP_TAG_TO_STR : OP_LOADIMMED_STR);
  726. codeStream.emit(index);
  727. break;
  728. case TypeReqUInt:
  729. codeStream.emit(OP_LOADIMMED_UINT);
  730. codeStream.emit(U32(fVal));
  731. break;
  732. case TypeReqFloat:
  733. codeStream.emit(OP_LOADIMMED_FLT);
  734. codeStream.emit(index);
  735. break;
  736. case TypeReqNone:
  737. break;
  738. }
  739. return codeStream.tell();
  740. }
  741. TypeReq StrConstNode::getPreferredType()
  742. {
  743. return TypeReqString;
  744. }
  745. //------------------------------------------------------------
  746. U32 ConstantNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  747. {
  748. if (type == TypeReqString)
  749. {
  750. precompileIdent(value);
  751. }
  752. else if (type != TypeReqNone)
  753. {
  754. fVal = consoleStringToNumber(value, dbgFileName, dbgLineNumber);
  755. if (type == TypeReqFloat)
  756. index = getCurrentFloatTable()->add(fVal);
  757. }
  758. switch (type)
  759. {
  760. case TypeReqString:
  761. codeStream.emit(OP_LOADIMMED_IDENT);
  762. codeStream.emitSTE(value);
  763. break;
  764. case TypeReqUInt:
  765. codeStream.emit(OP_LOADIMMED_UINT);
  766. codeStream.emit(U32(fVal));
  767. break;
  768. case TypeReqFloat:
  769. codeStream.emit(OP_LOADIMMED_FLT);
  770. codeStream.emit(index);
  771. break;
  772. case TypeReqNone:
  773. break;
  774. }
  775. return ip;
  776. }
  777. TypeReq ConstantNode::getPreferredType()
  778. {
  779. return TypeReqString;
  780. }
  781. //------------------------------------------------------------
  782. U32 AssignExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  783. {
  784. subType = expr->getPreferredType();
  785. if (subType == TypeReqNone)
  786. subType = type;
  787. if (subType == TypeReqNone)
  788. subType = TypeReqString;
  789. // if it's an array expr, the formula is:
  790. // eval expr
  791. // (push and pop if it's TypeReqString) OP_ADVANCE_STR
  792. // OP_LOADIMMED_IDENT
  793. // varName
  794. // OP_ADVANCE_STR
  795. // eval array
  796. // OP_REWIND_STR
  797. // OP_SETCURVAR_ARRAY_CREATE
  798. // OP_TERMINATE_REWIND_STR
  799. // OP_SAVEVAR
  800. //else
  801. // eval expr
  802. // OP_SETCURVAR_CREATE
  803. // varname
  804. // OP_SAVEVAR
  805. precompileIdent(varName);
  806. ip = expr->compile(codeStream, ip, subType);
  807. bool oldVariables = arrayIndex || varName[0] == '$';
  808. if (oldVariables)
  809. {
  810. if (arrayIndex)
  811. {
  812. //if (subType == TypeReqString)
  813. // codeStream.emit(OP_ADVANCE_STR);
  814. codeStream.emit(OP_LOADIMMED_IDENT);
  815. codeStream.emitSTE(varName);
  816. //codeStream.emit(OP_ADVANCE_STR);
  817. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  818. codeStream.emit(OP_REWIND_STR);
  819. codeStream.emit(OP_SETCURVAR_ARRAY_CREATE);
  820. if (type == TypeReqNone)
  821. codeStream.emit(OP_POP_STK);
  822. }
  823. else
  824. {
  825. codeStream.emit(OP_SETCURVAR_CREATE);
  826. codeStream.emitSTE(varName);
  827. }
  828. switch (subType)
  829. {
  830. case TypeReqString: codeStream.emit(OP_SAVEVAR_STR); break;
  831. case TypeReqUInt: codeStream.emit(OP_SAVEVAR_UINT); break;
  832. case TypeReqFloat: codeStream.emit(OP_SAVEVAR_FLT); break;
  833. }
  834. }
  835. else
  836. {
  837. switch (subType)
  838. {
  839. case TypeReqUInt: codeStream.emit(OP_SAVE_LOCAL_VAR_UINT); break;
  840. case TypeReqFloat: codeStream.emit(OP_SAVE_LOCAL_VAR_FLT); break;
  841. default: codeStream.emit(OP_SAVE_LOCAL_VAR_STR);
  842. }
  843. codeStream.emit(getFuncVars(dbgLineNumber)->assign(varName, subType == TypeReqNone ? TypeReqString : subType, dbgLineNumber));
  844. }
  845. if (type == TypeReqNone)
  846. codeStream.emit(OP_POP_STK);
  847. return ip;
  848. }
  849. TypeReq AssignExprNode::getPreferredType()
  850. {
  851. return expr->getPreferredType();
  852. }
  853. //------------------------------------------------------------
  854. static void getAssignOpTypeOp(S32 op, TypeReq& type, U32& operand)
  855. {
  856. switch (op)
  857. {
  858. case opPLUSPLUS:
  859. TORQUE_CASE_FALLTHROUGH;
  860. case '+':
  861. type = TypeReqFloat;
  862. operand = OP_ADD;
  863. break;
  864. case opMINUSMINUS:
  865. TORQUE_CASE_FALLTHROUGH;
  866. case '-':
  867. type = TypeReqFloat;
  868. operand = OP_SUB;
  869. break;
  870. case '*':
  871. type = TypeReqFloat;
  872. operand = OP_MUL;
  873. break;
  874. case '/':
  875. type = TypeReqFloat;
  876. operand = OP_DIV;
  877. break;
  878. case '%':
  879. type = TypeReqUInt;
  880. operand = OP_MOD;
  881. break;
  882. case '&':
  883. type = TypeReqUInt;
  884. operand = OP_BITAND;
  885. break;
  886. case '^':
  887. type = TypeReqUInt;
  888. operand = OP_XOR;
  889. break;
  890. case '|':
  891. type = TypeReqUInt;
  892. operand = OP_BITOR;
  893. break;
  894. case opSHL:
  895. type = TypeReqUInt;
  896. operand = OP_SHL;
  897. break;
  898. case opSHR:
  899. type = TypeReqUInt;
  900. operand = OP_SHR;
  901. break;
  902. default:
  903. AssertFatal(false, "Invalid opcode on operation expression");
  904. }
  905. }
  906. U32 AssignOpExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  907. {
  908. // goes like this...
  909. // eval expr as float or int
  910. // if there's an arrayIndex
  911. // OP_LOADIMMED_IDENT
  912. // varName
  913. // OP_ADVANCE_STR
  914. // eval arrayIndex stringwise
  915. // OP_REWIND_STR
  916. // OP_SETCURVAR_ARRAY_CREATE
  917. // else
  918. // OP_SETCURVAR_CREATE
  919. // varName
  920. // OP_LOADVAR_FLT or UINT
  921. // operand
  922. // OP_SAVEVAR_FLT or UINT
  923. // conversion OP if necessary.
  924. getAssignOpTypeOp(op, subType, operand);
  925. precompileIdent(varName);
  926. bool oldVariables = arrayIndex || varName[0] == '$';
  927. if (op == opPLUSPLUS && !oldVariables)
  928. {
  929. const S32 varIdx = getFuncVars(dbgLineNumber)->assign(varName, TypeReqFloat, dbgLineNumber);
  930. codeStream.emit(OP_INC);
  931. codeStream.emit(varIdx);
  932. }
  933. else
  934. {
  935. ip = expr->compile(codeStream, ip, subType);
  936. if (oldVariables)
  937. {
  938. if (!arrayIndex)
  939. {
  940. codeStream.emit(OP_SETCURVAR_CREATE);
  941. codeStream.emitSTE(varName);
  942. }
  943. else
  944. {
  945. codeStream.emit(OP_LOADIMMED_IDENT);
  946. codeStream.emitSTE(varName);
  947. //codeStream.emit(OP_ADVANCE_STR);
  948. ip = arrayIndex->compile(codeStream, ip, TypeReqString);
  949. codeStream.emit(OP_REWIND_STR);
  950. codeStream.emit(OP_SETCURVAR_ARRAY_CREATE);
  951. if (type == TypeReqNone)
  952. codeStream.emit(OP_POP_STK);
  953. }
  954. codeStream.emit((subType == TypeReqFloat) ? OP_LOADVAR_FLT : OP_LOADVAR_UINT);
  955. codeStream.emit(operand);
  956. codeStream.emit((subType == TypeReqFloat) ? OP_SAVEVAR_FLT : OP_SAVEVAR_UINT);
  957. }
  958. else
  959. {
  960. const bool isFloat = subType == TypeReqFloat;
  961. const S32 varIdx = getFuncVars(dbgLineNumber)->assign(varName, subType == TypeReqNone ? TypeReqString : subType, dbgLineNumber);
  962. codeStream.emit(isFloat ? OP_LOAD_LOCAL_VAR_FLT : OP_LOAD_LOCAL_VAR_UINT);
  963. codeStream.emit(varIdx);
  964. codeStream.emit(operand);
  965. codeStream.emit(isFloat ? OP_SAVE_LOCAL_VAR_FLT : OP_SAVE_LOCAL_VAR_UINT);
  966. codeStream.emit(varIdx);
  967. }
  968. if (type == TypeReqNone)
  969. codeStream.emit(OP_POP_STK);
  970. }
  971. return codeStream.tell();
  972. }
  973. TypeReq AssignOpExprNode::getPreferredType()
  974. {
  975. getAssignOpTypeOp(op, subType, operand);
  976. return subType;
  977. }
  978. //------------------------------------------------------------
  979. U32 TTagSetStmtNode::compileStmt(CodeStream&, U32 ip)
  980. {
  981. return ip;
  982. }
  983. //------------------------------------------------------------
  984. U32 TTagDerefNode::compile(CodeStream&, U32 ip, TypeReq)
  985. {
  986. return ip;
  987. }
  988. TypeReq TTagDerefNode::getPreferredType()
  989. {
  990. return TypeReqNone;
  991. }
  992. //------------------------------------------------------------
  993. U32 TTagExprNode::compile(CodeStream&, U32 ip, TypeReq)
  994. {
  995. return ip;
  996. }
  997. TypeReq TTagExprNode::getPreferredType()
  998. {
  999. return TypeReqNone;
  1000. }
  1001. //------------------------------------------------------------
  1002. U32 FuncCallExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1003. {
  1004. // OP_PUSH_FRAME
  1005. // arg OP_PUSH arg OP_PUSH arg OP_PUSH
  1006. // eval all the args, then call the function.
  1007. // OP_CALLFUNC
  1008. // function
  1009. // namespace
  1010. // isDot
  1011. precompileIdent(funcName);
  1012. precompileIdent(nameSpace);
  1013. S32 count = 0;
  1014. for (ExprNode* walk = args; walk; walk = static_cast<ExprNode*>(walk->getNext()))
  1015. count++;
  1016. codeStream.emit(OP_PUSH_FRAME);
  1017. codeStream.emit(count);
  1018. for (ExprNode* walk = args; walk; walk = static_cast<ExprNode*>(walk->getNext()))
  1019. {
  1020. TypeReq walkType = walk->getPreferredType();
  1021. if (walkType == TypeReqNone)
  1022. walkType = TypeReqString;
  1023. ip = walk->compile(codeStream, ip, walkType);
  1024. codeStream.emit(OP_PUSH);
  1025. }
  1026. codeStream.emit(OP_CALLFUNC);
  1027. codeStream.emitSTE(funcName);
  1028. codeStream.emitSTE(nameSpace);
  1029. codeStream.emit(callType);
  1030. if (type == TypeReqNone)
  1031. codeStream.emit(OP_POP_STK);
  1032. return codeStream.tell();
  1033. }
  1034. TypeReq FuncCallExprNode::getPreferredType()
  1035. {
  1036. return TypeReqString;
  1037. }
  1038. //------------------------------------------------------------
  1039. U32 AssertCallExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1040. {
  1041. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  1042. messageIndex = getCurrentStringTable()->add(message, true, false);
  1043. ip = testExpr->compile(codeStream, ip, TypeReqUInt);
  1044. codeStream.emit(OP_ASSERT);
  1045. codeStream.emit(messageIndex);
  1046. #endif
  1047. return codeStream.tell();
  1048. }
  1049. TypeReq AssertCallExprNode::getPreferredType()
  1050. {
  1051. return TypeReqNone;
  1052. }
  1053. //------------------------------------------------------------
  1054. U32 SlotAccessNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1055. {
  1056. if (type == TypeReqNone)
  1057. return ip;
  1058. precompileIdent(slotName);
  1059. if (arrayExpr)
  1060. {
  1061. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1062. }
  1063. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1064. codeStream.emit(OP_SETCUROBJECT);
  1065. codeStream.emit(OP_SETCURFIELD);
  1066. codeStream.emitSTE(slotName);
  1067. if (arrayExpr)
  1068. {
  1069. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1070. codeStream.emit(OP_POP_STK);
  1071. }
  1072. switch (type)
  1073. {
  1074. case TypeReqUInt:
  1075. codeStream.emit(OP_LOADFIELD_UINT);
  1076. break;
  1077. case TypeReqFloat:
  1078. codeStream.emit(OP_LOADFIELD_FLT);
  1079. break;
  1080. case TypeReqString:
  1081. codeStream.emit(OP_LOADFIELD_STR);
  1082. break;
  1083. case TypeReqNone:
  1084. break;
  1085. }
  1086. return codeStream.tell();
  1087. }
  1088. TypeReq SlotAccessNode::getPreferredType()
  1089. {
  1090. return TypeReqNone;
  1091. }
  1092. //-----------------------------------------------------------------------------
  1093. U32 InternalSlotAccessNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1094. {
  1095. if (type == TypeReqNone)
  1096. return ip;
  1097. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1098. codeStream.emit(OP_SETCUROBJECT);
  1099. ip = slotExpr->compile(codeStream, ip, TypeReqString);
  1100. codeStream.emit(OP_SETCUROBJECT_INTERNAL);
  1101. codeStream.emit(recurse);
  1102. return codeStream.tell();
  1103. }
  1104. TypeReq InternalSlotAccessNode::getPreferredType()
  1105. {
  1106. return TypeReqUInt;
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. U32 SlotAssignNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1110. {
  1111. // first eval the expression TypeReqString
  1112. // if it's an array:
  1113. // if OP_ADVANCE_STR 1
  1114. // eval array
  1115. // OP_ADVANCE_STR 1
  1116. // evaluate object expr
  1117. // OP_SETCUROBJECT 1
  1118. // OP_SETCURFIELD 1
  1119. // fieldName 1
  1120. // OP_TERMINATE_REWIND_STR 1
  1121. // OP_SETCURFIELDARRAY 1
  1122. // OP_TERMINATE_REWIND_STR 1
  1123. // else
  1124. // OP_ADVANCE_STR
  1125. // evaluate object expr
  1126. // OP_SETCUROBJECT
  1127. // OP_SETCURFIELD
  1128. // fieldName
  1129. // OP_TERMINATE_REWIND_STR
  1130. // OP_SAVEFIELD
  1131. // convert to return type if necessary.
  1132. precompileIdent(slotName);
  1133. ip = valueExpr->compile(codeStream, ip, TypeReqString);
  1134. if (arrayExpr)
  1135. {
  1136. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1137. }
  1138. if (objectExpr)
  1139. {
  1140. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1141. codeStream.emit(OP_SETCUROBJECT);
  1142. }
  1143. else
  1144. codeStream.emit(OP_SETCUROBJECT_NEW);
  1145. codeStream.emit(OP_SETCURFIELD);
  1146. codeStream.emitSTE(slotName);
  1147. if (arrayExpr)
  1148. {
  1149. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1150. codeStream.emit(OP_POP_STK);
  1151. }
  1152. codeStream.emit(OP_SAVEFIELD_STR);
  1153. if (typeID != -1)
  1154. {
  1155. codeStream.emit(OP_SETCURFIELD_TYPE);
  1156. codeStream.emit(typeID);
  1157. }
  1158. if (type == TypeReqNone)
  1159. codeStream.emit(OP_POP_STK);
  1160. return codeStream.tell();
  1161. }
  1162. TypeReq SlotAssignNode::getPreferredType()
  1163. {
  1164. return TypeReqString;
  1165. }
  1166. //------------------------------------------------------------
  1167. U32 SlotAssignOpNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1168. {
  1169. // first eval the expression as its type
  1170. // if it's an array:
  1171. // eval array
  1172. // OP_ADVANCE_STR
  1173. // evaluate object expr
  1174. // OP_SETCUROBJECT
  1175. // OP_SETCURFIELD
  1176. // fieldName
  1177. // OP_TERMINATE_REWIND_STR
  1178. // OP_SETCURFIELDARRAY
  1179. // else
  1180. // evaluate object expr
  1181. // OP_SETCUROBJECT
  1182. // OP_SETCURFIELD
  1183. // fieldName
  1184. // OP_LOADFIELD of appropriate type
  1185. // operand
  1186. // OP_SAVEFIELD of appropriate type
  1187. // convert to return type if necessary.
  1188. getAssignOpTypeOp(op, subType, operand);
  1189. precompileIdent(slotName);
  1190. ip = valueExpr->compile(codeStream, ip, subType);
  1191. if (arrayExpr)
  1192. {
  1193. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1194. }
  1195. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1196. codeStream.emit(OP_SETCUROBJECT);
  1197. codeStream.emit(OP_SETCURFIELD);
  1198. codeStream.emitSTE(slotName);
  1199. if (arrayExpr)
  1200. {
  1201. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1202. if (subType == TypeReqNone)
  1203. codeStream.emit(OP_POP_STK);
  1204. }
  1205. codeStream.emit((subType == TypeReqFloat) ? OP_LOADFIELD_FLT : OP_LOADFIELD_UINT);
  1206. codeStream.emit(operand);
  1207. codeStream.emit((subType == TypeReqFloat) ? OP_SAVEFIELD_FLT : OP_SAVEFIELD_UINT);
  1208. if (type == TypeReqNone)
  1209. codeStream.emit(OP_POP_STK);
  1210. return codeStream.tell();
  1211. }
  1212. TypeReq SlotAssignOpNode::getPreferredType()
  1213. {
  1214. getAssignOpTypeOp(op, subType, operand);
  1215. return subType;
  1216. }
  1217. //------------------------------------------------------------
  1218. U32 ObjectDeclNode::compileSubObject(CodeStream& codeStream, U32 ip, bool root)
  1219. {
  1220. // goes
  1221. // OP_PUSHFRAME 1
  1222. // name expr
  1223. // OP_PUSH 1
  1224. // args... PUSH
  1225. // OP_CREATE_OBJECT 1
  1226. // parentObject 1
  1227. // isDatablock 1
  1228. // internalName 1
  1229. // isSingleton 1
  1230. // lineNumber 1
  1231. // fail point 1
  1232. // for each field, eval
  1233. // OP_ADD_OBJECT (to UINT[0]) 1
  1234. // root? 1
  1235. // add all the sub objects.
  1236. // OP_END_OBJECT 1
  1237. // root? 1
  1238. // To fix the stack issue [7/9/2007 Black]
  1239. // OP_FINISH_OBJECT <-- fail point jumps to this opcode
  1240. S32 count = 2; // 2 OP_PUSH's
  1241. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1242. count++;
  1243. codeStream.emit(OP_PUSH_FRAME);
  1244. codeStream.emit(count);
  1245. ip = classNameExpr->compile(codeStream, ip, TypeReqString);
  1246. codeStream.emit(OP_PUSH);
  1247. ip = objectNameExpr->compile(codeStream, ip, TypeReqString);
  1248. codeStream.emit(OP_PUSH);
  1249. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1250. {
  1251. TypeReq walkType = exprWalk->getPreferredType();
  1252. if (walkType == TypeReqNone) walkType = TypeReqString;
  1253. ip = exprWalk->compile(codeStream, ip, walkType);
  1254. codeStream.emit(OP_PUSH);
  1255. }
  1256. codeStream.emit(OP_CREATE_OBJECT);
  1257. codeStream.emitSTE(parentObject);
  1258. codeStream.emit(isDatablock);
  1259. codeStream.emit(isClassNameInternal);
  1260. codeStream.emit(isSingleton);
  1261. codeStream.emit(dbgLineNumber);
  1262. const U32 failIp = codeStream.emit(0);
  1263. for (SlotAssignNode* slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode*)slotWalk->getNext())
  1264. ip = slotWalk->compile(codeStream, ip, TypeReqNone);
  1265. codeStream.emit(OP_ADD_OBJECT);
  1266. codeStream.emit(root);
  1267. for (ObjectDeclNode* objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode*)objectWalk->getNext())
  1268. ip = objectWalk->compileSubObject(codeStream, ip, false);
  1269. codeStream.emit(OP_END_OBJECT);
  1270. codeStream.emit(root || isDatablock);
  1271. // Added to fix the object creation issue [7/9/2007 Black]
  1272. failOffset = codeStream.emit(OP_FINISH_OBJECT);
  1273. codeStream.patch(failIp, failOffset);
  1274. return codeStream.tell();
  1275. }
  1276. U32 ObjectDeclNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1277. {
  1278. // root object decl does:
  1279. // push 0 onto the UINT stack OP_LOADIMMED_UINT
  1280. // precompiles the subObject(true)
  1281. // UINT stack now has object id
  1282. // type conv to type
  1283. codeStream.emit(OP_LOADIMMED_UINT);
  1284. codeStream.emit(0);
  1285. ip = compileSubObject(codeStream, ip, true);
  1286. if (type == TypeReqNone)
  1287. codeStream.emit(OP_POP_STK);
  1288. return codeStream.tell();
  1289. }
  1290. TypeReq ObjectDeclNode::getPreferredType()
  1291. {
  1292. return TypeReqUInt;
  1293. }
  1294. //------------------------------------------------------------
  1295. U32 FunctionDeclStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  1296. {
  1297. // OP_FUNC_DECL
  1298. // func name
  1299. // namespace
  1300. // package
  1301. // hasBody?
  1302. // func end ip
  1303. // argc
  1304. // ident array[argc]
  1305. // code
  1306. // OP_RETURN_VOID
  1307. setCurrentStringTable(&getFunctionStringTable());
  1308. setCurrentFloatTable(&getFunctionFloatTable());
  1309. FuncVars vars;
  1310. gFuncVars = &vars;
  1311. argc = 0;
  1312. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1313. {
  1314. precompileIdent(walk->varName);
  1315. getFuncVars(dbgLineNumber)->assign(walk->varName, TypeReqNone, dbgLineNumber);
  1316. argc++;
  1317. }
  1318. CodeBlock::smInFunction = true;
  1319. precompileIdent(fnName);
  1320. precompileIdent(nameSpace);
  1321. precompileIdent(package);
  1322. CodeBlock::smInFunction = false;
  1323. codeStream.emit(OP_FUNC_DECL);
  1324. codeStream.emitSTE(fnName);
  1325. codeStream.emitSTE(nameSpace);
  1326. codeStream.emitSTE(package);
  1327. codeStream.emit(U32(bool(stmts != NULL) ? 1 : 0) + U32(dbgLineNumber << 1));
  1328. const U32 endIp = codeStream.emit(0);
  1329. codeStream.emit(argc);
  1330. const U32 localNumVarsIP = codeStream.emit(0);
  1331. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1332. {
  1333. StringTableEntry name = walk->varName;
  1334. codeStream.emit(getFuncVars(dbgLineNumber)->lookup(name, dbgLineNumber));
  1335. }
  1336. CodeBlock::smInFunction = true;
  1337. ip = compileBlock(stmts, codeStream, ip);
  1338. // Add break so breakpoint can be set at closing brace or
  1339. // in empty function.
  1340. addBreakLine(codeStream);
  1341. CodeBlock::smInFunction = false;
  1342. codeStream.emit(OP_RETURN_VOID);
  1343. codeStream.patch(localNumVarsIP, getFuncVars(dbgLineNumber)->count());
  1344. codeStream.patch(endIp, codeStream.tell());
  1345. setCurrentStringTable(&getGlobalStringTable());
  1346. setCurrentFloatTable(&getGlobalFloatTable());
  1347. // map local variables to registers for this function.
  1348. CompilerLocalVariableToRegisterMappingTable* tbl = &getFunctionVariableMappingTable();
  1349. for (const auto& pair : gFuncVars->variableNameMap)
  1350. {
  1351. tbl->add(fnName, pair.second, pair.first);
  1352. }
  1353. gFuncVars = NULL;
  1354. return ip;
  1355. }