gdscript_byte_codegen.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  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) {
  1404. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  1405. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1406. // Store state.
  1407. for_counter_variables.push_back(counter);
  1408. for_container_variables.push_back(container);
  1409. }
  1410. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_list) {
  1411. const Address &container = for_container_variables.back()->get();
  1412. // Assign container.
  1413. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  1414. append(container);
  1415. append(p_list);
  1416. }
  1417. void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_conversion) {
  1418. const Address &counter = for_counter_variables.back()->get();
  1419. const Address &container = for_container_variables.back()->get();
  1420. current_breaks_to_patch.push_back(List<int>());
  1421. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1422. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1423. if (container.type.has_type) {
  1424. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1425. switch (container.type.builtin_type) {
  1426. case Variant::INT:
  1427. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1428. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1429. break;
  1430. case Variant::FLOAT:
  1431. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1432. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1433. break;
  1434. case Variant::VECTOR2:
  1435. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1436. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1437. break;
  1438. case Variant::VECTOR2I:
  1439. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1440. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1441. break;
  1442. case Variant::VECTOR3:
  1443. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1444. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1445. break;
  1446. case Variant::VECTOR3I:
  1447. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1448. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1449. break;
  1450. case Variant::STRING:
  1451. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1452. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1453. break;
  1454. case Variant::DICTIONARY:
  1455. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1456. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1457. break;
  1458. case Variant::ARRAY:
  1459. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1460. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1461. break;
  1462. case Variant::PACKED_BYTE_ARRAY:
  1463. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1464. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1465. break;
  1466. case Variant::PACKED_INT32_ARRAY:
  1467. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1468. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1469. break;
  1470. case Variant::PACKED_INT64_ARRAY:
  1471. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1472. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1473. break;
  1474. case Variant::PACKED_FLOAT32_ARRAY:
  1475. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1476. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1477. break;
  1478. case Variant::PACKED_FLOAT64_ARRAY:
  1479. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1480. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1481. break;
  1482. case Variant::PACKED_STRING_ARRAY:
  1483. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1484. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1485. break;
  1486. case Variant::PACKED_VECTOR2_ARRAY:
  1487. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1488. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1489. break;
  1490. case Variant::PACKED_VECTOR3_ARRAY:
  1491. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1492. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1493. break;
  1494. case Variant::PACKED_COLOR_ARRAY:
  1495. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1496. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1497. break;
  1498. case Variant::PACKED_VECTOR4_ARRAY:
  1499. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR4_ARRAY;
  1500. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR4_ARRAY;
  1501. break;
  1502. default:
  1503. break;
  1504. }
  1505. } else {
  1506. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1507. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1508. }
  1509. }
  1510. Address temp;
  1511. if (p_use_conversion) {
  1512. temp = Address(Address::LOCAL_VARIABLE, add_local("@iterator_temp", GDScriptDataType()));
  1513. }
  1514. // Begin loop.
  1515. append_opcode(begin_opcode);
  1516. append(counter);
  1517. append(container);
  1518. append(p_use_conversion ? temp : p_variable);
  1519. for_jmp_addrs.push_back(opcodes.size());
  1520. append(0); // End of loop address, will be patched.
  1521. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1522. append(opcodes.size() + 6); // Skip over 'continue' code.
  1523. // Next iteration.
  1524. int continue_addr = opcodes.size();
  1525. continue_addrs.push_back(continue_addr);
  1526. append_opcode(iterate_opcode);
  1527. append(counter);
  1528. append(container);
  1529. append(p_use_conversion ? temp : p_variable);
  1530. for_jmp_addrs.push_back(opcodes.size());
  1531. append(0); // Jump destination, will be patched.
  1532. if (p_use_conversion) {
  1533. write_assign_with_conversion(p_variable, temp);
  1534. if (p_variable.type.can_contain_object()) {
  1535. clear_address(temp); // Can contain `RefCounted`, so clear it.
  1536. }
  1537. }
  1538. }
  1539. void GDScriptByteCodeGenerator::write_endfor() {
  1540. // Jump back to loop check.
  1541. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1542. append(continue_addrs.back()->get());
  1543. continue_addrs.pop_back();
  1544. // Patch end jumps (two of them).
  1545. for (int i = 0; i < 2; i++) {
  1546. patch_jump(for_jmp_addrs.back()->get());
  1547. for_jmp_addrs.pop_back();
  1548. }
  1549. // Patch break statements.
  1550. for (const int &E : current_breaks_to_patch.back()->get()) {
  1551. patch_jump(E);
  1552. }
  1553. current_breaks_to_patch.pop_back();
  1554. // Pop state.
  1555. for_counter_variables.pop_back();
  1556. for_container_variables.pop_back();
  1557. }
  1558. void GDScriptByteCodeGenerator::start_while_condition() {
  1559. current_breaks_to_patch.push_back(List<int>());
  1560. continue_addrs.push_back(opcodes.size());
  1561. }
  1562. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1563. // Condition check.
  1564. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1565. append(p_condition);
  1566. while_jmp_addrs.push_back(opcodes.size());
  1567. append(0); // End of loop address, will be patched.
  1568. }
  1569. void GDScriptByteCodeGenerator::write_endwhile() {
  1570. // Jump back to loop check.
  1571. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1572. append(continue_addrs.back()->get());
  1573. continue_addrs.pop_back();
  1574. // Patch end jump.
  1575. patch_jump(while_jmp_addrs.back()->get());
  1576. while_jmp_addrs.pop_back();
  1577. // Patch break statements.
  1578. for (const int &E : current_breaks_to_patch.back()->get()) {
  1579. patch_jump(E);
  1580. }
  1581. current_breaks_to_patch.pop_back();
  1582. }
  1583. void GDScriptByteCodeGenerator::write_break() {
  1584. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1585. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1586. append(0);
  1587. }
  1588. void GDScriptByteCodeGenerator::write_continue() {
  1589. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1590. append(continue_addrs.back()->get());
  1591. }
  1592. void GDScriptByteCodeGenerator::write_breakpoint() {
  1593. append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
  1594. }
  1595. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1596. if (GDScriptLanguage::get_singleton()->should_track_call_stack()) {
  1597. // Add newline for debugger and stack tracking if enabled in the project settings.
  1598. append_opcode(GDScriptFunction::OPCODE_LINE);
  1599. append(p_line);
  1600. current_line = p_line;
  1601. }
  1602. }
  1603. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1604. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1605. // Either the function is untyped or the return value is also typed.
  1606. // If this is a typed function, then we need to check for potential conversions.
  1607. if (function->return_type.has_type) {
  1608. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
  1609. // Typed array.
  1610. const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
  1611. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1612. append(p_return_value);
  1613. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1614. append(element_type.builtin_type);
  1615. append(element_type.native_type);
  1616. } else if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::DICTIONARY &&
  1617. function->return_type.has_container_element_types()) {
  1618. // Typed dictionary.
  1619. const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
  1620. const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
  1621. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
  1622. append(p_return_value);
  1623. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1624. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1625. append(key_type.builtin_type);
  1626. append(key_type.native_type);
  1627. append(value_type.builtin_type);
  1628. append(value_type.native_type);
  1629. } 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) {
  1630. // Add conversion.
  1631. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1632. append(p_return_value);
  1633. append(function->return_type.builtin_type);
  1634. } else {
  1635. // Just assign.
  1636. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1637. append(p_return_value);
  1638. }
  1639. } else {
  1640. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1641. append(p_return_value);
  1642. }
  1643. } else {
  1644. switch (function->return_type.kind) {
  1645. case GDScriptDataType::BUILTIN: {
  1646. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
  1647. const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
  1648. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1649. append(p_return_value);
  1650. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1651. append(element_type.builtin_type);
  1652. append(element_type.native_type);
  1653. } else if (function->return_type.builtin_type == Variant::DICTIONARY && function->return_type.has_container_element_types()) {
  1654. const GDScriptDataType &key_type = function->return_type.get_container_element_type_or_variant(0);
  1655. const GDScriptDataType &value_type = function->return_type.get_container_element_type_or_variant(1);
  1656. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_DICTIONARY);
  1657. append(p_return_value);
  1658. append(get_constant_pos(key_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1659. append(get_constant_pos(value_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1660. append(key_type.builtin_type);
  1661. append(key_type.native_type);
  1662. append(value_type.builtin_type);
  1663. append(value_type.native_type);
  1664. } else {
  1665. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1666. append(p_return_value);
  1667. append(function->return_type.builtin_type);
  1668. }
  1669. } break;
  1670. case GDScriptDataType::NATIVE: {
  1671. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
  1672. append(p_return_value);
  1673. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1674. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1675. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1676. append(class_idx);
  1677. } break;
  1678. case GDScriptDataType::GDSCRIPT:
  1679. case GDScriptDataType::SCRIPT: {
  1680. Variant script = function->return_type.script_type;
  1681. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1682. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
  1683. append(p_return_value);
  1684. append(script_idx);
  1685. } break;
  1686. default: {
  1687. ERR_PRINT("Compiler bug: unresolved return.");
  1688. // Shouldn't get here, but fail-safe to a regular return;
  1689. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1690. append(p_return_value);
  1691. } break;
  1692. }
  1693. }
  1694. }
  1695. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1696. append_opcode(GDScriptFunction::OPCODE_ASSERT);
  1697. append(p_test);
  1698. append(p_message);
  1699. }
  1700. void GDScriptByteCodeGenerator::start_block() {
  1701. push_stack_identifiers();
  1702. }
  1703. void GDScriptByteCodeGenerator::end_block() {
  1704. pop_stack_identifiers();
  1705. }
  1706. void GDScriptByteCodeGenerator::clear_temporaries() {
  1707. for (int slot_idx : temporaries_pending_clear) {
  1708. // The temporary may have been reused as something else since it was added to the list.
  1709. // In that case, there's **no** need to clear it.
  1710. if (temporaries[slot_idx].can_contain_object) {
  1711. clear_address(Address(Address::TEMPORARY, slot_idx)); // Can contain `RefCounted`, so clear it.
  1712. }
  1713. }
  1714. temporaries_pending_clear.clear();
  1715. }
  1716. void GDScriptByteCodeGenerator::clear_address(const Address &p_address) {
  1717. // Do not check `is_local_dirty()` here! Always clear the address since the codegen doesn't track the compiler.
  1718. // Also, this method is used to initialize local variables of built-in types, since they cannot be `null`.
  1719. if (p_address.type.has_type && p_address.type.kind == GDScriptDataType::BUILTIN) {
  1720. switch (p_address.type.builtin_type) {
  1721. case Variant::BOOL:
  1722. write_assign_false(p_address);
  1723. break;
  1724. case Variant::DICTIONARY:
  1725. if (p_address.type.has_container_element_types()) {
  1726. 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>());
  1727. } else {
  1728. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1729. }
  1730. break;
  1731. case Variant::ARRAY:
  1732. if (p_address.type.has_container_element_type(0)) {
  1733. write_construct_typed_array(p_address, p_address.type.get_container_element_type(0), Vector<GDScriptCodeGenerator::Address>());
  1734. } else {
  1735. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1736. }
  1737. break;
  1738. case Variant::NIL:
  1739. case Variant::OBJECT:
  1740. write_assign_null(p_address);
  1741. break;
  1742. default:
  1743. write_construct(p_address, p_address.type.builtin_type, Vector<GDScriptCodeGenerator::Address>());
  1744. break;
  1745. }
  1746. } else {
  1747. write_assign_null(p_address);
  1748. }
  1749. if (p_address.mode == Address::LOCAL_VARIABLE) {
  1750. dirty_locals.erase(p_address.address);
  1751. }
  1752. }
  1753. // Returns `true` if the local has been reused and not cleaned up with `clear_address()`.
  1754. bool GDScriptByteCodeGenerator::is_local_dirty(const Address &p_address) const {
  1755. ERR_FAIL_COND_V(p_address.mode != Address::LOCAL_VARIABLE, false);
  1756. return dirty_locals.has(p_address.address);
  1757. }
  1758. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1759. if (!ended && function != nullptr) {
  1760. memdelete(function);
  1761. }
  1762. }