gd_compiler.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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-2015 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. void GDCompiler::_set_error(const String& p_error,const GDParser::Node *p_node) {
  32. if (error!="")
  33. return;
  34. error=p_error;
  35. if (p_node) {
  36. err_line=p_node->line;
  37. err_column=p_node->column;
  38. } else {
  39. err_line=0;
  40. err_column=0;
  41. }
  42. }
  43. bool GDCompiler::_create_unary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level) {
  44. ERR_FAIL_COND_V(on->arguments.size()!=1,false);
  45. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level);
  46. if (src_address_a<0)
  47. return false;
  48. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  49. codegen.opcodes.push_back(op); //which operator
  50. codegen.opcodes.push_back(src_address_a); // argument 1
  51. codegen.opcodes.push_back(src_address_a); // argument 2 (repeated)
  52. //codegen.opcodes.push_back(GDFunction::ADDR_TYPE_NIL); // argument 2 (unary only takes one parameter)
  53. return true;
  54. }
  55. bool GDCompiler::_create_binary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level,bool p_initializer) {
  56. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  57. int src_address_a = _parse_expression(codegen,on->arguments[0],p_stack_level,false,p_initializer);
  58. if (src_address_a<0)
  59. return false;
  60. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  61. p_stack_level++; //uses stack for return, increase stack
  62. int src_address_b = _parse_expression(codegen,on->arguments[1],p_stack_level,false,p_initializer);
  63. if (src_address_b<0)
  64. return false;
  65. codegen.opcodes.push_back(GDFunction::OPCODE_OPERATOR); // perform operator
  66. codegen.opcodes.push_back(op); //which operator
  67. codegen.opcodes.push_back(src_address_a); // argument 1
  68. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  69. return true;
  70. }
  71. /*
  72. int GDCompiler::_parse_subexpression(CodeGen& codegen,const GDParser::Node *p_expression) {
  73. int ret = _parse_expression(codegen,p_expression);
  74. if (ret<0)
  75. return ret;
  76. if (ret&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  77. codegen.stack_level++;
  78. codegen.check_max_stack_level();
  79. //stack was used, keep value
  80. }
  81. return ret;
  82. }
  83. */
  84. int GDCompiler::_parse_assign_right_expression(CodeGen& codegen,const GDParser::OperatorNode *p_expression, int p_stack_level) {
  85. Variant::Operator var_op=Variant::OP_MAX;
  86. switch(p_expression->op) {
  87. case GDParser::OperatorNode::OP_ASSIGN_ADD: var_op=Variant::OP_ADD; break;
  88. case GDParser::OperatorNode::OP_ASSIGN_SUB: var_op=Variant::OP_SUBSTRACT; break;
  89. case GDParser::OperatorNode::OP_ASSIGN_MUL: var_op=Variant::OP_MULTIPLY; break;
  90. case GDParser::OperatorNode::OP_ASSIGN_DIV: var_op=Variant::OP_DIVIDE; break;
  91. case GDParser::OperatorNode::OP_ASSIGN_MOD: var_op=Variant::OP_MODULE; break;
  92. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT: var_op=Variant::OP_SHIFT_LEFT; break;
  93. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT: var_op=Variant::OP_SHIFT_RIGHT; break;
  94. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND: var_op=Variant::OP_BIT_AND; break;
  95. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR: var_op=Variant::OP_BIT_OR; break;
  96. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR: var_op=Variant::OP_BIT_XOR; break;
  97. case GDParser::OperatorNode::OP_INIT_ASSIGN:
  98. case GDParser::OperatorNode::OP_ASSIGN: {
  99. //none
  100. } break;
  101. default: {
  102. ERR_FAIL_V(-1);
  103. }
  104. }
  105. bool initializer = p_expression->op==GDParser::OperatorNode::OP_INIT_ASSIGN;
  106. if (var_op==Variant::OP_MAX) {
  107. return _parse_expression(codegen,p_expression->arguments[1],p_stack_level,false,initializer);
  108. }
  109. if (!_create_binary_operator(codegen,p_expression,var_op,p_stack_level,initializer))
  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,bool p_initializer) {
  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 (!p_initializer && 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 MEMBERS!
  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].index;
  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. case GDParser::OperatorNode::OP_YIELD: {
  377. ERR_FAIL_COND_V(on->arguments.size() && on->arguments.size()!=2,-1);
  378. Vector<int> arguments;
  379. int slevel = p_stack_level;
  380. for(int i=0;i<on->arguments.size();i++) {
  381. int ret = _parse_expression(codegen,on->arguments[i],slevel);
  382. if (ret<0)
  383. return ret;
  384. if (ret&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  385. slevel++;
  386. codegen.alloc_stack(slevel);
  387. }
  388. arguments.push_back(ret);
  389. }
  390. //push call bytecode
  391. codegen.opcodes.push_back(arguments.size()==0?GDFunction::OPCODE_YIELD:GDFunction::OPCODE_YIELD_SIGNAL); // basic type constructor
  392. for(int i=0;i<arguments.size();i++)
  393. codegen.opcodes.push_back(arguments[i]); //arguments
  394. codegen.opcodes.push_back(GDFunction::OPCODE_YIELD_RESUME);
  395. //next will be where to place the result :)
  396. } break;
  397. //indexing operator
  398. case GDParser::OperatorNode::OP_INDEX:
  399. case GDParser::OperatorNode::OP_INDEX_NAMED: {
  400. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  401. int slevel = p_stack_level;
  402. bool named=(on->op==GDParser::OperatorNode::OP_INDEX_NAMED);
  403. int from = _parse_expression(codegen,on->arguments[0],slevel);
  404. if (from<0)
  405. return from;
  406. int index;
  407. if (named) {
  408. #ifdef DEBUG_ENABLED
  409. if (on->arguments[0]->type==GDParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  410. const Map<StringName,GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  411. if (MI && MI->get().getter==codegen.function_node->name) {
  412. String n = static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name;
  413. _set_error("Must use '"+n+"' instead of 'self."+n+"' in getter.",on);
  414. return -1;
  415. }
  416. }
  417. #endif
  418. index=codegen.get_name_map_pos(static_cast<GDParser::IdentifierNode*>(on->arguments[1])->name);
  419. } else {
  420. if (on->arguments[1]->type==GDParser::Node::TYPE_CONSTANT && static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value.get_type()==Variant::STRING) {
  421. //also, somehow, named (speed up anyway)
  422. StringName name = static_cast<const GDParser::ConstantNode*>(on->arguments[1])->value;
  423. index=codegen.get_name_map_pos(name);
  424. named=true;
  425. } else {
  426. //regular indexing
  427. if (from&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  428. slevel++;
  429. codegen.alloc_stack(slevel);
  430. }
  431. index = _parse_expression(codegen,on->arguments[1],slevel);
  432. if (index<0)
  433. return index;
  434. }
  435. }
  436. codegen.opcodes.push_back(named?GDFunction::OPCODE_GET_NAMED:GDFunction::OPCODE_GET); // perform operator
  437. codegen.opcodes.push_back(from); // argument 1
  438. codegen.opcodes.push_back(index); // argument 2 (unary only takes one parameter)
  439. } break;
  440. case GDParser::OperatorNode::OP_AND: {
  441. // AND operator with early out on failure
  442. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  443. if (res<0)
  444. return res;
  445. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  446. codegen.opcodes.push_back(res);
  447. int jump_fail_pos=codegen.opcodes.size();
  448. codegen.opcodes.push_back(0);
  449. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  450. if (res<0)
  451. return res;
  452. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  453. codegen.opcodes.push_back(res);
  454. int jump_fail_pos2=codegen.opcodes.size();
  455. codegen.opcodes.push_back(0);
  456. codegen.alloc_stack(p_stack_level); //it will be used..
  457. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  458. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  459. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  460. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  461. codegen.opcodes[jump_fail_pos]=codegen.opcodes.size();
  462. codegen.opcodes[jump_fail_pos2]=codegen.opcodes.size();
  463. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  464. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  465. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  466. } break;
  467. case GDParser::OperatorNode::OP_OR: {
  468. // OR operator with early out on success
  469. int res = _parse_expression(codegen,on->arguments[0],p_stack_level);
  470. if (res<0)
  471. return res;
  472. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  473. codegen.opcodes.push_back(res);
  474. int jump_success_pos=codegen.opcodes.size();
  475. codegen.opcodes.push_back(0);
  476. res = _parse_expression(codegen,on->arguments[1],p_stack_level);
  477. if (res<0)
  478. return res;
  479. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
  480. codegen.opcodes.push_back(res);
  481. int jump_success_pos2=codegen.opcodes.size();
  482. codegen.opcodes.push_back(0);
  483. codegen.alloc_stack(p_stack_level); //it will be used..
  484. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_FALSE);
  485. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  486. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  487. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  488. codegen.opcodes[jump_success_pos]=codegen.opcodes.size();
  489. codegen.opcodes[jump_success_pos2]=codegen.opcodes.size();
  490. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN_TRUE);
  491. codegen.opcodes.push_back(p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  492. return p_stack_level|GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS;
  493. } break;
  494. //unary operators
  495. case GDParser::OperatorNode::OP_NEG: { if (!_create_unary_operator(codegen,on,Variant::OP_NEGATE,p_stack_level)) return -1;} break;
  496. case GDParser::OperatorNode::OP_NOT: { if (!_create_unary_operator(codegen,on,Variant::OP_NOT,p_stack_level)) return -1;} break;
  497. case GDParser::OperatorNode::OP_BIT_INVERT: { if (!_create_unary_operator(codegen,on,Variant::OP_BIT_NEGATE,p_stack_level)) return -1;} break;
  498. case GDParser::OperatorNode::OP_PREINC: { } break; //?
  499. case GDParser::OperatorNode::OP_PREDEC: { } break;
  500. case GDParser::OperatorNode::OP_INC: { } break;
  501. case GDParser::OperatorNode::OP_DEC: { } break;
  502. //binary operators (in precedence order)
  503. case GDParser::OperatorNode::OP_IN: { if (!_create_binary_operator(codegen,on,Variant::OP_IN,p_stack_level)) return -1;} break;
  504. case GDParser::OperatorNode::OP_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_EQUAL,p_stack_level)) return -1;} break;
  505. case GDParser::OperatorNode::OP_NOT_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_NOT_EQUAL,p_stack_level)) return -1;} break;
  506. case GDParser::OperatorNode::OP_LESS: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS,p_stack_level)) return -1;} break;
  507. case GDParser::OperatorNode::OP_LESS_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_LESS_EQUAL,p_stack_level)) return -1;} break;
  508. case GDParser::OperatorNode::OP_GREATER: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER,p_stack_level)) return -1;} break;
  509. case GDParser::OperatorNode::OP_GREATER_EQUAL: { if (!_create_binary_operator(codegen,on,Variant::OP_GREATER_EQUAL,p_stack_level)) return -1;} break;
  510. case GDParser::OperatorNode::OP_ADD: { if (!_create_binary_operator(codegen,on,Variant::OP_ADD,p_stack_level)) return -1;} break;
  511. case GDParser::OperatorNode::OP_SUB: { if (!_create_binary_operator(codegen,on,Variant::OP_SUBSTRACT,p_stack_level)) return -1;} break;
  512. case GDParser::OperatorNode::OP_MUL: { if (!_create_binary_operator(codegen,on,Variant::OP_MULTIPLY,p_stack_level)) return -1;} break;
  513. case GDParser::OperatorNode::OP_DIV: { if (!_create_binary_operator(codegen,on,Variant::OP_DIVIDE,p_stack_level)) return -1;} break;
  514. case GDParser::OperatorNode::OP_MOD: { if (!_create_binary_operator(codegen,on,Variant::OP_MODULE,p_stack_level)) return -1;} break;
  515. //case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  516. //case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  517. case GDParser::OperatorNode::OP_BIT_AND: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_AND,p_stack_level)) return -1;} break;
  518. case GDParser::OperatorNode::OP_BIT_OR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_OR,p_stack_level)) return -1;} break;
  519. case GDParser::OperatorNode::OP_BIT_XOR: { if (!_create_binary_operator(codegen,on,Variant::OP_BIT_XOR,p_stack_level)) return -1;} break;
  520. //shift
  521. case GDParser::OperatorNode::OP_SHIFT_LEFT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_LEFT,p_stack_level)) return -1;} break;
  522. case GDParser::OperatorNode::OP_SHIFT_RIGHT: { if (!_create_binary_operator(codegen,on,Variant::OP_SHIFT_RIGHT,p_stack_level)) return -1;} break;
  523. //assignment operators
  524. case GDParser::OperatorNode::OP_ASSIGN_ADD:
  525. case GDParser::OperatorNode::OP_ASSIGN_SUB:
  526. case GDParser::OperatorNode::OP_ASSIGN_MUL:
  527. case GDParser::OperatorNode::OP_ASSIGN_DIV:
  528. case GDParser::OperatorNode::OP_ASSIGN_MOD:
  529. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_LEFT:
  530. case GDParser::OperatorNode::OP_ASSIGN_SHIFT_RIGHT:
  531. case GDParser::OperatorNode::OP_ASSIGN_BIT_AND:
  532. case GDParser::OperatorNode::OP_ASSIGN_BIT_OR:
  533. case GDParser::OperatorNode::OP_ASSIGN_BIT_XOR:
  534. case GDParser::OperatorNode::OP_INIT_ASSIGN:
  535. case GDParser::OperatorNode::OP_ASSIGN: {
  536. ERR_FAIL_COND_V(on->arguments.size()!=2,-1);
  537. 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)) {
  538. //SET (chained) MODE!!
  539. #ifdef DEBUG_ENABLED
  540. if (static_cast<GDParser::OperatorNode*>(on->arguments[0])->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  541. const GDParser::OperatorNode* inon = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  542. if (inon->arguments[0]->type==GDParser::Node::TYPE_SELF && codegen.script && codegen.function_node && !codegen.function_node->_static) {
  543. const Map<StringName,GDScript::MemberInfo>::Element *MI = codegen.script->member_indices.find(static_cast<GDParser::IdentifierNode*>(inon->arguments[1])->name);
  544. if (MI && MI->get().setter==codegen.function_node->name) {
  545. String n = static_cast<GDParser::IdentifierNode*>(inon->arguments[1])->name;
  546. _set_error("Must use '"+n+"' instead of 'self."+n+"' in setter.",inon);
  547. return -1;
  548. }
  549. }
  550. }
  551. #endif
  552. int slevel=p_stack_level;
  553. GDParser::OperatorNode* op = static_cast<GDParser::OperatorNode*>(on->arguments[0]);
  554. /* Find chain of sets */
  555. List<GDParser::OperatorNode*> chain;
  556. {
  557. //create get/set chain
  558. GDParser::OperatorNode* n=op;
  559. while(true) {
  560. chain.push_back(n);
  561. if (n->arguments[0]->type!=GDParser::Node::TYPE_OPERATOR)
  562. break;
  563. n = static_cast<GDParser::OperatorNode*>(n->arguments[0]);
  564. if (n->op!=GDParser::OperatorNode::OP_INDEX && n->op!=GDParser::OperatorNode::OP_INDEX_NAMED)
  565. break;
  566. }
  567. }
  568. /* Chain of gets */
  569. //get at (potential) root stack pos, so it can be returned
  570. int prev_pos = _parse_expression(codegen,chain.back()->get()->arguments[0],slevel);
  571. if (prev_pos<0)
  572. return prev_pos;
  573. int retval=prev_pos;
  574. //print_line("retval: "+itos(retval));
  575. if (retval&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  576. slevel++;
  577. codegen.alloc_stack(slevel);
  578. }
  579. Vector<int> setchain;
  580. int prev_key_idx=-1;
  581. for(List<GDParser::OperatorNode*>::Element *E=chain.back();E;E=E->prev()) {
  582. if (E==chain.front()) //ignore first
  583. break;
  584. bool named = E->get()->op==GDParser::OperatorNode::OP_INDEX_NAMED;
  585. int key_idx;
  586. if (named) {
  587. key_idx = codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(E->get()->arguments[1])->name);
  588. //printf("named key %x\n",key_idx);
  589. } else {
  590. if (prev_pos&(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)) {
  591. slevel++;
  592. codegen.alloc_stack(slevel);
  593. }
  594. GDParser::Node *key = E->get()->arguments[1];
  595. key_idx = _parse_expression(codegen,key,slevel);
  596. //printf("expr key %x\n",key_idx);
  597. //stack was raised here if retval was stack but..
  598. }
  599. if (key_idx<0)
  600. return key_idx;
  601. codegen.opcodes.push_back(named ? GDFunction::OPCODE_GET_NAMED : GDFunction::OPCODE_GET);
  602. codegen.opcodes.push_back(prev_pos);
  603. codegen.opcodes.push_back(key_idx);
  604. slevel++;
  605. codegen.alloc_stack(slevel);
  606. int dst_pos = (GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|slevel;
  607. codegen.opcodes.push_back(dst_pos);
  608. //add in reverse order, since it will be reverted
  609. setchain.push_back(dst_pos);
  610. setchain.push_back(key_idx);
  611. setchain.push_back(prev_pos);
  612. setchain.push_back(named ? GDFunction::OPCODE_SET_NAMED : GDFunction::OPCODE_SET);
  613. prev_pos=dst_pos;
  614. prev_key_idx=key_idx;
  615. }
  616. setchain.invert();
  617. int set_index;
  618. bool named=false;
  619. if (static_cast<const GDParser::OperatorNode*>(op)->op==GDParser::OperatorNode::OP_INDEX_NAMED) {
  620. set_index=codegen.get_name_map_pos(static_cast<const GDParser::IdentifierNode*>(op->arguments[1])->name);
  621. named=true;
  622. } else {
  623. set_index = _parse_expression(codegen,op->arguments[1],slevel+1);
  624. named=false;
  625. }
  626. if (set_index<0)
  627. return set_index;
  628. if (set_index&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  629. slevel++;
  630. codegen.alloc_stack(slevel);
  631. }
  632. int set_value = _parse_assign_right_expression(codegen,on,slevel+1);
  633. if (set_value<0)
  634. return set_value;
  635. codegen.opcodes.push_back(named?GDFunction::OPCODE_SET_NAMED:GDFunction::OPCODE_SET);
  636. codegen.opcodes.push_back(prev_pos);
  637. codegen.opcodes.push_back(set_index);
  638. codegen.opcodes.push_back(set_value);
  639. for(int i=0;i<setchain.size();i+=4) {
  640. codegen.opcodes.push_back(setchain[i+0]);
  641. codegen.opcodes.push_back(setchain[i+1]);
  642. codegen.opcodes.push_back(setchain[i+2]);
  643. codegen.opcodes.push_back(setchain[i+3]);
  644. }
  645. return retval;
  646. } else {
  647. //ASSIGNMENT MODE!!
  648. int slevel = p_stack_level;
  649. int dst_address_a = _parse_expression(codegen,on->arguments[0],slevel,false,on->op==GDParser::OperatorNode::OP_INIT_ASSIGN);
  650. if (dst_address_a<0)
  651. return -1;
  652. if (dst_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS) {
  653. slevel++;
  654. codegen.alloc_stack(slevel);
  655. }
  656. int src_address_b = _parse_assign_right_expression(codegen,on,slevel);
  657. if (src_address_b<0)
  658. return -1;
  659. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN); // perform operator
  660. codegen.opcodes.push_back(dst_address_a); // argument 1
  661. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  662. return dst_address_a; //if anything, returns wathever was assigned or correct stack position
  663. }
  664. } break;
  665. case GDParser::OperatorNode::OP_EXTENDS: {
  666. ERR_FAIL_COND_V(on->arguments.size()!=2,false);
  667. int slevel = p_stack_level;
  668. int src_address_a = _parse_expression(codegen,on->arguments[0],slevel);
  669. if (src_address_a<0)
  670. return -1;
  671. if (src_address_a&GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)
  672. slevel++; //uses stack for return, increase stack
  673. int src_address_b = _parse_expression(codegen,on->arguments[1],slevel);
  674. if (src_address_b<0)
  675. return -1;
  676. codegen.opcodes.push_back(GDFunction::OPCODE_EXTENDS_TEST); // perform operator
  677. codegen.opcodes.push_back(src_address_a); // argument 1
  678. codegen.opcodes.push_back(src_address_b); // argument 2 (unary only takes one parameter)
  679. } break;
  680. default: {
  681. ERR_EXPLAIN("Bug in bytecode compiler, unexpected operator #"+itos(on->op)+" in parse tree while parsing expression.");
  682. ERR_FAIL_V(0); //unreachable code
  683. } break;
  684. }
  685. int dst_addr=(p_stack_level)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  686. codegen.opcodes.push_back(dst_addr); // append the stack level as destination address of the opcode
  687. codegen.alloc_stack(p_stack_level);
  688. return dst_addr;
  689. } break;
  690. //TYPE_TYPE,
  691. default: {
  692. ERR_EXPLAIN("Bug in bytecode compiler, unexpected node in parse tree while parsing expression.");
  693. ERR_FAIL_V(-1); //unreachable code
  694. } break;
  695. }
  696. ERR_FAIL_V(-1); //unreachable code
  697. }
  698. Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level,int p_break_addr,int p_continue_addr) {
  699. codegen.push_stack_identifiers();
  700. int new_identifiers=0;
  701. codegen.current_line=p_block->line;
  702. for(int i=0;i<p_block->statements.size();i++) {
  703. const GDParser::Node *s = p_block->statements[i];
  704. switch(s->type) {
  705. case GDParser::Node::TYPE_NEWLINE: {
  706. const GDParser::NewLineNode *nl = static_cast<const GDParser::NewLineNode*>(s);
  707. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  708. codegen.opcodes.push_back(nl->line);
  709. codegen.current_line=nl->line;
  710. } break;
  711. case GDParser::Node::TYPE_CONTROL_FLOW: {
  712. // try subblocks
  713. const GDParser::ControlFlowNode *cf = static_cast<const GDParser::ControlFlowNode*>(s);
  714. switch(cf->cf_type) {
  715. case GDParser::ControlFlowNode::CF_IF: {
  716. #ifdef DEBUG_ENABLED
  717. codegen.opcodes.push_back(GDFunction::OPCODE_LINE);
  718. codegen.opcodes.push_back(cf->line);
  719. codegen.current_line=cf->line;
  720. #endif
  721. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  722. if (ret<0)
  723. return ERR_PARSE_ERROR;
  724. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  725. codegen.opcodes.push_back(ret);
  726. int else_addr=codegen.opcodes.size();
  727. codegen.opcodes.push_back(0); //temporary
  728. Error err = _parse_block(codegen,cf->body,p_stack_level,p_break_addr,p_continue_addr);
  729. if (err)
  730. return err;
  731. if (cf->body_else) {
  732. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  733. int end_addr=codegen.opcodes.size();
  734. codegen.opcodes.push_back(0);
  735. codegen.opcodes[else_addr]=codegen.opcodes.size();
  736. Error err = _parse_block(codegen,cf->body_else,p_stack_level,p_break_addr,p_continue_addr);
  737. if (err)
  738. return err;
  739. codegen.opcodes[end_addr]=codegen.opcodes.size();
  740. } else {
  741. //end without else
  742. codegen.opcodes[else_addr]=codegen.opcodes.size();
  743. }
  744. } break;
  745. case GDParser::ControlFlowNode::CF_FOR: {
  746. int slevel=p_stack_level;
  747. int iter_stack_pos=slevel;
  748. int iterator_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  749. int counter_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  750. int container_pos = (slevel++)|(GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS);
  751. codegen.alloc_stack(slevel);
  752. codegen.push_stack_identifiers();
  753. codegen.add_stack_identifier(static_cast<const GDParser::IdentifierNode*>(cf->arguments[0])->name,iter_stack_pos);
  754. int ret = _parse_expression(codegen,cf->arguments[1],slevel,false);
  755. if (ret<0)
  756. return ERR_COMPILATION_FAILED;
  757. //assign container
  758. codegen.opcodes.push_back(GDFunction::OPCODE_ASSIGN);
  759. codegen.opcodes.push_back(container_pos);
  760. codegen.opcodes.push_back(ret);
  761. //begin loop
  762. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE_BEGIN);
  763. codegen.opcodes.push_back(counter_pos);
  764. codegen.opcodes.push_back(container_pos);
  765. codegen.opcodes.push_back(codegen.opcodes.size()+4);
  766. codegen.opcodes.push_back(iterator_pos);
  767. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  768. codegen.opcodes.push_back(codegen.opcodes.size()+8);
  769. //break loop
  770. int break_pos=codegen.opcodes.size();
  771. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP); //skip code for next
  772. codegen.opcodes.push_back(0); //skip code for next
  773. //next loop
  774. int continue_pos=codegen.opcodes.size();
  775. codegen.opcodes.push_back(GDFunction::OPCODE_ITERATE);
  776. codegen.opcodes.push_back(counter_pos);
  777. codegen.opcodes.push_back(container_pos);
  778. codegen.opcodes.push_back(break_pos);
  779. codegen.opcodes.push_back(iterator_pos);
  780. Error err = _parse_block(codegen,cf->body,slevel,break_pos,continue_pos);
  781. if (err)
  782. return err;
  783. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  784. codegen.opcodes.push_back(continue_pos);
  785. codegen.opcodes[break_pos+1]=codegen.opcodes.size();
  786. codegen.pop_stack_identifiers();
  787. } break;
  788. case GDParser::ControlFlowNode::CF_WHILE: {
  789. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  790. codegen.opcodes.push_back(codegen.opcodes.size()+3);
  791. int break_addr=codegen.opcodes.size();
  792. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  793. codegen.opcodes.push_back(0);
  794. int continue_addr=codegen.opcodes.size();
  795. int ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  796. if (ret<0)
  797. return ERR_PARSE_ERROR;
  798. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF_NOT);
  799. codegen.opcodes.push_back(ret);
  800. codegen.opcodes.push_back(break_addr);
  801. Error err = _parse_block(codegen,cf->body,p_stack_level,break_addr,continue_addr);
  802. if (err)
  803. return err;
  804. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  805. codegen.opcodes.push_back(continue_addr);
  806. codegen.opcodes[break_addr+1]=codegen.opcodes.size();
  807. } break;
  808. case GDParser::ControlFlowNode::CF_SWITCH: {
  809. } break;
  810. case GDParser::ControlFlowNode::CF_BREAK: {
  811. if (p_break_addr<0) {
  812. _set_error("'break'' not within loop",cf);
  813. return ERR_COMPILATION_FAILED;
  814. }
  815. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  816. codegen.opcodes.push_back(p_break_addr);
  817. } break;
  818. case GDParser::ControlFlowNode::CF_CONTINUE: {
  819. if (p_continue_addr<0) {
  820. _set_error("'continue' not within loop",cf);
  821. return ERR_COMPILATION_FAILED;
  822. }
  823. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
  824. codegen.opcodes.push_back(p_continue_addr);
  825. } break;
  826. case GDParser::ControlFlowNode::CF_RETURN: {
  827. int ret;
  828. if (cf->arguments.size()) {
  829. ret = _parse_expression(codegen,cf->arguments[0],p_stack_level,false);
  830. if (ret<0)
  831. return ERR_PARSE_ERROR;
  832. } else {
  833. ret=GDFunction::ADDR_TYPE_NIL << GDFunction::ADDR_BITS;
  834. }
  835. codegen.opcodes.push_back(GDFunction::OPCODE_RETURN);
  836. codegen.opcodes.push_back(ret);
  837. } break;
  838. }
  839. } break;
  840. case GDParser::Node::TYPE_ASSERT: {
  841. // try subblocks
  842. const GDParser::AssertNode *as = static_cast<const GDParser::AssertNode*>(s);
  843. int ret = _parse_expression(codegen,as->condition,p_stack_level,false);
  844. if (ret<0)
  845. return ERR_PARSE_ERROR;
  846. codegen.opcodes.push_back(GDFunction::OPCODE_ASSERT);
  847. codegen.opcodes.push_back(ret);
  848. } break;
  849. case GDParser::Node::TYPE_LOCAL_VAR: {
  850. const GDParser::LocalVarNode *lv = static_cast<const GDParser::LocalVarNode*>(s);
  851. codegen.add_stack_identifier(lv->name,p_stack_level++);
  852. codegen.alloc_stack(p_stack_level);
  853. new_identifiers++;
  854. } break;
  855. default: {
  856. //expression
  857. int ret = _parse_expression(codegen,s,p_stack_level,true);
  858. if (ret<0)
  859. return ERR_PARSE_ERROR;
  860. } break;
  861. }
  862. }
  863. codegen.pop_stack_identifiers();
  864. return OK;
  865. }
  866. Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func,bool p_for_ready) {
  867. Vector<int> bytecode;
  868. CodeGen codegen;
  869. codegen.class_node=p_class;
  870. codegen.script=p_script;
  871. codegen.function_node=p_func;
  872. codegen.stack_max=0;
  873. codegen.current_line=0;
  874. codegen.call_max=0;
  875. codegen.debug_stack=ScriptDebugger::get_singleton()!=NULL;
  876. Vector<StringName> argnames;
  877. int stack_level=0;
  878. if (p_func) {
  879. for(int i=0;i<p_func->arguments.size();i++) {
  880. int idx = i;
  881. codegen.add_stack_identifier(p_func->arguments[i],i);
  882. #ifdef TOOLS_ENABLED
  883. argnames.push_back(p_func->arguments[i]);
  884. #endif
  885. }
  886. stack_level=p_func->arguments.size();
  887. }
  888. codegen.alloc_stack(stack_level);
  889. /* Parse initializer -if applies- */
  890. bool is_initializer=!p_for_ready && !p_func;
  891. if (is_initializer || String(p_func->name)=="_init") {
  892. //parse initializer for class members
  893. if (!p_func && p_class->extends_used && p_script->native.is_null()){
  894. //call implicit parent constructor
  895. codegen.opcodes.push_back(GDFunction::OPCODE_CALL_SELF_BASE);
  896. codegen.opcodes.push_back(codegen.get_name_map_pos("_init"));
  897. codegen.opcodes.push_back(0);
  898. codegen.opcodes.push_back((GDFunction::ADDR_TYPE_STACK<<GDFunction::ADDR_BITS)|0);
  899. }
  900. Error err = _parse_block(codegen,p_class->initializer,stack_level);
  901. if (err)
  902. return err;
  903. is_initializer=true;
  904. }
  905. if (p_for_ready || (p_func && String(p_func->name)=="_ready")) {
  906. //parse initializer for class members
  907. if (p_class->ready->statements.size()) {
  908. Error err = _parse_block(codegen,p_class->ready,stack_level);
  909. if (err)
  910. return err;
  911. }
  912. }
  913. /* Parse default argument code -if applies- */
  914. Vector<int> defarg_addr;
  915. StringName func_name;
  916. if (p_func) {
  917. if (p_func->default_values.size()) {
  918. codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  919. defarg_addr.push_back(codegen.opcodes.size());
  920. for(int i=0;i<p_func->default_values.size();i++) {
  921. _parse_expression(codegen,p_func->default_values[i],stack_level,true);
  922. defarg_addr.push_back(codegen.opcodes.size());
  923. }
  924. defarg_addr.invert();
  925. }
  926. Error err = _parse_block(codegen,p_func->body,stack_level);
  927. if (err)
  928. return err;
  929. func_name=p_func->name;
  930. } else {
  931. if (p_for_ready)
  932. func_name="_ready";
  933. else
  934. func_name="_init";
  935. }
  936. codegen.opcodes.push_back(GDFunction::OPCODE_END);
  937. GDFunction *gdfunc=NULL;
  938. //if (String(p_func->name)=="") { //initializer func
  939. // gdfunc = &p_script->initializer;
  940. //} else { //regular func
  941. p_script->member_functions[func_name]=GDFunction();
  942. gdfunc = &p_script->member_functions[func_name];
  943. //}
  944. if (p_func)
  945. gdfunc->_static=p_func->_static;
  946. #ifdef TOOLS_ENABLED
  947. gdfunc->arg_names=argnames;
  948. #endif
  949. //constants
  950. if (codegen.constant_map.size()) {
  951. gdfunc->_constant_count=codegen.constant_map.size();
  952. gdfunc->constants.resize(codegen.constant_map.size());
  953. gdfunc->_constants_ptr=&gdfunc->constants[0];
  954. const Variant *K=NULL;
  955. while((K=codegen.constant_map.next(K))) {
  956. int idx = codegen.constant_map[*K];
  957. gdfunc->constants[idx]=*K;
  958. }
  959. } else {
  960. gdfunc->_constants_ptr=NULL;
  961. gdfunc->_constant_count=0;
  962. }
  963. //global names
  964. if (codegen.name_map.size()) {
  965. gdfunc->global_names.resize(codegen.name_map.size());
  966. gdfunc->_global_names_ptr = &gdfunc->global_names[0];
  967. for(Map<StringName,int>::Element *E=codegen.name_map.front();E;E=E->next()) {
  968. gdfunc->global_names[E->get()]=E->key();
  969. }
  970. gdfunc->_global_names_count=gdfunc->global_names.size();
  971. } else {
  972. gdfunc->_global_names_ptr = NULL;
  973. gdfunc->_global_names_count =0;
  974. }
  975. if (codegen.opcodes.size()) {
  976. gdfunc->code=codegen.opcodes;
  977. gdfunc->_code_ptr=&gdfunc->code[0];
  978. gdfunc->_code_size=codegen.opcodes.size();
  979. } else {
  980. gdfunc->_code_ptr=NULL;
  981. gdfunc->_code_size=0;
  982. }
  983. if (defarg_addr.size()) {
  984. gdfunc->default_arguments=defarg_addr;
  985. gdfunc->_default_arg_count=defarg_addr.size();
  986. gdfunc->_default_arg_ptr=&gdfunc->default_arguments[0];
  987. } else {
  988. gdfunc->_default_arg_count=0;
  989. gdfunc->_default_arg_ptr=NULL;
  990. }
  991. gdfunc->_argument_count=p_func ? p_func->arguments.size() : 0;
  992. gdfunc->_stack_size=codegen.stack_max;
  993. gdfunc->_call_size=codegen.call_max;
  994. gdfunc->name=func_name;
  995. gdfunc->_script=p_script;
  996. gdfunc->source=source;
  997. #ifdef DEBUG_ENABLED
  998. {
  999. gdfunc->func_cname=(String(source)+" - "+String(func_name)).utf8();
  1000. gdfunc->_func_cname=gdfunc->func_cname.get_data();
  1001. }
  1002. #endif
  1003. if (p_func) {
  1004. gdfunc->_initial_line=p_func->line;
  1005. } else {
  1006. gdfunc->_initial_line=0;
  1007. }
  1008. if (codegen.debug_stack)
  1009. gdfunc->stack_debug=codegen.stack_debug;
  1010. if (is_initializer)
  1011. p_script->initializer=gdfunc;
  1012. return OK;
  1013. }
  1014. Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class) {
  1015. p_script->native=Ref<GDNativeClass>();
  1016. p_script->base=Ref<GDScript>();
  1017. p_script->_base=NULL;
  1018. p_script->members.clear();
  1019. p_script->constants.clear();
  1020. p_script->member_functions.clear();
  1021. p_script->member_indices.clear();
  1022. p_script->member_info.clear();
  1023. p_script->initializer=NULL;
  1024. p_script->subclasses.clear();
  1025. p_script->_owner=p_owner;
  1026. p_script->tool=p_class->tool;
  1027. p_script->name=p_class->name;
  1028. int index_from=0;
  1029. Ref<GDNativeClass> native;
  1030. if (p_class->extends_used) {
  1031. //do inheritance
  1032. String path = p_class->extends_file;
  1033. Ref<GDScript> script;
  1034. if (path!="") {
  1035. //path (and optionally subclasses)
  1036. if (path.is_rel_path()) {
  1037. String base = p_script->get_path();
  1038. if (base=="" || base.is_rel_path()) {
  1039. _set_error("Could not resolve relative path for parent class: "+path,p_class);
  1040. return ERR_FILE_NOT_FOUND;
  1041. }
  1042. path=base.get_base_dir().plus_file(path).simplify_path();
  1043. }
  1044. script = ResourceLoader::load(path);
  1045. if (script.is_null()) {
  1046. _set_error("Could not load base class: "+path,p_class);
  1047. return ERR_FILE_NOT_FOUND;
  1048. }
  1049. if (!script->valid) {
  1050. _set_error("Script not fully loaded (cyclic preload?): "+path,p_class);
  1051. return ERR_BUSY;
  1052. }
  1053. //print_line("EXTENDS PATH: "+path+" script is "+itos(script.is_valid())+" indices is "+itos(script->member_indices.size())+" valid? "+itos(script->valid));
  1054. if (p_class->extends_class.size()) {
  1055. for(int i=0;i<p_class->extends_class.size();i++) {
  1056. String sub = p_class->extends_class[i];
  1057. if (script->subclasses.has(sub)) {
  1058. script=script->subclasses[sub];
  1059. } else {
  1060. _set_error("Could not find subclass: "+sub,p_class);
  1061. return ERR_FILE_NOT_FOUND;
  1062. }
  1063. }
  1064. }
  1065. } else {
  1066. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  1067. //look around for the subclasses
  1068. String base=p_class->extends_class[0];
  1069. GDScript *p = p_owner;
  1070. Ref<GDScript> base_class;
  1071. while(p) {
  1072. if (p->subclasses.has(base)) {
  1073. base_class=p->subclasses[base];
  1074. break;
  1075. }
  1076. p=p->_owner;
  1077. }
  1078. if (base_class.is_valid()) {
  1079. for(int i=1;i<p_class->extends_class.size();i++) {
  1080. String subclass=p_class->extends_class[i];
  1081. if (base_class->subclasses.has(subclass)) {
  1082. base_class=base_class->subclasses[subclass];
  1083. } else {
  1084. _set_error("Could not find subclass: "+subclass,p_class);
  1085. return ERR_FILE_NOT_FOUND;
  1086. }
  1087. }
  1088. script=base_class;
  1089. } else {
  1090. if (p_class->extends_class.size()>1) {
  1091. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  1092. return ERR_FILE_NOT_FOUND;
  1093. }
  1094. //if not found, try engine classes
  1095. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  1096. _set_error("Unknown class: '"+base+"'",p_class);
  1097. return ERR_FILE_NOT_FOUND;
  1098. }
  1099. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1100. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1101. if (!native.is_valid()) {
  1102. _set_error("Global not a class: '"+base+"'",p_class);
  1103. return ERR_FILE_NOT_FOUND;
  1104. }
  1105. }
  1106. }
  1107. if (script.is_valid()) {
  1108. p_script->base=script;
  1109. p_script->_base=p_script->base.ptr();
  1110. p_script->member_indices=script->member_indices;
  1111. } else if (native.is_valid()) {
  1112. p_script->native=native;
  1113. } else {
  1114. _set_error("Could not determine inheritance",p_class);
  1115. return ERR_FILE_NOT_FOUND;
  1116. }
  1117. }
  1118. //print_line("Script: "+p_script->get_path()+" indices: "+itos(p_script->member_indices.size()));
  1119. for(int i=0;i<p_class->variables.size();i++) {
  1120. StringName name = p_class->variables[i].identifier;
  1121. if (p_script->member_indices.has(name)) {
  1122. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1123. return ERR_ALREADY_EXISTS;
  1124. }
  1125. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1126. p_script->member_info[name]=p_class->variables[i]._export;
  1127. #ifdef TOOLS_ENABLED
  1128. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1129. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1130. }
  1131. #endif
  1132. }
  1133. //int new_idx = p_script->member_indices.size();
  1134. GDScript::MemberInfo minfo;
  1135. minfo.index = p_script->member_indices.size();
  1136. minfo.setter = p_class->variables[i].setter;
  1137. minfo.getter = p_class->variables[i].getter;
  1138. p_script->member_indices[name]=minfo;
  1139. p_script->members.insert(name);
  1140. }
  1141. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1142. StringName name = p_class->constant_expressions[i].identifier;
  1143. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1144. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1145. p_script->constants.insert(name,constant->value);
  1146. //p_script->constants[constant->value].make_const();
  1147. }
  1148. for(int i=0;i<p_class->_signals.size();i++) {
  1149. StringName name = p_class->_signals[i].name;
  1150. GDScript *c = p_script;
  1151. while(c) {
  1152. if (c->_signals.has(name)) {
  1153. _set_error("Signal '"+name+"' redefined (in current or parent class)",p_class);
  1154. return ERR_ALREADY_EXISTS;
  1155. }
  1156. if (c->base.is_valid()) {
  1157. c=c->base.ptr();
  1158. } else {
  1159. c=NULL;
  1160. }
  1161. }
  1162. if (native.is_valid()) {
  1163. if (ObjectTypeDB::has_signal(native->get_name(),name)) {
  1164. _set_error("Signal '"+name+"' redefined (original in native class '"+String(native->get_name())+"')",p_class);
  1165. return ERR_ALREADY_EXISTS;
  1166. }
  1167. }
  1168. p_script->_signals[name]=p_class->_signals[i].arguments;
  1169. }
  1170. //parse sub-classes
  1171. for(int i=0;i<p_class->subclasses.size();i++) {
  1172. StringName name = p_class->subclasses[i]->name;
  1173. Ref<GDScript> subclass = memnew( GDScript );
  1174. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]);
  1175. if (err)
  1176. return err;
  1177. p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants
  1178. p_script->subclasses.insert(name,subclass);
  1179. }
  1180. //parse methods
  1181. bool has_initializer=false;
  1182. bool has_ready=false;
  1183. for(int i=0;i<p_class->functions.size();i++) {
  1184. if (!has_initializer && p_class->functions[i]->name=="_init")
  1185. has_initializer=true;
  1186. if (!has_ready && p_class->functions[i]->name=="_ready")
  1187. has_ready=true;
  1188. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1189. if (err)
  1190. return err;
  1191. }
  1192. //parse static methods
  1193. for(int i=0;i<p_class->static_functions.size();i++) {
  1194. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1195. if (err)
  1196. return err;
  1197. }
  1198. if (!has_initializer) {
  1199. //create a constructor
  1200. Error err = _parse_function(p_script,p_class,NULL);
  1201. if (err)
  1202. return err;
  1203. }
  1204. if (!has_ready && p_class->ready->statements.size()) {
  1205. //create a constructor
  1206. Error err = _parse_function(p_script,p_class,NULL,true);
  1207. if (err)
  1208. return err;
  1209. }
  1210. #ifdef DEBUG_ENABLED
  1211. //validate setters/getters if debug is enabled
  1212. for(int i=0;i<p_class->variables.size();i++) {
  1213. if (p_class->variables[i].setter) {
  1214. const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().find(p_class->variables[i].setter);
  1215. if (!E) {
  1216. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' not found in class.",NULL);
  1217. err_line=p_class->variables[i].line;
  1218. err_column=0;
  1219. return ERR_PARSE_ERROR;
  1220. }
  1221. if (E->get().is_static()) {
  1222. _set_error("Setter function '"+String(p_class->variables[i].setter)+"' is static.",NULL);
  1223. err_line=p_class->variables[i].line;
  1224. err_column=0;
  1225. return ERR_PARSE_ERROR;
  1226. }
  1227. }
  1228. if (p_class->variables[i].getter) {
  1229. const Map<StringName,GDFunction>::Element *E=p_script->get_member_functions().find(p_class->variables[i].getter);
  1230. if (!E) {
  1231. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' not found in class.",NULL);
  1232. err_line=p_class->variables[i].line;
  1233. err_column=0;
  1234. return ERR_PARSE_ERROR;
  1235. }
  1236. if (E->get().is_static()) {
  1237. _set_error("Getter function '"+String(p_class->variables[i].getter)+"' is static.",NULL);
  1238. err_line=p_class->variables[i].line;
  1239. err_column=0;
  1240. return ERR_PARSE_ERROR;
  1241. }
  1242. }
  1243. }
  1244. #endif
  1245. return OK;
  1246. }
  1247. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
  1248. err_line=-1;
  1249. err_column=-1;
  1250. error="";
  1251. parser=p_parser;
  1252. const GDParser::Node* root = parser->get_parse_tree();
  1253. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1254. source=p_script->get_path();
  1255. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root));
  1256. if (err)
  1257. return err;
  1258. return OK;
  1259. }
  1260. String GDCompiler::get_error() const {
  1261. return error;
  1262. }
  1263. int GDCompiler::get_error_line() const{
  1264. return err_line;
  1265. }
  1266. int GDCompiler::get_error_column() const{
  1267. return err_column;
  1268. }
  1269. GDCompiler::GDCompiler()
  1270. {
  1271. }