gdscript_byte_codegen.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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.has_type && 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.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  382. #define IS_BUILTIN_TYPE(m_var, m_type) \
  383. (m_var.type.has_type && 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. valid = p_right_operand.type.builtin_type != Variant::INT;
  540. break;
  541. case Variant::VECTOR2I:
  542. case Variant::VECTOR3I:
  543. case Variant::VECTOR4I:
  544. valid = p_right_operand.type.builtin_type != Variant::INT && p_right_operand.type.builtin_type != p_left_operand.type.builtin_type;
  545. break;
  546. default:
  547. break;
  548. }
  549. }
  550. if (valid) {
  551. if (p_target.mode == Address::TEMPORARY) {
  552. Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  553. Variant::Type temp_type = temporaries[p_target.address].type;
  554. if (result_type != temp_type) {
  555. write_type_adjust(p_target, result_type);
  556. }
  557. }
  558. // Gather specific operator.
  559. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  560. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  561. append(p_left_operand);
  562. append(p_right_operand);
  563. append(p_target);
  564. append(op_func);
  565. #ifdef DEBUG_ENABLED
  566. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  567. #endif
  568. return;
  569. }
  570. // No specific types, perform variant evaluation.
  571. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  572. append(p_left_operand);
  573. append(p_right_operand);
  574. append(p_target);
  575. append(p_operator);
  576. append(0); // Signature storage.
  577. append(0); // Return type storage.
  578. constexpr int _pointer_size = sizeof(Variant::ValidatedOperatorEvaluator) / sizeof(*(opcodes.ptr()));
  579. for (int i = 0; i < _pointer_size; i++) {
  580. append(0); // Space for function pointer.
  581. }
  582. }
  583. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  584. switch (p_type.kind) {
  585. case GDScriptDataType::BUILTIN: {
  586. if (p_type.builtin_type == Variant::ARRAY && p_type.has_container_element_type(0)) {
  587. const GDScriptDataType &element_type = p_type.get_container_element_type(0);
  588. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_ARRAY);
  589. append(p_target);
  590. append(p_source);
  591. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  592. append(element_type.builtin_type);
  593. append(element_type.native_type);
  594. } else if (p_type.builtin_type == Variant::DICTIONARY && p_type.has_container_element_types()) {
  595. const GDScriptDataType &key_element_type = p_type.get_container_element_type_or_variant(0);
  596. const GDScriptDataType &value_element_type = p_type.get_container_element_type_or_variant(1);
  597. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_DICTIONARY);
  598. append(p_target);
  599. append(p_source);
  600. append(get_constant_pos(key_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  601. append(get_constant_pos(value_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  602. append(key_element_type.builtin_type);
  603. append(key_element_type.native_type);
  604. append(value_element_type.builtin_type);
  605. append(value_element_type.native_type);
  606. } else {
  607. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_BUILTIN);
  608. append(p_target);
  609. append(p_source);
  610. append(p_type.builtin_type);
  611. }
  612. } break;
  613. case GDScriptDataType::NATIVE: {
  614. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_NATIVE);
  615. append(p_target);
  616. append(p_source);
  617. append(p_type.native_type);
  618. } break;
  619. case GDScriptDataType::SCRIPT:
  620. case GDScriptDataType::GDSCRIPT: {
  621. const Variant &script = p_type.script_type;
  622. append_opcode(GDScriptFunction::OPCODE_TYPE_TEST_SCRIPT);
  623. append(p_target);
  624. append(p_source);
  625. append(get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  626. } break;
  627. default: {
  628. ERR_PRINT("Compiler bug: unresolved type in type test.");
  629. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  630. append(p_target);
  631. }
  632. }
  633. }
  634. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  635. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  636. append(p_left_operand);
  637. logic_op_jump_pos1.push_back(opcodes.size());
  638. append(0); // Jump target, will be patched.
  639. }
  640. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  641. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  642. append(p_right_operand);
  643. logic_op_jump_pos2.push_back(opcodes.size());
  644. append(0); // Jump target, will be patched.
  645. }
  646. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  647. // If here means both operands are true.
  648. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  649. append(p_target);
  650. // Jump away from the fail condition.
  651. append_opcode(GDScriptFunction::OPCODE_JUMP);
  652. append(opcodes.size() + 3);
  653. // Here it means one of operands is false.
  654. patch_jump(logic_op_jump_pos1.back()->get());
  655. patch_jump(logic_op_jump_pos2.back()->get());
  656. logic_op_jump_pos1.pop_back();
  657. logic_op_jump_pos2.pop_back();
  658. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  659. append(p_target);
  660. }
  661. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  662. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  663. append(p_left_operand);
  664. logic_op_jump_pos1.push_back(opcodes.size());
  665. append(0); // Jump target, will be patched.
  666. }
  667. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  668. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  669. append(p_right_operand);
  670. logic_op_jump_pos2.push_back(opcodes.size());
  671. append(0); // Jump target, will be patched.
  672. }
  673. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  674. // If here means both operands are false.
  675. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  676. append(p_target);
  677. // Jump away from the success condition.
  678. append_opcode(GDScriptFunction::OPCODE_JUMP);
  679. append(opcodes.size() + 3);
  680. // Here it means one of operands is true.
  681. patch_jump(logic_op_jump_pos1.back()->get());
  682. patch_jump(logic_op_jump_pos2.back()->get());
  683. logic_op_jump_pos1.pop_back();
  684. logic_op_jump_pos2.pop_back();
  685. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  686. append(p_target);
  687. }
  688. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  689. ternary_result.push_back(p_target);
  690. }
  691. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  692. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  693. append(p_condition);
  694. ternary_jump_fail_pos.push_back(opcodes.size());
  695. append(0); // Jump target, will be patched.
  696. }
  697. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  698. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  699. append(ternary_result.back()->get());
  700. append(p_expr);
  701. // Jump away from the false path.
  702. append_opcode(GDScriptFunction::OPCODE_JUMP);
  703. ternary_jump_skip_pos.push_back(opcodes.size());
  704. append(0);
  705. // Fail must jump here.
  706. patch_jump(ternary_jump_fail_pos.back()->get());
  707. ternary_jump_fail_pos.pop_back();
  708. }
  709. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  710. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  711. append(ternary_result.back()->get());
  712. append(p_expr);
  713. }
  714. void GDScriptByteCodeGenerator::write_end_ternary() {
  715. patch_jump(ternary_jump_skip_pos.back()->get());
  716. ternary_jump_skip_pos.pop_back();
  717. ternary_result.pop_back();
  718. }
  719. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  720. if (HAS_BUILTIN_TYPE(p_target)) {
  721. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
  722. IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
  723. // Use indexed setter instead.
  724. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  725. append_opcode(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED);
  726. append(p_target);
  727. append(p_index);
  728. append(p_source);
  729. append(setter);
  730. return;
  731. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  732. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  733. append_opcode(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED);
  734. append(p_target);
  735. append(p_index);
  736. append(p_source);
  737. append(setter);
  738. return;
  739. }
  740. }
  741. append_opcode(GDScriptFunction::OPCODE_SET_KEYED);
  742. append(p_target);
  743. append(p_index);
  744. append(p_source);
  745. }
  746. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  747. if (HAS_BUILTIN_TYPE(p_source)) {
  748. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  749. // Use indexed getter instead.
  750. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  751. append_opcode(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED);
  752. append(p_source);
  753. append(p_index);
  754. append(p_target);
  755. append(getter);
  756. return;
  757. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  758. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  759. append_opcode(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED);
  760. append(p_source);
  761. append(p_index);
  762. append(p_target);
  763. append(getter);
  764. return;
  765. }
  766. }
  767. append_opcode(GDScriptFunction::OPCODE_GET_KEYED);
  768. append(p_source);
  769. append(p_index);
  770. append(p_target);
  771. }
  772. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  773. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
  774. IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
  775. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  776. append_opcode(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED);
  777. append(p_target);
  778. append(p_source);
  779. append(setter);
  780. #ifdef DEBUG_ENABLED
  781. add_debug_name(setter_names, get_setter_pos(setter), p_name);
  782. #endif
  783. return;
  784. }
  785. append_opcode(GDScriptFunction::OPCODE_SET_NAMED);
  786. append(p_target);
  787. append(p_source);
  788. append(p_name);
  789. }
  790. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  791. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  792. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  793. append_opcode(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED);
  794. append(p_source);
  795. append(p_target);
  796. append(getter);
  797. #ifdef DEBUG_ENABLED
  798. add_debug_name(getter_names, get_getter_pos(getter), p_name);
  799. #endif
  800. return;
  801. }
  802. append_opcode(GDScriptFunction::OPCODE_GET_NAMED);
  803. append(p_source);
  804. append(p_target);
  805. append(p_name);
  806. }
  807. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  808. append_opcode(GDScriptFunction::OPCODE_SET_MEMBER);
  809. append(p_value);
  810. append(p_name);
  811. }
  812. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  813. append_opcode(GDScriptFunction::OPCODE_GET_MEMBER);
  814. append(p_target);
  815. append(p_name);
  816. }
  817. void GDScriptByteCodeGenerator::write_set_static_variable(const Address &p_value, const Address &p_class, int p_index) {
  818. append_opcode(GDScriptFunction::OPCODE_SET_STATIC_VARIABLE);
  819. append(p_value);
  820. append(p_class);
  821. append(p_index);
  822. }
  823. void GDScriptByteCodeGenerator::write_get_static_variable(const Address &p_target, const Address &p_class, int p_index) {
  824. append_opcode(GDScriptFunction::OPCODE_GET_STATIC_VARIABLE);
  825. append(p_target);
  826. append(p_class);
  827. append(p_index);
  828. }
  829. void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
  830. switch (p_target.type.kind) {
  831. case GDScriptDataType::BUILTIN: {
  832. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type(0)) {
  833. const GDScriptDataType &element_type = p_target.type.get_container_element_type(0);
  834. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  835. append(p_target);
  836. append(p_source);
  837. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  838. append(element_type.builtin_type);
  839. append(element_type.native_type);
  840. } else if (p_target.type.builtin_type == Variant::DICTIONARY && p_target.type.has_container_element_types()) {
  841. const GDScriptDataType &key_type = p_target.type.get_container_element_type_or_variant(0);
  842. const GDScriptDataType &value_type = p_target.type.get_container_element_type_or_variant(1);
  843. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_DICTIONARY);
  844. append(p_target);
  845. append(p_source);
  846. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  847. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  848. append(key_type.builtin_type);
  849. append(key_type.native_type);
  850. append(value_type.builtin_type);
  851. append(value_type.native_type);
  852. } else {
  853. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  854. append(p_target);
  855. append(p_source);
  856. append(p_target.type.builtin_type);
  857. }
  858. } break;
  859. case GDScriptDataType::NATIVE: {
  860. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  861. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  862. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  863. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
  864. append(p_target);
  865. append(p_source);
  866. append(class_idx);
  867. } break;
  868. case GDScriptDataType::SCRIPT:
  869. case GDScriptDataType::GDSCRIPT: {
  870. Variant script = p_target.type.script_type;
  871. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  872. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
  873. append(p_target);
  874. append(p_source);
  875. append(idx);
  876. } break;
  877. default: {
  878. ERR_PRINT("Compiler bug: unresolved assign.");
  879. // Shouldn't get here, but fail-safe to a regular assignment
  880. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  881. append(p_target);
  882. append(p_source);
  883. }
  884. }
  885. }
  886. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  887. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type(0)) {
  888. const GDScriptDataType &element_type = p_target.type.get_container_element_type(0);
  889. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  890. append(p_target);
  891. append(p_source);
  892. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  893. append(element_type.builtin_type);
  894. append(element_type.native_type);
  895. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::DICTIONARY && p_target.type.has_container_element_types()) {
  896. const GDScriptDataType &key_type = p_target.type.get_container_element_type_or_variant(0);
  897. const GDScriptDataType &value_type = p_target.type.get_container_element_type_or_variant(1);
  898. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_DICTIONARY);
  899. append(p_target);
  900. append(p_source);
  901. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  902. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  903. append(key_type.builtin_type);
  904. append(key_type.native_type);
  905. append(value_type.builtin_type);
  906. append(value_type.native_type);
  907. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  908. // Need conversion.
  909. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  910. append(p_target);
  911. append(p_source);
  912. append(p_target.type.builtin_type);
  913. } else {
  914. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  915. append(p_target);
  916. append(p_source);
  917. }
  918. }
  919. void GDScriptByteCodeGenerator::write_assign_null(const Address &p_target) {
  920. append_opcode(GDScriptFunction::OPCODE_ASSIGN_NULL);
  921. append(p_target);
  922. }
  923. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  924. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  925. append(p_target);
  926. }
  927. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  928. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  929. append(p_target);
  930. }
  931. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src, bool p_use_conversion) {
  932. if (p_use_conversion) {
  933. write_assign_with_conversion(p_dst, p_src);
  934. } else {
  935. write_assign(p_dst, p_src);
  936. }
  937. function->default_arguments.push_back(opcodes.size());
  938. }
  939. void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
  940. append_opcode(GDScriptFunction::OPCODE_STORE_GLOBAL);
  941. append(p_dst);
  942. append(p_global_index);
  943. }
  944. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  945. append_opcode(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL);
  946. append(p_dst);
  947. append(p_global);
  948. }
  949. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  950. int index = 0;
  951. switch (p_type.kind) {
  952. case GDScriptDataType::BUILTIN: {
  953. append_opcode(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  954. index = p_type.builtin_type;
  955. } break;
  956. case GDScriptDataType::NATIVE: {
  957. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  958. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  959. append_opcode(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
  960. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  961. } break;
  962. case GDScriptDataType::SCRIPT:
  963. case GDScriptDataType::GDSCRIPT: {
  964. Variant script = p_type.script_type;
  965. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  966. append_opcode(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
  967. index = idx;
  968. } break;
  969. default: {
  970. return;
  971. }
  972. }
  973. append(p_source);
  974. append(p_target);
  975. append(index);
  976. }
  977. GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target(const GDScriptCodeGenerator::Address &p_target, Variant::Type p_type) {
  978. if (p_target.mode == Address::NIL) {
  979. GDScriptDataType type;
  980. if (p_type != Variant::NIL) {
  981. type.has_type = true;
  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.has_type = true;
  1410. int_type.kind = GDScriptDataType::BUILTIN;
  1411. int_type.builtin_type = Variant::INT;
  1412. Address range_from(Address::LOCAL_VARIABLE, add_local("@range_from", int_type), int_type);
  1413. Address range_to(Address::LOCAL_VARIABLE, add_local("@range_to", int_type), int_type);
  1414. Address range_step(Address::LOCAL_VARIABLE, add_local("@range_step", int_type), int_type);
  1415. // Store state.
  1416. for_range_from_variables.push_back(range_from);
  1417. for_range_to_variables.push_back(range_to);
  1418. for_range_step_variables.push_back(range_step);
  1419. } else {
  1420. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1421. // Store state.
  1422. for_container_variables.push_back(container);
  1423. }
  1424. }
  1425. void GDScriptByteCodeGenerator::write_for_list_assignment(const Address &p_list) {
  1426. const Address &container = for_container_variables.back()->get();
  1427. // Assign container.
  1428. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  1429. append(container);
  1430. append(p_list);
  1431. }
  1432. void GDScriptByteCodeGenerator::write_for_range_assignment(const Address &p_from, const Address &p_to, const Address &p_step) {
  1433. const Address &range_from = for_range_from_variables.back()->get();
  1434. const Address &range_to = for_range_to_variables.back()->get();
  1435. const Address &range_step = for_range_step_variables.back()->get();
  1436. // Assign range args.
  1437. write_assign(range_from, p_from);
  1438. write_assign(range_to, p_to);
  1439. write_assign(range_step, p_step);
  1440. }
  1441. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_conversion, bool p_is_range) {
  1442. const Address &counter = for_counter_variables.back()->get();
  1443. const Address &container = p_is_range ? Address() : for_container_variables.back()->get();
  1444. const Address &range_from = p_is_range ? for_range_from_variables.back()->get() : Address();
  1445. const Address &range_to = p_is_range ? for_range_to_variables.back()->get() : Address();
  1446. const Address &range_step = p_is_range ? for_range_step_variables.back()->get() : Address();
  1447. current_breaks_to_patch.push_back(List<int>());
  1448. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1449. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1450. if (p_is_range) {
  1451. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_RANGE;
  1452. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_RANGE;
  1453. } else if (container.type.has_type) {
  1454. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1455. switch (container.type.builtin_type) {
  1456. case Variant::INT:
  1457. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1458. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1459. break;
  1460. case Variant::FLOAT:
  1461. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1462. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1463. break;
  1464. case Variant::VECTOR2:
  1465. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1466. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1467. break;
  1468. case Variant::VECTOR2I:
  1469. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1470. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1471. break;
  1472. case Variant::VECTOR3:
  1473. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1474. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1475. break;
  1476. case Variant::VECTOR3I:
  1477. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1478. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1479. break;
  1480. case Variant::STRING:
  1481. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1482. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1483. break;
  1484. case Variant::DICTIONARY:
  1485. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1486. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1487. break;
  1488. case Variant::ARRAY:
  1489. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1490. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1491. break;
  1492. case Variant::PACKED_BYTE_ARRAY:
  1493. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1494. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1495. break;
  1496. case Variant::PACKED_INT32_ARRAY:
  1497. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1498. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1499. break;
  1500. case Variant::PACKED_INT64_ARRAY:
  1501. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1502. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1503. break;
  1504. case Variant::PACKED_FLOAT32_ARRAY:
  1505. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1506. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1507. break;
  1508. case Variant::PACKED_FLOAT64_ARRAY:
  1509. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1510. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1511. break;
  1512. case Variant::PACKED_STRING_ARRAY:
  1513. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1514. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1515. break;
  1516. case Variant::PACKED_VECTOR2_ARRAY:
  1517. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1518. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1519. break;
  1520. case Variant::PACKED_VECTOR3_ARRAY:
  1521. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1522. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1523. break;
  1524. case Variant::PACKED_COLOR_ARRAY:
  1525. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1526. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1527. break;
  1528. case Variant::PACKED_VECTOR4_ARRAY:
  1529. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY;
  1530. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR4_ARRAY;
  1531. break;
  1532. default:
  1533. break;
  1534. }
  1535. } else {
  1536. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1537. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1538. }
  1539. }
  1540. Address temp;
  1541. if (p_use_conversion) {
  1542. temp = Address(Address::LOCAL_VARIABLE, add_local("@iterator_temp", GDScriptDataType()));
  1543. }
  1544. // Begin loop.
  1545. append_opcode(begin_opcode);
  1546. append(counter);
  1547. if (p_is_range) {
  1548. append(range_from);
  1549. append(range_to);
  1550. append(range_step);
  1551. } else {
  1552. append(container);
  1553. }
  1554. append(p_use_conversion ? temp : p_variable);
  1555. for_jmp_addrs.push_back(opcodes.size());
  1556. append(0); // End of loop address, will be patched.
  1557. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1558. append(opcodes.size() + (p_is_range ? 7 : 6)); // Skip over 'continue' code.
  1559. // Next iteration.
  1560. int continue_addr = opcodes.size();
  1561. continue_addrs.push_back(continue_addr);
  1562. append_opcode(iterate_opcode);
  1563. append(counter);
  1564. if (p_is_range) {
  1565. append(range_to);
  1566. append(range_step);
  1567. } else {
  1568. append(container);
  1569. }
  1570. append(p_use_conversion ? temp : p_variable);
  1571. for_jmp_addrs.push_back(opcodes.size());
  1572. append(0); // Jump destination, will be patched.
  1573. if (p_use_conversion) {
  1574. write_assign_with_conversion(p_variable, temp);
  1575. if (p_variable.type.can_contain_object()) {
  1576. clear_address(temp); // Can contain `RefCounted`, so clear it.
  1577. }
  1578. }
  1579. }
  1580. void GDScriptByteCodeGenerator::write_endfor(bool p_is_range) {
  1581. // Jump back to loop check.
  1582. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1583. append(continue_addrs.back()->get());
  1584. continue_addrs.pop_back();
  1585. // Patch end jumps (two of them).
  1586. for (int i = 0; i < 2; i++) {
  1587. patch_jump(for_jmp_addrs.back()->get());
  1588. for_jmp_addrs.pop_back();
  1589. }
  1590. // Patch break statements.
  1591. for (const int &E : current_breaks_to_patch.back()->get()) {
  1592. patch_jump(E);
  1593. }
  1594. current_breaks_to_patch.pop_back();
  1595. // Pop state.
  1596. for_counter_variables.pop_back();
  1597. if (p_is_range) {
  1598. for_range_from_variables.pop_back();
  1599. for_range_to_variables.pop_back();
  1600. for_range_step_variables.pop_back();
  1601. } else {
  1602. for_container_variables.pop_back();
  1603. }
  1604. }
  1605. void GDScriptByteCodeGenerator::start_while_condition() {
  1606. current_breaks_to_patch.push_back(List<int>());
  1607. continue_addrs.push_back(opcodes.size());
  1608. }
  1609. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1610. // Condition check.
  1611. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1612. append(p_condition);
  1613. while_jmp_addrs.push_back(opcodes.size());
  1614. append(0); // End of loop address, will be patched.
  1615. }
  1616. void GDScriptByteCodeGenerator::write_endwhile() {
  1617. // Jump back to loop check.
  1618. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1619. append(continue_addrs.back()->get());
  1620. continue_addrs.pop_back();
  1621. // Patch end jump.
  1622. patch_jump(while_jmp_addrs.back()->get());
  1623. while_jmp_addrs.pop_back();
  1624. // Patch break statements.
  1625. for (const int &E : current_breaks_to_patch.back()->get()) {
  1626. patch_jump(E);
  1627. }
  1628. current_breaks_to_patch.pop_back();
  1629. }
  1630. void GDScriptByteCodeGenerator::write_break() {
  1631. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1632. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1633. append(0);
  1634. }
  1635. void GDScriptByteCodeGenerator::write_continue() {
  1636. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1637. append(continue_addrs.back()->get());
  1638. }
  1639. void GDScriptByteCodeGenerator::write_breakpoint() {
  1640. append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
  1641. }
  1642. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1643. if (GDScriptLanguage::get_singleton()->should_track_call_stack()) {
  1644. // Add newline for debugger and stack tracking if enabled in the project settings.
  1645. append_opcode(GDScriptFunction::OPCODE_LINE);
  1646. append(p_line);
  1647. current_line = p_line;
  1648. }
  1649. }
  1650. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1651. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1652. // Either the function is untyped or the return value is also typed.
  1653. // If this is a typed function, then we need to check for potential conversions.
  1654. if (function->return_type.has_type) {
  1655. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
  1656. // Typed array.
  1657. const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
  1658. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1659. append(p_return_value);
  1660. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1661. append(element_type.builtin_type);
  1662. append(element_type.native_type);
  1663. } else if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::DICTIONARY &&
  1664. function->return_type.has_container_element_types()) {
  1665. // Typed dictionary.
  1666. const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
  1667. const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
  1668. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
  1669. append(p_return_value);
  1670. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1671. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1672. append(key_type.builtin_type);
  1673. append(key_type.native_type);
  1674. append(value_type.builtin_type);
  1675. append(value_type.native_type);
  1676. } 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) {
  1677. // Add conversion.
  1678. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1679. append(p_return_value);
  1680. append(function->return_type.builtin_type);
  1681. } else {
  1682. // Just assign.
  1683. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1684. append(p_return_value);
  1685. }
  1686. } else {
  1687. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1688. append(p_return_value);
  1689. }
  1690. } else {
  1691. switch (function->return_type.kind) {
  1692. case GDScriptDataType::BUILTIN: {
  1693. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
  1694. const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
  1695. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1696. append(p_return_value);
  1697. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1698. append(element_type.builtin_type);
  1699. append(element_type.native_type);
  1700. } else if (function->return_type.builtin_type == Variant::DICTIONARY && function->return_type.has_container_element_types()) {
  1701. const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
  1702. const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
  1703. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
  1704. append(p_return_value);
  1705. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1706. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1707. append(key_type.builtin_type);
  1708. append(key_type.native_type);
  1709. append(value_type.builtin_type);
  1710. append(value_type.native_type);
  1711. } else {
  1712. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1713. append(p_return_value);
  1714. append(function->return_type.builtin_type);
  1715. }
  1716. } break;
  1717. case GDScriptDataType::NATIVE: {
  1718. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
  1719. append(p_return_value);
  1720. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1721. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1722. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1723. append(class_idx);
  1724. } break;
  1725. case GDScriptDataType::GDSCRIPT:
  1726. case GDScriptDataType::SCRIPT: {
  1727. Variant script = function->return_type.script_type;
  1728. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1729. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
  1730. append(p_return_value);
  1731. append(script_idx);
  1732. } break;
  1733. default: {
  1734. ERR_PRINT("Compiler bug: unresolved return.");
  1735. // Shouldn't get here, but fail-safe to a regular return;
  1736. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1737. append(p_return_value);
  1738. } break;
  1739. }
  1740. }
  1741. }
  1742. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1743. append_opcode(GDScriptFunction::OPCODE_ASSERT);
  1744. append(p_test);
  1745. append(p_message);
  1746. }
  1747. void GDScriptByteCodeGenerator::start_block() {
  1748. push_stack_identifiers();
  1749. }
  1750. void GDScriptByteCodeGenerator::end_block() {
  1751. pop_stack_identifiers();
  1752. }
  1753. void GDScriptByteCodeGenerator::clear_temporaries() {
  1754. for (int slot_idx : temporaries_pending_clear) {
  1755. // The temporary may have been reused as something else since it was added to the list.
  1756. // In that case, there's **no** need to clear it.
  1757. if (temporaries[slot_idx].can_contain_object) {
  1758. clear_address(Address(Address::TEMPORARY, slot_idx)); // Can contain `RefCounted`, so clear it.
  1759. }
  1760. }
  1761. temporaries_pending_clear.clear();
  1762. }
  1763. void GDScriptByteCodeGenerator::clear_address(const Address &p_address) {
  1764. // Do not check `is_local_dirty()` here! Always clear the address since the codegen doesn't track the compiler.
  1765. // Also, this method is used to initialize local variables of built-in types, since they cannot be `null`.
  1766. if (p_address.type.has_type && p_address.type.kind == GDScriptDataType::BUILTIN) {
  1767. switch (p_address.type.builtin_type) {
  1768. case Variant::BOOL:
  1769. write_assign_false(p_address);
  1770. break;
  1771. case Variant::DICTIONARY:
  1772. if (p_address.type.has_container_element_types()) {
  1773. 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>());
  1774. } else {
  1775. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1776. }
  1777. break;
  1778. case Variant::ARRAY:
  1779. if (p_address.type.has_container_element_type(0)) {
  1780. write_construct_typed_array(p_address, p_address.type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
  1781. } else {
  1782. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1783. }
  1784. break;
  1785. case Variant::NIL:
  1786. case Variant::OBJECT:
  1787. write_assign_null(p_address);
  1788. break;
  1789. default:
  1790. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1791. break;
  1792. }
  1793. } else {
  1794. write_assign_null(p_address);
  1795. }
  1796. if (p_address.mode == Address::LOCAL_VARIABLE) {
  1797. dirty_locals.erase(p_address.address);
  1798. }
  1799. }
  1800. // Returns `true` if the local has been reused and not cleaned up with `clear_address()`.
  1801. bool GDScriptByteCodeGenerator::is_local_dirty(const Address &p_address) const {
  1802. ERR_FAIL_COND_V(p_address.mode != Address::LOCAL_VARIABLE, false);
  1803. return dirty_locals.has(p_address.address);
  1804. }
  1805. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1806. if (!ended && function != nullptr) {
  1807. memdelete(function);
  1808. }
  1809. }