gdscript_byte_codegen.cpp 58 KB

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