2
0

astNodes.cpp 37 KB

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