gdscript_byte_codegen.cpp 25 KB

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