gdscript_byte_codegen.cpp 75 KB

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