gdscript_byte_codegen.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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, 0);
  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->_instruction_args_size = instr_args_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, 3);
  155. append(p_left_operand);
  156. append(p_right_operand);
  157. append(p_target);
  158. append(p_operator);
  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, 3);
  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, 3);
  168. append(p_source);
  169. append(p_target);
  170. append(p_type);
  171. }
  172. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  173. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  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, 1);
  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, 1);
  187. append(p_target);
  188. // Jump away from the fail condition.
  189. append(GDScriptFunction::OPCODE_JUMP, 0);
  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, 0);
  197. append(p_target);
  198. }
  199. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  200. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  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, 1);
  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, 1);
  214. append(p_target);
  215. // Jump away from the success condition.
  216. append(GDScriptFunction::OPCODE_JUMP, 0);
  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, 1);
  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, 1);
  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, 2);
  237. append(ternary_result.back()->get());
  238. append(p_expr);
  239. // Jump away from the false path.
  240. append(GDScriptFunction::OPCODE_JUMP, 0);
  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, 2);
  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, 3);
  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, 3);
  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, 2);
  270. append(p_target);
  271. append(p_source);
  272. append(p_name);
  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, 2);
  276. append(p_source);
  277. append(p_target);
  278. append(p_name);
  279. }
  280. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  281. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  282. append(p_value);
  283. append(p_name);
  284. }
  285. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  286. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  287. append(p_target);
  288. append(p_name);
  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, 2);
  296. append(p_target);
  297. append(p_source);
  298. append(p_target.type.builtin_type);
  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, 3);
  304. append(p_target);
  305. append(p_source);
  306. append(class_idx);
  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. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  313. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  314. append(p_target);
  315. append(p_source);
  316. append(idx);
  317. } break;
  318. default: {
  319. ERR_PRINT("Compiler bug: unresolved assign.");
  320. // Shouldn't get here, but fail-safe to a regular assignment
  321. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  322. append(p_target);
  323. append(p_source);
  324. }
  325. }
  326. } else {
  327. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  328. // Need conversion..
  329. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  330. append(p_target);
  331. append(p_source);
  332. append(p_target.type.builtin_type);
  333. } else {
  334. // Either untyped assignment or already type-checked by the parser
  335. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  336. append(p_target);
  337. append(p_source);
  338. }
  339. }
  340. }
  341. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  342. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  343. append(p_target);
  344. }
  345. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  346. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  347. append(p_target);
  348. }
  349. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  350. int index = 0;
  351. switch (p_type.kind) {
  352. case GDScriptDataType::BUILTIN: {
  353. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  354. index = p_type.builtin_type;
  355. } break;
  356. case GDScriptDataType::NATIVE: {
  357. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  358. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  359. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  360. index = class_idx;
  361. } break;
  362. case GDScriptDataType::SCRIPT:
  363. case GDScriptDataType::GDSCRIPT: {
  364. Variant script = p_type.script_type;
  365. int idx = get_constant_pos(script);
  366. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  367. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  368. index = idx;
  369. } break;
  370. default: {
  371. return;
  372. }
  373. }
  374. append(p_source);
  375. append(p_target);
  376. append(index);
  377. }
  378. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  379. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  380. for (int i = 0; i < p_arguments.size(); i++) {
  381. append(p_arguments[i]);
  382. }
  383. append(p_base);
  384. append(p_target);
  385. append(p_arguments.size());
  386. append(p_function_name);
  387. }
  388. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  389. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  390. for (int i = 0; i < p_arguments.size(); i++) {
  391. append(p_arguments[i]);
  392. }
  393. append(p_target);
  394. append(p_arguments.size());
  395. append(p_function_name);
  396. }
  397. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  398. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  399. for (int i = 0; i < p_arguments.size(); i++) {
  400. append(p_arguments[i]);
  401. }
  402. append(p_base);
  403. append(p_target);
  404. append(p_arguments.size());
  405. append(p_function_name);
  406. }
  407. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  408. append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
  409. for (int i = 0; i < p_arguments.size(); i++) {
  410. append(p_arguments[i]);
  411. }
  412. append(p_target);
  413. append(p_arguments.size());
  414. append(p_function);
  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, 2 + p_arguments.size());
  418. for (int i = 0; i < p_arguments.size(); i++) {
  419. append(p_arguments[i]);
  420. }
  421. append(p_base);
  422. append(p_target);
  423. append(p_arguments.size());
  424. append(p_method->get_name());
  425. }
  426. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  427. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  428. for (int i = 0; i < p_arguments.size(); i++) {
  429. append(p_arguments[i]);
  430. }
  431. append(p_base);
  432. append(p_target);
  433. append(p_arguments.size());
  434. append(p_method->get_name());
  435. }
  436. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  437. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  438. for (int i = 0; i < p_arguments.size(); i++) {
  439. append(p_arguments[i]);
  440. }
  441. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  442. append(p_target);
  443. append(p_arguments.size());
  444. append(p_function_name);
  445. }
  446. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  447. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  448. for (int i = 0; i < p_arguments.size(); i++) {
  449. append(p_arguments[i]);
  450. }
  451. append(p_base);
  452. append(p_target);
  453. append(p_arguments.size());
  454. append(p_function_name);
  455. }
  456. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  457. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  458. for (int i = 0; i < p_arguments.size(); i++) {
  459. append(p_arguments[i]);
  460. }
  461. append(p_target);
  462. append(p_arguments.size());
  463. append(p_type);
  464. }
  465. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  466. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  467. for (int i = 0; i < p_arguments.size(); i++) {
  468. append(p_arguments[i]);
  469. }
  470. append(p_target);
  471. append(p_arguments.size());
  472. }
  473. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  474. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  475. for (int i = 0; i < p_arguments.size(); i++) {
  476. append(p_arguments[i]);
  477. }
  478. append(p_target);
  479. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  480. }
  481. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  482. append(GDScriptFunction::OPCODE_AWAIT, 1);
  483. append(p_operand);
  484. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  485. append(p_target);
  486. }
  487. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  488. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  489. append(p_condition);
  490. if_jmp_addrs.push_back(opcodes.size());
  491. append(0); // Jump destination, will be patched.
  492. }
  493. void GDScriptByteCodeGenerator::write_else() {
  494. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  495. int else_jmp_addr = opcodes.size();
  496. append(0); // Jump destination, will be patched.
  497. patch_jump(if_jmp_addrs.back()->get());
  498. if_jmp_addrs.pop_back();
  499. if_jmp_addrs.push_back(else_jmp_addr);
  500. }
  501. void GDScriptByteCodeGenerator::write_endif() {
  502. patch_jump(if_jmp_addrs.back()->get());
  503. if_jmp_addrs.pop_back();
  504. }
  505. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
  506. int counter_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  507. int container_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  508. current_breaks_to_patch.push_back(List<int>());
  509. // Assign container.
  510. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  511. append(container_pos);
  512. append(p_list);
  513. // Begin loop.
  514. append(GDScriptFunction::OPCODE_ITERATE_BEGIN, 3);
  515. append(counter_pos);
  516. append(container_pos);
  517. append(p_variable);
  518. for_jmp_addrs.push_back(opcodes.size());
  519. append(0); // End of loop address, will be patched.
  520. append(GDScriptFunction::OPCODE_JUMP, 0);
  521. append(opcodes.size() + 6); // Skip over 'continue' code.
  522. // Next iteration.
  523. int continue_addr = opcodes.size();
  524. continue_addrs.push_back(continue_addr);
  525. append(GDScriptFunction::OPCODE_ITERATE, 3);
  526. append(counter_pos);
  527. append(container_pos);
  528. append(p_variable);
  529. for_jmp_addrs.push_back(opcodes.size());
  530. append(0); // Jump destination, will be patched.
  531. }
  532. void GDScriptByteCodeGenerator::write_endfor() {
  533. // Jump back to loop check.
  534. append(GDScriptFunction::OPCODE_JUMP, 0);
  535. append(continue_addrs.back()->get());
  536. continue_addrs.pop_back();
  537. // Patch end jumps (two of them).
  538. for (int i = 0; i < 2; i++) {
  539. patch_jump(for_jmp_addrs.back()->get());
  540. for_jmp_addrs.pop_back();
  541. }
  542. // Patch break statements.
  543. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  544. patch_jump(E->get());
  545. }
  546. current_breaks_to_patch.pop_back();
  547. // Remove loop temporaries.
  548. pop_temporary();
  549. pop_temporary();
  550. }
  551. void GDScriptByteCodeGenerator::start_while_condition() {
  552. current_breaks_to_patch.push_back(List<int>());
  553. continue_addrs.push_back(opcodes.size());
  554. }
  555. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  556. // Condition check.
  557. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  558. append(p_condition);
  559. while_jmp_addrs.push_back(opcodes.size());
  560. append(0); // End of loop address, will be patched.
  561. }
  562. void GDScriptByteCodeGenerator::write_endwhile() {
  563. // Jump back to loop check.
  564. append(GDScriptFunction::OPCODE_JUMP, 0);
  565. append(continue_addrs.back()->get());
  566. continue_addrs.pop_back();
  567. // Patch end jump.
  568. patch_jump(while_jmp_addrs.back()->get());
  569. while_jmp_addrs.pop_back();
  570. // Patch break statements.
  571. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  572. patch_jump(E->get());
  573. }
  574. current_breaks_to_patch.pop_back();
  575. }
  576. void GDScriptByteCodeGenerator::start_match() {
  577. match_continues_to_patch.push_back(List<int>());
  578. }
  579. void GDScriptByteCodeGenerator::start_match_branch() {
  580. // Patch continue statements.
  581. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  582. patch_jump(E->get());
  583. }
  584. match_continues_to_patch.pop_back();
  585. // Start a new list for next branch.
  586. match_continues_to_patch.push_back(List<int>());
  587. }
  588. void GDScriptByteCodeGenerator::end_match() {
  589. // Patch continue statements.
  590. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  591. patch_jump(E->get());
  592. }
  593. match_continues_to_patch.pop_back();
  594. }
  595. void GDScriptByteCodeGenerator::write_break() {
  596. append(GDScriptFunction::OPCODE_JUMP, 0);
  597. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  598. append(0);
  599. }
  600. void GDScriptByteCodeGenerator::write_continue() {
  601. append(GDScriptFunction::OPCODE_JUMP, 0);
  602. append(continue_addrs.back()->get());
  603. }
  604. void GDScriptByteCodeGenerator::write_continue_match() {
  605. append(GDScriptFunction::OPCODE_JUMP, 0);
  606. match_continues_to_patch.back()->get().push_back(opcodes.size());
  607. append(0);
  608. }
  609. void GDScriptByteCodeGenerator::write_breakpoint() {
  610. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  611. }
  612. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  613. append(GDScriptFunction::OPCODE_LINE, 0);
  614. append(p_line);
  615. current_line = p_line;
  616. }
  617. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  618. append(GDScriptFunction::OPCODE_RETURN, 1);
  619. append(p_return_value);
  620. }
  621. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  622. append(GDScriptFunction::OPCODE_ASSERT, 2);
  623. append(p_test);
  624. append(p_message);
  625. }
  626. void GDScriptByteCodeGenerator::start_block() {
  627. push_stack_identifiers();
  628. }
  629. void GDScriptByteCodeGenerator::end_block() {
  630. pop_stack_identifiers();
  631. }
  632. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  633. if (!ended && function != nullptr) {
  634. memdelete(function);
  635. }
  636. }