gdscript_byte_codegen.cpp 73 KB

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