gdscript_byte_codegen.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. /**************************************************************************/
  2. /* gdscript_byte_codegen.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript_byte_codegen.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #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::VECTOR4:
  79. case Variant::VECTOR4I:
  80. case Variant::PLANE:
  81. case Variant::QUATERNION:
  82. case Variant::AABB:
  83. case Variant::BASIS:
  84. case Variant::TRANSFORM3D:
  85. case Variant::PROJECTION:
  86. case Variant::COLOR:
  87. case Variant::STRING_NAME:
  88. case Variant::NODE_PATH:
  89. case Variant::RID:
  90. case Variant::OBJECT:
  91. case Variant::CALLABLE:
  92. case Variant::SIGNAL:
  93. case Variant::DICTIONARY:
  94. case Variant::ARRAY:
  95. temp_type = p_type.builtin_type;
  96. break;
  97. case Variant::PACKED_BYTE_ARRAY:
  98. case Variant::PACKED_INT32_ARRAY:
  99. case Variant::PACKED_INT64_ARRAY:
  100. case Variant::PACKED_FLOAT32_ARRAY:
  101. case Variant::PACKED_FLOAT64_ARRAY:
  102. case Variant::PACKED_STRING_ARRAY:
  103. case Variant::PACKED_VECTOR2_ARRAY:
  104. case Variant::PACKED_VECTOR3_ARRAY:
  105. case Variant::PACKED_COLOR_ARRAY:
  106. case Variant::VARIANT_MAX:
  107. // Packed arrays are reference counted, so we don't use the pool for them.
  108. temp_type = Variant::NIL;
  109. break;
  110. }
  111. } else {
  112. temp_type = Variant::OBJECT;
  113. }
  114. }
  115. if (!temporaries_pool.has(temp_type)) {
  116. temporaries_pool[temp_type] = List<int>();
  117. }
  118. List<int> &pool = temporaries_pool[temp_type];
  119. if (pool.is_empty()) {
  120. StackSlot new_temp(temp_type);
  121. int idx = temporaries.size();
  122. pool.push_back(idx);
  123. temporaries.push_back(new_temp);
  124. }
  125. int slot = pool.front()->get();
  126. pool.pop_front();
  127. used_temporaries.push_back(slot);
  128. return slot;
  129. }
  130. void GDScriptByteCodeGenerator::pop_temporary() {
  131. ERR_FAIL_COND(used_temporaries.is_empty());
  132. int slot_idx = used_temporaries.back()->get();
  133. const StackSlot &slot = temporaries[slot_idx];
  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[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. function->_ptrcall_args_size = ptrcall_max;
  363. #ifdef DEBUG_ENABLED
  364. function->operator_names = operator_names;
  365. function->setter_names = setter_names;
  366. function->getter_names = getter_names;
  367. function->builtin_methods_names = builtin_methods_names;
  368. function->constructors_names = constructors_names;
  369. function->utilities_names = utilities_names;
  370. function->gds_utilities_names = gds_utilities_names;
  371. #endif
  372. ended = true;
  373. return function;
  374. }
  375. #ifdef DEBUG_ENABLED
  376. void GDScriptByteCodeGenerator::set_signature(const String &p_signature) {
  377. function->profile.signature = p_signature;
  378. }
  379. #endif
  380. void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
  381. function->_initial_line = p_line;
  382. }
  383. #define HAS_BUILTIN_TYPE(m_var) \
  384. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
  385. #define IS_BUILTIN_TYPE(m_var, m_type) \
  386. (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type)
  387. void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) {
  388. switch (p_new_type) {
  389. case Variant::BOOL:
  390. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BOOL);
  391. break;
  392. case Variant::INT:
  393. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_INT);
  394. break;
  395. case Variant::FLOAT:
  396. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_FLOAT);
  397. break;
  398. case Variant::STRING:
  399. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING);
  400. break;
  401. case Variant::VECTOR2:
  402. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2);
  403. break;
  404. case Variant::VECTOR2I:
  405. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2I);
  406. break;
  407. case Variant::RECT2:
  408. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2);
  409. break;
  410. case Variant::RECT2I:
  411. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2I);
  412. break;
  413. case Variant::VECTOR3:
  414. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
  415. break;
  416. case Variant::VECTOR3I:
  417. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
  418. break;
  419. case Variant::TRANSFORM2D:
  420. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM2D);
  421. break;
  422. case Variant::VECTOR4:
  423. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3);
  424. break;
  425. case Variant::VECTOR4I:
  426. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I);
  427. break;
  428. case Variant::PLANE:
  429. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PLANE);
  430. break;
  431. case Variant::QUATERNION:
  432. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_QUATERNION);
  433. break;
  434. case Variant::AABB:
  435. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_AABB);
  436. break;
  437. case Variant::BASIS:
  438. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_BASIS);
  439. break;
  440. case Variant::TRANSFORM3D:
  441. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM3D);
  442. break;
  443. case Variant::PROJECTION:
  444. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PROJECTION);
  445. break;
  446. case Variant::COLOR:
  447. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_COLOR);
  448. break;
  449. case Variant::STRING_NAME:
  450. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING_NAME);
  451. break;
  452. case Variant::NODE_PATH:
  453. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_NODE_PATH);
  454. break;
  455. case Variant::RID:
  456. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_RID);
  457. break;
  458. case Variant::OBJECT:
  459. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_OBJECT);
  460. break;
  461. case Variant::CALLABLE:
  462. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_CALLABLE);
  463. break;
  464. case Variant::SIGNAL:
  465. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_SIGNAL);
  466. break;
  467. case Variant::DICTIONARY:
  468. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_DICTIONARY);
  469. break;
  470. case Variant::ARRAY:
  471. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_ARRAY);
  472. break;
  473. case Variant::PACKED_BYTE_ARRAY:
  474. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY);
  475. break;
  476. case Variant::PACKED_INT32_ARRAY:
  477. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY);
  478. break;
  479. case Variant::PACKED_INT64_ARRAY:
  480. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY);
  481. break;
  482. case Variant::PACKED_FLOAT32_ARRAY:
  483. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY);
  484. break;
  485. case Variant::PACKED_FLOAT64_ARRAY:
  486. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY);
  487. break;
  488. case Variant::PACKED_STRING_ARRAY:
  489. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY);
  490. break;
  491. case Variant::PACKED_VECTOR2_ARRAY:
  492. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY);
  493. break;
  494. case Variant::PACKED_VECTOR3_ARRAY:
  495. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY);
  496. break;
  497. case Variant::PACKED_COLOR_ARRAY:
  498. append_opcode(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY);
  499. break;
  500. case Variant::NIL:
  501. case Variant::VARIANT_MAX:
  502. return;
  503. }
  504. append(p_target);
  505. }
  506. void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) {
  507. if (HAS_BUILTIN_TYPE(p_left_operand)) {
  508. // Gather specific operator.
  509. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, Variant::NIL);
  510. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  511. append(p_left_operand);
  512. append(Address());
  513. append(p_target);
  514. append(op_func);
  515. #ifdef DEBUG_ENABLED
  516. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  517. #endif
  518. return;
  519. }
  520. // No specific types, perform variant evaluation.
  521. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  522. append(p_left_operand);
  523. append(Address());
  524. append(p_target);
  525. append(p_operator);
  526. }
  527. void GDScriptByteCodeGenerator::write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) {
  528. if (HAS_BUILTIN_TYPE(p_left_operand) && HAS_BUILTIN_TYPE(p_right_operand)) {
  529. if (p_target.mode == Address::TEMPORARY) {
  530. Variant::Type result_type = Variant::get_operator_return_type(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  531. Variant::Type temp_type = temporaries[p_target.address].type;
  532. if (result_type != temp_type) {
  533. write_type_adjust(p_target, result_type);
  534. }
  535. }
  536. // Gather specific operator.
  537. Variant::ValidatedOperatorEvaluator op_func = Variant::get_validated_operator_evaluator(p_operator, p_left_operand.type.builtin_type, p_right_operand.type.builtin_type);
  538. append_opcode(GDScriptFunction::OPCODE_OPERATOR_VALIDATED);
  539. append(p_left_operand);
  540. append(p_right_operand);
  541. append(p_target);
  542. append(op_func);
  543. #ifdef DEBUG_ENABLED
  544. add_debug_name(operator_names, get_operation_pos(op_func), Variant::get_operator_name(p_operator));
  545. #endif
  546. return;
  547. }
  548. // No specific types, perform variant evaluation.
  549. append_opcode(GDScriptFunction::OPCODE_OPERATOR);
  550. append(p_left_operand);
  551. append(p_right_operand);
  552. append(p_target);
  553. append(p_operator);
  554. }
  555. void GDScriptByteCodeGenerator::write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) {
  556. append_opcode(GDScriptFunction::OPCODE_EXTENDS_TEST);
  557. append(p_source);
  558. append(p_type);
  559. append(p_target);
  560. }
  561. void GDScriptByteCodeGenerator::write_type_test_builtin(const Address &p_target, const Address &p_source, Variant::Type p_type) {
  562. append_opcode(GDScriptFunction::OPCODE_IS_BUILTIN);
  563. append(p_source);
  564. append(p_target);
  565. append(p_type);
  566. }
  567. void GDScriptByteCodeGenerator::write_and_left_operand(const Address &p_left_operand) {
  568. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  569. append(p_left_operand);
  570. logic_op_jump_pos1.push_back(opcodes.size());
  571. append(0); // Jump target, will be patched.
  572. }
  573. void GDScriptByteCodeGenerator::write_and_right_operand(const Address &p_right_operand) {
  574. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  575. append(p_right_operand);
  576. logic_op_jump_pos2.push_back(opcodes.size());
  577. append(0); // Jump target, will be patched.
  578. }
  579. void GDScriptByteCodeGenerator::write_end_and(const Address &p_target) {
  580. // If here means both operands are true.
  581. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  582. append(p_target);
  583. // Jump away from the fail condition.
  584. append_opcode(GDScriptFunction::OPCODE_JUMP);
  585. append(opcodes.size() + 3);
  586. // Here it means one of operands is false.
  587. patch_jump(logic_op_jump_pos1.back()->get());
  588. patch_jump(logic_op_jump_pos2.back()->get());
  589. logic_op_jump_pos1.pop_back();
  590. logic_op_jump_pos2.pop_back();
  591. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  592. append(p_target);
  593. }
  594. void GDScriptByteCodeGenerator::write_or_left_operand(const Address &p_left_operand) {
  595. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  596. append(p_left_operand);
  597. logic_op_jump_pos1.push_back(opcodes.size());
  598. append(0); // Jump target, will be patched.
  599. }
  600. void GDScriptByteCodeGenerator::write_or_right_operand(const Address &p_right_operand) {
  601. append_opcode(GDScriptFunction::OPCODE_JUMP_IF);
  602. append(p_right_operand);
  603. logic_op_jump_pos2.push_back(opcodes.size());
  604. append(0); // Jump target, will be patched.
  605. }
  606. void GDScriptByteCodeGenerator::write_end_or(const Address &p_target) {
  607. // If here means both operands are false.
  608. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  609. append(p_target);
  610. // Jump away from the success condition.
  611. append_opcode(GDScriptFunction::OPCODE_JUMP);
  612. append(opcodes.size() + 3);
  613. // Here it means one of operands is true.
  614. patch_jump(logic_op_jump_pos1.back()->get());
  615. patch_jump(logic_op_jump_pos2.back()->get());
  616. logic_op_jump_pos1.pop_back();
  617. logic_op_jump_pos2.pop_back();
  618. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  619. append(p_target);
  620. }
  621. void GDScriptByteCodeGenerator::write_start_ternary(const Address &p_target) {
  622. ternary_result.push_back(p_target);
  623. }
  624. void GDScriptByteCodeGenerator::write_ternary_condition(const Address &p_condition) {
  625. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  626. append(p_condition);
  627. ternary_jump_fail_pos.push_back(opcodes.size());
  628. append(0); // Jump target, will be patched.
  629. }
  630. void GDScriptByteCodeGenerator::write_ternary_true_expr(const Address &p_expr) {
  631. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  632. append(ternary_result.back()->get());
  633. append(p_expr);
  634. // Jump away from the false path.
  635. append_opcode(GDScriptFunction::OPCODE_JUMP);
  636. ternary_jump_skip_pos.push_back(opcodes.size());
  637. append(0);
  638. // Fail must jump here.
  639. patch_jump(ternary_jump_fail_pos.back()->get());
  640. ternary_jump_fail_pos.pop_back();
  641. }
  642. void GDScriptByteCodeGenerator::write_ternary_false_expr(const Address &p_expr) {
  643. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  644. append(ternary_result.back()->get());
  645. append(p_expr);
  646. }
  647. void GDScriptByteCodeGenerator::write_end_ternary() {
  648. patch_jump(ternary_jump_skip_pos.back()->get());
  649. ternary_jump_skip_pos.pop_back();
  650. ternary_result.pop_back();
  651. }
  652. void GDScriptByteCodeGenerator::write_set(const Address &p_target, const Address &p_index, const Address &p_source) {
  653. if (HAS_BUILTIN_TYPE(p_target)) {
  654. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_setter(p_target.type.builtin_type) &&
  655. IS_BUILTIN_TYPE(p_source, Variant::get_indexed_element_type(p_target.type.builtin_type))) {
  656. // Use indexed setter instead.
  657. Variant::ValidatedIndexedSetter setter = Variant::get_member_validated_indexed_setter(p_target.type.builtin_type);
  658. append_opcode(GDScriptFunction::OPCODE_SET_INDEXED_VALIDATED);
  659. append(p_target);
  660. append(p_index);
  661. append(p_source);
  662. append(setter);
  663. return;
  664. } else if (Variant::get_member_validated_keyed_setter(p_target.type.builtin_type)) {
  665. Variant::ValidatedKeyedSetter setter = Variant::get_member_validated_keyed_setter(p_target.type.builtin_type);
  666. append_opcode(GDScriptFunction::OPCODE_SET_KEYED_VALIDATED);
  667. append(p_target);
  668. append(p_index);
  669. append(p_source);
  670. append(setter);
  671. return;
  672. }
  673. }
  674. append_opcode(GDScriptFunction::OPCODE_SET_KEYED);
  675. append(p_target);
  676. append(p_index);
  677. append(p_source);
  678. }
  679. void GDScriptByteCodeGenerator::write_get(const Address &p_target, const Address &p_index, const Address &p_source) {
  680. if (HAS_BUILTIN_TYPE(p_source)) {
  681. if (IS_BUILTIN_TYPE(p_index, Variant::INT) && Variant::get_member_validated_indexed_getter(p_source.type.builtin_type)) {
  682. // Use indexed getter instead.
  683. Variant::ValidatedIndexedGetter getter = Variant::get_member_validated_indexed_getter(p_source.type.builtin_type);
  684. append_opcode(GDScriptFunction::OPCODE_GET_INDEXED_VALIDATED);
  685. append(p_source);
  686. append(p_index);
  687. append(p_target);
  688. append(getter);
  689. return;
  690. } else if (Variant::get_member_validated_keyed_getter(p_source.type.builtin_type)) {
  691. Variant::ValidatedKeyedGetter getter = Variant::get_member_validated_keyed_getter(p_source.type.builtin_type);
  692. append_opcode(GDScriptFunction::OPCODE_GET_KEYED_VALIDATED);
  693. append(p_source);
  694. append(p_index);
  695. append(p_target);
  696. append(getter);
  697. return;
  698. }
  699. }
  700. append_opcode(GDScriptFunction::OPCODE_GET_KEYED);
  701. append(p_source);
  702. append(p_index);
  703. append(p_target);
  704. }
  705. void GDScriptByteCodeGenerator::write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  706. if (HAS_BUILTIN_TYPE(p_target) && Variant::get_member_validated_setter(p_target.type.builtin_type, p_name) &&
  707. IS_BUILTIN_TYPE(p_source, Variant::get_member_type(p_target.type.builtin_type, p_name))) {
  708. Variant::ValidatedSetter setter = Variant::get_member_validated_setter(p_target.type.builtin_type, p_name);
  709. append_opcode(GDScriptFunction::OPCODE_SET_NAMED_VALIDATED);
  710. append(p_target);
  711. append(p_source);
  712. append(setter);
  713. #ifdef DEBUG_ENABLED
  714. add_debug_name(setter_names, get_setter_pos(setter), p_name);
  715. #endif
  716. return;
  717. }
  718. append_opcode(GDScriptFunction::OPCODE_SET_NAMED);
  719. append(p_target);
  720. append(p_source);
  721. append(p_name);
  722. }
  723. void GDScriptByteCodeGenerator::write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) {
  724. if (HAS_BUILTIN_TYPE(p_source) && Variant::get_member_validated_getter(p_source.type.builtin_type, p_name)) {
  725. Variant::ValidatedGetter getter = Variant::get_member_validated_getter(p_source.type.builtin_type, p_name);
  726. append_opcode(GDScriptFunction::OPCODE_GET_NAMED_VALIDATED);
  727. append(p_source);
  728. append(p_target);
  729. append(getter);
  730. #ifdef DEBUG_ENABLED
  731. add_debug_name(getter_names, get_getter_pos(getter), p_name);
  732. #endif
  733. return;
  734. }
  735. append_opcode(GDScriptFunction::OPCODE_GET_NAMED);
  736. append(p_source);
  737. append(p_target);
  738. append(p_name);
  739. }
  740. void GDScriptByteCodeGenerator::write_set_member(const Address &p_value, const StringName &p_name) {
  741. append_opcode(GDScriptFunction::OPCODE_SET_MEMBER);
  742. append(p_value);
  743. append(p_name);
  744. }
  745. void GDScriptByteCodeGenerator::write_get_member(const Address &p_target, const StringName &p_name) {
  746. append_opcode(GDScriptFunction::OPCODE_GET_MEMBER);
  747. append(p_target);
  748. append(p_name);
  749. }
  750. void GDScriptByteCodeGenerator::write_assign_with_conversion(const Address &p_target, const Address &p_source) {
  751. switch (p_target.type.kind) {
  752. case GDScriptDataType::BUILTIN: {
  753. if (p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  754. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  755. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  756. append(p_target);
  757. append(p_source);
  758. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  759. append(element_type.builtin_type);
  760. append(element_type.native_type);
  761. } else {
  762. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  763. append(p_target);
  764. append(p_source);
  765. append(p_target.type.builtin_type);
  766. }
  767. } break;
  768. case GDScriptDataType::NATIVE: {
  769. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_target.type.native_type];
  770. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  771. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  772. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_NATIVE);
  773. append(p_target);
  774. append(p_source);
  775. append(class_idx);
  776. } break;
  777. case GDScriptDataType::SCRIPT:
  778. case GDScriptDataType::GDSCRIPT: {
  779. Variant script = p_target.type.script_type;
  780. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  781. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_SCRIPT);
  782. append(p_target);
  783. append(p_source);
  784. append(idx);
  785. } break;
  786. default: {
  787. ERR_PRINT("Compiler bug: unresolved assign.");
  788. // Shouldn't get here, but fail-safe to a regular assignment
  789. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  790. append(p_target);
  791. append(p_source);
  792. }
  793. }
  794. }
  795. void GDScriptByteCodeGenerator::write_assign(const Address &p_target, const Address &p_source) {
  796. if (p_target.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type == Variant::ARRAY && p_target.type.has_container_element_type()) {
  797. const GDScriptDataType &element_type = p_target.type.get_container_element_type();
  798. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_ARRAY);
  799. append(p_target);
  800. append(p_source);
  801. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  802. append(element_type.builtin_type);
  803. append(element_type.native_type);
  804. } else if (p_target.type.kind == GDScriptDataType::BUILTIN && p_source.type.kind == GDScriptDataType::BUILTIN && p_target.type.builtin_type != p_source.type.builtin_type) {
  805. // Need conversion.
  806. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TYPED_BUILTIN);
  807. append(p_target);
  808. append(p_source);
  809. append(p_target.type.builtin_type);
  810. } else {
  811. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  812. append(p_target);
  813. append(p_source);
  814. }
  815. }
  816. void GDScriptByteCodeGenerator::write_assign_true(const Address &p_target) {
  817. append_opcode(GDScriptFunction::OPCODE_ASSIGN_TRUE);
  818. append(p_target);
  819. }
  820. void GDScriptByteCodeGenerator::write_assign_false(const Address &p_target) {
  821. append_opcode(GDScriptFunction::OPCODE_ASSIGN_FALSE);
  822. append(p_target);
  823. }
  824. void GDScriptByteCodeGenerator::write_assign_default_parameter(const Address &p_dst, const Address &p_src, bool p_use_conversion) {
  825. if (p_use_conversion) {
  826. write_assign_with_conversion(p_dst, p_src);
  827. } else {
  828. write_assign(p_dst, p_src);
  829. }
  830. function->default_arguments.push_back(opcodes.size());
  831. }
  832. void GDScriptByteCodeGenerator::write_store_global(const Address &p_dst, int p_global_index) {
  833. append_opcode(GDScriptFunction::OPCODE_STORE_GLOBAL);
  834. append(p_dst);
  835. append(p_global_index);
  836. }
  837. void GDScriptByteCodeGenerator::write_store_named_global(const Address &p_dst, const StringName &p_global) {
  838. append_opcode(GDScriptFunction::OPCODE_STORE_NAMED_GLOBAL);
  839. append(p_dst);
  840. append(p_global);
  841. }
  842. void GDScriptByteCodeGenerator::write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) {
  843. int index = 0;
  844. switch (p_type.kind) {
  845. case GDScriptDataType::BUILTIN: {
  846. append_opcode(GDScriptFunction::OPCODE_CAST_TO_BUILTIN);
  847. index = p_type.builtin_type;
  848. } break;
  849. case GDScriptDataType::NATIVE: {
  850. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[p_type.native_type];
  851. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  852. append_opcode(GDScriptFunction::OPCODE_CAST_TO_NATIVE);
  853. index = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  854. } break;
  855. case GDScriptDataType::SCRIPT:
  856. case GDScriptDataType::GDSCRIPT: {
  857. Variant script = p_type.script_type;
  858. int idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  859. append_opcode(GDScriptFunction::OPCODE_CAST_TO_SCRIPT);
  860. index = idx;
  861. } break;
  862. default: {
  863. return;
  864. }
  865. }
  866. append(p_source);
  867. append(p_target);
  868. append(index);
  869. }
  870. GDScriptCodeGenerator::Address GDScriptByteCodeGenerator::get_call_target(const GDScriptCodeGenerator::Address &p_target, Variant::Type p_type) {
  871. if (p_target.mode == Address::NIL) {
  872. GDScriptDataType type;
  873. if (p_type != Variant::NIL) {
  874. type.has_type = true;
  875. type.kind = GDScriptDataType::BUILTIN;
  876. type.builtin_type = p_type;
  877. }
  878. uint32_t addr = add_temporary(type);
  879. pop_temporary();
  880. return Address(Address::TEMPORARY, addr, type);
  881. } else {
  882. return p_target;
  883. }
  884. }
  885. void GDScriptByteCodeGenerator::write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  886. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  887. for (int i = 0; i < p_arguments.size(); i++) {
  888. append(p_arguments[i]);
  889. }
  890. append(p_base);
  891. append(get_call_target(p_target));
  892. append(p_arguments.size());
  893. append(p_function_name);
  894. }
  895. void GDScriptByteCodeGenerator::write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  896. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_SELF_BASE, 1 + p_arguments.size());
  897. for (int i = 0; i < p_arguments.size(); i++) {
  898. append(p_arguments[i]);
  899. }
  900. append(get_call_target(p_target));
  901. append(p_arguments.size());
  902. append(p_function_name);
  903. }
  904. void GDScriptByteCodeGenerator::write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  905. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  906. for (int i = 0; i < p_arguments.size(); i++) {
  907. append(p_arguments[i]);
  908. }
  909. append(p_base);
  910. append(get_call_target(p_target));
  911. append(p_arguments.size());
  912. append(p_function_name);
  913. }
  914. void GDScriptByteCodeGenerator::write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  915. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_GDSCRIPT_UTILITY, 1 + p_arguments.size());
  916. GDScriptUtilityFunctions::FunctionPtr gds_function = GDScriptUtilityFunctions::get_function(p_function);
  917. for (int i = 0; i < p_arguments.size(); i++) {
  918. append(p_arguments[i]);
  919. }
  920. append(get_call_target(p_target));
  921. append(p_arguments.size());
  922. append(gds_function);
  923. #ifdef DEBUG_ENABLED
  924. add_debug_name(gds_utilities_names, get_gds_utility_pos(gds_function), p_function);
  925. #endif
  926. }
  927. void GDScriptByteCodeGenerator::write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) {
  928. bool is_validated = true;
  929. if (Variant::is_utility_function_vararg(p_function)) {
  930. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  931. } else if (p_arguments.size() == Variant::get_utility_function_argument_count(p_function)) {
  932. bool all_types_exact = true;
  933. for (int i = 0; i < p_arguments.size(); i++) {
  934. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_utility_function_argument_type(p_function, i))) {
  935. all_types_exact = false;
  936. break;
  937. }
  938. }
  939. is_validated = all_types_exact;
  940. }
  941. if (is_validated) {
  942. Variant::Type result_type = Variant::has_utility_function_return_value(p_function) ? Variant::get_utility_function_return_type(p_function) : Variant::NIL;
  943. Address target = get_call_target(p_target, result_type);
  944. Variant::Type temp_type = temporaries[target.address].type;
  945. if (result_type != temp_type) {
  946. write_type_adjust(target, result_type);
  947. }
  948. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY_VALIDATED, 1 + p_arguments.size());
  949. for (int i = 0; i < p_arguments.size(); i++) {
  950. append(p_arguments[i]);
  951. }
  952. append(target);
  953. append(p_arguments.size());
  954. append(Variant::get_validated_utility_function(p_function));
  955. #ifdef DEBUG_ENABLED
  956. add_debug_name(utilities_names, get_utility_pos(Variant::get_validated_utility_function(p_function)), p_function);
  957. #endif
  958. } else {
  959. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_UTILITY, 1 + p_arguments.size());
  960. for (int i = 0; i < p_arguments.size(); i++) {
  961. append(p_arguments[i]);
  962. }
  963. append(get_call_target(p_target));
  964. append(p_arguments.size());
  965. append(p_function);
  966. }
  967. }
  968. 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) {
  969. bool is_validated = false;
  970. // Check if all types are correct.
  971. if (Variant::is_builtin_method_vararg(p_type, p_method)) {
  972. is_validated = true; // Vararg works fine with any argument, since they can be any type.
  973. } else if (p_arguments.size() == Variant::get_builtin_method_argument_count(p_type, p_method)) {
  974. bool all_types_exact = true;
  975. for (int i = 0; i < p_arguments.size(); i++) {
  976. if (!IS_BUILTIN_TYPE(p_arguments[i], Variant::get_builtin_method_argument_type(p_type, p_method, i))) {
  977. all_types_exact = false;
  978. break;
  979. }
  980. }
  981. is_validated = all_types_exact;
  982. }
  983. if (!is_validated) {
  984. // Perform regular call.
  985. if (p_is_static) {
  986. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_STATIC, p_arguments.size() + 1);
  987. for (int i = 0; i < p_arguments.size(); i++) {
  988. append(p_arguments[i]);
  989. }
  990. append(get_call_target(p_target));
  991. append(p_type);
  992. append(p_method);
  993. append(p_arguments.size());
  994. } else {
  995. write_call(p_target, p_base, p_method, p_arguments);
  996. }
  997. return;
  998. }
  999. Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method);
  1000. Address target = get_call_target(p_target, result_type);
  1001. Variant::Type temp_type = temporaries[target.address].type;
  1002. if (result_type != temp_type) {
  1003. write_type_adjust(target, result_type);
  1004. }
  1005. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size());
  1006. for (int i = 0; i < p_arguments.size(); i++) {
  1007. append(p_arguments[i]);
  1008. }
  1009. append(p_base);
  1010. append(target);
  1011. append(p_arguments.size());
  1012. append(Variant::get_validated_builtin_method(p_type, p_method));
  1013. #ifdef DEBUG_ENABLED
  1014. add_debug_name(builtin_methods_names, get_builtin_method_pos(Variant::get_validated_builtin_method(p_type, p_method)), p_method);
  1015. #endif
  1016. }
  1017. 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) {
  1018. write_call_builtin_type(p_target, p_base, p_type, p_method, false, p_arguments);
  1019. }
  1020. void GDScriptByteCodeGenerator::write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) {
  1021. write_call_builtin_type(p_target, Address(), p_type, p_method, true, p_arguments);
  1022. }
  1023. void GDScriptByteCodeGenerator::write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) {
  1024. bool is_validated = false;
  1025. MethodBind *method = ClassDB::get_method(p_class, p_method);
  1026. if (!is_validated) {
  1027. // Perform regular call.
  1028. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_NATIVE_STATIC, p_arguments.size() + 1);
  1029. for (int i = 0; i < p_arguments.size(); i++) {
  1030. append(p_arguments[i]);
  1031. }
  1032. append(get_call_target(p_target));
  1033. append(method);
  1034. append(p_arguments.size());
  1035. return;
  1036. }
  1037. }
  1038. void GDScriptByteCodeGenerator::write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1039. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1040. for (int i = 0; i < p_arguments.size(); i++) {
  1041. append(p_arguments[i]);
  1042. }
  1043. append(p_base);
  1044. append(get_call_target(p_target));
  1045. append(p_arguments.size());
  1046. append(p_method);
  1047. }
  1048. void GDScriptByteCodeGenerator::write_call_ptrcall(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) {
  1049. #define CASE_TYPE(m_type) \
  1050. case Variant::m_type: \
  1051. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_##m_type, 2 + p_arguments.size()); \
  1052. break
  1053. bool is_ptrcall = true;
  1054. if (p_method->has_return()) {
  1055. MethodInfo info;
  1056. ClassDB::get_method_info(p_method->get_instance_class(), p_method->get_name(), &info);
  1057. switch (info.return_val.type) {
  1058. CASE_TYPE(BOOL);
  1059. CASE_TYPE(INT);
  1060. CASE_TYPE(FLOAT);
  1061. CASE_TYPE(STRING);
  1062. CASE_TYPE(VECTOR2);
  1063. CASE_TYPE(VECTOR2I);
  1064. CASE_TYPE(RECT2);
  1065. CASE_TYPE(RECT2I);
  1066. CASE_TYPE(VECTOR3);
  1067. CASE_TYPE(VECTOR3I);
  1068. CASE_TYPE(TRANSFORM2D);
  1069. CASE_TYPE(PLANE);
  1070. CASE_TYPE(AABB);
  1071. CASE_TYPE(BASIS);
  1072. CASE_TYPE(TRANSFORM3D);
  1073. CASE_TYPE(COLOR);
  1074. CASE_TYPE(STRING_NAME);
  1075. CASE_TYPE(NODE_PATH);
  1076. CASE_TYPE(RID);
  1077. CASE_TYPE(QUATERNION);
  1078. CASE_TYPE(OBJECT);
  1079. CASE_TYPE(CALLABLE);
  1080. CASE_TYPE(SIGNAL);
  1081. CASE_TYPE(DICTIONARY);
  1082. CASE_TYPE(ARRAY);
  1083. CASE_TYPE(PACKED_BYTE_ARRAY);
  1084. CASE_TYPE(PACKED_INT32_ARRAY);
  1085. CASE_TYPE(PACKED_INT64_ARRAY);
  1086. CASE_TYPE(PACKED_FLOAT32_ARRAY);
  1087. CASE_TYPE(PACKED_FLOAT64_ARRAY);
  1088. CASE_TYPE(PACKED_STRING_ARRAY);
  1089. CASE_TYPE(PACKED_VECTOR2_ARRAY);
  1090. CASE_TYPE(PACKED_VECTOR3_ARRAY);
  1091. CASE_TYPE(PACKED_COLOR_ARRAY);
  1092. default:
  1093. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL_METHOD_BIND : GDScriptFunction::OPCODE_CALL_METHOD_BIND_RET, 2 + p_arguments.size());
  1094. is_ptrcall = false;
  1095. break;
  1096. }
  1097. } else {
  1098. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_PTRCALL_NO_RETURN, 2 + p_arguments.size());
  1099. }
  1100. for (int i = 0; i < p_arguments.size(); i++) {
  1101. append(p_arguments[i]);
  1102. }
  1103. append(p_base);
  1104. append(get_call_target(p_target));
  1105. append(p_arguments.size());
  1106. append(p_method);
  1107. if (is_ptrcall) {
  1108. alloc_ptrcall(p_arguments.size());
  1109. }
  1110. #undef CASE_TYPE
  1111. }
  1112. void GDScriptByteCodeGenerator::write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1113. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1114. for (int i = 0; i < p_arguments.size(); i++) {
  1115. append(p_arguments[i]);
  1116. }
  1117. append(GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
  1118. append(get_call_target(p_target));
  1119. append(p_arguments.size());
  1120. append(p_function_name);
  1121. }
  1122. void GDScriptByteCodeGenerator::write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1123. append_opcode_and_argcount(GDScriptFunction::OPCODE_CALL_ASYNC, 2 + p_arguments.size());
  1124. for (int i = 0; i < p_arguments.size(); i++) {
  1125. append(p_arguments[i]);
  1126. }
  1127. append(GDScriptFunction::ADDR_SELF);
  1128. append(get_call_target(p_target));
  1129. append(p_arguments.size());
  1130. append(p_function_name);
  1131. }
  1132. void GDScriptByteCodeGenerator::write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) {
  1133. append_opcode_and_argcount(p_target.mode == Address::NIL ? GDScriptFunction::OPCODE_CALL : GDScriptFunction::OPCODE_CALL_RETURN, 2 + p_arguments.size());
  1134. for (int i = 0; i < p_arguments.size(); i++) {
  1135. append(p_arguments[i]);
  1136. }
  1137. append(p_base);
  1138. append(get_call_target(p_target));
  1139. append(p_arguments.size());
  1140. append(p_function_name);
  1141. }
  1142. void GDScriptByteCodeGenerator::write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) {
  1143. append_opcode_and_argcount(p_use_self ? GDScriptFunction::OPCODE_CREATE_SELF_LAMBDA : GDScriptFunction::OPCODE_CREATE_LAMBDA, 1 + p_captures.size());
  1144. for (int i = 0; i < p_captures.size(); i++) {
  1145. append(p_captures[i]);
  1146. }
  1147. append(get_call_target(p_target));
  1148. append(p_captures.size());
  1149. append(p_function);
  1150. }
  1151. void GDScriptByteCodeGenerator::write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) {
  1152. // Try to find an appropriate constructor.
  1153. bool all_have_type = true;
  1154. Vector<Variant::Type> arg_types;
  1155. for (int i = 0; i < p_arguments.size(); i++) {
  1156. if (!HAS_BUILTIN_TYPE(p_arguments[i])) {
  1157. all_have_type = false;
  1158. break;
  1159. }
  1160. arg_types.push_back(p_arguments[i].type.builtin_type);
  1161. }
  1162. if (all_have_type) {
  1163. int valid_constructor = -1;
  1164. for (int i = 0; i < Variant::get_constructor_count(p_type); i++) {
  1165. if (Variant::get_constructor_argument_count(p_type, i) != p_arguments.size()) {
  1166. continue;
  1167. }
  1168. int types_correct = true;
  1169. for (int j = 0; j < arg_types.size(); j++) {
  1170. if (arg_types[j] != Variant::get_constructor_argument_type(p_type, i, j)) {
  1171. types_correct = false;
  1172. break;
  1173. }
  1174. }
  1175. if (types_correct) {
  1176. valid_constructor = i;
  1177. break;
  1178. }
  1179. }
  1180. if (valid_constructor >= 0) {
  1181. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_VALIDATED, 1 + p_arguments.size());
  1182. for (int i = 0; i < p_arguments.size(); i++) {
  1183. append(p_arguments[i]);
  1184. }
  1185. append(get_call_target(p_target));
  1186. append(p_arguments.size());
  1187. append(Variant::get_validated_constructor(p_type, valid_constructor));
  1188. #ifdef DEBUG_ENABLED
  1189. add_debug_name(constructors_names, get_constructor_pos(Variant::get_validated_constructor(p_type, valid_constructor)), Variant::get_type_name(p_type));
  1190. #endif
  1191. return;
  1192. }
  1193. }
  1194. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT, 1 + p_arguments.size());
  1195. for (int i = 0; i < p_arguments.size(); i++) {
  1196. append(p_arguments[i]);
  1197. }
  1198. append(get_call_target(p_target));
  1199. append(p_arguments.size());
  1200. append(p_type);
  1201. }
  1202. void GDScriptByteCodeGenerator::write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) {
  1203. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_ARRAY, 1 + p_arguments.size());
  1204. for (int i = 0; i < p_arguments.size(); i++) {
  1205. append(p_arguments[i]);
  1206. }
  1207. append(get_call_target(p_target));
  1208. append(p_arguments.size());
  1209. }
  1210. void GDScriptByteCodeGenerator::write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) {
  1211. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_TYPED_ARRAY, 2 + p_arguments.size());
  1212. for (int i = 0; i < p_arguments.size(); i++) {
  1213. append(p_arguments[i]);
  1214. }
  1215. append(get_call_target(p_target));
  1216. append(get_constant_pos(p_element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1217. append(p_arguments.size());
  1218. append(p_element_type.builtin_type);
  1219. append(p_element_type.native_type);
  1220. }
  1221. void GDScriptByteCodeGenerator::write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) {
  1222. append_opcode_and_argcount(GDScriptFunction::OPCODE_CONSTRUCT_DICTIONARY, 1 + p_arguments.size());
  1223. for (int i = 0; i < p_arguments.size(); i++) {
  1224. append(p_arguments[i]);
  1225. }
  1226. append(get_call_target(p_target));
  1227. append(p_arguments.size() / 2); // This is number of key-value pairs, so only half of actual arguments.
  1228. }
  1229. void GDScriptByteCodeGenerator::write_await(const Address &p_target, const Address &p_operand) {
  1230. append_opcode(GDScriptFunction::OPCODE_AWAIT);
  1231. append(p_operand);
  1232. append_opcode(GDScriptFunction::OPCODE_AWAIT_RESUME);
  1233. append(p_target);
  1234. }
  1235. void GDScriptByteCodeGenerator::write_if(const Address &p_condition) {
  1236. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1237. append(p_condition);
  1238. if_jmp_addrs.push_back(opcodes.size());
  1239. append(0); // Jump destination, will be patched.
  1240. }
  1241. void GDScriptByteCodeGenerator::write_else() {
  1242. append_opcode(GDScriptFunction::OPCODE_JUMP); // Jump from true if block;
  1243. int else_jmp_addr = opcodes.size();
  1244. append(0); // Jump destination, will be patched.
  1245. patch_jump(if_jmp_addrs.back()->get());
  1246. if_jmp_addrs.pop_back();
  1247. if_jmp_addrs.push_back(else_jmp_addr);
  1248. }
  1249. void GDScriptByteCodeGenerator::write_endif() {
  1250. patch_jump(if_jmp_addrs.back()->get());
  1251. if_jmp_addrs.pop_back();
  1252. }
  1253. void GDScriptByteCodeGenerator::write_jump_if_shared(const Address &p_value) {
  1254. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_SHARED);
  1255. append(p_value);
  1256. if_jmp_addrs.push_back(opcodes.size());
  1257. append(0); // Jump destination, will be patched.
  1258. }
  1259. void GDScriptByteCodeGenerator::write_end_jump_if_shared() {
  1260. patch_jump(if_jmp_addrs.back()->get());
  1261. if_jmp_addrs.pop_back();
  1262. }
  1263. void GDScriptByteCodeGenerator::start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) {
  1264. Address counter(Address::LOCAL_VARIABLE, add_local("@counter_pos", p_iterator_type), p_iterator_type);
  1265. Address container(Address::LOCAL_VARIABLE, add_local("@container_pos", p_list_type), p_list_type);
  1266. // Store state.
  1267. for_counter_variables.push_back(counter);
  1268. for_container_variables.push_back(container);
  1269. }
  1270. void GDScriptByteCodeGenerator::write_for_assignment(const Address &p_variable, const Address &p_list) {
  1271. const Address &container = for_container_variables.back()->get();
  1272. // Assign container.
  1273. append_opcode(GDScriptFunction::OPCODE_ASSIGN);
  1274. append(container);
  1275. append(p_list);
  1276. for_iterator_variables.push_back(p_variable);
  1277. }
  1278. void GDScriptByteCodeGenerator::write_for() {
  1279. const Address &iterator = for_iterator_variables.back()->get();
  1280. const Address &counter = for_counter_variables.back()->get();
  1281. const Address &container = for_container_variables.back()->get();
  1282. current_breaks_to_patch.push_back(List<int>());
  1283. GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
  1284. GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
  1285. if (container.type.has_type) {
  1286. if (container.type.kind == GDScriptDataType::BUILTIN) {
  1287. switch (container.type.builtin_type) {
  1288. case Variant::INT:
  1289. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_INT;
  1290. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_INT;
  1291. break;
  1292. case Variant::FLOAT:
  1293. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_FLOAT;
  1294. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_FLOAT;
  1295. break;
  1296. case Variant::VECTOR2:
  1297. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2;
  1298. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2;
  1299. break;
  1300. case Variant::VECTOR2I:
  1301. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR2I;
  1302. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR2I;
  1303. break;
  1304. case Variant::VECTOR3:
  1305. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3;
  1306. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3;
  1307. break;
  1308. case Variant::VECTOR3I:
  1309. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_VECTOR3I;
  1310. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_VECTOR3I;
  1311. break;
  1312. case Variant::STRING:
  1313. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_STRING;
  1314. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_STRING;
  1315. break;
  1316. case Variant::DICTIONARY:
  1317. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_DICTIONARY;
  1318. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_DICTIONARY;
  1319. break;
  1320. case Variant::ARRAY:
  1321. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_ARRAY;
  1322. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_ARRAY;
  1323. break;
  1324. case Variant::PACKED_BYTE_ARRAY:
  1325. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY;
  1326. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_BYTE_ARRAY;
  1327. break;
  1328. case Variant::PACKED_INT32_ARRAY:
  1329. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY;
  1330. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT32_ARRAY;
  1331. break;
  1332. case Variant::PACKED_INT64_ARRAY:
  1333. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY;
  1334. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_INT64_ARRAY;
  1335. break;
  1336. case Variant::PACKED_FLOAT32_ARRAY:
  1337. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY;
  1338. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT32_ARRAY;
  1339. break;
  1340. case Variant::PACKED_FLOAT64_ARRAY:
  1341. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY;
  1342. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_FLOAT64_ARRAY;
  1343. break;
  1344. case Variant::PACKED_STRING_ARRAY:
  1345. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY;
  1346. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_STRING_ARRAY;
  1347. break;
  1348. case Variant::PACKED_VECTOR2_ARRAY:
  1349. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY;
  1350. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR2_ARRAY;
  1351. break;
  1352. case Variant::PACKED_VECTOR3_ARRAY:
  1353. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY;
  1354. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_VECTOR3_ARRAY;
  1355. break;
  1356. case Variant::PACKED_COLOR_ARRAY:
  1357. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY;
  1358. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_PACKED_COLOR_ARRAY;
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. } else {
  1364. begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN_OBJECT;
  1365. iterate_opcode = GDScriptFunction::OPCODE_ITERATE_OBJECT;
  1366. }
  1367. }
  1368. // Begin loop.
  1369. append_opcode(begin_opcode);
  1370. append(counter);
  1371. append(container);
  1372. append(iterator);
  1373. for_jmp_addrs.push_back(opcodes.size());
  1374. append(0); // End of loop address, will be patched.
  1375. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1376. append(opcodes.size() + 6); // Skip over 'continue' code.
  1377. // Next iteration.
  1378. int continue_addr = opcodes.size();
  1379. continue_addrs.push_back(continue_addr);
  1380. append_opcode(iterate_opcode);
  1381. append(counter);
  1382. append(container);
  1383. append(iterator);
  1384. for_jmp_addrs.push_back(opcodes.size());
  1385. append(0); // Jump destination, will be patched.
  1386. }
  1387. void GDScriptByteCodeGenerator::write_endfor() {
  1388. // Jump back to loop check.
  1389. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1390. append(continue_addrs.back()->get());
  1391. continue_addrs.pop_back();
  1392. // Patch end jumps (two of them).
  1393. for (int i = 0; i < 2; i++) {
  1394. patch_jump(for_jmp_addrs.back()->get());
  1395. for_jmp_addrs.pop_back();
  1396. }
  1397. // Patch break statements.
  1398. for (const int &E : current_breaks_to_patch.back()->get()) {
  1399. patch_jump(E);
  1400. }
  1401. current_breaks_to_patch.pop_back();
  1402. // Pop state.
  1403. for_iterator_variables.pop_back();
  1404. for_counter_variables.pop_back();
  1405. for_container_variables.pop_back();
  1406. }
  1407. void GDScriptByteCodeGenerator::start_while_condition() {
  1408. current_breaks_to_patch.push_back(List<int>());
  1409. continue_addrs.push_back(opcodes.size());
  1410. }
  1411. void GDScriptByteCodeGenerator::write_while(const Address &p_condition) {
  1412. // Condition check.
  1413. append_opcode(GDScriptFunction::OPCODE_JUMP_IF_NOT);
  1414. append(p_condition);
  1415. while_jmp_addrs.push_back(opcodes.size());
  1416. append(0); // End of loop address, will be patched.
  1417. }
  1418. void GDScriptByteCodeGenerator::write_endwhile() {
  1419. // Jump back to loop check.
  1420. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1421. append(continue_addrs.back()->get());
  1422. continue_addrs.pop_back();
  1423. // Patch end jump.
  1424. patch_jump(while_jmp_addrs.back()->get());
  1425. while_jmp_addrs.pop_back();
  1426. // Patch break statements.
  1427. for (const int &E : current_breaks_to_patch.back()->get()) {
  1428. patch_jump(E);
  1429. }
  1430. current_breaks_to_patch.pop_back();
  1431. }
  1432. void GDScriptByteCodeGenerator::write_break() {
  1433. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1434. current_breaks_to_patch.back()->get().push_back(opcodes.size());
  1435. append(0);
  1436. }
  1437. void GDScriptByteCodeGenerator::write_continue() {
  1438. append_opcode(GDScriptFunction::OPCODE_JUMP);
  1439. append(continue_addrs.back()->get());
  1440. }
  1441. void GDScriptByteCodeGenerator::write_breakpoint() {
  1442. append_opcode(GDScriptFunction::OPCODE_BREAKPOINT);
  1443. }
  1444. void GDScriptByteCodeGenerator::write_newline(int p_line) {
  1445. append_opcode(GDScriptFunction::OPCODE_LINE);
  1446. append(p_line);
  1447. current_line = p_line;
  1448. }
  1449. void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
  1450. if (!function->return_type.has_type || p_return_value.type.has_type) {
  1451. // Either the function is untyped or the return value is also typed.
  1452. // If this is a typed function, then we need to check for potential conversions.
  1453. if (function->return_type.has_type) {
  1454. if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1455. // Typed array.
  1456. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1457. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1458. append(p_return_value);
  1459. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1460. append(element_type.builtin_type);
  1461. append(element_type.native_type);
  1462. } 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) {
  1463. // Add conversion.
  1464. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1465. append(p_return_value);
  1466. append(function->return_type.builtin_type);
  1467. } else {
  1468. // Just assign.
  1469. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1470. append(p_return_value);
  1471. }
  1472. } else {
  1473. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1474. append(p_return_value);
  1475. }
  1476. } else {
  1477. switch (function->return_type.kind) {
  1478. case GDScriptDataType::BUILTIN: {
  1479. if (function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type()) {
  1480. const GDScriptDataType &element_type = function->return_type.get_container_element_type();
  1481. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_ARRAY);
  1482. append(p_return_value);
  1483. append(get_constant_pos(element_type.script_type) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS));
  1484. append(element_type.builtin_type);
  1485. append(element_type.native_type);
  1486. } else {
  1487. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_BUILTIN);
  1488. append(p_return_value);
  1489. append(function->return_type.builtin_type);
  1490. }
  1491. } break;
  1492. case GDScriptDataType::NATIVE: {
  1493. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_NATIVE);
  1494. append(p_return_value);
  1495. int class_idx = GDScriptLanguage::get_singleton()->get_global_map()[function->return_type.native_type];
  1496. Variant nc = GDScriptLanguage::get_singleton()->get_global_array()[class_idx];
  1497. class_idx = get_constant_pos(nc) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1498. append(class_idx);
  1499. } break;
  1500. case GDScriptDataType::GDSCRIPT:
  1501. case GDScriptDataType::SCRIPT: {
  1502. Variant script = function->return_type.script_type;
  1503. int script_idx = get_constant_pos(script) | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS);
  1504. append_opcode(GDScriptFunction::OPCODE_RETURN_TYPED_SCRIPT);
  1505. append(p_return_value);
  1506. append(script_idx);
  1507. } break;
  1508. default: {
  1509. ERR_PRINT("Compiler bug: unresolved return.");
  1510. // Shouldn't get here, but fail-safe to a regular return;
  1511. append_opcode(GDScriptFunction::OPCODE_RETURN);
  1512. append(p_return_value);
  1513. } break;
  1514. }
  1515. }
  1516. }
  1517. void GDScriptByteCodeGenerator::write_assert(const Address &p_test, const Address &p_message) {
  1518. append_opcode(GDScriptFunction::OPCODE_ASSERT);
  1519. append(p_test);
  1520. append(p_message);
  1521. }
  1522. void GDScriptByteCodeGenerator::start_block() {
  1523. push_stack_identifiers();
  1524. }
  1525. void GDScriptByteCodeGenerator::end_block() {
  1526. pop_stack_identifiers();
  1527. }
  1528. GDScriptByteCodeGenerator::~GDScriptByteCodeGenerator() {
  1529. if (!ended && function != nullptr) {
  1530. memdelete(function);
  1531. }
  1532. }