gdscript_byte_codegen.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*************************************************************************/
  2. /* gdscript_byte_codegen.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_byte_codegen.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "gdscript.h"
  33. uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) {
  34. #ifdef TOOLS_ENABLED
  35. function->arg_names.push_back(p_name);
  36. #endif
  37. function->_argument_count++;
  38. function->argument_types.push_back(p_type);
  39. if (p_is_optional) {
  40. if (function->_default_arg_count == 0) {
  41. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  42. }
  43. function->default_arguments.push_back(opcodes.size());
  44. function->_default_arg_count++;
  45. }
  46. return add_local(p_name, p_type);
  47. }
  48. uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  49. int stack_pos = increase_stack();
  50. add_stack_identifier(p_name, stack_pos);
  51. return stack_pos;
  52. }
  53. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  54. int index = add_or_get_constant(p_constant);
  55. local_constants[p_name] = index;
  56. return index;
  57. }
  58. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  59. if (constant_map.has(p_constant)) {
  60. return constant_map[p_constant];
  61. }
  62. int index = constant_map.size();
  63. constant_map[p_constant] = index;
  64. return index;
  65. }
  66. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  67. return get_name_map_pos(p_name);
  68. }
  69. uint32_t GDScriptByteCodeGenerator::add_temporary() {
  70. current_temporaries++;
  71. return increase_stack();
  72. }
  73. void GDScriptByteCodeGenerator::pop_temporary() {
  74. current_stack_size--;
  75. current_temporaries--;
  76. }
  77. void GDScriptByteCodeGenerator::start_parameters() {}
  78. void GDScriptByteCodeGenerator::end_parameters() {
  79. function->default_arguments.invert();
  80. }
  81. void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) {
  82. function = memnew(GDScriptFunction);
  83. debug_stack = EngineDebugger::is_active();
  84. function->name = p_function_name;
  85. function->_script = p_script;
  86. function->source = p_script->get_path();
  87. #ifdef DEBUG_ENABLED
  88. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  89. function->_func_cname = function->func_cname.get_data();
  90. #endif
  91. function->_static = p_static;
  92. function->return_type = p_return_type;
  93. function->rpc_mode = p_rpc_mode;
  94. function->_argument_count = 0;
  95. }
  96. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  97. append(GDScriptFunction::OPCODE_END);
  98. if (constant_map.size()) {
  99. function->_constant_count = constant_map.size();
  100. function->constants.resize(constant_map.size());
  101. function->_constants_ptr = function->constants.ptrw();
  102. const Variant *K = nullptr;
  103. while ((K = constant_map.next(K))) {
  104. int idx = constant_map[*K];
  105. function->constants.write[idx] = *K;
  106. }
  107. } else {
  108. function->_constants_ptr = nullptr;
  109. function->_constant_count = 0;
  110. }
  111. if (name_map.size()) {
  112. function->global_names.resize(name_map.size());
  113. function->_global_names_ptr = &function->global_names[0];
  114. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  115. function->global_names.write[E->get()] = E->key();
  116. }
  117. function->_global_names_count = function->global_names.size();
  118. } else {
  119. function->_global_names_ptr = nullptr;
  120. function->_global_names_count = 0;
  121. }
  122. if (opcodes.size()) {
  123. function->code = opcodes;
  124. function->_code_ptr = &function->code[0];
  125. function->_code_size = opcodes.size();
  126. } else {
  127. function->_code_ptr = nullptr;
  128. function->_code_size = 0;
  129. }
  130. if (function->default_arguments.size()) {
  131. function->_default_arg_count = function->default_arguments.size();
  132. function->_default_arg_ptr = &function->default_arguments[0];
  133. } else {
  134. function->_default_arg_count = 0;
  135. function->_default_arg_ptr = nullptr;
  136. }
  137. if (debug_stack) {
  138. function->stack_debug = stack_debug;
  139. }
  140. function->_stack_size = stack_max;
  141. function->_call_size = call_max;
  142. ended = true;
  143. return function;
  144. }
  145. #ifdef DEBUG_ENABLED
  146. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  147. function->profile.signature = p_signature;
  148. }
  149. #endif
  150. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  151. function->_initial_line = p_line;
  152. }
  153. void GDScriptByteCodeGenerator::write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  154. append(GDScriptFunction::OPCODE_OPERATOR);
  155. append(p_operator);
  156. append(p_left_operand);
  157. append(p_right_operand);
  158. append(p_target);
  159. }
  160. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  161. append(GDScriptFunction::OPCODE_EXTENDS_TEST);
  162. append(p_source);
  163. append(p_type);
  164. append(p_target);
  165. }
  166. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  167. append(GDScriptFunction::OPCODE_IS_BUILTIN);
  168. append(p_source);
  169. append(p_type);
  170. append(p_target);
  171. }
  172. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  173. append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  174. append(p_left_operand);
  175. logic_op_jump_pos1.push_back(opcodes.size());
  176. append(0); // Jump target, will be patched.
  177. }
  178. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  179. append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  180. append(p_right_operand);
  181. logic_op_jump_pos2.push_back(opcodes.size());
  182. append(0); // Jump target, will be patched.
  183. }
  184. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  185. // If here means both operands are true.
  186. append(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  187. append(p_target);
  188. // Jump away from the fail condition.
  189. append(GDScriptFunction::OPCODE_JUMP);
  190. append(opcodes.size() + 3);
  191. // Here it means one of operands is false.
  192. patch_jump(logic_op_jump_pos1.back()->get());
  193. patch_jump(logic_op_jump_pos2.back()->get());
  194. logic_op_jump_pos1.pop_back();
  195. logic_op_jump_pos2.pop_back();
  196. append(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  197. append(p_target);
  198. }
  199. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  200. append(GDScriptFunction::OPCODE_JUMP_IF);
  201. append(p_left_operand);
  202. logic_op_jump_pos1.push_back(opcodes.size());
  203. append(0); // Jump target, will be patched.
  204. }
  205. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  206. append(GDScriptFunction::OPCODE_JUMP_IF);
  207. append(p_right_operand);
  208. logic_op_jump_pos2.push_back(opcodes.size());
  209. append(0); // Jump target, will be patched.
  210. }
  211. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  212. // If here means both operands are false.
  213. append(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  214. append(p_target);
  215. // Jump away from the success condition.
  216. append(GDScriptFunction::OPCODE_JUMP);
  217. append(opcodes.size() + 3);
  218. // Here it means one of operands is false.
  219. patch_jump(logic_op_jump_pos1.back()->get());
  220. patch_jump(logic_op_jump_pos2.back()->get());
  221. logic_op_jump_pos1.pop_back();
  222. logic_op_jump_pos2.pop_back();
  223. append(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  224. append(p_target);
  225. }
  226. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  227. ternary_result.push_back(p_target);
  228. }
  229. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  230. append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  231. append(p_condition);
  232. ternary_jump_fail_pos.push_back(opcodes.size());
  233. append(0); // Jump target, will be patched.
  234. }
  235. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  236. append(GDScriptFunction::OPCODE_ASSIGN);
  237. append(ternary_result.back()->get());
  238. append(p_expr);
  239. // Jump away from the false path.
  240. append(GDScriptFunction::OPCODE_JUMP);
  241. ternary_jump_skip_pos.push_back(opcodes.size());
  242. append(0);
  243. // Fail must jump here.
  244. patch_jump(ternary_jump_fail_pos.back()->get());
  245. ternary_jump_fail_pos.pop_back();
  246. }
  247. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  248. append(GDScriptFunction::OPCODE_ASSIGN);
  249. append(ternary_result.back()->get());
  250. append(p_expr);
  251. }
  252. void GDScriptByteCodeGenerator::write_end_ternary() {
  253. patch_jump(ternary_jump_skip_pos.back()->get());
  254. ternary_jump_skip_pos.pop_back();
  255. }
  256. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  257. append(GDScriptFunction::OPCODE_SET);
  258. append(p_target);
  259. append(p_index);
  260. append(p_source);
  261. }
  262. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  263. append(GDScriptFunction::OPCODE_GET);
  264. append(p_source);
  265. append(p_index);
  266. append(p_target);
  267. }
  268. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  269. append(GDScriptFunction::OPCODE_SET_NAMED);
  270. append(p_target);
  271. append(p_name);
  272. append(p_source);
  273. }
  274. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  275. append(GDScriptFunction::OPCODE_GET_NAMED);
  276. append(p_source);
  277. append(p_name);
  278. append(p_target);
  279. }
  280. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  281. append(GDScriptFunction::OPCODE_SET_MEMBER);
  282. append(p_name);
  283. append(p_value);
  284. }
  285. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  286. append(GDScriptFunction::OPCODE_GET_MEMBER);
  287. append(p_name);
  288. append(p_target);
  289. }
  290. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  291. if (p_target.type.has_type && !p_source.type.has_type) {
  292. // Typed assignment.
  293. switch (p_target.type.kind) {
  294. case GDScriptDataType::BUILTIN: {
  295. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  296. append(p_target.type.builtin_type);
  297. append(p_target);
  298. append(p_source);
  299. } break;
  300. case GDScriptDataType::NATIVE: {
  301. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  302. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  303. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
  304. append(class_idx);
  305. append(p_target);
  306. append(p_source);
  307. } break;
  308. case GDScriptDataType::SCRIPT:
  309. case GDScriptDataType::GDSCRIPT: {
  310. Variant script = p_target.type.script_type;
  311. int idx = get_constant_pos(script);
  312. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
  313. append(idx);
  314. append(p_target);
  315. append(p_source);
  316. } break;
  317. default: {
  318. ERR_PRINT("Compiler bug: unresolved assign.");
  319. // Shouldn't get here, but fail-safe to a regular assignment
  320. append(GDScriptFunction::OPCODE_ASSIGN);
  321. append(p_target);
  322. append(p_source);
  323. }
  324. }
  325. } else {
  326. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  327. // Need conversion..
  328. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  329. append(p_target.type.builtin_type);
  330. append(p_target);
  331. append(p_source);
  332. } else {
  333. // Either untyped assignment or already type-checked by the parser
  334. append(GDScriptFunction::OPCODE_ASSIGN);
  335. append(p_target);
  336. append(p_source);
  337. }
  338. }
  339. }
  340. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  341. append(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  342. append(p_target);
  343. }
  344. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  345. append(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  346. append(p_target);
  347. }
  348. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  349. switch (p_type.kind) {
  350. case GDScriptDataType::BUILTIN: {
  351. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  352. append(p_type.builtin_type);
  353. } break;
  354. case GDScriptDataType::NATIVE: {
  355. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  356. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  357. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
  358. append(class_idx);
  359. } break;
  360. case GDScriptDataType::SCRIPT:
  361. case GDScriptDataType::GDSCRIPT: {
  362. Variant script = p_type.script_type;
  363. int idx = get_constant_pos(script);
  364. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
  365. append(idx);
  366. } break;
  367. default: {
  368. return;
  369. }
  370. }
  371. append(p_source);
  372. append(p_target);
  373. }
  374. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  375. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  376. append(p_arguments.size());
  377. append(p_base);
  378. append(p_function_name);
  379. for (int i = 0; i < p_arguments.size(); i++) {
  380. append(p_arguments[i]);
  381. }
  382. append(p_target);
  383. alloc_call(p_arguments.size());
  384. }
  385. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  386. append(GDScriptFunction::OPCODE_CALL_SELF_BASE);
  387. append(p_function_name);
  388. append(p_arguments.size());
  389. for (int i = 0; i < p_arguments.size(); i++) {
  390. append(p_arguments[i]);
  391. }
  392. append(p_target);
  393. alloc_call(p_arguments.size());
  394. }
  395. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  396. append(GDScriptFunction::OPCODE_CALL_ASYNC);
  397. append(p_arguments.size());
  398. append(p_base);
  399. append(p_function_name);
  400. for (int i = 0; i < p_arguments.size(); i++) {
  401. append(p_arguments[i]);
  402. }
  403. append(p_target);
  404. alloc_call(p_arguments.size());
  405. }
  406. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  407. append(GDScriptFunction::OPCODE_CALL_BUILT_IN);
  408. append(p_function);
  409. append(p_arguments.size());
  410. for (int i = 0; i < p_arguments.size(); i++) {
  411. append(p_arguments[i]);
  412. }
  413. append(p_target);
  414. alloc_call(p_arguments.size());
  415. }
  416. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  417. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  418. append(p_arguments.size());
  419. append(p_base);
  420. append(p_method->get_name());
  421. for (int i = 0; i < p_arguments.size(); i++) {
  422. append(p_arguments[i]);
  423. }
  424. append(p_target);
  425. alloc_call(p_arguments.size());
  426. }
  427. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  428. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  429. append(p_arguments.size());
  430. append(p_base);
  431. append(p_method->get_name());
  432. for (int i = 0; i < p_arguments.size(); i++) {
  433. append(p_arguments[i]);
  434. }
  435. append(p_target);
  436. alloc_call(p_arguments.size());
  437. }
  438. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  439. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  440. append(p_arguments.size());
  441. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  442. append(p_function_name);
  443. for (int i = 0; i < p_arguments.size(); i++) {
  444. append(p_arguments[i]);
  445. }
  446. append(p_target);
  447. alloc_call(p_arguments.size());
  448. }
  449. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  450. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN);
  451. append(p_arguments.size());
  452. append(p_base);
  453. append(p_function_name);
  454. for (int i = 0; i < p_arguments.size(); i++) {
  455. append(p_arguments[i]);
  456. }
  457. append(p_target);
  458. alloc_call(p_arguments.size());
  459. }
  460. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  461. append(GDScriptFunction::OPCODE_CONSTRUCT);
  462. append(p_type);
  463. append(p_arguments.size());
  464. for (int i = 0; i < p_arguments.size(); i++) {
  465. append(p_arguments[i]);
  466. }
  467. append(p_target);
  468. }
  469. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  470. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY);
  471. append(p_arguments.size());
  472. for (int i = 0; i < p_arguments.size(); i++) {
  473. append(p_arguments[i]);
  474. }
  475. append(p_target);
  476. }
  477. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  478. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY);
  479. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  480. for (int i = 0; i < p_arguments.size(); i++) {
  481. append(p_arguments[i]);
  482. }
  483. append(p_target);
  484. }
  485. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  486. append(GDScriptFunction::OPCODE_AWAIT);
  487. append(p_operand);
  488. append(GDScriptFunction::OPCODE_AWAIT_RESUME);
  489. append(p_target);
  490. }
  491. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  492. append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  493. append(p_condition);
  494. if_jmp_addrs.push_back(opcodes.size());
  495. append(0); // Jump destination, will be patched.
  496. }
  497. void GDScriptByteCodeGenerator::write_else() {
  498. append(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
  499. int else_jmp_addr = opcodes.size();
  500. append(0); // Jump destination, will be patched.
  501. patch_jump(if_jmp_addrs.back()->get());
  502. if_jmp_addrs.pop_back();
  503. if_jmp_addrs.push_back(else_jmp_addr);
  504. }
  505. void GDScriptByteCodeGenerator::write_endif() {
  506. patch_jump(if_jmp_addrs.back()->get());
  507. if_jmp_addrs.pop_back();
  508. }
  509. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
  510. int counter_pos = increase_stack() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  511. int container_pos = increase_stack() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  512. current_breaks_to_patch.push_back(List<int>());
  513. // Assign container.
  514. append(GDScriptFunction::OPCODE_ASSIGN);
  515. append(container_pos);
  516. append(p_list);
  517. // Begin loop.
  518. append(GDScriptFunction::OPCODE_ITERATE_BEGIN);
  519. append(counter_pos);
  520. append(container_pos);
  521. for_jmp_addrs.push_back(opcodes.size());
  522. append(0); // End of loop address, will be patched.
  523. append(p_variable);
  524. append(GDScriptFunction::OPCODE_JUMP);
  525. append(opcodes.size() + 6); // Skip over 'continue' code.
  526. // Next iteration.
  527. int continue_addr = opcodes.size();
  528. continue_addrs.push_back(continue_addr);
  529. append(GDScriptFunction::OPCODE_ITERATE);
  530. append(counter_pos);
  531. append(container_pos);
  532. for_jmp_addrs.push_back(opcodes.size());
  533. append(0); // Jump destination, will be patched.
  534. append(p_variable);
  535. }
  536. void GDScriptByteCodeGenerator::write_endfor() {
  537. // Jump back to loop check.
  538. append(GDScriptFunction::OPCODE_JUMP);
  539. append(continue_addrs.back()->get());
  540. continue_addrs.pop_back();
  541. // Patch end jumps (two of them).
  542. for (int i = 0; i < 2; i++) {
  543. patch_jump(for_jmp_addrs.back()->get());
  544. for_jmp_addrs.pop_back();
  545. }
  546. // Patch break statements.
  547. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  548. patch_jump(E->get());
  549. }
  550. current_breaks_to_patch.pop_back();
  551. current_stack_size -= 2; // Remove loop temporaries.
  552. }
  553. void GDScriptByteCodeGenerator::start_while_condition() {
  554. current_breaks_to_patch.push_back(List<int>());
  555. continue_addrs.push_back(opcodes.size());
  556. }
  557. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  558. // Condition check.
  559. append(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  560. append(p_condition);
  561. while_jmp_addrs.push_back(opcodes.size());
  562. append(0); // End of loop address, will be patched.
  563. }
  564. void GDScriptByteCodeGenerator::write_endwhile() {
  565. // Jump back to loop check.
  566. append(GDScriptFunction::OPCODE_JUMP);
  567. append(continue_addrs.back()->get());
  568. continue_addrs.pop_back();
  569. // Patch end jump.
  570. patch_jump(while_jmp_addrs.back()->get());
  571. while_jmp_addrs.pop_back();
  572. // Patch break statements.
  573. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  574. patch_jump(E->get());
  575. }
  576. current_breaks_to_patch.pop_back();
  577. }
  578. void GDScriptByteCodeGenerator::start_match() {
  579. match_continues_to_patch.push_back(List<int>());
  580. }
  581. void GDScriptByteCodeGenerator::start_match_branch() {
  582. // Patch continue statements.
  583. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  584. patch_jump(E->get());
  585. }
  586. match_continues_to_patch.pop_back();
  587. // Start a new list for next branch.
  588. match_continues_to_patch.push_back(List<int>());
  589. }
  590. void GDScriptByteCodeGenerator::end_match() {
  591. // Patch continue statements.
  592. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  593. patch_jump(E->get());
  594. }
  595. match_continues_to_patch.pop_back();
  596. }
  597. void GDScriptByteCodeGenerator::write_break() {
  598. append(GDScriptFunction::OPCODE_JUMP);
  599. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  600. append(0);
  601. }
  602. void GDScriptByteCodeGenerator::write_continue() {
  603. append(GDScriptFunction::OPCODE_JUMP);
  604. append(continue_addrs.back()->get());
  605. }
  606. void GDScriptByteCodeGenerator::write_continue_match() {
  607. append(GDScriptFunction::OPCODE_JUMP);
  608. match_continues_to_patch.back()->get().push_back(opcodes.size());
  609. append(0);
  610. }
  611. void GDScriptByteCodeGenerator::write_breakpoint() {
  612. append(GDScriptFunction::OPCODE_BREAKPOINT);
  613. }
  614. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  615. append(GDScriptFunction::OPCODE_LINE);
  616. append(p_line);
  617. current_line = p_line;
  618. }
  619. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  620. append(GDScriptFunction::OPCODE_RETURN);
  621. append(p_return_value);
  622. }
  623. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  624. append(GDScriptFunction::OPCODE_ASSERT);
  625. append(p_test);
  626. append(p_message);
  627. }
  628. void GDScriptByteCodeGenerator::start_block() {
  629. push_stack_identifiers();
  630. }
  631. void GDScriptByteCodeGenerator::end_block() {
  632. pop_stack_identifiers();
  633. }
  634. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  635. if (!ended && function != nullptr) {
  636. memdelete(function);
  637. }
  638. }