gdscript_byte_codegen.cpp 64 KB

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