gd_compiler.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*************************************************************************/
  2. /* gd_compiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "gd_compiler.h"
  30. #include "gd_script.h"
  31. /* TODO:
  32. *AND and OR need early abort
  33. -Inheritance properly process (done?)
  34. *create built in initializer and constructor
  35. *assign operators
  36. *build arrays and dictionaries
  37. *call parent constructor
  38. */
  39. void GDCompiler::_set_error(const String& p_error,const GDParser::Node *p_node) {
  40. if (error!="")
  41. return;
  42. error=p_error;
  43. err_line=p_node->line;
  44. err_column=p_node->column;
  45. }
  46. bool GDCompiler::_create_unary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level) {
  47. ERR_FAIL_COND_V(on->arguments.size()!=1,false);
  48. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level);
  49. if (src_address_a<0)
  50. return false;
  51. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  52. codegen.opcodes.push_back(op); //which operator
  53. codegen.opcodes.push_back(src_address_a); // argument 1
  54. codegen.opcodes.push_back(src_address_a); // argument 2 (repeated)
  55. //codegen.opcodes.push_back(GDFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
  56. return true;
  57. }
  58. bool GDCompiler::_create_binary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level) {
  59. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  60. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level);
  61. if (src_address_a<0)
  62. return false;
  63. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  64. p_stack_level++; //uses stack for return, increase stack
  65. int src_address_b = _parse_expression(codegen,on->arguments[1],p_stack_level);
  66. if (src_address_b<0)
  67. return false;
  68. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  69. codegen.opcodes.push_back(op); //which operator
  70. codegen.opcodes.push_back(src_address_a); // argument 1
  71. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  72. return true;
  73. }
  74. /*
  75. int GDCompiler::_parse_subexpression(CodeGen& codegen,const GDParser::Node *p_expression) {
  76. int ret = _parse_expression(codegen,p_expression);
  77. if (ret<0)
  78. return ret;
  79. if (ret&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  80. codegen.stack_level++;
  81. codegen.check_max_stack_level();
  82. //stack was used, keep value
  83. }
  84. return ret;
  85. }
  86. */
  87. int GDCompiler::_parse_assign_right_expression(CodeGen& codegen,const GDParser::OperatorNode *p_expression, int p_stack_level) {
  88. Variant::Operator var_op=Variant::OP_MAX;
  89. switch(p_expression->op) {
  90. case GDParser::OperatorNode::OP_ASSIGN_ADD: var_op=Variant::OP_ADD; break;
  91. case GDParser::OperatorNode::OP_ASSIGN_SUB: var_op=Variant::OP_SUBSTRACT; break;
  92. case GDParser::OperatorNode::OP_ASSIGN_MUL: var_op=Variant::OP_MULTIPLY; break;
  93. case GDParser::OperatorNode::OP_ASSIGN_DIV: var_op=Variant::OP_DIVIDE; break;
  94. case GDParser::OperatorNode::OP_ASSIGN_MOD: var_op=Variant::OP_MODULE; break;
  95. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: var_op=Variant::OP_SHIFT_LEFT; break;
  96. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: var_op=Variant::OP_SHIFT_RIGHT; break;
  97. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND: var_op=Variant::OP_BIT_AND; break;
  98. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR: var_op=Variant::OP_BIT_OR; break;
  99. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR: var_op=Variant::OP_BIT_XOR; break;
  100. case GDParser::OperatorNode::OP_ASSIGN: {
  101. //none
  102. } break;
  103. default: {
  104. ERR_FAIL_V(-1);
  105. }
  106. }
  107. if (var_op==Variant::OP_MAX) {
  108. return _parse_expression(codegen,p_expression->arguments[1],p_stack_level);
  109. }
  110. if (!_create_binary_operator(codegen,p_expression,var_op,p_stack_level))
  111. return -1;
  112. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  113. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  114. codegen.alloc_stack(p_stack_level);
  115. return dst_addr;
  116. }
  117. int GDCompiler::_parse_expression(CodeGen& codegen,const GDParser::Node *p_expression, int p_stack_level,bool p_root) {
  118. switch(p_expression->type) {
  119. //should parse variable declaration and adjust stack accordingly...
  120. case GDParser::Node::TYPE_IDENTIFIER: {
  121. //return identifier
  122. //wait, identifier could be a local variable or something else... careful here, must reference properly
  123. //as stack may be more interesting to work with
  124. //This could be made much simpler by just indexing "self", but done this way (with custom self-addressing modes) increases peformance a lot.
  125. const GDParser::IdentifierNode *in = static_cast<const GDParser::IdentifierNode*>(p_expression);
  126. StringName identifier = in->name;
  127. // TRY STACK!
  128. if (codegen.stack_identifiers.has(identifier)) {
  129. int pos = codegen.stack_identifiers[identifier];
  130. return pos|(GDFunction::ADDR_TYPE_STACK_VARIABLE<<GDFunction::ADDR_BITS);
  131. }
  132. //TRY ARGUMENTS!
  133. if (!codegen.function_node || !codegen.function_node->_static) {
  134. // TRY MEMBER VARIABLES!
  135. //static function
  136. if (codegen.script->member_indices.has(identifier)) {
  137. int idx = codegen.script->member_indices[identifier];
  138. return idx|(GDFunction::ADDR_TYPE_MEMBER<<GDFunction::ADDR_BITS); //argument (stack root)
  139. }
  140. }
  141. //TRY CLASS CONSTANTS
  142. GDScript *owner = codegen.script;
  143. while (owner) {
  144. GDScript *scr = owner;
  145. GDNativeClass *nc=NULL;
  146. while(scr) {
  147. if (scr->constants.has(identifier)) {
  148. //int idx=scr->constants[identifier];
  149. int idx = codegen.get_name_map_pos(identifier);
  150. return idx|(GDFunction::ADDR_TYPE_CLASS_CONSTANT<<GDFunction::ADDR_BITS); //argument (stack root)
  151. }
  152. if (scr->native.is_valid())
  153. nc=scr->native.ptr();
  154. scr=scr->_base;
  155. }
  156. // CLASS C++ Integer Constant
  157. if (nc) {
  158. bool success=false;
  159. int constant = ObjectTypeDB::get_integer_constant(nc->get_name(),identifier,&success);
  160. if (success) {
  161. Variant key=constant;
  162. int idx;
  163. if (!codegen.constant_map.has(key)) {
  164. idx=codegen.constant_map.size();
  165. codegen.constant_map[key]=idx;
  166. } else {
  167. idx=codegen.constant_map[key];
  168. }
  169. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //make it a local constant (faster access)
  170. }
  171. }
  172. owner=owner->_owner;
  173. }
  174. /*
  175. handled in constants now
  176. if (codegen.script->subclasses.has(identifier)) {
  177. //same with a subclass, make it a local constant.
  178. int idx = codegen.get_constant_pos(codegen.script->subclasses[identifier]);
  179. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //make it a local constant (faster access)
  180. }*/
  181. if (GDScriptLanguage::get_singleton()->get_global_map().has(identifier)) {
  182. int idx = GDScriptLanguage::get_singleton()->get_global_map()[identifier];
  183. return idx|(GDFunction::ADDR_TYPE_GLOBAL<<GDFunction::ADDR_BITS); //argument (stack root)
  184. }
  185. //not found, error
  186. _set_error("Identifier not found: "+String(identifier),p_expression);
  187. return -1;
  188. } break;
  189. case GDParser::Node::TYPE_CONSTANT: {
  190. //return constant
  191. const GDParser::ConstantNode *cn = static_cast<const GDParser::ConstantNode*>(p_expression);
  192. int idx;
  193. if (!codegen.constant_map.has(cn->value)) {
  194. idx=codegen.constant_map.size();
  195. codegen.constant_map[cn->value]=idx;
  196. } else {
  197. idx=codegen.constant_map[cn->value];
  198. }
  199. return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<<GDFunction::ADDR_BITS); //argument (stack root)
  200. } break;
  201. case GDParser::Node::TYPE_SELF: {
  202. //return constant
  203. if (codegen.function_node && codegen.function_node->_static) {
  204. _set_error("'self' not present in static function!",p_expression);
  205. return -1;
  206. }
  207. return (GDFunction::ADDR_TYPE_SELF<<GDFunction::ADDR_BITS);
  208. } break;
  209. case GDParser::Node::TYPE_ARRAY: {
  210. const GDParser::ArrayNode *an = static_cast<const GDParser::ArrayNode*>(p_expression);
  211. Vector<int> values;
  212. int slevel=p_stack_level;
  213. for(int i=0;i<an->elements.size();i++) {
  214. int ret = _parse_expression(codegen,an->elements[i],slevel);
  215. if (ret<0)
  216. return ret;
  217. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  218. slevel++;
  219. codegen.alloc_stack(slevel);
  220. }
  221. values.push_back(ret);
  222. }
  223. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT_ARRAY);
  224. codegen.opcodes.push_back(values.size());
  225. for(int i=0;i<values.size();i++)
  226. codegen.opcodes.push_back(values[i]);
  227. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  228. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  229. codegen.alloc_stack(p_stack_level);
  230. return dst_addr;
  231. } break;
  232. case GDParser::Node::TYPE_DICTIONARY: {
  233. const GDParser::DictionaryNode *dn = static_cast<const GDParser::DictionaryNode*>(p_expression);
  234. Vector<int> values;
  235. int slevel=p_stack_level;
  236. for(int i=0;i<dn->elements.size();i++) {
  237. int ret = _parse_expression(codegen,dn->elements[i].key,slevel);
  238. if (ret<0)
  239. return ret;
  240. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  241. slevel++;
  242. codegen.alloc_stack(slevel);
  243. }
  244. values.push_back(ret);
  245. ret = _parse_expression(codegen,dn->elements[i].value,slevel);
  246. if (ret<0)
  247. return ret;
  248. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  249. slevel++;
  250. codegen.alloc_stack(slevel);
  251. }
  252. values.push_back(ret);
  253. }
  254. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT_DICTIONARY);
  255. codegen.opcodes.push_back(dn->elements.size());
  256. for(int i=0;i<values.size();i++)
  257. codegen.opcodes.push_back(values[i]);
  258. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  259. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  260. codegen.alloc_stack(p_stack_level);
  261. return dst_addr;
  262. } break;
  263. case GDParser::Node::TYPE_OPERATOR: {
  264. //hell breaks loose
  265. const GDParser::OperatorNode *on = static_cast<const GDParser::OperatorNode*>(p_expression);
  266. switch(on->op) {
  267. //call/constructor operator
  268. case GDParser::OperatorNode::OP_PARENT_CALL: {
  269. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  270. const GDParser::IdentifierNode *in = (const GDParser::IdentifierNode *)on->arguments[0];
  271. Vector<int> arguments;
  272. int slevel = p_stack_level;
  273. for(int i=1;i<on->arguments.size();i++) {
  274. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  275. if (ret<0)
  276. return ret;
  277. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  278. slevel++;
  279. codegen.alloc_stack(slevel);
  280. }
  281. arguments.push_back(ret);
  282. }
  283. //push call bytecode
  284. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE); // basic type constructor
  285. codegen.opcodes.push_back(codegen.get_name_map_pos(in->name)); //instance
  286. codegen.opcodes.push_back(arguments.size()); //argument count
  287. codegen.alloc_call(arguments.size());
  288. for(int i=0;i<arguments.size();i++)
  289. codegen.opcodes.push_back(arguments[i]); //arguments
  290. } break;
  291. case GDParser::OperatorNode::OP_CALL: {
  292. if (on->arguments[0]->type==GDParser::Node::TYPE_TYPE) {
  293. //construct a basic type
  294. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  295. const GDParser::TypeNode *tn = (const GDParser::TypeNode *)on->arguments[0];
  296. int vtype = tn->vtype;
  297. Vector<int> arguments;
  298. int slevel = p_stack_level;
  299. for(int i=1;i<on->arguments.size();i++) {
  300. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  301. if (ret<0)
  302. return ret;
  303. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  304. slevel++;
  305. codegen.alloc_stack(slevel);
  306. }
  307. arguments.push_back(ret);
  308. }
  309. //push call bytecode
  310. codegen.opcodes.push_back(GDFunction::OPCODE_CONSTRUCT); // basic type constructor
  311. codegen.opcodes.push_back(vtype); //instance
  312. codegen.opcodes.push_back(arguments.size()); //argument count
  313. codegen.alloc_call(arguments.size());
  314. for(int i=0;i<arguments.size();i++)
  315. codegen.opcodes.push_back(arguments[i]); //arguments
  316. } else if (on->arguments[0]->type==GDParser::Node::TYPE_BUILT_IN_FUNCTION) {
  317. //built in function
  318. ERR_FAIL_COND_V(on->arguments.size()<1,-1);
  319. Vector<int> arguments;
  320. int slevel = p_stack_level;
  321. for(int i=1;i<on->arguments.size();i++) {
  322. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  323. if (ret<0)
  324. return ret;
  325. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  326. slevel++;
  327. codegen.alloc_stack(slevel);
  328. }
  329. arguments.push_back(ret);
  330. }
  331. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_BUILT_IN);
  332. codegen.opcodes.push_back(static_cast<const GDParser::BuiltInFunctionNode*>(on->arguments[0])->function);
  333. codegen.opcodes.push_back(on->arguments.size()-1);
  334. codegen.alloc_call(on->arguments.size()-1);
  335. for(int i=0;i<arguments.size();i++)
  336. codegen.opcodes.push_back(arguments[i]);
  337. } else {
  338. //regular function
  339. ERR_FAIL_COND_V(on->arguments.size()<2,-1);
  340. const GDParser::Node *instance = on->arguments[0];
  341. bool in_static=false;
  342. if (instance->type==GDParser::Node::TYPE_SELF) {
  343. //room for optimization
  344. }
  345. Vector<int> arguments;
  346. int slevel = p_stack_level;
  347. for(int i=0;i<on->arguments.size();i++) {
  348. int ret;
  349. if (i==0 && on->arguments[i]->type==GDParser::Node::TYPE_SELF && codegen.function_node && codegen.function_node->_static) {
  350. //static call to self
  351. ret=(GDFunction::ADDR_TYPE_CLASS<<GDFunction::ADDR_BITS);
  352. } else if (i==1) {
  353. if (on->arguments[i]->type!=GDParser::Node::TYPE_IDENTIFIER) {
  354. _set_error("Attempt to call a non-identifier.",on);
  355. return -1;
  356. }
  357. GDParser::IdentifierNode *id = static_cast<GDParser::IdentifierNode*>(on->arguments[i]);
  358. ret=codegen.get_name_map_pos(id->name);
  359. } else {
  360. ret = _parse_expression(codegen,on->arguments[i],slevel);
  361. if (ret<0)
  362. return ret;
  363. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  364. slevel++;
  365. codegen.alloc_stack(slevel);
  366. }
  367. }
  368. arguments.push_back(ret);
  369. }
  370. codegen.opcodes.push_back(p_root?GDFunction::OPCODE_CALL:GDFunction::OPCODE_CALL_RETURN); // perform operator
  371. codegen.opcodes.push_back(on->arguments.size()-2);
  372. codegen.alloc_call(on->arguments.size()-2);
  373. for(int i=0;i<arguments.size();i++)
  374. codegen.opcodes.push_back(arguments[i]);
  375. }
  376. } break;
  377. case GDParser::OperatorNode::OP_YIELD: {
  378. ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size()!=2,-1);
  379. Vector<int> arguments;
  380. int slevel = p_stack_level;
  381. for(int i=0;i<on->arguments.size();i++) {
  382. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  383. if (ret<0)
  384. return ret;
  385. if (ret&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  386. slevel++;
  387. codegen.alloc_stack(slevel);
  388. }
  389. arguments.push_back(ret);
  390. }
  391. //push call bytecode
  392. codegen.opcodes.push_back(arguments.size()==0?GDFunction::OPCODE_YIELD:GDFunction::OPCODE_YIELD_SIGNAL); // basic type constructor
  393. for(int i=0;i<arguments.size();i++)
  394. codegen.opcodes.push_back(arguments[i]); //arguments
  395. codegen.opcodes.push_back(GDFunction::OPCODE_YIELD_RESUME);
  396. //next will be where to place the result :)
  397. } break;
  398. //indexing operator
  399. case GDParser::OperatorNode::OP_INDEX:
  400. case GDParser::OperatorNode::OP_INDEX_NAMED: {
  401. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  402. int slevel = p_stack_level;
  403. bool named=(on->op==GDParser::OperatorNode::OP_INDEX_NAMED);
  404. int from = _parse_expression(codegen,on->arguments[0],slevel);
  405. if (from<0)
  406. return from;
  407. int index;
  408. if (named) {
  409. index=codegen.get_name_map_pos(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  410. } else {
  411. if (on->arguments[1]->type==GDParser::Node::TYPE_CONSTANT && static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value.get_type()==Variant::STRING) {
  412. //also, somehow, named (speed up anyway)
  413. StringName name = static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value;
  414. index=codegen.get_name_map_pos(name);
  415. named=true;
  416. } else {
  417. //regular indexing
  418. if (from&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  419. slevel++;
  420. codegen.alloc_stack(slevel);
  421. }
  422. index = _parse_expression(codegen,on->arguments[1],slevel);
  423. if (index<0)
  424. return index;
  425. }
  426. }
  427. codegen.opcodes.push_back(named?GDFunction::OPCODE_GET_NAMED:GDFunction::OPCODE_GET); // perform operator
  428. codegen.opcodes.push_back(from); // argument 1
  429. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  430. } break;
  431. case GDParser::OperatorNode::OP_AND: {
  432. // AND operator with early out on failure
  433. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  434. if (res<0)
  435. return res;
  436. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  437. codegen.opcodes.push_back(res);
  438. int jump_fail_pos=codegen.opcodes.size();
  439. codegen.opcodes.push_back(0);
  440. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  441. if (res<0)
  442. return res;
  443. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  444. codegen.opcodes.push_back(res);
  445. int jump_fail_pos2=codegen.opcodes.size();
  446. codegen.opcodes.push_back(0);
  447. codegen.alloc_stack(p_stack_level); //it will be used..
  448. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  449. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  450. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  451. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  452. codegen.opcodes[jump_fail_pos]=codegen.opcodes.size();
  453. codegen.opcodes[jump_fail_pos2]=codegen.opcodes.size();
  454. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  455. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  456. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  457. } break;
  458. case GDParser::OperatorNode::OP_OR: {
  459. // OR operator with early out on success
  460. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  461. if (res<0)
  462. return res;
  463. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  464. codegen.opcodes.push_back(res);
  465. int jump_success_pos=codegen.opcodes.size();
  466. codegen.opcodes.push_back(0);
  467. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  468. if (res<0)
  469. return res;
  470. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  471. codegen.opcodes.push_back(res);
  472. int jump_success_pos2=codegen.opcodes.size();
  473. codegen.opcodes.push_back(0);
  474. codegen.alloc_stack(p_stack_level); //it will be used..
  475. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  476. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  477. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  478. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  479. codegen.opcodes[jump_success_pos]=codegen.opcodes.size();
  480. codegen.opcodes[jump_success_pos2]=codegen.opcodes.size();
  481. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  482. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  483. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  484. } break;
  485. //unary operators
  486. case GDParser::OperatorNode::OP_NEG: { if (!_create_unary_operator(codegen,on,Variant::OP_NEGATE,p_stack_level)) return -1;} break;
  487. case GDParser::OperatorNode::OP_NOT: { if (!_create_unary_operator(codegen,on,Variant::OP_NOT,p_stack_level)) return -1;} break;
  488. case GDParser::OperatorNode::OP_BIT_INVERT: { if (!_create_unary_operator(codegen,on,Variant::OP_BIT_NEGATE,p_stack_level)) return -1;} break;
  489. case GDParser::OperatorNode::OP_PREINC: { } break; //?
  490. case GDParser::OperatorNode::OP_PREDEC: { } break;
  491. case GDParser::OperatorNode::OP_INC: { } break;
  492. case GDParser::OperatorNode::OP_DEC: { } break;
  493. //binary operators (in precedence order)
  494. case GDParser::OperatorNode::OP_IN: { if (!_create_binary_operator(codegen,on,Variant::OP_IN,p_stack_level)) return -1;} break;
  495. case GDParser::OperatorNode::OP_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_EQUAL,p_stack_level)) return -1;} break;
  496. case GDParser::OperatorNode::OP_NOT_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_NOT_EQUAL,p_stack_level)) return -1;} break;
  497. case GDParser::OperatorNode::OP_LESS: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS,p_stack_level)) return -1;} break;
  498. case GDParser::OperatorNode::OP_LESS_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS_EQUAL,p_stack_level)) return -1;} break;
  499. case GDParser::OperatorNode::OP_GREATER: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER,p_stack_level)) return -1;} break;
  500. case GDParser::OperatorNode::OP_GREATER_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER_EQUAL,p_stack_level)) return -1;} break;
  501. case GDParser::OperatorNode::OP_ADD: { if (!_create_binary_operator(codegen,on,Variant::OP_ADD,p_stack_level)) return -1;} break;
  502. case GDParser::OperatorNode::OP_SUB: { if (!_create_binary_operator(codegen,on,Variant::OP_SUBSTRACT,p_stack_level)) return -1;} break;
  503. case GDParser::OperatorNode::OP_MUL: { if (!_create_binary_operator(codegen,on,Variant::OP_MULTIPLY,p_stack_level)) return -1;} break;
  504. case GDParser::OperatorNode::OP_DIV: { if (!_create_binary_operator(codegen,on,Variant::OP_DIVIDE,p_stack_level)) return -1;} break;
  505. case GDParser::OperatorNode::OP_MOD: { if (!_create_binary_operator(codegen,on,Variant::OP_MODULE,p_stack_level)) return -1;} break;
  506. //case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  507. //case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  508. case GDParser::OperatorNode::OP_BIT_AND: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_AND,p_stack_level)) return -1;} break;
  509. case GDParser::OperatorNode::OP_BIT_OR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_OR,p_stack_level)) return -1;} break;
  510. case GDParser::OperatorNode::OP_BIT_XOR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_XOR,p_stack_level)) return -1;} break;
  511. //shift
  512. case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  513. case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  514. //assignment operators
  515. case GDParser::OperatorNode::OP_ASSIGN_ADD:
  516. case GDParser::OperatorNode::OP_ASSIGN_SUB:
  517. case GDParser::OperatorNode::OP_ASSIGN_MUL:
  518. case GDParser::OperatorNode::OP_ASSIGN_DIV:
  519. case GDParser::OperatorNode::OP_ASSIGN_MOD:
  520. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  521. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  522. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND:
  523. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR:
  524. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  525. case GDParser::OperatorNode::OP_ASSIGN: {
  526. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  527. if (on->arguments[0]->type==GDParser::Node::TYPE_OPERATOR && (static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX || static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX_NAMED)) {
  528. //SET (chained) MODE!!
  529. int slevel=p_stack_level;
  530. GDParser::OperatorNode* op = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  531. /* Find chain of sets */
  532. List<GDParser::OperatorNode*> chain;
  533. {
  534. //create get/set chain
  535. GDParser::OperatorNode* n=op;
  536. while(true) {
  537. chain.push_back(n);
  538. if (n->arguments[0]->type!=GDParser::Node::TYPE_OPERATOR)
  539. break;
  540. n = static_cast<GDParser::OperatorNode*>(n->arguments[0]);
  541. if (n->op!=GDParser::OperatorNode::OP_INDEX && n->op!=GDParser::OperatorNode::OP_INDEX_NAMED)
  542. break;
  543. }
  544. }
  545. /* Chain of gets */
  546. //get at (potential) root stack pos, so it can be returned
  547. int prev_pos = _parse_expression(codegen,chain.back()->get()->arguments[0],slevel);
  548. if (prev_pos<0)
  549. return prev_pos;
  550. int retval=prev_pos;
  551. //print_line("retval: "+itos(retval));
  552. if (retval&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  553. slevel++;
  554. codegen.alloc_stack(slevel);
  555. }
  556. Vector<int> setchain;
  557. int prev_key_idx=-1;
  558. for(List<GDParser::OperatorNode*>::Element *E=chain.back();E;E=E->prev()) {
  559. if (E==chain.front()) //ignore first
  560. break;
  561. bool named = E->get()->op==GDParser::OperatorNode::OP_INDEX_NAMED;
  562. int key_idx;
  563. if (named) {
  564. key_idx = codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(E->get()->arguments[1])->name);
  565. //printf("named key %x\n",key_idx);
  566. } else {
  567. if (prev_pos&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  568. slevel++;
  569. codegen.alloc_stack(slevel);
  570. }
  571. GDParser::Node *key = E->get()->arguments[1];
  572. key_idx = _parse_expression(codegen,key,slevel);
  573. //printf("expr key %x\n",key_idx);
  574. //stack was raised here if retval was stack but..
  575. }
  576. if (key_idx<0)
  577. return key_idx;
  578. codegen.opcodes.push_back(named ? GDFunction::OPCODE_GET_NAMED : GDFunction::OPCODE_GET);
  579. codegen.opcodes.push_back(prev_pos);
  580. codegen.opcodes.push_back(key_idx);
  581. slevel++;
  582. codegen.alloc_stack(slevel);
  583. int dst_pos = (GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|slevel;
  584. codegen.opcodes.push_back(dst_pos);
  585. //add in reverse order, since it will be reverted
  586. setchain.push_back(dst_pos);
  587. setchain.push_back(key_idx);
  588. setchain.push_back(prev_pos);
  589. setchain.push_back(named ? GDFunction::OPCODE_SET_NAMED : GDFunction::OPCODE_SET);
  590. prev_pos=dst_pos;
  591. prev_key_idx=key_idx;
  592. }
  593. setchain.invert();
  594. int set_index;
  595. bool named=false;
  596. if (static_cast<const GDParser::OperatorNode*>(op)->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  597. set_index=codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name);
  598. named=true;
  599. } else {
  600. set_index = _parse_expression(codegen,op->arguments[1],slevel+1);
  601. named=false;
  602. }
  603. if (set_index<0)
  604. return set_index;
  605. if (set_index&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  606. slevel++;
  607. codegen.alloc_stack(slevel);
  608. }
  609. int set_value = _parse_assign_right_expression(codegen,on,slevel+1);
  610. if (set_value<0)
  611. return set_value;
  612. codegen.opcodes.push_back(named?GDFunction::OPCODE_SET_NAMED:GDFunction::OPCODE_SET);
  613. codegen.opcodes.push_back(prev_pos);
  614. codegen.opcodes.push_back(set_index);
  615. codegen.opcodes.push_back(set_value);
  616. for(int i=0;i<setchain.size();i+=4) {
  617. codegen.opcodes.push_back(setchain[i+0]);
  618. codegen.opcodes.push_back(setchain[i+1]);
  619. codegen.opcodes.push_back(setchain[i+2]);
  620. codegen.opcodes.push_back(setchain[i+3]);
  621. }
  622. return retval;
  623. } else {
  624. //ASSIGNMENT MODE!!
  625. int slevel = p_stack_level;
  626. int dst_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  627. if (dst_address_a<0)
  628. return -1;
  629. if (dst_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  630. slevel++;
  631. codegen.alloc_stack(slevel);
  632. }
  633. int src_address_b = _parse_assign_right_expression(codegen,on,slevel);
  634. if (src_address_b<0)
  635. return -1;
  636. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN); // perform operator
  637. codegen.opcodes.push_back(dst_address_a); // argument 1
  638. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  639. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  640. }
  641. } break;
  642. case GDParser::OperatorNode::OP_EXTENDS: {
  643. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  644. int slevel = p_stack_level;
  645. int src_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  646. if (src_address_a<0)
  647. return -1;
  648. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  649. slevel++; //uses stack for return, increase stack
  650. int src_address_b = _parse_expression(codegen,on->arguments[1],slevel);
  651. if (src_address_b<0)
  652. return -1;
  653. codegen.opcodes.push_back(GDFunction::OPCODE_EXTENDS_TEST); // perform operator
  654. codegen.opcodes.push_back(src_address_a); // argument 1
  655. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  656. } break;
  657. default: {
  658. ERR_EXPLAIN("Bug in bytecode compiler, unexpected operator #"+itos(on->op)+" in parse tree while parsing expression.");
  659. ERR_FAIL_V(0); //unreachable code
  660. } break;
  661. }
  662. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  663. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  664. codegen.alloc_stack(p_stack_level);
  665. return dst_addr;
  666. } break;
  667. //TYPE_TYPE,
  668. default: {
  669. ERR_EXPLAIN("Bug in bytecode compiler, unexpected node in parse tree while parsing expression.");
  670. ERR_FAIL_V(-1); //unreachable code
  671. } break;
  672. }
  673. ERR_FAIL_V(-1); //unreachable code
  674. }
  675. Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level,int p_break_addr,int p_continue_addr) {
  676. codegen.push_stack_identifiers();
  677. int new_identifiers=0;
  678. codegen.current_line=p_block->line;
  679. for(int i=0;i<p_block->statements.size();i++) {
  680. const GDParser::Node *s = p_block->statements[i];
  681. switch(s->type) {
  682. case GDParser::Node::TYPE_NEWLINE: {
  683. const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
  684. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  685. codegen.opcodes.push_back(nl->line);
  686. codegen.current_line=nl->line;
  687. } break;
  688. case GDParser::Node::TYPE_CONTROL_FLOW: {
  689. // try subblocks
  690. const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);
  691. switch(cf->cf_type) {
  692. case GDParser::ControlFlowNode::CF_IF: {
  693. #ifdef DEBUG_ENABLED
  694. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  695. codegen.opcodes.push_back(cf->line);
  696. codegen.current_line=cf->line;
  697. #endif
  698. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  699. if (ret<0)
  700. return ERR_PARSE_ERROR;
  701. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  702. codegen.opcodes.push_back(ret);
  703. int else_addr=codegen.opcodes.size();
  704. codegen.opcodes.push_back(0); //temporary
  705. Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
  706. if (err)
  707. return err;
  708. if (cf->body_else) {
  709. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  710. int end_addr=codegen.opcodes.size();
  711. codegen.opcodes.push_back(0);
  712. codegen.opcodes[else_addr]=codegen.opcodes.size();
  713. Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
  714. if (err)
  715. return err;
  716. codegen.opcodes[end_addr]=codegen.opcodes.size();
  717. } else {
  718. //end without else
  719. codegen.opcodes[else_addr]=codegen.opcodes.size();
  720. }
  721. } break;
  722. case GDParser::ControlFlowNode::CF_FOR: {
  723. int slevel=p_stack_level;
  724. int iter_stack_pos=slevel;
  725. int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  726. int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  727. int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  728. codegen.alloc_stack(slevel);
  729. codegen.push_stack_identifiers();
  730. codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);
  731. int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
  732. if (ret<0)
  733. return ERR_COMPILATION_FAILED;
  734. //assign container
  735. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
  736. codegen.opcodes.push_back(container_pos);
  737. codegen.opcodes.push_back(ret);
  738. //begin loop
  739. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
  740. codegen.opcodes.push_back(counter_pos);
  741. codegen.opcodes.push_back(container_pos);
  742. codegen.opcodes.push_back(codegen.opcodes.size()+4);
  743. codegen.opcodes.push_back(iterator_pos);
  744. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  745. codegen.opcodes.push_back(codegen.opcodes.size()+8);
  746. //break loop
  747. int break_pos=codegen.opcodes.size();
  748. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  749. codegen.opcodes.push_back(0); //skip code for next
  750. //next loop
  751. int continue_pos=codegen.opcodes.size();
  752. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
  753. codegen.opcodes.push_back(counter_pos);
  754. codegen.opcodes.push_back(container_pos);
  755. codegen.opcodes.push_back(break_pos);
  756. codegen.opcodes.push_back(iterator_pos);
  757. Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
  758. if (err)
  759. return err;
  760. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  761. codegen.opcodes.push_back(continue_pos);
  762. codegen.opcodes[break_pos+1]=codegen.opcodes.size();
  763. codegen.pop_stack_identifiers();
  764. } break;
  765. case GDParser::ControlFlowNode::CF_WHILE: {
  766. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  767. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  768. int break_addr=codegen.opcodes.size();
  769. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  770. codegen.opcodes.push_back(0);
  771. int continue_addr=codegen.opcodes.size();
  772. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  773. if (ret<0)
  774. return ERR_PARSE_ERROR;
  775. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  776. codegen.opcodes.push_back(ret);
  777. codegen.opcodes.push_back(break_addr);
  778. Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
  779. if (err)
  780. return err;
  781. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  782. codegen.opcodes.push_back(continue_addr);
  783. codegen.opcodes[break_addr+1]=codegen.opcodes.size();
  784. } break;
  785. case GDParser::ControlFlowNode::CF_SWITCH: {
  786. } break;
  787. case GDParser::ControlFlowNode::CF_BREAK: {
  788. if (p_break_addr<0) {
  789. _set_error("'break'' not within loop",cf);
  790. return ERR_COMPILATION_FAILED;
  791. }
  792. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  793. codegen.opcodes.push_back(p_break_addr);
  794. } break;
  795. case GDParser::ControlFlowNode::CF_CONTINUE: {
  796. if (p_continue_addr<0) {
  797. _set_error("'continue' not within loop",cf);
  798. return ERR_COMPILATION_FAILED;
  799. }
  800. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  801. codegen.opcodes.push_back(p_continue_addr);
  802. } break;
  803. case GDParser::ControlFlowNode::CF_RETURN: {
  804. int ret;
  805. if (cf->arguments.size()) {
  806. ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  807. if (ret<0)
  808. return ERR_PARSE_ERROR;
  809. } else {
  810. ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
  811. }
  812. codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
  813. codegen.opcodes.push_back(ret);
  814. } break;
  815. }
  816. } break;
  817. case GDParser::Node::TYPE_ASSERT: {
  818. // try subblocks
  819. const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);
  820. int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
  821. if (ret<0)
  822. return ERR_PARSE_ERROR;
  823. codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
  824. codegen.opcodes.push_back(ret);
  825. } break;
  826. case GDParser::Node::TYPE_LOCAL_VAR: {
  827. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);
  828. codegen.add_stack_identifier(lv->name,p_stack_level++);
  829. codegen.alloc_stack(p_stack_level);
  830. new_identifiers++;
  831. } break;
  832. default: {
  833. //expression
  834. int ret = _parse_expression(codegen,s,p_stack_level,true);
  835. if (ret<0)
  836. return ERR_PARSE_ERROR;
  837. } break;
  838. }
  839. }
  840. codegen.pop_stack_identifiers();
  841. return OK;
  842. }
  843. Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func) {
  844. Vector<int> bytecode;
  845. CodeGen codegen;
  846. codegen.class_node=p_class;
  847. codegen.script=p_script;
  848. codegen.function_node=p_func;
  849. codegen.stack_max=0;
  850. codegen.current_line=0;
  851. codegen.call_max=0;
  852. codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;
  853. int stack_level=0;
  854. if (p_func) {
  855. for(int i=0;i<p_func->arguments.size();i++) {
  856. int idx = i;
  857. codegen.add_stack_identifier(p_func->arguments[i],i);
  858. }
  859. stack_level=p_func->arguments.size();
  860. }
  861. codegen.alloc_stack(stack_level);
  862. /* Parse initializer -if applies- */
  863. bool is_initializer=false || !p_func;
  864. if (!p_func || String(p_func->name)=="_init") {
  865. //parse initializer for class members
  866. if (!p_func && p_class->extends_used && p_script->native.is_null()){
  867. //call implicit parent constructor
  868. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
  869. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  870. codegen.opcodes.push_back(0);
  871. codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);
  872. }
  873. Error err = _parse_block(codegen,p_class->initializer,stack_level);
  874. if (err)
  875. return err;
  876. is_initializer=true;
  877. }
  878. /* Parse default argument code -if applies- */
  879. Vector<int> defarg_addr;
  880. StringName func_name;
  881. if (p_func) {
  882. if (p_func->default_values.size()) {
  883. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  884. defarg_addr.push_back(codegen.opcodes.size());
  885. for(int i=0;i<p_func->default_values.size();i++) {
  886. _parse_expression(codegen,p_func->default_values[i],stack_level,true);
  887. defarg_addr.push_back(codegen.opcodes.size());
  888. }
  889. defarg_addr.invert();
  890. }
  891. Error err = _parse_block(codegen,p_func->body,stack_level);
  892. if (err)
  893. return err;
  894. func_name=p_func->name;
  895. } else {
  896. func_name="_init";
  897. }
  898. codegen.opcodes.push_back(GDFunction::OPCODE_END);
  899. GDFunction *gdfunc=NULL;
  900. //if (String(p_func->name)=="") { //initializer func
  901. // gdfunc = &p_script->initializer;
  902. //} else { //regular func
  903. p_script->member_functions[func_name]=GDFunction();
  904. gdfunc = &p_script->member_functions[func_name];
  905. //}
  906. if (p_func)
  907. gdfunc->_static=p_func->_static;
  908. //constants
  909. if (codegen.constant_map.size()) {
  910. gdfunc->_constant_count=codegen.constant_map.size();
  911. gdfunc->constants.resize(codegen.constant_map.size());
  912. gdfunc->_constants_ptr=&gdfunc->constants[0];
  913. const Variant *K=NULL;
  914. while((K=codegen.constant_map.next(K))) {
  915. int idx = codegen.constant_map[*K];
  916. gdfunc->constants[idx]=*K;
  917. }
  918. } else {
  919. gdfunc->_constants_ptr=NULL;
  920. gdfunc->_constant_count=0;
  921. }
  922. //global names
  923. if (codegen.name_map.size()) {
  924. gdfunc->global_names.resize(codegen.name_map.size());
  925. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  926. for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {
  927. gdfunc->global_names[E->get()]=E->key();
  928. }
  929. gdfunc->_global_names_count=gdfunc->global_names.size();
  930. } else {
  931. gdfunc->_global_names_ptr = NULL;
  932. gdfunc->_global_names_count =0;
  933. }
  934. if (codegen.opcodes.size()) {
  935. gdfunc->code=codegen.opcodes;
  936. gdfunc->_code_ptr=&gdfunc->code[0];
  937. gdfunc->_code_size=codegen.opcodes.size();
  938. } else {
  939. gdfunc->_code_ptr=NULL;
  940. gdfunc->_code_size=0;
  941. }
  942. if (defarg_addr.size()) {
  943. gdfunc->default_arguments=defarg_addr;
  944. gdfunc->_default_arg_count=defarg_addr.size();
  945. gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
  946. } else {
  947. gdfunc->_default_arg_count=0;
  948. gdfunc->_default_arg_ptr=NULL;
  949. }
  950. gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
  951. gdfunc->_stack_size=codegen.stack_max;
  952. gdfunc->_call_size=codegen.call_max;
  953. gdfunc->name=func_name;
  954. gdfunc->_script=p_script;
  955. gdfunc->source=source;
  956. #ifdef DEBUG_ENABLED
  957. {
  958. gdfunc->func_cname=(String(source)+" - "+String(func_name)).utf8();
  959. gdfunc->_func_cname=gdfunc->func_cname.get_data();
  960. }
  961. #endif
  962. if (p_func) {
  963. gdfunc->_initial_line=p_func->line;
  964. } else {
  965. gdfunc->_initial_line=0;
  966. }
  967. if (codegen.debug_stack)
  968. gdfunc->stack_debug=codegen.stack_debug;
  969. if (is_initializer)
  970. p_script->initializer=gdfunc;
  971. return OK;
  972. }
  973. Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class) {
  974. p_script->native=Ref<GDNativeClass>();
  975. p_script->base=Ref<GDScript>();
  976. p_script->_base=NULL;
  977. p_script->members.clear();
  978. p_script->constants.clear();
  979. p_script->member_functions.clear();
  980. p_script->member_indices.clear();
  981. p_script->member_info.clear();
  982. p_script->initializer=NULL;
  983. p_script->subclasses.clear();
  984. p_script->_owner=p_owner;
  985. p_script->tool=p_class->tool;
  986. p_script->name=p_class->name;
  987. int index_from=0;
  988. if (p_class->extends_used) {
  989. //do inheritance
  990. String path = p_class->extends_file;
  991. Ref<GDScript> script;
  992. Ref<GDNativeClass> native;
  993. if (path!="") {
  994. //path (and optionally subclasses)
  995. if (path.is_rel_path()) {
  996. String base = p_script->get_path();
  997. if (base=="" || base.is_rel_path()) {
  998. _set_error("Could not resolve relative path for parent class: "+path,p_class);
  999. return ERR_FILE_NOT_FOUND;
  1000. }
  1001. path=base.get_base_dir().plus_file(path);
  1002. }
  1003. script = ResourceLoader::load(path);
  1004. if (script.is_null()) {
  1005. _set_error("Could not load base class: "+path,p_class);
  1006. return ERR_FILE_NOT_FOUND;
  1007. }
  1008. if (!script->valid) {
  1009. _set_error("Script not fully loaded (cyclic preload?): "+path,p_class);
  1010. return ERR_BUSY;
  1011. }
  1012. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  1013. if (p_class->extends_class.size()) {
  1014. for(int i=0;i<p_class->extends_class.size();i++) {
  1015. String sub = p_class->extends_class[i];
  1016. if (script->subclasses.has(sub)) {
  1017. script=script->subclasses[sub];
  1018. } else {
  1019. _set_error("Could not find subclass: "+sub,p_class);
  1020. return ERR_FILE_NOT_FOUND;
  1021. }
  1022. }
  1023. }
  1024. } else {
  1025. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  1026. //look around for the subclasses
  1027. String base=p_class->extends_class[0];
  1028. GDScript *p = p_owner;
  1029. Ref<GDScript> base_class;
  1030. while(p) {
  1031. if (p->subclasses.has(base)) {
  1032. base_class=p->subclasses[base];
  1033. break;
  1034. }
  1035. p=p->_owner;
  1036. }
  1037. if (base_class.is_valid()) {
  1038. for(int i=1;i<p_class->extends_class.size();i++) {
  1039. String subclass=p_class->extends_class[i];
  1040. if (base_class->subclasses.has(subclass)) {
  1041. base_class=base_class->subclasses[subclass];
  1042. } else {
  1043. _set_error("Could not find subclass: "+subclass,p_class);
  1044. return ERR_FILE_NOT_FOUND;
  1045. }
  1046. }
  1047. script=base_class;
  1048. } else {
  1049. if (p_class->extends_class.size()>1) {
  1050. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  1051. return ERR_FILE_NOT_FOUND;
  1052. }
  1053. //if not found, try engine classes
  1054. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  1055. _set_error("Unknown class: '"+base+"'",p_class);
  1056. return ERR_FILE_NOT_FOUND;
  1057. }
  1058. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1059. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1060. if (!native.is_valid()) {
  1061. _set_error("Global not a class: '"+base+"'",p_class);
  1062. return ERR_FILE_NOT_FOUND;
  1063. }
  1064. }
  1065. }
  1066. if (script.is_valid()) {
  1067. p_script->base=script;
  1068. p_script->_base=p_script->base.ptr();
  1069. p_script->member_indices=script->member_indices;
  1070. } else if (native.is_valid()) {
  1071. p_script->native=native;
  1072. } else {
  1073. _set_error("Could not determine inheritance",p_class);
  1074. return ERR_FILE_NOT_FOUND;
  1075. }
  1076. }
  1077. //print_line("Script: "+p_script->get_path()+" indices: "+itos(p_script->member_indices.size()));
  1078. for(int i=0;i<p_class->variables.size();i++) {
  1079. StringName name = p_class->variables[i].identifier;
  1080. if (p_script->member_indices.has(name)) {
  1081. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1082. return ERR_ALREADY_EXISTS;
  1083. }
  1084. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1085. p_script->member_info[name]=p_class->variables[i]._export;
  1086. #ifdef TOOLS_ENABLED
  1087. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1088. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1089. }
  1090. #endif
  1091. }
  1092. int new_idx = p_script->member_indices.size();
  1093. p_script->member_indices[name]=new_idx;
  1094. p_script->members.insert(name);
  1095. }
  1096. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1097. StringName name = p_class->constant_expressions[i].identifier;
  1098. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1099. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1100. p_script->constants.insert(name,constant->value);
  1101. //p_script->constants[constant->value].make_const();
  1102. }
  1103. //parse sub-classes
  1104. for(int i=0;i<p_class->subclasses.size();i++) {
  1105. StringName name = p_class->subclasses[i]->name;
  1106. Ref<GDScript> subclass = memnew( GDScript );
  1107. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]);
  1108. if (err)
  1109. return err;
  1110. p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants
  1111. p_script->subclasses.insert(name,subclass);
  1112. }
  1113. //parse methods
  1114. bool has_initializer=false;
  1115. for(int i=0;i<p_class->functions.size();i++) {
  1116. if (!has_initializer && p_class->functions[i]->name=="_init")
  1117. has_initializer=true;
  1118. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1119. if (err)
  1120. return err;
  1121. }
  1122. //parse static methods
  1123. for(int i=0;i<p_class->static_functions.size();i++) {
  1124. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1125. if (err)
  1126. return err;
  1127. }
  1128. if (!has_initializer) {
  1129. //create a constructor
  1130. Error err = _parse_function(p_script,p_class,NULL);
  1131. if (err)
  1132. return err;
  1133. }
  1134. return OK;
  1135. }
  1136. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
  1137. err_line=-1;
  1138. err_column=-1;
  1139. error="";
  1140. parser=p_parser;
  1141. const GDParser::Node* root = parser->get_parse_tree();
  1142. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1143. source=p_script->get_path();
  1144. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root));
  1145. if (err)
  1146. return err;
  1147. return OK;
  1148. }
  1149. String GDCompiler::get_error() const {
  1150. return error;
  1151. }
  1152. int GDCompiler::get_error_line() const{
  1153. return err_line;
  1154. }
  1155. int GDCompiler::get_error_column() const{
  1156. return err_column;
  1157. }
  1158. GDCompiler::GDCompiler()
  1159. {
  1160. }