gdscript_byte_codegen.cpp 73 KB

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