astNodes.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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. codeStream.emit(OP_POP_STK);
  1068. if (arrayExpr)
  1069. {
  1070. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1071. codeStream.emit(OP_POP_STK);
  1072. }
  1073. switch (type)
  1074. {
  1075. case TypeReqUInt:
  1076. codeStream.emit(OP_LOADFIELD_UINT);
  1077. break;
  1078. case TypeReqFloat:
  1079. codeStream.emit(OP_LOADFIELD_FLT);
  1080. break;
  1081. case TypeReqString:
  1082. codeStream.emit(OP_LOADFIELD_STR);
  1083. break;
  1084. case TypeReqNone:
  1085. break;
  1086. }
  1087. return codeStream.tell();
  1088. }
  1089. TypeReq SlotAccessNode::getPreferredType()
  1090. {
  1091. return TypeReqNone;
  1092. }
  1093. //-----------------------------------------------------------------------------
  1094. U32 InternalSlotAccessNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1095. {
  1096. if (type == TypeReqNone)
  1097. return ip;
  1098. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1099. codeStream.emit(OP_SETCUROBJECT);
  1100. ip = slotExpr->compile(codeStream, ip, TypeReqString);
  1101. codeStream.emit(OP_SETCUROBJECT_INTERNAL);
  1102. codeStream.emit(recurse);
  1103. codeStream.emit(OP_POP_STK);
  1104. return codeStream.tell();
  1105. }
  1106. TypeReq InternalSlotAccessNode::getPreferredType()
  1107. {
  1108. return TypeReqUInt;
  1109. }
  1110. //-----------------------------------------------------------------------------
  1111. U32 SlotAssignNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1112. {
  1113. precompileIdent(slotName);
  1114. ip = valueExpr->compile(codeStream, ip, TypeReqString);
  1115. if (arrayExpr)
  1116. {
  1117. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1118. }
  1119. if (objectExpr)
  1120. {
  1121. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1122. codeStream.emit(OP_SETCUROBJECT);
  1123. }
  1124. else
  1125. codeStream.emit(OP_SETCUROBJECT_NEW);
  1126. codeStream.emit(OP_SETCURFIELD);
  1127. codeStream.emitSTE(slotName);
  1128. if (objectExpr)
  1129. {
  1130. // Don't pop unless we are assigning a field to an object
  1131. // (For initializer fields, we don't wanna pop)
  1132. codeStream.emit(OP_POP_STK);
  1133. }
  1134. if (arrayExpr)
  1135. {
  1136. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1137. codeStream.emit(OP_POP_STK);
  1138. }
  1139. codeStream.emit(OP_SAVEFIELD_STR);
  1140. if (typeID != -1)
  1141. {
  1142. codeStream.emit(OP_SETCURFIELD_TYPE);
  1143. codeStream.emit(typeID);
  1144. }
  1145. if (type == TypeReqNone)
  1146. codeStream.emit(OP_POP_STK);
  1147. return codeStream.tell();
  1148. }
  1149. TypeReq SlotAssignNode::getPreferredType()
  1150. {
  1151. return TypeReqString;
  1152. }
  1153. //------------------------------------------------------------
  1154. U32 SlotAssignOpNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1155. {
  1156. // first eval the expression as its type
  1157. // if it's an array:
  1158. // eval array
  1159. // OP_ADVANCE_STR
  1160. // evaluate object expr
  1161. // OP_SETCUROBJECT
  1162. // OP_SETCURFIELD
  1163. // fieldName
  1164. // OP_TERMINATE_REWIND_STR
  1165. // OP_SETCURFIELDARRAY
  1166. // else
  1167. // evaluate object expr
  1168. // OP_SETCUROBJECT
  1169. // OP_SETCURFIELD
  1170. // fieldName
  1171. // OP_LOADFIELD of appropriate type
  1172. // operand
  1173. // OP_SAVEFIELD of appropriate type
  1174. // convert to return type if necessary.
  1175. getAssignOpTypeOp(op, subType, operand);
  1176. precompileIdent(slotName);
  1177. ip = valueExpr->compile(codeStream, ip, subType);
  1178. if (arrayExpr)
  1179. {
  1180. ip = arrayExpr->compile(codeStream, ip, TypeReqString);
  1181. }
  1182. ip = objectExpr->compile(codeStream, ip, TypeReqString);
  1183. codeStream.emit(OP_SETCUROBJECT);
  1184. codeStream.emit(OP_SETCURFIELD);
  1185. codeStream.emitSTE(slotName);
  1186. codeStream.emit(OP_POP_STK);
  1187. if (arrayExpr)
  1188. {
  1189. codeStream.emit(OP_SETCURFIELD_ARRAY);
  1190. if (subType == TypeReqNone)
  1191. codeStream.emit(OP_POP_STK);
  1192. }
  1193. codeStream.emit((subType == TypeReqFloat) ? OP_LOADFIELD_FLT : OP_LOADFIELD_UINT);
  1194. codeStream.emit(operand);
  1195. codeStream.emit((subType == TypeReqFloat) ? OP_SAVEFIELD_FLT : OP_SAVEFIELD_UINT);
  1196. if (type == TypeReqNone)
  1197. codeStream.emit(OP_POP_STK);
  1198. return codeStream.tell();
  1199. }
  1200. TypeReq SlotAssignOpNode::getPreferredType()
  1201. {
  1202. getAssignOpTypeOp(op, subType, operand);
  1203. return subType;
  1204. }
  1205. //------------------------------------------------------------
  1206. U32 ObjectDeclNode::compileSubObject(CodeStream& codeStream, U32 ip, bool root)
  1207. {
  1208. // goes
  1209. // OP_PUSHFRAME 1
  1210. // name expr
  1211. // OP_PUSH 1
  1212. // args... PUSH
  1213. // OP_CREATE_OBJECT 1
  1214. // parentObject 1
  1215. // isDatablock 1
  1216. // internalName 1
  1217. // isSingleton 1
  1218. // lineNumber 1
  1219. // fail point 1
  1220. // for each field, eval
  1221. // OP_ADD_OBJECT (to UINT[0]) 1
  1222. // root? 1
  1223. // add all the sub objects.
  1224. // OP_END_OBJECT 1
  1225. // root? 1
  1226. // To fix the stack issue [7/9/2007 Black]
  1227. // OP_FINISH_OBJECT <-- fail point jumps to this opcode
  1228. S32 count = 2; // 2 OP_PUSH's
  1229. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1230. count++;
  1231. codeStream.emit(OP_PUSH_FRAME);
  1232. codeStream.emit(count);
  1233. ip = classNameExpr->compile(codeStream, ip, TypeReqString);
  1234. codeStream.emit(OP_PUSH);
  1235. ip = objectNameExpr->compile(codeStream, ip, TypeReqString);
  1236. codeStream.emit(OP_PUSH);
  1237. for (ExprNode* exprWalk = argList; exprWalk; exprWalk = (ExprNode*)exprWalk->getNext())
  1238. {
  1239. TypeReq walkType = exprWalk->getPreferredType();
  1240. if (walkType == TypeReqNone) walkType = TypeReqString;
  1241. ip = exprWalk->compile(codeStream, ip, walkType);
  1242. codeStream.emit(OP_PUSH);
  1243. }
  1244. codeStream.emit(OP_CREATE_OBJECT);
  1245. codeStream.emitSTE(parentObject);
  1246. codeStream.emit(isDatablock);
  1247. codeStream.emit(isClassNameInternal);
  1248. codeStream.emit(isSingleton);
  1249. codeStream.emit(dbgLineNumber);
  1250. const U32 failIp = codeStream.emit(0);
  1251. for (SlotAssignNode* slotWalk = slotDecls; slotWalk; slotWalk = (SlotAssignNode*)slotWalk->getNext())
  1252. ip = slotWalk->compile(codeStream, ip, TypeReqNone);
  1253. codeStream.emit(OP_ADD_OBJECT);
  1254. codeStream.emit(root);
  1255. for (ObjectDeclNode* objectWalk = subObjects; objectWalk; objectWalk = (ObjectDeclNode*)objectWalk->getNext())
  1256. ip = objectWalk->compileSubObject(codeStream, ip, false);
  1257. codeStream.emit(OP_END_OBJECT);
  1258. codeStream.emit(root || isDatablock);
  1259. // Added to fix the object creation issue [7/9/2007 Black]
  1260. failOffset = codeStream.emit(OP_FINISH_OBJECT);
  1261. codeStream.patch(failIp, failOffset);
  1262. return codeStream.tell();
  1263. }
  1264. U32 ObjectDeclNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
  1265. {
  1266. // root object decl does:
  1267. // push 0 onto the UINT stack OP_LOADIMMED_UINT
  1268. // precompiles the subObject(true)
  1269. // UINT stack now has object id
  1270. // type conv to type
  1271. codeStream.emit(OP_LOADIMMED_UINT);
  1272. codeStream.emit(0);
  1273. ip = compileSubObject(codeStream, ip, true);
  1274. if (type == TypeReqNone)
  1275. codeStream.emit(OP_POP_STK);
  1276. return codeStream.tell();
  1277. }
  1278. TypeReq ObjectDeclNode::getPreferredType()
  1279. {
  1280. return TypeReqUInt;
  1281. }
  1282. //------------------------------------------------------------
  1283. U32 FunctionDeclStmtNode::compileStmt(CodeStream& codeStream, U32 ip)
  1284. {
  1285. // OP_FUNC_DECL
  1286. // func name
  1287. // namespace
  1288. // package
  1289. // hasBody?
  1290. // func end ip
  1291. // argc
  1292. // ident array[argc]
  1293. // code
  1294. // OP_RETURN_VOID
  1295. setCurrentStringTable(&getFunctionStringTable());
  1296. setCurrentFloatTable(&getFunctionFloatTable());
  1297. FuncVars vars;
  1298. gFuncVars = &vars;
  1299. argc = 0;
  1300. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1301. {
  1302. precompileIdent(walk->varName);
  1303. getFuncVars(dbgLineNumber)->assign(walk->varName, TypeReqNone, dbgLineNumber);
  1304. argc++;
  1305. }
  1306. CodeBlock::smInFunction = true;
  1307. precompileIdent(fnName);
  1308. precompileIdent(nameSpace);
  1309. precompileIdent(package);
  1310. CodeBlock::smInFunction = false;
  1311. codeStream.emit(OP_FUNC_DECL);
  1312. codeStream.emitSTE(fnName);
  1313. codeStream.emitSTE(nameSpace);
  1314. codeStream.emitSTE(package);
  1315. codeStream.emit(U32(bool(stmts != NULL) ? 1 : 0) + U32(dbgLineNumber << 1));
  1316. const U32 endIp = codeStream.emit(0);
  1317. codeStream.emit(argc);
  1318. const U32 localNumVarsIP = codeStream.emit(0);
  1319. for (VarNode* walk = args; walk; walk = (VarNode*)((StmtNode*)walk)->getNext())
  1320. {
  1321. StringTableEntry name = walk->varName;
  1322. codeStream.emit(getFuncVars(dbgLineNumber)->lookup(name, dbgLineNumber));
  1323. }
  1324. CodeBlock::smInFunction = true;
  1325. ip = compileBlock(stmts, codeStream, ip);
  1326. // Add break so breakpoint can be set at closing brace or
  1327. // in empty function.
  1328. addBreakLine(codeStream);
  1329. CodeBlock::smInFunction = false;
  1330. codeStream.emit(OP_RETURN_VOID);
  1331. codeStream.patch(localNumVarsIP, getFuncVars(dbgLineNumber)->count());
  1332. codeStream.patch(endIp, codeStream.tell());
  1333. setCurrentStringTable(&getGlobalStringTable());
  1334. setCurrentFloatTable(&getGlobalFloatTable());
  1335. // map local variables to registers for this function.
  1336. CompilerLocalVariableToRegisterMappingTable* tbl = &getFunctionVariableMappingTable();
  1337. for (const auto& pair : gFuncVars->variableNameMap)
  1338. {
  1339. tbl->add(fnName, pair.second, pair.first);
  1340. }
  1341. gFuncVars = NULL;
  1342. return ip;
  1343. }