gdscript_byte_codegen.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. int idx = increase_stack();
  72. #ifdef DEBUG_ENABLED
  73. temp_stack.push_back(idx);
  74. #endif
  75. return idx;
  76. }
  77. void GDScriptByteCodeGenerator::pop_temporary() {
  78. ERR_FAIL_COND(current_temporaries == 0);
  79. current_stack_size--;
  80. #ifdef DEBUG_ENABLED
  81. if (temp_stack.back()->get() != current_stack_size) {
  82. ERR_PRINT("Mismatched popping of temporary value");
  83. }
  84. temp_stack.pop_back();
  85. #endif
  86. current_temporaries--;
  87. }
  88. void GDScriptByteCodeGenerator::start_parameters() {}
  89. void GDScriptByteCodeGenerator::end_parameters() {
  90. function->default_arguments.invert();
  91. }
  92. 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) {
  93. function = memnew(GDScriptFunction);
  94. debug_stack = EngineDebugger::is_active();
  95. function->name = p_function_name;
  96. function->_script = p_script;
  97. function->source = p_script->get_path();
  98. #ifdef DEBUG_ENABLED
  99. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  100. function->_func_cname = function->func_cname.get_data();
  101. #endif
  102. function->_static = p_static;
  103. function->return_type = p_return_type;
  104. function->rpc_mode = p_rpc_mode;
  105. function->_argument_count = 0;
  106. }
  107. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  108. #ifdef DEBUG_ENABLED
  109. if (current_temporaries != 0) {
  110. ERR_PRINT("Non-zero temporary variables at end of function: " + itos(current_temporaries));
  111. }
  112. #endif
  113. append(GDScriptFunction::OPCODE_END, 0);
  114. if (constant_map.size()) {
  115. function->_constant_count = constant_map.size();
  116. function->constants.resize(constant_map.size());
  117. function->_constants_ptr = function->constants.ptrw();
  118. const Variant *K = nullptr;
  119. while ((K = constant_map.next(K))) {
  120. int idx = constant_map[*K];
  121. function->constants.write[idx] = *K;
  122. }
  123. } else {
  124. function->_constants_ptr = nullptr;
  125. function->_constant_count = 0;
  126. }
  127. if (name_map.size()) {
  128. function->global_names.resize(name_map.size());
  129. function->_global_names_ptr = &function->global_names[0];
  130. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  131. function->global_names.write[E->get()] = E->key();
  132. }
  133. function->_global_names_count = function->global_names.size();
  134. } else {
  135. function->_global_names_ptr = nullptr;
  136. function->_global_names_count = 0;
  137. }
  138. if (opcodes.size()) {
  139. function->code = opcodes;
  140. function->_code_ptr = &function->code[0];
  141. function->_code_size = opcodes.size();
  142. } else {
  143. function->_code_ptr = nullptr;
  144. function->_code_size = 0;
  145. }
  146. if (function->default_arguments.size()) {
  147. function->_default_arg_count = function->default_arguments.size();
  148. function->_default_arg_ptr = &function->default_arguments[0];
  149. } else {
  150. function->_default_arg_count = 0;
  151. function->_default_arg_ptr = nullptr;
  152. }
  153. if (operator_func_map.size()) {
  154. function->operator_funcs.resize(operator_func_map.size());
  155. function->_operator_funcs_count = function->operator_funcs.size();
  156. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  157. for (const Map<Variant::ValidatedOperatorEvaluator, int>::Element *E = operator_func_map.front(); E; E = E->next()) {
  158. function->operator_funcs.write[E->get()] = E->key();
  159. }
  160. } else {
  161. function->_operator_funcs_count = 0;
  162. function->_operator_funcs_ptr = nullptr;
  163. }
  164. if (setters_map.size()) {
  165. function->setters.resize(setters_map.size());
  166. function->_setters_count = function->setters.size();
  167. function->_setters_ptr = function->setters.ptr();
  168. for (const Map<Variant::ValidatedSetter, int>::Element *E = setters_map.front(); E; E = E->next()) {
  169. function->setters.write[E->get()] = E->key();
  170. }
  171. } else {
  172. function->_setters_count = 0;
  173. function->_setters_ptr = nullptr;
  174. }
  175. if (getters_map.size()) {
  176. function->getters.resize(getters_map.size());
  177. function->_getters_count = function->getters.size();
  178. function->_getters_ptr = function->getters.ptr();
  179. for (const Map<Variant::ValidatedGetter, int>::Element *E = getters_map.front(); E; E = E->next()) {
  180. function->getters.write[E->get()] = E->key();
  181. }
  182. } else {
  183. function->_getters_count = 0;
  184. function->_getters_ptr = nullptr;
  185. }
  186. if (keyed_setters_map.size()) {
  187. function->keyed_setters.resize(keyed_setters_map.size());
  188. function->_keyed_setters_count = function->keyed_setters.size();
  189. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  190. for (const Map<Variant::ValidatedKeyedSetter, int>::Element *E = keyed_setters_map.front(); E; E = E->next()) {
  191. function->keyed_setters.write[E->get()] = E->key();
  192. }
  193. } else {
  194. function->_keyed_setters_count = 0;
  195. function->_keyed_setters_ptr = nullptr;
  196. }
  197. if (keyed_getters_map.size()) {
  198. function->keyed_getters.resize(keyed_getters_map.size());
  199. function->_keyed_getters_count = function->keyed_getters.size();
  200. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  201. for (const Map<Variant::ValidatedKeyedGetter, int>::Element *E = keyed_getters_map.front(); E; E = E->next()) {
  202. function->keyed_getters.write[E->get()] = E->key();
  203. }
  204. } else {
  205. function->_keyed_getters_count = 0;
  206. function->_keyed_getters_ptr = nullptr;
  207. }
  208. if (indexed_setters_map.size()) {
  209. function->indexed_setters.resize(indexed_setters_map.size());
  210. function->_indexed_setters_count = function->indexed_setters.size();
  211. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  212. for (const Map<Variant::ValidatedIndexedSetter, int>::Element *E = indexed_setters_map.front(); E; E = E->next()) {
  213. function->indexed_setters.write[E->get()] = E->key();
  214. }
  215. } else {
  216. function->_indexed_setters_count = 0;
  217. function->_indexed_setters_ptr = nullptr;
  218. }
  219. if (indexed_getters_map.size()) {
  220. function->indexed_getters.resize(indexed_getters_map.size());
  221. function->_indexed_getters_count = function->indexed_getters.size();
  222. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  223. for (const Map<Variant::ValidatedIndexedGetter, int>::Element *E = indexed_getters_map.front(); E; E = E->next()) {
  224. function->indexed_getters.write[E->get()] = E->key();
  225. }
  226. } else {
  227. function->_indexed_getters_count = 0;
  228. function->_indexed_getters_ptr = nullptr;
  229. }
  230. if (builtin_method_map.size()) {
  231. function->builtin_methods.resize(builtin_method_map.size());
  232. function->_builtin_methods_ptr = function->builtin_methods.ptr();
  233. function->_builtin_methods_count = builtin_method_map.size();
  234. for (const Map<Variant::ValidatedBuiltInMethod, int>::Element *E = builtin_method_map.front(); E; E = E->next()) {
  235. function->builtin_methods.write[E->get()] = E->key();
  236. }
  237. } else {
  238. function->_builtin_methods_ptr = nullptr;
  239. function->_builtin_methods_count = 0;
  240. }
  241. if (constructors_map.size()) {
  242. function->constructors.resize(constructors_map.size());
  243. function->_constructors_ptr = function->constructors.ptr();
  244. function->_constructors_count = constructors_map.size();
  245. for (const Map<Variant::ValidatedConstructor, int>::Element *E = constructors_map.front(); E; E = E->next()) {
  246. function->constructors.write[E->get()] = E->key();
  247. }
  248. } else {
  249. function->_constructors_ptr = nullptr;
  250. function->_constructors_count = 0;
  251. }
  252. if (method_bind_map.size()) {
  253. function->methods.resize(method_bind_map.size());
  254. function->_methods_ptr = function->methods.ptrw();
  255. function->_methods_count = method_bind_map.size();
  256. for (const Map<MethodBind *, int>::Element *E = method_bind_map.front(); E; E = E->next()) {
  257. function->methods.write[E->get()] = E->key();
  258. }
  259. } else {
  260. function->_methods_ptr = nullptr;
  261. function->_methods_count = 0;
  262. }
  263. if (debug_stack) {
  264. function->stack_debug = stack_debug;
  265. }
  266. function->_stack_size = stack_max;
  267. function->_instruction_args_size = instr_args_max;
  268. function->_ptrcall_args_size = ptrcall_max;
  269. ended = true;
  270. return function;
  271. }
  272. #ifdef DEBUG_ENABLED
  273. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  274. function->profile.signature = p_signature;
  275. }
  276. #endif
  277. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  278. function->_initial_line = p_line;
  279. }
  280. #define HAS_BUILTIN_TYPE(m_var) \
  281. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  282. #define IS_BUILTIN_TYPE(m_var, m_type) \
  283. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  284. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  285. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  286. // Gather specific operator.
  287. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  288. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  289. append(p_left_operand);
  290. append(Address());
  291. append(p_target);
  292. append(op_func);
  293. return;
  294. }
  295. // No specific types, perform variant evaluation.
  296. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  297. append(p_left_operand);
  298. append(Address());
  299. append(p_target);
  300. append(p_operator);
  301. }
  302. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  303. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  304. // Gather specific operator.
  305. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  306. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  307. append(p_left_operand);
  308. append(p_right_operand);
  309. append(p_target);
  310. append(op_func);
  311. return;
  312. }
  313. // No specific types, perform variant evaluation.
  314. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  315. append(p_left_operand);
  316. append(p_right_operand);
  317. append(p_target);
  318. append(p_operator);
  319. }
  320. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  321. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  322. append(p_source);
  323. append(p_type);
  324. append(p_target);
  325. }
  326. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  327. append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
  328. append(p_source);
  329. append(p_target);
  330. append(p_type);
  331. }
  332. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  333. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  334. append(p_left_operand);
  335. logic_op_jump_pos1.push_back(opcodes.size());
  336. append(0); // Jump target, will be patched.
  337. }
  338. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  339. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  340. append(p_right_operand);
  341. logic_op_jump_pos2.push_back(opcodes.size());
  342. append(0); // Jump target, will be patched.
  343. }
  344. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  345. // If here means both operands are true.
  346. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  347. append(p_target);
  348. // Jump away from the fail condition.
  349. append(GDScriptFunction::OPCODE_JUMP, 0);
  350. append(opcodes.size() + 3);
  351. // Here it means one of operands is false.
  352. patch_jump(logic_op_jump_pos1.back()->get());
  353. patch_jump(logic_op_jump_pos2.back()->get());
  354. logic_op_jump_pos1.pop_back();
  355. logic_op_jump_pos2.pop_back();
  356. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 0);
  357. append(p_target);
  358. }
  359. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  360. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  361. append(p_left_operand);
  362. logic_op_jump_pos1.push_back(opcodes.size());
  363. append(0); // Jump target, will be patched.
  364. }
  365. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  366. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  367. append(p_right_operand);
  368. logic_op_jump_pos2.push_back(opcodes.size());
  369. append(0); // Jump target, will be patched.
  370. }
  371. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  372. // If here means both operands are false.
  373. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  374. append(p_target);
  375. // Jump away from the success condition.
  376. append(GDScriptFunction::OPCODE_JUMP, 0);
  377. append(opcodes.size() + 3);
  378. // Here it means one of operands is false.
  379. patch_jump(logic_op_jump_pos1.back()->get());
  380. patch_jump(logic_op_jump_pos2.back()->get());
  381. logic_op_jump_pos1.pop_back();
  382. logic_op_jump_pos2.pop_back();
  383. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  384. append(p_target);
  385. }
  386. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  387. ternary_result.push_back(p_target);
  388. }
  389. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  390. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  391. append(p_condition);
  392. ternary_jump_fail_pos.push_back(opcodes.size());
  393. append(0); // Jump target, will be patched.
  394. }
  395. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  396. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  397. append(ternary_result.back()->get());
  398. append(p_expr);
  399. // Jump away from the false path.
  400. append(GDScriptFunction::OPCODE_JUMP, 0);
  401. ternary_jump_skip_pos.push_back(opcodes.size());
  402. append(0);
  403. // Fail must jump here.
  404. patch_jump(ternary_jump_fail_pos.back()->get());
  405. ternary_jump_fail_pos.pop_back();
  406. }
  407. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  408. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  409. append(ternary_result.back()->get());
  410. append(p_expr);
  411. }
  412. void GDScriptByteCodeGenerator::write_end_ternary() {
  413. patch_jump(ternary_jump_skip_pos.back()->get());
  414. ternary_jump_skip_pos.pop_back();
  415. }
  416. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  417. if (HAS_BUILTIN_TYPE(p_target)) {
  418. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type)) {
  419. // Use indexed setter instead.
  420. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  421. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  422. append(p_target);
  423. append(p_index);
  424. append(p_source);
  425. append(setter);
  426. return;
  427. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  428. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  429. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  430. append(p_target);
  431. append(p_index);
  432. append(p_source);
  433. append(setter);
  434. return;
  435. }
  436. }
  437. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  438. append(p_target);
  439. append(p_index);
  440. append(p_source);
  441. }
  442. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  443. if (HAS_BUILTIN_TYPE(p_source)) {
  444. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  445. // Use indexed getter instead.
  446. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  447. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  448. append(p_source);
  449. append(p_index);
  450. append(p_target);
  451. append(getter);
  452. return;
  453. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  454. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  455. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  456. append(p_source);
  457. append(p_index);
  458. append(p_target);
  459. append(getter);
  460. return;
  461. }
  462. }
  463. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  464. append(p_source);
  465. append(p_index);
  466. append(p_target);
  467. }
  468. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  469. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name)) {
  470. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  471. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  472. append(p_target);
  473. append(p_source);
  474. append(setter);
  475. return;
  476. }
  477. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  478. append(p_target);
  479. append(p_source);
  480. append(p_name);
  481. }
  482. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  483. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  484. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  485. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  486. append(p_source);
  487. append(p_target);
  488. append(getter);
  489. return;
  490. }
  491. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  492. append(p_source);
  493. append(p_target);
  494. append(p_name);
  495. }
  496. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  497. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  498. append(p_value);
  499. append(p_name);
  500. }
  501. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  502. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  503. append(p_target);
  504. append(p_name);
  505. }
  506. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  507. if (p_target.type.has_type && !p_source.type.has_type) {
  508. // Typed assignment.
  509. switch (p_target.type.kind) {
  510. case GDScriptDataType::BUILTIN: {
  511. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  512. append(p_target);
  513. append(p_source);
  514. append(p_target.type.builtin_type);
  515. } break;
  516. case GDScriptDataType::NATIVE: {
  517. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  518. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  519. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  520. append(p_target);
  521. append(p_source);
  522. append(class_idx);
  523. } break;
  524. case GDScriptDataType::SCRIPT:
  525. case GDScriptDataType::GDSCRIPT: {
  526. Variant script = p_target.type.script_type;
  527. int idx = get_constant_pos(script);
  528. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  529. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  530. append(p_target);
  531. append(p_source);
  532. append(idx);
  533. } break;
  534. default: {
  535. ERR_PRINT("Compiler bug: unresolved assign.");
  536. // Shouldn't get here, but fail-safe to a regular assignment
  537. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  538. append(p_target);
  539. append(p_source);
  540. }
  541. }
  542. } else {
  543. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  544. // Need conversion..
  545. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  546. append(p_target);
  547. append(p_source);
  548. append(p_target.type.builtin_type);
  549. } else {
  550. // Either untyped assignment or already type-checked by the parser
  551. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  552. append(p_target);
  553. append(p_source);
  554. }
  555. }
  556. }
  557. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  558. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  559. append(p_target);
  560. }
  561. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  562. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  563. append(p_target);
  564. }
  565. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  566. int index = 0;
  567. switch (p_type.kind) {
  568. case GDScriptDataType::BUILTIN: {
  569. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  570. index = p_type.builtin_type;
  571. } break;
  572. case GDScriptDataType::NATIVE: {
  573. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  574. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  575. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  576. index = class_idx;
  577. } break;
  578. case GDScriptDataType::SCRIPT:
  579. case GDScriptDataType::GDSCRIPT: {
  580. Variant script = p_type.script_type;
  581. int idx = get_constant_pos(script);
  582. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  583. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  584. index = idx;
  585. } break;
  586. default: {
  587. return;
  588. }
  589. }
  590. append(p_source);
  591. append(p_target);
  592. append(index);
  593. }
  594. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  595. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  596. for (int i = 0; i < p_arguments.size(); i++) {
  597. append(p_arguments[i]);
  598. }
  599. append(p_base);
  600. append(p_target);
  601. append(p_arguments.size());
  602. append(p_function_name);
  603. }
  604. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  605. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  606. for (int i = 0; i < p_arguments.size(); i++) {
  607. append(p_arguments[i]);
  608. }
  609. append(p_target);
  610. append(p_arguments.size());
  611. append(p_function_name);
  612. }
  613. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  614. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  615. for (int i = 0; i < p_arguments.size(); i++) {
  616. append(p_arguments[i]);
  617. }
  618. append(p_base);
  619. append(p_target);
  620. append(p_arguments.size());
  621. append(p_function_name);
  622. }
  623. void GDScriptByteCodeGenerator::write_call_builtin(const Address &p_target, GDScriptFunctions::Function p_function, const Vector<Address> &p_arguments) {
  624. append(GDScriptFunction::OPCODE_CALL_BUILT_IN, 1 + p_arguments.size());
  625. for (int i = 0; i < p_arguments.size(); i++) {
  626. append(p_arguments[i]);
  627. }
  628. append(p_target);
  629. append(p_arguments.size());
  630. append(p_function);
  631. }
  632. void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  633. bool is_validated = false;
  634. // Check if all types are correct.
  635. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  636. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  637. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  638. bool all_types_exact = true;
  639. for (int i = 0; i < p_arguments.size(); i++) {
  640. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  641. all_types_exact = false;
  642. break;
  643. }
  644. }
  645. is_validated = all_types_exact;
  646. }
  647. if (!is_validated) {
  648. // Perform regular call.
  649. write_call(p_target, p_base, p_method, p_arguments);
  650. return;
  651. }
  652. append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  653. for (int i = 0; i < p_arguments.size(); i++) {
  654. append(p_arguments[i]);
  655. }
  656. append(p_base);
  657. append(p_target);
  658. append(p_arguments.size());
  659. append(Variant::get_validated_builtin_method(p_type, p_method));
  660. }
  661. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  662. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  663. for (int i = 0; i < p_arguments.size(); i++) {
  664. append(p_arguments[i]);
  665. }
  666. append(p_base);
  667. append(p_target);
  668. append(p_arguments.size());
  669. append(p_method);
  670. }
  671. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  672. #define CASE_TYPE(m_type) \
  673. case Variant::m_type: \
  674. append(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  675. break
  676. bool is_ptrcall = true;
  677. if (p_method->has_return()) {
  678. MethodInfo info;
  679. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  680. switch (info.return_val.type) {
  681. CASE_TYPE(BOOL);
  682. CASE_TYPE(INT);
  683. CASE_TYPE(FLOAT);
  684. CASE_TYPE(STRING);
  685. CASE_TYPE(VECTOR2);
  686. CASE_TYPE(VECTOR2I);
  687. CASE_TYPE(RECT2);
  688. CASE_TYPE(RECT2I);
  689. CASE_TYPE(VECTOR3);
  690. CASE_TYPE(VECTOR3I);
  691. CASE_TYPE(TRANSFORM2D);
  692. CASE_TYPE(PLANE);
  693. CASE_TYPE(AABB);
  694. CASE_TYPE(BASIS);
  695. CASE_TYPE(TRANSFORM);
  696. CASE_TYPE(COLOR);
  697. CASE_TYPE(STRING_NAME);
  698. CASE_TYPE(NODE_PATH);
  699. CASE_TYPE(RID);
  700. CASE_TYPE(QUAT);
  701. CASE_TYPE(OBJECT);
  702. CASE_TYPE(CALLABLE);
  703. CASE_TYPE(SIGNAL);
  704. CASE_TYPE(DICTIONARY);
  705. CASE_TYPE(ARRAY);
  706. CASE_TYPE(PACKED_BYTE_ARRAY);
  707. CASE_TYPE(PACKED_INT32_ARRAY);
  708. CASE_TYPE(PACKED_INT64_ARRAY);
  709. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  710. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  711. CASE_TYPE(PACKED_STRING_ARRAY);
  712. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  713. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  714. CASE_TYPE(PACKED_COLOR_ARRAY);
  715. default:
  716. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  717. is_ptrcall = false;
  718. break;
  719. }
  720. } else {
  721. append(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  722. }
  723. for (int i = 0; i < p_arguments.size(); i++) {
  724. append(p_arguments[i]);
  725. }
  726. append(p_base);
  727. append(p_target);
  728. append(p_arguments.size());
  729. append(p_method);
  730. if (is_ptrcall) {
  731. alloc_ptrcall(p_arguments.size());
  732. }
  733. #undef CASE_TYPE
  734. }
  735. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  736. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  737. for (int i = 0; i < p_arguments.size(); i++) {
  738. append(p_arguments[i]);
  739. }
  740. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  741. append(p_target);
  742. append(p_arguments.size());
  743. append(p_function_name);
  744. }
  745. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  746. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  747. for (int i = 0; i < p_arguments.size(); i++) {
  748. append(p_arguments[i]);
  749. }
  750. append(p_base);
  751. append(p_target);
  752. append(p_arguments.size());
  753. append(p_function_name);
  754. }
  755. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  756. // Try to find an appropriate constructor.
  757. bool all_have_type = true;
  758. Vector<Variant::Type> arg_types;
  759. for (int i = 0; i < p_arguments.size(); i++) {
  760. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  761. all_have_type = false;
  762. break;
  763. }
  764. arg_types.push_back(p_arguments[i].type.builtin_type);
  765. }
  766. if (all_have_type) {
  767. int valid_constructor = -1;
  768. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  769. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  770. continue;
  771. }
  772. int types_correct = true;
  773. for (int j = 0; j < arg_types.size(); j++) {
  774. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  775. types_correct = false;
  776. break;
  777. }
  778. }
  779. if (types_correct) {
  780. valid_constructor = i;
  781. break;
  782. }
  783. }
  784. if (valid_constructor >= 0) {
  785. append(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  786. for (int i = 0; i < p_arguments.size(); i++) {
  787. append(p_arguments[i]);
  788. }
  789. append(p_target);
  790. append(p_arguments.size());
  791. append(Variant::get_validated_constructor(p_type, valid_constructor));
  792. return;
  793. }
  794. }
  795. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  796. for (int i = 0; i < p_arguments.size(); i++) {
  797. append(p_arguments[i]);
  798. }
  799. append(p_target);
  800. append(p_arguments.size());
  801. append(p_type);
  802. }
  803. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  804. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  805. for (int i = 0; i < p_arguments.size(); i++) {
  806. append(p_arguments[i]);
  807. }
  808. append(p_target);
  809. append(p_arguments.size());
  810. }
  811. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  812. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  813. for (int i = 0; i < p_arguments.size(); i++) {
  814. append(p_arguments[i]);
  815. }
  816. append(p_target);
  817. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  818. }
  819. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  820. append(GDScriptFunction::OPCODE_AWAIT, 1);
  821. append(p_operand);
  822. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  823. append(p_target);
  824. }
  825. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  826. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  827. append(p_condition);
  828. if_jmp_addrs.push_back(opcodes.size());
  829. append(0); // Jump destination, will be patched.
  830. }
  831. void GDScriptByteCodeGenerator::write_else() {
  832. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  833. int else_jmp_addr = opcodes.size();
  834. append(0); // Jump destination, will be patched.
  835. patch_jump(if_jmp_addrs.back()->get());
  836. if_jmp_addrs.pop_back();
  837. if_jmp_addrs.push_back(else_jmp_addr);
  838. }
  839. void GDScriptByteCodeGenerator::write_endif() {
  840. patch_jump(if_jmp_addrs.back()->get());
  841. if_jmp_addrs.pop_back();
  842. }
  843. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  844. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  845. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  846. // Store state.
  847. for_counter_variables.push_back(counter);
  848. for_container_variables.push_back(container);
  849. }
  850. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  851. const Address &container = for_container_variables.back()->get();
  852. // Assign container.
  853. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  854. append(container);
  855. append(p_list);
  856. for_iterator_variables.push_back(p_variable);
  857. }
  858. void GDScriptByteCodeGenerator::write_for() {
  859. const Address &iterator = for_iterator_variables.back()->get();
  860. const Address &counter = for_counter_variables.back()->get();
  861. const Address &container = for_container_variables.back()->get();
  862. current_breaks_to_patch.push_back(List<int>());
  863. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  864. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  865. if (container.type.has_type) {
  866. if (container.type.kind == GDScriptDataType::BUILTIN) {
  867. switch (container.type.builtin_type) {
  868. case Variant::INT:
  869. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  870. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  871. break;
  872. case Variant::FLOAT:
  873. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  874. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  875. break;
  876. case Variant::VECTOR2:
  877. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  878. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  879. break;
  880. case Variant::VECTOR2I:
  881. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  882. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  883. break;
  884. case Variant::VECTOR3:
  885. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  886. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  887. break;
  888. case Variant::VECTOR3I:
  889. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  890. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  891. break;
  892. case Variant::STRING:
  893. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  894. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  895. break;
  896. case Variant::DICTIONARY:
  897. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  898. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  899. break;
  900. case Variant::ARRAY:
  901. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  902. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  903. break;
  904. case Variant::PACKED_BYTE_ARRAY:
  905. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  906. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  907. break;
  908. case Variant::PACKED_INT32_ARRAY:
  909. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  910. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  911. break;
  912. case Variant::PACKED_INT64_ARRAY:
  913. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  914. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  915. break;
  916. case Variant::PACKED_FLOAT32_ARRAY:
  917. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  918. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  919. break;
  920. case Variant::PACKED_FLOAT64_ARRAY:
  921. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  922. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  923. break;
  924. case Variant::PACKED_STRING_ARRAY:
  925. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  926. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  927. break;
  928. case Variant::PACKED_VECTOR2_ARRAY:
  929. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  930. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  931. break;
  932. case Variant::PACKED_VECTOR3_ARRAY:
  933. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  934. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  935. break;
  936. case Variant::PACKED_COLOR_ARRAY:
  937. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  938. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  939. break;
  940. default:
  941. break;
  942. }
  943. } else {
  944. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  945. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  946. }
  947. }
  948. // Begin loop.
  949. append(begin_opcode, 3);
  950. append(counter);
  951. append(container);
  952. append(iterator);
  953. for_jmp_addrs.push_back(opcodes.size());
  954. append(0); // End of loop address, will be patched.
  955. append(GDScriptFunction::OPCODE_JUMP, 0);
  956. append(opcodes.size() + 6); // Skip over 'continue' code.
  957. // Next iteration.
  958. int continue_addr = opcodes.size();
  959. continue_addrs.push_back(continue_addr);
  960. append(iterate_opcode, 3);
  961. append(counter);
  962. append(container);
  963. append(iterator);
  964. for_jmp_addrs.push_back(opcodes.size());
  965. append(0); // Jump destination, will be patched.
  966. }
  967. void GDScriptByteCodeGenerator::write_endfor() {
  968. // Jump back to loop check.
  969. append(GDScriptFunction::OPCODE_JUMP, 0);
  970. append(continue_addrs.back()->get());
  971. continue_addrs.pop_back();
  972. // Patch end jumps (two of them).
  973. for (int i = 0; i < 2; i++) {
  974. patch_jump(for_jmp_addrs.back()->get());
  975. for_jmp_addrs.pop_back();
  976. }
  977. // Patch break statements.
  978. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  979. patch_jump(E->get());
  980. }
  981. current_breaks_to_patch.pop_back();
  982. // Pop state.
  983. for_iterator_variables.pop_back();
  984. for_counter_variables.pop_back();
  985. for_container_variables.pop_back();
  986. }
  987. void GDScriptByteCodeGenerator::start_while_condition() {
  988. current_breaks_to_patch.push_back(List<int>());
  989. continue_addrs.push_back(opcodes.size());
  990. }
  991. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  992. // Condition check.
  993. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  994. append(p_condition);
  995. while_jmp_addrs.push_back(opcodes.size());
  996. append(0); // End of loop address, will be patched.
  997. }
  998. void GDScriptByteCodeGenerator::write_endwhile() {
  999. // Jump back to loop check.
  1000. append(GDScriptFunction::OPCODE_JUMP, 0);
  1001. append(continue_addrs.back()->get());
  1002. continue_addrs.pop_back();
  1003. // Patch end jump.
  1004. patch_jump(while_jmp_addrs.back()->get());
  1005. while_jmp_addrs.pop_back();
  1006. // Patch break statements.
  1007. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1008. patch_jump(E->get());
  1009. }
  1010. current_breaks_to_patch.pop_back();
  1011. }
  1012. void GDScriptByteCodeGenerator::start_match() {
  1013. match_continues_to_patch.push_back(List<int>());
  1014. }
  1015. void GDScriptByteCodeGenerator::start_match_branch() {
  1016. // Patch continue statements.
  1017. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1018. patch_jump(E->get());
  1019. }
  1020. match_continues_to_patch.pop_back();
  1021. // Start a new list for next branch.
  1022. match_continues_to_patch.push_back(List<int>());
  1023. }
  1024. void GDScriptByteCodeGenerator::end_match() {
  1025. // Patch continue statements.
  1026. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1027. patch_jump(E->get());
  1028. }
  1029. match_continues_to_patch.pop_back();
  1030. }
  1031. void GDScriptByteCodeGenerator::write_break() {
  1032. append(GDScriptFunction::OPCODE_JUMP, 0);
  1033. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1034. append(0);
  1035. }
  1036. void GDScriptByteCodeGenerator::write_continue() {
  1037. append(GDScriptFunction::OPCODE_JUMP, 0);
  1038. append(continue_addrs.back()->get());
  1039. }
  1040. void GDScriptByteCodeGenerator::write_continue_match() {
  1041. append(GDScriptFunction::OPCODE_JUMP, 0);
  1042. match_continues_to_patch.back()->get().push_back(opcodes.size());
  1043. append(0);
  1044. }
  1045. void GDScriptByteCodeGenerator::write_breakpoint() {
  1046. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  1047. }
  1048. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1049. append(GDScriptFunction::OPCODE_LINE, 0);
  1050. append(p_line);
  1051. current_line = p_line;
  1052. }
  1053. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1054. append(GDScriptFunction::OPCODE_RETURN, 1);
  1055. append(p_return_value);
  1056. }
  1057. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1058. append(GDScriptFunction::OPCODE_ASSERT, 2);
  1059. append(p_test);
  1060. append(p_message);
  1061. }
  1062. void GDScriptByteCodeGenerator::start_block() {
  1063. push_stack_identifiers();
  1064. }
  1065. void GDScriptByteCodeGenerator::end_block() {
  1066. pop_stack_identifiers();
  1067. }
  1068. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1069. if (!ended && function != nullptr) {
  1070. memdelete(function);
  1071. }
  1072. }