gdscript_byte_codegen.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 (utilities_map.size()) {
  254. function->utilities.resize(utilities_map.size());
  255. function->_utilities_ptr = function->utilities.ptr();
  256. function->_utilities_count = utilities_map.size();
  257. for (const Map<Variant::ValidatedUtilityFunction, int>::Element *E = utilities_map.front(); E; E = E->next()) {
  258. function->utilities.write[E->get()] = E->key();
  259. }
  260. } else {
  261. function->_utilities_ptr = nullptr;
  262. function->_utilities_count = 0;
  263. }
  264. if (gds_utilities_map.size()) {
  265. function->gds_utilities.resize(gds_utilities_map.size());
  266. function->_gds_utilities_ptr = function->gds_utilities.ptr();
  267. function->_gds_utilities_count = gds_utilities_map.size();
  268. for (const Map<GDScriptUtilityFunctions::FunctionPtr, int>::Element *E = gds_utilities_map.front(); E; E = E->next()) {
  269. function->gds_utilities.write[E->get()] = E->key();
  270. }
  271. } else {
  272. function->_gds_utilities_ptr = nullptr;
  273. function->_gds_utilities_count = 0;
  274. }
  275. if (method_bind_map.size()) {
  276. function->methods.resize(method_bind_map.size());
  277. function->_methods_ptr = function->methods.ptrw();
  278. function->_methods_count = method_bind_map.size();
  279. for (const Map<MethodBind *, int>::Element *E = method_bind_map.front(); E; E = E->next()) {
  280. function->methods.write[E->get()] = E->key();
  281. }
  282. } else {
  283. function->_methods_ptr = nullptr;
  284. function->_methods_count = 0;
  285. }
  286. if (debug_stack) {
  287. function->stack_debug = stack_debug;
  288. }
  289. function->_stack_size = stack_max;
  290. function->_instruction_args_size = instr_args_max;
  291. function->_ptrcall_args_size = ptrcall_max;
  292. ended = true;
  293. return function;
  294. }
  295. #ifdef DEBUG_ENABLED
  296. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  297. function->profile.signature = p_signature;
  298. }
  299. #endif
  300. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  301. function->_initial_line = p_line;
  302. }
  303. #define HAS_BUILTIN_TYPE(m_var) \
  304. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  305. #define IS_BUILTIN_TYPE(m_var, m_type) \
  306. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  307. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  308. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  309. // Gather specific operator.
  310. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  311. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  312. append(p_left_operand);
  313. append(Address());
  314. append(p_target);
  315. append(op_func);
  316. return;
  317. }
  318. // No specific types, perform variant evaluation.
  319. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  320. append(p_left_operand);
  321. append(Address());
  322. append(p_target);
  323. append(p_operator);
  324. }
  325. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  326. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  327. // Gather specific operator.
  328. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  329. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  330. append(p_left_operand);
  331. append(p_right_operand);
  332. append(p_target);
  333. append(op_func);
  334. return;
  335. }
  336. // No specific types, perform variant evaluation.
  337. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  338. append(p_left_operand);
  339. append(p_right_operand);
  340. append(p_target);
  341. append(p_operator);
  342. }
  343. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  344. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  345. append(p_source);
  346. append(p_type);
  347. append(p_target);
  348. }
  349. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  350. append(GDScriptFunction::OPCODE_IS_BUILTIN, 3);
  351. append(p_source);
  352. append(p_target);
  353. append(p_type);
  354. }
  355. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  356. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  357. append(p_left_operand);
  358. logic_op_jump_pos1.push_back(opcodes.size());
  359. append(0); // Jump target, will be patched.
  360. }
  361. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  362. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  363. append(p_right_operand);
  364. logic_op_jump_pos2.push_back(opcodes.size());
  365. append(0); // Jump target, will be patched.
  366. }
  367. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  368. // If here means both operands are true.
  369. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  370. append(p_target);
  371. // Jump away from the fail condition.
  372. append(GDScriptFunction::OPCODE_JUMP, 0);
  373. append(opcodes.size() + 3);
  374. // Here it means one of operands is false.
  375. patch_jump(logic_op_jump_pos1.back()->get());
  376. patch_jump(logic_op_jump_pos2.back()->get());
  377. logic_op_jump_pos1.pop_back();
  378. logic_op_jump_pos2.pop_back();
  379. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  380. append(p_target);
  381. }
  382. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  383. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  384. append(p_left_operand);
  385. logic_op_jump_pos1.push_back(opcodes.size());
  386. append(0); // Jump target, will be patched.
  387. }
  388. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  389. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  390. append(p_right_operand);
  391. logic_op_jump_pos2.push_back(opcodes.size());
  392. append(0); // Jump target, will be patched.
  393. }
  394. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  395. // If here means both operands are false.
  396. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  397. append(p_target);
  398. // Jump away from the success condition.
  399. append(GDScriptFunction::OPCODE_JUMP, 0);
  400. append(opcodes.size() + 3);
  401. // Here it means one of operands is true.
  402. patch_jump(logic_op_jump_pos1.back()->get());
  403. patch_jump(logic_op_jump_pos2.back()->get());
  404. logic_op_jump_pos1.pop_back();
  405. logic_op_jump_pos2.pop_back();
  406. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  407. append(p_target);
  408. }
  409. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  410. ternary_result.push_back(p_target);
  411. }
  412. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  413. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  414. append(p_condition);
  415. ternary_jump_fail_pos.push_back(opcodes.size());
  416. append(0); // Jump target, will be patched.
  417. }
  418. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  419. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  420. append(ternary_result.back()->get());
  421. append(p_expr);
  422. // Jump away from the false path.
  423. append(GDScriptFunction::OPCODE_JUMP, 0);
  424. ternary_jump_skip_pos.push_back(opcodes.size());
  425. append(0);
  426. // Fail must jump here.
  427. patch_jump(ternary_jump_fail_pos.back()->get());
  428. ternary_jump_fail_pos.pop_back();
  429. }
  430. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  431. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  432. append(ternary_result.back()->get());
  433. append(p_expr);
  434. }
  435. void GDScriptByteCodeGenerator::write_end_ternary() {
  436. patch_jump(ternary_jump_skip_pos.back()->get());
  437. ternary_jump_skip_pos.pop_back();
  438. }
  439. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  440. if (HAS_BUILTIN_TYPE(p_target)) {
  441. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type)) {
  442. // Use indexed setter instead.
  443. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  444. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  445. append(p_target);
  446. append(p_index);
  447. append(p_source);
  448. append(setter);
  449. return;
  450. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  451. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  452. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  453. append(p_target);
  454. append(p_index);
  455. append(p_source);
  456. append(setter);
  457. return;
  458. }
  459. }
  460. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  461. append(p_target);
  462. append(p_index);
  463. append(p_source);
  464. }
  465. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  466. if (HAS_BUILTIN_TYPE(p_source)) {
  467. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  468. // Use indexed getter instead.
  469. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  470. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  471. append(p_source);
  472. append(p_index);
  473. append(p_target);
  474. append(getter);
  475. return;
  476. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  477. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  478. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  479. append(p_source);
  480. append(p_index);
  481. append(p_target);
  482. append(getter);
  483. return;
  484. }
  485. }
  486. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  487. append(p_source);
  488. append(p_index);
  489. append(p_target);
  490. }
  491. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  492. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name)) {
  493. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  494. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  495. append(p_target);
  496. append(p_source);
  497. append(setter);
  498. return;
  499. }
  500. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  501. append(p_target);
  502. append(p_source);
  503. append(p_name);
  504. }
  505. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  506. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  507. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  508. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  509. append(p_source);
  510. append(p_target);
  511. append(getter);
  512. return;
  513. }
  514. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  515. append(p_source);
  516. append(p_target);
  517. append(p_name);
  518. }
  519. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  520. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  521. append(p_value);
  522. append(p_name);
  523. }
  524. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  525. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  526. append(p_target);
  527. append(p_name);
  528. }
  529. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  530. if (p_target.type.has_type && !p_source.type.has_type) {
  531. // Typed assignment.
  532. switch (p_target.type.kind) {
  533. case GDScriptDataType::BUILTIN: {
  534. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  535. append(p_target);
  536. append(p_source);
  537. append(p_target.type.builtin_type);
  538. } break;
  539. case GDScriptDataType::NATIVE: {
  540. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  541. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  542. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  543. append(p_target);
  544. append(p_source);
  545. append(class_idx);
  546. } break;
  547. case GDScriptDataType::SCRIPT:
  548. case GDScriptDataType::GDSCRIPT: {
  549. Variant script = p_target.type.script_type;
  550. int idx = get_constant_pos(script);
  551. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  552. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  553. append(p_target);
  554. append(p_source);
  555. append(idx);
  556. } break;
  557. default: {
  558. ERR_PRINT("Compiler bug: unresolved assign.");
  559. // Shouldn't get here, but fail-safe to a regular assignment
  560. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  561. append(p_target);
  562. append(p_source);
  563. }
  564. }
  565. } else {
  566. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  567. // Need conversion..
  568. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  569. append(p_target);
  570. append(p_source);
  571. append(p_target.type.builtin_type);
  572. } else {
  573. // Either untyped assignment or already type-checked by the parser
  574. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  575. append(p_target);
  576. append(p_source);
  577. }
  578. }
  579. }
  580. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  581. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  582. append(p_target);
  583. }
  584. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  585. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  586. append(p_target);
  587. }
  588. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src) {
  589. write_assign(p_dst, p_src);
  590. function->default_arguments.push_back(opcodes.size());
  591. }
  592. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  593. int index = 0;
  594. switch (p_type.kind) {
  595. case GDScriptDataType::BUILTIN: {
  596. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  597. index = p_type.builtin_type;
  598. } break;
  599. case GDScriptDataType::NATIVE: {
  600. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  601. class_idx |= (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS);
  602. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  603. index = class_idx;
  604. } break;
  605. case GDScriptDataType::SCRIPT:
  606. case GDScriptDataType::GDSCRIPT: {
  607. Variant script = p_type.script_type;
  608. int idx = get_constant_pos(script);
  609. idx |= (GDScriptFunction::ADDR_TYPE_LOCAL_CONSTANT << GDScriptFunction::ADDR_BITS);
  610. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  611. index = idx;
  612. } break;
  613. default: {
  614. return;
  615. }
  616. }
  617. append(p_source);
  618. append(p_target);
  619. append(index);
  620. }
  621. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  622. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  623. for (int i = 0; i < p_arguments.size(); i++) {
  624. append(p_arguments[i]);
  625. }
  626. append(p_base);
  627. append(p_target);
  628. append(p_arguments.size());
  629. append(p_function_name);
  630. }
  631. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  632. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  633. for (int i = 0; i < p_arguments.size(); i++) {
  634. append(p_arguments[i]);
  635. }
  636. append(p_target);
  637. append(p_arguments.size());
  638. append(p_function_name);
  639. }
  640. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  641. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  642. for (int i = 0; i < p_arguments.size(); i++) {
  643. append(p_arguments[i]);
  644. }
  645. append(p_base);
  646. append(p_target);
  647. append(p_arguments.size());
  648. append(p_function_name);
  649. }
  650. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, GDScriptUtilityFunctions::FunctionPtr p_function, const Vector<Address> &p_arguments) {
  651. append(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  652. for (int i = 0; i < p_arguments.size(); i++) {
  653. append(p_arguments[i]);
  654. }
  655. append(p_target);
  656. append(p_arguments.size());
  657. append(p_function);
  658. }
  659. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  660. bool is_validated = true;
  661. if (Variant::is_utility_function_vararg(p_function)) {
  662. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  663. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  664. bool all_types_exact = true;
  665. for (int i = 0; i < p_arguments.size(); i++) {
  666. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  667. all_types_exact = false;
  668. break;
  669. }
  670. }
  671. is_validated = all_types_exact;
  672. }
  673. if (is_validated) {
  674. append(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  675. for (int i = 0; i < p_arguments.size(); i++) {
  676. append(p_arguments[i]);
  677. }
  678. append(p_target);
  679. append(p_arguments.size());
  680. append(Variant::get_validated_utility_function(p_function));
  681. } else {
  682. append(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  683. for (int i = 0; i < p_arguments.size(); i++) {
  684. append(p_arguments[i]);
  685. }
  686. append(p_target);
  687. append(p_arguments.size());
  688. append(p_function);
  689. }
  690. }
  691. 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) {
  692. bool is_validated = false;
  693. // Check if all types are correct.
  694. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  695. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  696. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  697. bool all_types_exact = true;
  698. for (int i = 0; i < p_arguments.size(); i++) {
  699. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  700. all_types_exact = false;
  701. break;
  702. }
  703. }
  704. is_validated = all_types_exact;
  705. }
  706. if (!is_validated) {
  707. // Perform regular call.
  708. write_call(p_target, p_base, p_method, p_arguments);
  709. return;
  710. }
  711. append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  712. for (int i = 0; i < p_arguments.size(); i++) {
  713. append(p_arguments[i]);
  714. }
  715. append(p_base);
  716. append(p_target);
  717. append(p_arguments.size());
  718. append(Variant::get_validated_builtin_method(p_type, p_method));
  719. }
  720. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  721. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  722. for (int i = 0; i < p_arguments.size(); i++) {
  723. append(p_arguments[i]);
  724. }
  725. append(p_base);
  726. append(p_target);
  727. append(p_arguments.size());
  728. append(p_method);
  729. }
  730. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  731. #define CASE_TYPE(m_type) \
  732. case Variant::m_type: \
  733. append(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  734. break
  735. bool is_ptrcall = true;
  736. if (p_method->has_return()) {
  737. MethodInfo info;
  738. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  739. switch (info.return_val.type) {
  740. CASE_TYPE(BOOL);
  741. CASE_TYPE(INT);
  742. CASE_TYPE(FLOAT);
  743. CASE_TYPE(STRING);
  744. CASE_TYPE(VECTOR2);
  745. CASE_TYPE(VECTOR2I);
  746. CASE_TYPE(RECT2);
  747. CASE_TYPE(RECT2I);
  748. CASE_TYPE(VECTOR3);
  749. CASE_TYPE(VECTOR3I);
  750. CASE_TYPE(TRANSFORM2D);
  751. CASE_TYPE(PLANE);
  752. CASE_TYPE(AABB);
  753. CASE_TYPE(BASIS);
  754. CASE_TYPE(TRANSFORM);
  755. CASE_TYPE(COLOR);
  756. CASE_TYPE(STRING_NAME);
  757. CASE_TYPE(NODE_PATH);
  758. CASE_TYPE(RID);
  759. CASE_TYPE(QUAT);
  760. CASE_TYPE(OBJECT);
  761. CASE_TYPE(CALLABLE);
  762. CASE_TYPE(SIGNAL);
  763. CASE_TYPE(DICTIONARY);
  764. CASE_TYPE(ARRAY);
  765. CASE_TYPE(PACKED_BYTE_ARRAY);
  766. CASE_TYPE(PACKED_INT32_ARRAY);
  767. CASE_TYPE(PACKED_INT64_ARRAY);
  768. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  769. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  770. CASE_TYPE(PACKED_STRING_ARRAY);
  771. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  772. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  773. CASE_TYPE(PACKED_COLOR_ARRAY);
  774. default:
  775. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  776. is_ptrcall = false;
  777. break;
  778. }
  779. } else {
  780. append(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  781. }
  782. for (int i = 0; i < p_arguments.size(); i++) {
  783. append(p_arguments[i]);
  784. }
  785. append(p_base);
  786. append(p_target);
  787. append(p_arguments.size());
  788. append(p_method);
  789. if (is_ptrcall) {
  790. alloc_ptrcall(p_arguments.size());
  791. }
  792. #undef CASE_TYPE
  793. }
  794. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  795. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  796. for (int i = 0; i < p_arguments.size(); i++) {
  797. append(p_arguments[i]);
  798. }
  799. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  800. append(p_target);
  801. append(p_arguments.size());
  802. append(p_function_name);
  803. }
  804. void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  805. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  806. for (int i = 0; i < p_arguments.size(); i++) {
  807. append(p_arguments[i]);
  808. }
  809. append(GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
  810. append(p_target);
  811. append(p_arguments.size());
  812. append(p_function_name);
  813. }
  814. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  815. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  816. for (int i = 0; i < p_arguments.size(); i++) {
  817. append(p_arguments[i]);
  818. }
  819. append(p_base);
  820. append(p_target);
  821. append(p_arguments.size());
  822. append(p_function_name);
  823. }
  824. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  825. // Try to find an appropriate constructor.
  826. bool all_have_type = true;
  827. Vector<Variant::Type> arg_types;
  828. for (int i = 0; i < p_arguments.size(); i++) {
  829. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  830. all_have_type = false;
  831. break;
  832. }
  833. arg_types.push_back(p_arguments[i].type.builtin_type);
  834. }
  835. if (all_have_type) {
  836. int valid_constructor = -1;
  837. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  838. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  839. continue;
  840. }
  841. int types_correct = true;
  842. for (int j = 0; j < arg_types.size(); j++) {
  843. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  844. types_correct = false;
  845. break;
  846. }
  847. }
  848. if (types_correct) {
  849. valid_constructor = i;
  850. break;
  851. }
  852. }
  853. if (valid_constructor >= 0) {
  854. append(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  855. for (int i = 0; i < p_arguments.size(); i++) {
  856. append(p_arguments[i]);
  857. }
  858. append(p_target);
  859. append(p_arguments.size());
  860. append(Variant::get_validated_constructor(p_type, valid_constructor));
  861. return;
  862. }
  863. }
  864. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  865. for (int i = 0; i < p_arguments.size(); i++) {
  866. append(p_arguments[i]);
  867. }
  868. append(p_target);
  869. append(p_arguments.size());
  870. append(p_type);
  871. }
  872. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  873. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  874. for (int i = 0; i < p_arguments.size(); i++) {
  875. append(p_arguments[i]);
  876. }
  877. append(p_target);
  878. append(p_arguments.size());
  879. }
  880. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  881. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  882. for (int i = 0; i < p_arguments.size(); i++) {
  883. append(p_arguments[i]);
  884. }
  885. append(p_target);
  886. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  887. }
  888. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  889. append(GDScriptFunction::OPCODE_AWAIT, 1);
  890. append(p_operand);
  891. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  892. append(p_target);
  893. }
  894. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  895. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  896. append(p_condition);
  897. if_jmp_addrs.push_back(opcodes.size());
  898. append(0); // Jump destination, will be patched.
  899. }
  900. void GDScriptByteCodeGenerator::write_else() {
  901. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  902. int else_jmp_addr = opcodes.size();
  903. append(0); // Jump destination, will be patched.
  904. patch_jump(if_jmp_addrs.back()->get());
  905. if_jmp_addrs.pop_back();
  906. if_jmp_addrs.push_back(else_jmp_addr);
  907. }
  908. void GDScriptByteCodeGenerator::write_endif() {
  909. patch_jump(if_jmp_addrs.back()->get());
  910. if_jmp_addrs.pop_back();
  911. }
  912. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  913. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  914. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  915. // Store state.
  916. for_counter_variables.push_back(counter);
  917. for_container_variables.push_back(container);
  918. }
  919. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  920. const Address &container = for_container_variables.back()->get();
  921. // Assign container.
  922. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  923. append(container);
  924. append(p_list);
  925. for_iterator_variables.push_back(p_variable);
  926. }
  927. void GDScriptByteCodeGenerator::write_for() {
  928. const Address &iterator = for_iterator_variables.back()->get();
  929. const Address &counter = for_counter_variables.back()->get();
  930. const Address &container = for_container_variables.back()->get();
  931. current_breaks_to_patch.push_back(List<int>());
  932. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  933. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  934. if (container.type.has_type) {
  935. if (container.type.kind == GDScriptDataType::BUILTIN) {
  936. switch (container.type.builtin_type) {
  937. case Variant::INT:
  938. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  939. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  940. break;
  941. case Variant::FLOAT:
  942. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  943. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  944. break;
  945. case Variant::VECTOR2:
  946. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  947. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  948. break;
  949. case Variant::VECTOR2I:
  950. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  951. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  952. break;
  953. case Variant::VECTOR3:
  954. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  955. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  956. break;
  957. case Variant::VECTOR3I:
  958. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  959. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  960. break;
  961. case Variant::STRING:
  962. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  963. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  964. break;
  965. case Variant::DICTIONARY:
  966. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  967. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  968. break;
  969. case Variant::ARRAY:
  970. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  971. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  972. break;
  973. case Variant::PACKED_BYTE_ARRAY:
  974. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  975. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  976. break;
  977. case Variant::PACKED_INT32_ARRAY:
  978. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  979. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  980. break;
  981. case Variant::PACKED_INT64_ARRAY:
  982. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  983. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  984. break;
  985. case Variant::PACKED_FLOAT32_ARRAY:
  986. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  987. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  988. break;
  989. case Variant::PACKED_FLOAT64_ARRAY:
  990. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  991. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  992. break;
  993. case Variant::PACKED_STRING_ARRAY:
  994. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  995. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  996. break;
  997. case Variant::PACKED_VECTOR2_ARRAY:
  998. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  999. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1000. break;
  1001. case Variant::PACKED_VECTOR3_ARRAY:
  1002. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1003. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1004. break;
  1005. case Variant::PACKED_COLOR_ARRAY:
  1006. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1007. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1008. break;
  1009. default:
  1010. break;
  1011. }
  1012. } else {
  1013. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1014. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1015. }
  1016. }
  1017. // Begin loop.
  1018. append(begin_opcode, 3);
  1019. append(counter);
  1020. append(container);
  1021. append(iterator);
  1022. for_jmp_addrs.push_back(opcodes.size());
  1023. append(0); // End of loop address, will be patched.
  1024. append(GDScriptFunction::OPCODE_JUMP, 0);
  1025. append(opcodes.size() + 6); // Skip over 'continue' code.
  1026. // Next iteration.
  1027. int continue_addr = opcodes.size();
  1028. continue_addrs.push_back(continue_addr);
  1029. append(iterate_opcode, 3);
  1030. append(counter);
  1031. append(container);
  1032. append(iterator);
  1033. for_jmp_addrs.push_back(opcodes.size());
  1034. append(0); // Jump destination, will be patched.
  1035. }
  1036. void GDScriptByteCodeGenerator::write_endfor() {
  1037. // Jump back to loop check.
  1038. append(GDScriptFunction::OPCODE_JUMP, 0);
  1039. append(continue_addrs.back()->get());
  1040. continue_addrs.pop_back();
  1041. // Patch end jumps (two of them).
  1042. for (int i = 0; i < 2; i++) {
  1043. patch_jump(for_jmp_addrs.back()->get());
  1044. for_jmp_addrs.pop_back();
  1045. }
  1046. // Patch break statements.
  1047. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1048. patch_jump(E->get());
  1049. }
  1050. current_breaks_to_patch.pop_back();
  1051. // Pop state.
  1052. for_iterator_variables.pop_back();
  1053. for_counter_variables.pop_back();
  1054. for_container_variables.pop_back();
  1055. }
  1056. void GDScriptByteCodeGenerator::start_while_condition() {
  1057. current_breaks_to_patch.push_back(List<int>());
  1058. continue_addrs.push_back(opcodes.size());
  1059. }
  1060. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1061. // Condition check.
  1062. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  1063. append(p_condition);
  1064. while_jmp_addrs.push_back(opcodes.size());
  1065. append(0); // End of loop address, will be patched.
  1066. }
  1067. void GDScriptByteCodeGenerator::write_endwhile() {
  1068. // Jump back to loop check.
  1069. append(GDScriptFunction::OPCODE_JUMP, 0);
  1070. append(continue_addrs.back()->get());
  1071. continue_addrs.pop_back();
  1072. // Patch end jump.
  1073. patch_jump(while_jmp_addrs.back()->get());
  1074. while_jmp_addrs.pop_back();
  1075. // Patch break statements.
  1076. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1077. patch_jump(E->get());
  1078. }
  1079. current_breaks_to_patch.pop_back();
  1080. }
  1081. void GDScriptByteCodeGenerator::start_match() {
  1082. match_continues_to_patch.push_back(List<int>());
  1083. }
  1084. void GDScriptByteCodeGenerator::start_match_branch() {
  1085. // Patch continue statements.
  1086. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1087. patch_jump(E->get());
  1088. }
  1089. match_continues_to_patch.pop_back();
  1090. // Start a new list for next branch.
  1091. match_continues_to_patch.push_back(List<int>());
  1092. }
  1093. void GDScriptByteCodeGenerator::end_match() {
  1094. // Patch continue statements.
  1095. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1096. patch_jump(E->get());
  1097. }
  1098. match_continues_to_patch.pop_back();
  1099. }
  1100. void GDScriptByteCodeGenerator::write_break() {
  1101. append(GDScriptFunction::OPCODE_JUMP, 0);
  1102. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1103. append(0);
  1104. }
  1105. void GDScriptByteCodeGenerator::write_continue() {
  1106. append(GDScriptFunction::OPCODE_JUMP, 0);
  1107. append(continue_addrs.back()->get());
  1108. }
  1109. void GDScriptByteCodeGenerator::write_continue_match() {
  1110. append(GDScriptFunction::OPCODE_JUMP, 0);
  1111. match_continues_to_patch.back()->get().push_back(opcodes.size());
  1112. append(0);
  1113. }
  1114. void GDScriptByteCodeGenerator::write_breakpoint() {
  1115. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  1116. }
  1117. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1118. append(GDScriptFunction::OPCODE_LINE, 0);
  1119. append(p_line);
  1120. current_line = p_line;
  1121. }
  1122. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1123. append(GDScriptFunction::OPCODE_RETURN, 1);
  1124. append(p_return_value);
  1125. }
  1126. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1127. append(GDScriptFunction::OPCODE_ASSERT, 2);
  1128. append(p_test);
  1129. append(p_message);
  1130. }
  1131. void GDScriptByteCodeGenerator::start_block() {
  1132. push_stack_identifiers();
  1133. }
  1134. void GDScriptByteCodeGenerator::end_block() {
  1135. pop_stack_identifiers();
  1136. }
  1137. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1138. if (!ended && function != nullptr) {
  1139. memdelete(function);
  1140. }
  1141. }