gd_compiler.cpp 47 KB

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