astNodes.cpp 39 KB

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