gdscript_byte_codegen.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 = locals.size() + RESERVED_STACK;
  46. locals.push_back(StackSlot(p_type.builtin_type));
  47. add_stack_identifier(p_name, stack_pos);
  48. return stack_pos;
  49. }
  50. uint32_t GDScriptByteCodeGenerator::add_local_constant(const StringName &p_name, const Variant &p_constant) {
  51. int index = add_or_get_constant(p_constant);
  52. local_constants[p_name] = index;
  53. return index;
  54. }
  55. uint32_t GDScriptByteCodeGenerator::add_or_get_constant(const Variant &p_constant) {
  56. return get_constant_pos(p_constant);
  57. }
  58. uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
  59. return get_name_map_pos(p_name);
  60. }
  61. uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_type) {
  62. Variant::Type temp_type = Variant::NIL;
  63. if (p_type.has_type) {
  64. if (p_type.kind == GDScriptDataType::BUILTIN) {
  65. switch (p_type.builtin_type) {
  66. case Variant::NIL:
  67. case Variant::BOOL:
  68. case Variant::INT:
  69. case Variant::FLOAT:
  70. case Variant::STRING:
  71. case Variant::VECTOR2:
  72. case Variant::VECTOR2I:
  73. case Variant::RECT2:
  74. case Variant::RECT2I:
  75. case Variant::VECTOR3:
  76. case Variant::VECTOR3I:
  77. case Variant::TRANSFORM2D:
  78. case Variant::PLANE:
  79. case Variant::QUATERNION:
  80. case Variant::AABB:
  81. case Variant::BASIS:
  82. case Variant::TRANSFORM3D:
  83. case Variant::COLOR:
  84. case Variant::STRING_NAME:
  85. case Variant::NODE_PATH:
  86. case Variant::RID:
  87. case Variant::OBJECT:
  88. case Variant::CALLABLE:
  89. case Variant::SIGNAL:
  90. case Variant::DICTIONARY:
  91. case Variant::ARRAY:
  92. temp_type = p_type.builtin_type;
  93. break;
  94. case Variant::PACKED_BYTE_ARRAY:
  95. case Variant::PACKED_INT32_ARRAY:
  96. case Variant::PACKED_INT64_ARRAY:
  97. case Variant::PACKED_FLOAT32_ARRAY:
  98. case Variant::PACKED_FLOAT64_ARRAY:
  99. case Variant::PACKED_STRING_ARRAY:
  100. case Variant::PACKED_VECTOR2_ARRAY:
  101. case Variant::PACKED_VECTOR3_ARRAY:
  102. case Variant::PACKED_COLOR_ARRAY:
  103. case Variant::VARIANT_MAX:
  104. // Packed arrays are reference counted, so we don't use the pool for them.
  105. temp_type = Variant::NIL;
  106. break;
  107. }
  108. } else {
  109. temp_type = Variant::OBJECT;
  110. }
  111. }
  112. if (!temporaries_pool.has(temp_type)) {
  113. temporaries_pool[temp_type] = List<int>();
  114. }
  115. List<int> &pool = temporaries_pool[temp_type];
  116. if (pool.is_empty()) {
  117. StackSlot new_temp(temp_type);
  118. int idx = temporaries.size();
  119. pool.push_back(idx);
  120. temporaries.push_back(new_temp);
  121. }
  122. int slot = pool.front()->get();
  123. pool.pop_front();
  124. used_temporaries.push_back(slot);
  125. return slot;
  126. }
  127. void GDScriptByteCodeGenerator::pop_temporary() {
  128. ERR_FAIL_COND(used_temporaries.is_empty());
  129. int slot_idx = used_temporaries.back()->get();
  130. const StackSlot &slot = temporaries[slot_idx];
  131. temporaries_pool[slot.type].push_back(slot_idx);
  132. used_temporaries.pop_back();
  133. }
  134. void GDScriptByteCodeGenerator::start_parameters() {
  135. if (function->_default_arg_count > 0) {
  136. append(GDScriptFunction::OPCODE_JUMP_TO_DEF_ARGUMENT);
  137. function->default_arguments.push_back(opcodes.size());
  138. }
  139. }
  140. void GDScriptByteCodeGenerator::end_parameters() {
  141. function->default_arguments.reverse();
  142. }
  143. void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, Multiplayer::RPCConfig p_rpc_config, const GDScriptDataType &p_return_type) {
  144. function = memnew(GDScriptFunction);
  145. debug_stack = EngineDebugger::is_active();
  146. function->name = p_function_name;
  147. function->_script = p_script;
  148. function->source = p_script->get_path();
  149. #ifdef DEBUG_ENABLED
  150. function->func_cname = (String(function->source) + " - " + String(p_function_name)).utf8();
  151. function->_func_cname = function->func_cname.get_data();
  152. #endif
  153. function->_static = p_static;
  154. function->return_type = p_return_type;
  155. function->rpc_config = p_rpc_config;
  156. function->_argument_count = 0;
  157. }
  158. GDScriptFunction *GDScriptByteCodeGenerator::write_end() {
  159. #ifdef DEBUG_ENABLED
  160. if (!used_temporaries.is_empty()) {
  161. ERR_PRINT("Non-zero temporary variables at end of function: " + itos(used_temporaries.size()));
  162. }
  163. #endif
  164. append(GDScriptFunction::OPCODE_END, 0);
  165. for (int i = 0; i < temporaries.size(); i++) {
  166. int stack_index = i + max_locals + RESERVED_STACK;
  167. for (int j = 0; j < temporaries[i].bytecode_indices.size(); j++) {
  168. opcodes.write[temporaries[i].bytecode_indices[j]] = stack_index | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  169. }
  170. if (temporaries[i].type != Variant::NIL) {
  171. function->temporary_slots[stack_index] = temporaries[i].type;
  172. }
  173. }
  174. if (constant_map.size()) {
  175. function->_constant_count = constant_map.size();
  176. function->constants.resize(constant_map.size());
  177. function->_constants_ptr = function->constants.ptrw();
  178. const Variant *K = nullptr;
  179. while ((K = constant_map.next(K))) {
  180. int idx = constant_map[*K];
  181. function->constants.write[idx] = *K;
  182. }
  183. } else {
  184. function->_constants_ptr = nullptr;
  185. function->_constant_count = 0;
  186. }
  187. if (name_map.size()) {
  188. function->global_names.resize(name_map.size());
  189. function->_global_names_ptr = &function->global_names[0];
  190. for (const KeyValue<StringName, int> &E : name_map) {
  191. function->global_names.write[E.value] = E.key;
  192. }
  193. function->_global_names_count = function->global_names.size();
  194. } else {
  195. function->_global_names_ptr = nullptr;
  196. function->_global_names_count = 0;
  197. }
  198. if (opcodes.size()) {
  199. function->code = opcodes;
  200. function->_code_ptr = &function->code[0];
  201. function->_code_size = opcodes.size();
  202. } else {
  203. function->_code_ptr = nullptr;
  204. function->_code_size = 0;
  205. }
  206. if (function->default_arguments.size()) {
  207. function->_default_arg_count = function->default_arguments.size() - 1;
  208. function->_default_arg_ptr = &function->default_arguments[0];
  209. } else {
  210. function->_default_arg_count = 0;
  211. function->_default_arg_ptr = nullptr;
  212. }
  213. if (operator_func_map.size()) {
  214. function->operator_funcs.resize(operator_func_map.size());
  215. function->_operator_funcs_count = function->operator_funcs.size();
  216. function->_operator_funcs_ptr = function->operator_funcs.ptr();
  217. for (const KeyValue<Variant::ValidatedOperatorEvaluator, int> &E : operator_func_map) {
  218. function->operator_funcs.write[E.value] = E.key;
  219. }
  220. } else {
  221. function->_operator_funcs_count = 0;
  222. function->_operator_funcs_ptr = nullptr;
  223. }
  224. if (setters_map.size()) {
  225. function->setters.resize(setters_map.size());
  226. function->_setters_count = function->setters.size();
  227. function->_setters_ptr = function->setters.ptr();
  228. for (const KeyValue<Variant::ValidatedSetter, int> &E : setters_map) {
  229. function->setters.write[E.value] = E.key;
  230. }
  231. } else {
  232. function->_setters_count = 0;
  233. function->_setters_ptr = nullptr;
  234. }
  235. if (getters_map.size()) {
  236. function->getters.resize(getters_map.size());
  237. function->_getters_count = function->getters.size();
  238. function->_getters_ptr = function->getters.ptr();
  239. for (const KeyValue<Variant::ValidatedGetter, int> &E : getters_map) {
  240. function->getters.write[E.value] = E.key;
  241. }
  242. } else {
  243. function->_getters_count = 0;
  244. function->_getters_ptr = nullptr;
  245. }
  246. if (keyed_setters_map.size()) {
  247. function->keyed_setters.resize(keyed_setters_map.size());
  248. function->_keyed_setters_count = function->keyed_setters.size();
  249. function->_keyed_setters_ptr = function->keyed_setters.ptr();
  250. for (const KeyValue<Variant::ValidatedKeyedSetter, int> &E : keyed_setters_map) {
  251. function->keyed_setters.write[E.value] = E.key;
  252. }
  253. } else {
  254. function->_keyed_setters_count = 0;
  255. function->_keyed_setters_ptr = nullptr;
  256. }
  257. if (keyed_getters_map.size()) {
  258. function->keyed_getters.resize(keyed_getters_map.size());
  259. function->_keyed_getters_count = function->keyed_getters.size();
  260. function->_keyed_getters_ptr = function->keyed_getters.ptr();
  261. for (const KeyValue<Variant::ValidatedKeyedGetter, int> &E : keyed_getters_map) {
  262. function->keyed_getters.write[E.value] = E.key;
  263. }
  264. } else {
  265. function->_keyed_getters_count = 0;
  266. function->_keyed_getters_ptr = nullptr;
  267. }
  268. if (indexed_setters_map.size()) {
  269. function->indexed_setters.resize(indexed_setters_map.size());
  270. function->_indexed_setters_count = function->indexed_setters.size();
  271. function->_indexed_setters_ptr = function->indexed_setters.ptr();
  272. for (const KeyValue<Variant::ValidatedIndexedSetter, int> &E : indexed_setters_map) {
  273. function->indexed_setters.write[E.value] = E.key;
  274. }
  275. } else {
  276. function->_indexed_setters_count = 0;
  277. function->_indexed_setters_ptr = nullptr;
  278. }
  279. if (indexed_getters_map.size()) {
  280. function->indexed_getters.resize(indexed_getters_map.size());
  281. function->_indexed_getters_count = function->indexed_getters.size();
  282. function->_indexed_getters_ptr = function->indexed_getters.ptr();
  283. for (const KeyValue<Variant::ValidatedIndexedGetter, int> &E : indexed_getters_map) {
  284. function->indexed_getters.write[E.value] = E.key;
  285. }
  286. } else {
  287. function->_indexed_getters_count = 0;
  288. function->_indexed_getters_ptr = nullptr;
  289. }
  290. if (builtin_method_map.size()) {
  291. function->builtin_methods.resize(builtin_method_map.size());
  292. function->_builtin_methods_ptr = function->builtin_methods.ptr();
  293. function->_builtin_methods_count = builtin_method_map.size();
  294. for (const KeyValue<Variant::ValidatedBuiltInMethod, int> &E : builtin_method_map) {
  295. function->builtin_methods.write[E.value] = E.key;
  296. }
  297. } else {
  298. function->_builtin_methods_ptr = nullptr;
  299. function->_builtin_methods_count = 0;
  300. }
  301. if (constructors_map.size()) {
  302. function->constructors.resize(constructors_map.size());
  303. function->_constructors_ptr = function->constructors.ptr();
  304. function->_constructors_count = constructors_map.size();
  305. for (const KeyValue<Variant::ValidatedConstructor, int> &E : constructors_map) {
  306. function->constructors.write[E.value] = E.key;
  307. }
  308. } else {
  309. function->_constructors_ptr = nullptr;
  310. function->_constructors_count = 0;
  311. }
  312. if (utilities_map.size()) {
  313. function->utilities.resize(utilities_map.size());
  314. function->_utilities_ptr = function->utilities.ptr();
  315. function->_utilities_count = utilities_map.size();
  316. for (const KeyValue<Variant::ValidatedUtilityFunction, int> &E : utilities_map) {
  317. function->utilities.write[E.value] = E.key;
  318. }
  319. } else {
  320. function->_utilities_ptr = nullptr;
  321. function->_utilities_count = 0;
  322. }
  323. if (gds_utilities_map.size()) {
  324. function->gds_utilities.resize(gds_utilities_map.size());
  325. function->_gds_utilities_ptr = function->gds_utilities.ptr();
  326. function->_gds_utilities_count = gds_utilities_map.size();
  327. for (const KeyValue<GDScriptUtilityFunctions::FunctionPtr, int> &E : gds_utilities_map) {
  328. function->gds_utilities.write[E.value] = E.key;
  329. }
  330. } else {
  331. function->_gds_utilities_ptr = nullptr;
  332. function->_gds_utilities_count = 0;
  333. }
  334. if (method_bind_map.size()) {
  335. function->methods.resize(method_bind_map.size());
  336. function->_methods_ptr = function->methods.ptrw();
  337. function->_methods_count = method_bind_map.size();
  338. for (const KeyValue<MethodBind *, int> &E : method_bind_map) {
  339. function->methods.write[E.value] = E.key;
  340. }
  341. } else {
  342. function->_methods_ptr = nullptr;
  343. function->_methods_count = 0;
  344. }
  345. if (lambdas_map.size()) {
  346. function->lambdas.resize(lambdas_map.size());
  347. function->_lambdas_ptr = function->lambdas.ptrw();
  348. function->_lambdas_count = lambdas_map.size();
  349. for (const KeyValue<GDScriptFunction *, int> &E : lambdas_map) {
  350. function->lambdas.write[E.value] = E.key;
  351. }
  352. } else {
  353. function->_lambdas_ptr = nullptr;
  354. function->_lambdas_count = 0;
  355. }
  356. if (debug_stack) {
  357. function->stack_debug = stack_debug;
  358. }
  359. function->_stack_size = RESERVED_STACK + max_locals + temporaries.size();
  360. function->_instruction_args_size = instr_args_max;
  361. function->_ptrcall_args_size = ptrcall_max;
  362. ended = true;
  363. return function;
  364. }
  365. #ifdef DEBUG_ENABLED
  366. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  367. function->profile.signature = p_signature;
  368. }
  369. #endif
  370. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  371. function->_initial_line = p_line;
  372. }
  373. #define HAS_BUILTIN_TYPE(m_var) \
  374. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  375. #define IS_BUILTIN_TYPE(m_var, m_type) \
  376. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  377. void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) {
  378. switch (p_new_type) {
  379. case Variant::BOOL:
  380. append(GDScriptFunction::OPCODE_TYPE_ADJUST_BOOL, 1);
  381. break;
  382. case Variant::INT:
  383. append(GDScriptFunction::OPCODE_TYPE_ADJUST_INT, 1);
  384. break;
  385. case Variant::FLOAT:
  386. append(GDScriptFunction::OPCODE_TYPE_ADJUST_FLOAT, 1);
  387. break;
  388. case Variant::STRING:
  389. append(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING, 1);
  390. break;
  391. case Variant::VECTOR2:
  392. append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2, 1);
  393. break;
  394. case Variant::VECTOR2I:
  395. append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2I, 1);
  396. break;
  397. case Variant::RECT2:
  398. append(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2, 1);
  399. break;
  400. case Variant::RECT2I:
  401. append(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2I, 1);
  402. break;
  403. case Variant::VECTOR3:
  404. append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3, 1);
  405. break;
  406. case Variant::VECTOR3I:
  407. append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I, 1);
  408. break;
  409. case Variant::TRANSFORM2D:
  410. append(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM2D, 1);
  411. break;
  412. case Variant::PLANE:
  413. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PLANE, 1);
  414. break;
  415. case Variant::QUATERNION:
  416. append(GDScriptFunction::OPCODE_TYPE_ADJUST_QUATERNION, 1);
  417. break;
  418. case Variant::AABB:
  419. append(GDScriptFunction::OPCODE_TYPE_ADJUST_AABB, 1);
  420. break;
  421. case Variant::BASIS:
  422. append(GDScriptFunction::OPCODE_TYPE_ADJUST_BASIS, 1);
  423. break;
  424. case Variant::TRANSFORM3D:
  425. append(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM, 1);
  426. break;
  427. case Variant::COLOR:
  428. append(GDScriptFunction::OPCODE_TYPE_ADJUST_COLOR, 1);
  429. break;
  430. case Variant::STRING_NAME:
  431. append(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING_NAME, 1);
  432. break;
  433. case Variant::NODE_PATH:
  434. append(GDScriptFunction::OPCODE_TYPE_ADJUST_NODE_PATH, 1);
  435. break;
  436. case Variant::RID:
  437. append(GDScriptFunction::OPCODE_TYPE_ADJUST_RID, 1);
  438. break;
  439. case Variant::OBJECT:
  440. append(GDScriptFunction::OPCODE_TYPE_ADJUST_OBJECT, 1);
  441. break;
  442. case Variant::CALLABLE:
  443. append(GDScriptFunction::OPCODE_TYPE_ADJUST_CALLABLE, 1);
  444. break;
  445. case Variant::SIGNAL:
  446. append(GDScriptFunction::OPCODE_TYPE_ADJUST_SIGNAL, 1);
  447. break;
  448. case Variant::DICTIONARY:
  449. append(GDScriptFunction::OPCODE_TYPE_ADJUST_DICTIONARY, 1);
  450. break;
  451. case Variant::ARRAY:
  452. append(GDScriptFunction::OPCODE_TYPE_ADJUST_ARRAY, 1);
  453. break;
  454. case Variant::PACKED_BYTE_ARRAY:
  455. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, 1);
  456. break;
  457. case Variant::PACKED_INT32_ARRAY:
  458. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, 1);
  459. break;
  460. case Variant::PACKED_INT64_ARRAY:
  461. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, 1);
  462. break;
  463. case Variant::PACKED_FLOAT32_ARRAY:
  464. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, 1);
  465. break;
  466. case Variant::PACKED_FLOAT64_ARRAY:
  467. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, 1);
  468. break;
  469. case Variant::PACKED_STRING_ARRAY:
  470. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, 1);
  471. break;
  472. case Variant::PACKED_VECTOR2_ARRAY:
  473. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, 1);
  474. break;
  475. case Variant::PACKED_VECTOR3_ARRAY:
  476. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, 1);
  477. break;
  478. case Variant::PACKED_COLOR_ARRAY:
  479. append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, 1);
  480. break;
  481. case Variant::NIL:
  482. case Variant::VARIANT_MAX:
  483. return;
  484. }
  485. append(p_target);
  486. }
  487. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  488. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  489. // Gather specific operator.
  490. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  491. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  492. append(p_left_operand);
  493. append(Address());
  494. append(p_target);
  495. append(op_func);
  496. return;
  497. }
  498. // No specific types, perform variant evaluation.
  499. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  500. append(p_left_operand);
  501. append(Address());
  502. append(p_target);
  503. append(p_operator);
  504. }
  505. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  506. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  507. if (p_target.mode == Address::TEMPORARY) {
  508. Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  509. Variant::Type temp_type = temporaries[p_target.address].type;
  510. if (result_type != temp_type) {
  511. write_type_adjust(p_target, result_type);
  512. }
  513. }
  514. // Gather specific operator.
  515. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  516. append(GDScriptFunction::OPCODE_OPERATOR_VALIDATED, 3);
  517. append(p_left_operand);
  518. append(p_right_operand);
  519. append(p_target);
  520. append(op_func);
  521. return;
  522. }
  523. // No specific types, perform variant evaluation.
  524. append(GDScriptFunction::OPCODE_OPERATOR, 3);
  525. append(p_left_operand);
  526. append(p_right_operand);
  527. append(p_target);
  528. append(p_operator);
  529. }
  530. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  531. append(GDScriptFunction::OPCODE_EXTENDS_TEST, 3);
  532. append(p_source);
  533. append(p_type);
  534. append(p_target);
  535. }
  536. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  537. append(GDScriptFunction::OPCODE_IS_BUILTIN, 2);
  538. append(p_source);
  539. append(p_target);
  540. append(p_type);
  541. }
  542. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  543. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  544. append(p_left_operand);
  545. logic_op_jump_pos1.push_back(opcodes.size());
  546. append(0); // Jump target, will be patched.
  547. }
  548. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  549. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  550. append(p_right_operand);
  551. logic_op_jump_pos2.push_back(opcodes.size());
  552. append(0); // Jump target, will be patched.
  553. }
  554. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  555. // If here means both operands are true.
  556. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  557. append(p_target);
  558. // Jump away from the fail condition.
  559. append(GDScriptFunction::OPCODE_JUMP, 0);
  560. append(opcodes.size() + 3);
  561. // Here it means one of operands is false.
  562. patch_jump(logic_op_jump_pos1.back()->get());
  563. patch_jump(logic_op_jump_pos2.back()->get());
  564. logic_op_jump_pos1.pop_back();
  565. logic_op_jump_pos2.pop_back();
  566. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  567. append(p_target);
  568. }
  569. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  570. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  571. append(p_left_operand);
  572. logic_op_jump_pos1.push_back(opcodes.size());
  573. append(0); // Jump target, will be patched.
  574. }
  575. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  576. append(GDScriptFunction::OPCODE_JUMP_IF, 1);
  577. append(p_right_operand);
  578. logic_op_jump_pos2.push_back(opcodes.size());
  579. append(0); // Jump target, will be patched.
  580. }
  581. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  582. // If here means both operands are false.
  583. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  584. append(p_target);
  585. // Jump away from the success condition.
  586. append(GDScriptFunction::OPCODE_JUMP, 0);
  587. append(opcodes.size() + 3);
  588. // Here it means one of operands is true.
  589. patch_jump(logic_op_jump_pos1.back()->get());
  590. patch_jump(logic_op_jump_pos2.back()->get());
  591. logic_op_jump_pos1.pop_back();
  592. logic_op_jump_pos2.pop_back();
  593. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  594. append(p_target);
  595. }
  596. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  597. ternary_result.push_back(p_target);
  598. }
  599. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  600. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  601. append(p_condition);
  602. ternary_jump_fail_pos.push_back(opcodes.size());
  603. append(0); // Jump target, will be patched.
  604. }
  605. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  606. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  607. append(ternary_result.back()->get());
  608. append(p_expr);
  609. // Jump away from the false path.
  610. append(GDScriptFunction::OPCODE_JUMP, 0);
  611. ternary_jump_skip_pos.push_back(opcodes.size());
  612. append(0);
  613. // Fail must jump here.
  614. patch_jump(ternary_jump_fail_pos.back()->get());
  615. ternary_jump_fail_pos.pop_back();
  616. }
  617. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  618. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  619. append(ternary_result.back()->get());
  620. append(p_expr);
  621. }
  622. void GDScriptByteCodeGenerator::write_end_ternary() {
  623. patch_jump(ternary_jump_skip_pos.back()->get());
  624. ternary_jump_skip_pos.pop_back();
  625. ternary_result.pop_back();
  626. }
  627. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  628. if (HAS_BUILTIN_TYPE(p_target)) {
  629. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
  630. IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
  631. // Use indexed setter instead.
  632. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  633. append(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED, 3);
  634. append(p_target);
  635. append(p_index);
  636. append(p_source);
  637. append(setter);
  638. return;
  639. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  640. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  641. append(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED, 3);
  642. append(p_target);
  643. append(p_index);
  644. append(p_source);
  645. append(setter);
  646. return;
  647. }
  648. }
  649. append(GDScriptFunction::OPCODE_SET_KEYED, 3);
  650. append(p_target);
  651. append(p_index);
  652. append(p_source);
  653. }
  654. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  655. if (HAS_BUILTIN_TYPE(p_source)) {
  656. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  657. // Use indexed getter instead.
  658. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  659. append(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED, 3);
  660. append(p_source);
  661. append(p_index);
  662. append(p_target);
  663. append(getter);
  664. return;
  665. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  666. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  667. append(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED, 3);
  668. append(p_source);
  669. append(p_index);
  670. append(p_target);
  671. append(getter);
  672. return;
  673. }
  674. }
  675. append(GDScriptFunction::OPCODE_GET_KEYED, 3);
  676. append(p_source);
  677. append(p_index);
  678. append(p_target);
  679. }
  680. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  681. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
  682. IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
  683. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  684. append(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED, 2);
  685. append(p_target);
  686. append(p_source);
  687. append(setter);
  688. return;
  689. }
  690. append(GDScriptFunction::OPCODE_SET_NAMED, 2);
  691. append(p_target);
  692. append(p_source);
  693. append(p_name);
  694. }
  695. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  696. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  697. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  698. append(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED, 2);
  699. append(p_source);
  700. append(p_target);
  701. append(getter);
  702. return;
  703. }
  704. append(GDScriptFunction::OPCODE_GET_NAMED, 2);
  705. append(p_source);
  706. append(p_target);
  707. append(p_name);
  708. }
  709. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  710. append(GDScriptFunction::OPCODE_SET_MEMBER, 1);
  711. append(p_value);
  712. append(p_name);
  713. }
  714. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  715. append(GDScriptFunction::OPCODE_GET_MEMBER, 1);
  716. append(p_target);
  717. append(p_name);
  718. }
  719. void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
  720. switch (p_target.type.kind) {
  721. case GDScriptDataType::BUILTIN: {
  722. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  723. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY, 2);
  724. append(p_target);
  725. append(p_source);
  726. } else {
  727. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  728. append(p_target);
  729. append(p_source);
  730. append(p_target.type.builtin_type);
  731. }
  732. } break;
  733. case GDScriptDataType::NATIVE: {
  734. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  735. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  736. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  737. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE, 3);
  738. append(p_target);
  739. append(p_source);
  740. append(class_idx);
  741. } break;
  742. case GDScriptDataType::SCRIPT:
  743. case GDScriptDataType::GDSCRIPT: {
  744. Variant script = p_target.type.script_type;
  745. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  746. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT, 3);
  747. append(p_target);
  748. append(p_source);
  749. append(idx);
  750. } break;
  751. default: {
  752. ERR_PRINT("Compiler bug: unresolved assign.");
  753. // Shouldn't get here, but fail-safe to a regular assignment
  754. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  755. append(p_target);
  756. append(p_source);
  757. }
  758. }
  759. }
  760. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  761. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  762. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY, 2);
  763. append(p_target);
  764. append(p_source);
  765. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  766. // Need conversion.
  767. append(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN, 2);
  768. append(p_target);
  769. append(p_source);
  770. append(p_target.type.builtin_type);
  771. } else {
  772. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  773. append(p_target);
  774. append(p_source);
  775. }
  776. }
  777. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  778. append(GDScriptFunction::OPCODE_ASSIGN_TRUE, 1);
  779. append(p_target);
  780. }
  781. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  782. append(GDScriptFunction::OPCODE_ASSIGN_FALSE, 1);
  783. append(p_target);
  784. }
  785. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src) {
  786. write_assign(p_dst, p_src);
  787. function->default_arguments.push_back(opcodes.size());
  788. }
  789. void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
  790. append(GDScriptFunction::OPCODE_STORE_GLOBAL, 1);
  791. append(p_dst);
  792. append(p_global_index);
  793. }
  794. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  795. append(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL, 1);
  796. append(p_dst);
  797. append(p_global);
  798. }
  799. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  800. int index = 0;
  801. switch (p_type.kind) {
  802. case GDScriptDataType::BUILTIN: {
  803. append(GDScriptFunction::OPCODE_CAST_TO_BUILTIN, 2);
  804. index = p_type.builtin_type;
  805. } break;
  806. case GDScriptDataType::NATIVE: {
  807. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  808. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  809. append(GDScriptFunction::OPCODE_CAST_TO_NATIVE, 3);
  810. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  811. } break;
  812. case GDScriptDataType::SCRIPT:
  813. case GDScriptDataType::GDSCRIPT: {
  814. Variant script = p_type.script_type;
  815. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  816. append(GDScriptFunction::OPCODE_CAST_TO_SCRIPT, 3);
  817. index = idx;
  818. } break;
  819. default: {
  820. return;
  821. }
  822. }
  823. append(p_source);
  824. append(p_target);
  825. append(index);
  826. }
  827. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  828. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  829. for (int i = 0; i < p_arguments.size(); i++) {
  830. append(p_arguments[i]);
  831. }
  832. append(p_base);
  833. append(p_target);
  834. append(p_arguments.size());
  835. append(p_function_name);
  836. }
  837. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  838. append(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  839. for (int i = 0; i < p_arguments.size(); i++) {
  840. append(p_arguments[i]);
  841. }
  842. append(p_target);
  843. append(p_arguments.size());
  844. append(p_function_name);
  845. }
  846. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  847. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  848. for (int i = 0; i < p_arguments.size(); i++) {
  849. append(p_arguments[i]);
  850. }
  851. append(p_base);
  852. append(p_target);
  853. append(p_arguments.size());
  854. append(p_function_name);
  855. }
  856. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, GDScriptUtilityFunctions::FunctionPtr p_function, const Vector<Address> &p_arguments) {
  857. append(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  858. for (int i = 0; i < p_arguments.size(); i++) {
  859. append(p_arguments[i]);
  860. }
  861. append(p_target);
  862. append(p_arguments.size());
  863. append(p_function);
  864. }
  865. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  866. bool is_validated = true;
  867. if (Variant::is_utility_function_vararg(p_function)) {
  868. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  869. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  870. bool all_types_exact = true;
  871. for (int i = 0; i < p_arguments.size(); i++) {
  872. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  873. all_types_exact = false;
  874. break;
  875. }
  876. }
  877. is_validated = all_types_exact;
  878. }
  879. if (is_validated) {
  880. append(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  881. for (int i = 0; i < p_arguments.size(); i++) {
  882. append(p_arguments[i]);
  883. }
  884. append(p_target);
  885. append(p_arguments.size());
  886. append(Variant::get_validated_utility_function(p_function));
  887. } else {
  888. append(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  889. for (int i = 0; i < p_arguments.size(); i++) {
  890. append(p_arguments[i]);
  891. }
  892. append(p_target);
  893. append(p_arguments.size());
  894. append(p_function);
  895. }
  896. }
  897. 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) {
  898. bool is_validated = false;
  899. // Check if all types are correct.
  900. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  901. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  902. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  903. bool all_types_exact = true;
  904. for (int i = 0; i < p_arguments.size(); i++) {
  905. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  906. all_types_exact = false;
  907. break;
  908. }
  909. }
  910. is_validated = all_types_exact;
  911. }
  912. if (!is_validated) {
  913. // Perform regular call.
  914. write_call(p_target, p_base, p_method, p_arguments);
  915. return;
  916. }
  917. if (p_target.mode == Address::TEMPORARY) {
  918. Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
  919. Variant::Type temp_type = temporaries[p_target.address].type;
  920. if (result_type != temp_type) {
  921. write_type_adjust(p_target, result_type);
  922. }
  923. }
  924. append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  925. for (int i = 0; i < p_arguments.size(); i++) {
  926. append(p_arguments[i]);
  927. }
  928. append(p_base);
  929. append(p_target);
  930. append(p_arguments.size());
  931. append(Variant::get_validated_builtin_method(p_type, p_method));
  932. }
  933. void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  934. bool is_validated = false;
  935. // Check if all types are correct.
  936. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  937. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  938. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  939. bool all_types_exact = true;
  940. for (int i = 0; i < p_arguments.size(); i++) {
  941. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  942. all_types_exact = false;
  943. break;
  944. }
  945. }
  946. is_validated = all_types_exact;
  947. }
  948. if (!is_validated) {
  949. // Perform regular call.
  950. append(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
  951. for (int i = 0; i < p_arguments.size(); i++) {
  952. append(p_arguments[i]);
  953. }
  954. append(p_target);
  955. append(p_type);
  956. append(p_method);
  957. append(p_arguments.size());
  958. return;
  959. }
  960. if (p_target.mode == Address::TEMPORARY) {
  961. Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
  962. Variant::Type temp_type = temporaries[p_target.address].type;
  963. if (result_type != temp_type) {
  964. write_type_adjust(p_target, result_type);
  965. }
  966. }
  967. append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  968. for (int i = 0; i < p_arguments.size(); i++) {
  969. append(p_arguments[i]);
  970. }
  971. append(Address()); // No base since it's static.
  972. append(p_target);
  973. append(p_arguments.size());
  974. append(Variant::get_validated_builtin_method(p_type, p_method));
  975. }
  976. void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
  977. bool is_validated = false;
  978. MethodBind *method = ClassDB::get_method(p_class, p_method);
  979. if (!is_validated) {
  980. // Perform regular call.
  981. append(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
  982. for (int i = 0; i < p_arguments.size(); i++) {
  983. append(p_arguments[i]);
  984. }
  985. append(p_target);
  986. append(method);
  987. append(p_arguments.size());
  988. return;
  989. }
  990. }
  991. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  992. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  993. for (int i = 0; i < p_arguments.size(); i++) {
  994. append(p_arguments[i]);
  995. }
  996. append(p_base);
  997. append(p_target);
  998. append(p_arguments.size());
  999. append(p_method);
  1000. }
  1001. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1002. #define CASE_TYPE(m_type) \
  1003. case Variant::m_type: \
  1004. append(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  1005. break
  1006. bool is_ptrcall = true;
  1007. if (p_method->has_return()) {
  1008. MethodInfo info;
  1009. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  1010. switch (info.return_val.type) {
  1011. CASE_TYPE(BOOL);
  1012. CASE_TYPE(INT);
  1013. CASE_TYPE(FLOAT);
  1014. CASE_TYPE(STRING);
  1015. CASE_TYPE(VECTOR2);
  1016. CASE_TYPE(VECTOR2I);
  1017. CASE_TYPE(RECT2);
  1018. CASE_TYPE(RECT2I);
  1019. CASE_TYPE(VECTOR3);
  1020. CASE_TYPE(VECTOR3I);
  1021. CASE_TYPE(TRANSFORM2D);
  1022. CASE_TYPE(PLANE);
  1023. CASE_TYPE(AABB);
  1024. CASE_TYPE(BASIS);
  1025. CASE_TYPE(TRANSFORM3D);
  1026. CASE_TYPE(COLOR);
  1027. CASE_TYPE(STRING_NAME);
  1028. CASE_TYPE(NODE_PATH);
  1029. CASE_TYPE(RID);
  1030. CASE_TYPE(QUATERNION);
  1031. CASE_TYPE(OBJECT);
  1032. CASE_TYPE(CALLABLE);
  1033. CASE_TYPE(SIGNAL);
  1034. CASE_TYPE(DICTIONARY);
  1035. CASE_TYPE(ARRAY);
  1036. CASE_TYPE(PACKED_BYTE_ARRAY);
  1037. CASE_TYPE(PACKED_INT32_ARRAY);
  1038. CASE_TYPE(PACKED_INT64_ARRAY);
  1039. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  1040. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  1041. CASE_TYPE(PACKED_STRING_ARRAY);
  1042. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  1043. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  1044. CASE_TYPE(PACKED_COLOR_ARRAY);
  1045. default:
  1046. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1047. is_ptrcall = false;
  1048. break;
  1049. }
  1050. } else {
  1051. append(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  1052. }
  1053. for (int i = 0; i < p_arguments.size(); i++) {
  1054. append(p_arguments[i]);
  1055. }
  1056. append(p_base);
  1057. append(p_target);
  1058. append(p_arguments.size());
  1059. append(p_method);
  1060. if (is_ptrcall) {
  1061. alloc_ptrcall(p_arguments.size());
  1062. }
  1063. #undef CASE_TYPE
  1064. }
  1065. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1066. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1067. for (int i = 0; i < p_arguments.size(); i++) {
  1068. append(p_arguments[i]);
  1069. }
  1070. append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1071. append(p_target);
  1072. append(p_arguments.size());
  1073. append(p_function_name);
  1074. }
  1075. void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1076. append(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  1077. for (int i = 0; i < p_arguments.size(); i++) {
  1078. append(p_arguments[i]);
  1079. }
  1080. append(GDScriptFunction::ADDR_SELF);
  1081. append(p_target);
  1082. append(p_arguments.size());
  1083. append(p_function_name);
  1084. }
  1085. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1086. append(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1087. for (int i = 0; i < p_arguments.size(); i++) {
  1088. append(p_arguments[i]);
  1089. }
  1090. append(p_base);
  1091. append(p_target);
  1092. append(p_arguments.size());
  1093. append(p_function_name);
  1094. }
  1095. void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures) {
  1096. append(GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
  1097. for (int i = 0; i < p_captures.size(); i++) {
  1098. append(p_captures[i]);
  1099. }
  1100. append(p_target);
  1101. append(p_captures.size());
  1102. append(p_function);
  1103. }
  1104. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  1105. // Try to find an appropriate constructor.
  1106. bool all_have_type = true;
  1107. Vector<Variant::Type> arg_types;
  1108. for (int i = 0; i < p_arguments.size(); i++) {
  1109. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  1110. all_have_type = false;
  1111. break;
  1112. }
  1113. arg_types.push_back(p_arguments[i].type.builtin_type);
  1114. }
  1115. if (all_have_type) {
  1116. int valid_constructor = -1;
  1117. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  1118. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  1119. continue;
  1120. }
  1121. int types_correct = true;
  1122. for (int j = 0; j < arg_types.size(); j++) {
  1123. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  1124. types_correct = false;
  1125. break;
  1126. }
  1127. }
  1128. if (types_correct) {
  1129. valid_constructor = i;
  1130. break;
  1131. }
  1132. }
  1133. if (valid_constructor >= 0) {
  1134. append(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  1135. for (int i = 0; i < p_arguments.size(); i++) {
  1136. append(p_arguments[i]);
  1137. }
  1138. append(p_target);
  1139. append(p_arguments.size());
  1140. append(Variant::get_validated_constructor(p_type, valid_constructor));
  1141. return;
  1142. }
  1143. }
  1144. append(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  1145. for (int i = 0; i < p_arguments.size(); i++) {
  1146. append(p_arguments[i]);
  1147. }
  1148. append(p_target);
  1149. append(p_arguments.size());
  1150. append(p_type);
  1151. }
  1152. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  1153. append(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  1154. for (int i = 0; i < p_arguments.size(); i++) {
  1155. append(p_arguments[i]);
  1156. }
  1157. append(p_target);
  1158. append(p_arguments.size());
  1159. }
  1160. void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
  1161. append(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
  1162. for (int i = 0; i < p_arguments.size(); i++) {
  1163. append(p_arguments[i]);
  1164. }
  1165. append(p_target);
  1166. if (p_element_type.script_type) {
  1167. Variant script_type = Ref<Script>(p_element_type.script_type);
  1168. int addr = get_constant_pos(script_type);
  1169. addr |= GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS;
  1170. append(addr);
  1171. } else {
  1172. append(Address()); // null.
  1173. }
  1174. append(p_arguments.size());
  1175. append(p_element_type.builtin_type);
  1176. append(p_element_type.native_type);
  1177. }
  1178. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  1179. append(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  1180. for (int i = 0; i < p_arguments.size(); i++) {
  1181. append(p_arguments[i]);
  1182. }
  1183. append(p_target);
  1184. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  1185. }
  1186. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  1187. append(GDScriptFunction::OPCODE_AWAIT, 1);
  1188. append(p_operand);
  1189. append(GDScriptFunction::OPCODE_AWAIT_RESUME, 1);
  1190. append(p_target);
  1191. }
  1192. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  1193. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  1194. append(p_condition);
  1195. if_jmp_addrs.push_back(opcodes.size());
  1196. append(0); // Jump destination, will be patched.
  1197. }
  1198. void GDScriptByteCodeGenerator::write_else() {
  1199. append(GDScriptFunction::OPCODE_JUMP, 0); // Jump from true if block;
  1200. int else_jmp_addr = opcodes.size();
  1201. append(0); // Jump destination, will be patched.
  1202. patch_jump(if_jmp_addrs.back()->get());
  1203. if_jmp_addrs.pop_back();
  1204. if_jmp_addrs.push_back(else_jmp_addr);
  1205. }
  1206. void GDScriptByteCodeGenerator::write_endif() {
  1207. patch_jump(if_jmp_addrs.back()->get());
  1208. if_jmp_addrs.pop_back();
  1209. }
  1210. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  1211. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  1212. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1213. // Store state.
  1214. for_counter_variables.push_back(counter);
  1215. for_container_variables.push_back(container);
  1216. }
  1217. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  1218. const Address &container = for_container_variables.back()->get();
  1219. // Assign container.
  1220. append(GDScriptFunction::OPCODE_ASSIGN, 2);
  1221. append(container);
  1222. append(p_list);
  1223. for_iterator_variables.push_back(p_variable);
  1224. }
  1225. void GDScriptByteCodeGenerator::write_for() {
  1226. const Address &iterator = for_iterator_variables.back()->get();
  1227. const Address &counter = for_counter_variables.back()->get();
  1228. const Address &container = for_container_variables.back()->get();
  1229. current_breaks_to_patch.push_back(List<int>());
  1230. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1231. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1232. if (container.type.has_type) {
  1233. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1234. switch (container.type.builtin_type) {
  1235. case Variant::INT:
  1236. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1237. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1238. break;
  1239. case Variant::FLOAT:
  1240. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1241. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1242. break;
  1243. case Variant::VECTOR2:
  1244. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1245. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1246. break;
  1247. case Variant::VECTOR2I:
  1248. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1249. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1250. break;
  1251. case Variant::VECTOR3:
  1252. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1253. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1254. break;
  1255. case Variant::VECTOR3I:
  1256. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1257. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1258. break;
  1259. case Variant::STRING:
  1260. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1261. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1262. break;
  1263. case Variant::DICTIONARY:
  1264. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1265. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1266. break;
  1267. case Variant::ARRAY:
  1268. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1269. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1270. break;
  1271. case Variant::PACKED_BYTE_ARRAY:
  1272. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1273. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1274. break;
  1275. case Variant::PACKED_INT32_ARRAY:
  1276. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1277. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1278. break;
  1279. case Variant::PACKED_INT64_ARRAY:
  1280. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1281. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1282. break;
  1283. case Variant::PACKED_FLOAT32_ARRAY:
  1284. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1285. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1286. break;
  1287. case Variant::PACKED_FLOAT64_ARRAY:
  1288. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1289. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1290. break;
  1291. case Variant::PACKED_STRING_ARRAY:
  1292. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1293. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1294. break;
  1295. case Variant::PACKED_VECTOR2_ARRAY:
  1296. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1297. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1298. break;
  1299. case Variant::PACKED_VECTOR3_ARRAY:
  1300. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1301. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1302. break;
  1303. case Variant::PACKED_COLOR_ARRAY:
  1304. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1305. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1306. break;
  1307. default:
  1308. break;
  1309. }
  1310. } else {
  1311. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1312. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1313. }
  1314. }
  1315. // Begin loop.
  1316. append(begin_opcode, 3);
  1317. append(counter);
  1318. append(container);
  1319. append(iterator);
  1320. for_jmp_addrs.push_back(opcodes.size());
  1321. append(0); // End of loop address, will be patched.
  1322. append(GDScriptFunction::OPCODE_JUMP, 0);
  1323. append(opcodes.size() + 6); // Skip over 'continue' code.
  1324. // Next iteration.
  1325. int continue_addr = opcodes.size();
  1326. continue_addrs.push_back(continue_addr);
  1327. append(iterate_opcode, 3);
  1328. append(counter);
  1329. append(container);
  1330. append(iterator);
  1331. for_jmp_addrs.push_back(opcodes.size());
  1332. append(0); // Jump destination, will be patched.
  1333. }
  1334. void GDScriptByteCodeGenerator::write_endfor() {
  1335. // Jump back to loop check.
  1336. append(GDScriptFunction::OPCODE_JUMP, 0);
  1337. append(continue_addrs.back()->get());
  1338. continue_addrs.pop_back();
  1339. // Patch end jumps (two of them).
  1340. for (int i = 0; i < 2; i++) {
  1341. patch_jump(for_jmp_addrs.back()->get());
  1342. for_jmp_addrs.pop_back();
  1343. }
  1344. // Patch break statements.
  1345. for (const int &E : current_breaks_to_patch.back()->get()) {
  1346. patch_jump(E);
  1347. }
  1348. current_breaks_to_patch.pop_back();
  1349. // Pop state.
  1350. for_iterator_variables.pop_back();
  1351. for_counter_variables.pop_back();
  1352. for_container_variables.pop_back();
  1353. }
  1354. void GDScriptByteCodeGenerator::start_while_condition() {
  1355. current_breaks_to_patch.push_back(List<int>());
  1356. continue_addrs.push_back(opcodes.size());
  1357. }
  1358. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1359. // Condition check.
  1360. append(GDScriptFunction::OPCODE_JUMP_IF_NOT, 1);
  1361. append(p_condition);
  1362. while_jmp_addrs.push_back(opcodes.size());
  1363. append(0); // End of loop address, will be patched.
  1364. }
  1365. void GDScriptByteCodeGenerator::write_endwhile() {
  1366. // Jump back to loop check.
  1367. append(GDScriptFunction::OPCODE_JUMP, 0);
  1368. append(continue_addrs.back()->get());
  1369. continue_addrs.pop_back();
  1370. // Patch end jump.
  1371. patch_jump(while_jmp_addrs.back()->get());
  1372. while_jmp_addrs.pop_back();
  1373. // Patch break statements.
  1374. for (const int &E : current_breaks_to_patch.back()->get()) {
  1375. patch_jump(E);
  1376. }
  1377. current_breaks_to_patch.pop_back();
  1378. }
  1379. void GDScriptByteCodeGenerator::start_match() {
  1380. match_continues_to_patch.push_back(List<int>());
  1381. }
  1382. void GDScriptByteCodeGenerator::start_match_branch() {
  1383. // Patch continue statements.
  1384. for (const int &E : match_continues_to_patch.back()->get()) {
  1385. patch_jump(E);
  1386. }
  1387. match_continues_to_patch.pop_back();
  1388. // Start a new list for next branch.
  1389. match_continues_to_patch.push_back(List<int>());
  1390. }
  1391. void GDScriptByteCodeGenerator::end_match() {
  1392. // Patch continue statements.
  1393. for (const int &E : match_continues_to_patch.back()->get()) {
  1394. patch_jump(E);
  1395. }
  1396. match_continues_to_patch.pop_back();
  1397. }
  1398. void GDScriptByteCodeGenerator::write_break() {
  1399. append(GDScriptFunction::OPCODE_JUMP, 0);
  1400. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1401. append(0);
  1402. }
  1403. void GDScriptByteCodeGenerator::write_continue() {
  1404. append(GDScriptFunction::OPCODE_JUMP, 0);
  1405. append(continue_addrs.back()->get());
  1406. }
  1407. void GDScriptByteCodeGenerator::write_continue_match() {
  1408. append(GDScriptFunction::OPCODE_JUMP, 0);
  1409. match_continues_to_patch.back()->get().push_back(opcodes.size());
  1410. append(0);
  1411. }
  1412. void GDScriptByteCodeGenerator::write_breakpoint() {
  1413. append(GDScriptFunction::OPCODE_BREAKPOINT, 0);
  1414. }
  1415. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1416. append(GDScriptFunction::OPCODE_LINE, 0);
  1417. append(p_line);
  1418. current_line = p_line;
  1419. }
  1420. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1421. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1422. // Either the function is untyped or the return value is also typed.
  1423. // If this is a typed function, then we need to check for potential conversions.
  1424. if (function->return_type.has_type) {
  1425. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1426. // Typed array.
  1427. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1428. Variant script = function->return_type.script_type;
  1429. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1430. append(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY, 2);
  1431. append(p_return_value);
  1432. append(script_idx);
  1433. append(element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT);
  1434. append(element_type.native_type);
  1435. } else if (function->return_type.kind == GDScriptDataType::BUILTIN && p_return_value.type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type != p_return_value.type.builtin_type) {
  1436. // Add conversion.
  1437. append(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN, 1);
  1438. append(p_return_value);
  1439. append(function->return_type.builtin_type);
  1440. } else {
  1441. // Just assign.
  1442. append(GDScriptFunction::OPCODE_RETURN, 1);
  1443. append(p_return_value);
  1444. }
  1445. } else {
  1446. append(GDScriptFunction::OPCODE_RETURN, 1);
  1447. append(p_return_value);
  1448. }
  1449. } else {
  1450. switch (function->return_type.kind) {
  1451. case GDScriptDataType::BUILTIN: {
  1452. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1453. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1454. Variant script = function->return_type.script_type;
  1455. int script_idx = get_constant_pos(script);
  1456. script_idx |= (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1457. append(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY, 2);
  1458. append(p_return_value);
  1459. append(script_idx);
  1460. append(element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT);
  1461. append(element_type.native_type);
  1462. } else {
  1463. append(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN, 1);
  1464. append(p_return_value);
  1465. append(function->return_type.builtin_type);
  1466. }
  1467. } break;
  1468. case GDScriptDataType::NATIVE: {
  1469. append(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE, 2);
  1470. append(p_return_value);
  1471. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1472. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1473. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1474. append(class_idx);
  1475. } break;
  1476. case GDScriptDataType::GDSCRIPT:
  1477. case GDScriptDataType::SCRIPT: {
  1478. Variant script = function->return_type.script_type;
  1479. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1480. append(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT, 2);
  1481. append(p_return_value);
  1482. append(script_idx);
  1483. } break;
  1484. default: {
  1485. ERR_PRINT("Compiler bug: unresolved return.");
  1486. // Shouldn't get here, but fail-safe to a regular return;
  1487. append(GDScriptFunction::OPCODE_RETURN, 1);
  1488. append(p_return_value);
  1489. } break;
  1490. }
  1491. }
  1492. }
  1493. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1494. append(GDScriptFunction::OPCODE_ASSERT, 2);
  1495. append(p_test);
  1496. append(p_message);
  1497. }
  1498. void GDScriptByteCodeGenerator::start_block() {
  1499. push_stack_identifiers();
  1500. }
  1501. void GDScriptByteCodeGenerator::end_block() {
  1502. pop_stack_identifiers();
  1503. }
  1504. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1505. if (!ended && function != nullptr) {
  1506. memdelete(function);
  1507. }
  1508. }