gdscript_byte_codegen.cpp 43 KB

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