gdscript_byte_codegen.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*************************************************************************/
  2. /* gdscript_byte_codegen.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_byte_codegen.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "gdscript.h"
  33. uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) {
  34. #ifdef TOOLS_ENABLED
  35. function->arg_names.push_back(p_name);
  36. #endif
  37. function->_argument_count++;
  38. function->argument_types.push_back(p_type);
  39. if (p_is_optional) {
  40. function->_default_arg_count++;
  41. }
  42. return add_local(p_name, p_type);
  43. }
  44. uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  45. int stack_pos = increase_stack();
  46. add_stack_identifier(p_name, stack_pos);
  47. return stack_pos;
  48. }
  49. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  50. int index = add_or_get_constant(p_constant);
  51. local_constants[p_name] = index;
  52. return index;
  53. }
  54. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  55. if (constant_map.has(p_constant)) {
  56. return constant_map[p_constant];
  57. }
  58. int index = constant_map.size();
  59. constant_map[p_constant] = index;
  60. return index;
  61. }
  62. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  63. return get_name_map_pos(p_name);
  64. }
  65. uint32_t GDScriptByteCodeGenerator::add_temporary() {
  66. current_temporaries++;
  67. int idx = increase_stack();
  68. #ifdef DEBUG_ENABLED
  69. temp_stack.push_back(idx);
  70. #endif
  71. return idx;
  72. }
  73. void GDScriptByteCodeGenerator::pop_temporary() {
  74. ERR_FAIL_COND(current_temporaries == 0);
  75. current_stack_size--;
  76. #ifdef DEBUG_ENABLED
  77. if (temp_stack.back()->get() != current_stack_size) {
  78. ERR_PRINT("Mismatched popping of temporary value");
  79. }
  80. temp_stack.pop_back();
  81. #endif
  82. current_temporaries--;
  83. }
  84. void GDScriptByteCodeGenerator::start_parameters() {
  85. if (function->_default_arg_count > 0) {
  86. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  87. function->default_arguments.push_back(opcodes.size());
  88. }
  89. }
  90. void GDScriptByteCodeGenerator::end_parameters() {
  91. function->default_arguments.invert();
  92. }
  93. void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) {
  94. function = memnew(GDScriptFunction);
  95. debug_stack = EngineDebugger::is_active();
  96. function->name = p_function_name;
  97. function->_script = p_script;
  98. function->source = p_script->get_path();
  99. #ifdef DEBUG_ENABLED
  100. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  101. function->_func_cname = function->func_cname.get_data();
  102. #endif
  103. function->_static = p_static;
  104. function->return_type = p_return_type;
  105. function->rpc_mode = p_rpc_mode;
  106. function->_argument_count = 0;
  107. }
  108. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  109. #ifdef DEBUG_ENABLED
  110. if (current_temporaries != 0) {
  111. ERR_PRINT("Non-zero temporary variables at end of function: " + itos(current_temporaries));
  112. }
  113. #endif
  114. append(GDScriptFunction::OPCODE_END, 0);
  115. if (constant_map.size()) {
  116. function->_constant_count = constant_map.size();
  117. function->constants.resize(constant_map.size());
  118. function->_constants_ptr = function->constants.ptrw();
  119. const Variant *K = nullptr;
  120. while ((K = constant_map.next(K))) {
  121. int idx = constant_map[*K];
  122. function->constants.write[idx] = *K;
  123. }
  124. } else {
  125. function->_constants_ptr = nullptr;
  126. function->_constant_count = 0;
  127. }
  128. if (name_map.size()) {
  129. function->global_names.resize(name_map.size());
  130. function->_global_names_ptr = &function->global_names[0];
  131. for (Map<StringName, int>::Element *E = name_map.front(); E; E = E->next()) {
  132. function->global_names.write[E->get()] = E->key();
  133. }
  134. function->_global_names_count = function->global_names.size();
  135. } else {
  136. function->_global_names_ptr = nullptr;
  137. function->_global_names_count = 0;
  138. }
  139. if (opcodes.size()) {
  140. function->code = opcodes;
  141. function->_code_ptr = &function->code[0];
  142. function->_code_size = opcodes.size();
  143. } else {
  144. function->_code_ptr = nullptr;
  145. function->_code_size = 0;
  146. }
  147. if (function->default_arguments.size()) {
  148. function->_default_arg_count = function->default_arguments.size() - 1;
  149. function->_default_arg_ptr = &function->default_arguments[0];
  150. } else {
  151. function->_default_arg_count = 0;
  152. function->_default_arg_ptr = nullptr;
  153. }
  154. if (operator_func_map.size()) {
  155. function->operator_funcs.resize(operator_func_map.size());
  156. function->_operator_funcs_count = function->operator_funcs.size();
  157. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  158. for (const Map<Variant::ValidatedOperatorEvaluator, int>::Element *E = operator_func_map.front(); E; E = E->next()) {
  159. function->operator_funcs.write[E->get()] = E->key();
  160. }
  161. } else {
  162. function->_operator_funcs_count = 0;
  163. function->_operator_funcs_ptr = nullptr;
  164. }
  165. if (setters_map.size()) {
  166. function->setters.resize(setters_map.size());
  167. function->_setters_count = function->setters.size();
  168. function->_setters_ptr = function->setters.ptr();
  169. for (const Map<Variant::ValidatedSetter, int>::Element *E = setters_map.front(); E; E = E->next()) {
  170. function->setters.write[E->get()] = E->key();
  171. }
  172. } else {
  173. function->_setters_count = 0;
  174. function->_setters_ptr = nullptr;
  175. }
  176. if (getters_map.size()) {
  177. function->getters.resize(getters_map.size());
  178. function->_getters_count = function->getters.size();
  179. function->_getters_ptr = function->getters.ptr();
  180. for (const Map<Variant::ValidatedGetter, int>::Element *E = getters_map.front(); E; E = E->next()) {
  181. function->getters.write[E->get()] = E->key();
  182. }
  183. } else {
  184. function->_getters_count = 0;
  185. function->_getters_ptr = nullptr;
  186. }
  187. if (keyed_setters_map.size()) {
  188. function->keyed_setters.resize(keyed_setters_map.size());
  189. function->_keyed_setters_count = function->keyed_setters.size();
  190. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  191. for (const Map<Variant::ValidatedKeyedSetter, int>::Element *E = keyed_setters_map.front(); E; E = E->next()) {
  192. function->keyed_setters.write[E->get()] = E->key();
  193. }
  194. } else {
  195. function->_keyed_setters_count = 0;
  196. function->_keyed_setters_ptr = nullptr;
  197. }
  198. if (keyed_getters_map.size()) {
  199. function->keyed_getters.resize(keyed_getters_map.size());
  200. function->_keyed_getters_count = function->keyed_getters.size();
  201. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  202. for (const Map<Variant::ValidatedKeyedGetter, int>::Element *E = keyed_getters_map.front(); E; E = E->next()) {
  203. function->keyed_getters.write[E->get()] = E->key();
  204. }
  205. } else {
  206. function->_keyed_getters_count = 0;
  207. function->_keyed_getters_ptr = nullptr;
  208. }
  209. if (indexed_setters_map.size()) {
  210. function->indexed_setters.resize(indexed_setters_map.size());
  211. function->_indexed_setters_count = function->indexed_setters.size();
  212. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  213. for (const Map<Variant::ValidatedIndexedSetter, int>::Element *E = indexed_setters_map.front(); E; E = E->next()) {
  214. function->indexed_setters.write[E->get()] = E->key();
  215. }
  216. } else {
  217. function->_indexed_setters_count = 0;
  218. function->_indexed_setters_ptr = nullptr;
  219. }
  220. if (indexed_getters_map.size()) {
  221. function->indexed_getters.resize(indexed_getters_map.size());
  222. function->_indexed_getters_count = function->indexed_getters.size();
  223. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  224. for (const Map<Variant::ValidatedIndexedGetter, int>::Element *E = indexed_getters_map.front(); E; E = E->next()) {
  225. function->indexed_getters.write[E->get()] = E->key();
  226. }
  227. } else {
  228. function->_indexed_getters_count = 0;
  229. function->_indexed_getters_ptr = nullptr;
  230. }
  231. if (builtin_method_map.size()) {
  232. function->builtin_methods.resize(builtin_method_map.size());
  233. function->_builtin_methods_ptr = function->builtin_methods.ptr();
  234. function->_builtin_methods_count = builtin_method_map.size();
  235. for (const Map<Variant::ValidatedBuiltInMethod, int>::Element *E = builtin_method_map.front(); E; E = E->next()) {
  236. function->builtin_methods.write[E->get()] = E->key();
  237. }
  238. } else {
  239. function->_builtin_methods_ptr = nullptr;
  240. function->_builtin_methods_count = 0;
  241. }
  242. if (constructors_map.size()) {
  243. function->constructors.resize(constructors_map.size());
  244. function->_constructors_ptr = function->constructors.ptr();
  245. function->_constructors_count = constructors_map.size();
  246. for (const Map<Variant::ValidatedConstructor, int>::Element *E = constructors_map.front(); E; E = E->next()) {
  247. function->constructors.write[E->get()] = E->key();
  248. }
  249. } else {
  250. function->_constructors_ptr = nullptr;
  251. function->_constructors_count = 0;
  252. }
  253. if (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, 0);
  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 false.
  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_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  805. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  806. for (int i = 0; i < p_arguments.size(); i++) {
  807. append(p_arguments[i]);
  808. }
  809. append(p_base);
  810. append(p_target);
  811. append(p_arguments.size());
  812. append(p_function_name);
  813. }
  814. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  815. // Try to find an appropriate constructor.
  816. bool all_have_type = true;
  817. Vector<Variant::Type> arg_types;
  818. for (int i = 0; i < p_arguments.size(); i++) {
  819. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  820. all_have_type = false;
  821. break;
  822. }
  823. arg_types.push_back(p_arguments[i].type.builtin_type);
  824. }
  825. if (all_have_type) {
  826. int valid_constructor = -1;
  827. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  828. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  829. continue;
  830. }
  831. int types_correct = true;
  832. for (int j = 0; j < arg_types.size(); j++) {
  833. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  834. types_correct = false;
  835. break;
  836. }
  837. }
  838. if (types_correct) {
  839. valid_constructor = i;
  840. break;
  841. }
  842. }
  843. if (valid_constructor >= 0) {
  844. append(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  845. for (int i = 0; i < p_arguments.size(); i++) {
  846. append(p_arguments[i]);
  847. }
  848. append(p_target);
  849. append(p_arguments.size());
  850. append(Variant::get_validated_constructor(p_type, valid_constructor));
  851. return;
  852. }
  853. }
  854. append(GDScriptFunction::OPCODE_CONSTRUCT, 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(p_type);
  861. }
  862. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  863. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  864. for (int i = 0; i < p_arguments.size(); i++) {
  865. append(p_arguments[i]);
  866. }
  867. append(p_target);
  868. append(p_arguments.size());
  869. }
  870. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  871. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  872. for (int i = 0; i < p_arguments.size(); i++) {
  873. append(p_arguments[i]);
  874. }
  875. append(p_target);
  876. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  877. }
  878. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  879. append(GDScriptFunction::OPCODE_AWAIT, 1);
  880. append(p_operand);
  881. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  882. append(p_target);
  883. }
  884. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  885. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  886. append(p_condition);
  887. if_jmp_addrs.push_back(opcodes.size());
  888. append(0); // Jump destination, will be patched.
  889. }
  890. void GDScriptByteCodeGenerator::write_else() {
  891. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  892. int else_jmp_addr = opcodes.size();
  893. append(0); // Jump destination, will be patched.
  894. patch_jump(if_jmp_addrs.back()->get());
  895. if_jmp_addrs.pop_back();
  896. if_jmp_addrs.push_back(else_jmp_addr);
  897. }
  898. void GDScriptByteCodeGenerator::write_endif() {
  899. patch_jump(if_jmp_addrs.back()->get());
  900. if_jmp_addrs.pop_back();
  901. }
  902. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  903. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  904. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  905. // Store state.
  906. for_counter_variables.push_back(counter);
  907. for_container_variables.push_back(container);
  908. }
  909. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  910. const Address &container = for_container_variables.back()->get();
  911. // Assign container.
  912. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  913. append(container);
  914. append(p_list);
  915. for_iterator_variables.push_back(p_variable);
  916. }
  917. void GDScriptByteCodeGenerator::write_for() {
  918. const Address &iterator = for_iterator_variables.back()->get();
  919. const Address &counter = for_counter_variables.back()->get();
  920. const Address &container = for_container_variables.back()->get();
  921. current_breaks_to_patch.push_back(List<int>());
  922. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  923. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  924. if (container.type.has_type) {
  925. if (container.type.kind == GDScriptDataType::BUILTIN) {
  926. switch (container.type.builtin_type) {
  927. case Variant::INT:
  928. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  929. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  930. break;
  931. case Variant::FLOAT:
  932. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  933. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  934. break;
  935. case Variant::VECTOR2:
  936. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  937. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  938. break;
  939. case Variant::VECTOR2I:
  940. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  941. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  942. break;
  943. case Variant::VECTOR3:
  944. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  945. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  946. break;
  947. case Variant::VECTOR3I:
  948. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  949. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  950. break;
  951. case Variant::STRING:
  952. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  953. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  954. break;
  955. case Variant::DICTIONARY:
  956. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  957. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  958. break;
  959. case Variant::ARRAY:
  960. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  961. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  962. break;
  963. case Variant::PACKED_BYTE_ARRAY:
  964. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  965. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  966. break;
  967. case Variant::PACKED_INT32_ARRAY:
  968. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  969. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  970. break;
  971. case Variant::PACKED_INT64_ARRAY:
  972. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  973. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  974. break;
  975. case Variant::PACKED_FLOAT32_ARRAY:
  976. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  977. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  978. break;
  979. case Variant::PACKED_FLOAT64_ARRAY:
  980. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  981. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  982. break;
  983. case Variant::PACKED_STRING_ARRAY:
  984. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  985. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  986. break;
  987. case Variant::PACKED_VECTOR2_ARRAY:
  988. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  989. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  990. break;
  991. case Variant::PACKED_VECTOR3_ARRAY:
  992. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  993. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  994. break;
  995. case Variant::PACKED_COLOR_ARRAY:
  996. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  997. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. } else {
  1003. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1004. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1005. }
  1006. }
  1007. // Begin loop.
  1008. append(begin_opcode, 3);
  1009. append(counter);
  1010. append(container);
  1011. append(iterator);
  1012. for_jmp_addrs.push_back(opcodes.size());
  1013. append(0); // End of loop address, will be patched.
  1014. append(GDScriptFunction::OPCODE_JUMP, 0);
  1015. append(opcodes.size() + 6); // Skip over 'continue' code.
  1016. // Next iteration.
  1017. int continue_addr = opcodes.size();
  1018. continue_addrs.push_back(continue_addr);
  1019. append(iterate_opcode, 3);
  1020. append(counter);
  1021. append(container);
  1022. append(iterator);
  1023. for_jmp_addrs.push_back(opcodes.size());
  1024. append(0); // Jump destination, will be patched.
  1025. }
  1026. void GDScriptByteCodeGenerator::write_endfor() {
  1027. // Jump back to loop check.
  1028. append(GDScriptFunction::OPCODE_JUMP, 0);
  1029. append(continue_addrs.back()->get());
  1030. continue_addrs.pop_back();
  1031. // Patch end jumps (two of them).
  1032. for (int i = 0; i < 2; i++) {
  1033. patch_jump(for_jmp_addrs.back()->get());
  1034. for_jmp_addrs.pop_back();
  1035. }
  1036. // Patch break statements.
  1037. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1038. patch_jump(E->get());
  1039. }
  1040. current_breaks_to_patch.pop_back();
  1041. // Pop state.
  1042. for_iterator_variables.pop_back();
  1043. for_counter_variables.pop_back();
  1044. for_container_variables.pop_back();
  1045. }
  1046. void GDScriptByteCodeGenerator::start_while_condition() {
  1047. current_breaks_to_patch.push_back(List<int>());
  1048. continue_addrs.push_back(opcodes.size());
  1049. }
  1050. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1051. // Condition check.
  1052. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  1053. append(p_condition);
  1054. while_jmp_addrs.push_back(opcodes.size());
  1055. append(0); // End of loop address, will be patched.
  1056. }
  1057. void GDScriptByteCodeGenerator::write_endwhile() {
  1058. // Jump back to loop check.
  1059. append(GDScriptFunction::OPCODE_JUMP, 0);
  1060. append(continue_addrs.back()->get());
  1061. continue_addrs.pop_back();
  1062. // Patch end jump.
  1063. patch_jump(while_jmp_addrs.back()->get());
  1064. while_jmp_addrs.pop_back();
  1065. // Patch break statements.
  1066. for (const List<int>::Element *E = current_breaks_to_patch.back()->get().front(); E; E = E->next()) {
  1067. patch_jump(E->get());
  1068. }
  1069. current_breaks_to_patch.pop_back();
  1070. }
  1071. void GDScriptByteCodeGenerator::start_match() {
  1072. match_continues_to_patch.push_back(List<int>());
  1073. }
  1074. void GDScriptByteCodeGenerator::start_match_branch() {
  1075. // Patch continue statements.
  1076. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1077. patch_jump(E->get());
  1078. }
  1079. match_continues_to_patch.pop_back();
  1080. // Start a new list for next branch.
  1081. match_continues_to_patch.push_back(List<int>());
  1082. }
  1083. void GDScriptByteCodeGenerator::end_match() {
  1084. // Patch continue statements.
  1085. for (const List<int>::Element *E = match_continues_to_patch.back()->get().front(); E; E = E->next()) {
  1086. patch_jump(E->get());
  1087. }
  1088. match_continues_to_patch.pop_back();
  1089. }
  1090. void GDScriptByteCodeGenerator::write_break() {
  1091. append(GDScriptFunction::OPCODE_JUMP, 0);
  1092. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1093. append(0);
  1094. }
  1095. void GDScriptByteCodeGenerator::write_continue() {
  1096. append(GDScriptFunction::OPCODE_JUMP, 0);
  1097. append(continue_addrs.back()->get());
  1098. }
  1099. void GDScriptByteCodeGenerator::write_continue_match() {
  1100. append(GDScriptFunction::OPCODE_JUMP, 0);
  1101. match_continues_to_patch.back()->get().push_back(opcodes.size());
  1102. append(0);
  1103. }
  1104. void GDScriptByteCodeGenerator::write_breakpoint() {
  1105. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  1106. }
  1107. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1108. append(GDScriptFunction::OPCODE_LINE, 0);
  1109. append(p_line);
  1110. current_line = p_line;
  1111. }
  1112. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1113. append(GDScriptFunction::OPCODE_RETURN, 1);
  1114. append(p_return_value);
  1115. }
  1116. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1117. append(GDScriptFunction::OPCODE_ASSERT, 2);
  1118. append(p_test);
  1119. append(p_message);
  1120. }
  1121. void GDScriptByteCodeGenerator::start_block() {
  1122. push_stack_identifiers();
  1123. }
  1124. void GDScriptByteCodeGenerator::end_block() {
  1125. pop_stack_identifiers();
  1126. }
  1127. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1128. if (!ended && function != nullptr) {
  1129. memdelete(function);
  1130. }
  1131. }