gd_compiler.cpp 48 KB

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