gmCodeGen.cpp 45 KB

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