gd_compiler.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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 (p_class->extends_class.size()) {
  981. for(int i=0;i<p_class->extends_class.size();i++) {
  982. String sub = p_class->extends_class[i];
  983. if (script->subclasses.has(sub)) {
  984. script=script->subclasses[sub];
  985. } else {
  986. _set_error("Could not find subclass: "+sub,p_class);
  987. return ERR_FILE_NOT_FOUND;
  988. }
  989. }
  990. }
  991. } else {
  992. ERR_FAIL_COND_V(p_class->extends_class.size()==0,ERR_BUG);
  993. //look around for the subclasses
  994. String base=p_class->extends_class[0];
  995. GDScript *p = p_owner;
  996. Ref<GDScript> base_class;
  997. while(p) {
  998. if (p->subclasses.has(base)) {
  999. base_class=p->subclasses[base];
  1000. break;
  1001. }
  1002. p=p->_owner;
  1003. }
  1004. if (base_class.is_valid()) {
  1005. for(int i=1;i<p_class->extends_class.size();i++) {
  1006. String subclass=p_class->extends_class[i];
  1007. if (base_class->subclasses.has(subclass)) {
  1008. base_class=base_class->subclasses[subclass];
  1009. } else {
  1010. _set_error("Could not find subclass: "+subclass,p_class);
  1011. return ERR_FILE_NOT_FOUND;
  1012. }
  1013. }
  1014. script=base_class;
  1015. } else {
  1016. if (p_class->extends_class.size()>1) {
  1017. _set_error("Invalid inheritance (unknown class+subclasses)",p_class);
  1018. return ERR_FILE_NOT_FOUND;
  1019. }
  1020. //if not found, try engine classes
  1021. if (!GDScriptLanguage::get_singleton()->get_global_map().has(base)) {
  1022. _set_error("Unknown class: '"+base+"'",p_class);
  1023. return ERR_FILE_NOT_FOUND;
  1024. }
  1025. int base_idx = GDScriptLanguage::get_singleton()->get_global_map()[base];
  1026. native = GDScriptLanguage::get_singleton()->get_global_array()[base_idx];
  1027. if (!native.is_valid()) {
  1028. _set_error("Global not a class: '"+base+"'",p_class);
  1029. return ERR_FILE_NOT_FOUND;
  1030. }
  1031. }
  1032. }
  1033. if (script.is_valid()) {
  1034. p_script->base=script;
  1035. p_script->_base=p_script->base.ptr();
  1036. p_script->member_indices=script->member_indices;
  1037. } else if (native.is_valid()) {
  1038. p_script->native=native;
  1039. } else {
  1040. _set_error("Could not determine inheritance",p_class);
  1041. return ERR_FILE_NOT_FOUND;
  1042. }
  1043. }
  1044. for(int i=0;i<p_class->variables.size();i++) {
  1045. StringName name = p_class->variables[i].identifier;
  1046. if (p_script->member_indices.has(name)) {
  1047. _set_error("Member '"+name+"' already exists (in current or parent class)",p_class);
  1048. return ERR_ALREADY_EXISTS;
  1049. }
  1050. if (p_class->variables[i]._export.type!=Variant::NIL) {
  1051. p_script->member_info[name]=p_class->variables[i]._export;
  1052. #ifdef TOOLS_ENABLED
  1053. if (p_class->variables[i].default_value.get_type()!=Variant::NIL) {
  1054. p_script->member_default_values[name]=p_class->variables[i].default_value;
  1055. }
  1056. #endif
  1057. }
  1058. int new_idx = p_script->member_indices.size();
  1059. p_script->member_indices[name]=new_idx;
  1060. p_script->members.insert(name);
  1061. }
  1062. for(int i=0;i<p_class->constant_expressions.size();i++) {
  1063. StringName name = p_class->constant_expressions[i].identifier;
  1064. ERR_CONTINUE( p_class->constant_expressions[i].expression->type!=GDParser::Node::TYPE_CONSTANT );
  1065. GDParser::ConstantNode *constant = static_cast<GDParser::ConstantNode*>(p_class->constant_expressions[i].expression);
  1066. p_script->constants.insert(name,constant->value);
  1067. //p_script->constants[constant->value].make_const();
  1068. }
  1069. //parse sub-classes
  1070. for(int i=0;i<p_class->subclasses.size();i++) {
  1071. StringName name = p_class->subclasses[i]->name;
  1072. Ref<GDScript> subclass = memnew( GDScript );
  1073. Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]);
  1074. if (err)
  1075. return err;
  1076. p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants
  1077. p_script->subclasses.insert(name,subclass);
  1078. }
  1079. //parse methods
  1080. bool has_initializer=false;
  1081. for(int i=0;i<p_class->functions.size();i++) {
  1082. if (!has_initializer && p_class->functions[i]->name=="_init")
  1083. has_initializer=true;
  1084. Error err = _parse_function(p_script,p_class,p_class->functions[i]);
  1085. if (err)
  1086. return err;
  1087. }
  1088. //parse static methods
  1089. for(int i=0;i<p_class->static_functions.size();i++) {
  1090. Error err = _parse_function(p_script,p_class,p_class->static_functions[i]);
  1091. if (err)
  1092. return err;
  1093. }
  1094. if (!has_initializer) {
  1095. //create a constructor
  1096. Error err = _parse_function(p_script,p_class,NULL);
  1097. if (err)
  1098. return err;
  1099. }
  1100. return OK;
  1101. }
  1102. Error GDCompiler::compile(const GDParser *p_parser,GDScript *p_script) {
  1103. err_line=-1;
  1104. err_column=-1;
  1105. error="";
  1106. parser=p_parser;
  1107. const GDParser::Node* root = parser->get_parse_tree();
  1108. ERR_FAIL_COND_V(root->type!=GDParser::Node::TYPE_CLASS,ERR_INVALID_DATA);
  1109. source=p_script->get_path();
  1110. Error err = _parse_class(p_script,NULL,static_cast<const GDParser::ClassNode*>(root));
  1111. if (err)
  1112. return err;
  1113. return OK;
  1114. }
  1115. String GDCompiler::get_error() const {
  1116. return error;
  1117. }
  1118. int GDCompiler::get_error_line() const{
  1119. return err_line;
  1120. }
  1121. int GDCompiler::get_error_column() const{
  1122. return err_column;
  1123. }
  1124. GDCompiler::GDCompiler()
  1125. {
  1126. }