gdscript_byte_codegen.cpp 40 KB

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