gdscript_byte_codegen.cpp 42 KB

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