gmCodeGen.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /*
  2. _____ __ ___ __ ____ _ __
  3. / ___/__ ___ _ ___ / |/ /__ ___ / /_____ __ __/ __/_______(_)__ / /_
  4. / (_ / _ `/ ' \/ -_) /|_/ / _ \/ _ \/ '_/ -_) // /\ \/ __/ __/ / _ \/ __/
  5. \___/\_,_/_/_/_/\__/_/ /_/\___/_//_/_/\_\\__/\_, /___/\__/_/ /_/ .__/\__/
  6. /___/ /_/
  7. See Copyright Notice in gmMachine.h
  8. */
  9. #include "gmCodeGen.h"
  10. #include "gmCodeTree.h"
  11. #include "gmByteCodeGen.h"
  12. #include "gmArraySimple.h"
  13. #include "gmListDouble.h"
  14. #if GM_USE_INCDECOPERATORS
  15. static const char * s_tempVarName0 = "__t0";
  16. #endif //GM_USE_INCDECOPERATORS
  17. static const char * s_tempVarName1 = "__t1";
  18. #define SIZEOF_BC_BRA (sizeof(gmuint32)+sizeof(gmptr)) // instruction + address
  19. /// \brief gmSortDebugLines will sort debug line information
  20. static void gmSortDebugLines(gmArraySimple<gmLineInfo> &a_lineInfo)
  21. {
  22. int count = (int)a_lineInfo.Count();
  23. // sort by address
  24. int i;
  25. for(i = 0; i < count; ++i)
  26. {
  27. int min = i, j;
  28. for(j = i + 1; j < count; ++j)
  29. {
  30. if(a_lineInfo[j].m_address < a_lineInfo[min].m_address)
  31. {
  32. min = j;
  33. }
  34. }
  35. gmLineInfo t = a_lineInfo[min];
  36. a_lineInfo[min] = a_lineInfo[i];
  37. a_lineInfo[i] = t;
  38. }
  39. // remove duplicate line numbers
  40. int s, d;
  41. for(s = 1, d = 0; s < count; ++s)
  42. {
  43. if(a_lineInfo[s].m_lineNumber != a_lineInfo[d].m_lineNumber)
  44. {
  45. a_lineInfo[++d] = a_lineInfo[s];
  46. }
  47. }
  48. a_lineInfo.SetCount(++d);
  49. }
  50. /*!
  51. \class gmCodeGenPrivate
  52. \brief implementation of gmCodeGen
  53. */
  54. class gmCodeGenPrivate : public gmCodeGen
  55. {
  56. public:
  57. gmCodeGenPrivate();
  58. virtual ~gmCodeGenPrivate();
  59. // implementation
  60. virtual void FreeMemory();
  61. virtual int Lock(const gmCodeTreeNode * a_codeTree, gmCodeGenHooks * a_hooks, bool a_debug, gmLog * a_log);
  62. virtual int Unlock();
  63. // helpers
  64. bool Generate(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode, bool a_siblings = true);
  65. bool GenDeclVariable(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  66. bool GenExprFunction(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  67. bool GenExprTable(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  68. #if GM_USE_FORK
  69. bool GenStmtFork(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  70. #endif //GM_USE_FORK
  71. bool GenStmtReturn(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  72. bool GenStmtBreak(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  73. bool GenStmtContinue(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  74. bool GenStmtFor(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  75. bool GenStmtForEach(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  76. bool GenStmtWhile(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  77. bool GenStmtDoWhile(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  78. bool GenStmtIf(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  79. bool GenStmtCompound(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  80. bool GenExprOpDot(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  81. bool GenExprOpUnary(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  82. #if GM_USE_INCDECOPERATORS
  83. bool GenExprOpPreIncDec(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  84. #endif //GM_USE_INCDECOPERATORS
  85. bool GenExprOpArrayIndex(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  86. bool GenExprOpAr(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  87. bool GenExprOpShift(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  88. bool GenExprOpComparison(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  89. bool GenExprOpBitwise(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  90. bool GenExprOpAnd(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  91. bool GenExprOpOr(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  92. bool GenExprOpAssign(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  93. bool GenExprConstant(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  94. bool GenExprIdentifier(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  95. bool GenExprCall(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  96. bool GenExprThis(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode);
  97. bool m_locked;
  98. int m_errors;
  99. gmLog * m_log;
  100. gmCodeGenHooks * m_hooks;
  101. bool m_debug;
  102. // Variable
  103. struct Variable
  104. {
  105. int m_offset;
  106. gmCodeTreeVariableType m_type;
  107. const char * m_symbol;
  108. };
  109. // FunctionState
  110. class FunctionState : public gmListDoubleNode<FunctionState>
  111. {
  112. public:
  113. FunctionState();
  114. ~FunctionState() {}
  115. void Reset();
  116. int SetVariableType(const char * a_symbol, gmCodeTreeVariableType a_type);
  117. // return -2 if the variable does not exist, -1 if it exists but is not a local
  118. // set a type to var type if return >= -1
  119. int GetVariableOffset(const char * a_symbol, gmCodeTreeVariableType &a_type);
  120. const char * m_debugName; // name of the variable the function is assigned to.
  121. gmArraySimple<Variable> m_variables;
  122. int m_numLocals; // number of local variables including parameters.
  123. gmByteCodeGen m_byteCode;
  124. // line number debug.
  125. int m_currentLine;
  126. gmArraySimple<gmLineInfo> m_lineInfo;
  127. };
  128. // Patch
  129. struct Patch
  130. {
  131. gmuint32 m_address;
  132. int m_next;
  133. };
  134. // LoopInfo
  135. struct LoopInfo
  136. {
  137. int m_breaks;
  138. int m_continues;
  139. };
  140. int m_currentLoop; //!< loop top of stack
  141. FunctionState * m_currentFunction; //!< function top of stack
  142. gmListDouble<FunctionState> m_functionStack;
  143. gmArraySimple<LoopInfo> m_loopStack;
  144. gmArraySimple<Patch> m_patches;
  145. // helper functions
  146. FunctionState * PushFunction();
  147. FunctionState * PopFunction();
  148. void PushLoop();
  149. void PopLoop();
  150. void ApplyPatches(int a_patches, gmByteCodeGen * a_byteCode, gmuint32 a_value);
  151. };
  152. //
  153. // gmLineNumberCallback is used to record byte code instruction addresses against source code line numbers.
  154. // The callback records entries into
  155. //
  156. void GM_CDECL gmLineNumberCallback(int a_address, void * a_context)
  157. {
  158. gmCodeGenPrivate::FunctionState * state = (gmCodeGenPrivate::FunctionState *) a_context;
  159. gmLineInfo info, * lastEntry = NULL;
  160. info.m_address = a_address;
  161. info.m_lineNumber = state->m_currentLine;
  162. if(state->m_lineInfo.Count() > 0)
  163. {
  164. lastEntry = &state->m_lineInfo[state->m_lineInfo.Count() - 1];
  165. }
  166. if(lastEntry == NULL || (lastEntry->m_address != a_address) || (lastEntry->m_lineNumber != state->m_currentLine))
  167. {
  168. state->m_lineInfo.InsertLast(info);
  169. }
  170. }
  171. gmCodeGen& gmCodeGen::Get()
  172. {
  173. static gmCodeGenPrivate codeGen;
  174. return codeGen;
  175. }
  176. //
  177. //
  178. // Implementation of gmCodeGenPrivate
  179. //
  180. //
  181. gmCodeGenPrivate::gmCodeGenPrivate()
  182. {
  183. m_locked = false;
  184. m_errors = 0;
  185. m_log = NULL;
  186. m_hooks = NULL;
  187. m_debug = false;
  188. m_currentLoop = -1;
  189. m_currentFunction = NULL;
  190. }
  191. gmCodeGenPrivate::~gmCodeGenPrivate()
  192. {
  193. FreeMemory();
  194. }
  195. void gmCodeGenPrivate::FreeMemory()
  196. {
  197. if(m_locked == false)
  198. {
  199. m_currentLoop = -1;
  200. m_currentFunction = NULL;
  201. m_loopStack.ResetAndFreeMemory();
  202. m_functionStack.RemoveAndDeleteAll();
  203. m_patches.ResetAndFreeMemory();
  204. }
  205. }
  206. int gmCodeGenPrivate::Lock(const gmCodeTreeNode * a_codeTree, gmCodeGenHooks * a_hooks, bool a_debug, gmLog * a_log)
  207. {
  208. if(m_locked == true) return 1;
  209. // set up members
  210. m_errors = 0;
  211. m_locked = true;
  212. m_log = a_log;
  213. m_hooks = a_hooks;
  214. m_debug = a_debug;
  215. GM_ASSERT(m_hooks != NULL);
  216. // set up memory and stacks.
  217. m_currentLoop = -1;
  218. m_currentFunction = NULL;
  219. m_loopStack.Reset();
  220. m_patches.Reset();
  221. // set up the stacks for the first procedure.
  222. m_hooks->Begin(m_debug);
  223. PushFunction();
  224. GM_ASSERT(m_currentFunction);
  225. // generate the byte code for the root procedure
  226. if(!Generate(a_codeTree, &m_currentFunction->m_byteCode))
  227. {
  228. ++m_errors;
  229. }
  230. else
  231. {
  232. m_currentFunction->m_byteCode.Emit(BC_RET);
  233. // Create a locals table
  234. const char ** locals = NULL;
  235. if(m_debug)
  236. {
  237. locals = (const char **) alloca(sizeof(const char *) * m_currentFunction->m_numLocals);
  238. memset(locals, 0, sizeof(const char *) * m_currentFunction->m_numLocals);
  239. for(gmint v = 0; v < m_currentFunction->m_variables.Count(); ++v)
  240. {
  241. Variable &variable = m_currentFunction->m_variables[v];
  242. if(variable.m_offset != -1)
  243. {
  244. locals[variable.m_offset] = variable.m_symbol;
  245. }
  246. }
  247. }
  248. // Fill out a function info struct and add the function to the code gen hooks.
  249. gmSortDebugLines(m_currentFunction->m_lineInfo);
  250. gmFunctionInfo info;
  251. info.m_id = m_hooks->GetFunctionId();
  252. info.m_root = true;
  253. info.m_byteCode = m_currentFunction->m_byteCode.GetData();
  254. info.m_byteCodeLength = (int)m_currentFunction->m_byteCode.Tell();
  255. info.m_numParams = 0;
  256. info.m_numLocals = m_currentFunction->m_numLocals;
  257. info.m_symbols = locals;
  258. info.m_maxStackSize = m_currentFunction->m_byteCode.GetMaxTos();
  259. info.m_lineInfoCount = (int)m_currentFunction->m_lineInfo.Count();
  260. info.m_lineInfo = m_currentFunction->m_lineInfo.GetData();
  261. info.m_debugName = "__main";
  262. m_hooks->AddFunction(info);
  263. //gmByteCodePrint(stdout, info.m_byteCode, info.m_byteCodeLength);
  264. }
  265. PopFunction();
  266. m_hooks->End(m_errors);
  267. return m_errors;
  268. }
  269. int gmCodeGenPrivate::Unlock()
  270. {
  271. m_errors = 0;
  272. m_locked = false;
  273. m_log = NULL;
  274. m_hooks = NULL;
  275. m_debug = false;
  276. m_currentLoop = -1;
  277. m_loopStack.Reset();
  278. m_patches.Reset();
  279. return 0;
  280. }
  281. bool gmCodeGenPrivate::Generate(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode, bool a_siblings)
  282. {
  283. bool res = true;
  284. while(a_node)
  285. {
  286. // record line number
  287. static int s_line = 0;
  288. if(m_currentFunction) m_currentFunction->m_currentLine = a_node->m_lineNumber;
  289. // if we are in debug, emit a BC_LINE instruction
  290. if(m_debug && (s_line != a_node->m_lineNumber) &&
  291. !(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_COMPOUND))
  292. {
  293. a_byteCode->Emit(BC_LINE);
  294. s_line = a_node->m_lineNumber;
  295. }
  296. switch(a_node->m_type)
  297. {
  298. case CTNT_DECLARATION :
  299. {
  300. switch(a_node->m_subType)
  301. {
  302. case CTNDT_VARIABLE : res = GenDeclVariable(a_node, a_byteCode); break;
  303. default:
  304. {
  305. GM_ASSERT(false);
  306. return false;
  307. }
  308. }
  309. break;
  310. }
  311. case CTNT_STATEMENT :
  312. {
  313. switch(a_node->m_subType)
  314. {
  315. case CTNST_RETURN : res = GenStmtReturn(a_node, a_byteCode); break;
  316. case CTNST_BREAK : res = GenStmtBreak(a_node, a_byteCode); break;
  317. case CTNST_CONTINUE : res = GenStmtContinue(a_node, a_byteCode); break;
  318. case CTNST_FOR : res = GenStmtFor(a_node, a_byteCode); break;
  319. case CTNST_FOREACH : res = GenStmtForEach(a_node, a_byteCode); break;
  320. case CTNST_WHILE : res = GenStmtWhile(a_node, a_byteCode); break;
  321. case CTNST_DOWHILE : res = GenStmtDoWhile(a_node, a_byteCode); break;
  322. case CTNST_IF : res = GenStmtIf(a_node, a_byteCode); break;
  323. case CTNST_COMPOUND : res = GenStmtCompound(a_node, a_byteCode); break;
  324. #if GM_USE_FORK
  325. case CTNST_FORK : res = GenStmtFork(a_node, a_byteCode); break;
  326. #else //GM_USE_FORK
  327. case CTNST_FORK: // Unsupported, but tokens exist
  328. {
  329. if(m_log && m_currentFunction)
  330. {
  331. m_log->LogEntry("error (%d) 'fork' instruction not supported", m_currentFunction->m_currentLine);
  332. }
  333. return false;
  334. }
  335. #endif //GM_USE_FORK
  336. default:
  337. {
  338. GM_ASSERT(false);
  339. return false;
  340. }
  341. }
  342. break;
  343. }
  344. case CTNT_EXPRESSION :
  345. {
  346. switch(a_node->m_subType)
  347. {
  348. case CTNET_OPERATION :
  349. {
  350. switch(a_node->m_subTypeType)
  351. {
  352. case CTNOT_DOT : res = GenExprOpDot(a_node, a_byteCode); break;
  353. case CTNOT_UNARY_PLUS :
  354. case CTNOT_UNARY_MINUS :
  355. case CTNOT_UNARY_COMPLEMENT :
  356. case CTNOT_UNARY_NOT : res = GenExprOpUnary(a_node, a_byteCode); break;
  357. #if GM_USE_INCDECOPERATORS
  358. case CTNOT_PRE_DEC :
  359. case CTNOT_PRE_INC : res = GenExprOpPreIncDec(a_node, a_byteCode); break;
  360. #endif //GM_USE_INCDECOPERATORS
  361. case CTNOT_ARRAY_INDEX : res = GenExprOpArrayIndex(a_node, a_byteCode); break;
  362. case CTNOT_TIMES :
  363. case CTNOT_DIVIDE :
  364. case CTNOT_REM :
  365. case CTNOT_MINUS :
  366. case CTNOT_ADD : res = GenExprOpAr(a_node, a_byteCode); break;
  367. case CTNOT_SHIFT_LEFT :
  368. case CTNOT_SHIFT_RIGHT : res = GenExprOpShift(a_node, a_byteCode); break;
  369. case CTNOT_LT :
  370. case CTNOT_GT :
  371. case CTNOT_LTE :
  372. case CTNOT_GTE :
  373. case CTNOT_EQ :
  374. case CTNOT_NEQ : res = GenExprOpComparison(a_node, a_byteCode); break;
  375. case CTNOT_BIT_AND :
  376. case CTNOT_BIT_XOR :
  377. case CTNOT_BIT_OR : res = GenExprOpBitwise(a_node, a_byteCode); break;
  378. case CTNOT_AND : res = GenExprOpAnd(a_node, a_byteCode); break;
  379. case CTNOT_OR : res = GenExprOpOr(a_node, a_byteCode); break;
  380. case CTNOT_ASSIGN : res = GenExprOpAssign(a_node, a_byteCode); break;
  381. default:
  382. {
  383. GM_ASSERT(false);
  384. return false;
  385. }
  386. }
  387. break;
  388. }
  389. case CTNET_CONSTANT : res = GenExprConstant(a_node, a_byteCode); break;
  390. case CTNET_IDENTIFIER : res = GenExprIdentifier(a_node, a_byteCode); break;
  391. case CTNET_CALL : res = GenExprCall(a_node, a_byteCode); break;
  392. case CTNET_THIS : res = GenExprThis(a_node, a_byteCode); break;
  393. case CTNET_FUNCTION : res = GenExprFunction(a_node, a_byteCode); break;
  394. case CTNET_TABLE : res = GenExprTable(a_node, a_byteCode); break;
  395. default :
  396. {
  397. GM_ASSERT(false);
  398. return false;
  399. }
  400. }
  401. break;
  402. }
  403. default:
  404. {
  405. GM_ASSERT(false);
  406. return false;
  407. }
  408. }
  409. if(!res)
  410. {
  411. return false;
  412. }
  413. if((a_node->m_flags & gmCodeTreeNode::CTN_POP) > 0)
  414. {
  415. a_byteCode->Emit(BC_POP);
  416. }
  417. if(a_siblings)
  418. {
  419. a_node = a_node->m_sibling;
  420. }
  421. else a_node = NULL;
  422. }
  423. return true;
  424. }
  425. bool gmCodeGenPrivate::GenDeclVariable(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  426. {
  427. GM_ASSERT(a_node->m_type == CTNT_DECLARATION && a_node->m_subType == CTNDT_VARIABLE);
  428. GM_ASSERT(m_currentFunction);
  429. m_currentFunction->SetVariableType(a_node->m_children[0]->m_data.m_string, (gmCodeTreeVariableType) a_node->m_subTypeType);
  430. return true;
  431. }
  432. bool gmCodeGenPrivate::GenExprFunction(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  433. {
  434. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_FUNCTION);
  435. gmptr id = m_hooks->GetFunctionId();
  436. a_byteCode->EmitPtr(BC_PUSHFN, id);
  437. // Create the function
  438. PushFunction();
  439. // Get a debug function name as the name of the variable the function is assigned to
  440. if(m_debug && a_node->m_parent && a_node->m_parent->m_type == CTNT_EXPRESSION && a_node->m_parent->m_subType == CTNET_OPERATION &&
  441. (a_node->m_parent->m_subTypeType == CTNOT_ASSIGN || a_node->m_parent->m_subTypeType == CTNOT_ASSIGN_FIELD) && a_node->m_parent->m_children[1] == a_node)
  442. {
  443. const gmCodeTreeNode * debugName = a_node->m_parent->m_children[0];
  444. if(debugName && debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_IDENTIFIER)
  445. {
  446. }
  447. else if(debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_OPERATION &&
  448. debugName->m_subTypeType == CTNOT_DOT)
  449. {
  450. debugName = debugName->m_children[1];
  451. }
  452. else
  453. {
  454. debugName = NULL;
  455. }
  456. if(debugName)
  457. {
  458. GM_ASSERT(debugName->m_type == CTNT_EXPRESSION && debugName->m_subType == CTNET_IDENTIFIER);
  459. m_currentFunction->m_debugName = debugName->m_data.m_string;
  460. }
  461. }
  462. // Parameters
  463. const gmCodeTreeNode * params = a_node->m_children[0];
  464. int numParams = 0;
  465. while(params)
  466. {
  467. const gmCodeTreeNode * param = params->m_children[0];
  468. GM_ASSERT(param->m_type == CTNT_EXPRESSION && param->m_subType == CTNET_IDENTIFIER);
  469. if(m_currentFunction->SetVariableType(param->m_data.m_string, CTVT_LOCAL) != numParams)
  470. {
  471. if(m_log) m_log->LogEntry("error (%d) parameter %s already declared", param->m_lineNumber, param->m_data.m_string);
  472. PopFunction();
  473. return false;
  474. }
  475. ++numParams;
  476. params = params->m_sibling;
  477. }
  478. // Generate the code
  479. bool res = Generate(a_node->m_children[1], &m_currentFunction->m_byteCode);
  480. // Generate a return incase the function didnt have one.
  481. m_currentFunction->m_byteCode.Emit(BC_RET);
  482. if(res)
  483. {
  484. // Create a locals table
  485. const char ** locals = NULL;
  486. if(m_debug)
  487. {
  488. locals = (const char **) alloca(sizeof(const char *) * m_currentFunction->m_numLocals);
  489. memset(locals, 0, sizeof(const char *) * m_currentFunction->m_numLocals);
  490. for(gmint v = 0; v < m_currentFunction->m_variables.Count(); ++v)
  491. {
  492. Variable &variable = m_currentFunction->m_variables[v];
  493. if(variable.m_offset != -1)
  494. {
  495. locals[variable.m_offset] = variable.m_symbol;
  496. }
  497. }
  498. }
  499. // Add the function to the hooks.
  500. gmSortDebugLines(m_currentFunction->m_lineInfo);
  501. gmFunctionInfo info;
  502. info.m_id = id;
  503. info.m_root = false;
  504. info.m_byteCode = m_currentFunction->m_byteCode.GetData();
  505. info.m_byteCodeLength = (int)m_currentFunction->m_byteCode.Tell();
  506. info.m_numParams = numParams;
  507. info.m_numLocals = m_currentFunction->m_numLocals - numParams;
  508. info.m_symbols = locals;
  509. info.m_maxStackSize = m_currentFunction->m_byteCode.GetMaxTos();
  510. info.m_lineInfoCount = (int)m_currentFunction->m_lineInfo.Count();
  511. info.m_lineInfo = m_currentFunction->m_lineInfo.GetData();
  512. info.m_debugName = m_currentFunction->m_debugName;
  513. m_hooks->AddFunction(info);
  514. //gmByteCodePrint(stdout, info.m_byteCode, info.m_byteCodeLength);
  515. }
  516. PopFunction();
  517. return res;
  518. }
  519. bool gmCodeGenPrivate::GenExprTable(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  520. {
  521. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_TABLE);
  522. gmuint32 index = 0;
  523. const gmCodeTreeNode * fields = a_node->m_children[0];
  524. // Create table
  525. a_byteCode->Emit(BC_PUSHTBL);
  526. // Create fields
  527. while(fields)
  528. {
  529. a_byteCode->Emit(BC_DUP);
  530. if(fields->m_type == CTNT_EXPRESSION && fields->m_subType == CTNET_OPERATION && fields->m_subTypeType == CTNOT_ASSIGN_FIELD)
  531. {
  532. if(!Generate(fields->m_children[1], a_byteCode)) return false;
  533. a_byteCode->EmitPtr(BC_SETDOT, m_hooks->GetSymbolId(fields->m_children[0]->m_data.m_string));
  534. }
  535. else
  536. {
  537. a_byteCode->Emit(BC_PUSHINT, (gmint)(index++));
  538. if(!Generate(fields, a_byteCode, false)) return false;
  539. a_byteCode->Emit(BC_SETIND);
  540. }
  541. fields = fields->m_sibling;
  542. }
  543. return true;
  544. }
  545. bool gmCodeGenPrivate::GenStmtReturn(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  546. {
  547. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_RETURN);
  548. if(a_node->m_children[0])
  549. {
  550. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  551. return a_byteCode->Emit(BC_RETV);
  552. }
  553. return a_byteCode->Emit(BC_RET);
  554. }
  555. bool gmCodeGenPrivate::GenStmtBreak(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  556. {
  557. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_BREAK);
  558. if(m_currentLoop >= 0)
  559. {
  560. a_byteCode->Emit(BC_BRA);
  561. Patch * patch = &m_patches.InsertLast();
  562. patch->m_address = a_byteCode->Skip(sizeof(gmptr)); // NOTE: Using gmptr size addresses
  563. patch->m_next = m_loopStack[m_currentLoop].m_breaks;
  564. m_loopStack[m_currentLoop].m_breaks = (int)m_patches.Count()-1;
  565. return true;
  566. }
  567. if(m_log) m_log->LogEntry("error (%d) illegal break statement", a_node->m_lineNumber);
  568. return false;
  569. }
  570. bool gmCodeGenPrivate::GenStmtContinue(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  571. {
  572. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_CONTINUE);
  573. if(m_currentLoop >= 0)
  574. {
  575. a_byteCode->Emit(BC_BRA);
  576. Patch * patch = &m_patches.InsertLast();
  577. patch->m_address = a_byteCode->Skip(sizeof(gmptr)); // NOTE: Using gmptr size addresses
  578. patch->m_next = m_loopStack[m_currentLoop].m_continues;
  579. m_loopStack[m_currentLoop].m_continues = (int)m_patches.Count()-1;
  580. return true;
  581. }
  582. if(m_log) m_log->LogEntry("error (%d) illegal continue statement", a_node->m_lineNumber);
  583. return false;
  584. }
  585. bool gmCodeGenPrivate::GenStmtFor(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  586. {
  587. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_FOR);
  588. unsigned int loc1, loc2 = 0, continueAddress;
  589. // Initialisers
  590. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  591. PushLoop();
  592. loc1 = (int)a_byteCode->Tell();
  593. // Condition expression
  594. if(!Generate(a_node->m_children[1], a_byteCode))
  595. {
  596. PopLoop();
  597. return false;
  598. }
  599. if(a_node->m_children[1] != NULL) // no branch for no test.
  600. {
  601. loc2 = a_byteCode->Skip(SIZEOF_BC_BRA);
  602. }
  603. // Body
  604. if(!Generate(a_node->m_children[3], a_byteCode))
  605. {
  606. PopLoop();
  607. return false;
  608. }
  609. // Continue patch
  610. continueAddress = (int)a_byteCode->Tell();
  611. // Loop Expression
  612. if(!Generate(a_node->m_children[2], a_byteCode))
  613. {
  614. PopLoop();
  615. return false;
  616. }
  617. a_byteCode->EmitPtr(BC_BRA, loc1);
  618. loc1 = (int)a_byteCode->Tell();
  619. if(a_node->m_children[1] != NULL)
  620. {
  621. a_byteCode->Seek(loc2);
  622. a_byteCode->EmitPtr(BC_BRZ, loc1);
  623. a_byteCode->Seek(loc1);
  624. }
  625. ApplyPatches(m_loopStack[m_currentLoop].m_breaks, a_byteCode, loc1);
  626. ApplyPatches(m_loopStack[m_currentLoop].m_continues, a_byteCode, continueAddress);
  627. PopLoop();
  628. return true;
  629. }
  630. bool gmCodeGenPrivate::GenStmtForEach(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  631. {
  632. unsigned int breakAddress, continueAddress, loc1, loc2;
  633. // Generate table
  634. if(!Generate(a_node->m_children[0], a_byteCode))
  635. {
  636. return false;
  637. }
  638. PushLoop();
  639. // Push the first iterator
  640. a_byteCode->Emit(BC_PUSHINT, (gmint) -2); // first iterator value.
  641. continueAddress = (int)a_byteCode->Tell();
  642. // Generate call
  643. const char * keyVar = s_tempVarName1;
  644. if(a_node->m_children[2]) keyVar = a_node->m_children[2]->m_data.m_string;
  645. const char * valueVar = a_node->m_children[1]->m_data.m_string;
  646. gmuint16 keyOffset = (gmuint16) m_currentFunction->SetVariableType(keyVar, CTVT_LOCAL);
  647. gmuint16 valueOffset = (gmuint16) m_currentFunction->SetVariableType(valueVar, CTVT_LOCAL);
  648. gmuint32 opcode = (keyOffset << 16) | (valueOffset & 0xffff);
  649. loc1 = (int)a_byteCode->Tell();
  650. a_byteCode->Emit(BC_FOREACH, opcode);
  651. // Skip space for jump
  652. loc2 = a_byteCode->Skip(SIZEOF_BC_BRA);
  653. // Generate body
  654. if(!Generate(a_node->m_children[3], a_byteCode))
  655. {
  656. PopLoop();
  657. return false;
  658. }
  659. a_byteCode->EmitPtr(BC_BRA, (gmuint32) loc1);
  660. breakAddress = (int)a_byteCode->Seek(loc2);
  661. a_byteCode->EmitPtr(BC_BRZ, breakAddress);
  662. a_byteCode->Seek(breakAddress);
  663. // pop table and iterator
  664. a_byteCode->Emit(BC_POP2);
  665. ApplyPatches(m_loopStack[m_currentLoop].m_breaks, a_byteCode, breakAddress);
  666. ApplyPatches(m_loopStack[m_currentLoop].m_continues, a_byteCode, continueAddress);
  667. PopLoop();
  668. return true;
  669. }
  670. bool gmCodeGenPrivate::GenStmtWhile(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  671. {
  672. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_WHILE);
  673. unsigned int loc1, loc2, continueAddress;
  674. PushLoop();
  675. // Continue address
  676. loc1 = continueAddress = (int)a_byteCode->Tell();
  677. // Condition expression
  678. if(!Generate(a_node->m_children[0], a_byteCode))
  679. {
  680. PopLoop();
  681. return false;
  682. }
  683. loc2 = a_byteCode->Skip(SIZEOF_BC_BRA);
  684. // Loop body
  685. if(!Generate(a_node->m_children[1], a_byteCode))
  686. {
  687. PopLoop();
  688. return false;
  689. }
  690. a_byteCode->EmitPtr(BC_BRA, loc1);
  691. loc1 = (int)a_byteCode->Seek(loc2);
  692. a_byteCode->EmitPtr(BC_BRZ, loc1);
  693. a_byteCode->Seek(loc1);
  694. ApplyPatches(m_loopStack[m_currentLoop].m_breaks, a_byteCode, loc1);
  695. ApplyPatches(m_loopStack[m_currentLoop].m_continues, a_byteCode, continueAddress);
  696. PopLoop();
  697. return true;
  698. }
  699. bool gmCodeGenPrivate::GenStmtDoWhile(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  700. {
  701. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_DOWHILE);
  702. unsigned int loc1, continueAddress;
  703. PushLoop();
  704. loc1 = (int)a_byteCode->Tell();
  705. // Loop body
  706. if(!Generate(a_node->m_children[1], a_byteCode))
  707. {
  708. PopLoop();
  709. return false;
  710. }
  711. // Continue address
  712. continueAddress = (int)a_byteCode->Tell();
  713. // Condition expression
  714. if(!Generate(a_node->m_children[0], a_byteCode))
  715. {
  716. PopLoop();
  717. return false;
  718. }
  719. a_byteCode->EmitPtr(BC_BRNZ, loc1);
  720. loc1 = (int)a_byteCode->Tell();
  721. ApplyPatches(m_loopStack[m_currentLoop].m_breaks, a_byteCode, loc1);
  722. ApplyPatches(m_loopStack[m_currentLoop].m_continues, a_byteCode, continueAddress);
  723. PopLoop();
  724. return true;
  725. }
  726. bool gmCodeGenPrivate::GenStmtIf(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  727. {
  728. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_IF);
  729. unsigned int loc1, loc2, loc3;
  730. if(a_node->m_children[2]) // Is this an if-else, or just an if
  731. {
  732. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  733. loc1 = a_byteCode->Skip(SIZEOF_BC_BRA);
  734. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  735. loc2 = a_byteCode->Skip(SIZEOF_BC_BRA);
  736. if(!Generate(a_node->m_children[2], a_byteCode)) return false;
  737. loc3 = (int)a_byteCode->Seek(loc1);
  738. a_byteCode->EmitPtr(BC_BRZ, loc2+SIZEOF_BC_BRA);
  739. a_byteCode->Seek(loc2);
  740. a_byteCode->EmitPtr(BC_BRA, loc3);
  741. a_byteCode->Seek(loc3);
  742. }
  743. else
  744. {
  745. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  746. loc1 = a_byteCode->Skip(SIZEOF_BC_BRA);
  747. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  748. loc2 = (int)a_byteCode->Seek(loc1);
  749. m_currentFunction->m_currentLine = a_node->m_lineNumber;
  750. a_byteCode->EmitPtr(BC_BRZ, loc2);
  751. a_byteCode->Seek(loc2);
  752. }
  753. return true;
  754. }
  755. #if GM_USE_FORK
  756. bool gmCodeGenPrivate::GenStmtFork(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  757. {
  758. GM_ASSERT(a_node->m_type == CTNT_STATEMENT && a_node->m_subType == CTNST_FORK );
  759. gmuint32 loc1,loc2;
  760. // create the var for the thread id
  761. const char * valname = 0;
  762. gmuint32 valref = 0;
  763. if ( a_node->m_children[1])
  764. {
  765. valname = a_node->m_children[1]->m_data.m_string;
  766. valref = m_currentFunction->SetVariableType( valname, CTVT_LOCAL );
  767. }
  768. loc1 = a_byteCode->Skip( SIZEOF_BC_BRA );
  769. if (!valname) a_byteCode->Emit( BC_POP ); // if not specified then just pop
  770. else a_byteCode->Emit( BC_SETLOCAL, valref ); // store the thread id
  771. if (!Generate(a_node->m_children[0], a_byteCode )) return false;
  772. a_byteCode->Emit( BC_RET );
  773. loc2 = (int)a_byteCode->Seek( loc1 );
  774. a_byteCode->Emit( BC_FORK, loc2 );
  775. a_byteCode->Seek( loc2 );
  776. if (!valname) a_byteCode->Emit( BC_POP ); // if not specified then just pop
  777. else a_byteCode->Emit( BC_SETLOCAL, valref ); // store the thread id
  778. return true;
  779. }
  780. #endif //GM_USE_FORK
  781. bool gmCodeGenPrivate::GenStmtCompound(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  782. {
  783. return Generate(a_node->m_children[0], a_byteCode);
  784. }
  785. bool gmCodeGenPrivate::GenExprOpDot(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  786. {
  787. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION && a_node->m_subTypeType == CTNOT_DOT);
  788. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  789. // make sure child 1 is an identifier
  790. const gmCodeTreeNode * id = a_node->m_children[1];
  791. if(id && id->m_type == CTNT_EXPRESSION && id->m_subType == CTNET_IDENTIFIER)
  792. {
  793. return a_byteCode->EmitPtr(BC_GETDOT, m_hooks->GetSymbolId(a_node->m_children[1]->m_data.m_string));
  794. }
  795. if(m_log) m_log->LogEntry("error (%d) illegal dot operator", a_node->m_lineNumber);
  796. return false;
  797. }
  798. bool gmCodeGenPrivate::GenExprOpUnary(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  799. {
  800. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  801. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  802. switch(a_node->m_subTypeType)
  803. {
  804. case CTNOT_UNARY_PLUS : return a_byteCode->Emit(BC_OP_POS);
  805. case CTNOT_UNARY_MINUS : return a_byteCode->Emit(BC_OP_NEG);
  806. case CTNOT_UNARY_NOT : return a_byteCode->Emit(BC_OP_NOT);
  807. case CTNOT_UNARY_COMPLEMENT : return a_byteCode->Emit(BC_BIT_INV);
  808. default :
  809. {
  810. if(m_log) m_log->LogEntry("error (%d) unkown operator", a_node->m_lineNumber);
  811. }
  812. }
  813. return false;
  814. }
  815. #if GM_USE_INCDECOPERATORS
  816. bool gmCodeGenPrivate::GenExprOpPreIncDec(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  817. {
  818. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  819. // Make sure child 0 is an l-value
  820. const gmCodeTreeNode * lValue = a_node->m_children[0];
  821. int type = 0;
  822. if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_OPERATION && lValue->m_subTypeType == CTNOT_DOT)
  823. {
  824. // Generate half l-value
  825. if(!Generate(lValue->m_children[0], a_byteCode)) return false;
  826. a_byteCode->Emit(BC_DUP);
  827. a_byteCode->EmitPtr(BC_GETDOT, m_hooks->GetSymbolId(lValue->m_children[1]->m_data.m_string));
  828. type = 0;
  829. }
  830. else if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_OPERATION && lValue->m_subTypeType == CTNOT_ARRAY_INDEX)
  831. {
  832. // Generate half l-value
  833. if(!Generate(lValue->m_children[0], a_byteCode)) return false;
  834. if(!Generate(lValue->m_children[1], a_byteCode)) return false;
  835. a_byteCode->Emit(BC_DUP2);
  836. a_byteCode->Emit(BC_GETIND);
  837. type = 1;
  838. }
  839. else if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_IDENTIFIER)
  840. {
  841. if(!Generate(lValue, a_byteCode)) return false;
  842. type = 2;
  843. }
  844. else
  845. {
  846. if(m_log) m_log->LogEntry("illegal l-value for '++/--' operator, line %d", a_node->m_lineNumber);
  847. return false;
  848. }
  849. // Perform inc/dec
  850. switch(a_node->m_subTypeType)
  851. {
  852. case CTNOT_PRE_INC : a_byteCode->Emit(BC_OP_INC); break;
  853. case CTNOT_PRE_DEC : a_byteCode->Emit(BC_OP_DEC); break;
  854. default :
  855. {
  856. if(m_log) m_log->LogEntry("unkown operator");
  857. return false;
  858. }
  859. }
  860. // Write back the value
  861. if(type == 0)
  862. {
  863. int offset = m_currentFunction->SetVariableType(s_tempVarName0, CTVT_LOCAL);
  864. a_byteCode->Emit(BC_DUP);
  865. a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  866. a_byteCode->EmitPtr(BC_SETDOT, m_hooks->GetSymbolId(lValue->m_children[1]->m_data.m_string));
  867. a_byteCode->Emit(BC_GETLOCAL, (gmuint32) offset);
  868. }
  869. else if(type == 1)
  870. {
  871. int offset = m_currentFunction->SetVariableType(s_tempVarName0, CTVT_LOCAL);
  872. a_byteCode->Emit(BC_DUP);
  873. a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  874. a_byteCode->Emit(BC_SETIND);
  875. a_byteCode->Emit(BC_GETLOCAL, (gmuint32) offset);
  876. }
  877. else if(type == 2)
  878. {
  879. a_byteCode->Emit(BC_DUP);
  880. gmCodeTreeVariableType vtype;
  881. int offset = m_currentFunction->GetVariableOffset(lValue->m_data.m_string, vtype);
  882. // if local, set local regardless
  883. // if member set this
  884. // if global, set global
  885. // set and add local
  886. if((lValue->m_flags & gmCodeTreeNode::CTN_MEMBER) > 0)
  887. {
  888. return a_byteCode->EmitPtr(BC_SETTHIS, m_hooks->GetSymbolId(lValue->m_data.m_string));
  889. }
  890. if(offset >= 0 && vtype == CTVT_LOCAL)
  891. {
  892. return a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  893. }
  894. else if(offset == -1)
  895. {
  896. if(vtype == CTVT_MEMBER)
  897. {
  898. return a_byteCode->EmitPtr(BC_SETTHIS, m_hooks->GetSymbolId(lValue->m_data.m_string));
  899. }
  900. else if(vtype == CTVT_GLOBAL)
  901. {
  902. return a_byteCode->EmitPtr(BC_SETGLOBAL, m_hooks->GetSymbolId(lValue->m_data.m_string));
  903. }
  904. if(m_log) m_log->LogEntry("internal error");
  905. return false;
  906. }
  907. offset = m_currentFunction->SetVariableType(lValue->m_data.m_string, CTVT_LOCAL);
  908. return a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  909. }
  910. else
  911. {
  912. // paranoia
  913. if(m_log) m_log->LogEntry("internal error");
  914. return false;
  915. }
  916. return true;
  917. }
  918. #endif
  919. bool gmCodeGenPrivate::GenExprOpArrayIndex(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  920. {
  921. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION && a_node->m_subTypeType == CTNOT_ARRAY_INDEX);
  922. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  923. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  924. return a_byteCode->Emit(BC_GETIND);
  925. }
  926. bool gmCodeGenPrivate::GenExprOpAr(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  927. {
  928. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  929. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  930. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  931. switch(a_node->m_subTypeType)
  932. {
  933. case CTNOT_TIMES : return a_byteCode->Emit(BC_OP_MUL);
  934. case CTNOT_DIVIDE : return a_byteCode->Emit(BC_OP_DIV);
  935. case CTNOT_REM : return a_byteCode->Emit(BC_OP_REM);
  936. case CTNOT_ADD : return a_byteCode->Emit(BC_OP_ADD);
  937. case CTNOT_MINUS : return a_byteCode->Emit(BC_OP_SUB);
  938. default :
  939. {
  940. if(m_log) m_log->LogEntry("error (%d) unkown arithmatic operator", a_node->m_lineNumber);
  941. }
  942. }
  943. return false;
  944. }
  945. bool gmCodeGenPrivate::GenExprOpShift(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  946. {
  947. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  948. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  949. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  950. switch(a_node->m_subTypeType)
  951. {
  952. case CTNOT_SHIFT_LEFT : return a_byteCode->Emit(BC_BIT_SHL);
  953. case CTNOT_SHIFT_RIGHT : return a_byteCode->Emit(BC_BIT_SHR);
  954. default :
  955. {
  956. if(m_log) m_log->LogEntry("error (%d) unkown shift operator", a_node->m_lineNumber);
  957. }
  958. }
  959. return false;
  960. }
  961. bool gmCodeGenPrivate::GenExprOpComparison(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  962. {
  963. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  964. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  965. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  966. switch(a_node->m_subTypeType)
  967. {
  968. case CTNOT_LT : return a_byteCode->Emit(BC_OP_LT);
  969. case CTNOT_GT : return a_byteCode->Emit(BC_OP_GT);
  970. case CTNOT_LTE : return a_byteCode->Emit(BC_OP_LTE);
  971. case CTNOT_GTE : return a_byteCode->Emit(BC_OP_GTE);
  972. case CTNOT_EQ : return a_byteCode->Emit(BC_OP_EQ);
  973. case CTNOT_NEQ : return a_byteCode->Emit(BC_OP_NEQ);
  974. default :
  975. {
  976. if(m_log) m_log->LogEntry("error (%d) unkown comparison operator", a_node->m_lineNumber);
  977. }
  978. }
  979. return false;
  980. }
  981. bool gmCodeGenPrivate::GenExprOpBitwise(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  982. {
  983. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  984. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  985. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  986. switch(a_node->m_subTypeType)
  987. {
  988. case CTNOT_BIT_AND : return a_byteCode->Emit(BC_BIT_AND);
  989. case CTNOT_BIT_XOR : return a_byteCode->Emit(BC_BIT_XOR);
  990. case CTNOT_BIT_OR : return a_byteCode->Emit(BC_BIT_OR);
  991. default :
  992. {
  993. if(m_log) m_log->LogEntry("error (%d) unkown bitwise operator", a_node->m_lineNumber);
  994. }
  995. }
  996. return false;
  997. }
  998. bool gmCodeGenPrivate::GenExprOpAnd(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  999. {
  1000. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION && a_node->m_subTypeType == CTNOT_AND);
  1001. unsigned int loc1, loc2;
  1002. // Generate expression 1
  1003. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  1004. loc1 = a_byteCode->Skip(SIZEOF_BC_BRA);
  1005. // Generate expression 2
  1006. a_byteCode->Emit(BC_POP);
  1007. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  1008. // Seek back and finish expression 1
  1009. loc2 = (int)a_byteCode->Seek(loc1);
  1010. a_byteCode->EmitPtr(BC_BRZK, loc2);
  1011. a_byteCode->Seek(loc2);
  1012. return true;
  1013. }
  1014. bool gmCodeGenPrivate::GenExprOpOr(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1015. {
  1016. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION && a_node->m_subTypeType == CTNOT_OR);
  1017. unsigned int loc1, loc2;
  1018. // Generate expression 1
  1019. if(!Generate(a_node->m_children[0], a_byteCode)) return false;
  1020. loc1 = a_byteCode->Skip(SIZEOF_BC_BRA);
  1021. // Generate expression 2
  1022. a_byteCode->Emit(BC_POP);
  1023. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  1024. // Seek back and finish expression 1
  1025. loc2 = (int)a_byteCode->Seek(loc1);
  1026. a_byteCode->EmitPtr(BC_BRNZK, loc2);
  1027. a_byteCode->Seek(loc2);
  1028. return true;
  1029. }
  1030. bool gmCodeGenPrivate::GenExprOpAssign(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1031. {
  1032. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_OPERATION);
  1033. // value on left hand side must be an l-value... ie, a dot, array or identifier.
  1034. const gmCodeTreeNode * lValue = a_node->m_children[0];
  1035. int type = 0;
  1036. if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_OPERATION && lValue->m_subTypeType == CTNOT_DOT)
  1037. {
  1038. // Generate half l-value
  1039. if(!Generate(lValue->m_children[0], a_byteCode)) return false;
  1040. type = 0;
  1041. }
  1042. else if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_OPERATION && lValue->m_subTypeType == CTNOT_ARRAY_INDEX)
  1043. {
  1044. // Generate half l-value
  1045. if(!Generate(lValue->m_children[0], a_byteCode)) return false;
  1046. if(!Generate(lValue->m_children[1], a_byteCode)) return false;
  1047. type = 1;
  1048. }
  1049. else if(lValue->m_type == CTNT_EXPRESSION && lValue->m_subType == CTNET_IDENTIFIER)
  1050. {
  1051. type = 2;
  1052. }
  1053. else
  1054. {
  1055. if(m_log) m_log->LogEntry("error (%d) illegal l-value for '=' operator", a_node->m_lineNumber);
  1056. return false;
  1057. }
  1058. // Generate r-value
  1059. if(!Generate(a_node->m_children[1], a_byteCode)) return false;
  1060. // complete assignment
  1061. if(type == 0)
  1062. {
  1063. a_byteCode->EmitPtr(BC_SETDOT, m_hooks->GetSymbolId(lValue->m_children[1]->m_data.m_string));
  1064. }
  1065. else if(type == 1)
  1066. {
  1067. a_byteCode->Emit(BC_SETIND);
  1068. }
  1069. else if(type == 2)
  1070. {
  1071. gmCodeTreeVariableType vtype;
  1072. int offset = m_currentFunction->GetVariableOffset(lValue->m_data.m_string, vtype);
  1073. // if local, set local regardless
  1074. // if member set this
  1075. // if global, set global
  1076. // set and add local
  1077. if((lValue->m_flags & gmCodeTreeNode::CTN_MEMBER) > 0)
  1078. {
  1079. return a_byteCode->EmitPtr(BC_SETTHIS, m_hooks->GetSymbolId(lValue->m_data.m_string));
  1080. }
  1081. if(offset >= 0 && vtype == CTVT_LOCAL)
  1082. {
  1083. return a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  1084. }
  1085. else if(offset == -1)
  1086. {
  1087. if(vtype == CTVT_MEMBER)
  1088. {
  1089. return a_byteCode->EmitPtr(BC_SETTHIS, m_hooks->GetSymbolId(lValue->m_data.m_string));
  1090. }
  1091. else if(vtype == CTVT_GLOBAL)
  1092. {
  1093. return a_byteCode->EmitPtr(BC_SETGLOBAL, m_hooks->GetSymbolId(lValue->m_data.m_string));
  1094. }
  1095. if(m_log) m_log->LogEntry("internal error");
  1096. return false;
  1097. }
  1098. offset = m_currentFunction->SetVariableType(lValue->m_data.m_string, CTVT_LOCAL);
  1099. return a_byteCode->Emit(BC_SETLOCAL, (gmuint32) offset);
  1100. }
  1101. else
  1102. {
  1103. // paranoia
  1104. if(m_log) m_log->LogEntry("internal error");
  1105. return false;
  1106. }
  1107. return true;
  1108. }
  1109. bool gmCodeGenPrivate::GenExprConstant(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1110. {
  1111. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_CONSTANT);
  1112. switch(a_node->m_subTypeType)
  1113. {
  1114. case CTNCT_INT : // INT
  1115. {
  1116. if(a_node->m_data.m_iValue == 0)
  1117. {
  1118. a_byteCode->Emit(BC_PUSHINT0);
  1119. }
  1120. else if(a_node->m_data.m_iValue == 1)
  1121. {
  1122. a_byteCode->Emit(BC_PUSHINT1);
  1123. }
  1124. else
  1125. {
  1126. // Note, must support 32 or 64 bit float
  1127. a_byteCode->Emit(BC_PUSHINT, a_node->m_data.m_iValue);
  1128. }
  1129. break;
  1130. }
  1131. case CTNCT_FLOAT : // FLOAT
  1132. {
  1133. // Note, must support 32 or 64 bit float
  1134. a_byteCode->Emit(BC_PUSHFP, a_node->m_data.m_fValue);
  1135. break;
  1136. }
  1137. case CTNCT_STRING : // STRING
  1138. {
  1139. a_byteCode->EmitPtr(BC_PUSHSTR, m_hooks->GetStringId(a_node->m_data.m_string));
  1140. break;
  1141. }
  1142. case CTNCT_NULL : // NULL
  1143. {
  1144. a_byteCode->Emit(BC_PUSHNULL);
  1145. break;
  1146. }
  1147. default:
  1148. {
  1149. if(m_log) m_log->LogEntry("unkown constant type");
  1150. return false;
  1151. }
  1152. }
  1153. return true;
  1154. }
  1155. bool gmCodeGenPrivate::GenExprIdentifier(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1156. {
  1157. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_IDENTIFIER);
  1158. // if local, get local regardless
  1159. // if member, get this
  1160. // if global, get global
  1161. // get global
  1162. if((a_node->m_flags & gmCodeTreeNode::CTN_MEMBER) > 0)
  1163. {
  1164. return a_byteCode->EmitPtr(BC_GETTHIS, m_hooks->GetSymbolId(a_node->m_data.m_string));
  1165. }
  1166. gmCodeTreeVariableType type;
  1167. int offset = m_currentFunction->GetVariableOffset(a_node->m_data.m_string, type);
  1168. if(offset >= 0 && type == CTVT_LOCAL)
  1169. {
  1170. return a_byteCode->Emit(BC_GETLOCAL, (gmuint32) offset);
  1171. }
  1172. else if(offset != -2)
  1173. {
  1174. if(type == CTVT_MEMBER)
  1175. {
  1176. return a_byteCode->EmitPtr(BC_GETTHIS, m_hooks->GetSymbolId(a_node->m_data.m_string));
  1177. }
  1178. else if(type == CTVT_GLOBAL)
  1179. {
  1180. return a_byteCode->EmitPtr(BC_GETGLOBAL, m_hooks->GetSymbolId(a_node->m_data.m_string));
  1181. }
  1182. if(m_log) m_log->LogEntry("internal error");
  1183. return false;
  1184. }
  1185. return a_byteCode->EmitPtr(BC_GETGLOBAL, m_hooks->GetSymbolId(a_node->m_data.m_string));
  1186. }
  1187. bool gmCodeGenPrivate::GenExprCall(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1188. {
  1189. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_CALL);
  1190. // mark the stack.
  1191. int stackLevel = a_byteCode->GetTos();
  1192. // if callee is a dot function, push left side of dot as 'this'
  1193. const gmCodeTreeNode * callee = a_node->m_children[0];
  1194. if(callee->m_type == CTNT_EXPRESSION && callee->m_subType == CTNET_OPERATION && callee->m_subTypeType == CTNOT_DOT)
  1195. {
  1196. if(!Generate(callee->m_children[0], a_byteCode)) return false;
  1197. a_byteCode->Emit(BC_DUP);
  1198. a_byteCode->EmitPtr(BC_GETDOT, m_hooks->GetSymbolId(callee->m_children[1]->m_data.m_string));
  1199. }
  1200. else
  1201. {
  1202. if(a_node->m_children[2])
  1203. {
  1204. if(!Generate(a_node->m_children[2], a_byteCode)) return false;
  1205. }
  1206. else
  1207. {
  1208. #if GM_COMPILE_PASS_THIS_ALWAYS
  1209. a_byteCode->Emit(BC_PUSHTHIS);
  1210. #else // !GM_COMPILE_PASS_THIS_ALWAYS
  1211. // if the lvalue is a member, pass 'this', otherwise pass 'null'
  1212. bool pushed = false;
  1213. if(callee->m_type == CTNT_EXPRESSION && callee->m_subType == CTNET_IDENTIFIER)
  1214. {
  1215. gmCodeTreeVariableType vtype;
  1216. int offset = m_currentFunction->GetVariableOffset(callee->m_data.m_string, vtype);
  1217. if(((callee->m_flags & gmCodeTreeNode::CTN_MEMBER) > 0) || (offset == -1 && vtype == CTVT_MEMBER))
  1218. {
  1219. a_byteCode->Emit(BC_PUSHTHIS);
  1220. pushed = true;
  1221. }
  1222. }
  1223. if(!pushed)
  1224. {
  1225. a_byteCode->Emit(BC_PUSHNULL);
  1226. }
  1227. #endif // !GM_COMPILE_PASS_THIS_ALWAYS
  1228. }
  1229. if(!Generate(callee, a_byteCode)) return false;
  1230. }
  1231. // push parameters, count the number of parameters
  1232. gmuint32 numParams = 0;
  1233. const gmCodeTreeNode * params = a_node->m_children[1];
  1234. while(params)
  1235. {
  1236. ++numParams;
  1237. if(!Generate(params, a_byteCode, false)) return false;
  1238. params = params->m_sibling;
  1239. }
  1240. // call
  1241. a_byteCode->Emit(BC_CALL, (gmuint32) numParams);
  1242. // restore the stack level.
  1243. a_byteCode->SetTos(stackLevel + 1);
  1244. return true;
  1245. }
  1246. bool gmCodeGenPrivate::GenExprThis(const gmCodeTreeNode * a_node, gmByteCodeGen * a_byteCode)
  1247. {
  1248. GM_ASSERT(a_node->m_type == CTNT_EXPRESSION && a_node->m_subType == CTNET_THIS);
  1249. return a_byteCode->Emit(BC_PUSHTHIS);
  1250. }
  1251. gmCodeGenPrivate::FunctionState::FunctionState()
  1252. {
  1253. m_debugName = NULL;
  1254. m_numLocals = 0;
  1255. m_currentLine = 1;
  1256. m_byteCode.Reset(this);
  1257. }
  1258. void gmCodeGenPrivate::FunctionState::Reset()
  1259. {
  1260. m_debugName = NULL;
  1261. m_variables.Reset();
  1262. m_numLocals = 0;
  1263. m_currentLine = 1;
  1264. m_byteCode.Reset(this);
  1265. m_lineInfo.Reset();
  1266. }
  1267. int gmCodeGenPrivate::FunctionState::GetVariableOffset(const char * a_symbol, gmCodeTreeVariableType &a_type)
  1268. {
  1269. for(gmint v = 0; v < m_variables.Count(); ++v)
  1270. {
  1271. Variable &variable = m_variables[v];
  1272. if(strcmp(variable.m_symbol, a_symbol) == 0)
  1273. {
  1274. a_type = variable.m_type;
  1275. if(variable.m_type == CTVT_LOCAL)
  1276. {
  1277. return variable.m_offset;
  1278. }
  1279. return -1;
  1280. }
  1281. }
  1282. a_type = CTVT_GLOBAL;
  1283. return -2;
  1284. }
  1285. int gmCodeGenPrivate::FunctionState::SetVariableType(const char * a_symbol, gmCodeTreeVariableType a_type)
  1286. {
  1287. for(gmint v = 0; v < m_variables.Count(); ++v)
  1288. {
  1289. Variable &variable = m_variables[v];
  1290. if(strcmp(variable.m_symbol, a_symbol) == 0)
  1291. {
  1292. variable.m_type = a_type;
  1293. // if this variable was previously not a local, be is now being declared as local, get a stack offset.
  1294. if(a_type == CTVT_LOCAL && variable.m_offset == -1)
  1295. {
  1296. variable.m_offset = m_numLocals++;
  1297. }
  1298. return variable.m_offset;
  1299. }
  1300. }
  1301. Variable &variable = m_variables.InsertLast();
  1302. // if the new variable is a local, get a stack offset for it.
  1303. if(a_type == CTVT_LOCAL)
  1304. {
  1305. variable.m_offset = m_numLocals++;
  1306. }
  1307. else variable.m_offset = -1;
  1308. variable.m_type = a_type;
  1309. variable.m_symbol = a_symbol;
  1310. return variable.m_offset;
  1311. }
  1312. gmCodeGenPrivate::FunctionState * gmCodeGenPrivate::PushFunction()
  1313. {
  1314. if(m_currentFunction)
  1315. {
  1316. if(m_currentFunction != m_functionStack.GetLast())
  1317. {
  1318. m_currentFunction = m_functionStack.GetNext(m_currentFunction);
  1319. }
  1320. else
  1321. {
  1322. m_currentFunction = GM_NEW( FunctionState() );
  1323. m_functionStack.InsertLast(m_currentFunction);
  1324. }
  1325. }
  1326. else
  1327. {
  1328. if(m_functionStack.IsEmpty())
  1329. {
  1330. m_currentFunction = GM_NEW( FunctionState() );
  1331. m_functionStack.InsertLast(m_currentFunction);
  1332. }
  1333. else
  1334. {
  1335. m_currentFunction = m_functionStack.GetFirst();
  1336. }
  1337. }
  1338. m_currentFunction->Reset();
  1339. m_currentFunction->m_byteCode.SetSwapEndianOnWrite(m_hooks->SwapEndian());
  1340. // if we are debugging, set up some line number debugging.
  1341. if(m_debug)
  1342. {
  1343. m_currentFunction->m_byteCode.m_emitCallback = gmLineNumberCallback;
  1344. }
  1345. return m_currentFunction;
  1346. }
  1347. gmCodeGenPrivate::FunctionState * gmCodeGenPrivate::PopFunction()
  1348. {
  1349. if(m_currentFunction)
  1350. {
  1351. m_currentFunction->Reset();
  1352. m_currentFunction = m_functionStack.GetPrev(m_currentFunction);
  1353. if(!m_functionStack.IsValid(m_currentFunction))
  1354. {
  1355. m_currentFunction = NULL;
  1356. }
  1357. }
  1358. return m_currentFunction;
  1359. }
  1360. void gmCodeGenPrivate::PushLoop()
  1361. {
  1362. LoopInfo * loop = &m_loopStack.InsertLast();
  1363. m_currentLoop = (int)m_loopStack.Count()-1;
  1364. loop->m_breaks = -1;
  1365. loop->m_continues = -1;
  1366. }
  1367. void gmCodeGenPrivate::PopLoop()
  1368. {
  1369. m_loopStack.RemoveLast();
  1370. if(m_loopStack.Count())
  1371. {
  1372. m_currentLoop = (int)m_loopStack.Count() - 1;
  1373. }
  1374. else
  1375. {
  1376. m_currentLoop = -1;
  1377. }
  1378. }
  1379. void gmCodeGenPrivate::ApplyPatches(int a_patches, gmByteCodeGen * a_byteCode, gmuint32 a_value)
  1380. {
  1381. int pos = (int)a_byteCode->Tell();
  1382. while(a_patches >= 0)
  1383. {
  1384. Patch * curPatch = &m_patches[a_patches];
  1385. a_byteCode->Seek(curPatch->m_address);
  1386. *a_byteCode << (gmptr)a_value; // NOTE: we are using gmptr size addresses/offsets
  1387. a_patches = curPatch->m_next;
  1388. }
  1389. a_byteCode->Seek(pos);
  1390. }