gdscript_byte_codegen.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 (operator_func_map.size()) {
  138. function->operator_funcs.resize(operator_func_map.size());
  139. function->_operator_funcs_count = function->operator_funcs.size();
  140. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  141. for (const Map<Variant::ValidatedOperatorEvaluator, int>::Element *E = operator_func_map.front(); E; E = E->next()) {
  142. function->operator_funcs.write[E->get()] = E->key();
  143. }
  144. } else {
  145. function->_operator_funcs_count = 0;
  146. function->_operator_funcs_ptr = nullptr;
  147. }
  148. if (debug_stack) {
  149. function->stack_debug = stack_debug;
  150. }
  151. function->_stack_size = stack_max;
  152. function->_instruction_args_size = instr_args_max;
  153. ended = true;
  154. return function;
  155. }
  156. #ifdef DEBUG_ENABLED
  157. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  158. function->profile.signature = p_signature;
  159. }
  160. #endif
  161. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  162. function->_initial_line = p_line;
  163. }
  164. #define HAS_BUILTIN_TYPE(m_var) \
  165. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  166. void GDScriptByteCodeGenerator::write_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  167. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  168. // Gather specific operator.
  169. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  170. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  171. append(p_left_operand);
  172. append(p_right_operand);
  173. append(p_target);
  174. append(op_func);
  175. return;
  176. }
  177. // No specific types, perform variant evaluation.
  178. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  179. append(p_left_operand);
  180. append(p_right_operand);
  181. append(p_target);
  182. append(p_operator);
  183. }
  184. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  185. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  186. append(p_source);
  187. append(p_type);
  188. append(p_target);
  189. }
  190. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  191. append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
  192. append(p_source);
  193. append(p_target);
  194. append(p_type);
  195. }
  196. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  197. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  198. append(p_left_operand);
  199. logic_op_jump_pos1.push_back(opcodes.size());
  200. append(0); // Jump target, will be patched.
  201. }
  202. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  203. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  204. append(p_right_operand);
  205. logic_op_jump_pos2.push_back(opcodes.size());
  206. append(0); // Jump target, will be patched.
  207. }
  208. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  209. // If here means both operands are true.
  210. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  211. append(p_target);
  212. // Jump away from the fail condition.
  213. append(GDScriptFunction::OPCODE_JUMP, 0);
  214. append(opcodes.size() + 3);
  215. // Here it means one of operands is false.
  216. patch_jump(logic_op_jump_pos1.back()->get());
  217. patch_jump(logic_op_jump_pos2.back()->get());
  218. logic_op_jump_pos1.pop_back();
  219. logic_op_jump_pos2.pop_back();
  220. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 0);
  221. append(p_target);
  222. }
  223. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  224. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  225. append(p_left_operand);
  226. logic_op_jump_pos1.push_back(opcodes.size());
  227. append(0); // Jump target, will be patched.
  228. }
  229. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  230. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  231. append(p_right_operand);
  232. logic_op_jump_pos2.push_back(opcodes.size());
  233. append(0); // Jump target, will be patched.
  234. }
  235. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  236. // If here means both operands are false.
  237. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  238. append(p_target);
  239. // Jump away from the success condition.
  240. append(GDScriptFunction::OPCODE_JUMP, 0);
  241. append(opcodes.size() + 3);
  242. // Here it means one of operands is false.
  243. patch_jump(logic_op_jump_pos1.back()->get());
  244. patch_jump(logic_op_jump_pos2.back()->get());
  245. logic_op_jump_pos1.pop_back();
  246. logic_op_jump_pos2.pop_back();
  247. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  248. append(p_target);
  249. }
  250. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  251. ternary_result.push_back(p_target);
  252. }
  253. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  254. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  255. append(p_condition);
  256. ternary_jump_fail_pos.push_back(opcodes.size());
  257. append(0); // Jump target, will be patched.
  258. }
  259. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  260. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  261. append(ternary_result.back()->get());
  262. append(p_expr);
  263. // Jump away from the false path.
  264. append(GDScriptFunction::OPCODE_JUMP, 0);
  265. ternary_jump_skip_pos.push_back(opcodes.size());
  266. append(0);
  267. // Fail must jump here.
  268. patch_jump(ternary_jump_fail_pos.back()->get());
  269. ternary_jump_fail_pos.pop_back();
  270. }
  271. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  272. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  273. append(ternary_result.back()->get());
  274. append(p_expr);
  275. }
  276. void GDScriptByteCodeGenerator::write_end_ternary() {
  277. patch_jump(ternary_jump_skip_pos.back()->get());
  278. ternary_jump_skip_pos.pop_back();
  279. }
  280. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  281. append(GDScriptFunction::OPCODE_SET, 3);
  282. append(p_target);
  283. append(p_index);
  284. append(p_source);
  285. }
  286. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  287. append(GDScriptFunction::OPCODE_GET, 3);
  288. append(p_source);
  289. append(p_index);
  290. append(p_target);
  291. }
  292. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  293. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  294. append(p_target);
  295. append(p_source);
  296. append(p_name);
  297. }
  298. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  299. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  300. append(p_source);
  301. append(p_target);
  302. append(p_name);
  303. }
  304. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  305. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  306. append(p_value);
  307. append(p_name);
  308. }
  309. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  310. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  311. append(p_target);
  312. append(p_name);
  313. }
  314. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  315. if (p_target.type.has_type && !p_source.type.has_type) {
  316. // Typed assignment.
  317. switch (p_target.type.kind) {
  318. case GDScriptDataType::BUILTIN: {
  319. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  320. append(p_target);
  321. append(p_source);
  322. append(p_target.type.builtin_type);
  323. } break;
  324. case GDScriptDataType::NATIVE: {
  325. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  326. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  327. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  328. append(p_target);
  329. append(p_source);
  330. append(class_idx);
  331. } break;
  332. case GDScriptDataType::SCRIPT:
  333. case GDScriptDataType::GDSCRIPT: {
  334. Variant script = p_target.type.script_type;
  335. int idx = get_constant_pos(script);
  336. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  337. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  338. append(p_target);
  339. append(p_source);
  340. append(idx);
  341. } break;
  342. default: {
  343. ERR_PRINT("Compiler bug: unresolved assign.");
  344. // Shouldn't get here, but fail-safe to a regular assignment
  345. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  346. append(p_target);
  347. append(p_source);
  348. }
  349. }
  350. } else {
  351. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  352. // Need conversion..
  353. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  354. append(p_target);
  355. append(p_source);
  356. append(p_target.type.builtin_type);
  357. } else {
  358. // Either untyped assignment or already type-checked by the parser
  359. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  360. append(p_target);
  361. append(p_source);
  362. }
  363. }
  364. }
  365. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  366. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  367. append(p_target);
  368. }
  369. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  370. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  371. append(p_target);
  372. }
  373. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  374. int index = 0;
  375. switch (p_type.kind) {
  376. case GDScriptDataType::BUILTIN: {
  377. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  378. index = p_type.builtin_type;
  379. } break;
  380. case GDScriptDataType::NATIVE: {
  381. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  382. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  383. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  384. index = class_idx;
  385. } break;
  386. case GDScriptDataType::SCRIPT:
  387. case GDScriptDataType::GDSCRIPT: {
  388. Variant script = p_type.script_type;
  389. int idx = get_constant_pos(script);
  390. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  391. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  392. index = idx;
  393. } break;
  394. default: {
  395. return;
  396. }
  397. }
  398. append(p_source);
  399. append(p_target);
  400. append(index);
  401. }
  402. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  403. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  404. for (int i = 0; i < p_arguments.size(); i++) {
  405. append(p_arguments[i]);
  406. }
  407. append(p_base);
  408. append(p_target);
  409. append(p_arguments.size());
  410. append(p_function_name);
  411. }
  412. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  413. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  414. for (int i = 0; i < p_arguments.size(); i++) {
  415. append(p_arguments[i]);
  416. }
  417. append(p_target);
  418. append(p_arguments.size());
  419. append(p_function_name);
  420. }
  421. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  422. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  423. for (int i = 0; i < p_arguments.size(); i++) {
  424. append(p_arguments[i]);
  425. }
  426. append(p_base);
  427. append(p_target);
  428. append(p_arguments.size());
  429. append(p_function_name);
  430. }
  431. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  432. append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
  433. for (int i = 0; i < p_arguments.size(); i++) {
  434. append(p_arguments[i]);
  435. }
  436. append(p_target);
  437. append(p_arguments.size());
  438. append(p_function);
  439. }
  440. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  441. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  442. for (int i = 0; i < p_arguments.size(); i++) {
  443. append(p_arguments[i]);
  444. }
  445. append(p_base);
  446. append(p_target);
  447. append(p_arguments.size());
  448. append(p_method->get_name());
  449. }
  450. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, const MethodBind *p_method, const Vector<Address> &p_arguments) {
  451. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  452. for (int i = 0; i < p_arguments.size(); i++) {
  453. append(p_arguments[i]);
  454. }
  455. append(p_base);
  456. append(p_target);
  457. append(p_arguments.size());
  458. append(p_method->get_name());
  459. }
  460. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  461. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  462. for (int i = 0; i < p_arguments.size(); i++) {
  463. append(p_arguments[i]);
  464. }
  465. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  466. append(p_target);
  467. append(p_arguments.size());
  468. append(p_function_name);
  469. }
  470. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  471. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  472. for (int i = 0; i < p_arguments.size(); i++) {
  473. append(p_arguments[i]);
  474. }
  475. append(p_base);
  476. append(p_target);
  477. append(p_arguments.size());
  478. append(p_function_name);
  479. }
  480. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  481. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  482. for (int i = 0; i < p_arguments.size(); i++) {
  483. append(p_arguments[i]);
  484. }
  485. append(p_target);
  486. append(p_arguments.size());
  487. append(p_type);
  488. }
  489. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  490. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  491. for (int i = 0; i < p_arguments.size(); i++) {
  492. append(p_arguments[i]);
  493. }
  494. append(p_target);
  495. append(p_arguments.size());
  496. }
  497. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  498. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  499. for (int i = 0; i < p_arguments.size(); i++) {
  500. append(p_arguments[i]);
  501. }
  502. append(p_target);
  503. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  504. }
  505. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  506. append(GDScriptFunction::OPCODE_AWAIT, 1);
  507. append(p_operand);
  508. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  509. append(p_target);
  510. }
  511. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  512. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  513. append(p_condition);
  514. if_jmp_addrs.push_back(opcodes.size());
  515. append(0); // Jump destination, will be patched.
  516. }
  517. void GDScriptByteCodeGenerator::write_else() {
  518. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  519. int else_jmp_addr = opcodes.size();
  520. append(0); // Jump destination, will be patched.
  521. patch_jump(if_jmp_addrs.back()->get());
  522. if_jmp_addrs.pop_back();
  523. if_jmp_addrs.push_back(else_jmp_addr);
  524. }
  525. void GDScriptByteCodeGenerator::write_endif() {
  526. patch_jump(if_jmp_addrs.back()->get());
  527. if_jmp_addrs.pop_back();
  528. }
  529. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, const Address &p_list) {
  530. int counter_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  531. int container_pos = add_temporary() | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  532. current_breaks_to_patch.push_back(List<int>());
  533. // Assign container.
  534. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  535. append(container_pos);
  536. append(p_list);
  537. // Begin loop.
  538. append(GDScriptFunction::OPCODE_ITERATE_BEGIN, 3);
  539. append(counter_pos);
  540. append(container_pos);
  541. append(p_variable);
  542. for_jmp_addrs.push_back(opcodes.size());
  543. append(0); // End of loop address, will be patched.
  544. append(GDScriptFunction::OPCODE_JUMP, 0);
  545. append(opcodes.size() + 6); // Skip over 'continue' code.
  546. // Next iteration.
  547. int continue_addr = opcodes.size();
  548. continue_addrs.push_back(continue_addr);
  549. append(GDScriptFunction::OPCODE_ITERATE, 3);
  550. append(counter_pos);
  551. append(container_pos);
  552. append(p_variable);
  553. for_jmp_addrs.push_back(opcodes.size());
  554. append(0); // Jump destination, will be patched.
  555. }
  556. void GDScriptByteCodeGenerator::write_endfor() {
  557. // Jump back to loop check.
  558. append(GDScriptFunction::OPCODE_JUMP, 0);
  559. append(continue_addrs.back()->get());
  560. continue_addrs.pop_back();
  561. // Patch end jumps (two of them).
  562. for (int i = 0; i < 2; i++) {
  563. patch_jump(for_jmp_addrs.back()->get());
  564. for_jmp_addrs.pop_back();
  565. }
  566. // Patch break statements.
  567. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  568. patch_jump(E->get());
  569. }
  570. current_breaks_to_patch.pop_back();
  571. // Remove loop temporaries.
  572. pop_temporary();
  573. pop_temporary();
  574. }
  575. void GDScriptByteCodeGenerator::start_while_condition() {
  576. current_breaks_to_patch.push_back(List<int>());
  577. continue_addrs.push_back(opcodes.size());
  578. }
  579. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  580. // Condition check.
  581. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  582. append(p_condition);
  583. while_jmp_addrs.push_back(opcodes.size());
  584. append(0); // End of loop address, will be patched.
  585. }
  586. void GDScriptByteCodeGenerator::write_endwhile() {
  587. // Jump back to loop check.
  588. append(GDScriptFunction::OPCODE_JUMP, 0);
  589. append(continue_addrs.back()->get());
  590. continue_addrs.pop_back();
  591. // Patch end jump.
  592. patch_jump(while_jmp_addrs.back()->get());
  593. while_jmp_addrs.pop_back();
  594. // Patch break statements.
  595. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  596. patch_jump(E->get());
  597. }
  598. current_breaks_to_patch.pop_back();
  599. }
  600. void GDScriptByteCodeGenerator::start_match() {
  601. match_continues_to_patch.push_back(List<int>());
  602. }
  603. void GDScriptByteCodeGenerator::start_match_branch() {
  604. // Patch continue statements.
  605. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  606. patch_jump(E->get());
  607. }
  608. match_continues_to_patch.pop_back();
  609. // Start a new list for next branch.
  610. match_continues_to_patch.push_back(List<int>());
  611. }
  612. void GDScriptByteCodeGenerator::end_match() {
  613. // Patch continue statements.
  614. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  615. patch_jump(E->get());
  616. }
  617. match_continues_to_patch.pop_back();
  618. }
  619. void GDScriptByteCodeGenerator::write_break() {
  620. append(GDScriptFunction::OPCODE_JUMP, 0);
  621. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  622. append(0);
  623. }
  624. void GDScriptByteCodeGenerator::write_continue() {
  625. append(GDScriptFunction::OPCODE_JUMP, 0);
  626. append(continue_addrs.back()->get());
  627. }
  628. void GDScriptByteCodeGenerator::write_continue_match() {
  629. append(GDScriptFunction::OPCODE_JUMP, 0);
  630. match_continues_to_patch.back()->get().push_back(opcodes.size());
  631. append(0);
  632. }
  633. void GDScriptByteCodeGenerator::write_breakpoint() {
  634. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  635. }
  636. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  637. append(GDScriptFunction::OPCODE_LINE, 0);
  638. append(p_line);
  639. current_line = p_line;
  640. }
  641. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  642. append(GDScriptFunction::OPCODE_RETURN, 1);
  643. append(p_return_value);
  644. }
  645. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  646. append(GDScriptFunction::OPCODE_ASSERT, 2);
  647. append(p_test);
  648. append(p_message);
  649. }
  650. void GDScriptByteCodeGenerator::start_block() {
  651. push_stack_identifiers();
  652. }
  653. void GDScriptByteCodeGenerator::end_block() {
  654. pop_stack_identifiers();
  655. }
  656. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  657. if (!ended && function != nullptr) {
  658. memdelete(function);
  659. }
  660. }